You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.0 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage Session
  7. * @since 2010-02-28
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. class SessionModel extends Model
  12. {
  13. protected $life_time;
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. $this->life_time = get_cfg_var('session.gc_maxlifetime');
  18. }
  19. /* Session handler methods */
  20. public function open($save_path, $sess_name)
  21. {
  22. return true;
  23. }
  24. public function close()
  25. {
  26. return true;
  27. }
  28. public function read($id)
  29. {
  30. $sql = 'SELECT `data` FROM ' . $this->table() . ' WHERE `id`=? AND `expires` > UNIX_TIMESTAMP()';
  31. return (string) $this->fetchField($sql, $id, 'data');
  32. }
  33. public function write($id, $data)
  34. {
  35. preg_match('/user\|.+s:2:"id";s:(\d+):"(\d+)"/', $data, $match);
  36. $user_id = empty($match) ? 0 : (int) $match[2];
  37. $sql = 'INSERT INTO ' . $this->table() . ' (`id`, `expires`, `user_id`, `data`)'
  38. . ' VALUES(' . $this->quote($id) . ', UNIX_TIMESTAMP() + ' . (int) $this->life_time . ', ' . $user_id . ', ' . $this->quote($data) . ')'
  39. . ' ON DUPLICATE KEY UPDATE `expires`=UNIX_TIMESTAMP() + ' . (int) $this->life_time . ', `user_id`=' . $user_id . ', `data`=' . $this->quote($data);
  40. $affected = $this->db->query($sql)->affectedRows();
  41. return (bool) ($this->getInsertId()) ? $this->getInsertId() : $affected;
  42. }
  43. public function destroy($id)
  44. {
  45. return (bool) $this->db->delete($this->table(false), array('`id`=?' => (string) $id));
  46. }
  47. public function gc($max_life_time)
  48. {
  49. return (bool) $this->db->delete($this->table(false), '`expires` < UNIX_TIMESTAMP()');
  50. }
  51. /* End of Session handler methods */
  52. public function destroyByUserId($user_id)
  53. {
  54. return $this->db->delete($this->table(false), array('`user_id`=?' => $user_id));
  55. }
  56. }