Source for file Creole.php
Documentation is available at Creole.php
// +-----------------------------------------------------------------------+
// | Copyright (c) 2004, Tony Bibbs |
// | All rights reserved. |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | o Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | o Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution.|
// | o The names of the authors may not be used to endorse or promote |
// | products derived from this software without specific prior written |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// +-----------------------------------------------------------------------+
// | Author: Tony Bibbs <tony@geeklog.net |
// +-----------------------------------------------------------------------+
* Abstract container class
require_once 'HTTP/Session2/Container.php';
require_once 'creole/Creole.php';
require_once 'PEAR/Exception.php';
* Creole container for session data
* Create the following table to store session data
* CREATE TABLE `sessiondata` (
* `id` CHAR(32) NOT NULL,
* `expiry` INT UNSIGNED NOT NULL DEFAULT 0,
* @author Tony Bibbs <tony@geeklog.net>
* Creole connection object
* $options is an array with the options.<br>
* <li>'dsn' - The DSN string</li>
* <li>'table' - Table with session data, default is 'sessiondata'</li>
* <li>'autooptimize' - Boolean, 'true' to optimize
* the table on garbage collection, default is 'false'.</li>
* @param array $options The options
$this->options = $options;
if (is_array($options)) {
$this->parseOptions($options);
$this->options['dsn'] = $options;
* Connect to database by using the given DSN string
* @param string DSN string or DSN Array (See http://creole.phpdb.org/wiki/index.php?node=5#A2)
private function connect ($dsn)
$this->db = Creole ::getConnection ($dsn);
throw new PEAR_Exception ('Invalid DSN Given');
* Set some default options
private function setDefaults ()
$this->options['dsn'] = null;
$this->options['table'] = 'sessiondata';
$this->options['autooptimize'] = false;
* Establish connection to a database
public function open($save_path, $session_name)
$this->connect ($this->options['dsn']);
public function read($id)
$sql = sprintf('SELECT data FROM %s WHERE id = ? AND expiry >= ?', $this->options['table']);
$prepStmt = $this->db->prepareStatement ($sql);
$rs = $prepStmt->executeQuery ($args,ResultSet ::FETCHMODE_NUM );
if ($rs->getRecordCount () == 0 ) {
throw new PEAR_Exception (" No record in session table for id: $id" );
$result = $rs->getString (0 );
public function write($id, $data)
if ((false !== $this->crc) AND ($this->crc === strlen($data) . crc32($data))) {
// $_SESSION hasn't been touched, no need to update the blob column
$sql = sprintf('UPDATE %s SET expiry = ? WHERE id = ? AND expiry >= ?',
$this->options['table']);
$prepStmt = $this->db->prepareStatement ($sql);
$args = array (time() + ini_get('session.gc_maxlifetime'),
// Check if table row already exists
$sql = sprintf('SELECT COUNT(id) FROM %s WHERE id = ?', $this->options['table']);
$prepStmt = $this->db->prepareStatement ($sql);
$rs = $prepStmt->executeQuery ($args);
$result = $rs->getInt (1 );
print 'doing insert'; exit;
// Insert new row into table
$sql = sprintf('INSERT INTO %s (id, expiry, data) VALUES (?, ?, ?)',
$this->options['table']);
$prepStmt = $this->db->prepareStatement ($sql);
$args = array (md5($id), time() + ini_get('session.gc_maxlifetime'), $data);
print 'hows this possible'; exit;
$sql = sprintf('UPDATE %s SET expiry = ?, data = ? WHERE id = ? AND expiry >= ?',
$this->options['table']);
$prepStmt = $this->db->prepareStatement ($sql);
$args = array (time() + ini_get('session.gc_maxlifetime'),
$result = $prepStmt->executeQuery ($args);
$sql = sprintf('DELETE FROM %s WHERE id = ?', $this->options['table']);
$prepStmt = $this->db->prepareStatement ($sql);
$result = $prepStmt->executeQuery ($args);
public function gc($maxlifetime)
$sql = sprintf('DELETE FROM %s WHERE expiry < ?', $this->options['table']);
$prepStmt = $this->db->prepareStatement ($sql);
$result = $prepStmt->executeQuery ($args);
/*if ($this->options['autooptimize']) {
switch($this->db->type) {
$query = sprintf("OPTIMIZE TABLE %s", $this->options['table']);
$query = sprintf("VACUUM %s", $this->options['table']);
$result = $this->db->query($query);
if (DB::isError($result)) {
new DB_Error($result->code, PEAR_ERROR_DIE);
Documentation generated on Mon, 11 Mar 2019 14:24:41 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|