blob: bef5ee41f49e7c1dc8e540d46f01421b362231e6 [file] [log] [blame]
Andrey Andreev47a47fb2014-05-31 16:08:30 +03001<?php
2/**
3 * CodeIgniter
4 *
Ivan Tcholakov90da83c2015-01-19 17:23:08 +02005 * An open source application development framework for PHP
Andrey Andreev47a47fb2014-05-31 16:08:30 +03006 *
Andrey Andreev46f2f262014-11-11 14:37:51 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev47a47fb2014-05-31 16:08:30 +03008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreev47a47fb2014-05-31 16:08:30 +030010 *
Andrey Andreev46f2f262014-11-11 14:37:51 +020011 * 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:
Andrey Andreev47a47fb2014-05-31 16:08:30 +030017 *
Andrey Andreev46f2f262014-11-11 14:37:51 +020018 * 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
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreev46f2f262014-11-11 14:37:51 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreev46f2f262014-11-11 14:37:51 +020035 * @since Version 3.0.0
Andrey Andreev47a47fb2014-05-31 16:08:30 +030036 * @filesource
37 */
38defined('BASEPATH') OR exit('No direct script access allowed');
39
40/**
41 * CodeIgniter Session Driver Class
42 *
Andrey Andreev46f2f262014-11-11 14:37:51 +020043 * @package CodeIgniter
Andrey Andreev47a47fb2014-05-31 16:08:30 +030044 * @subpackage Libraries
45 * @category Sessions
Andrey Andreev46f2f262014-11-11 14:37:51 +020046 * @author Andrey Andreev
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/libraries/sessions.html
Andrey Andreev47a47fb2014-05-31 16:08:30 +030048 */
49abstract class CI_Session_driver implements SessionHandlerInterface {
50
Andrey Andreevdfb39be2014-10-06 01:50:14 +030051 protected $_config;
Andrey Andreev47a47fb2014-05-31 16:08:30 +030052
53 /**
Andrey Andreev93d9fa72014-08-27 22:14:36 +030054 * Data fingerprint
Andrey Andreev47a47fb2014-05-31 16:08:30 +030055 *
56 * @var bool
57 */
58 protected $_fingerprint;
59
Andrey Andreev93d9fa72014-08-27 22:14:36 +030060 /**
61 * Lock placeholder
62 *
63 * @var mixed
64 */
65 protected $_lock = FALSE;
66
Andrey Andreev7474a672014-10-31 23:35:32 +020067 /**
68 * Read session ID
69 *
70 * Used to detect session_regenerate_id() calls because PHP only calls
71 * write() after regenerating the ID.
72 *
73 * @var string
74 */
75 protected $_session_id;
76
Andrey Andreevaf849692015-12-12 14:07:39 +020077 /**
78 * Success and failure return values
79 *
80 * Necessary due to a bug in all PHP 5 versions where return values
81 * from userspace handlers are not handled properly. PHP 7 fixes the
82 * bug, so we need to return different values depending on the version.
83 *
84 * @see https://wiki.php.net/rfc/session.user.return-value
85 * @var mixed
86 */
87 protected $_success, $_failure;
88
Andrey Andreev47a47fb2014-05-31 16:08:30 +030089 // ------------------------------------------------------------------------
90
91 /**
92 * Class constructor
93 *
94 * @param array $params Configuration parameters
95 * @return void
96 */
Andrey Andreevdfb39be2014-10-06 01:50:14 +030097 public function __construct(&$params)
Andrey Andreev47a47fb2014-05-31 16:08:30 +030098 {
Andrey Andreevdfb39be2014-10-06 01:50:14 +030099 $this->_config =& $params;
Andrey Andreevaf849692015-12-12 14:07:39 +0200100
101 if (is_php('7'))
102 {
103 $this->_success = TRUE;
104 $this->_failure = FALSE;
105 }
106 else
107 {
108 $this->_success = 0;
109 $this->_failure = -1;
110 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300111 }
112
113 // ------------------------------------------------------------------------
114
Andrey Andreev10411fc2015-01-19 13:54:53 +0200115 /**
116 * Cookie destroy
117 *
118 * Internal method to force removal of a cookie by the client
119 * when session_destroy() is called.
120 *
121 * @return bool
122 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300123 protected function _cookie_destroy()
124 {
125 return setcookie(
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300126 $this->_config['cookie_name'],
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300127 NULL,
128 1,
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300129 $this->_config['cookie_path'],
130 $this->_config['cookie_domain'],
131 $this->_config['cookie_secure'],
132 TRUE
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300133 );
134 }
135
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300136 // ------------------------------------------------------------------------
137
138 /**
139 * Get lock
140 *
Andrey Andreev19c25242015-01-14 22:13:36 +0200141 * A dummy method allowing drivers with no locking functionality
142 * (databases other than PostgreSQL and MySQL) to act as if they
143 * do acquire a lock.
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300144 *
145 * @param string $session_id
146 * @return bool
147 */
148 protected function _get_lock($session_id)
149 {
Andrey Andreev19c25242015-01-14 22:13:36 +0200150 $this->_lock = TRUE;
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300151 return TRUE;
152 }
153
154 // ------------------------------------------------------------------------
155
156 /**
157 * Release lock
158 *
159 * @return bool
160 */
161 protected function _release_lock()
162 {
Andrey Andreev19c25242015-01-14 22:13:36 +0200163 if ($this->_lock)
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300164 {
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300165 $this->_lock = FALSE;
166 }
167
168 return TRUE;
169 }
170
Andrey Andreeva027a7f2016-03-10 13:59:20 +0200171 // ------------------------------------------------------------------------
172
173 /**
174 * Fail
175 *
176 * Drivers other than the 'files' one don't (need to) use the
177 * session.save_path INI setting, but that leads to confusing
178 * error messages emitted by PHP when open() or write() fail,
179 * as the message contains session.save_path ...
180 * To work around the problem, the drivers will call this method
181 * so that the INI is set just in time for the error message to
182 * be properly generated.
183 *
184 * @return mixed
185 */
186 protected function _fail()
187 {
188 ini_set('session.save_path', config_item('sess_save_path'));
189 return $this->_failure;
190 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300191}