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.

71 lines
2.2 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. $ip = Env::Server('HTTP_X_FORWARDED_FOR', Env::Server('REMOTE_ADDR'));
  38. $sql = 'INSERT INTO ' . $this->table() . ' (`id`, `expires`, `ip`, `user_id`, `data`)'
  39. . ' VALUES(' . $this->quote($id) . ', UNIX_TIMESTAMP() + ' . (int) $this->life_time . ', INET_ATON(' . $this->quote($ip) . '), ' . $user_id . ', ' . $this->quote($data) . ')'
  40. . ' ON DUPLICATE KEY UPDATE `expires`=UNIX_TIMESTAMP() + ' . (int) $this->life_time . ', `ip`=INET_ATON(' . $this->quote($ip) . '), `user_id`=' . $user_id . ', `data`=' . $this->quote($data);
  41. $affected = $this->db->query($sql)->affectedRows();
  42. return (bool) ($this->getInsertId()) ? $this->getInsertId() : $affected;
  43. }
  44. public function destroy($id)
  45. {
  46. return (bool) $this->db->delete($this->table(false), array('`id`=?' => (string) $id));
  47. }
  48. public function gc($max_life_time)
  49. {
  50. return (bool) $this->db->delete($this->table(false), '`expires` < UNIX_TIMESTAMP()');
  51. }
  52. /* End of Session handler methods */
  53. public function destroyByUserId($user_id)
  54. {
  55. return $this->db->delete($this->table(false), array('`user_id`=?' => $user_id));
  56. }
  57. }