1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Session
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * Database session storage handler for PHP
14 *
15 * @link https://secure.php.net/manual/en/function.session-set-save-handler.php
16 * @since 11.1
17 * @deprecated 4.0 The CMS' Session classes will be replaced with the `joomla/session` package
18 */
19 class JSessionStorageDatabase extends JSessionStorage
20 {
21 /**
22 * Read the data for a particular session identifier from the SessionHandler backend.
23 *
24 * @param string $id The session identifier.
25 *
26 * @return string The session data.
27 *
28 * @since 11.1
29 */
30 public function read($id)
31 {
32 // Get the database connection object and verify its connected.
33 $db = JFactory::getDbo();
34
35 try
36 {
37 // Get the session data from the database table.
38 $query = $db->getQuery(true)
39 ->select($db->quoteName('data'))
40 ->from($db->quoteName('#__session'))
41 ->where($db->quoteName('session_id') . ' = ' . $db->quote($id));
42
43 $db->setQuery($query);
44
45 $result = (string) $db->loadResult();
46
47 $result = str_replace('\0\0\0', chr(0) . '*' . chr(0), $result);
48
49 return $result;
50 }
51 catch (RuntimeException $e)
52 {
53 return false;
54 }
55 }
56
57 /**
58 * Write session data to the SessionHandler backend.
59 *
60 * @param string $id The session identifier.
61 * @param string $data The session data.
62 *
63 * @return boolean True on success, false otherwise.
64 *
65 * @since 11.1
66 */
67 public function write($id, $data)
68 {
69 // Get the database connection object and verify its connected.
70 $db = JFactory::getDbo();
71
72 $data = str_replace(chr(0) . '*' . chr(0), '\0\0\0', $data);
73
74 try
75 {
76 $query = $db->getQuery(true)
77 ->update($db->quoteName('#__session'))
78 ->set($db->quoteName('data') . ' = ' . $db->quote($data))
79 ->set($db->quoteName('time') . ' = ' . $db->quote((int) time()))
80 ->where($db->quoteName('session_id') . ' = ' . $db->quote($id));
81
82 // Try to update the session data in the database table.
83 $db->setQuery($query);
84 $db->execute();
85
86 /*
87 * Since $db->execute did not throw an exception, so the query was successful.
88 * Either the data changed, or the data was identical.
89 * In either case we are done.
90 */
91 return true;
92 }
93 catch (RuntimeException $e)
94 {
95 return false;
96 }
97 }
98
99 /**
100 * Destroy the data for a particular session identifier in the SessionHandler backend.
101 *
102 * @param string $id The session identifier.
103 *
104 * @return boolean True on success, false otherwise.
105 *
106 * @since 11.1
107 */
108 public function destroy($id)
109 {
110 // Get the database connection object and verify its connected.
111 $db = JFactory::getDbo();
112
113 try
114 {
115 $query = $db->getQuery(true)
116 ->delete($db->quoteName('#__session'))
117 ->where($db->quoteName('session_id') . ' = ' . $db->quote($id));
118
119 // Remove a session from the database.
120 $db->setQuery($query);
121
122 return (boolean) $db->execute();
123 }
124 catch (RuntimeException $e)
125 {
126 return false;
127 }
128 }
129
130 /**
131 * Garbage collect stale sessions from the SessionHandler backend.
132 *
133 * @param integer $lifetime The maximum age of a session.
134 *
135 * @return boolean True on success, false otherwise.
136 *
137 * @since 11.1
138 */
139 public function gc($lifetime = 1440)
140 {
141 // Get the database connection object and verify its connected.
142 $db = JFactory::getDbo();
143
144 // Determine the timestamp threshold with which to purge old sessions.
145 $past = time() - $lifetime;
146
147 try
148 {
149 $query = $db->getQuery(true)
150 ->delete($db->quoteName('#__session'))
151 ->where($db->quoteName('time') . ' < ' . $db->quote((int) $past));
152
153 // Remove expired sessions from the database.
154 $db->setQuery($query);
155
156 return (boolean) $db->execute();
157 }
158 catch (RuntimeException $e)
159 {
160 return false;
161 }
162 }
163 }
164