1 <?php
2 /**
3 * Part of the Joomla Framework Session Package
4 *
5 * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE
7 */
8
9 namespace Joomla\Session\Storage;
10
11 use Joomla\Session\Storage;
12 use Joomla\Database\DatabaseDriver;
13
14 /**
15 * Database session storage handler for PHP
16 *
17 * @see http://www.php.net/manual/en/function.session-set-save-handler.php
18 * @since 1.0
19 * @deprecated 2.0 The Storage class chain will be removed
20 */
21 class Database extends Storage
22 {
23 /**
24 * The DatabaseDriver to use when querying.
25 *
26 * @var DatabaseDriver
27 * @since 1.0
28 * @deprecated 2.0
29 */
30 protected $db;
31
32 /**
33 * Constructor
34 *
35 * @param array $options Optional parameters. A `dbo` options is required.
36 *
37 * @since 1.0
38 * @throws \RuntimeException
39 * @deprecated 2.0
40 */
41 public function __construct($options = array())
42 {
43 if (isset($options['db']) && ($options['db'] instanceof DatabaseDriver))
44 {
45 parent::__construct($options);
46 $this->db = $options['db'];
47 }
48 else
49 {
50 throw new \RuntimeException(
51 sprintf('The %s storage engine requires a `db` option that is an instance of Joomla\\Database\\DatabaseDriver.', __CLASS__)
52 );
53 }
54 }
55
56 /**
57 * Read the data for a particular session identifier from the SessionHandler backend.
58 *
59 * @param string $id The session identifier.
60 *
61 * @return string The session data.
62 *
63 * @since 1.0
64 * @deprecated 2.0
65 */
66 public function read($id)
67 {
68 try
69 {
70 // Get the session data from the database table.
71 $query = $this->db->getQuery(true);
72 $query->select($this->db->quoteName('data'))
73 ->from($this->db->quoteName('#__session'))
74 ->where($this->db->quoteName('session_id') . ' = ' . $this->db->quote($id));
75
76 $this->db->setQuery($query);
77
78 return (string) $this->db->loadResult();
79 }
80 catch (\Exception $e)
81 {
82 return false;
83 }
84 }
85
86 /**
87 * Write session data to the SessionHandler backend.
88 *
89 * @param string $id The session identifier.
90 * @param string $data The session data.
91 *
92 * @return boolean True on success, false otherwise.
93 *
94 * @since 1.0
95 * @deprecated 2.0
96 */
97 public function write($id, $data)
98 {
99 try
100 {
101 $query = $this->db->getQuery(true);
102 $query->update($this->db->quoteName('#__session'))
103 ->set($this->db->quoteName('data') . ' = ' . $this->db->quote($data))
104 ->set($this->db->quoteName('time') . ' = ' . $this->db->quote((int) time()))
105 ->where($this->db->quoteName('session_id') . ' = ' . $this->db->quote($id));
106
107 // Try to update the session data in the database table.
108 $this->db->setQuery($query);
109
110 if (!$this->db->execute())
111 {
112 return false;
113 }
114
115 // Since $this->db->execute did not throw an exception the query was successful.
116 // Either the data changed, or the data was identical. In either case we are done.
117
118 return true;
119 }
120 catch (\Exception $e)
121 {
122 return false;
123 }
124 }
125
126 /**
127 * Destroy the data for a particular session identifier in the SessionHandler backend.
128 *
129 * @param string $id The session identifier.
130 *
131 * @return boolean True on success, false otherwise.
132 *
133 * @since 1.0
134 * @deprecated 2.0
135 */
136 public function destroy($id)
137 {
138 try
139 {
140 $query = $this->db->getQuery(true);
141 $query->delete($this->db->quoteName('#__session'))
142 ->where($this->db->quoteName('session_id') . ' = ' . $this->db->quote($id));
143
144 // Remove a session from the database.
145 $this->db->setQuery($query);
146
147 return (boolean) $this->db->execute();
148 }
149 catch (\Exception $e)
150 {
151 return false;
152 }
153 }
154
155 /**
156 * Garbage collect stale sessions from the SessionHandler backend.
157 *
158 * @param integer $lifetime The maximum age of a session.
159 *
160 * @return boolean True on success, false otherwise.
161 *
162 * @since 1.0
163 * @deprecated 2.0
164 */
165 public function gc($lifetime = 1440)
166 {
167 // Determine the timestamp threshold with which to purge old sessions.
168 $past = time() - $lifetime;
169
170 try
171 {
172 $query = $this->db->getQuery(true);
173 $query->delete($this->db->quoteName('#__session'))
174 ->where($this->db->quoteName('time') . ' < ' . $this->db->quote((int) $past));
175
176 // Remove expired sessions from the database.
177 $this->db->setQuery($query);
178
179 return (boolean) $this->db->execute();
180 }
181 catch (\Exception $e)
182 {
183 return false;
184 }
185 }
186 }
187