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

Source for file MDB.php

Documentation is available at MDB.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: Lorenzo Alberton <l dot alberton at quipo dot it>             |
  34. // +-----------------------------------------------------------------------+
  35. //
  36.  
  37. require_once 'HTTP/Session2/Container.php';
  38. require_once 'MDB.php';
  39.  
  40. /**
  41.  * Database container for session data
  42.  *
  43.  * Create the following table to store session data
  44.  * <code>
  45.  * CREATE TABLE `sessiondata` (
  46.  *     `id` CHAR(32) NOT NULL,
  47.  *     `expiry` INT UNSIGNED NOT NULL DEFAULT 0,
  48.  *     `data` TEXT NOT NULL,
  49.  *     PRIMARY KEY (`id`)
  50.  * );
  51.  * </code>
  52.  *
  53.  * @author  Lorenzo Alberton <l dot alberton at quipo dot it>
  54.  * @package HTTP_Session
  55.  * @access  public
  56.  */
  57. {
  58.  
  59.     /**
  60.      * MDB connection object
  61.      *
  62.      * @var object MDB 
  63.      * @access private
  64.      */
  65.     var $db = null;
  66.  
  67.     /**
  68.      * Session data cache id
  69.      *
  70.      * @var mixed 
  71.      * @access private
  72.      */
  73.     var $crc = false;
  74.  
  75.     /**
  76.      * Constructor method
  77.      *
  78.      * $options is an array with the options.<br>
  79.      * The options are:
  80.      * <ul>
  81.      * <li>'dsn' - The DSN string</li>
  82.      * <li>'table' - Table with session data, default is 'sessiondata'</li>
  83.      * <li>'autooptimize' - Boolean, 'true' to optimize
  84.      * the table on garbage collection, default is 'false'.</li>
  85.      * </ul>
  86.      *
  87.      * @access public
  88.      * @param  array  $options The options
  89.      * @return void 
  90.      */
  91.     function HTTP_Session2_Container_MDB($options)
  92.     {
  93.  
  94.         $this->_setDefaults();
  95.         if (is_array($options)) {
  96.         $this->options['dsn'$options['dsn'];
  97.         else {
  98.             $this->options['dsn'$options;
  99.         }
  100.     }
  101.  
  102.     /**
  103.      * Connect to database by using the given DSN string
  104.      *
  105.      * @access private
  106.      * @param  string DSN string
  107.      * @return mixed  Object on error, otherwise bool
  108.      */
  109.     function _connect($dsn)
  110.     {
  111.         if (is_string($dsn|| is_array($dsn)) {
  112.             $this->db = MDB::connect($dsn);
  113.         else if (is_object($dsn&& is_a($dsn'mdb_common')) {
  114.             $this->db $dsn;
  115.         else if (is_object($dsn&& MDB::isError($dsn)) {
  116.             return new MDB_Error($dsn->codePEAR_ERROR_DIE);
  117.         else {
  118.     echo 'yea? '.$dsn;
  119.             return new PEAR_Error("The given dsn was not valid in file " . __FILE__ . " at line " . __LINE__,
  120.                                   41,
  121.                                   PEAR_ERROR_RETURN,
  122.                                   null,
  123.                                   null
  124.                                   );
  125.  
  126.         }
  127.         if (MDB::isError($this->db)) {
  128.             return new MDB_Error($this->db->codePEAR_ERROR_DIE);
  129.         }
  130.  
  131.         return true;
  132.     }
  133.  
  134.     /**
  135.      * Set some default options
  136.      *
  137.      * @access private
  138.      */
  139.     function _setDefaults()
  140.     {
  141.         $this->options['dsn']           = null;
  142.         $this->options['table']         'sessiondata';
  143.         $this->options['autooptimize']  = false;
  144.     }
  145.  
  146.     /**
  147.      * Establish connection to a database
  148.      *
  149.      */
  150.     function open($save_path$session_name)
  151.     {
  152.         if (MDB::isError($this->_connect($this->options['dsn']))) {
  153.             return false;
  154.         else {
  155.             return true;
  156.         }
  157.     }
  158.  
  159.     /**
  160.      * Free resources
  161.      *
  162.      */
  163.     function close()
  164.     {
  165.         return true;
  166.     }
  167.  
  168.     /**
  169.      * Read session data
  170.      *
  171.      */
  172.     function read($id)
  173.     {
  174.         $query sprintf("SELECT data FROM %s WHERE id = %s AND expiry >= %d",
  175.          $this->options['table'],
  176.         $this->db->getTextValue(md5($id)),
  177.         time());
  178.  
  179.         $result $this->db->getOne($query);
  180.         if (MDB::isError($result)) {
  181.             new MDB_Error($result->codePEAR_ERROR_DIE);
  182.             return false;
  183.         }
  184.         $this->crc strlen($resultcrc32($result);
  185.         return $result;
  186.     }
  187.  
  188.     /**
  189.      * Write session data
  190.      *
  191.      */
  192.     function write($id$data)
  193.     {
  194.         if ((false !== $this->crc&& ($this->crc === strlen($datacrc32($data))) {
  195.             // $_SESSION hasn't been touched, no need to update the blob column
  196.             $query sprintf("UPDATE %s SET expiry = %d WHERE id = %s AND expiry >= %d",
  197.                              $this->options['table'],
  198.                              time(ini_get('session.gc_maxlifetime'),
  199.                              $this->db->getTextValue(md5($id)),
  200.                              time()
  201.                              );
  202.         else {
  203.             // Check if table row already exists
  204.             $query sprintf("SELECT COUNT(id) FROM %s WHERE id = %s",
  205.                              $this->options['table'],
  206.                              $this->db->getTextValue(md5($id))
  207.                              );
  208.             $result $this->db->getOne($query);
  209.             if (MDB::isError($result)) {
  210.                 new MDB_Error($result->codePEAR_ERROR_DIE);
  211.                 return false;
  212.             }
  213.             if (0 == intval($result)) {
  214.                 // Insert new row into table
  215.                 $query sprintf("INSERT INTO %s (id, expiry, data) VALUES (%s, %d, %s)",
  216.                                  $this->options['table'],
  217.                                  $this->db->getTextValue(md5($id)),
  218.                                  time(ini_get('session.gc_maxlifetime'),
  219.                                  $this->db->getTextValue($data)
  220.                                  );
  221.             else {
  222.                 // Update existing row
  223.                 $query sprintf("UPDATE %s SET expiry = %d, data = %s WHERE id = %s AND expiry >= %d",
  224.                                  $this->options['table'],
  225.                                  time(ini_get('session.gc_maxlifetime'),
  226.                                  $this->db->getTextValue($data),
  227.                                  $this->db->getTextValue(md5($id)),
  228.                                  time()
  229.                                  );
  230.             }
  231.         }
  232.         $result $this->db->query($query);
  233.         if (MDB::isError($result)) {
  234.             new MDB_Error($result->codePEAR_ERROR_DIE);
  235.             return false;
  236.         }
  237.  
  238.         return true;
  239.     }
  240.  
  241.     /**
  242.      * Destroy session data
  243.      *
  244.      */
  245.     function destroy($id)
  246.     {
  247.         $query sprintf("DELETE FROM %s WHERE id = %s",
  248.                          $this->options['table'],
  249.                          $this->db->getTextValue(md5($id))
  250.                          );
  251.         $result $this->db->query($query);
  252.         if (MDB::isError($result)) {
  253.             new MDB_Error($result->codePEAR_ERROR_DIE);
  254.             return false;
  255.         }
  256.  
  257.         return true;
  258.     }
  259.  
  260.     /**
  261.      * Garbage collection
  262.      *
  263.      */
  264.     function gc($maxlifetime)
  265.     {
  266.         $query sprintf("DELETE FROM %s WHERE expiry < %d",
  267.                          $this->options['table'],
  268.                          time()
  269.                          );
  270.         $result $this->db->query($query);
  271.         if (MDB::isError($result)) {
  272.             new MDB_Error($result->codePEAR_ERROR_DIE);
  273.             return false;
  274.         }
  275.         if ($this->options['autooptimize']{
  276.             switch($this->db->type{
  277.                 case 'mysql':
  278.                     $query sprintf("OPTIMIZE TABLE %s"$this->options['table']);
  279.                     break;
  280.                 case 'pgsql':
  281.                     $query sprintf("VACUUM %s"$this->options['table']);
  282.                     break;
  283.                 default:
  284.                     $query = null;
  285.                     break;
  286.             }
  287.             if (isset($query)) {
  288.                 $result $this->db->query($query);
  289.                 if (MDB::isError($result)) {
  290.                     new MDB_Error($result->codePEAR_ERROR_DIE);
  291.                     return false;
  292.                 }
  293.             }
  294.         }
  295.  
  296.         return true;
  297.     }
  298.  
  299. }
  300.  
  301. ?>

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