<?php

/************************************************************************/
/* PHP-NUKE: Web Portal System                                          */
/* ===========================                                          */
/*                                                                      */
/* Copyright (c) 2002 by Francisco Burzi                                */
/* http://phpnuke.org                                                   */
/*                                                                      */
/* Note: If you need more than one banner block, just copy this file    */
/*       with another name                                              */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/

if (eregi("block-Advertising.php",$PHP_SELF)) {
    Header("Location: index.php");
    die();
}

global $prefix, $dbi;

$bresult = sql_query("select * from ".$prefix."_banner WHERE type='1' AND active='1'", $dbi);
$numrows = sql_num_rows($bresult, $dbi);

if ($numrows>1) {
    $numrows = $numrows-1;
    mt_srand((double)microtime()*1000000);
    $bannum = mt_rand(0, $numrows);
} else {
    $bannum = 0;
}
$bresult2 = sql_query("select bid, imageurl from ".$prefix."_banner WHERE type='1' AND active='1' limit $bannum,1", $dbi);
list($bid, $imageurl) = sql_fetch_row($bresult2, $dbi);
    
if (is_admin($admin)) {
} else {
    sql_query("update ".$prefix."_banner set impmade=impmade+1 where bid='$bid'", $dbi);
}
if($numrows>0) {
    $aborrar = sql_query("select cid, imptotal, impmade, clicks, date from ".$prefix."_banner where bid=$bid", $dbi);
    list($cid, $imptotal, $impmade, $clicks, $date) = sql_fetch_row($aborrar, $dbi);

/* Check if this impression is the last one and print the banner */

    if (($imptotal <= $impmade) AND ($imptotal != 0)) {
        sql_query("update ".$prefix."_banner set active='0' WHERE bid='$bid'", $dbi);
    }
    $content = "<center><br><a href=\"banners.php?op=click&amp;bid=$bid\" target=\"_blank\"><img src=\"$imageurl\" border=\"1\" alt=\"\"></a><br></center>";
}

?>