Subversion-Projekte lars-tiefland.ci

Revision

Revision 2257 | Ganze Datei anzeigen | Leerzeichen ignorieren | Details | Blame | Letzte Änderung | Log anzeigen | RSS feed

Revision 2257 Revision 2414
Zeile 4... Zeile 4...
4
 *
4
 *
5
 * An open source application development framework for PHP
5
 * An open source application development framework for PHP
6
 *
6
 *
7
 * This content is released under the MIT License (MIT)
7
 * This content is released under the MIT License (MIT)
8
 *
8
 *
9
 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
9
 * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
10
 *
10
 *
11
 * Permission is hereby granted, free of charge, to any person obtaining a copy
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
12
 * of this software and associated documentation files (the "Software"), to deal
13
 * in the Software without restriction, including without limitation the rights
13
 * in the Software without restriction, including without limitation the rights
14
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Zeile 27... Zeile 27...
27
 * THE SOFTWARE.
27
 * THE SOFTWARE.
28
 *
28
 *
29
 * @package	CodeIgniter
29
 * @package	CodeIgniter
30
 * @author	EllisLab Dev Team
30
 * @author	EllisLab Dev Team
31
 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
31
 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
32
 * @copyright	Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
32
 * @copyright	Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33
 * @license	http://opensource.org/licenses/MIT	MIT License
33
 * @license	https://opensource.org/licenses/MIT	MIT License
34
 * @link	https://codeigniter.com
34
 * @link	https://codeigniter.com
35
 * @since	Version 3.0.0
35
 * @since	Version 3.0.0
36
 * @filesource
36
 * @filesource
37
 */
37
 */
38
defined('BASEPATH') OR exit('No direct script access allowed');
38
defined('BASEPATH') OR exit('No direct script access allowed');
Zeile 74... Zeile 74...
74
	 *
74
	 *
75
	 * @var bool
75
	 * @var bool
76
	 */
76
	 */
77
	protected $_key_exists = FALSE;
77
	protected $_key_exists = FALSE;
Zeile -... Zeile 78...
-
 
78
 
-
 
79
	/**
-
 
80
	 * Name of setTimeout() method in phpRedis
-
 
81
	 *
-
 
82
	 * Due to some deprecated methods in phpRedis, we need to call the
-
 
83
	 * specific methods depending on the version of phpRedis.
-
 
84
	 *
-
 
85
	 * @var string
-
 
86
	 */
-
 
87
	protected $_setTimeout_name;
-
 
88
 
-
 
89
	/**
-
 
90
	 * Name of delete() method in phpRedis
-
 
91
	 *
-
 
92
	 * Due to some deprecated methods in phpRedis, we need to call the
-
 
93
	 * specific methods depending on the version of phpRedis.
-
 
94
	 *
-
 
95
	 * @var string
-
 
96
	 */
-
 
97
	protected $_delete_name;
-
 
98
 
-
 
99
	/**
-
 
100
	 * Success return value of ping() method in phpRedis
-
 
101
	 *
-
 
102
	 * @var mixed
-
 
103
	 */
-
 
104
	protected $_ping_success;
78
 
105
 
Zeile 79... Zeile 106...
79
	// ------------------------------------------------------------------------
106
	// ------------------------------------------------------------------------
80
 
107
 
81
	/**
108
	/**
Zeile 86... Zeile 113...
86
	 */
113
	 */
87
	public function __construct(&$params)
114
	public function __construct(&$params)
88
	{
115
	{
89
		parent::__construct($params);
116
		parent::__construct($params);
Zeile -... Zeile 117...
-
 
117
 
-
 
118
		// Detect the names of some methods in phpRedis instance
-
 
119
		if (version_compare(phpversion('redis'), '5', '>='))
-
 
120
		{
-
 
121
			$this->_setTimeout_name = 'expire';
-
 
122
			$this->_delete_name = 'del';
-
 
123
			$this->_ping_success = TRUE;
-
 
124
		}
-
 
125
		else
-
 
126
		{
-
 
127
			$this->_setTimeout_name = 'setTimeout';
-
 
128
			$this->_delete_name = 'delete';
-
 
129
			$this->_ping_success = '+PONG';
-
 
130
		}
90
 
131
 
91
		if (empty($this->_config['save_path']))
132
		if (empty($this->_config['save_path']))
92
		{
133
		{
93
			log_message('error', 'Session: No Redis save path configured.');
134
			log_message('error', 'Session: No Redis save path configured.');
94
		}
135
		}
Zeile 129... Zeile 170...
129
	 */
170
	 */
130
	public function open($save_path, $name)
171
	public function open($save_path, $name)
131
	{
172
	{
132
		if (empty($this->_config['save_path']))
173
		if (empty($this->_config['save_path']))
133
		{
174
		{
134
			return $this->_fail();
175
			return $this->_failure;
135
		}
176
		}
Zeile 136... Zeile 177...
136
 
177
 
137
		$redis = new Redis();
178
		$redis = new Redis();
138
		if ( ! $redis->connect($this->_config['save_path']['host'], $this->_config['save_path']['port'], $this->_config['save_path']['timeout']))
179
		if ( ! $redis->connect($this->_config['save_path']['host'], $this->_config['save_path']['port'], $this->_config['save_path']['timeout']))
Zeile 148... Zeile 189...
148
			log_message('error', 'Session: Unable to select Redis database with index '.$this->_config['save_path']['database']);
189
			log_message('error', 'Session: Unable to select Redis database with index '.$this->_config['save_path']['database']);
149
		}
190
		}
150
		else
191
		else
151
		{
192
		{
152
			$this->_redis = $redis;
193
			$this->_redis = $redis;
-
 
194
			$this->php5_validate_id();
153
			return $this->_success;
195
			return $this->_success;
154
		}
196
		}
Zeile 155... Zeile 197...
155
 
197
 
156
		return $this->_fail();
198
		return $this->_failure;
Zeile 157... Zeile 199...
157
	}
199
	}
Zeile 158... Zeile 200...
158
 
200
 
Zeile 181... Zeile 223...
181
 
223
 
182
			$this->_fingerprint = md5($session_data);
224
			$this->_fingerprint = md5($session_data);
183
			return $session_data;
225
			return $session_data;
Zeile 184... Zeile 226...
184
		}
226
		}
185
 
227
 
Zeile 186... Zeile 228...
186
		return $this->_fail();
228
		return $this->_failure;
Zeile 187... Zeile 229...
187
	}
229
	}
Zeile 199... Zeile 241...
199
	 */
241
	 */
200
	public function write($session_id, $session_data)
242
	public function write($session_id, $session_data)
201
	{
243
	{
202
		if ( ! isset($this->_redis, $this->_lock_key))
244
		if ( ! isset($this->_redis, $this->_lock_key))
203
		{
245
		{
204
			return $this->_fail();
246
			return $this->_failure;
205
		}
247
		}
206
		// Was the ID regenerated?
248
		// Was the ID regenerated?
207
		elseif ($session_id !== $this->_session_id)
249
		elseif ($session_id !== $this->_session_id)
208
		{
250
		{
209
			if ( ! $this->_release_lock() OR ! $this->_get_lock($session_id))
251
			if ( ! $this->_release_lock() OR ! $this->_get_lock($session_id))
210
			{
252
			{
211
				return $this->_fail();
253
				return $this->_failure;
212
			}
254
			}
Zeile 213... Zeile 255...
213
 
255
 
214
			$this->_key_exists = FALSE;
256
			$this->_key_exists = FALSE;
215
			$this->_session_id = $session_id;
257
			$this->_session_id = $session_id;
Zeile 216... Zeile 258...
216
		}
258
		}
217
 
259
 
218
		$this->_redis->setTimeout($this->_lock_key, 300);
260
		$this->_redis->{$this->_setTimeout_name}($this->_lock_key, 300);
219
		if ($this->_fingerprint !== ($fingerprint = md5($session_data)) OR $this->_key_exists === FALSE)
261
		if ($this->_fingerprint !== ($fingerprint = md5($session_data)) OR $this->_key_exists === FALSE)
220
		{
262
		{
221
			if ($this->_redis->set($this->_key_prefix.$session_id, $session_data, $this->_config['expiration']))
263
			if ($this->_redis->set($this->_key_prefix.$session_id, $session_data, $this->_config['expiration']))
222
			{
264
			{
223
				$this->_fingerprint = $fingerprint;
265
				$this->_fingerprint = $fingerprint;
224
				$this->_key_exists = TRUE;
266
				$this->_key_exists = TRUE;
Zeile 225... Zeile 267...
225
				return $this->_success;
267
				return $this->_success;
226
			}
268
			}
Zeile 227... Zeile 269...
227
 
269
 
228
			return $this->_fail();
270
			return $this->_failure;
229
		}
271
		}
230
 
272
 
Zeile 231... Zeile 273...
231
		return ($this->_redis->setTimeout($this->_key_prefix.$session_id, $this->_config['expiration']))
273
		return ($this->_redis->{$this->_setTimeout_name}($this->_key_prefix.$session_id, $this->_config['expiration']))
Zeile 232... Zeile 274...
232
			? $this->_success
274
			? $this->_success
Zeile 245... Zeile 287...
245
	public function close()
287
	public function close()
246
	{
288
	{
247
		if (isset($this->_redis))
289
		if (isset($this->_redis))
248
		{
290
		{
249
			try {
291
			try {
250
				if ($this->_redis->ping() === '+PONG')
292
				if ($this->_redis->ping() === $this->_ping_success)
251
				{
293
				{
252
					$this->_release_lock();
294
					$this->_release_lock();
253
					if ($this->_redis->close() === FALSE)
295
					if ($this->_redis->close() === FALSE)
254
					{
296
					{
255
						return $this->_fail();
297
						return $this->_failure;
256
					}
298
					}
257
				}
299
				}
258
			}
300
			}
259
			catch (RedisException $e)
301
			catch (RedisException $e)
260
			{
302
			{
Zeile 280... Zeile 322...
280
	 */
322
	 */
281
	public function destroy($session_id)
323
	public function destroy($session_id)
282
	{
324
	{
283
		if (isset($this->_redis, $this->_lock_key))
325
		if (isset($this->_redis, $this->_lock_key))
284
		{
326
		{
285
			if (($result = $this->_redis->delete($this->_key_prefix.$session_id)) !== 1)
327
			if (($result = $this->_redis->{$this->_delete_name}($this->_key_prefix.$session_id)) !== 1)
286
			{
328
			{
287
				log_message('debug', 'Session: Redis::delete() expected to return 1, got '.var_export($result, TRUE).' instead.');
329
				log_message('debug', 'Session: Redis::'.$this->_delete_name.'() expected to return 1, got '.var_export($result, TRUE).' instead.');
288
			}
330
			}
Zeile 289... Zeile 331...
289
 
331
 
290
			$this->_cookie_destroy();
332
			$this->_cookie_destroy();
291
			return $this->_success;
333
			return $this->_success;
Zeile 292... Zeile 334...
292
		}
334
		}
293
 
335
 
Zeile 294... Zeile 336...
294
		return $this->_fail();
336
		return $this->_failure;
Zeile 295... Zeile 337...
295
	}
337
	}
Zeile 308... Zeile 350...
308
	{
350
	{
309
		// Not necessary, Redis takes care of that.
351
		// Not necessary, Redis takes care of that.
310
		return $this->_success;
352
		return $this->_success;
311
	}
353
	}
Zeile -... Zeile 354...
-
 
354
 
-
 
355
	// --------------------------------------------------------------------
-
 
356
 
-
 
357
	/**
-
 
358
	 * Validate ID
-
 
359
	 *
-
 
360
	 * Checks whether a session ID record exists server-side,
-
 
361
	 * to enforce session.use_strict_mode.
-
 
362
	 *
-
 
363
	 * @param	string	$id
-
 
364
	 * @return	bool
-
 
365
	 */
-
 
366
	public function validateSessionId($id)
-
 
367
	{
-
 
368
		return (bool) $this->_redis->exists($this->_key_prefix.$id);
-
 
369
	}
312
 
370
 
Zeile 313... Zeile 371...
313
	// ------------------------------------------------------------------------
371
	// ------------------------------------------------------------------------
314
 
372
 
315
	/**
373
	/**
Zeile 325... Zeile 383...
325
		// PHP 7 reuses the SessionHandler object on regeneration,
383
		// PHP 7 reuses the SessionHandler object on regeneration,
326
		// so we need to check here if the lock key is for the
384
		// so we need to check here if the lock key is for the
327
		// correct session ID.
385
		// correct session ID.
328
		if ($this->_lock_key === $this->_key_prefix.$session_id.':lock')
386
		if ($this->_lock_key === $this->_key_prefix.$session_id.':lock')
329
		{
387
		{
330
			return $this->_redis->setTimeout($this->_lock_key, 300);
388
			return $this->_redis->{$this->_setTimeout_name}($this->_lock_key, 300);
331
		}
389
		}
Zeile 332... Zeile 390...
332
 
390
 
333
		// 30 attempts to obtain a lock, in case another request already has it
391
		// 30 attempts to obtain a lock, in case another request already has it
334
		$lock_key = $this->_key_prefix.$session_id.':lock';
392
		$lock_key = $this->_key_prefix.$session_id.':lock';
Zeile 339... Zeile 397...
339
			{
397
			{
340
				sleep(1);
398
				sleep(1);
341
				continue;
399
				continue;
342
			}
400
			}
Zeile 343... Zeile -...
343
 
-
 
344
			$result = ($ttl === -2)
401
 
-
 
402
			if ($ttl === -2 && ! $this->_redis->set($lock_key, time(), array('nx', 'ex' => 300)))
345
				? $this->_redis->set($lock_key, time(), array('nx', 'ex' => 300))
403
			{
-
 
404
				// Sleep for 1s to wait for lock releases.
-
 
405
				sleep(1);
346
				: $this->_redis->setex($lock_key, 300, time());
406
				continue;
347
 
407
			}
348
			if ( ! $result)
408
			elseif ( ! $this->_redis->setex($lock_key, 300, time()))
349
			{
409
			{
350
				log_message('error', 'Session: Error while trying to obtain lock for '.$this->_key_prefix.$session_id);
410
				log_message('error', 'Session: Error while trying to obtain lock for '.$this->_key_prefix.$session_id);
351
				return FALSE;
411
				return FALSE;
Zeile 381... Zeile 441...
381
	 */
441
	 */
382
	protected function _release_lock()
442
	protected function _release_lock()
383
	{
443
	{
384
		if (isset($this->_redis, $this->_lock_key) && $this->_lock)
444
		if (isset($this->_redis, $this->_lock_key) && $this->_lock)
385
		{
445
		{
386
			if ( ! $this->_redis->delete($this->_lock_key))
446
			if ( ! $this->_redis->{$this->_delete_name}($this->_lock_key))
387
			{
447
			{
388
				log_message('error', 'Session: Error while trying to free lock for '.$this->_lock_key);
448
				log_message('error', 'Session: Error while trying to free lock for '.$this->_lock_key);
389
				return FALSE;
449
				return FALSE;
390
			}
450
			}