[eluser]mezoni[/eluser]
This is a alfa version. Not tested.
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Variable Helper
*
* @author Inozemcev Andrew
* @copyright Copyright (c) 2007, Inozemcev Andrew.
* @since Version 1.0
*/
// ------------------------------------------------------------------------
/**
* @subpackage Helpers
* @category Helpers
* @author Inozemcev Andrew
*/
// ------------------------------------------------------------------------
/**
* Variable Read
*
* Read array of variables from file system.
*
* @access public
* @return array
*/
function &variable_read()
{
static $registered = FALSE;
static $variables;
if(!$registered)
{
register_shutdown_function('variable_write');
$registered = TRUE;
}
if(is_array($variables))
{
return $variables;
}
$variables = array();
$filepath = APPPATH . 'config/variables.cfg';
if(!file_exists($filepath))
{
return $variables;
}
$size = filesize($filepath);
if($size === 0)
{
return $variables;
}
if($fp = @fopen($filepath, 'r'))
{
flock($fp, LOCK_SH);
$variables = unserialize(fread($fp, $size));
flock($fp, LOCK_UN);
fclose($fp);
}
return $variables;
}
// ------------------------------------------------------------------------
/**
* Variable Write
*
* Write array of variables to file system.
*
* @access public
*/
function variable_write()
{
$variables =& variable_read();
if(!isset($variables['access']))
{
return;
}
$filepath = APPPATH . 'config/variables.cfg';
$mode = file_exists($filepath) ? 'r+' : 'w+';
if($fp = @fopen($filepath, $mode))
{
flock($fp, LOCK_EX);
$size = filesize($filepath);
if($size > 0)
{
$original = unserialize(fread($fp, $size));
}
else
{
$original = array();
}
$current =& $variables['current'];
foreach($variables['access'] as $key => $val)
{
if($val === 'c')
{
$original[$key] = $current[$key];
}
elseif($val === 'd')
{
unset($original[$key]);
}
}
ftruncate($fp, 0);
fseek($fp, 0);
fwrite($fp, serialize($original));
flock($fp, LOCK_UN);
fclose($fp);
}
@chmod($filename, 0666);
}
// ------------------------------------------------------------------------
/**
* Variable Get
*
* Get variable.
*
* @access public
* @param string
* @param mixed
* @return mixed
*/
function variable_get($name, $default = NULL)
{
$variables =& variable_read();
if(isset($variables['access'][$name]) AND $variables['access'][$name] === 'd')
{
return $default;
}
if(!isset($variables['current'][$name]))
{
return $default;
}
return $variables['current'][$name];
}
// ------------------------------------------------------------------------
/**
* Variable Set
*
* Set variable.
*
* @access public
* @param string
* @param mixed
*/
function variable_set($name, $value)
{
$variables =& variable_read();
$variables['current'][$name] = $value;
$variables['access'][$name] = 'c';
}
// ------------------------------------------------------------------------
/**
* Variable Delete
*
* Delete variable.
*
* @access public
* @param string
*/
function variable_delete($name)
{
$variables =& variable_read();
$variables['access'][$name] = 'd';
}
?>