<?
if (!defined('webid')) {
    exit();
}

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

if (isset($_POST['del'])) {
    mysql_query("UPDATE tb_users SET referer = 0 WHERE referer = '" . $_POST['id'] . "'");
    mysql_query("DELETE FROM tb_users WHERE id = '" . $_POST['id'] . "'");
    mysql_query("DELETE FROM tb_forum_mes WHERE userid = '" . $_POST['id'] . "'");
    mysql_query("DELETE FROM tb_forum_topic WHERE avtor = '" . $_POST['id'] . "'");
    mysql_query("DELETE FROM tb_wall WHERE fromus = '" . $_POST['id'] . "'");
}

if (isset($_GET['edit'])) {
    if (isset($_POST['save'])) {
        if ($_POST['adblockpay'] == 'on') {
            $_POST['adblockpay'] = 1;
        } else {
            $_POST['adblockpay'] = 0;
        }
        if ($_POST['adblockforum'] == 'on') {
            $_POST['adblockforum'] = 1;
        } else {
            $_POST['adblockforum'] = 0;
        }
        if ($_POST['adban'] == 'on') {
            $_POST['adban'] = 1;
        } else {
            $_POST['adban'] = 0;
        }
		if ($_POST['adkeyok'] == 'on') {
            $_POST['adkeyok'] = 1;
        } else {
            $_POST['adkeyok'] = 0;
        }		
		if ($_POST['adblockip'] == 'on') {
            $_POST['adblockip'] = $_POST['adlastiplog'];
        } else {
            $_POST['adblockip'] = '';
        }
        if($_POST['adfine'] == 0) {
		    $sqlfine1 = mysql_fetch_array(mysql_query("SELECT * FROM tb_users WHERE id='$_GET[edit]'"));
			if($sqlfine1['finestatus'] != 0) {
		        mysql_query("DELETE FROM seo_basket WHERE fromid = '$sqlfine1[id]' AND type = '8'");
            } 
		} else {
		    $sqlfine2 = mysql_fetch_array(mysql_query("SELECT * FROM tb_users WHERE id='$_GET[edit]'"));
		    if($sqlfine2['finestatus'] == 0) {
    		   
	            mysql_query("UPDATE tb_users SET ban = '1', fine_text = 'Мультиаккаунтинг' WHERE lastiplog = '" . $sqlfine2['lastiplog'] . "' AND id != '$sqlfine2[id]'");
			    mysql_query("UPDATE tb_users SET ban = '1', fine_text = 'Мультиаккаунтинг' WHERE wmid = '" . $sqlfine2['wmid'] . "' AND id != '$sqlfine2[id]' AND wmid != ''");
			    mysql_query("UPDATE tb_users SET fine_text = 'Мультиаккаунтинг' WHERE id='$_GET[edit]'");
			
			}
		}
        mysql_query("UPDATE tb_users SET username='$_POST[aduser]', password='$_POST[adpass]', email='$_POST[ademail]', ip='$_POST[adip]', lastiplog='$_POST[adlastiplog]', referer='$_POST[adref1]', referer2='$_POST[adref2]', payeers='$_POST[adpayeers]', pemail='$_POST[adpemail]', wmid='$_POST[adwmid]', money='$_POST[admoney]', admoney='$_POST[adadmoney]', reyting='$_POST[adstatusr]', points='$_POST[adpoints]', golos='$_POST[adgolos]', user_status='$_POST[adstatus]', keyok='$_POST[adkeyok]', blockip='$_POST[adblockip]', blockpay='$_POST[adblockpay]', blockforum='$_POST[adblockforum]', ban='$_POST[adban]', finestatus='$_POST[adfine]'  WHERE id='$_GET[edit]'"); 
        ?>
        <script type="text/javascript">window.location.replace('/admin/?adm=6');</script>
        <?
    }
    $sql = mysql_fetch_array(mysql_query("SELECT * FROM tb_users WHERE id = '" . $_GET['edit'] . "'"));
    ?>
    <form action="" method="post"> 
    <table width="60%" border="0" cellpadding="0" cellspacing="1" class="tab">
    <thead>
        <th colspan="2" style="background: #ebebeb; padding: 5px;" align="right"><input type="submit" value="Сохранить" id="save" name="save" /></th>
    </thead>
    <tr>
        <td class="lt" width="200"><?=$sql['id']?></td>
        <td class="rt">ID Пользователя</td>
    </tr>
    <tr>
        <td class="lt"><input type="text" name="aduser" size="30" value="<?=$sql['username']?>" /></td>
        <td class="rt">Ник</td>
    </tr>
    <tr>
        <td class="lt"><input type="text" name="adpass" size="30" value="<?=$sql['password']?>" /></td>
        <td class="rt">Пароль</td>
    </tr>
    <tr>
        <td class="lt"><input type="text" name="ademail" size="30" value="<?=$sql['email']?>" /></td>
        <td class="rt">email</td>
    </tr>    
	<tr>
        <td class="lt"><input type="text" name="adlastiplog" size="30" value="<?=$sql['lastiplog']?>" /></td>
        <td class="rt">IP Последнего входа в аккаунт</td>
    </tr>
	<tr>
        <td class="lt"><input type="text" name="adip" size="30" value="<?=$sql['ip']?>" /></td>
        <td class="rt">IP Регистрации</td>
    </tr>
    <tr>
        <td class="lt"><input type="text" name="adref1" size="30" value="<?=$sql['referer']?>" /></td>
        <td class="rt">Реферер</td>
    </tr>
    <tr>
        <td class="lt"><input type="text" name="adref2" size="30" value="<?=$sql['referer2']?>" /></td>
        <td class="rt">Реферер2</td>
    </tr>
    <tr>
        <td class="lt"><input type="text" name="adpayeers" size="30" value="<?=$sql['payeers']?>" /></td>
        <td class="rt">Payeer кошелек</td>
    </tr>
    <tr>
        <td class="lt"><input type="text" name="adpemail" size="30" value="<?=$sql['pemail']?>" /></td>
        <td class="rt">WMR кошелек</td>
    </tr>
    <tr>
        <td class="lt"><input type="text" name="adwmid" size="30" value="<?=$sql['wmid']?>" /></td>
        <td class="rt">WMID</td>
    </tr>
	<tr>
        <td class="lt"><input type="text" name="admoney" size="30" value="<?=moneyraund($sql['money'], 3)?>" /></td>
        <td class="rt">Рабочий счет</td>
    </tr>	
	<tr>
        <td class="lt"><input type="text" name="adadmoney" size="30" value="<?=moneyraund($sql['admoney'], 3)?>" /></td>
        <td class="rt">Рекламный счет</td>
    </tr>
	<tr>
        <td class="lt"><input type="text" name="adstatusr" size="30" value="<?=$sql['reyting']?>" /></td>
        <td class="rt">Рейтинг</td>
    </tr>		
	<tr>
        <td class="lt"><input type="text" name="adpoints" size="30" value="<?=$sql['points']?>" /></td>
        <td class="rt">Поинты</td>
    </tr>	
	<tr>
        <td class="lt"><input type="text" name="adgolos" size="30" value="<?=$sql['golos']?>" /></td>
        <td class="rt">Голоса</td>
    </tr>	
	<tr>
        <td class="lt"><select style="width: 99%;" name="adstatus">
        <option value="admin"<?if($sql['user_status']=='admin') echo 'selected'?>>Администратор</option>
        <option value="arbitr"<?if($sql['user_status']=='arbitr') echo 'selected'?>>Арбитр</option>
        <option value="moder"<?if($sql['user_status']=='moder') echo 'selected'?>>Модератор</option>
        <option value="user"<?if($sql['user_status']=='user') echo 'selected'?>>Пользователь</option>
        </select></td>
        <td class="rt">Системный статус пользователя</td>
    </tr>	
	<tr>
        <td class="lt"><select style="width: 99%;" name="adfine">
        <option value="1"<?if($sql['finestatus']=='1') echo 'selected'?>>Оштрафован</option>
        <option value="0"<?if($sql['finestatus']=='0') echo 'selected'?>>Нет штрафа</option>
        </select></td>
        <td class="rt">Статус штрафника</td>
    </tr>
	<!--tr>
        <td class="lt"><input type="checkbox" <?if($sql['blogaccess']==1) echo 'checked=checked'?> name="adblogaccess" /></td>
        <td class="rt">Блокировка создания блога</td>
    </tr-->
	<tr>
        <td class="lt"><input type="checkbox" <?if($sql['keyok']==1) echo 'checked=checked'?> name="adkeyok" /></td>
        <td class="rt">Активация акаунта пинкодом   <font style="color: #800000;">( пинкод пользователя: <b><?=$sql['actcode']?></b> )</font></td>
    </tr>	
	<tr>
        <td class="lt"><input type="checkbox" <?if($sql['blockip'] != '') echo 'checked=checked'?> name="adblockip" /></td>
        <td class="rt">Блокировка входа в аккаунт по IP</td>
    </tr>	
    <tr>
        <td class="lt"><input type="checkbox" <?if($sql['blockpay']==1) echo 'checked=checked'?> name="adblockpay" /></td>
        <td class="rt">Блокировка выплат</td>
    </tr>
    <tr>
        <td class="lt"><input type="checkbox" <?if($sql['blockforum']==1) echo 'checked=checked'?> name="adblockforum" /></td>
        <td class="rt">Бан на форуме</td>
    </tr>
    <tr>
        <td class="lt"><input type="checkbox" <?if($sql['ban']==1) echo 'checked=checked'?> name="adban" /></td>
        <td class="rt">Бан на проекте</td>
    </tr>    
    </table>
    </form>
    <?
} else {
?>
<style type="text/css">
.cons {
    width: 400px;
    background: #fafafa;
    margin-bottom: 20px;
    padding: 10px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    -khtml-border-radius: 3px;
    border-radius: 3px;
    -moz-box-shadow: 0 0 2px rgba(0,0,0,0.2);
	-webkit-box-shadow: 0 0 2px rgba(0,0,0,0.2);
    -khtml-box-shadow: 0 0 2px rgba(0,0,0,0.2);
    box-shadow: 0 0 2px rgba(0,0,0,0.2);
    
}
</style>
<script type="text/javascript">
function gofilter(mode) {
    setCookie("user",mode,"Mon, 01-Jan-2020 00:00:00 GMT");
    window.location.reload();
    return false;
}

function setCookie (name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}
</script>
<div class="cons">
<form action="" method="post">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="padding: 5px;" width="100"><input type="text" name="names" style="width: 99%;" /></td>
<td width="200" style="padding: 5px;">
    <select name="namev" style="width: 99%;">
	    <option value="id">ID Пользователя</option>
		<option value="username">Имя пользователя</option>
		<option value="referer">ID Реферара</option>
		<option value="ip">IP Регистрации</option>
		<option value="lastiplog">IP Последнего входа</option>
		<option value="blockip">IP Блокировки входа в аккаунт</option>
		<option value="wmid">Wmid Пользователя</option>
		<option value="pemail">Кошелек пользователя</option>
		<option value="email">Имейл пользователя</option>
		<option value="money">Денег на рабочем счете</option>
		<option value="admoney">Денег на рекламном счете</option>
		<option value="visits">Кликов</option>
		<option value="golos">Голосов</option>
		<option value="points">Поинтов</option>
		<option value="reyting">Рейтинка</option>
	</select>
</td>
<td style="padding: 5px;"><input type="submit" name="search" value="Поиск" id="save" /></td>
</tr>
</table>
</form>
</div>
<?
if (isset($_COOKIE['user'])) {
    $filter = $_COOKIE['user'];
}
if ($filter == 1) {
    $zapros = "ORDER BY money DESC";
}
if ($filter == 2) {
    $zapros = "ORDER BY id ASC";
}
if (isset($_POST['search'])) {
    if ($_POST['names'] == '') {
        $zapros = '';
    } else {
        $zapros = 'WHERE '.$_POST['namev'].'=\''.$_POST['names'].'\'';
    }
}
$kolvo = 20;
$allnews = mysql_num_rows(mysql_query("SELECT id FROM tb_users $zapros"));
$vsego = intval($allnews / $kolvo);
if ($vsego == 0) $vsego = 1; $nowpage = $_GET['pg'];
if ($nowpage == '' or $nowpage <= 0) { $nowpage = 1; } else { $nowpage = intval($_GET["pg"]); }
if ($nowpage - 1 > $vsego) $nowpage = $vsego;
if (!isset($_GET["pg"]) or $_GET['pg'] == '') $nowpage = 1;
$gg = $vsego * $kolvo;
if ($gg < $allnews) { $vsego = $vsego + 1; }
$pages = $vsego / $kolvo;
$pages1 = floor($pages);
$pg1 = $kolvo * ($nowpage - 1);
$pg2 = $kolvo;
if ($nowpage > $vsego) { $pg1 = 0; $pg2 = $kolvo; }
if ($pages > $pages1) {
    $pages = $pages1 + 1;
}

$sql = mysql_query("SELECT * FROM  tb_users $zapros limit $pg1,$pg2") or die(mysql_error());
echo "<table width='70%' style='margin-bottom: 5px;'><tr><td nowrap='nowrap'>";
$nextp = $nowpage + 1;
$lastp = $nowpage - 1;
if ($nowpage != 1) { echo "<a href='/admin/?adm=6&pg=$lastp' class=\"link\"><span class='text14'>"; } else { echo "<span class='textgray'>"; } echo "&larr;&nbsp;Предыдущие</span>";
if ($nowpage == 1) { echo "</a>"; } echo "</td><td width='90%' align='center'>Страница $nowpage из $vsego</td><td nowrap='nowrap'>";
if ($nowpage == $vsego) { echo "<span class='textgray'>"; } else { echo "<a href='/admin/?adm=6&pg=$nextp' class=\"link\"><span class='text14'>"; } echo "Следующие&nbsp;&rarr;</span>"; 
if ($nowpage == $vsego) { echo "</a>"; } echo "</td></tr></table>";
?>
<table width="70%" border="0" cellpadding="0" cellspacing="1" class="tab">
<tr align="center">
<td class="rt" style="background: #ebebeb;"><a href="#" onclick="javascript:gofilter(2);">ID</a></td>
<td class="rt" style="background: #ebebeb;">Аватар</td>
<td class="rt" style="background: #ebebeb;">Пользователь</td>
<td class="rt" style="background: #ebebeb;">Реферер</td>
<td class="rt" style="background: #ebebeb;">Просмотров</td>
<td class="rt" style="background: #ebebeb;"><a href="#" onclick="javascript:gofilter(1);">Денег</a></td>
<td class="rt" style="background: #ebebeb;">Дата регистрации</td>
<td class="rt" style="background: #ebebeb;">Последний визит</td>
<td class="rt" style="background: #ebebeb;" colspan="2" width="120">Действия</td>
</tr>
<?
while ($row = mysql_fetch_array($sql)) {
    if ($row['ban'] == 1 AND $row['finestatus'] == 0) {
        $usclass = 'class="bt"';
    } else 
	if ($row['finestatus'] != 0 AND $row['ban'] == 0) {
        $usclass = 'class="fn"';
    } else 
	if ($row['ban'] != 1 AND $row['finestatus'] == 0) {
	    $usclass = 'class="lt"';
	} else 
	if ($row['ban'] == 1 AND $row['finestatus'] != 0) {
	    $usclass = 'class="fb"';
	}
	
	if ($row['lastlogdate'] == '') {
		$lastdate = '-';
	} else {
		$lastdate = date('d.m.Y', $row['lastlogdate']);
	}
    ?>
    <tr>
    <td <?=$usclass?> align="center"><?=$row['id']?></td>
	<td class="lt">
	    <a href="/#:id<?=$row['id']?>" target="_blank" >
		    <img src="/avp/<?=$row['avatar']?>" width="35" height="35" style=" margin-top: 5px;" class="avatar" /> 
		</a>
	</td>
    <td <?=$usclass?>><?=$row['username']?></td>
    <td <?=$usclass?> align="center"><?=$row['referer']?></td>
    <td <?=$usclass?> align="center"><?=$row['visits']?></td>
    <td <?=$usclass?> align="center"><?=moneyraund($row['money'], 3)?></td>
    <td <?=$usclass?> align="center"><?=date('d.m.Y', $row['joindate'])?></td>
    <td <?=$usclass?> align="center"><?=$lastdate?></td>
    <td <?=$usclass?> align="center"><form action="/admin/?adm=6&edit=<?=$row['id']?>" method="post"><input type="submit" value="Редактировать" id="save" /></form></td>
    <td <?=$usclass?> align="center"><form action="" method="post"><input type="hidden" name="id" value="<?=$row['id']?>" /><input type="submit" value="Удалить" id="save" name="del" /></form></td>
    </tr>
    <?
}
?>
</table>
<?}?>