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

Source for file Creole.php

Documentation is available at Creole.php

  1. <?php
  2. //
  3. // +-----------------------------------------------------------------------+
  4. // | Copyright (c) 2004, Tony Bibbs                                        |
  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: Tony Bibbs <tony@geeklog.net                                  |
  34. // +-----------------------------------------------------------------------+
  35. //
  36.  
  37. /**
  38.  * Abstract container class
  39.  */
  40. require_once 'HTTP/Session2/Container.php';
  41.  
  42. /**
  43.  * Creole library
  44.  */
  45. require_once 'creole/Creole.php';
  46.  
  47. /**
  48.  * PEAR::Exception
  49.  */
  50. require_once 'PEAR/Exception.php';
  51.  
  52. /**
  53.  * Creole container for session data
  54.  *
  55.  * Create the following table to store session data
  56.  * <code>
  57.  * CREATE TABLE `sessiondata` (
  58.  *     `id` CHAR(32) NOT NULL,
  59.  *     `expiry` INT UNSIGNED NOT NULL DEFAULT 0,
  60.  *     `data` TEXT NOT NULL,
  61.  *     PRIMARY KEY (`id`)
  62.  * );
  63.  * </code>
  64.  *
  65.  * @author  Tony Bibbs <tony@geeklog.net>
  66.  * @package HTTP_Session2
  67.  * @access  public
  68.  */
  69. {
  70.  
  71.     /**
  72.      * Creole connection object
  73.      *
  74.      * @var object DB 
  75.      * @access private
  76.      */
  77.     private $db = null;
  78.  
  79.     /**
  80.      * Session data cache id
  81.      *
  82.      * @var mixed 
  83.      * @access private
  84.      */
  85.     private $crc = false;
  86.  
  87.     /**
  88.      * Constrtuctor method
  89.      *
  90.      * $options is an array with the options.<br>
  91.      * The options are:
  92.      * <ul>
  93.      * <li>'dsn' - The DSN string</li>
  94.      * <li>'table' - Table with session data, default is 'sessiondata'</li>
  95.      * <li>'autooptimize' - Boolean, 'true' to optimize
  96.      * the table on garbage collection, default is 'false'.</li>
  97.      * </ul>
  98.      *
  99.      * @access public
  100.      * @param  array  $options The options
  101.      * @return void 
  102.      */
  103.     public function __construct($options)
  104.     {
  105.         $this->options $options;
  106.         /*$this->setDefaults();
  107.         if (is_array($options)) {
  108.             $this->parseOptions($options);
  109.         } else {
  110.             $this->options['dsn'] = $options;
  111.         }*/
  112.     }
  113.  
  114.     /**
  115.      * Connect to database by using the given DSN string
  116.      *
  117.      * @access private
  118.      * @param  string DSN string or DSN Array (See http://creole.phpdb.org/wiki/index.php?node=5#A2)
  119.      * 
  120.      */
  121.     private function connect($dsn)
  122.     {        
  123.         if (is_string($dsnOR is_array($dsn)) {
  124.             $this->db = Creole::getConnection($dsn);
  125.         else {
  126.             throw new PEAR_Exception('Invalid DSN Given');
  127.         }        
  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.         $this->connect($this->options['dsn']);        
  149.     }
  150.  
  151.     /**
  152.      * Free resources
  153.      *
  154.      */
  155.     public function close()
  156.     {
  157.         return true;
  158.     }
  159.  
  160.     /**
  161.      * Read session data
  162.      *
  163.      */
  164.     public function read($id)
  165.     {
  166.         $sql sprintf('SELECT data FROM %s WHERE id = ? AND expiry >= ?'$this->options['table']);
  167.         $prepStmt $this->db->prepareStatement($sql);
  168.         $args = array(md5($id),time());
  169.         $rs $prepStmt->executeQuery($args,ResultSet::FETCHMODE_NUM);
  170.         if ($rs->getRecordCount(== 0{
  171.             return '';
  172.             throw new PEAR_Exception("No record in session table for id: $id");
  173.         }
  174.         $rs->next();
  175.         $result $rs->getString(0);
  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->crcAND ($this->crc === strlen($datacrc32($data))) {
  187.             // $_SESSION hasn't been touched, no need to update the blob column
  188.             $sql sprintf('UPDATE %s SET expiry = ? WHERE id = ? AND expiry >= ?'
  189.                 $this->options['table']);
  190.             $prepStmt $this->db->prepareStatement($sql);
  191.             $args = array(time(ini_get('session.gc_maxlifetime'),
  192.                             md5($id)time());
  193.         else {
  194.             // Check if table row already exists
  195.             $sql sprintf('SELECT COUNT(id) FROM %s WHERE id = ?'$this->options['table']);
  196.             $prepStmt $this->db->prepareStatement($sql);
  197.             $args = array(md5($id));
  198.             $rs $prepStmt->executeQuery($args);
  199.             $rs->next();
  200.             $result $rs->getInt(1);
  201.             if (0 == intval($result)) {
  202.                 print 'doing insert'; exit;
  203.                 // Insert new row into table
  204.                 $sql sprintf('INSERT INTO %s (id, expiry, data) VALUES (?, ?, ?)'
  205.                     $this->options['table']);
  206.                 $prepStmt $this->db->prepareStatement($sql);
  207.                 $args = array(md5($id)time(ini_get('session.gc_maxlifetime')$data)
  208.             else {
  209.                 print 'hows this possible'; exit;
  210.                 // Update existing row
  211.                 $sql sprintf('UPDATE %s SET expiry = ?, data = ? WHERE id = ? AND expiry >= ?'
  212.                     $this->options['table']);
  213.                 $prepStmt $this->db->prepareStatement($sql);
  214.                 $args = array(time(ini_get('session.gc_maxlifetime')
  215.                     $datamd5(id)time())
  216.             }
  217.         }
  218.         
  219.         $result $prepStmt->executeQuery($args);
  220.         
  221.         return true;
  222.     }
  223.  
  224.     /**
  225.      * Destroy session data
  226.      *
  227.      */
  228.     public function destroy($id)
  229.     {
  230.         $sql sprintf('DELETE FROM %s WHERE id = ?'$this->options['table']);
  231.         $prepStmt $this->db->prepareStatement($sql);
  232.         $args = array(md5($id));
  233.         $result $prepStmt->executeQuery($args);
  234.         return true;
  235.     }
  236.  
  237.     /**
  238.      * Garbage collection
  239.      *
  240.      */
  241.     public function gc($maxlifetime)
  242.     {
  243.         $sql sprintf('DELETE FROM %s WHERE expiry < ?'$this->options['table']);
  244.         $prepStmt $this->db->prepareStatement($sql);
  245.         $args = array(time());
  246.         $result $prepStmt->executeQuery($args);
  247.         /*if ($this->options['autooptimize']) {
  248.             switch($this->db->type) {
  249.                 case 'mysql':
  250.                     $query = sprintf("OPTIMIZE TABLE %s", $this->options['table']);
  251.                     break;
  252.                 case 'pgsql':
  253.                     $query = sprintf("VACUUM %s", $this->options['table']);
  254.                     break;
  255.                 default:
  256.                     $query = null;
  257.                     break;
  258.             }
  259.             if (isset($query)) {
  260.                 $result = $this->db->query($query);
  261.                 if (DB::isError($result)) {
  262.                     new DB_Error($result->code, PEAR_ERROR_DIE);
  263.                     return false;
  264.                 }
  265.             }
  266.         }*/
  267.  
  268.         return true;
  269.     }
  270.  
  271. }
  272.  
  273. ?>

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