<?php
include_once("incs/conf.php");
include_once("incs/fmysql.php");
include_once("incs/fnc.php");
include_once("incs/head.php");

$a = fil($_GET['a']);
$p = fil($_GET['p']);
$id = intval($_GET['id']);

if($settings[lib]==1){
head("Библиотека",$meuser,$auth);
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "Библиотека</div>";
echo "Раздел закрыт<br>";
include_once("incs/down.php");
}

if($on==1){
$gde="lib";
mysql_query("UPDATE `user` SET `time_gde` = '$time', `gde` = '$gde' WHERE `id` = '$user[id]'");
}
$gde_user = mysql_num_rows(mysql_query("SELECT `id` FROM `user` WHERE `gde` = '$gde' AND `time_gde` > '".($time-180)."'"));

$ban_ok=$user[ban]-$time;
if($ban_ok>0){
head("Библиотека",$meuser,$auth);
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "Библиотека</div>";

echo "Вы забанены по причине:<br>";
echo "<b>$user[ban_prich]</b><br>";
echo "До окончания бана осталось: $ban_ok сек.<br>";
echo "Старайтесь больше не нарушать <a href=\"rules.php\">правила</a> сайта!<br>";
include_once("incs/down.php");
}

$lib_id=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id` = '$id'"));

switch($a){

case 'del_yes':
if($user[level]>=9){
mysql_query("DELETE FROM `lib` WHERE `id` = '$id'");
}
break;

case 'del':
if($user[level]>=9){
head("Библиотека - Удаление",$meuser,$auth);
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "<a href=\"lib.php\">Библиотека</a> - Удаление</div>";

if($lib_id[id_them]!=0){
$chto="ету статью";
}else if($lib_id[id_pod_razd]!=0){
$chto="етот подраздел";
}else if($lib_id[id_razd]!=0){
$chto="етот раздел";
}

echo "Вы уверены что хотите удалить $chto?<br>";
echo "<div class=\"menu\"><a href=\"lib.php?id=$id&a=del_yes\">Да, уверен</a></div>";
echo "<div class=\"menu\"><a href=\"lib.php?id=$id\">Нет, вернутса</a></div>";

include_once("incs/down.php");
}
break;

case 'mod':
if($user[level]>=9){
head("Библиотека - Модерация",$meuser,$auth);
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "<a href=\"lib.php\">Библиотека</a> - Модерация</div>";

echo "$err";

if($p=="mod_ok"){
mysql_query("UPDATE `lib` SET `mod` = '1' WHERE `id` = '$id'");
}

if($p=="mod_del"){
mysql_query("DELETE FROM `lib` WHERE `id` = '$id' AND `id_them` != '0'");
}

$k_post = mysql_num_rows(mysql_query("SELECT id FROM `lib` WHERE `id_them` != '0' AND `mod` = '0'"));
if ($k_post==0)
{
echo "Нет статей для модерации<br>";
}

if($on==1){
$end=$user[msg_for_page];
}else{
$end=10;}
$skok_pages=ceil($k_post/$end);
if(!isset($_GET['page'])){
$page=1;
}else{
$page=intval($_GET['page']);
if($page<1){$page=1;}
if($page>$skok_pages){$page=1;}
}
if($page==1 OR $page<1){
$start=0;
}else{
$start=($page-1)*$end;
}

$for_t=mysql_query("SELECT * FROM `lib` WHERE `id_them` != '0' AND `mod` = '0' ORDER BY id DESC LIMIT $start,$end");
while ($for_them = mysql_fetch_array($for_t))
{
if($num==1){ 
echo "<div class='reki'>"; 
$num=0;
}else{ 
echo "<div class='rek'>"; 
$num=1;}
$k_post_them = mysql_num_rows(mysql_query("SELECT id FROM `lib_post` WHERE `id_razd` = '$for_them[id_razd]' AND `id_pod_razd` = '$for_them[id_pod_razd]' AND `id_them` = '$for_them[id_them]'"));
echo "<img src=\"styles/vooz/img/mod.png\" alt=\"image\"/> ";
echo "<a href='?id=$for_them[id]&p=mod'>$for_them[name]</a>($k_post_them)<br>";
echo "<a href='?id=$for_them[id]&a=mod&p=mod_ok'>Подтвердить</a> | <a href='?id=$for_them[id]&a=mod&p=mod_del'>Удалить</a></div>";
}

if($skok_pages>1){
for($i=1;$i<=$skok_pages;$i++){
if($i==$page){
echo "<span class=\"str_t\">$i</span>";
}else{
echo "<span class=\"str_l\"><a href='?a=mod&page=$i'>$i</a></span>";
}}}
include_once("incs/down.php");
}
break;

case 'plus':
$lib_access=mysql_fetch_array(mysql_query("SELECT * FROM `lib_access` WHERE `id_user` = '$user[id]' AND `id_stat` = '$id'"));
if($lib_access[id]==""){
mysql_query("INSERT INTO `lib_access` (`id_user`, `id_stat`) values('$user[id]', '$id')");
$new_ratyng=$lib_id[ratyng]+1;
mysql_query("UPDATE `lib` SET `ratyng` = '$new_ratyng' WHERE `id` = '$id'");
$sis_msg="Ваш голос принят<br>";
}
break;

case 'minus':
$lib_access=mysql_fetch_array(mysql_query("SELECT * FROM `lib_access` WHERE `id_user` = '$user[id]' AND `id_stat` = '$id'"));
if($lib_access[id]==""){
mysql_query("INSERT INTO `lib_access` (`id_user`, `id_stat`) values('$user[id]', '$id')");
$new_ratyng=$lib_id[ratyng]-1;
mysql_query("UPDATE `lib` SET `ratyng` = '$new_ratyng' WHERE `id` = '$id'");
$sis_msg="Ваш голос принят<br>";
}
break;

case 'vse':

$forum_c=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id` = '$id' AND `id_them` != '0'"));
if($forum_c[id]!=""){
$f_put_pod=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id_razd` = '$forum_c[id_razd]' AND `id_pod_razd` = '$forum_c[id_pod_razd]' AND `id_them` = '0'"));
$f_put_razd=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id_razd` = '$forum_c[id_razd]' AND `id_pod_razd` = '0'"));
head("Библиотека - $f_put_razd[name] - $f_put_pod[name] - $forum_c[name]",$meuser,$auth);
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "<a href=\"lib.php\">Библиотека</a> - <a href=\"lib.php?id=$id\">$forum_c[name]</a> - Читать все</div>";

echo "<b>$lib_id[name]</b><br>";
echo "".str_replace("\n","<br>",$lib_id[text])."<br>";
}else{
head("Библиотека - Ошибка - Такой статти не существует",$meuser,$auth);
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "<a href=\"lib.php\">Библиотека</a> - Ошибка - Такой статти не существует</div>";
}
include_once("incs/down.php");
break;

case 'top_sto':

head("Библиотека - TOP 100",$meuser,$auth);
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "<a href=\"lib.php\">Библиотека</a> - TOP 100</div>";

echo "$err";

$k_post = mysql_num_rows(mysql_query("SELECT id FROM `lib` WHERE `id_them` != '0'"));
if ($k_post==0)
{
echo "Нет стетей<br>";
}

if($on==1){
$end=$user[msg_for_page];
}else{
$end=10;}
$skok_pages=ceil($k_post/$end);
if(!isset($_GET['page'])){
$page=1;
}else{
$page=intval($_GET['page']);
if($page<1){$page=1;}
if($page>$skok_pages){$page=1;}
}
if($page==1 OR $page<1){
$start=0;
}else{
$start=($page-1)*$end;
}

$for_t=mysql_query("SELECT * FROM `lib` WHERE `id_them` != '0' ORDER BY ratyng DESC LIMIT $start,$end");
while ($for_them = mysql_fetch_array($for_t))
{
if($num==1){ 
echo "<div class='reki'>"; 
$num=0;
}else{ 
echo "<div class='rek'>"; 
$num=1;}
$k_post_them = mysql_num_rows(mysql_query("SELECT id FROM `lib_post` WHERE `id_razd` = '$for_them[id_razd]' AND `id_pod_razd` = '$for_them[id_pod_razd]' AND `id_them` = '$for_them[id_them]'"));
echo "<img src=\"styles/img/new.png\" alt=\"image\"/> ";
echo "<a href='?id=$for_them[id]'>$for_them[name]</a>($k_post_them)</div>";
}

if($skok_pages>1){
for($i=1;$i<=$skok_pages;$i++){
if($i==$page){
echo "<span class=\"str_t\">$i</span>";
}else{
echo "<span class=\"str_l\"><a href='?act=top_sto&page=$i'>$i</a></span>";
}}}
include_once("incs/down.php");
break;

case 'top_new':

head("Библиотека - Новые статьи",$meuser,$auth);
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "<a href=\"lib.php\">Библиотека</a> - Новые статьи</div>";

echo "$err";

$k_post = mysql_num_rows(mysql_query("SELECT id FROM `lib` WHERE `id_them` != '0'"));
if ($k_post==0)
{
echo "Нет стетей<br>";
}

if($on==1){
$end=$user[msg_for_page];
}else{
$end=10;}
$skok_pages=ceil($k_post/$end);
if(!isset($_GET['page'])){
$page=1;
}else{
$page=intval($_GET['page']);
if($page<1){$page=1;}
if($page>$skok_pages){$page=1;}
}
if($page==1 OR $page<1){
$start=0;
}else{
$start=($page-1)*$end;
}

$for_t=mysql_query("SELECT * FROM `lib` WHERE `id_them` != '0' ORDER BY id DESC LIMIT $start,$end");
while ($for_them = mysql_fetch_array($for_t))
{
if($num==1){ 
echo "<div class='reki'>"; 
$num=0;
}else{ 
echo "<div class='rek'>"; 
$num=1;}
$k_post_them = mysql_num_rows(mysql_query("SELECT id FROM `lib_post` WHERE `id_razd` = '$for_them[id_razd]' AND `id_pod_razd` = '$for_them[id_pod_razd]' AND `id_them` = '$for_them[id_them]'"));
echo "<img src=\"styles/img/new.png\" alt=\"image\"/> ";
echo "<a href='?id=$for_them[id]'>$for_them[name]</a>($k_post_them)</div>";
}

if($skok_pages>1){
for($i=1;$i<=$skok_pages;$i++){
if($i==$page){
echo "<span class=\"str_t\">$i</span>";
}else{
echo "<span class=\"str_l\"><a href='?a=top_new&page=$i'>$i</a></span>";
}}}
include_once("incs/down.php");
break;

case 'top_my':

head("Библиотека - Мои статьи",$meuser,$auth);
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "<a href=\"lib.php\">Библиотека</a> - Мои статьи</div>";

echo "$err";

$k_post = mysql_num_rows(mysql_query("SELECT id FROM `lib` WHERE `id_user` = '$user[id]'"));
if ($k_post==0)
{
echo "Нет вашых статей<br>";
}

if($on==1){
$end=$user[msg_for_page];
}else{
$end=10;}
$skok_pages=ceil($k_post/$end);
if(!isset($_GET['page'])){
$page=1;
}else{
$page=intval($_GET['page']);
if($page<1){$page=1;}
if($page>$skok_pages){$page=1;}
}
if($page==1 OR $page<1){
$start=0;
}else{
$start=($page-1)*$end;
}

$for_t=mysql_query("SELECT * FROM `lib` WHERE `id_user` = '$user[id]' ORDER BY id DESC LIMIT $start,$end");
while ($for_them = mysql_fetch_array($for_t))
{
if($num==1){ 
echo "<div class='reki'>"; 
$num=0;
}else{ 
echo "<div class='rek'>"; 
$num=1;}
$k_post_them = mysql_num_rows(mysql_query("SELECT id FROM `lib_post` WHERE `id_razd` = '$for_them[id_razd]' AND `id_pod_razd` = '$for_them[id_pod_razd]' AND `id_them` = '$for_them[id_them]'"));
echo "<img src=\"styles/img/my.png\" alt=\"image\"/> ";
echo "<a href='?id=$for_them[id]'>$for_them[name]</a>($k_post_them)</div>";
}

if($skok_pages>1){
for($i=1;$i<=$skok_pages;$i++){
if($i==$page){
echo "<span class=\"str_t\">$i</span>";
}else{
echo "<span class=\"str_l\"><a href='?a=top_my&page=$i'>$i</a></span>";
}}}
include_once("incs/down.php");
break;

case 'new':
if($user[level]==3 OR $user[level]==5 OR $user[level]==7 OR $user[level]==8 OR $user[level]>=9){
if($_POST['name']!=""){
$name=filtr($_POST['name']);
$nom=filtr($_POST['nom']);
if($nom!=""){
$nom=$nom;
}else{
$nom=0;
}

$povtor=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id` != '0' ORDER BY id DESC LIMIT 1"));
if($povtor[name]!=$name){
$id_razd_n=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id_pod_razd` = '0' ORDER BY id_razd DESC LIMIT 1"));
$id_razd_n=$id_razd_n[id_razd]+1;
mysql_query("INSERT INTO `lib` (`id_razd`, `name`, `nom`) values('$id_razd_n', '$name', '$nom')");
}else{
$sis_msg="Повторное действие<br>";
}
}else{
$err="Вы не ввели название раздела!<br>";
}
}
break;

case 'adm':
if($user[level]==3 OR $user[level]==5 OR $user[level]==7 OR $user[level]==8 OR $user[level]>=9){
head("Библиотека - Новый раздел",$meuser,$auth);
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "<a href=\"?\">Библиотека</a> - Новый раздел</div>";
echo '<form method="post" action="?a=new">';
echo 'Название раздела:<br/><input type="text" name="name" maxlength="32"/><br>';
echo 'Место:<br/><input type="text" name="nom" maxlength="32"/><br>';
echo '<input value="Создать" type="submit"/></form>';
include_once("incs/down.php");
}
break;

case 'new_p':
if($user[level]==3 OR $user[level]==5 OR $user[level]==7 OR $user[level]==8 OR $user[level]>=9){
$test=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id` = '$id'"));
if($test[id_razd]!=0 && $test[id_pod_razd]==0){
if($_POST['name']!=""){
$name=filtr($_POST['name']);
$nom=filtr($_POST['nom']);
if($nom!=""){
$nom=$nom;
}else{
$nom=0;
}
$povtor=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id` != '0' ORDER BY id DESC LIMIT 1"));
if($povtor[name]!=$name){
$id_n_p=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id` = '$id'"));
$id_razd_n=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id_razd` = '$id_n_p[id_razd]' AND `id_them` = '0' ORDER BY id_pod_razd DESC LIMIT 1"));
$id_razd_n=$id_razd_n[id_pod_razd]+1;
mysql_query("INSERT INTO `lib` (`id_razd`, `id_pod_razd`, `name`, `nom`) values('$id_n_p[id_razd]', '$id_razd_n', '$name', '$nom')");
}else{
$sis_msg="Повторное действие<br>";
}
}else{
$err="Вы не ввели название подраздела!<br>";
}
}else{
$err="Подразделы можно создавать только в разделах<br>";
}
}
break;

case 'adm_p':
if($user[level]==3 OR $user[level]==5 OR $user[level]==7 OR $user[level]==8 OR $user[level]>=9){
head("Библиотека - Новый подраздел",$meuser,$auth);
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "<a href=\"?\">Библиотека</a> - Новый подраздел</div>";
$test=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id` = '$id'"));
if($test[id_razd]!=0 && $test[id_pod_razd]==0){
echo "<form method=\"post\" action=\"?id=$id&a=new_p\">";
echo 'Название подраздела:<br/><input type="text" name="name" maxlength="32"/><br>';
echo 'Место:<br/><input type="text" name="nom" maxlength="32"/><br>';
echo '<input value="Создать" type="submit"/></form>';
}else{
echo "Подразделы можно создавать только в разделах<br>";
}
include_once("incs/down.php");
}
break;

case 'new_them_ok':
if($user[level]>=0 && $on==1){
$test=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id` = '$id'"));
if($test[id_razd]!=0 && $test[id_pod_razd]!=0 && $test[id_them]==0){
if($_POST['name']!="" OR $_POST['msg']!=""){
$name=filtr($_POST['name']);
$msg=filtr($_POST['msg']);
$msg=bbcode(smiles($msg));
$nam=strlen($_POST['name']);
$msg_len=strlen($_POST['msg']);
if($nam>3 && $nam<30 && $msg_len>300){
$povtor=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id` != '0' ORDER BY id DESC LIMIT 1"));
if($povtor[name]!=$name){
$id_n_p=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id` = '$id'"));
$id_razd_t=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id_razd` = '$id_n_p[id_razd]' AND `id_pod_razd` = '$id_n_p[id_pod_razd]' ORDER BY id_them DESC LIMIT 1"));
$id_razd_t=$id_razd_t[id_them]+1;
mysql_query("INSERT INTO `lib` (`id_razd`, `id_pod_razd`, `id_them`, `name`, `id_user`, `text`) values('$id_n_p[id_razd]', '$id_n_p[id_pod_razd]', '$id_razd_t', '$name', '$user[id]', '$msg')");
$sis_msg="Статья успешно создана, появитса после модерации<br>";
}else{
$sis_msg="Повторное действие<br>";
}
}else{
$err="Название не может быть короче 3, длинее 30 а текст короче 300 символов<br>";
}
}else{
$err="Вы не ввели название или сообщение!<br>";
}
}else{
echo "Статьи можно создавать только в подразделах<br>";
}
}
break;

case 'new_them':
if($user[level]>=0 && $on==1){
head("Библиотека - Новая статья",$meuser,$auth);
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "<a href=\"?\">Библиотека</a> - Новая статья</div>";
$test=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id` = '$id'"));
if($test[id_razd]!=0 && $test[id_pod_razd]!=0 && $test[id_them]==0){
echo "<form method=\"post\" action=\"?id=$id&a=new_them_ok\">";
echo 'Название темы:<br><input type="text" name="name" maxlength="32"/><br>';
echo 'Сообщение:<br><textarea name="msg"></textarea><br>';
echo '<input value="Создать" type="submit"/></form>';
}else{
echo "Статьи можно создавать только в подразделах<br>";
}
include_once("incs/down.php");
}
break;

case 'del_post':
if($user[level]>=9){
mysql_query("DELETE FROM `lib_post` WHERE `id` = '$p'");
}
break;

}

$forum_c=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id` = '$id' AND `id_them` != '0'"));
if($forum_c[id]!=""){
$f_put_pod=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id_razd` = '$forum_c[id_razd]' AND `id_pod_razd` = '$forum_c[id_pod_razd]' AND `id_them` = '0'"));
$f_put_razd=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id_razd` = '$forum_c[id_razd]' AND `id_pod_razd` = '0'"));
head("Библиотека - $f_put_razd[name] - $f_put_pod[name] - $forum_c[name]",$meuser,$auth);
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "<a href=\"lib.php\">Библиотека</a> - <a href=\"lib.php?id=$f_put_razd[id]\">$f_put_razd[name]</a> - <a href=\"lib.php?id=$f_put_pod[id]\">$f_put_pod[name]</a> - $forum_c[name]</div>";

if($p=="dob" && $on==1){
$povtor=mysql_fetch_array(mysql_query("SELECT * FROM `lib_post` WHERE `id` != '0' ORDER BY id DESC LIMIT 1"));
if($povtor[msg]!=filtr($_POST['msg'])){
if($_POST['msg']!=""){
$msg=$_POST['msg'];
$msg=filtr($msg);
$msg=bbcode(smiles($msg));
$name=$user['nick'];
$nom = mysql_num_rows(mysql_query("SELECT id FROM `lib_post` WHERE `id_razd` = '$forum_c[id_razd]' AND `id_pod_razd` = '$forum_c[id_pod_razd]' AND `id_them` = '$forum_c[id_them]'"));
$nom=$nom+1;
mysql_query("INSERT INTO `lib_post` (`id_razd`, `id_pod_razd`, `id_them`, `id_user`, `nom`, `nick`, `msg`, `time`) values('$forum_c[id_razd]', '$forum_c[id_pod_razd]', '$forum_c[id_them]', '$user[id]', '$nom', '$user[nick]', '$msg', '".date('Y.m.d/H:i')."')");
}else{
echo "Вы не заполнили поле!<br>";
}
}else{
echo "Сообщение повторяетса!<br>";
}
}

if($lib_id[mod]==0 && $p!="mod"){
echo "Статья еще не прошла модерацию. Подождите немного...<br>";
include_once("incs/down.php");
}

$lib_access=mysql_fetch_array(mysql_query("SELECT * FROM `lib_access` WHERE `id_user` = '$user[id]' AND `id_stat` = '$id'"));
echo "$sis_msg";
$chast = substr("$lib_id[text]", 0, 100);
echo "<b>$lib_id[name]</b><br>";
echo "".str_replace("\n","<br>",$chast)."...<br>";
$new_skik=$lib_id[skik]+1;
mysql_query("UPDATE `lib` SET `skik` = '$new_skik' WHERE `id` = '$id'");
$user_lib=mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '$lib_id[id_user]'"));
echo "<div class='news'>";
echo "<div class=\"menu\">Рейтинг: ";
if($lib_access[id]=="" && $on==1){echo "<a href=\"lib.php?id=$id&a=minus\">-</a> ";}
echo "$lib_id[ratyng] ";
if($lib_access[id]=="" && $on==1){echo "<a href=\"lib.php?id=$id&a=plus\">+</a>";}
echo "<br>";
echo "Просмотров: <b>$lib_id[skik]</b><br>";
echo "Добавил: <b><a href=\"my.php?id=$lib_id[id_user]\">$user_lib[nick]</a></b></div></div>";
echo "<div class=\"menu\">[<a href=\"?id=$id&a=vse\">Читать полностью</a>]<br></div>";
if($user[level]>=9){
echo "<div class=\"menu\"><a href=\"lib.php?id=$id&a=del\">Удалить</a></div>";
}
echo "<div class='rowup'>Коментарии:</div>";

if($on==1){
echo "<form method=\"post\" action=\"?id=$id&page=end&p=dob\">";
echo 'Сообщение:<br/><textarea name="msg"></textarea><br>';
echo '<input value="Отправить" type="submit"/></form>';
}

$k_post = mysql_num_rows(mysql_query("SELECT id FROM `lib_post` WHERE `id_razd` = '$forum_c[id_razd]' AND `id_pod_razd` = '$forum_c[id_pod_razd]' AND `id_them` = '$forum_c[id_them]'"));
if ($k_post==0)
{
echo "Нет коментариев!<br>";
}

if($on==1){
$end=$user[msg_for_page];
}else{
$end=10;}
$skok_pages=ceil($k_post/$end);

if(!isset($_GET['page'])){
$page=1;
}else{
$page=intval($_GET['page']);
if($page<1){$page=1;}
if($page>$skok_pages){$page=1;}
}
if($_GET['page']=='end'){
$page=$skok_pages;}
if($page==1 OR $page<1){
$start=0;
}else{
$start=($page-1)*$end;
}

$for_pos=mysql_query("SELECT * FROM `lib_post` WHERE `id_razd` = '$forum_c[id_razd]' AND `id_pod_razd` = '$forum_c[id_pod_razd]' AND `id_them` = '$forum_c[id_them]' ORDER BY id ASC LIMIT $start,$end");
while ($for_post = mysql_fetch_array($for_pos))
{
if($num==1){ 
echo "<div class='reki'>"; 
$num=0;
}else{ 
echo "<div class='rek'>"; 
$num=1;}
$i=$i+1;
$pa=(($page-1)*10)+$i;

echo "$pa. <a href='my.php?id=$for_post[id_user]'>$for_post[nick]</a> ($for_post[time])<br>";
echo "".str_replace("\n","<br>",$for_post[msg])."";
if($user[level]>=9){
echo "<br><a href=\"lib.php?id=$id&a=del_post&p=$for_post[id]\">Удалить</a>";
}
echo "</div>";
}

if($skok_pages>1){
for($i=1;$i<=$skok_pages;$i++){
if($i==$page){
echo "<span class=\"str_t\">$i</span>";
}else{
echo "<span class=\"str_l\"><a href='?id=$id&page=$i'>$i</a></span>";
}}}
include_once("incs/down.php");
}

$forum_b=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id` = '$id' AND `id_razd` != '0' AND `id_pod_razd` != '0' AND `id_them` = '0'"));
if($forum_b[id]!=""){
$f_put_razd=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id_razd` = '$forum_b[id_razd]' AND `id_pod_razd` = '0'"));
head("Библиотека - $f_put_razd[name] - $forum_b[name]",$meuser,$auth);
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "<a href=\"lib.php\">Библиотека</a> - <a href=\"lib.php?id=$f_put_razd[id]\">$f_put_razd[name]</a> - $forum_b[name]</div>";

echo "$err";
echo "$sis_msg";

$k_post = mysql_num_rows(mysql_query("SELECT id FROM `lib` WHERE `id_razd` = '$forum_b[id_razd]' AND `id_pod_razd` = '$forum_b[id_pod_razd]' AND `id_them` != '0' AND `mod` = '1'"));
if ($k_post==0)
{
echo "Нет статей<br>";
}

if($on==1){
$end=$user[msg_for_page];
}else{
$end=10;}
$skok_pages=ceil($k_post/$end);
if(!isset($_GET['page'])){
$page=1;
}else{
$page=intval($_GET['page']);
if($page<1){$page=1;}
if($page>$skok_pages){$page=1;}
}
if($page==1 OR $page<1){
$start=0;
}else{
$start=($page-1)*$end;
}

$for_t=mysql_query("SELECT * FROM `lib` WHERE `id_razd` = '$forum_b[id_razd]' AND `id_pod_razd` = '$forum_b[id_pod_razd]' AND `id_them` != '0' AND `mod` = '1' ORDER BY id DESC LIMIT $start,$end");
while ($for_them = mysql_fetch_array($for_t))
{
if($num==1){ 
echo "<div class='reki'>"; 
$num=0;
}else{ 
echo "<div class='rek'>"; 
$num=1;}
echo "<img src=\"styles/img/lib_s.png\" alt=\"image\"/> ";
echo "<a href='?id=$for_them[id]'>$for_them[name]</a><br>";
$chast = substr("$for_them[text]", 0, 100);
echo "$chast...</div>";
}

if($skok_pages>1){
for($i=1;$i<=$skok_pages;$i++){
if($i==$page){
echo "<span class=\"str_t\">$i</span>";
}else{
echo "<span class=\"str_l\"><a href='?id=$id&page=$i'>$i</a></span>";
}}}

if($user[level]>=0 && $on==1){
echo "<div class='menu'><a href=\"?id=$id&a=new_them\">Создать статью</a></div>";
}
if($user[level]>=9){
echo "<div class=\"menu\"><a href=\"lib.php?id=$id&a=del\">Удалить подраздел</a></div>";
}
include_once("incs/down.php");
}

$forum_a=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id` = '$id' AND `id_razd` != '0' AND `id_pod_razd` = '0'"));
if($forum_a[id]!=""){
$forum_raz=mysql_fetch_array(mysql_query("SELECT * FROM `lib` WHERE `id` = '$id'"));
head("Библиотека - $forum_raz[name]",$meuser,$auth);
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "<a href=\"lib.php\">Библиотека</a> - $forum_raz[name]</div>";

echo "$err";
echo "$sis_msg";

$k_post = mysql_num_rows(mysql_query("SELECT id FROM `lib` WHERE `id_razd` = '$forum_a[id_razd]' AND `id_pod_razd` != '0' AND `id_them` = '0'"));
if ($k_post==0)
{
echo "Нет подразделов<br>";
}

if($on==1){
$end=$user[msg_for_page];
}else{
$end=10;}
$skok_pages=ceil($k_post/$end);
if(!isset($_GET['page'])){
$page=1;
}else{
$page=intval($_GET['page']);
if($page<1){$page=1;}
if($page>$skok_pages){$page=1;}
}
if($page==1 OR $page<1){
$start=0;
}else{
$start=($page-1)*$end;
}

$for_p=mysql_query("SELECT * FROM `lib` WHERE `id_razd` = '$forum_raz[id_razd]' AND `id_pod_razd` != '0' AND `id_them` = '0' ORDER BY nom ASC LIMIT $start,$end");
while ($for_pod_raz = mysql_fetch_array($for_p))
{
if($num==1){ 
echo "<div class='reki'>"; 
$num=0;
}else{ 
echo "<div class='rek'>"; 
$num=1;}
$kol_stat = mysql_num_rows(mysql_query("SELECT id FROM `lib` WHERE `id_razd` = '$for_pod_raz[id_razd]' AND `id_pod_razd` = '$for_pod_raz[id_pod_razd]' AND `id_them` != '0' AND `mod` = '1'"));
echo "<img src=\"styles/img/pod_razd.png\" alt=\"image\"/> ";
echo "<a href='?id=$for_pod_raz[id]'>$for_pod_raz[name]</a>($kol_stat)</div>";
}

if($skok_pages>1){
for($i=1;$i<=$skok_pages;$i++){
if($i==$page){
echo "<span class=\"str_t\">$i</span>";
}else{
echo "<span class=\"str_l\"><a href='?id=$id&page=$i'>$i</a></span>";
}}}

if($user[level]>=9){
echo "<div class='menu'><a href=\"?id=$id&a=adm_p\">Добавление подразделов</a></div>";
echo "<div class=\"menu\"><a href=\"lib.php?id=$id&a=del\">Удалить раздел</a></div>";
}
include_once("incs/down.php");
}

head("Библиотека",$meuser,$auth);
echo "<div class='rowup'>";
include_once("incs/title.php");
echo "Библиотека: <a href=\"?a=top_sto\">TOP 100</a> | <a href=\"?a=top_new\">новые статьи</a> | <a href=\"?a=top_my\">мои статьи</a></div>";
if($user[level]>=9){
$kol_stat_mod = mysql_num_rows(mysql_query("SELECT id FROM `lib` WHERE `id_them` != '0' AND `mod` = '0'"));
echo "<div class='menu'><a href=\"?a=mod\">Модерация</a>($kol_stat_mod)</div>";}

echo "$err";
echo "$sis_msg";

$k_post = mysql_num_rows(mysql_query("SELECT id FROM `lib`"));
if ($k_post==0)
{
echo "Нет разделов<br>";
}

$for=mysql_query("SELECT * FROM `lib` WHERE `id_pod_razd` = '0' ORDER BY id ASC");
while ($for_raz = mysql_fetch_array($for))
{
if($num==1){ 
echo "<div class='reki'>"; 
$num=0;
}else{ 
echo "<div class='rek'>"; 
$num=1;}
$kol_stat = mysql_num_rows(mysql_query("SELECT id FROM `lib` WHERE `id_razd` = '$for_raz[id_razd]' AND `id_them` != '0' AND `mod` = '1'"));
echo "<img src=\"styles/img/razd.png\" alt=\"image\"/> ";
echo "<a href='?id=$for_raz[id]'>$for_raz[name]</a>($kol_stat)</div>";
}
if($user[level]>=9){
echo "<div class='menu'><a href=\"?a=adm\">Добавление разделов</a></div>";
}
include_once("incs/down.php");
?>