<?PHP

extract($_GET); extract($_POST); extract($_SERVER); extract($_ENV); extract($_REQUEST); extract($_FILES);


$id=$ID;

$VARS[DDIR] = './data';

require("$VARS[DDIR]/vars.php");

if( $VARS[PCONN] )
  $dbh = mysql_pconnect($host, $user, $pass);
else
  $dbh = mysql_connect($host, $user, $pass);

mysql_select_db($db, $dbh);

if( $REQUEST_METHOD == 'POST' ) {
  giveHit( $id );
}
else {

  if( $VARS[H_GAT] )
    displayGate( $id );
  else
    giveHit( $id );

}

exit;

function giveHit( $id ) {
  global $VARS, $HTTP_VIA, $HTTP_X_FORWARDED_FOR, $REMOTE_ADDR, $HTTP_COOKIE, $HTTP_ACCEPT, $HTTP_USER_AGENT, $HTTP_REFERER, $ses, $dbh;

  // If using the gateway, and requiring cookie enabled browser, check the cookie_id cookie
  if( $VARS[H_RCK] && $VARS[H_GAT] && !strstr($HTTP_COOKIE, "hitcheck_$id=") )
    cheat( 'no_cook', $id );

  // If using cookies to track hits, check the hit_id cookie
  if( $VARS[H_COK] && strstr($HTTP_COOKIE, "hitmult_$id=") )
    cheat( 'mult_cook', $id );

  // If not allowing proxies, check for proxy server
  if( $VARS[H_PXY] && ($HTTP_VIA || $HTTP_X_FORWARDED_FOR) )
    cheat( 'use_proxy', $id );

  // Check for a valid browser
  if( !$HTTP_ACCEPT || !$HTTP_USER_AGENT )
    cheat( 'bad_brwsr', $id );
  
  // If using the gateway, check for valid referer
  if( $VARS[H_GAT] && !strstr($HTTP_REFERER, $VARS[IU]) )
    cheat( 'no_refer', $id );

  // If using the gateway, check the session
  if( $VARS[H_GAT] && checkSession($ses) )
    cheat( 'expr_gate', $id );

  // If using banned IPs, check the banned list
  if( $VARS[H_BIP] )
    checkBannedIP( $id, $REMOTE_ADDR );

  // If using IP log, check it
  if( $VARS[H_ILG] )
    checkIP( $id, $REMOTE_ADDR );

  
  if( $VARS[H_COK] ) {
if (!isset($RusMedTopsitesCookie)) {  setcookie("RusMedTopsitesCookie","RusMedTopsitesValue1",time()+3600,"/");}

    setcookie( "hitmult_$id", time(), time() + $VARS[H_ET] );}

  // Update hit count
  mysql_query("UPDATE arp_hits SET recv=recv+1,trecv=trecv+1,inact=0 WHERE user='$id'", $dbh);




  $fwd = $VARS[FU];
  if( $VARS[FCP] ) {
    $val = @mysql_result( @mysql_query("SELECT cat FROM arp_accts WHERE user='$id'"), 0 );
    if( $val )
      $fwd = $VARS[HU] . "/" . html_Name($val);
  }

  header("Location: http://www.rusmedserv.com/");

  if( !$VARS[CRN] )
    checkTime();
}

// Display the gateway page
function displayGate( $id ) {
  global $VARS;

  // If the id was left blank, send surfer to the Forward URL
  if( !$id ) {
    header("Location: http://www.rusmedserv.com/");
    exit;
  }

  $ses = newSession();

  // Set cookie so only cookie enabled browsers can send hits
  if( $VARS[H_RCK] )
    setcookie( "hitcheck_$id", time(), time() + $VARS[H_ET] );
?>

<html>
<head>
  <title>Enter</title>
</head>
<body onLoad="if (self != top) top.location = self.location">

<div align="center">

<font face="Verdana" size="2">

<h2>Enter This Site</h2>

<form name="click" action="<?PHP echo $VARS[IU] ?>" method="POST">
<input type="hidden" name="ses" value="<?PHP echo $ses ?>">
<input type="hidden" name="id" value="<?PHP echo $id ?>">
<script language="JavaScript">
 <!--
 document.write('<a href="javascript:document.click.submit()">Посетить сайт</a>');
 //-->
</script>
<noscript>
<input type="submit" value="Нажми для перехода">
</noscript>
</form>

</font>

</div>

</body>
</html>

<?PHP
}

// Create a new session
function newSession() {
  global $REMOTE_ADDR;

  $ip   = str_replace(".", "", $REMOTE_ADDR);
  $ip   = substr($ip, 0, 9);
  $time = time();

  return base64_encode( strrev($time - $ip) );
}

// Check a session
function checkSession( $ses ) {
  global $REMOTE_ADDR, $VARS, $id;

  $val  = strrev( base64_decode($ses) );
  $ip   = str_replace(".", "", $REMOTE_ADDR);
  $val  = substr($ip, 0, 9) + $val;
  $time = time();

  if( $time - $val > $VARS[H_ST] )
    return true;

  return false;
}

function checkIP( $id, $ip ) {
  global $dbh;

  $res = mysql_query("SELECT COUNT(*) FROM arp_hips WHERE user='$id' AND ip='$ip'", $dbh);
  $row = mysql_fetch_row($res);

  if( $row[0] )
    cheat( 'mult_ip', $id );
  else
    mysql_query("INSERT INTO arp_hips VALUES ( '$id', '$ip' )", $dbh);
}

function checkBannedIP( $id, $ip ) {
  global $dbh;

  $res = mysql_query("SELECT COUNT(*) FROM arp_proxy WHERE ip='$ip'", $dbh);
  $row = mysql_fetch_row($res);

  if( $row[0] )
    cheat( 'banned_ip', $id );
}

#malediven

function cheat( $msg, $id ) {
  global $HTTP_X_FORWARDED_FOR, $REMOTE_ADDR, $VARS, $dbh;

  $cheats = array (
                    'mult_ip'   => 'Множественный клик   -   IP',
                    'mult_cook' => 'Множественный клик - куки',
                    'expr_gate' => 'Сессия устарела',
                    'use_proxy' => 'Используется прокси',
                    'no_refer'  => 'Плохой рефер',
                    'bad_brwsr' => 'Используется программный кликер',
                    'no_cook'   => 'нет поддержки кук',
                    'banned_ip' => 'запрещенный IP'
                  );

  if( $VARS[H_CLG] ) {
    $date = date("H:i m-d-Y");

    $fd = fopen("$VARS[DDIR]/hcheat.log", "a");
    fwrite( $fd, "[ $date ]  [ $cheats[$msg] ]  [ $id ] [ $REMOTE_ADDR ($HTTP_X_FORWARDED_FOR) ]\n" );
    fclose($fd);
  }

  if( $VARS[H_CST] ) {
    mysql_query("UPDATE arp_hcheat SET $msg=$msg+1 WHERE user='$id'", $dbh);
  }

  $fwd = $VARS[FU];
  if( $VARS[FCP] ) {
    $val = @mysql_result( @mysql_query("SELECT cat FROM arp_accts WHERE user='$id'"), 0 );
    if( $val )
      $fwd = $VARS[HU] . "/" . html_Name($val);
  }

  header("Location: http://www.rusmedserv.com/");

  if( !$VARS[CRN] )
    checkTime();
    
  exit;
}

function html_Name( $category ) {
  global $VARS;
  $category = eregi_replace("[^[:alnum:]]", "", strtolower($category));
  return "$category.$VARS[FE]";
}

function checkTime() {
  global $VARS, $dbh;

  $curtime = time();
  $rerank  = frdline("$VARS[DDIR]/times/rerank");
  $reset   = frdline("$VARS[DDIR]/times/reset");
  $clear   = frdline("$VARS[DDIR]/times/hclear");
  $inc     = false;

  if( $curtime - $rerank >= $VARS[RR] ) {
    include('ar.php');
    rerankList();

    write_f("$VARS[DDIR]/times/rerank", $curtime);
    write_f("$VARS[DDIR]/times/rerank.frm", 'in.php');
    $inc = true;
  }

  if( $curtime - $reset >= $VARS[RS] ) {
    if( !$inc ) include('ar.php');
    resetList();
    write_f("$VARS[DDIR]/times/reset", $curtime);
    write_f("$VARS[DDIR]/times/reset.frm", 'in.php');
  }

  if( $curtime - $clear >= $VARS[H_CN] ) {
    mysql_query("DELETE FROM arp_hips", $dbh);
    write_f("$VARS[DDIR]/times/hclear", $curtime);
  }
}

function frdline($file) {
  $fd = @fopen($file, 'r') or error("не могу читать $file");
  $line = fgets($fd, 4096);
  fclose($fd);
  chop($line);
  return $line;
}

function write_f($file, $data) {
  $fd = @fopen($file, 'w') or error("не могу писать $file");
  flock($fd, 2);
  fwrite($fd, $data);
  fclose($fd);
  @chmod($file, 0666);
}

function error( $msg ) {
?>

<html>
<head>
  <title>Непоправимая ошибка</title>
</head>
<body>

<pre>
Непоправимая ошибка
==========================
Описание  :   <?PHP echo "$msg\n" ?>
</pre>

</body>
</html>

<?PHP
    exit;
}

?>