Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
database.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Session
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  * Database session storage handler for PHP
14  *
15  * @package Joomla.Platform
16  * @subpackage Session
17  * @see http://www.php.net/manual/en/function.session-set-save-handler.php
18  * @since 11.1
19  */
21 {
22  /**
23  * Read the data for a particular session identifier from the SessionHandler backend.
24  *
25  * @param string $id The session identifier.
26  *
27  * @return string The session data.
28  *
29  * @since 11.1
30  */
31  public function read($id)
32  {
33  // Get the database connection object and verify its connected.
34  $db = JFactory::getDbo();
35 
36  try
37  {
38  // Get the session data from the database table.
39  $query = $db->getQuery(true)
40  ->select($db->quoteName('data'))
41  ->from($db->quoteName('#__session'))
42  ->where($db->quoteName('session_id') . ' = ' . $db->quote($id));
43 
44  $db->setQuery($query);
45 
46  $result = (string) $db->loadResult();
47 
48  $result = str_replace('\0\0\0', chr(0) . '*' . chr(0), $result);
49 
50  return $result;
51  }
52  catch (Exception $e)
53  {
54  return false;
55  }
56  }
57 
58  /**
59  * Write session data to the SessionHandler backend.
60  *
61  * @param string $id The session identifier.
62  * @param string $data The session data.
63  *
64  * @return boolean True on success, false otherwise.
65  *
66  * @since 11.1
67  */
68  public function write($id, $data)
69  {
70  // Get the database connection object and verify its connected.
71  $db = JFactory::getDbo();
72 
73  $data = str_replace(chr(0) . '*' . chr(0), '\0\0\0', $data);
74 
75  try
76  {
77  $query = $db->getQuery(true)
78  ->update($db->quoteName('#__session'))
79  ->set($db->quoteName('data') . ' = ' . $db->quote($data))
80  ->set($db->quoteName('time') . ' = ' . $db->quote((int) time()))
81  ->where($db->quoteName('session_id') . ' = ' . $db->quote($id));
82 
83  // Try to update the session data in the database table.
84  $db->setQuery($query);
85  if (!$db->execute())
86  {
87  return false;
88  }
89  /* Since $db->execute did not throw an exception, so the query was successful.
90  Either the data changed, or the data was identical.
91  In either case we are done.
92  */
93  return true;
94  }
95  catch (Exception $e)
96  {
97  return false;
98  }
99  }
100 
101  /**
102  * Destroy the data for a particular session identifier in the SessionHandler backend.
103  *
104  * @param string $id The session identifier.
105  *
106  * @return boolean True on success, false otherwise.
107  *
108  * @since 11.1
109  */
110  public function destroy($id)
111  {
112  // Get the database connection object and verify its connected.
113  $db = JFactory::getDbo();
114 
115  try
116  {
117  $query = $db->getQuery(true)
118  ->delete($db->quoteName('#__session'))
119  ->where($db->quoteName('session_id') . ' = ' . $db->quote($id));
120 
121  // Remove a session from the database.
122  $db->setQuery($query);
123 
124  return (boolean) $db->execute();
125  }
126  catch (Exception $e)
127  {
128  return false;
129  }
130  }
131 
132  /**
133  * Garbage collect stale sessions from the SessionHandler backend.
134  *
135  * @param integer $lifetime The maximum age of a session.
136  *
137  * @return boolean True on success, false otherwise.
138  *
139  * @since 11.1
140  */
141  public function gc($lifetime = 1440)
142  {
143  // Get the database connection object and verify its connected.
144  $db = JFactory::getDbo();
145 
146  // Determine the timestamp threshold with which to purge old sessions.
147  $past = time() - $lifetime;
148 
149  try
150  {
151  $query = $db->getQuery(true)
152  ->delete($db->quoteName('#__session'))
153  ->where($db->quoteName('time') . ' < ' . $db->quote((int) $past));
154 
155  // Remove expired sessions from the database.
156  $db->setQuery($query);
157 
158  return (boolean) $db->execute();
159  }
160  catch (Exception $e)
161  {
162  return false;
163  }
164  }
165 }