<?

session_start();
// указываем папку, куда будет сохраняться информация сессий
session_save_path( $_SERVER["DOCUMENT_ROOT"]."/logs/session" );
// устанавливаем защиту по IP (сессии) не чаще, чем раз в три секунды.
if ( isset( $_SESSION['refresh_access'] ) ) {
    $t = ( ( int ) ( ( time() - $_SESSION['refresh_access'] ) ) );
    if ( $t < 3 ) die( 'Нельзя обращаться к сайту чаще, чем 1 раз в три секунды!' );
}

$_SESSION['refresh_access'] = time();

define( 'ROOT_DIR', $_SERVER['DOCUMENT_ROOT'] );
include_once ROOT_DIR . '/engine/config.php';

$get_klink = mysql_query( "SELECT id, url, balance, statistic FROM klink WHERE id = '" . ( int ) $_GET['id'] . "' and paused = 1 LIMIT 1" );

if ( mysql_num_rows( $get_klink ) > 0 ) {
    $get_klink_row = mysql_fetch_array( $get_klink );
    
    if ( $get_klink_row['balance'] < $config['klink'] ) {
        exit();
    } else {
        mysql_query( "UPDATE klink SET balance = balance - '" . $config['klink'] . "', visits = visits + 1 WHERE id = '" . ( int ) $_GET['id'] . "' and paused = 1" );
        mysql_query( "UPDATE viruchka SET money = money + '" . $config['klink'] . "' WHERE ident = 2" );
        
        /*---Создаем статистику по дням---*/
        if ( empty( $get_klink_row['statistic'] ) ) {
            $array[date( 'dmY' )] = 1;
            mysql_query( "UPDATE klink SET statistic = '" . serialize( $array ) . "' WHERE id = '" . $get_klink_row['id'] . "'" );
        } else {
            $array = unserialize( $get_klink_row['statistic'] );
            if ( array_key_exists( date( 'dmY' ), $array ) ) {
                $arrayValue = $array[date( 'dmY' )];
                $arrayValueNew = array( date( 'dmY' ) => $arrayValue + 1 );
                $array = array_replace( $array, $arrayValueNew );
                mysql_query( "UPDATE klink SET statistic = '" . serialize( $array ) . "' WHERE id = '" . $get_klink_row['id'] . "'" );
            } else {
                $array[date( 'dmY' )] = 1;
                mysql_query( "UPDATE klink SET statistic = '" . serialize( $array ) . "' WHERE id = '" . $get_klink_row['id'] . "'" );
            }
        }
        ?>
        <meta http-equiv="refresh" content="0; url=<?=$get_klink_row['url']?>" />
        <?
    }
}

?>