| 
<?PHP/*
 * x0Cache engine
 * Version 0.1.0
 * Copyright (c) 2012 F3llony, [email protected]
 * Website: http://cache.x0.lv/
 * Licensed under the terms of
 * GNU GENERAL PUBLIC LICENSE Version 3
 *
 * Base class for x0Cache caching mechanism.
 */
 
 class x0cache
 {
 // $config will contain reference to active configuration.
 // You can modify configuration options in the runtime.
 public $config = null;
 // Engine currently loaded.
 public $engine;
 // Active engine name
 public $activeEngineName = null;
 // Session object. Null if not active.
 public $session = null;
 
 // Constructor, loads config and engine.
 public function __construct( $user_engine = null ,$user_sessions = null)
 {
 define( '__x0path', dirname( __FILE__ ) );
 if ( PHP_MAJOR_VERSION <= 5 && PHP_MINOR_VERSION < 3 )
 {
 throw new Exception( 'x0Cache: unsupported PHP version: ' . PHP_VERSION . '. Minimum supported version is PHP 5.3.10. Code: 1004', 1004 );
 }
 require( __x0path . '/x0cache/config.php' );
 $this->config = $x0config;
 
 if ( $user_engine == null )
 {
 $this->loadEngine( $this->config->engine );
 }
 else
 {
 $this->loadEngine( $user_engine );
 }
 if($user_sessions ==  true || ($user_sessions == null && $this->config->session->enable == true)){
 require(__x0path . '/x0cache/session.php');
 $this->session = new x0cacheSession($this->config);
 }
 }
 
 /*    Loads caching engine. You can switch between caching engines in runtime using this function.
 *    Gotcha: x0Cache DOES NOT monitors what keys are stored in what engines!
 *        If you store value in one engine, then switch to different one, you must switch back to
 *        engine in wich you stored the value, otherwise your value will be non-existent in new engine
 *        and this might lead to false 'exists' results.
 */
 public function loadEngine( $name )
 {
 require(__x0path . '/x0cache/engine_' . $name . '.php');
 $this->engine           = new $name( $this->config );
 $this->activeEngineName = $name;
 return true;
 }
 
 // Wrapper to store item in engine selected. Will ALWAYS overwrite existing items. NO EXCEPTIONS!
 // Key must always be 3 or more symbols long, must contain only "A-z", "0-9", and "_".
 public function set( $key, $val, $ttl = 0 )
 {
 if ( strlen( $key ) < 3 )
 {
 throw new Exception( 'x0Cache: failed to cache variable with key "' . $key . '" - name too short. Minimum length is 3 symbols. Code 1003', 1003 );
 }
 if ( is_scalar( $val ) || is_array( $val ) || is_object( $val ) )
 {
 return $this->engine->set( (string) $key, $val, $ttl );
 }
 else
 {
 if ( $this->config->debug == true )
 {
 throw new Exception( 'x0Cache: failed to cache unsupported variable type "' . gettype( $val ) . '" with key "' . $key . '". Code 1002', 1002 );
 }
 return false;
 }
 }
 
 public function get( $key )
 {
 return $this->engine->get( $key );
 }
 
 public function delete( $key )
 {
 return $this->engine->delete( $key );
 }
 
 public function exists( $key )
 {
 return $this->engine->exists( $key );
 }
 
 public function flush()
 {
 return $this->engine->flush();
 }
 
 // Encrypts values for caching if engine encryption is enabled
 static function encrypt( $key, $value, $algo )
 {
 if ( is_array( $value ) || is_object( $value ) )
 {
 $value = 'x0cser:' . serialize( $value );
 }
 
 $enc = mcrypt_encrypt( $algo, $key, 'x0env:' . $value, MCRYPT_MODE_ECB );
 return base64_encode( $enc );
 }
 
 // Decrypts values from cache engine if encryption is enabled.
 static function decrypt( $key, $value, $algo )
 {
 $data = mcrypt_decrypt( $algo, $key, base64_decode( $value ), MCRYPT_MODE_ECB );
 if ( substr( $data, 0, 6 ) != 'x0env:' )
 {
 throw new Exception( 'x0Cache: unexpected- unable to decrypt encrypted value. If You had changed the encryption key, You must flush the cache. Code: 5001', 5001 );
 return false;
 }
 else
 {
 $data = trim(substr( $data, 6 ));
 }
 if ( substr( $data, 0, 7 ) == 'x0cser:' )
 {
 $data = trim( $data );
 $data = unserialize( substr( $data, 7 ) );
 }
 return $data;
 }
 }
 ?>
 |