PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/db/mongo/session.php

http://github.com/bcosca/fatfree
PHP | 194 lines | 92 code | 17 blank | 85 comment | 6 complexity | 9ff938c556c489c709e4bba6fb9cad6b MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. Copyright (c) 2009-2019 F3::Factory/Bong Cosca, All rights reserved.
  4. This file is part of the Fat-Free Framework (http://fatfreeframework.com).
  5. This is free software: you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation, either version 3 of the License, or later.
  8. Fat-Free Framework is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with Fat-Free Framework. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. namespace DB\Mongo;
  16. //! MongoDB-managed session handler
  17. class Session extends Mapper {
  18. protected
  19. //! Session ID
  20. $sid,
  21. //! Anti-CSRF token
  22. $_csrf,
  23. //! User agent
  24. $_agent,
  25. //! IP,
  26. $_ip,
  27. //! Suspect callback
  28. $onsuspect;
  29. /**
  30. * Open session
  31. * @return TRUE
  32. * @param $path string
  33. * @param $name string
  34. **/
  35. function open($path,$name) {
  36. return TRUE;
  37. }
  38. /**
  39. * Close session
  40. * @return TRUE
  41. **/
  42. function close() {
  43. $this->reset();
  44. $this->sid=NULL;
  45. return TRUE;
  46. }
  47. /**
  48. * Return session data in serialized format
  49. * @return string
  50. * @param $id string
  51. **/
  52. function read($id) {
  53. $this->load(['session_id'=>$this->sid=$id]);
  54. if ($this->dry())
  55. return '';
  56. if ($this->get('ip')!=$this->_ip || $this->get('agent')!=$this->_agent) {
  57. $fw=\Base::instance();
  58. if (!isset($this->onsuspect) ||
  59. $fw->call($this->onsuspect,[$this,$id])===FALSE) {
  60. // NB: `session_destroy` can't be called at that stage;
  61. // `session_start` not completed
  62. $this->destroy($id);
  63. $this->close();
  64. unset($fw->{'COOKIE.'.session_name()});
  65. $fw->error(403);
  66. }
  67. }
  68. return $this->get('data');
  69. }
  70. /**
  71. * Write session data
  72. * @return TRUE
  73. * @param $id string
  74. * @param $data string
  75. **/
  76. function write($id,$data) {
  77. $this->set('session_id',$id);
  78. $this->set('data',$data);
  79. $this->set('ip',$this->_ip);
  80. $this->set('agent',$this->_agent);
  81. $this->set('stamp',time());
  82. $this->save();
  83. return TRUE;
  84. }
  85. /**
  86. * Destroy session
  87. * @return TRUE
  88. * @param $id string
  89. **/
  90. function destroy($id) {
  91. $this->erase(['session_id'=>$id]);
  92. return TRUE;
  93. }
  94. /**
  95. * Garbage collector
  96. * @return TRUE
  97. * @param $max int
  98. **/
  99. function cleanup($max) {
  100. $this->erase(['$where'=>'this.stamp+'.$max.'<'.time()]);
  101. return TRUE;
  102. }
  103. /**
  104. * Return session id (if session has started)
  105. * @return string|NULL
  106. **/
  107. function sid() {
  108. return $this->sid;
  109. }
  110. /**
  111. * Return anti-CSRF token
  112. * @return string
  113. **/
  114. function csrf() {
  115. return $this->_csrf;
  116. }
  117. /**
  118. * Return IP address
  119. * @return string
  120. **/
  121. function ip() {
  122. return $this->_ip;
  123. }
  124. /**
  125. * Return Unix timestamp
  126. * @return string|FALSE
  127. **/
  128. function stamp() {
  129. if (!$this->sid)
  130. session_start();
  131. return $this->dry()?FALSE:$this->get('stamp');
  132. }
  133. /**
  134. * Return HTTP user agent
  135. * @return string
  136. **/
  137. function agent() {
  138. return $this->_agent;
  139. }
  140. /**
  141. * Instantiate class
  142. * @param $db \DB\Mongo
  143. * @param $table string
  144. * @param $onsuspect callback
  145. * @param $key string
  146. **/
  147. function __construct(\DB\Mongo $db,$table='sessions',$onsuspect=NULL,$key=NULL) {
  148. parent::__construct($db,$table);
  149. $this->onsuspect=$onsuspect;
  150. session_set_save_handler(
  151. [$this,'open'],
  152. [$this,'close'],
  153. [$this,'read'],
  154. [$this,'write'],
  155. [$this,'destroy'],
  156. [$this,'cleanup']
  157. );
  158. register_shutdown_function('session_commit');
  159. $fw=\Base::instance();
  160. $headers=$fw->HEADERS;
  161. $this->_csrf=$fw->hash($fw->SEED.
  162. extension_loaded('openssl')?
  163. implode(unpack('L',openssl_random_pseudo_bytes(4))):
  164. mt_rand()
  165. );
  166. if ($key)
  167. $fw->$key=$this->_csrf;
  168. $this->_agent=isset($headers['User-Agent'])?$headers['User-Agent']:'';
  169. $this->_ip=$fw->IP;
  170. }
  171. }