<?
require( "inc/conf.inc.php" );
require( "inc/check.php" );
mysql_query( "update users set `ltime`='".time( )."', `lroom`='20' where id='{$id}';" );
echo "<title>-=Голосования=-</title>";
echo "<link rel=\"../shortcut icon\" href=\"favicon.ico\">";
echo "<meta http-equiv=\"refresh\" content=\"{$avr}\">";
echo "<style type=\"text/css\">";
echo "body { font-size: {$fs}; color: {$tcol}; background-color: {$bcol} }";
echo "a:link,a:visited { text-decoration: none; color: {$lcol} }";
echo "div { border-style:solid; border-width: 1px; background-color: {$rcol} }";
echo ".cp { border-style:solid; border-width: 0px; color: {$lcol} ; background-color: {$bcol}; text-align: left }";
echo "form { margin: 0px; }a:hover { text-decoration: none;  font-size: 15px; color: red }";
echo "</style>";
echo "</head>";
echo "<body>\n";

$mid = intval(check($mid));

if(empty($tyu))$tyu="left";
if(empty($title))$title="Голосования";

echo "$div1<b>Голосования</b>$div9<br />\n";


$moder=$row["moder"];
$avtor=$row["login"];
$date=date("j.m.Y");
$mode = check($mode);
switch($mode) {

default:
$a = @mysql_query("select id,name,date from votes");

while($arr=mysql_fetch_array($a)){
$name=$arr['name'];
$date=$arr['date'];
$bid=$arr['id'];
$votes = mysql_fetch_array(@mysql_query("select count(klu4) as num from voting where vote='".$bid."'"));


echo "<a href=\"votes.php?mode=view&amp;ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}&amp;mid=$bid\">$name</a> (<b>$votes[0]</b> | $date)";
if ($moder>1) echo " [<a href=\"votes.php?mode=del&amp;ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}&amp;mid=$bid\">Уд.</a> | <a href=\"votes.php?mode=edit&amp;ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}&amp;mid=$bid\">Ред.</a>]";
echo '<br/>';

}

if (mysql_affected_rows() == 0){
echo '<div class="d2">';
echo '<b>Голосований пока нет :)</b><br/>';
echo '</div>';
}

if ($moder>1) {


echo "<br />$div3<a href=\"votes.php?mode=add&amp;ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}&amp;ref=$ref\">Добавить опрос</a>$div9\n";

}
break;

case 'view':
$bid=intval($bid);
$q = mysql_query("select * from votes where id='".$mid."'");

if (mysql_affected_rows() == 0){

echo 'Нет такой записи :(<br/>';

} else {
$arr=mysql_fetch_array($q);
$mid=$arr['id'];
$name=$arr['name'];
$avtor=$arr['avtor'];
$vopros=$arr['vopros'];
$v1=$arr['v1'];
$v2=$arr['v2'];
$v3=$arr['v3'];
$v4=$arr['v4'];
$v5=$arr['v5'];


echo '<b>'.$name.'</b><br/>';
echo $vopros.' // Автор: <b>'.$avtor.'</a></b><br/>';


if ($wap==1){echo $divide;}


$a = mysql_fetch_array(@mysql_query("select count(klu4) as num from voting where vote='".$mid."' and var='1'"));
echo "&#8226; <a href=\"votes.php?mode=vote&amp;ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}&amp;mid=$mid&amp;v=1\">$v1</a> (<a href=\"votes.php?mode=who&amp;ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}&amp;mid=$mid&amp;v=1\">$a[0]</a>)<br/>";
$a = mysql_fetch_array(@mysql_query("select count(klu4) as num from voting where vote='".$mid."' and var='2'"));
echo "&#8226; <a href=\"votes.php?mode=vote&amp;ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}&amp;mid=$mid&amp;v=2\">$v2</a> (<a href=\"votes.php?mode=who&amp;ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}&amp;mid=$mid&amp;v=2\">$a[0]</a>)<br/>";
if ($v3) {$a = mysql_fetch_array(@mysql_query("select count(klu4) as num from voting where vote='".$mid."' and var='3'"));
echo "&#8226; <a href=\"votes.php?mode=vote&amp;ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}&amp;mid=$mid&amp;v=3\">$v3</a> (<a href=\"votes.php?mode=who&amp;ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}&amp;mid=$mid&amp;v=3\">$a[0]</a>)<br/>";}
if ($v4) {$a = mysql_fetch_array(@mysql_query("select count(klu4) as num from voting where vote='".$mid."' and var='4'"));
echo "&#8226; <a href=\"votes.php?mode=vote&amp;ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}&amp;mid=$mid&amp;v=4\">$v4</a> (<a href=\"votes.php?mode=who&amp;ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}&amp;mid=$mid&amp;v=4\">$a[0]</a>)<br/>";}
if ($v5) {$a = mysql_fetch_array(@mysql_query("select count(klu4) as num from voting where vote='".$mid."' and var='5'"));
echo "&#8226; <a href=\"votes.php?mode=vote&amp;ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}&amp;mid=$mid&amp;v=5\">$v5</a> (<a href=\"votes.php?mode=who&amp;ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}&amp;mid=$mid&amp;v=5\">$a[0]</a>)<br/>";}


if ($wap==1){echo $divide;}


$a = mysql_fetch_array(@mysql_query("select count(klu4) as num from voting where vote='".$mid."'"));

echo 'Голосов: '.$a[0].'<br/>';

}
break;

case 'add':
if ($moder<1) die('<b>У вас нет прав доступа =)</b>'.$fsize2.'');
if (!$act){

echo '<b>Добавление Опроса:</b><br/>';
echo "<form method=\"POST\" action=\"votes.php?mode=add&amp;ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}&amp;act=1\" name=\"auth\">\n";
echo 'Тема:<br/>';
echo "<input name=\"name\" maxlength=\"50\" title=\"infa\" emptyok=\"false\"/><br/>\n";

echo 'Вопрос:<br/>';
echo "<input name=\"vopros\" maxlength=\"100\" title=\"infa\" emptyok=\"false\"/><br/>\n";

echo 'Вариант 1:<br/>';
echo "<input name=\"v1\" maxlength=\"50\" title=\"infa\" emptyok=\"false\"/><br/>\n";

echo 'Вариант 2:<br/>';
echo "<input name=\"v2\" maxlength=\"50\" title=\"infa\" emptyok=\"false\"/><br/>\n";

echo 'Вариант 3:<br/>';
echo "<input name=\"v3\" maxlength=\"50\" title=\"infa\" emptyok=\"false\"/><br/>\n";

echo 'Вариант 4:<br/>';
echo "<input name=\"v4\" maxlength=\"50\" title=\"infa\" emptyok=\"false\"/><br/>\n";

echo 'Вариант 5:<br/>';
echo "<input name=\"v5\" maxlength=\"50\" title=\"infa\" emptyok=\"false\"/><br/>\n";


echo "<input type=\"submit\" value=\"Добавить\" name=\"enter\"><br/>\n";



} else {
$name = substr(check($name),0,100);
$vopros = substr(check($vopros),0,200);
$v1 = substr(check($v1),0,100);
$v2 = substr(check($v2),0,100);
$v3 = substr(check($v3),0,100);
$v4 = substr(check($v4),0,100);
$v5 = substr(check($v5),0,100);

if ($row["translit"]==1){
$name = trun_to_rus($name);
$vopros = trun_to_rus($vopros);
$v1 = trun_to_rus($v1);
$v2 = trun_to_rus($v2);
$v3 = trun_to_rus($v3);
$v4 = trun_to_rus($v4);
$v5 = trun_to_rus($v5);
}



if (!$vopros or !$v1 or !$v2) {

echo 'Не введён вопрос, или опрос содержит менее двух вариантов ответов :)<br/>';

} else {
mysql_query("Insert into votes set name ='".$name."', avtor ='".$avtor."', date ='".$date."', vopros = '".$vopros."', v1 = '".$v1."', v2 = '".$v2."', v3 = '".$v3."', v4 = '".$v4."', v5 = '".$v5."'");

echo '<b>Опрос успешно добавлен :)</b><br/>';


// сообщение о добавлении Опроса, во все комнаты =)
$adm = @mysql_query ("Select login from users where id='1' LIMIT 1;");
$z = @mysql_fetch_array ($adm);
$administration = $z["login"];
$administration = check($administration);
$time = time();

$rnd = rand(0,99999999);
$today=date ("H:i");
$time = time();
$txt = "<b>ВНИМАНИЕ!!!</b> <u>Добавлен новый опрос</u> :) <u>Отдай свой голос, ведь он может быть очень важен для чата</u> ;)";
for ($num = 0; $num <= 22; $num++){
$room = "room".$num;
mysql_query ("Insert into $room set klu4= '".$rnd."', time='".$today."', who='".$administration."', message='".$txt."', id='".$time."', towhom='', hid='0', usid='1', komu=''");
}
//

}
}

break;

case 'edit':
if ($moder<1) die('<b>*Nemate prawo pristupa*</b>'.$fsize2.'</p></card></wml>');
if (!$act){
$q = mysql_query("select * from votes where id='".$mid."'");
if (mysql_affected_rows() == 0) die('<b>Нет такой категории :(</b>'.$fsize2.'</p></card></wml>');
$arr=mysql_fetch_array($q);
$vopros=$arr['vopros'];
$name=$arr['name'];
$v1=$arr['v1'];
$v2=$arr['v2'];
$v3=$arr['v3'];
$v4=$arr['v4'];
$v5=$arr['v5'];


echo '<b>Редактирование категории:</b><br/>';

if ($wap==2) echo "<form method=\"POST\" action=\"votes.php?mode=edit&amp;ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}&amp;mid=$mid&amp;act=1\" name=\"auth\">\n";
echo 'Вопрос:<br/>';
echo "<input name=\"name\" maxlength=\"50\" value=\"$name\" title=\"infa\" emptyok=\"false\"/><br/>\n";

echo 'Описание:<br/>';
echo "<input name=\"vopros\" maxlength=\"100\" value=\"$vopros\" title=\"infa\" emptyok=\"false\"/><br/>\n";

echo 'Вариант 1:<br/>';
echo "<input name=\"v1\" maxlength=\"50\" value=\"$v1\" title=\"infa\" emptyok=\"false\"/><br/>\n";

echo 'Вариант 2:<br/>';
echo "<input name=\"v2\" maxlength=\"50\" value=\"$v2\" title=\"infa\" emptyok=\"false\"/><br/>\n";

echo 'Вариант 3:<br/>';
echo "<input name=\"v3\" maxlength=\"50\" value=\"$v3\" title=\"infa\" emptyok=\"false\"/><br/>\n";

echo 'Вариант 4:<br/>';
echo "<input name=\"v4\" maxlength=\"50\" value=\"$v4\" title=\"infa\" emptyok=\"false\"/><br/>\n";

echo 'Вариант 5:<br/>';
echo "<input name=\"v5\" maxlength=\"50\" value=\"$v5\" title=\"infa\" emptyok=\"false\"/><br/>\n";

if ($wap==1) {
echo "<anchor title=\"go\">Изменить<go href=\"votes.php?mode=edit&amp;ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}&amp;mid=$mid&amp;act=1\" method=\"post\">\n";
echo "<postfield name=\"name\" value=\"$(name)\"/>\n";
echo "<postfield name=\"vopros\" value=\"$(vopros)\"/>\n";
echo "<postfield name=\"v1\" value=\"$(v1)\"/>\n";
echo "<postfield name=\"v2\" value=\"$(v2)\"/>\n";
echo "<postfield name=\"v3\" value=\"$(v3)\"/>\n";
echo "<postfield name=\"v4\" value=\"$(v4)\"/>\n";
echo "<postfield name=\"v5\" value=\"$(v5)\"/>\n";
echo '</go></anchor><br/>';
} else {

echo "<input type=\"submit\" value=\"Изменить\" name=\"enter\"><br/>\n";
}


} else {
$name = substr(check($name),0,100);
$vopros = substr(check($vopros),0,200);
$v1 = substr(check($v1),0,100);
$v2 = substr(check($v2),0,100);
$v3 = substr(check($v3),0,100);
$v4 = substr(check($v4),0,100);
$v5 = substr(check($v5),0,100);

if ($row["translit"]==1){
$name = trun_to_rus($name);
$vopros = trun_to_rus($vopros);
$v1 = trun_to_rus($v1);
$v2 = trun_to_rus($v2);
$v3 = trun_to_rus($v3);
$v4 = trun_to_rus($v4);
$v5 = trun_to_rus($v5);
}


if (!$vopros or !$v1 or !$v2) {

echo 'Не введён вопрос, или опрос содержит менее двух вариантов ответов :)<br/>';

} else {
mysql_query("update votes set name ='".$name."', avtor ='".$avtor."', vopros = '".$vopros."', v1 = '".$v1."', v2 = '".$v2."', v3 = '".$v3."', v4 = '".$v4."', v5 = '".$v5."' where id = '".$mid."'");

echo '<b>Вопрос изменен :)</b><br/>';

}
}

break;

case 'del':
if ($moder<1) die('<b>*Nemate prawo pristupa*</b>'.$fsize2.'');
if (!$act){

echo "<b>Удалить вопрос?</b><br/>
<a href=\"votes.php?mode=del&amp;ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}&amp;act=go&amp;mid=$mid\">Да</a> | <a href=\"votes.php?ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}&amp;ref=$ref\">Нет</a><br/>";

} else {
$zapros="delete from votes where id= '".$mid."'";
$zapros2="delete from voting where vote= '".$mid."'";
if (mysql_query($zapros) and mysql_query($zapros2)){

echo '<b>Вопрос успешно удален :)</b><br/>';

} else {

echo '<b>Ошибка при удалении :(</b><br/>';

}
}
break;

case 'vote':
$v=intval($v);
$date=date("j.m.Y");

if ($v<1 or $v>5) die();
mysql_query ("Select * from voting where vote='".$mid."' and who='".$id."'");

if (mysql_affected_rows() == 0) {
mysql_query("INSERT INTO voting SET vote = '".$mid."', date = '$date', who = '".$id."', var = '".$v."'");
//mysql_query("update blog set votes=votes+'1' where id = '".$mid."'");
//mysql_query("update users set blog=blog+'1' where id='".$usid."'");

echo '<b>Ваш голос принят :)</b><br/>';


} else {

echo '<b>Вы уже голосовали :)</b><br/>';
}

break;

case 'who':
$query = mysql_query("select who,date from voting where vote = '".$mid."' and var = '".$v."'");

if (mysql_affected_rows() == 0) {
echo '<b>Ещё не голосовали :)</b><br/>';

mysql_close ($link);
exit;
} else {
echo '<b>Кто отдал голос:</b><br/>';


$i = 1;

while($arr=mysql_fetch_array($query)){
$r = mysql_fetch_array(@mysql_query ("SELECT login from users where id='".$arr[0]."' LIMIT 1;"));
echo ($i++).') '.$r[0].'  (ID: '.$arr[0].') - '.$arr[1].' <br/>';
}

}
break;

}






if($mode) {
print "<br/>$div1<a href=\"?ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}\">Голосование</a>$div9";
}


echo "$div1<a href=\"enter.php?ci={$ci}&amp;id={$id}&amp;pass={$pass}&amp;url={$url}&amp;ref=$rand\">Прихожая</a>$div9";
echo $div2;
echo $div9;
echo $fsize2;
if ($wap=="1")echo "</p></card></wml>";
else echo "</div></body></html>";
mysql_close($link);
?>