HTTP_Session
[ class tree: HTTP_Session ] [ index: HTTP_Session ] [ all elements ]

Source for file DB.php

Documentation is available at DB.php

  1. <?php
  2. //
  3. // +-----------------------------------------------------------------------+
  4. // | Copyright (c) 2002, Alexander Radivanovich                            |
  5. // | All rights reserved.                                                  |
  6. // |                                                                       |
  7. // | Redistribution and use in source and binary forms, with or without    |
  8. // | modification, are permitted provided that the following conditions    |
  9. // | are met:                                                              |
  10. // |                                                                       |
  11. // | o Redistributions of source code must retain the above copyright      |
  12. // |   notice, this list of conditions and the following disclaimer.       |
  13. // | o Redistributions in binary form must reproduce the above copyright   |
  14. // |   notice, this list of conditions and the following disclaimer in the |
  15. // |   documentation and/or other materials provided with the distribution.|
  16. // | o The names of the authors may not be used to endorse or promote      |
  17. // |   products derived from this software without specific prior written  |
  18. // |   permission.                                                         |
  19. // |                                                                       |
  20. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   |
  21. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     |
  22. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
  23. // | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  |
  24. // | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
  25. // | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT      |
  26. // | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
  27. // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
  28. // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   |
  29. // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
  30. // | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  |
  31. // |                                                                       |
  32. // +-----------------------------------------------------------------------+
  33. // | Author: Alexander Radivanovich <info@wwwlab.net>                      |
  34. // +-----------------------------------------------------------------------+
  35. //
  36.  
  37. require_once 'HTTP/Session2/Container.php';
  38. require_once 'DB.php';
  39. require_once 'PEAR/Exception.php';
  40.  
  41. /**
  42.  * Database container for session data
  43.  *
  44.  * Create the following table to store session data
  45.  * <code>
  46.  * CREATE TABLE `sessiondata` (
  47.  *     `id` CHAR(32) NOT NULL,
  48.  *     `expiry` INT UNSIGNED NOT NULL DEFAULT 0,
  49.  *     `data` TEXT NOT NULL,
  50.  *     PRIMARY KEY (`id`)
  51.  * );
  52.  * </code>
  53.  *
  54.  * @author  Alexander Radivanovich <info@wwwlab.net>
  55.  * @package HTTP_Session
  56.  * @access  public
  57.  */
  58. {
  59.  
  60.     /**
  61.      * DB connection object
  62.      *
  63.      * @var object DB 
  64.      * @access private
  65.      */
  66.     private $db = null;
  67.  
  68.     /**
  69.      * Session data cache id
  70.      *
  71.      * @var mixed 
  72.      * @access private
  73.      */
  74.     private $crc = false;
  75.  
  76.     /**
  77.      * Constrtuctor method
  78.      *
  79.      * $options is an array with the options.<br>
  80.      * The options are:
  81.      * <ul>
  82.      * <li>'dsn' - The DSN string</li>
  83.      * <li>'table' - Table with session data, default is 'sessiondata'</li>
  84.      * <li>'autooptimize' - Boolean, 'true' to optimize
  85.      * the table on garbage collection, default is 'false'.</li>
  86.      * </ul>
  87.      *
  88.      * @access public
  89.      * @param  array  $options The options
  90.      * @return void 
  91.      */
  92.     public function __construct($options)
  93.     {
  94.     $this->options['table'$options['table'];
  95.     $this->options['dsn'= "{$options['dsn']['phptype']}://{$options['dsn']['username']}:{$options['dsn']['password']}@{$options['dsn']['hostspec']}/{$options['dsn']['database']}";    
  96.     }
  97.  
  98.     /**
  99.      * Connect to database by using the given DSN string
  100.      *
  101.      * @access private
  102.      * @param  string DSN string
  103.      * @return mixed  Object on error, otherwise bool
  104.      */
  105.     private function connect($dsn)
  106.     {
  107.         if (is_string($dsn)) {
  108.             $this->db = DB::connect($dsn);
  109.         else if (is_object($dsn&& is_a($dsn'db_common')) {
  110.             $this->db $dsn;
  111.         else if (is_object($dsn&& DB::isError($dsn)) {
  112.             return new DB_Error($dsn->codePEAR_ERROR_DIE);
  113.         else {
  114.             return new PEAR_Error("The given dsn was not valid in file " . __FILE__ . " at line " . __LINE__,
  115.                                   41,
  116.                                   PEAR_ERROR_RETURN,
  117.                                   null,
  118.                                   null
  119.                                   );
  120.  
  121.         }
  122.  
  123.         if (DB::isError($this->db)) {
  124.             return new DB_Error($this->db->codePEAR_ERROR_DIE);
  125.         }
  126.  
  127.         return true;
  128.     }
  129.  
  130.     /**
  131.      * Set some default options
  132.      *
  133.      * @access private
  134.      */
  135.     private function setDefaults()
  136.     {
  137.         $this->options['dsn']           = null;
  138.         $this->options['table']         'sessiondata';
  139.         $this->options['autooptimize']  = false;
  140.     }
  141.  
  142.     /**
  143.      * Establish connection to a database
  144.      *
  145.      */
  146.     public function open($save_path$session_name)
  147.     {
  148.         if (DB::isError($this->connect($this->options['dsn']))) {
  149.             return false;
  150.         else {
  151.             return true;
  152.         }
  153.     }
  154.  
  155.     /**
  156.      * Free resources
  157.      *
  158.      */
  159.     public function close()
  160.     {
  161.         return true;
  162.     }
  163.  
  164.     /**
  165.      * Read session data
  166.      *
  167.      */
  168.     public function read($id)
  169.     {
  170.         $query sprintf("SELECT data FROM %s WHERE id = %s AND expiry >= %d",$this->options['table'],$this->db->quote(md5($id)),time());
  171.         $result $this->db->getOne($query);
  172.         if (DB::isError($result)) {
  173.             new DB_Error($result->codePEAR_ERROR_DIE);
  174.             return false;
  175.         }
  176.         $this->crc strlen($resultcrc32($result);
  177.         return $result;
  178.     }
  179.  
  180.     /**
  181.      * Write session data
  182.      *
  183.      */
  184.     public function write($id$data)
  185.     {
  186.         if ((false !== $this->crc&& ($this->crc === strlen($datacrc32($data))) {
  187.             // $_SESSION hasn't been touched, no need to update the blob column
  188.             $query sprintf("UPDATE %s SET expiry = %d WHERE id = %s AND expiry >= %d",
  189.                              $this->options['table'],
  190.                              time(ini_get('session.gc_maxlifetime'),
  191.                              $this->db->quote(md5($id)),
  192.                              time()
  193.                              );
  194.         else {
  195.             // Check if table row already exists
  196.             $query sprintf("SELECT COUNT(id) FROM %s WHERE id = '%s'",
  197.                              $this->options['table'],
  198.                              md5($id)
  199.                              );
  200.             $result $this->db->getOne($query);
  201.             if (DB::isError($result)) {
  202.                 new DB_Error($result->codePEAR_ERROR_DIE);
  203.                 return false;
  204.             }
  205.             if (0 == intval($result)) {
  206.                 // Insert new row into table
  207.                 $query sprintf("INSERT INTO %s (id, expiry, data) VALUES (%s, %d, %s)",
  208.                                  $this->options['table'],
  209.                                  $this->db->quote(md5($id)),
  210.                                  time(ini_get('session.gc_maxlifetime'),
  211.                                  $this->db->quote($data)
  212.                                  );
  213.             else {
  214.                 // Update existing row
  215.                 $query sprintf("UPDATE %s SET expiry = %d, data = %s WHERE id = %s AND expiry >= %d",
  216.                                  $this->options['table'],
  217.                                  time(ini_get('session.gc_maxlifetime'),
  218.                                  $this->db->quote($data),
  219.                                  $this->db->quote(md5($id)),
  220.                                  time()
  221.                                  );
  222.             }
  223.         }
  224.         $result $this->db->query($query);
  225.         if (DB::isError($result)) {
  226.             new DB_Error($result->codePEAR_ERROR_DIE);
  227.             return false;
  228.         }
  229.  
  230.         return true;
  231.     }
  232.  
  233.     /**
  234.      * Destroy session data
  235.      *
  236.      */
  237.     public function destroy($id)
  238.     {
  239.         $query sprintf("DELETE FROM %s WHERE id = %s",
  240.                          $this->options['table'],
  241.                          $this->db->quote(md5($id))
  242.                          );
  243.         $result $this->db->query($query);
  244.         if (DB::isError($result)) {
  245.             new DB_Error($result->codePEAR_ERROR_DIE);
  246.             return false;
  247.         }
  248.  
  249.         return true;
  250.     }
  251.  
  252.     /**
  253.      * Garbage collection
  254.      *
  255.      */
  256.     public function gc($maxlifetime)
  257.     {
  258.         $query sprintf("DELETE FROM %s WHERE expiry < %d",
  259.                          $this->options['table'],
  260.                          time()
  261.                          );
  262.         $result $this->db->query($query);
  263.         if (DB::isError($result)) {
  264.             new DB_Error($result->codePEAR_ERROR_DIE);
  265.             return false;
  266.         }
  267.         if ($this->options['autooptimize']{
  268.             switch($this->db->type{
  269.                 case 'mysql':
  270.                     $query sprintf("OPTIMIZE TABLE %s"$this->options['table']);
  271.                     break;
  272.                 case 'pgsql':
  273.                     $query sprintf("VACUUM %s"$this->options['table']);
  274.                     break;
  275.                 default:
  276.                     $query = null;
  277.                     break;
  278.             }
  279.             if (isset($query)) {
  280.                 $result $this->db->query($query);
  281.                 if (DB::isError($result)) {
  282.                     new DB_Error($result->codePEAR_ERROR_DIE);
  283.                     return false;
  284.                 }
  285.             }
  286.         }
  287.  
  288.         return true;
  289.     }
  290. }
  291.  
  292. ?>

Documentation generated on Mon, 11 Mar 2019 14:18:39 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.