<?php
/**
* $Revision: 177 $
* $Author: CrashX $
* $Date: 2010-02-02 14:27:01 +0600 (Вт, 02 фев 2010)$
* $LastChangedDate: 2010-10-22 13:06:26 +0700 (Пт, 22 окт 2010) $
* $Id: variable.php 177 2010-10-22 06:06:26Z CrashX $
* Copyright © CrashX <XSiteCMS@gmail.com>
* Всі права захищено © CrashX
*/
if (!defined('_SHELL'))
die();
/**
* Класс Переменных
*/
class Variable {
var $version = 0.01;
/**
* Проверка переменной массива GET при register_globals = Off & safe_mode = On
* @param string имя переменной
* @return mixed
*/
function is($var=null) {
$this->vars($var, null);
return $var;
}
/**
* Изъятие переменной из массива GET при register_globals = Off & safe_mode = On
* @param string имя переменной
* @return mixed
*/
function get(&$var=null) {
if (is_array($var)):
foreach ($var as $column => $value):
isset($_GET[$column]) ? $var[$column] = $_GET[$column] : $var[$column] = null;
endforeach;
unset($column);
unset($value);
else:
isset($_GET[$var]) ? $var = $_GET[$var] : $var = null;
endif;
return $var;
}
/**
* Изъятие переменной из массива POST при register_globals = Off & safe_mode = On
* @param string имя переменной
* @return mixed
*/
function post(&$var=null, $free=true) {
if (is_array($var)):
foreach ($var as $column => $value):
isset($_POST[$column]) ? $var[$column] = $_POST[$column] : $var[$column] = null;
endforeach;
unset($column);
unset($value);
else:
isset($_POST[$var]) ? $var = $_POST[$var] : $var = null;
endif;
if ($free):
unset($_POST);
endif;
return $var;
}
/**
* Изъятие переменной из массивов при register_globals = Off & safe_mode = On
* @param string имя переменной
* @return mixed
*/
function vars(&$var=null, $default=null) {
if (is_array($var)):
foreach ($var as $column => $value):
if (isset($_GET[$column])):
$var[$column] = $_GET[$column];
elseif (isset($_POST[$column])):
$var[$column] = $_POST[$column];
else:
$var[$column] = $default;
endif;
endforeach;
unset($column);
unset($value);
else:
if (isset($_GET[$var])):
$var = $_GET[$var];
elseif (isset($_POST[$var])):
$var = $_POST[$var];
else:
$var = $default;
endif;
endif;
return $var;
}
/**
* Уничтожение переменной
* @param string имя переменной
*/
function free($uses) {
$GLOBALS[$uses] = null;
unset($GLOBALS[$uses]);
unset($uses);
}
/**
* Удаляет экранирующие бэкслэши в массиве
* @param array $array
* @return array $array
*/
function stripslash(&$array) {
if (is_array($array)):
reset($array);
foreach ($array as $key => $value):
if (is_array($array[$key])):
$this->stripslash($array[$key]);
else:
$array[$key] = stripslashes($value);
endif;
endforeach;
else:
$array = stripslashes($array);
endif;
return $array;
}
/**
* Удаляет экранирующие бэкслэши в массиве
* @param array $array
* @return array $array
*/
function addslash(&$array) {
reset($array);
foreach ($array as $key => $value):
if (is_array($array[$key])): $this->addslash($array[$key]);
else: $array[$key] = str_replace(array("/", "."), array("\\/", "\\."), $folder);
endif;
endforeach;
return $array;
}
/**
* Браузер
* @return mixed браузер, бот или false если не определен
*/
function browser() {
global $HTTP_USER_AGENT;
if (empty($HTTP_USER_AGENT)):
if (!empty($_SERVER) && isset($_SERVER['HTTP_USER_AGENT'])): $HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
elseif (@getenv('HTTP_USER_AGENT')): $HTTP_USER_AGENT = getenv('HTTP_USER_AGENT');
endif;
endif;
$browser = array(
// браузеры
"msie" => "Internet Explorer",
"opera" => "Opera", "mozilla" => "Mozilla",
"firebird" => "Mozilla Firebird",
"firefox" => "Mozilla Firefox",
"go!zilla" => "Go!Zilla", "konqueror" => "Konqueror",
"teleport" => "TelePort Pro",
"webcapture" => "Acrobat",
"webcopier" => "WebCopier",
// телефоны
"alcatel" => "Alcatel Browser (PDA/Phone browser)",
"mot-" => "Motorola Browser (PDA/Phone browser)",
"nokia" => "Nokia Browser (PDA/Phone browser)", "panasonic" => "Panasonic Browser (PDA/Phone browser)",
"philips" => "Philips Browser (PDA/Phone browser)", "sonyericsson" => "Sony/Ericsson Browser (PDA/Phone browser)",
"docomo" => "I-Mode phone (PDA/Phone browser)", "portalmmm" => "I-Mode phone (PDA/Phone browser)",
// валидаторы
"csscheck" => "WDG CSS Validator",
"w3m" => "W3M Validator",
"w3c_css_validator" => "W3C CSS Validator",
"w3c_validator" => "W3C HTML Validator",
"wdg_validator" => "WDG HTML Validator",
// поисковики
"Yandex" => "Yandex",
"google" => "Google",
"Googlebot" => "Google",
"Mediapartners-Google" => "Google",
"StackRambler" => "Rambler",
"stack" => "Rambler",
"rambler" => "Rambler",
"Mail.Ru" => "Mail.Ru",
"meta" => "Meta",
"fast" => "Fast",
"aport" => "Aport",
"yahoo" => "Yahoo",
"Yahoo-MMCrawler" => "Yahoo",
"lycos" => "Lycos",
"MSNBot" => "MNS\Live",
"Teoma" => "ASK",
"ia_archiver" => "Alexa",
"Crawler/2.0" => "WebAlta",
"Slurp" => "Slurp",
"Wget" => "Wget",
"WebCrawler" => "WebCrawler",
"ZyBorg" => "ZyBorg",
"scooter" => "AltaVista",
);
if (preg_match("/msie[\/\sa-z]*([\d\.]*)/i", $HTTP_USER_AGENT, $matches) && !preg_match("/webtv/i", $HTTP_USER_AGENT) && !preg_match("/opera/i", $HTTP_USER_AGENT)): return "Internet Explorer " . $matches[1];
elseif (preg_match("/netscape.?\/([\d\.]*)/i", $HTTP_USER_AGENT, $matches)): return "Netscape " . $matches[1];
elseif (preg_match("/mozilla[\/\sa-z]*([\d\.]*)/i", $HTTP_USER_AGENT, $matches) && !preg_match("/compatible/i", $HTTP_USER_AGENT) && !preg_match("/opera/i", $HTTP_USER_AGENT)): return "Netscape " . $matches[2];
else:
foreach ($browser as $key => $string):
if (preg_match("/$key.?\/([\d\.]*)/i", $HTTP_USER_AGENT, $matches)): return $browser[$key] . " " . $matches[1];
endif;
endforeach;
endif;
return $HTTP_USER_AGENT;
}
/**
* Запрашиваемый адрес (URI)
* @return mixed URI или false если не определен
*/
function uri() {
global $REQUEST_URI;
if (empty($REQUEST_URI)):
if (!empty($_SERVER) && isset($_SERVER['REQUEST_URI'])): $REQUEST_URI = $_SERVER['REQUEST_URI'];
elseif (@getenv('REQUEST_URI')): $REQUEST_URI = getenv('REQUEST_URI');
endif;
endif;
if (!empty($REQUEST_URI)): return $REQUEST_URI;
endif;
return false;
}
/**
* Страницу-рефферер, откуда пришел пользователь (REFERER)
* @return mixed REFERER или false если не определен
*/
function referer() {
global $HTTP_REFERER;
if (empty($HTTP_REFERER)):
if (!empty($_SERVER) && isset($_SERVER['HTTP_REFERER'])): $HTTP_REFERER = $_SERVER['HTTP_REFERER'];
elseif (@getenv('HTTP_REFERER')): $HTTP_REFERER = getenv('HTTP_REFERER');
endif;
endif;
if (!empty($HTTP_REFERER)): return $HTTP_REFERER;
endif;
return false;
}
/**
* Определьне хоста (HOST)
* @return mixed имя HOST или false если не определен
*/
function host() {
global $REMOTE_HOST;
global $HTTP_HOST;
if (empty($REMOTE_HOST)):
if (!empty($_SERVER) && isset($_SERVER['REMOTE_HOST'])): $REMOTE_HOST = $_SERVER['REMOTE_HOST'];
elseif (@getenv('REMOTE_HOST')): $REMOTE_HOST = getenv('REMOTE_HOST');
endif;
endif;
if (empty($HTTP_HOST)):
if (!empty($_SERVER) && isset($_SERVER['HTTP_HOST'])): $HTTP_HOST = $_SERVER['HTTP_HOST'];
elseif (@getenv('HTTP_HOST')): $HTTP_HOST = getenv('HTTP_HOST');
endif;
endif;
if (!empty($REMOTE_HOST) && strtolower($REMOTE_HOST) != 'unknown'): return $REMOTE_HOST;
elseif (!empty($HTTP_HOST) && strtolower($HTTP_HOST) != 'unknown'): return $HTTP_HOST;
endif;
return false;
}
/**
* IP адрес текущего пользователя
* @return mixed IP адрес XXX.XXX.XXX.XXX или false если не определен
*/
function ip() {
global $REMOTE_ADDR;
global $HTTP_CLIENT_IP;
global $HTTP_X_REAL_IP, $HTTP_X_FORWARDED_FOR, $HTTP_X_FORWARDED, $HTTP_FORWARDED_FOR, $HTTP_FORWARDED;
global $HTTP_VIA, $HTTP_X_COMING_FROM, $HTTP_COMING_FROM;
$ip = null;
if (empty($REMOTE_ADDR)):
if (!empty($_SERVER) && isset($_SERVER['REMOTE_ADDR'])): $REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
elseif (!empty($_ENV) && isset($_ENV['REMOTE_ADDR'])): $REMOTE_ADDR = $_ENV['REMOTE_ADDR'];
elseif (@getenv('REMOTE_ADDR')): $REMOTE_ADDR = getenv('REMOTE_ADDR');
endif;
endif;
if (empty($HTTP_CLIENT_IP)):
if (!empty($_SERVER) && isset($_SERVER['HTTP_CLIENT_IP'])): $REMOTE_ADDR = $_SERVER['HTTP_CLIENT_IP'];
elseif (!empty($_ENV) && isset($_ENV['HTTP_CLIENT_IP'])): $REMOTE_ADDR = $_ENV['HTTP_CLIENT_IP'];
elseif (@getenv('HTTP_CLIENT_IP')): $HTTP_CLIENT_IP = getenv('HTTP_CLIENT_IP');
endif;
endif;
if (empty($HTTP_X_REAL_IP)):
if (!empty($_SERVER) && isset($_SERVER['HTTP_X_REAL_IP'])): $HTTP_X_REAL_IP = $_SERVER['HTTP_X_REAL_IP'];
elseif (!empty($_ENV) && isset($_ENV['HTTP_X_REAL_IP'])): $HTTP_X_REAL_IP = $_ENV['HTTP_X_REAL_IP'];
elseif (@getenv('HTTP_X_REAL_IP')): $HTTP_X_REAL_IP = getenv('HTTP_X_REAL_IP');
endif;
endif;
if (empty($HTTP_X_FORWARDED_FOR)):
if (!empty($_SERVER) && isset($_SERVER['HTTP_X_FORWARDED_FOR'])): $HTTP_X_FORWARDED_FOR = $_SERVER['HTTP_X_FORWARDED_FOR'];
elseif (!empty($_ENV) && isset($_ENV['HTTP_X_FORWARDED_FOR'])): $HTTP_X_FORWARDED_FOR = $_ENV['HTTP_X_FORWARDED_FOR'];
elseif (@getenv('HTTP_X_FORWARDED_FOR')): $HTTP_X_FORWARDED_FOR = getenv('HTTP_X_FORWARDED_FOR');
endif;
endif;
if (empty($HTTP_X_FORWARDED)):
if (!empty($_SERVER) && isset($_SERVER['HTTP_X_FORWARDED'])): $HTTP_X_FORWARDED = $_SERVER['HTTP_X_FORWARDED'];
elseif (!empty($_ENV) && isset($_ENV['HTTP_X_FORWARDED'])): $HTTP_X_FORWARDED = $_ENV['HTTP_X_FORWARDED'];
elseif (@getenv('HTTP_X_FORWARDED')): $HTTP_X_FORWARDED = getenv('HTTP_X_FORWARDED');
endif;
endif;
if (empty($HTTP_FORWARDED_FOR)):
if (!empty($_SERVER) && isset($_SERVER['HTTP_FORWARDED_FOR'])): $HTTP_FORWARDED_FOR = $_SERVER['HTTP_FORWARDED_FOR'];
elseif (!empty($_ENV) && isset($_ENV['HTTP_FORWARDED_FOR'])): $HTTP_FORWARDED_FOR = $_ENV['HTTP_FORWARDED_FOR'];
elseif (@getenv('HTTP_FORWARDED_FOR')): $HTTP_FORWARDED_FOR = getenv('HTTP_FORWARDED_FOR');
endif;
endif;
if (empty($HTTP_FORWARDED)):
if (!empty($_SERVER) && isset($_SERVER['HTTP_FORWARDED'])): $HTTP_FORWARDED = $_SERVER['HTTP_FORWARDED'];
elseif (!empty($_ENV) && isset($_ENV['HTTP_FORWARDED'])): $HTTP_FORWARDED = $_ENV['HTTP_FORWARDED'];
elseif (@getenv('HTTP_FORWARDED')): $HTTP_FORWARDED = getenv('HTTP_FORWARDED');
endif;
endif;
if (empty($HTTP_VIA)):
if (!empty($_SERVER) && isset($_SERVER['HTTP_VIA'])): $HTTP_VIA = $_SERVER['HTTP_VIA'];
elseif (!empty($_ENV) && isset($_ENV['HTTP_VIA'])): $HTTP_VIA = $_ENV['HTTP_VIA'];
elseif (@getenv('HTTP_VIA')): $HTTP_VIA = getenv('HTTP_VIA');
endif;
endif;
if (empty($HTTP_X_COMING_FROM)):
if (!empty($_SERVER) && isset($_SERVER['HTTP_X_COMING_FROM'])): $HTTP_X_COMING_FROM = $_SERVER['HTTP_X_COMING_FROM'];
elseif (!empty($_ENV) && isset($_ENV['HTTP_X_COMING_FROM'])): $HTTP_X_COMING_FROM = $_ENV['HTTP_X_COMING_FROM'];
elseif (@getenv('HTTP_X_COMING_FROM')): $HTTP_X_COMING_FROM = getenv('HTTP_X_COMING_FROM');
endif;
endif;
if (empty($HTTP_COMING_FROM)):
if (!empty($_SERVER) && isset($_SERVER['HTTP_COMING_FROM'])): $HTTP_COMING_FROM = $_SERVER['HTTP_COMING_FROM'];
elseif (!empty($_ENV) && isset($_ENV['HTTP_COMING_FROM'])): $HTTP_COMING_FROM = $_ENV['HTTP_COMING_FROM'];
elseif (@getenv('HTTP_COMING_FROM')): $HTTP_COMING_FROM = getenv('HTTP_COMING_FROM');
endif;
endif;
if (!empty($REMOTE_ADDR)): $ip = $REMOTE_ADDR;
endif;
if (!empty($HTTP_CLIENT_IP)): $ip = $HTTP_CLIENT_IP;
endif;
$proxy = null;
if (!empty($HTTP_X_REAL_IP)): $proxy = $HTTP_X_REAL_IP;
elseif (!empty($HTTP_X_FORWARDED_FOR) && (strtolower($HTTP_X_FORWARDED_FOR) != 'unknown' && strtolower($HTTP_X_FORWARDED_FOR) != 'undefine' && $HTTP_X_FORWARDED_FOR != 'undefine')): $proxy = $HTTP_X_FORWARDED_FOR;
elseif (!empty($HTTP_X_FORWARDED)): $proxy = $HTTP_X_FORWARDED;
elseif (!empty($HTTP_FORWARDED_FOR)): $proxy = $HTTP_FORWARDED_FOR;
elseif (!empty($HTTP_FORWARDED)): $proxy = $HTTP_FORWARDED;
elseif (!empty($HTTP_VIA)): $proxy = $HTTP_VIA;
elseif (!empty($HTTP_X_COMING_FROM)): $proxy = $HTTP_X_COMING_FROM;
elseif (!empty($HTTP_COMING_FROM)): $proxy = $HTTP_COMING_FROM;
endif;
if (empty($proxy)): return $ip;
else: $is_ip = preg_match('|^([0-9]{1,3}\.){3,3}[0-9]{1,3}|', $proxy, $regs);
if ($is_ip && (count($regs) > 0)): return $regs[0];
endif;
endif;
return false;
}
/**
* Фильтраця IP адресов
* @param array Диапазоны фильтрации
* @return boolean
*/
function ip_filter($ip_array) {
/*
$ip=explode(".",$this->ip());
if(count($ip_array) == 0): return false; else:
foreach($ip_array as $key=>$string):
list($ip['ip1'],$ip['ip2'])=explode(":|:",$string);
$ip['ip1']=explode(".",$ip['ip1']);
$ip['ip2']=explode(".",$ip['ip2']);
if((($ip[0]>=$ip['ip1'][0]) && ($ip[0]<=$ip['ip2'][0]))&&(($ip[1]>=$ip['ip1'][1]) && ($ip[1]<=$ip['ip2'][1]))&&(($ip[2]>=$ip['ip1'][2]) && ($ip[2]<=$ip['ip2'][2]))&&(($ip[3]>=$ip['ip1'][3]) && ($ip[3]<=$ip['ip2'][3]))): return true; endif;
endforeach;
endif;
return false;
*/
global $engine;
$ip = $this->ip();
if (count($ip_array) == 0):
return false;
else:
foreach ($ip_array as $key => $string):
$ban = '/^' . str_replace('*', '(\d*)', str_replace('.', '\\.', $engine->string->trim($string))) . '$/';
if (preg_match($ban, $ip)):
return true;
endif;
endforeach;
endif;
return false;
}
/**
* Эмуляция register globals = off
*/
function unregister_globals() {
$REQUEST = $_REQUEST;
$GET = $_GET;
$POST = $_POST;
$COOKIE = $_COOKIE;
if (isset($_SESSION)) {
$SESSION = $_SESSION;
}
$FILES = $_FILES;
$ENV = $_ENV;
$SERVER = $_SERVER;
foreach ($GLOBALS as $key => $value) {
if ($key != 'GLOBALS') {
unset($GLOBALS [$key]);
}
}
$_REQUEST = $REQUEST;
$_GET = $GET;
$_POST = $POST;
$_COOKIE = $COOKIE;
if (isset($SESSION)) {
$_SESSION = $SESSION;
}
$_FILES = $FILES;
$_ENV = $ENV;
$_SERVER = $SERVER;
}
function compatibility() {
if ((!isset($_GET) && !isset($_POST) && !isset($_SERVER)) || ((isset($HTTP_POST_VARS) || isset($HTTP_GET_VARS)))):
$_GET = @$HTTP_GET_VARS;
$_REQUEST = array_merge($_POST, $_GET);
$_POST = @$HTTP_POST_VARS;
$_COOKIE = @$HTTP_COOKIE_VARS;
$_SESSION = @$HTTP_SESSION_VARS;
$_ENV = @$HTTP_ENV_VARS;
$_SERVER = @$HTTP_SERVER_VARS;
return true;
endif;
return false;
}
}
?>