<?
session_start();
header( 'Content-type: text/html; charset=windows-1251' );
if (!isset($_SESSION['username']) and !isset($_SESSION['password'])) {
    exit();
}
include '../sfb.php';
include 'functiones.php';

if ($user_status != 'admin') {
    exit();
}

define( 'ROOT', $_SERVER['HTTP_HOST'] );
define( 'ROOT_DIR', $_SERVER['DOCUMENT_ROOT'] );

define('webid', true);

$dyncount = mysql_fetch_array(mysql_query("SELECT count(*) as count FROM tb_ads WHERE modered = 0"));
$pmcount = mysql_fetch_array(mysql_query("SELECT count(*) as count FROM seo_paymails WHERE modered = 0"));
$taskscount = mysql_fetch_array(mysql_query("SELECT count(*) as count FROM seo_tasks WHERE modered = 0"));
$statlinkcount = mysql_fetch_array(mysql_query("SELECT count(*) as count FROM tb_statlinks WHERE modered = 0"));
$cepcount = mysql_fetch_array(mysql_query("SELECT count(*) as count FROM cep_links WHERE modered = 0"));
$ban1count = mysql_fetch_array(mysql_query("SELECT count(*) as count FROM tb_advban WHERE modered = 0"));
$ban2count = mysql_fetch_array(mysql_query("SELECT count(*) as count FROM tb_advban_100 WHERE modered = 0"));
$ban3count = mysql_fetch_array(mysql_query("SELECT count(*) as count FROM tb_advban_300 WHERE modered = 0"));
$paycount = mysql_fetch_array(mysql_query("SELECT count(*) as count FROM tb_payme"));
$summoney = mysql_fetch_array(mysql_query("SELECT SUM(money) as sum FROM tb_users"));
$adsummoney = mysql_fetch_array(mysql_query("SELECT SUM(admoney) as adsum FROM tb_users"));
$masrefcount = mysql_fetch_array(mysql_query("SELECT count(*) as count FROM masref WHERE active = 0"));
//Сумма рекламных площадок
$rs1 = mysql_fetch_array(mysql_query("SELECT SUM(balance) as sum FROM tb_ads"));
$rs2 = mysql_fetch_array(mysql_query("SELECT SUM(balance) as sum FROM seo_paymails"));
$rs3 = mysql_fetch_array(mysql_query("SELECT SUM(balance) as sum FROM seo_tasks"));
$rs4 = mysql_fetch_array(mysql_query("SELECT SUM(balance) as sum FROM tb_statlinks"));
$rs5 = mysql_fetch_array(mysql_query("SELECT SUM(balance) as sum FROM tb_advban"));
$sumreclama = $rs1['sum'] + $rs2['sum'] + $rs3['sum'] + $rs4['sum'] + $rs5['sum'];

/// конкурсы ------------------------------------
    $get_end_kon1r = mysql_fetch_array(mysql_query("SELECT * FROM konkurs WHERE end_date <= '".date('d.m.Y', time())."' AND type = 1 ORDER by id DESC"));
    $get_end_kon1k = mysql_fetch_array(mysql_query("SELECT * FROM konkurs WHERE end_date <= '".date('d.m.Y', time())."' AND type = 2 ORDER by id DESC"));
    #рекламодателя
    if($get_end_kon1r['id'] > 0)  {
        $lider_konr = mysql_query("SELECT * FROM konkurs_temp WHERE summ >= '".$get_end_kon1r['min_type']."' and type = '1' AND idkonkurs = '".$get_end_kon1r['id']."' ORDER BY summ DESC limit 3");
        if (mysql_num_rows($lider_konr) > 0) {
            for ($ir = 1; $ir <= mysql_num_rows($lider_konr); $ir++) {
                $lider_rowr = mysql_fetch_array($lider_konr);
                if($ir == 1) { $user1k = $lider_rowr['iduser'];} else
			    if($ir == 2) { $user2k = $lider_rowr['iduser'];} else
			    if($ir == 3) { $user3k = $lider_rowr['iduser'];} 
			}
			mysql_query("INSERT INTO konkurs_end (idkonkurs, type, user1, price1, user2, price2, user3, price3) VALUES ('$get_end_kon1r[id]', '$get_end_kon1r[type]', '$user1k', '$get_end_kon1r[price1]', '$user2k', '$get_end_kon1r[price2]', '$user3k', '$get_end_kon1r[price3]')"); 
			mysql_query("DELETE FROM konkurs_temp WHERE type = 1"); 
            mysql_query("DELETE FROM konkurs WHERE type = 1"); 
        }
    }
	#рефералов
	if($get_end_kon1k['id'] > 0)  {
        $lider_konk = mysql_query("SELECT * FROM konkurs_temp WHERE summ >= '".$get_end_kon1k['min_type']."' and type = '2' AND idkonkurs = '".$get_end_kon1k['id']."' ORDER BY summ DESC limit 3");
        if (mysql_num_rows($lider_konk) > 0) {
            for ($ik = 1; $ik <= mysql_num_rows($lider_konk); $ik++) {
                $lider_rowr = mysql_fetch_array($lider_konk);
                if($ik == 1) { $user1k = $lider_rowr['iduser'];} else
			    if($ik == 2) { $user2k = $lider_rowr['iduser'];} else
			    if($ik == 3) { $user3k = $lider_rowr['iduser'];} 
			}
			mysql_query("INSERT INTO konkurs_end (idkonkurs, type, user1, price1, user2, price2, user3, price3) VALUES ('$get_end_kon1k[id]', '$get_end_kon1k[type]', '$user1k', '$get_end_kon1k[price1]', '$user2k', '$get_end_kon1k[price2]', '$user3k', '$get_end_kon1k[price3]')"); 
			mysql_query("DELETE FROM konkurs_temp WHERE type = 2"); 
            mysql_query("DELETE FROM konkurs WHERE type = 2"); 
        }
    }
/// ---------------------------------------------
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251" />
<title>Администрирование</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../theme/js/jquery.min.js"></script>
</head>
<body>
<table width="100%" style="height: 100%;" border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td colspan="2" style="background: #333333; padding: 5px; height: 20px;"><div style="position: relative;">&nbsp;<a href="/admin" class="link2">Главная</a>&nbsp;&nbsp;&nbsp;<a href="/admin/?adm=13" class="link2">Sitemap</a>&nbsp;&nbsp;&nbsp;<div style="position: absolute; right: 5px; top: 0;"><font color="#ebebeb">Сумма Рекламных площадок - </font> <font color="#E50101"><?=moneyraund($sumreclama, 3)?></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#ebebeb">Сумма балансов - </font> <font color="#E50101"><?=moneyraund($summoney['sum'], 3)?></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#ebebeb">Сумма рекламных балансов - </font> <font color="#E50101"><?=moneyraund($adsummoney['adsum'], 3)?></font></div></div></td>
    </tr>
    <tr>
        <td valign="top" align="center" style="padding-top: 20px; padding-bottom: 10px;"><?include 'engine.php'?></td>
        <td width="200" valign="top" style="background: url(img/rbg.jpg);"><?include 'menu.php'?></td>
    </tr>
</table>
</body>
</html>