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
1.8 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. */
  9. class SessionModel extends SqlModel
  10. {
  11. protected $life_time;
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. $this->life_time = get_cfg_var('session.gc_maxlifetime');
  16. }
  17. /* Session handler methods */
  18. public function open($save_path, $sess_name)
  19. {
  20. return true;
  21. }
  22. public function close()
  23. {
  24. return true;
  25. }
  26. public function read($id)
  27. {
  28. $sql = 'SELECT `data` FROM :table WHERE `id`=? AND `expires` > UNIX_TIMESTAMP()';
  29. return (string) $this->fetchField($sql, $id, 'data');
  30. }
  31. public function write($id, $data)
  32. {
  33. preg_match('/user\|.+s:2:"id";s:(\d+):"(\d+)"/', $data, $match);
  34. $user_id = empty($match) ? 0 : (int) $match[2];
  35. $ip = Env::Server('HTTP_X_FORWARDED_FOR', Env::Server('REMOTE_ADDR'));
  36. $update = array(
  37. 'expires' => new DbExpr('UNIX_TIMESTAMP() + ' . (int) $this->life_time),
  38. 'ip' => new DbExpr('INET_ATON(' . $this->quote($ip) . ')'),
  39. 'user_id' => $user_id,
  40. 'data' => $data
  41. );
  42. return (bool) $this->insert(array('id' => $id) + $update, $update);
  43. }
  44. public function destroy($id)
  45. {
  46. return (bool) $this->db->delete($this->table(), array('`id`=?' => (string) $id));
  47. }
  48. public function gc($max_life_time)
  49. {
  50. return (bool) $this->db->delete($this->table(), '`expires` < UNIX_TIMESTAMP()');
  51. }
  52. /* End of Session handler methods */
  53. public function destroyByUserId($user_id)
  54. {
  55. return $this->db->delete($this->table(), array('`user_id`=?' => $user_id));
  56. }
  57. }