<?
include_once '../sys/inc/start.php';
include_once '../sys/inc/compress.php';
include_once '../sys/inc/sess.php';
include_once '../sys/inc/home.php';
include_once '../sys/inc/settings.php';
include_once '../sys/inc/db_connect.php';
include_once '../sys/inc/ipua.php';
include_once '../sys/inc/fnc.php';
include_once '../sys/inc/user.php';
include_once 'config.php';
if (isset($_GET['act'])) {$act = altec($_GET['act']);} else {$act = 'index';} 
switch ($act):
case "index":
$set['title']='Анекдоты'; // заголовок страницы
include_once '../sys/inc/thead.php';
title();
aut(); // форма авторизации
$k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `jokes` WHERE `mod`='1'"),0);
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
echo "<table class='post'>\n";
if ($k_post==0){
echo "   <tr>\n";
echo "  <td class='p_t'>\n";
echo "Нет анекдотов!\n";
echo "  </td>\n";
echo "   </tr>\n";}
$q=mysql_query("SELECT * FROM `jokes` WHERE `mod`='1' ORDER BY `id` DESC LIMIT $start, $set[p_str]");
while ($post = mysql_fetch_assoc($q)){
echo "  <td class='p_t'>\n";
echo '<img src="icon/happy.png" alt="!"/> <a href="index.php?act=view&id='.$post['id'].'">Анекдот #'.$post['id'].'</a>';
echo "  </td>\n";
echo "   </tr>\n";
echo "   <tr>\n";
if ($set['set_show_icon']==1)echo "  <td class='p_m' colspan='2'>\n"; else echo "  <td class='p_m'>\n";
echo truncate_utf8($post['text'], 200)."<br />\n";
if (user_access('guest_delete'))echo '<img src="icon/remove.png" alt="!"/> <a href="index.php?act=delete&id='.$post['id'].'">Удалить анекдот</a><br />';
echo "  </td>\n";
echo "   </tr>\n";
}
echo "</table>\n";
if ($k_page>1)str('index.php?act=index&',$k_page,$page); // Вывод страниц
echo "<div class='foot'>\n";
if($guest_set==1 || isset($user)){echo '<img src="icon/add.png" alt="!"/> <a href="index.php?act=add">Добавить анекдот</a><br/>';}
if($user['level']>4){echo '<img src="icon/administrator.gif" alt="!"/> <a href="index.php?act=mod">На модерации</a> ['.mysql_result(mysql_query("SELECT COUNT(*) FROM `jokes` where `mod`='0'"),0).']<br />';}
echo "</div>\n";
break;
case "add":
$set['title']='Добавление анекдота'; // заголовок страницы
include_once '../sys/inc/thead.php';
if($guest_set==1 || isset($user)){
if(isset($_POST['text'])){
$text = altec($_POST['text']);
if (utf_strlen($text) >= 20 && utf_strlen($text) < 1000) {
$mod = (isset($user)) ? 1 : 0;
mysql_query("INSERT INTO `jokes` (`user`, `text`, `time`, `mod`) values('$user[id]', '$text', '$time', '$mod')");
$jokes = mysql_insert_id();
header("Location: index.php?act=view&id=$jokes");
}else{$err[]='Анекдот не может привышать 1000 символов';}
}
title();
aut(); // форма авторизации
echo '<form action="index.php?act=add" method="post">';
echo 'Анекдот:<br />';
echo '<textarea name="text"></textarea><br />';
echo '<input class="submit" type="submit" value="Добавить" /><br />';
if(!isset($user))echo '<span style="color:#ff0000">* после модерации ваш анекдот будет добавлен на сайт</span>';
echo '</form>';
}else{header("Location: index.php");}
break;
case "view":
if(isset($_GET['id'])){$id = intval($_GET['id']);}else{header("Location: index.php");}
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `jokes` WHERE `id`='$id'"),0)==0)header("Location: index.php");
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `jokes` WHERE `id`='$id' and `mod`='1'"),0)==0)header("Location: index.php");
$jokes = mysql_fetch_assoc(mysql_query("SELECT * FROM `jokes` WHERE `id` = $id LIMIT 1"));
$set['title']='Анекдот #'.$jokes['id']; // заголовок страницы
include_once '../sys/inc/thead.php';
title();
aut(); // форма авторизации
echo "  <div class='p_t'>\n";
echo '<img src="icon/happy.png" alt="!"/> Анекдот #'.$jokes['id'];
echo "  </div>\n";
echo '<div class="p_m">';
echo output_text($jokes['text'])."<br />\n";
echo '</div>';
if($jokes['user']!=0)$ank = get_user($jokes['user']);
$l = ($jokes['user']!=0) ? '<a href="/info.php?id='.$jokes['user'].'">'.$ank['nick'].'</a>' : 'Гость';
echo 'Добавил: '.$l.'<br/>';
echo 'Добавлено: '.vremja($jokes['time']).'<br/>';
break;
case "mod":
$set['title']='Модерация анекдотов'; // заголовок страницы
include_once '../sys/inc/thead.php';
title();
aut(); // форма авторизации
if(isset($_GET['mod'])){
$id = intval($_GET['id']);
mysql_query("UPDATE `jokes` SET `mod` = '1', `time`='$time' WHERE `id` = '$id' LIMIT 1");
msg("Анекдот успешно промодерирован");
}
if(isset($_GET['del'])){
$id = intval($_GET['id']);
mysql_query("DELETE FROM `jokes` WHERE `id` = '$id' LIMIT 1");
msg("Отказано в модерации");
}
$k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `jokes` WHERE `mod`='0'"),0);
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
echo "<table class='post'>\n";
if ($k_post==0){
echo "   <tr>\n";
echo "  <td class='p_t'>\n";
echo "Нет анекдотов на модерации!\n";
echo "  </td>\n";
echo "   </tr>\n";}
$q=mysql_query("SELECT * FROM `jokes` WHERE `mod`='0' ORDER BY `id` ASC LIMIT $start, $set[p_str]");
while ($post = mysql_fetch_assoc($q)){
echo "  <td class='p_t'>\n";
echo '<img src="icon/happy.png" alt="!"/> <a href="index.php?act=view&id='.$post['id'].'">Анекдот #'.$post['id'].'</a>';
echo "  </td>\n";
echo "   </tr>\n";
echo "   <tr>\n";
if ($set['set_show_icon']==1)echo "  <td class='p_m' colspan='2'>\n"; else echo "  <td class='p_m'>\n";
echo output_text($post['text'])."<br />\n";
echo '<img src="icon/add_administrator.gif" alt="!"/> <a href="index.php?act=mod&id='.$post['id'].'&mod">Одобрить</a> ';
echo '<img src="icon/del_administrator.gif" alt="!"/> <a href="index.php?act=mod&id='.$post['id'].'&del">Отказать</a> ';
echo "  </td>\n";
echo "   </tr>\n";
}
echo "</table>\n";
if ($k_page>1)str('index.php?act=mod&',$k_page,$page); // Вывод страниц
break;
case "delete":
if($user['level']<4)header("Location: index.php");
if(isset($_GET['id'])){$id = intval($_GET['id']);}else{header("Location: index.php");}
$jokes = mysql_fetch_assoc(mysql_query("SELECT * FROM `jokes` WHERE `id` = $id LIMIT 1"));
$set['title']='Удаление анекдота #'.$jokes['id']; // заголовок страницы
include_once '../sys/inc/thead.php';
title();
aut(); // форма авторизации
if(isset($_GET['ok'])){
mysql_query("DELETE FROM `jokes` WHERE `id` = '$id' LIMIT 1");
header("Location: index.php");
}
echo "  <div class='p_t'>\n";
echo '<img src="icon/happy.png" alt="!"/> <a href="index.php?act=view&id='.$jokes['id'].'">Анекдот #'.$jokes['id'].'</a>';
echo "  </div>\n";
echo "  <div class='p_m'>\n";
echo output_text($jokes['text'])."<br />\n";
echo '<img src="icon/remove.png" alt="!"/> <a href="index.php?act=delete&id='.$id.'&ok">Удалить</a> ';
echo "  </div>\n";
break;
default:
header("location: index.php?");
endswitch;
include_once '../sys/inc/tfoot.php';
?>