You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
4.8 KiB

7 years ago
  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 - 2017, 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 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://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. * Cookie destroy
  106. *
  107. * Internal method to force removal of a cookie by the client
  108. * when session_destroy() is called.
  109. *
  110. * @return bool
  111. */
  112. protected function _cookie_destroy()
  113. {
  114. return setcookie(
  115. $this->_config['cookie_name'],
  116. NULL,
  117. 1,
  118. $this->_config['cookie_path'],
  119. $this->_config['cookie_domain'],
  120. $this->_config['cookie_secure'],
  121. TRUE
  122. );
  123. }
  124. // ------------------------------------------------------------------------
  125. /**
  126. * Get lock
  127. *
  128. * A dummy method allowing drivers with no locking functionality
  129. * (databases other than PostgreSQL and MySQL) to act as if they
  130. * do acquire a lock.
  131. *
  132. * @param string $session_id
  133. * @return bool
  134. */
  135. protected function _get_lock($session_id)
  136. {
  137. $this->_lock = TRUE;
  138. return TRUE;
  139. }
  140. // ------------------------------------------------------------------------
  141. /**
  142. * Release lock
  143. *
  144. * @return bool
  145. */
  146. protected function _release_lock()
  147. {
  148. if ($this->_lock)
  149. {
  150. $this->_lock = FALSE;
  151. }
  152. return TRUE;
  153. }
  154. // ------------------------------------------------------------------------
  155. /**
  156. * Fail
  157. *
  158. * Drivers other than the 'files' one don't (need to) use the
  159. * session.save_path INI setting, but that leads to confusing
  160. * error messages emitted by PHP when open() or write() fail,
  161. * as the message contains session.save_path ...
  162. * To work around the problem, the drivers will call this method
  163. * so that the INI is set just in time for the error message to
  164. * be properly generated.
  165. *
  166. * @return mixed
  167. */
  168. protected function _fail()
  169. {
  170. ini_set('session.save_path', config_item('sess_save_path'));
  171. return $this->_failure;
  172. }
  173. }