<?php
/**
* PHP VoiceViewer - Configuration Class
*
* Provides the basic configuration class which can be used instead
* of a global variable and provides some IO control.
* Also provides auto completion on paths and urls.
*
* Project : PHP VoiceViewer
* Copyright : Christopher Cooper © 2010
* License : FreeBSD
* Author : Christopher Cooper (PaNtHaLooN), hide@address.com
* Date : March 2010
* Version : 1.0.0
*
* Copyright 2010 Christopher Cooper. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY CHRISTOPHER COOPER ``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 CHRISTOPHER COOPER 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.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of Christopher Cooper.
*/
// Define acronyms
defined('DS') || define('DS', DIRECTORY_SEPARATOR);
class vvConfig implements ArrayAccess
{
private static $_config = array();
private static $_overwrite = true;
private static $_pattern_set_magic = '/^(?P<var>\w+)\|(?P<action>\w+)$/';
private static $_pattern_get_magic = '/^\w+\|\w+$/';
private static $_pattern_path_set = '/^vv_path(?:_\w+)?$/';
private static $_pattern_path_get = '/^(?P<var>vv_path_\w+)\|(?P<action>\w+)$/';
private static $_pattern_url_set = '/^vv_url(?:_\w+)?$/';
private static $_pattern_url_get = '/^(?P<var>vv_url_\w+)\|(?P<action>\w+)$/';
public function __construct( array $config = null )
{
($config !== null ) && self::$_config = $config;
}
public function offsetSet( $offset, $value )
{
if ( $offset === null )
{
self::$_config[] = $value;
return;
}
$noupdate = false;
if ( preg_match(self::$_pattern_set_magic, $offset, $matches) === 1 )
{
$offset = $matches['var'];
switch( $matches['action'] )
{
case 'noupdate':
$noupdate = true;
break;
default:
break;
}
}
if ( preg_match(self::$_pattern_path_set, $offset) === 1 )
{
if ( $value[strlen($value)-1] !== DS )
$value .= DS;
}
else if ( preg_match(self::$_pattern_url_set, $offset) === 1 )
{
if ( $value[strlen($value)-1] !== '/' )
$value .= '/';
}
if ( (($noupdate === true) || (self::$_overwrite === false)) &&
array_key_exists($offset, self::$_config) )
{
return;
}
self::$_config[$offset] = $value;
}
public function offsetExists( $offset )
{
if ( array_key_exists($offset, self::$_config) )
{
if ( (self::$_config[$offset] !== false) &&
!(is_string(self::$_config[$offset]) && (strlen(self::$_config[$offset]) === 0)) &&
(self::$_config[$offset] !== null) &&
!(is_array(self::$_config[$offset]) && (count(self::$_config[$offset]) > 0)) )
{
return true;
}
}
return false;
}
public function offsetUnset( $offset )
{
unset(self::$_config[$offset]);
}
public function offsetGet( $offset )
{
$prefix = false;
if ( preg_match(self::$_pattern_get_magic, $offset) === 1 )
{
if ( preg_match(self::$_pattern_path_get, $offset, $matches) === 1 )
{
$offset = $matches['var'];
switch( $matches['action'] )
{
case 'full':
$prefix = self::$_config['vv_path'];
break;
default:
break;
}
}
else if ( preg_match(self::$_pattern_url_get, $offset, $matches) === 1 )
{
$offset = $matches['var'];
switch( $matches['action'] )
{
case 'full':
$prefix = self::$_config['vv_url'];
break;
default:
break;
}
}
}
if ( array_key_exists($offset, self::$_config) )
{
if ( $prefix !== false )
{
return $prefix . self::$_config[$offset];
}
return self::$_config[$offset];
}
return null;
}
public static function getArray()
{
return self::$_config;
}
public static function setOverwrite( $overwrite = true )
{
self::$_overwrite = $overwrite;
}
}
// Main include
@require_once(dirname(__FILE__) . DS . '..' . DS . 'main.inc.php');
?>