Source for file database.php

Documentation is available at database.php

  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.  */
  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 (Exception $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.             if (!$db->execute())
  85.             {
  86.                 return false;
  87.             }
  88.             /* Since $db->execute did not throw an exception, so the query was successful.
  89.             Either the data changed, or the data was identical.
  90.             In either case we are done.
  91.             */
  92.             return true;
  93.         }
  94.         catch (Exception $e)
  95.         {
  96.             return false;
  97.         }
  98.     }
  99.  
  100.     /**
  101.      * Destroy the data for a particular session identifier in the SessionHandler backend.
  102.      *
  103.      * @param   string  $id  The session identifier.
  104.      *
  105.      * @return  boolean  True on success, false otherwise.
  106.      *
  107.      * @since   11.1
  108.      */
  109.     public function destroy($id)
  110.     {
  111.         // Get the database connection object and verify its connected.
  112.         $db JFactory::getDbo();
  113.  
  114.         try
  115.         {
  116.             $query $db->getQuery(true)
  117.                 ->delete($db->quoteName('#__session'))
  118.                 ->where($db->quoteName('session_id'' = ' $db->quote($id));
  119.  
  120.             // Remove a session from the database.
  121.             $db->setQuery($query);
  122.  
  123.             return (boolean) $db->execute();
  124.         }
  125.         catch (Exception $e)
  126.         {
  127.             return false;
  128.         }
  129.     }
  130.  
  131.     /**
  132.      * Garbage collect stale sessions from the SessionHandler backend.
  133.      *
  134.      * @param   integer  $lifetime  The maximum age of a session.
  135.      *
  136.      * @return  boolean  True on success, false otherwise.
  137.      *
  138.      * @since   11.1
  139.      */
  140.     public function gc($lifetime 1440)
  141.     {
  142.         // Get the database connection object and verify its connected.
  143.         $db JFactory::getDbo();
  144.  
  145.         // Determine the timestamp threshold with which to purge old sessions.
  146.         $past time($lifetime;
  147.  
  148.         try
  149.         {
  150.             $query $db->getQuery(true)
  151.                 ->delete($db->quoteName('#__session'))
  152.                 ->where($db->quoteName('time'' < ' $db->quote((int) $past));
  153.  
  154.             // Remove expired sessions from the database.
  155.             $db->setQuery($query);
  156.  
  157.             return (boolean) $db->execute();
  158.         }
  159.         catch (Exception $e)
  160.         {
  161.             return false;
  162.         }
  163.     }
  164. }

Documentation generated on Tue, 19 Nov 2013 14:57:47 +0100 by phpDocumentor 1.4.3