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.

73 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. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. class SessionModel extends SqlModel
  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 :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. $update = array(
  39. 'expires' => new DbExpr('UNIX_TIMESTAMP() + ' . (int) $this->life_time),
  40. 'ip' => new DbExpr('INET_ATON(' . $this->quote($ip) . ')'),
  41. 'user_id' => $user_id,
  42. 'data' => $data
  43. );
  44. return (bool) $this->insert(array('id' => $id) + $update, $update);
  45. }
  46. public function destroy($id)
  47. {
  48. return (bool) $this->db->delete($this->table(), array('`id`=?' => (string) $id));
  49. }
  50. public function gc($max_life_time)
  51. {
  52. return (bool) $this->db->delete($this->table(), '`expires` < UNIX_TIMESTAMP()');
  53. }
  54. /* End of Session handler methods */
  55. public function destroyByUserId($user_id)
  56. {
  57. return $this->db->delete($this->table(), array('`user_id`=?' => $user_id));
  58. }
  59. }