session.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /*
  3. Copyright (c) 2009-2014 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\Jig;
  12. //! Jig-managed session handler
  13. class Session extends Mapper {
  14. protected
  15. //! Session ID
  16. $sid;
  17. /**
  18. * Open session
  19. * @return TRUE
  20. * @param $path string
  21. * @param $name string
  22. **/
  23. function open($path,$name) {
  24. return TRUE;
  25. }
  26. /**
  27. * Close session
  28. * @return TRUE
  29. **/
  30. function close() {
  31. return TRUE;
  32. }
  33. /**
  34. * Return session data in serialized format
  35. * @return string|FALSE
  36. * @param $id string
  37. **/
  38. function read($id) {
  39. if ($id!=$this->sid)
  40. $this->load(array('@session_id=?',$this->sid=$id));
  41. return $this->dry()?FALSE:$this->get('data');
  42. }
  43. /**
  44. * Write session data
  45. * @return TRUE
  46. * @param $id string
  47. * @param $data string
  48. **/
  49. function write($id,$data) {
  50. $fw=\Base::instance();
  51. $sent=headers_sent();
  52. $headers=$fw->get('HEADERS');
  53. if ($id!=$this->sid)
  54. $this->load(array('@session_id=?',$this->sid=$id));
  55. $csrf=$fw->hash($fw->get('ROOT').$fw->get('BASE')).'.'.
  56. $fw->hash(mt_rand());
  57. $this->set('session_id',$id);
  58. $this->set('data',$data);
  59. $this->set('csrf',$sent?$this->csrf():$csrf);
  60. $this->set('ip',$fw->get('IP'));
  61. $this->set('agent',
  62. isset($headers['User-Agent'])?$headers['User-Agent']:'');
  63. $this->set('stamp',time());
  64. $this->save();
  65. return TRUE;
  66. }
  67. /**
  68. * Destroy session
  69. * @return TRUE
  70. * @param $id string
  71. **/
  72. function destroy($id) {
  73. $this->erase(array('@session_id=?',$id));
  74. setcookie(session_name(),'',strtotime('-1 year'));
  75. unset($_COOKIE[session_name()]);
  76. header_remove('Set-Cookie');
  77. return TRUE;
  78. }
  79. /**
  80. * Garbage collector
  81. * @return TRUE
  82. * @param $max int
  83. **/
  84. function cleanup($max) {
  85. $this->erase(array('@stamp+?<?',$max,time()));
  86. return TRUE;
  87. }
  88. /**
  89. * Return anti-CSRF token
  90. * @return string|FALSE
  91. **/
  92. function csrf() {
  93. return $this->dry()?FALSE:$this->get('csrf');
  94. }
  95. /**
  96. * Return IP address
  97. * @return string|FALSE
  98. **/
  99. function ip() {
  100. return $this->dry()?FALSE:$this->get('ip');
  101. }
  102. /**
  103. * Return Unix timestamp
  104. * @return string|FALSE
  105. **/
  106. function stamp() {
  107. return $this->dry()?FALSE:$this->get('stamp');
  108. }
  109. /**
  110. * Return HTTP user agent
  111. * @return string|FALSE
  112. **/
  113. function agent() {
  114. return $this->dry()?FALSE:$this->get('agent');
  115. }
  116. /**
  117. * Instantiate class
  118. * @param $db object
  119. * @param $table string
  120. **/
  121. function __construct(\DB\Jig $db,$table='sessions') {
  122. parent::__construct($db,'sessions');
  123. session_set_save_handler(
  124. array($this,'open'),
  125. array($this,'close'),
  126. array($this,'read'),
  127. array($this,'write'),
  128. array($this,'destroy'),
  129. array($this,'cleanup')
  130. );
  131. register_shutdown_function('session_commit');
  132. @session_start();
  133. $fw=\Base::instance();
  134. $headers=$fw->get('HEADERS');
  135. if (($ip=$this->ip()) && $ip!=$fw->get('IP') ||
  136. ($agent=$this->agent()) &&
  137. (!isset($headers['User-Agent']) ||
  138. $agent!=$headers['User-Agent'])) {
  139. session_destroy();
  140. $fw->error(403);
  141. }
  142. $csrf=$fw->hash($fw->get('ROOT').$fw->get('BASE')).'.'.
  143. $fw->hash(mt_rand());
  144. if ($this->load(array('@session_id=?',$this->sid=session_id()))) {
  145. $this->set('csrf',$csrf);
  146. $this->save();
  147. }
  148. }
  149. }