Completed
Pull Request — development (#2979)
by Stephen
08:55
created

DatabaseHandler::read()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 22
ccs 10
cts 11
cp 0.9091
crap 3.0067
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Our handler for database sessions
5
 *
6
 * @name      ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
9
 *
10
 * This file contains code covered by:
11
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
12
 * license:		BSD, See included LICENSE.TXT for terms and conditions.
13
 *
14
 * @version 1.1 Release Candidate 1
15
 *
16
 */
17
18
namespace ElkArte\sources\subs\SessionHandler;
19
20
/**
21
 * Class DatabaseHandler
22
 *
23
 * @package ElkArte\sources\subs\SessionHandler
24
 */
25
class DatabaseHandler extends SessionHandler
26
{
27
	/**
28
	 * {@inheritdoc}
29
	 */
30 1
	public function destroy($sessionId)
31
	{
32
		// Better safe than sorry
33 1
		if (preg_match('~^[A-Za-z0-9,-]{16,64}$~', $sessionId) == 0)
34 1
		{
35
			return false;
36
		}
37
38
		// Just delete the row...
39 1
		$this->_db->query('', '
40
			DELETE FROM {db_prefix}sessions
41 1
			WHERE session_id = {string:session_id}',
42
			array(
43 1
				'session_id' => $sessionId,
44
			)
45 1
		);
46
47 1
		return true;
48
	}
49
50
	/**
51
	 * {@inheritdoc}
52
	 */
53
	public function gc($maxLifetime)
54
	{
55
		// Just set to the default or lower?  Ignore it for a higher value. (hopefully)
56
		if (!empty($this->_modSettings['databaseSession_lifetime']) && ($maxLifetime <= 1440 || $this->_modSettings['databaseSession_lifetime'] > $maxLifetime))
57
		{
58
			$maxLifetime = max($this->_modSettings['databaseSession_lifetime'], 60);
59
		}
60
61
		// Clean up after yerself ;).
62
		$this->_db->query('', '
63
			DELETE FROM {db_prefix}sessions
64
			WHERE last_update < {int:last_update}',
65
			array(
66
				'last_update' => time() - $maxLifetime,
67
			)
68
		);
69
70
		return $this->_db->affected_rows() != 0;
71
	}
72
73
	/**
74
	 * {@inheritdoc}
75
	 */
76 1
	public function read($sessionId)
77
	{
78 1
		if (preg_match('~^[A-Za-z0-9,-]{16,64}$~', $sessionId) == 0)
79 1
		{
80
			return '';
81
		}
82
83
		// Look for it in the database.
84 1
		$result = $this->_db->query('', '
85
			SELECT data
86
			FROM {db_prefix}sessions
87
			WHERE session_id = {string:session_id}
88 1
			LIMIT 1',
89
			array(
90 1
				'session_id' => $sessionId,
91
			)
92 1
		);
93 1
		list ($sessionData) = $this->_db->fetch_row($result);
94 1
		$this->_db->free_result($result);
95
96 1
		return empty($sessionData) ? '' : $sessionData;
97
	}
98
99
	/**
100
	 * {@inheritdoc}
101
	 */
102
	public function write($sessionId, $data)
103
	{
104
		if (preg_match('~^[A-Za-z0-9,-]{16,64}$~', $sessionId) == 0)
105
		{
106
			return false;
107
		}
108
109
		// First try to update an existing row...
110
		$this->_db->query('', '
111
			UPDATE {db_prefix}sessions
112
			SET data = {string:data}, last_update = {int:last_update}
113
			WHERE session_id = {string:session_id}',
114
			array(
115
				'last_update' => time(),
116
				'data' => $data,
117
				'session_id' => $sessionId,
118
			)
119
		);
120
121
		// If that didn't work, try inserting a new one.
122
		if ($this->_db->affected_rows() == 0)
123
		{
124
			$this->_db->insert('ignore',
125
				'{db_prefix}sessions',
126
				array('session_id' => 'string', 'data' => 'string', 'last_update' => 'int'),
127
				array($sessionId, $data, time()),
128
				array('session_id')
129
			);
130
		}
131
132
		return true;
133
	}
134
}
135