<?

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_banner468x60 = mysql_query( "SELECT id, url, balance, statistic FROM banners WHERE id = '" . ( int ) $_GET['id'] . "' and paused = 1 LIMIT 1" ) or die(mysql_error());

if ( mysql_num_rows( $get_banner468x60 ) > 0 ) {
    $get_banner468x60_row = mysql_fetch_array( $get_banner468x60 );
    
    if ( $get_banner468x60_row['balance'] < $config['banner468x60'] ) {
        exit();
    } else {
        if ( parse_url( $get_banner468x60_row['url'], PHP_URL_SCHEME ) == '' ) {
            $get_banner468x60_row['url'] = 'http://' . $get_banner468x60_row['url'];
        }
        mysql_query( "UPDATE banners SET balance = balance - '" . $config['banner468x60'] . "', visits = visits + 1 WHERE id = '" . ( int ) $_GET['id'] . "' and paused = 1" );
        mysql_query( "UPDATE viruchka SET money = money + '" . $config['banner468x60'] . "' WHERE ident = 3" );
        
        /*---Создаем статистику по дням---*/
        if ( empty( $get_banner468x60_row['statistic'] ) ) {
            $array[date( 'dmY' )] = 1;
            mysql_query( "UPDATE banners SET statistic = '" . serialize( $array ) . "' WHERE id = '" . $get_banner468x60_row['id'] . "'" );
        } else {
            $array = unserialize( $get_banner468x60_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 banners SET statistic = '" . serialize( $array ) . "' WHERE id = '" . $get_banner468x60_row['id'] . "'" );
            } else {
                $array[date( 'dmY' )] = 1;
                mysql_query( "UPDATE banners SET statistic = '" . serialize( $array ) . "' WHERE id = '" . $get_banner468x60_row['id'] . "'" );
            }
        }
        
        header( 'Location: ' . $get_banner468x60_row['url'] );
    }
}

?>