Файл: adultscript-2.0.3-pro/files/libraries/framework.php
Строк: 246
<?php
defined('_VALID') or die('Restricted Access!');
define('USE_APC', FALSE);
final class VF
{
public static $libraries = array();
public static $instances = array();
public static $config = array();
public static $running = TRUE;
/**
* Load a library from the framework.
*
* @param string $key The path key in dotted format (eg: framework.database')
*/
public static function load($key)
{
if (!isset(self::$libraries[$key])) {
self::$libraries[$key] = 1;
try {
require LIBRARY_DIR.'/'.str_replace('.', '/', $key).'.php';
} catch (Exception $e) {
throw new Exception($e);
}
}
}
/**
* Load a object from the object registry
*
* @param string $class The name of the class to load from the registry
* @param array $options Options to pass to the class
*/
public static function &factory($class, $options=array())
{
if (!isset(self::$instances[$class])) {
self::load('framework.'.$class);
$class_name = 'V'.ucfirst($class);
self::$instances[$class] = new $class_name($options);
if (method_exists(self::$instances[$class], 'getDriver')) {
self::$instances[$class] = self::$instances[$class]->getDriver();
}
}
return self::$instances[$class];
}
public static function factory_remove($class)
{
if (isset(self::$instances[$class])) {
if (method_exists(self::$instances[$class], 'close')) {
@self::$instances[$class]->close();
}
unset(self::$instances[$class]);
}
}
/**
* Load a configuration group or item
* @param string $key The config key in dotted format
* @param boolean $required If true the config group or item if forced, if not found the system will exit
* @param boolean $clean If true the config cache will be reloaded before loading key
*/
public static function cfg($key, $required=TRUE, $clean=FALSE, $return=FALSE)
{
if (isset(self::$config[$key]) && $clean === FALSE) {
return self::$config[$key];
}
$parts = explode('.', $key);
if (!isset($parts['0']) OR !isset($parts['1'])) {
die('Invalid configuration key ('.$key.') format!');
}
$type = $parts['0'];
$item = $parts['1'];
if ($type == 'core' && $item == 'config') {
self::$config[$key] = require BASE_DIR.'/config.php';
if (!$cfg = self::cache_get('config', 'config')) {
self::__cache_cfg(self::$config['core.config']['default'], 'config');
$cfg = self::cache_get($item, 'config');
}
self::$config[$key] = array_merge(self::$config[$key], (array) $cfg);
} elseif ($type == 'library') {
self::$config[$key] = require CONFIG_DIR.'/'.$item.'.php';
} else {
if (!self::$config[$key] = self::cache_get($item, $type)) {
self::__cache_cfg($item, $type);
self::$config[$key] = self::cache_get($item, $type);
}
}
if (!isset(self::$config[$key])) {
die('Failed to load configuration data!');
}
if ($return === TRUE) {
return;
}
return self::$config[$key];
}
public static function cfg_remove($key)
{
if (isset(self::$config[$key])) {
unset(self::$config[$key]);
}
}
public static function cfg_item($key, $required=TRUE, $clean=FALSE)
{
if (strpos($key, '.') === FALSE) {
return self::cfg_core_item($key, $required, $clean);
}
$key = explode('.', $key);
if (!isset($key['0']) OR !isset($key['1']) OR !isset($key['2'])) {
die('Invalid config item key!');
}
$cfg_key = $key['0'].'.'.$key['1'];
$item = $key['2'];
if (!isset(self::$config[$cfg_key]) OR $clean === TRUE) {
self::cfg($cfg_key, $required, $clean, TRUE);
}
return self::$config[$cfg_key][$item];
}
public static function cfg_core_item($item, $required=TRUE, $clean=FALSE)
{
if (isset(self::$config['core.config'])) {
if (isset(self::$config['core.config'][$item]) && $clean === FALSE) {
return self::$config['core.config'][$item];
}
}
self::cfg('core.config', $required, $clean);
if (isset(self::$config['core.config'][$item])) {
return self::$config['core.config'][$item];
}
die('Invalid configuration item ('.$item.')! Aborting...');
}
public static function cfg_item_set($item, $value)
{
if (strpos($item, '.') === FALSE) {
if (isset(self::$config['core.config'][$item])) {
return self::$config['core.config'][$item] = $value;
}
}
$key = explode('.', $item);
if (!isset($key['0']) OR !isset($key['1']) OR !isset($key['2'])) {
die('Invalid config item key!');
}
$cfg_key = $key['0'].'.'.$key['1'];
$item = $key['2'];
if (isset(self::$config[$cfg_key][$item])) {
self::$config[$cfg_key][$item] = $value;
}
}
public static function close()
{
session_write_close();
if (isset(self::$instances['database'])) {
self::$instances['database']->close();
}
while(ob_get_level()) {
ob_end_flush();
}
self::$running = FALSE;
}
public static function redirect($url, $method='302')
{
session_write_close();
if (headers_sent()) {
echo "<script>document.location.href='" .$url. "';</script>n";
} else {
$codes = array(
'refresh' => 'Refresh',
'300' => 'Multiple Choices',
'301' => 'Moved Permanently',
'302' => 'Found',
'303' => 'See Other',
'304' => 'Not Modified',
'305' => 'Use Proxy',
'307' => 'Temporary Redirect'
);
$method = isset($codes[$method]) ? (string) $method : '302';
if ($method == 'refresh') {
header('Refresh: 0; url='.$uri);
} else {
header('HTTP/1.1 '.$method.' '.$codes[$method]);
header('Location: '. $url);
}
}
die('Never Reached!');
}
public static function debug()
{
if (func_num_args() === 0) {
return;
}
$params = func_get_args();
$output = array();
foreach ($params as $var) {
$output[] = '<pre>('.gettype($var).') '.htmlspecialchars(print_r($var, TRUE), ENT_QUOTES, 'UTF-8').'</pre>';
}
return implode("n", $output);
}
public static function auto_load($class)
{
if (class_exists($class, FALSE)) {
return;
}
$class = strtolower(substr($class, 1));
self::$libraries['framework.'.$class] = 1;
require LIBRARY_DIR.'/framework/'.$class.'.php';
}
public static function dump($var)
{
return self::$$var;
}
private static function __cache_cfg($name, $type='module')
{
$db = self::factory('database');
$db->query("SELECT config_cache FROM #__".$type." WHERE name = '".$db->escape($name)."' LIMIT 1");
if ($db->affected_rows()) {
$name = ($type == 'config') ? 'config' : $name;
self::cache_set($name, unserialize(stripslashes($db->fetch_field('config_cache'))), $type);
} else {
die('Failed to load '.$name.':'.$type.' configuration cache!');
}
}
public static function cache_exists($cache_id, $type='module')
{
if (USE_APC === TRUE) {
return apc_exists($type.'_'.$cache_id);
}
$file = self::cache_file($cache_id, $type);
if (file_exists($file) && is_file($file)) {
return TRUE;
}
return FALSE;
}
public static function cache_set($cache_id, $data, $type='module', $serialize=TRUE)
{
if (USE_APC === TRUE) {
return apc_store($type.'_'.$cache_id, $data, 0);
}
$file = self::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) {
$value = (strpos($value, "'") !== false) ? str_replace(''', '\'', $value) : $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;
}
public static function cache_get($cache_id, $type='module', $unserialize=TRUE)
{
if (USE_APC === TRUE) {
return apc_fetch($type.'_'.$cache_id);
}
clearstatcache();
$file = self::cache_file($cache_id, $type);
if (file_exists($file)) {
if ($unserialize === TRUE) {
return unserialize(file_get_contents($file));
} else {
$data = require $file;
return $data;
}
}
return FALSE;
}
public static function cache_del($cache_id, $type='module')
{
if (USE_APC === TRUE) {
return apc_delete($type.'_'.$cache_id);
}
$file = self::cache_file($cache_id, $type);
if (file_exists($file)) {
unlink($file);
}
}
private static function cache_file($cache_id, $type='module')
{
$secret = self::cfg_item('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';
}
}
if (!function_exists('apc_exists')) {
function apc_exists($cache_id)
{
return (bool) apc_fetch($cache_id);
}
}
?>