Session_driver.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
  33. * @license https://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 3.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * CodeIgniter Session Driver Class
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Libraries
  44. * @category Sessions
  45. * @author Andrey Andreev
  46. * @link https://codeigniter.com/user_guide/libraries/sessions.html
  47. */
  48. abstract class CI_Session_driver implements SessionHandlerInterface {
  49. protected $_config;
  50. /**
  51. * Data fingerprint
  52. *
  53. * @var bool
  54. */
  55. protected $_fingerprint;
  56. /**
  57. * Lock placeholder
  58. *
  59. * @var mixed
  60. */
  61. protected $_lock = FALSE;
  62. /**
  63. * Read session ID
  64. *
  65. * Used to detect session_regenerate_id() calls because PHP only calls
  66. * write() after regenerating the ID.
  67. *
  68. * @var string
  69. */
  70. protected $_session_id;
  71. /**
  72. * Success and failure return values
  73. *
  74. * Necessary due to a bug in all PHP 5 versions where return values
  75. * from userspace handlers are not handled properly. PHP 7 fixes the
  76. * bug, so we need to return different values depending on the version.
  77. *
  78. * @see https://wiki.php.net/rfc/session.user.return-value
  79. * @var mixed
  80. */
  81. protected $_success, $_failure;
  82. // ------------------------------------------------------------------------
  83. /**
  84. * Class constructor
  85. *
  86. * @param array $params Configuration parameters
  87. * @return void
  88. */
  89. public function __construct(&$params)
  90. {
  91. $this->_config =& $params;
  92. if (is_php('7'))
  93. {
  94. $this->_success = TRUE;
  95. $this->_failure = FALSE;
  96. }
  97. else
  98. {
  99. $this->_success = 0;
  100. $this->_failure = -1;
  101. }
  102. }
  103. // ------------------------------------------------------------------------
  104. /**
  105. * PHP 5.x validate ID
  106. *
  107. * Enforces session.use_strict_mode
  108. *
  109. * @return void
  110. */
  111. public function php5_validate_id()
  112. {
  113. if (isset($_COOKIE[$this->_config['cookie_name']]) && ! $this->validateSessionId($_COOKIE[$this->_config['cookie_name']]))
  114. {
  115. unset($_COOKIE[$this->_config['cookie_name']]);
  116. }
  117. }
  118. // ------------------------------------------------------------------------
  119. /**
  120. * Cookie destroy
  121. *
  122. * Internal method to force removal of a cookie by the client
  123. * when session_destroy() is called.
  124. *
  125. * @return bool
  126. */
  127. protected function _cookie_destroy()
  128. {
  129. return setcookie(
  130. $this->_config['cookie_name'],
  131. NULL,
  132. 1,
  133. $this->_config['cookie_path'],
  134. $this->_config['cookie_domain'],
  135. $this->_config['cookie_secure'],
  136. TRUE
  137. );
  138. }
  139. // ------------------------------------------------------------------------
  140. /**
  141. * Get lock
  142. *
  143. * A dummy method allowing drivers with no locking functionality
  144. * (databases other than PostgreSQL and MySQL) to act as if they
  145. * do acquire a lock.
  146. *
  147. * @param string $session_id
  148. * @return bool
  149. */
  150. protected function _get_lock($session_id)
  151. {
  152. $this->_lock = TRUE;
  153. return TRUE;
  154. }
  155. // ------------------------------------------------------------------------
  156. /**
  157. * Release lock
  158. *
  159. * @return bool
  160. */
  161. protected function _release_lock()
  162. {
  163. if ($this->_lock)
  164. {
  165. $this->_lock = FALSE;
  166. }
  167. return TRUE;
  168. }
  169. }