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