Файл: adultscript-2.0.3-pro/files/libraries/framework/cfg.php
Строк: 95
<?php
defined('_VALID') or die('Restricted Access!');
class VCfg
{
private static $cache = array();
public static function get($key, $required = true, $clean = false)
{
$parts = explode('.', $key);
$count = count($parts);
if ($count === 1) {
array_unshift($parts, 'config');
array_unshift($parts, 'core');
} elseif ($count === 2) {
array_unshift($parts, 'module');
}
$key = $parts['0'].'.'.$parts['1'];
if (!isset(static::$cache[$key])) {
static::group($key, true, false, false);
}
$name = $parts['2'];
if (!isset(static::$cache[$key][$name]) && $required === true) {
die('Invalid config item key: '.$key.', name: '.$name);
}
return static::$cache[$key][$name];
}
public static function group($key, $required = true, $clean = false, $return = true)
{
if (isset(static::$cache[$key]) && !$clean) {
return static::$cache[$key];
}
$parts = explode('.', $key);
if (count($parts) !== 2) {
die('Invalid configuration group key: '.$key);
}
if ($key == 'core.config') {
static::$cache[$key] = require BASE_DIR.'/config.php';
if (!$group = static::cache_get('config', 'config')) {
$group = static::cache_load('config', 'config', true);
}
static::$cache[$key] = array_merge(static::$cache[$key], $group);
} else {
$type = $parts['0'];
if ($type == 'library') {
static::$cache[$key] = require CONFIG_DIR.'/'.$parts['1'].'.php';
} else {
$name = $parts['1'];
if (!static::$cache[$key] = static::cache_get($name, $type)) {
static::$cache[$key] = static::cache_load($name, $type, true);
}
}
}
if ($return) {
return static::$cache[$key];
}
}
private static function cache_load($name, $type = 'module', $return = false)
{
$db = VF::factory('database');
$db->query("
SELECT config_cache
FROM #__".$db->escape($type)."
WHERE name = '".$db->escape($name)."'
LIMIT 1
");
if ($db->affected_rows()) {
$name = ($type == 'config') ? 'config' : $name;
$config = unserialize($db->fetch_field('config_cache'));
static::cache_set($name, $config, $type);
if ($return) {
return $config;
}
}
die('Failed to load config cache for name: '.$name.', type: '.$type);
}
public static function cache_set($cache_id, $data, $type = 'module', $serialize = true)
{
$file = static::cache_file($cache_id, $type);
if ($serialize === true) {
$data = serialize($data);
} else {
$output = "<?php defined('_VALID') or die('Restricted access'); return array(";
foreach ($data as $key => $value) {
$output .= "'".$key."' => '".$value."',";
}
$output .= "); ?>";
$data = $output;
}
if (($tmpfile = tempnam(CACHE_DIR, '.tmp')) &&
file_put_contents($tmpfile, $data) &&
rename($tmpfile, $file) &&
chmod($file, 0666)) {
return true;
}
return false;
}
private static function cache_get($cache_id, $type = 'module', $serialize = true)
{
clearstatcache();
$file = static::cache_file($cache_id, $type);
if (file_exists($file)) {
if ($serialize) {
return unserialize(file_get_contents($file));
} else {
$data = require $file;
return $data;
}
}
return false;
}
public static function cache_del($cache_id, $type = 'module')
{
$file = static::cache_file($cache_id, $type);
if (file_exists($file) && is_file($file)) {
@unlink($file);
}
}
private static function cache_file($cache_id, $type = 'module')
{
$secret = static::get('core.config.secret');
if ($type == 'config') {
$dir = '/config/';
} elseif ($type == 'language') {
$dir = '/language/';
} else {
$dir = '/config/';
$cache_id = $type.'_'.$cache_id;
}
return CACHE_DIR.$dir.$cache_id.'_'.md5($secret).'.php';
}
}