session.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /*
  3. Copyright (c) 2009-2013 F3::Factory/Bong Cosca, All rights reserved.
  4. This file is part of the Fat-Free Framework (http://fatfree.sf.net).
  5. THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
  6. ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  7. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  8. PURPOSE.
  9. Please see the license.txt file for more information.
  10. */
  11. namespace DB\SQL;
  12. //! SQL-managed session handler
  13. class Session extends Mapper {
  14. /**
  15. * Open session
  16. * @return TRUE
  17. * @param $path string
  18. * @param $name string
  19. **/
  20. function open($path,$name) {
  21. return TRUE;
  22. }
  23. /**
  24. * Close session
  25. * @return TRUE
  26. **/
  27. function close() {
  28. return TRUE;
  29. }
  30. /**
  31. * Return session data in serialized format
  32. * @return string|FALSE
  33. * @param $id string
  34. **/
  35. function read($id) {
  36. $this->load(array('session_id=?',$id));
  37. return $this->dry()?FALSE:$this->get('data');
  38. }
  39. /**
  40. * Write session data
  41. * @return TRUE
  42. * @param $id string
  43. * @param $data string
  44. **/
  45. function write($id,$data) {
  46. $fw=\Base::instance();
  47. $headers=$fw->get('HEADERS');
  48. $this->load(array('session_id=?',$id));
  49. $this->set('session_id',$id);
  50. $this->set('data',$data);
  51. $this->set('ip',$fw->get('IP'));
  52. $this->set('agent',
  53. isset($headers['User-Agent'])?$headers['User-Agent']:'');
  54. $this->set('stamp',time());
  55. $this->save();
  56. return TRUE;
  57. }
  58. /**
  59. * Destroy session
  60. * @return TRUE
  61. * @param $id string
  62. **/
  63. function destroy($id) {
  64. $this->erase(array('session_id=?',$id));
  65. return TRUE;
  66. }
  67. /**
  68. * Garbage collector
  69. * @return TRUE
  70. * @param $max int
  71. **/
  72. function cleanup($max) {
  73. $this->erase(array('stamp+?<?',$max,time()));
  74. return TRUE;
  75. }
  76. /**
  77. * Return IP address associated with specified session ID
  78. * @return string|FALSE
  79. * @param $id string
  80. **/
  81. function ip($id=NULL) {
  82. $this->load(array('session_id=?',$id?:session_id()));
  83. return $this->dry()?FALSE:$this->get('ip');
  84. }
  85. /**
  86. * Return Unix timestamp associated with specified session ID
  87. * @return string|FALSE
  88. * @param $id string
  89. **/
  90. function stamp($id=NULL) {
  91. $this->load(array('session_id=?',$id?:session_id()));
  92. return $this->dry()?FALSE:$this->get('stamp');
  93. }
  94. /**
  95. * Return HTTP user agent associated with specified session ID
  96. * @return string|FALSE
  97. * @param $id string
  98. **/
  99. function agent($id=NULL) {
  100. $this->load(array('session_id=?',$id?:session_id()));
  101. return $this->dry()?FALSE:$this->get('agent');
  102. }
  103. /**
  104. * Instantiate class
  105. * @param $db object
  106. * @param $table string
  107. **/
  108. function __construct(\DB\SQL $db,$table='sessions') {
  109. $db->exec(
  110. 'CREATE TABLE IF NOT EXISTS '.
  111. (($name=$db->name())?($name.'.'):'').$table.' ('.
  112. 'session_id VARCHAR(40),'.
  113. 'data TEXT,'.
  114. 'ip VARCHAR(40),'.
  115. 'agent VARCHAR(255),'.
  116. 'stamp INTEGER,'.
  117. 'PRIMARY KEY(session_id)'.
  118. ');'
  119. );
  120. parent::__construct($db,$table);
  121. session_set_save_handler(
  122. array($this,'open'),
  123. array($this,'close'),
  124. array($this,'read'),
  125. array($this,'write'),
  126. array($this,'destroy'),
  127. array($this,'cleanup')
  128. );
  129. register_shutdown_function('session_commit');
  130. }
  131. }