1 <?php
2 /**
3 * @package Joomla.Legacy
4 * @subpackage Table
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.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * Session table
14 *
15 * @since 1.5
16 * @deprecated 3.2 Use SQL queries to interact with the session table.
17 */
18 class JTableSession extends JTable
19 {
20 /**
21 * Constructor
22 *
23 * @param JDatabaseDriver $db Database driver object.
24 *
25 * @since 1.5
26 * @deprecated 3.2 Use SQL queries to interact with the session table.
27 */
28 public function __construct(JDatabaseDriver $db)
29 {
30 JLog::add('JTableSession is deprecated. Use SQL queries directly to interact with the session table.', JLog::WARNING, 'deprecated');
31 parent::__construct('#__session', 'session_id', $db);
32
33 $this->guest = 1;
34 $this->username = '';
35 }
36
37 /**
38 * Insert a session
39 *
40 * @param string $sessionId The session id
41 * @param integer $clientId The id of the client application
42 *
43 * @return boolean True on success
44 *
45 * @since 1.5
46 * @deprecated 3.2 Use SQL queries to interact with the session table.
47 */
48 public function insert($sessionId, $clientId)
49 {
50 $this->session_id = $sessionId;
51 $this->client_id = $clientId;
52
53 $this->time = time();
54 $ret = $this->_db->insertObject($this->_tbl, $this, 'session_id');
55
56 if (!$ret)
57 {
58 $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_STORE_FAILED', strtolower(get_class($this)), $this->_db->stderr()));
59
60 return false;
61 }
62 else
63 {
64 return true;
65 }
66 }
67
68 /**
69 * Updates the session
70 *
71 * @param boolean $updateNulls True to update fields even if they are null.
72 *
73 * @return boolean True on success.
74 *
75 * @since 1.5
76 * @deprecated 3.2 Use SQL queries to interact with the session table.
77 */
78 public function update($updateNulls = false)
79 {
80 $this->time = time();
81 $ret = $this->_db->updateObject($this->_tbl, $this, 'session_id', $updateNulls);
82
83 if (!$ret)
84 {
85 $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_STORE_FAILED', strtolower(get_class($this)), $this->_db->stderr()));
86
87 return false;
88 }
89 else
90 {
91 return true;
92 }
93 }
94
95 /**
96 * Destroys the pre-existing session
97 *
98 * @param integer $userId Identifier of the user for this session.
99 * @param array $clientIds Array of client ids for which session(s) will be destroyed
100 *
101 * @return boolean True on success.
102 *
103 * @since 1.5
104 * @deprecated 3.2 Use SQL queries to interact with the session table.
105 */
106 public function destroy($userId, $clientIds = array())
107 {
108 $clientIds = implode(',', $clientIds);
109
110 $query = $this->_db->getQuery(true)
111 ->delete($this->_db->quoteName($this->_tbl))
112 ->where($this->_db->quoteName('userid') . ' = ' . $this->_db->quote($userId))
113 ->where($this->_db->quoteName('client_id') . ' IN (' . $clientIds . ')');
114 $this->_db->setQuery($query);
115
116 if (!$this->_db->execute())
117 {
118 $this->setError($this->_db->stderr());
119
120 return false;
121 }
122
123 return true;
124 }
125
126 /**
127 * Purge old sessions
128 *
129 * @param integer $maxLifetime Session age in seconds
130 *
131 * @return mixed Resource on success, null on fail
132 *
133 * @since 1.5
134 * @deprecated 3.2 Use SQL queries to interact with the session table.
135 */
136 public function purge($maxLifetime = 1440)
137 {
138 $past = time() - $maxLifetime;
139 $query = $this->_db->getQuery(true)
140 ->delete($this->_db->quoteName($this->_tbl))
141 ->where($this->_db->quoteName('time') . ' < ' . (int) $past);
142 $this->_db->setQuery($query);
143
144 return $this->_db->execute();
145 }
146
147 /**
148 * Find out if a user has one or more active sessions
149 *
150 * @param integer $userid The identifier of the user
151 *
152 * @return boolean True if a session for this user exists
153 *
154 * @since 1.5
155 * @deprecated 3.2 Use SQL queries to interact with the session table.
156 */
157 public function exists($userid)
158 {
159 $query = $this->_db->getQuery(true)
160 ->select('COUNT(userid)')
161 ->from($this->_db->quoteName($this->_tbl))
162 ->where($this->_db->quoteName('userid') . ' = ' . $this->_db->quote($userid));
163 $this->_db->setQuery($query);
164
165 if (!$result = $this->_db->loadResult())
166 {
167 $this->setError($this->_db->stderr());
168
169 return false;
170 }
171
172 return (boolean) $result;
173 }
174
175 /**
176 * Overloaded delete method
177 *
178 * We must override it because of the non-integer primary key
179 *
180 * @param integer $oid The object id (optional).
181 *
182 * @return mixed True if successful otherwise an error message
183 *
184 * @since 1.5
185 * @deprecated 3.2 Use SQL queries to interact with the session table.
186 */
187 public function delete($oid = null)
188 {
189 $k = $this->_tbl_key;
190
191 if ($oid)
192 {
193 $this->$k = $oid;
194 }
195
196 $query = $this->_db->getQuery(true)
197 ->delete($this->_db->quoteName($this->_tbl))
198 ->where($this->_db->quoteName($this->_tbl_key) . ' = ' . $this->_db->quote($this->$k));
199 $this->_db->setQuery($query);
200
201 $this->_db->execute();
202
203 return true;
204 }
205 }
206