/framework/core/session/SessionDb.php

http://zoop.googlecode.com/ · PHP · 116 lines · 94 code · 18 blank · 4 comment · 6 complexity · 9d13d0c448bb40cc9f84172fb7d8eecb MD5 · raw file

  1. <?php
  2. class SessionDb
  3. {
  4. var $db;
  5. var $style;
  6. var $saveChanges;
  7. var $sessionName;
  8. var $sessionId;
  9. public function __construct($params)
  10. {
  11. $this->db = DbModule::getConnection($params['db_connection']);
  12. $this->style = $params['style'];
  13. }
  14. // user functions
  15. function start()
  16. {
  17. $this->saveChanges = 0;
  18. session_set_save_handler(array($this, 'open'), array($this, 'close'), array($this, 'read'), array($this, 'write'), array($this, 'destroy'), array($this, 'gc'));
  19. session_start();
  20. }
  21. function get($key = '__default__')
  22. {
  23. $row = $this->db->selsertRow('session_data', array('value'), array('session_id' => $this->sessionId, 'key' => $key));
  24. if($row['value'])
  25. return $row['value'];
  26. else
  27. return NULL;
  28. }
  29. function getWithLock($key = '__default__')
  30. {
  31. }
  32. function set($value, $key = '__default__')
  33. {
  34. $this->db->upsertRow('session_data', array('session_id' => $this->sessionId, 'key' => $key), array('value' => $value));
  35. }
  36. function saveChanges()
  37. {
  38. if(session_style != 'serialize_manaul')
  39. trigger_error("this function is only used with session_style = 'serialize_manaual'");
  40. $this->saveChanges = 1;
  41. }
  42. function saveChangesUnsafe()
  43. {
  44. if($this->style != 'write_manual')
  45. trigger_error("this function is only used with session_style = 'write_manaual'");
  46. $this->saveChanges = 1;
  47. }
  48. // callbacks
  49. function open($savePath, $sessionName)
  50. {
  51. $this->sessionName = $sessionName;
  52. return true;
  53. }
  54. function close()
  55. {
  56. return true;
  57. }
  58. function read($sessionId)
  59. {
  60. $this->sessionId = $sessionId;
  61. // make sure the session_base record is in there
  62. $this->db->upsertRow('session_base', array('session_id' => $sessionId), array('last_active:keyword' => 'CURRENT_TIMESTAMP'));
  63. $data = $this->get();
  64. // get the default row
  65. return $data;
  66. }
  67. function write($sessionId, $sessionData)
  68. {
  69. switch($this->style)
  70. {
  71. case 'serialize_manual':
  72. case 'write_manual':
  73. if($this->saveChanges)
  74. $this->set($sessionData);
  75. break;
  76. default:
  77. trigger_error("session style '" . session_style . "' not yet implemented");
  78. break;
  79. }
  80. return true;
  81. }
  82. function destroy($sessionId)
  83. {
  84. $this->db->beginTransaction();
  85. $this->db->deleteRows("delete from session_data where session_id = :session_id", array('session_id' => $sessionId));
  86. $this->db->deleteRow("delete from session_base where session_id = :session_id", array('session_id' => $sessionId));
  87. $this->db->commitTransaction();
  88. return true;
  89. }
  90. function gc($maxLifetime)
  91. {
  92. $this->db->beginTransaction();
  93. $this->db->deleteRows("delete from session_base where extract(epoch from CURRENT_TIMESTAMP - last_active) > :maxLifetime", array('maxLifetime' => $maxLifetime));
  94. $this->db->deleteRows("delete from session_data where session_id not in (select session_id from session_base)", array());
  95. $this->db->commitTransaction();
  96. return true;
  97. }
  98. }