<?php
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';
$set['title']='Дневники';
$p_str=$set['p_str'];
only_reg();
function head(){
global $set,$user;
include_once 'sys/inc/thead.php';
title();
err();
aut();
}

function foot(){
global $set,$user,$conf;
include_once 'sys/inc/tfoot.php';
exit;
}


$act = (isset($_GET['act'])) ? htmlspecialchars($_GET['act']) : null;

switch($act){

case 'see':

$uid=(int)abs((int)$_GET['id']);

if(mysql_result(mysql_query("select count(`id`) from `blogs` where `uid`='$uid';"),'count')==0 && mysql_result(mysql_query("select count(`id`) from `blogs_title` where `uid`='$uid';"),'count')==0){
head();
echo "Обитатель еще не создал дневник";
foot();
}
if(mysql_result(mysql_query("select count(`id`) from `user` where `id`='$uid';"),'count')==0){
head();
echo "Обитатель еще не создал дневник";
foot();
}

head();
//////////////////////////////////////////////////////////////////////////////
echo "<div class=\"str\">\n";
echo '<a href="blogs.php?">Дневники</a> &gt; ';
echo "Мой дневник<br/>\n";
echo "</div>\n";
//////////////////////////////////////////////////////////////////////////////
$login=mysql_result(mysql_query("select `nick` from `user` where `id`='$uid';"),'nick');
if($_SESSION['id_user']==$uid)
{

echo "<img src='http://vipspaces.ru/sys/img/forum.gif' alt=''/> \n";
echo "<a href=\"?act=add\">Написать в дневник</a>";

}
$page = ( isset($_GET['p']) ) ? (int)abs((int)$_GET['p']) : 1;
$page = ( $page <= 1 ) ? 1 : $page;
$start = ( $page == 1 ) ? 0 : ($page-1)*$p_str;

$c = mysql_result(mysql_query("select count(`id`) from `blogs` where `uid`='$uid';"), 'count');
$all = ceil($c/$p_str);

if ( $page > $all && $all != 0 ){
header("Location: blogs.php?act=see&p=".$all."&SESS=".session_id());
exit;
}
else{
if($c==0) echo "Нет записей<br />";
else{
$res = mysql_query("select * from `blogs` where `uid`='$uid' order by `date` desc limit $start,$p_str;"); 
$num=1; 
while ($blog = mysql_fetch_array($res)){

if($num==1){ 
echo "<div class='str'>"; 
$num=0;
}else{ 
echo "<div class='str'>"; 
$num=1;}

$url = "files/blog";
if (is_file($url."/".$blog['id'].".jpg")) echo "<img src='/sys/img/photo.gif' alt=''/> \n";
else
if (is_file($url."/".$blog['id'].".gif")) echo "<img src='/sys/img/photo.gif' alt=''/> \n";
else
if (is_file($url."/".$blog['id'].".png")) echo "<img src='/sys/img/photo.gif' alt=''/> \n";
else
echo "<img src='/img/blog.gif' alt=''/> \n";

echo "<a href=\"/blogs.php?act=rec&amp;id=".$blog['id']."\">";

if ($blog['name']!=NULL)echo esc(trim(br(bbcode(smiles(links($blog['name']))))))." ";
else
echo "Без названия\n";

echo "</a>";
echo "(".vremja($blog['date']).")";
echo "</div>\n";
}

if ( $page > 1 ) echo "<a href=\"?act=see&amp;id=".$uid."&amp;p=".($page-1)."\">&lt;&lt;&lt;</a> ";
if ( $page < $all ) echo "<a href=\"?act=see&amp;id=".$uid."&amp;p=".($page+1)."\">&gt;&gt;&gt;</a> ";
}
}

foot();

break;

case 'addblog':

if(!isset($user)){
header("Location: blogs.php");
exit;
}

head();

if(mysql_result(mysql_query("select count(`id`) from `blogs_title` where `uid`='".$_SESSION['id_user']."';"),'count')==0){
mysql_query("INSERT INTO `blogs_title` SET `uid`='".$_SESSION['id_user']."', `date`='".time()."', `update`='0';");
echo "<div align=\"center\">Ваш блог создан!<br /><a href=\"blogs.php?act=see&amp;id=".$_SESSION['id_user']."\">Перейти</a></div>";
}
else echo "<div align=\"center\">У вас уже есть блог!<br /><a href=\"blogs.php?act=see&amp;id=".$_SESSION['id_user']."\">Перейти</a></div>";

foot();

break;

case 'add':

if(!isset($user)){
header("Location: blogs.php");
exit;
}

if(mysql_result(mysql_query("select count(`id`) from `blogs_title` where `uid`='".$_SESSION['id_user']."';"),'count')==0){
$date_create=time();
mysql_query("INSERT INTO `blogs_title` SET `uid`='".$_SESSION['id_user']."', `date`='$date_create', `update`='0';");
$create = "<center>Ваш блог был автоматически создан!</center><br />";
}
if(!isset($_POST['but']) && !isset($_POST['files'])){
head();
//////////////////////////////////////////////////////////////////////////////
echo "<div class=\"str\">\n";
echo '<a href="blogs.php?">Дневники</a> &gt; ';
echo "Создание блога<br/>\n";
echo "</div>\n";
//////////////////////////////////////////////////////////////////////////////
if(isset($create)) echo $create;
if(isset($_GET['l'])) echo "<center>Запись должна быть не менее 10 символов!</center><br />";
if(isset($_GET['m'])) echo "<center>Короткое настроение? (:</center><br />";
if(isset($_GET['s'])) echo "<center>Файл слишком большой!</center><br />";
if(isset($_GET['c'])) echo "<center>Недопустимые символы в названии!</center><br />";
if(isset($_GET['u'])) echo "<center>Это не файлообменник! Unknown file!</center><br />";
if(isset($_GET['i'])) echo "<center>Файл с таким названием уже существует!</center><br />";


echo "<form action=\"/blogs.php?act=add\" method=\"post\">";
echo "Название:<br />";
echo "<input name=\"name\" type=\"text\" maxlength='32' value='' /><br />\n";
echo "Текст: <br />";
echo "<textarea name=\"text\" cols=\"20\" rows=\"3\" maxlength=\"1024\"></textarea><br />";
if ($user['set_translit']==1) echo "<label><input type=\"checkbox\" name=\"translit\" value=\"1\" /> Транслит</label><br />\n";
echo "<input type=\"submit\" name=\"but\" value=\"Добавить\" /></form>";


foot();
}
else{
if(isset($_POST['files']) && isset($_FILES['file']['tmp_name'])){ #start upload file

$_FILES['file']['name'] = retranslit($_FILES['file']['name']);
$_FILES['file']['name'] = str_replace(" ", "_", $_FILES['file']['name']);

if(preg_match("~(\.php$|\.php(3|4|5)$|\.php\.*|\.html$|\.htm$|\.dhtml$|\.phtml$|\.wml$|\.wmls$|\.cgi$|\.pl$|\.inc$|\.inc\.*)~i", $_FILES['file']['name'])){
unlink($_FILES['file']['tmp_name']);
header("Location: blogs.php?act=add&u&SESS=".session_id());
exit;
}

if(preg_match("~(\\|/|:|\*|\?|\"|\'|<|>|\|)~", $_FILES['file']['name'])){
unlink($_FILES['file']['tmp_name']);
header("Location: blogs.php?act=add&c&SESS=".session_id());
exit;
}

if($_FILES['file']['error'] == 1 || $_FILES['file']['size']/1024 > $files_size){
unlink($_FILES['file']['tmp_name']);
header("Location: blogs.php?act=add&s&SESS=".session_id());
exit;
}

if($_FILES['file']['error'] == 0){
if(preg_match("~(\.3gp$|\.mp4$|\.avi$)~i", $_FILES['file']['name'])){
$_SESSION['files'] = $_FILES['file']['name'];
$_SESSION['files_type'] = 'video';
}
elseif(preg_match("~(\.jpg$|\.jpeg$|\.bmp$|\.png$|\.gif$)~i", $_FILES['file']['name'])){
$_SESSION['files'] = $_FILES['file']['name'];
$_SESSION['files_type'] = 'pic';
}
elseif(preg_match("~(\.mp3$|\.wav$|\.amr$)~i", $_FILES['file']['name'])){
$_SESSION['files'] = $_FILES['file']['name'];
$_SESSION['files_type'] = 'sound';
}
else{
header("Location: blogs.php?act=add&u&SESS=".session_id());
exit;
}

if(file_exists("blogs/".$_SESSION['files_type']."/".$_SESSION['id_user']."/".$_SESSION['files'])){
unlink($_FILES['file']['tmp_name'],$_SESSION['files'],$_SESSION['files_type']);
header("Location: blogs.php?act=add&i&SESS=".session_id());
exit;
}

chdir("blogs/".$_SESSION['files_type']);
if(!is_dir($_SESSION['id_user'])){
mkdir($_SESSION['id_user'],0777);
}

move_uploaded_file($_FILES['file']['tmp_name'], $_SESSION['id_user']."/".$_SESSION['files']);
header("Location: blogs.php?act=add&SESS=".session_id());
exit;
}
}


if(isset($_POST['but'])){
$text=trim($_POST['text']);
$name=trim($_POST['name']);
if($_POST['translit']==1){
$text=translit($text);
$name=translit($name);
}
$text=mysql_escape_string(htmlspecialchars(stripslashes($text)));
$name=mysql_escape_string(htmlspecialchars(stripslashes($name)));
if(strlen($text)<10){
header("Location: blogs.php?act=add&l&SESS=".session_id());
exit;
}

if(mysql_result(mysql_query("select `id` from `blogs` where `uid`='".$_SESSION['id_user']."' and `text` like $text;"),'id')!=0){
header("Location: blogs.php?act=see&id=".$_SESSION['id_user']."&SESS=".session_id());
exit;
}

if(isset($_SESSION['files']) && file_exists("blogs/".$_SESSION['files_type']."/".$_SESSION['id_user']."/".$_SESSION['files'])){
$query = ",`".$_SESSION['files_type']."`='".$_SESSION['files']."'";
unset($_SESSION['files_type'],$_SESSION['files']);
}
else $query=null;

$date=time();
mysql_query("insert into `blogs` set `uid`='".$_SESSION['id_user']."', `date`='$date',`name`='$name', `text`='$text' ".$query.";");
$rec=mysql_insert_id();
mysql_query("update `blogs_title` set `update`='$date' where `uid`='".$_SESSION['id_user']."' ;");
header("Location: blogs.php?act=rec&id=".$rec."&SESS=".session_id());
}
}

break;


case 'addfile':




if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id']!=NULL)
{

head();

if (isset($_FILES['file']))
{
if (eregi('\.jpe?g$',$_FILES['file']['name']) && $imgc=@imagecreatefromjpeg($_FILES['file']['tmp_name']))
{

$id = intval($_GET['id']);

if (imagesx($imgc)>100 || imagesy($imgc)>100)
{
$img_x=imagesx($imgc);
$img_y=imagesy($imgc);
if ($img_x==$img_y)
{
$dstW=100; // ширина
$dstH=100; // высота 
}
elseif ($img_x>$img_y)
{
$prop=$img_x/$img_y;
$dstW=100;
$dstH=ceil($dstW/$prop);
}
else
{
$prop=$img_y/$img_x;
$dstH=100;
$dstW=ceil($dstH/$prop);
}

$screen=imagecreatetruecolor($dstW, $dstH);
imagecopyresampled($screen, $imgc, 0, 0, 0, 0, $dstW, $dstH, $img_x, $img_y);
imagedestroy($imgc);
@chmod(H."files/blog/".$id.".jpg",0777);
@chmod(H."files/blog/".$id.".gif",0777);
@chmod(H."files/blog/".$id.".png",0777);
@unlink(H."files/blog/".$id.".jpg");
@unlink(H."files/blog/".$id.".gif");
@unlink(H."files/blog/".$id.".png");
imagejpeg($screen,H."files/blog/".$id.".jpg",100);
@chmod(H."files/blog/".$id.".jpg",0777);
imagedestroy($screen);
}
else
{
copy($_FILES['file']['tmp_name'], H."files/blog/".$id.".jpg");
}

msg("Логотип успешно установлен");
}
elseif (eregi('\.gif$',$_FILES['file']['name']) && $imgc=@imagecreatefromgif($_FILES['file']['tmp_name']))
{
$screen=gif_resize(fread ( fopen ($_FILES['file']['tmp_name'], "rb" ), filesize ($_FILES['file']['tmp_name']) ),100,100);
@chmod(H."files/blog/".$id.".jpg",0777);
@chmod(H."files/blog/".$id.".gif",0777);
@chmod(H."files/blog/".$id.".png",0777);
@unlink(H."files/blog/".$id.".jpg");
@unlink(H."files/blog/".$id.".gif");
@unlink(H."files/blog/".$id.".png");

file_put_contents(H."files/blog/$id.gif", $screen);
@chmod(H."files/blog/$id.gif",0777);

msg("Логотип успешно установлен");
}
elseif (eregi('\.png$',$_FILES['file']['name']) && $imgc=@imagecreatefrompng($_FILES['file']['tmp_name']))
{
if (imagesx($imgc)>100 || imagesy($imgc)>100)
{
$img_x=imagesx($imgc);
$img_y=imagesy($imgc);
if ($img_x==$img_y)
{
$dstW=100; // ширина
$dstH=100; // высота 
}
elseif ($img_x>$img_y)
{
$prop=$img_x/$img_y;
$dstW=100;
$dstH=ceil($dstW/$prop);
}
else
{
$prop=$img_y/$img_x;
$dstH=100;
$dstW=ceil($dstH/$prop);
}

$screen=ImageCreate($dstW, $dstH);
imagecopyresampled($screen, $imgc, 0, 0, 0, 0, $dstW, $dstH, $img_x, $img_y);
imagedestroy($imgc);


@chmod(H."files/blog/".$id.".jpg",0777);
@chmod(H."files/blog/".$id.".gif",0777);
@chmod(H."files/blog/".$id.".png",0777);
@unlink(H."files/blog/".$id.".jpg");
@unlink(H."files/blog/".$id.".gif");
@unlink(H."files/blog/".$id.".png");
imagepng($screen,H."files/blog/".$id.".png");
@chmod(H."files/blog/".$id.".png",0777);
imagedestroy($screen);
}
else
{

copy($_FILES['file']['tmp_name'], H."files/blog/".$id.".png");
}

msg("Логотип успешно установлен");
}
else
{
$err='Неверный формат файла';
}
}

$id = intval($_GET['id']);

echo "Можно загружать картинки форматов: GIF, JPG, PNG<br />\n";


echo "<form method='post' enctype=\"multipart/form-data\" action='?act=addfile&id=$id'>\n";
echo "<input type=\"file\" name=\"file\" accept='image/*,image/gif,image/png,image/jpeg' />\n";
echo "<input value=\"Загрузить\" type=\"submit\" />\n";
echo "</form>\n";
}
echo "<a href='?act=rec&id=$id'>Назад</a><br/>";
foot();

break;
/////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
case 'rec':
$id = intval($_GET['id']);
head();
$rec=mysql_query("select * from `blogs` where `id`='$id';");
//////////////////////////////////////////////////////////////////////////////
echo "<div class=\"str\">\n";
echo '<a href="blogs.php?">Дневники</a> &gt; ';
echo "В блоге<br/>\n";
echo "</div>\n";
//////////////////////////////////////////////////////////////////////////////



if(mysql_num_rows($rec)==0){
echo "Нет такой записи!";
}
else{
$rec=mysql_fetch_array($rec);
$login=mysql_fetch_array(mysql_query("select `nick`,`pol` from `user` where `id`='".$rec['uid']."' limit 1;"));

echo "".online($rec['uid'])." ";
echo "<a href='/info.php?id=$rec[uid]'>".$login['nick']."</a> ";
echo "(".vremja($rec['date']).")";
echo'<br>';




$url = "files/blog";
if (is_file($url."/$id.jpg")) echo "<img src='$url/$id.jpg' alt=''/><br/>\n";
else
if (is_file($url."/$id.gif")) echo "<img src='$url/$id.gif' alt=''/><br/>\n";
else
if (is_file($url."/$id.png")) echo "<img src='$url/$id.png' alt=''/><br/>\n";
else{
if($_SESSION['id_user']==$rec['uid'] || $user['level']>2){
echo "<img src='/sys/img/photo.gif' alt=''/> <a href='?act=addfile&id=$id'>Добавить рисунок</a><br/>";
}}
echo esc(trim(br(bbcode(smiles(links($rec['text']))))))."<br/>";
if($_SESSION['id_user']==$rec['uid'] || $user['level']>2){echo "<a href='blogs.php?act=edit&id=$id'>Редактировать</a> / <a href='blogs.php?act=del&id=$id'>Удалить</a><br/>";}
echo "<img src='/img/bullet.png' alt='' /> <a href=\"/blogs.php?act=comm&amp;id=".$id."\">Комментарии</a> (";
echo mysql_result(mysql_query("select count(`id`) from `blogs_comments` where `record`='$id';"),'count').")<br />";
}



foot();

break;
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
case 'comm':

$id=(int)abs((int)$_GET['id']);

if(mysql_result(mysql_query("select count(`id`) from `blogs` where `id`='$id';"),'count')==0){
head();
echo "Нет такого блога!";
foot();
}

head();
//////////////////////////////////////////////////////////////////////////////
echo "<div class=\"str\">\n";
echo '<a href="blogs.php?">Дневники</a> &gt; ';
echo "<a href=\"/blogs.php?act=rec&amp;id=".$id."\">";
if ($blog['name']!=NULL)echo esc(trim(br(bbcode(smiles(links($blog['name']))))))." ";
else
echo "Без названия\n";
echo "</a> &gt; ";
echo 'Kомментарии';
echo "</div>\n";
//////////////////////////////////////////////////////////////////////////////
$record=mysql_result(mysql_query("select `text` from `blogs` where `id`='$id';"),'text');
if(isset($_GET['l'])) echo "Комментарий слишком короткий!<br />";
if(isset($_GET['ok'])) echo "Комментарий добавлен!<br />";

$page = ( isset($_REQUEST['p']) ) ? (int)abs((int)$_REQUEST['p']) : 1;
$page = ( $page <= 1 ) ? 1 : $page;
$start = ( $page == 1 ) ? 0 : ($page-1)*$p_str;

$all = ceil(mysql_result(mysql_query("select count(`id`) from `blogs_comments` where `record`='$id';"), 'count')/$p_str);

if ( $page > $all && $all != 0 ){
header("Location: blogs.php?act=comm&id=".$id."&p=".$all."&SESS=".session_id());
exit;
}
else{
if(mysql_result(mysql_query("select count(`id`) from `blogs_comments` where `record`='$id';"),'count')==0) echo "Нет комментариев";
else{
$res = mysql_query("select * from `blogs_comments` where `record`='$id' order by `date` desc limit $start,$p_str;"); 
while ($comm = mysql_fetch_array($res)){
$login=mysql_fetch_array(mysql_query("select `nick`,`pol` from `user` where `id`='".$comm['uid']."' limit 1;"));


if (isset($user) && $user['id']==$comm['bautor_id'])mysql_query("UPDATE `blogs_comments` SET `read` = '1' WHERE `id` = '$comm[id]' AND `record` = '$comm[record]' AND `read` = '0'");

if($num==1){ 
echo "<div class='str'>"; 
$num=0;
}else{ 
echo "<div class='main_menu'>"; 
$num=1;}

echo "".online($comm['uid'])." ";
if(empty($login)) echo "Deleted";
else echo "<a href=\"info.php?id=".$comm['uid']."\">".$login['nick']."</a>";
echo " ".adm($comm['uid'])." ";
echo " (".vremja($comm['date']).")<br />";
echo esc(trim(br(bbcode(smiles(links($comm['text']))))))."<br/>";
if ($user['level']>2)echo "<a href=\"blogs.php?act=delcomm&amp;id=".$comm['id']."&amp;c=".$id."\">Удалить</a><br />";
echo "</div>\n";
}

if ( $page > 1 ) echo "<a href=\"?act=comm&amp;id=".$id."&amp;p=".($page-1)."&id=$id\">&lt;&lt;&lt;</a> ";
if ( $page < $all ) echo "<a href=\"?act=comm&amp;id=".$id."&amp;p=".($page+1)."&id=$id\">&gt;&gt;&gt;</a> ";
}
if(isset($user)){
echo "<form method=\"post\" action=\"blogs.php?act=addcomm\">\n";
echo "Комментарий: <a href=\"/smiles\"><small>Смайлы</small></a><br /><textarea name=\"comm\"></textarea><br />";
echo "<input type=\"hidden\" name=\"id\" value=\"".$id."\" />";
$msg = strtr($msg, array('B'=>'.', 'b'=>'.', 'C'=>'.', 'c'=>'.', 'D'=>'.', 'd'=>'.', 'F'=>'.', 'f'=>'.', 'G'=>'.', 'g'=>'.', 'H'=>'.', 'h'=>'.', 'I'=>'.', 'i'=>'.', 'J'=>'.', 'j'=>'.', 'K'=>'.', 'k'=>'.', 'L'=>'.', 'l'=>'.', 'M'=>'.', 'm'=>'.', 'N'=>'...', 'n'=>'.', 'O'=>'.', 'o'=>'.', 'P'=>'.', 'p'=>'.', 'Q'=>'.', 'q'=>'.', 'R'=>'.', 'r'=>'.', 'S'=>'.', 's'=>'.', 'T'=>'.', 't'=>'.', 'U'=>'.', 'u'=>'.', 'V'=>'.', 'v'=>'.', 'W'=>'.', 'w'=>'.', 'X'=>'.', 'x'=>'.', 'Y'=>'.', 'y'=>'.', 'Z'=>'.', 'z'=>'.'));

if ($user['set_translit']==1) echo "<label><input type=\"checkbox\" name=\"translit\" value=\"1\" /> Транслит</label><br />\n";
echo "<input value=\"Отправить\" name=\"but\" type=\"submit\" />";
echo "</form>\n";
}
}

foot();

break;

case 'addcomm':

if(!isset($user)){
header("Location: blogs.php");
exit;
}

$id=(int)abs((int)$_POST['id']);
if(mysql_result(mysql_query("select count(`id`) from `blogs` where `id`='$id';"),'count')==0 || !isset($_POST['but'])){
header("Location: index.php");
exit;
}

if(!isset($_POST['but'])){
header("Location: index.php");
exit;
}

$comm=mysql_escape_string(htmlspecialchars(stripslashes(trim($_POST['comm']))));
if(strlen($comm)<3){
header("Location: blogs.php?act=comm&id=".$id."&l&SESS=".session_id());
exit;
}

$login=mysql_fetch_array(mysql_query("select `uid` from `blogs` where `id`='$id' limit 1;"));

if($_POST['translit']==1) $comm=translit($comm);
mysql_query("insert into `blogs_comments` set `record`='$id', `uid`='".$_SESSION['id_user']."', `date`='".time()."', `text`='$comm', `read`='0', `bautor_id`='".$login['uid']."';");
header("Location: blogs.php?act=comm&id=".$id."&ok&SESS=".session_id());

break;

case 'delcomm':

if(!isset($user) || $user['level']<2){
header("Location: blogs.php");
exit;
}

$id=(int)abs((int)$_GET['id']);
$comm=(int)abs((int)$_GET['c']);

mysql_query("delete from `blogs_comments` where `id`='$id';");
header("Location: blogs.php?act=comm&id=".$comm."&SESS=".session_id());

break;

case 'del':

$id=(int)abs((int)$_GET['id']);
if(mysql_result(mysql_query("select count(`id`) from `blogs` where `id`='$id';"),'count')==0){
head();
echo "Нет такой записи!<br />";
foot();
}

$uid=mysql_result(mysql_query("select `uid` from `blogs` where `id`='$id';"),'uid');
if($_SESSION['id_user'] != $uid && $user['level']<2){
header("Location: blogs.php");
exit;
}

if(!isset($_GET['yes'])){
head();
echo "Вы уверены, что хотите удалить запись?<br />";
echo "[<a href=\"blogs.php?act=del&amp;id=".$id."&amp;yes\">Да</a>] [<a href=\"?act=rec&id=".$id."\">отмена</a>]<br />";
foot();
}
else{
$files=mysql_fetch_array(mysql_query("select `pic`,`video`,`sound` from `blogs` where `id`='$id';"));
if(!empty($files['pic'])) unlink("blogs/pic/".$uid."/".$files['pic']);
if(!empty($files['video'])) unlink("blogs/video/".$uid."/".$files['video']);
if(!empty($files['sound'])) unlink("blogs/sound/".$uid."/".$files['sound']);
mysql_query("delete from `blogs` where `id`='$id';");
header("Location: blogs.php?act=see&id=".$uid."&SESS=".session_id());
exit;
}

break;


case 'delfile':

head();

if(!isset($user) || !isset($_SESSION['files'])){
header("Location: blogs.php");
exit;
}

if(file_exists("/blogs/".$_SESSION['files_type']."/".$_SESSION['id_user']."/".$_SESSION['files'])){
unlink("blogs/".$_SESSION['files_type']."/".$_SESSION['id_user']."/".$_SESSION['files']);
unset($_SESSION['files'],$_SESSION['files_type']);
header("Location: blogs.php?act=add&SESS=".session_id());
exit;
}
header("Location: blogs.php?act=add&SESS=".session_id());

break;


case 'delp':

if(!isset($user) || $user['level']<2){
header("Location: blogs.php");
exit;
}

$id=(int)abs((int)$_GET['id']);
$file = mysql_fetch_array(mysql_query("select `uid`,`pic` from `blogs` where `id`='$id';"));
if(empty($file['pic']) || !file_exists("blogs/pic/".$file['uid']."/".$file['pic'])){
header("Location: blogs.php?act=rec&id=".$id."&SESS=".session_id());
exit;
}
unlink("blogs/pic/".$file['uid']."/".$file['pic']);
mysql_query("update `blogs` set `pic`='' where `id`='$id';");
header("Location: blogs.php?act=rec&id=".$id."&SESS=".session_id());
exit;

break;


case 'delv':

if(!isset($user) || $user['level']<2){
header("Location: blogs.php");
exit;
}

$id=(int)abs((int)$_GET['id']);
$file = mysql_fetch_array(mysql_query("select `uid`,`video` from `blogs` where `id`='$id';"));
if(empty($file['video']) || !file_exists("blogs/video/".$file['uid']."/".$file['video'])){
header("Location: blogs.php?act=rec&id=".$id."&SESS=".session_id());
exit;
}
unlink("blogs/video/".$file['uid']."/".$file['video']);
mysql_query("update `blogs` set `video`='' where `id`='$id';");
header("Location: blogs.php?act=rec&id=".$id."&SESS=".session_id());
exit;

break;


case 'dels':

if(!isset($user) || $user['level']<2){
header("Location: blogs.php");
exit;
}

$id=(int)abs((int)$_GET['id']);
$file = mysql_fetch_array(mysql_query("select `uid`,`sound` from `blogs` where `id`='$id';"));
if(empty($file['sound']) || !file_exists("blogs/sound/".$file['uid']."/".$file['sound'])){
header("Location: blogs.php?act=rec&id=".$id."&SESS=".session_id());
exit;
}
unlink("blogs/sound/".$file['uid']."/".$file['sound']);
mysql_query("update `blogs` set `sound`='' where `id`='$id';");
header("Location: blogs.php?act=rec&id=".$id."&SESS=".session_id());
exit;

break;


case 'edit':

$id=(isset($_GET['id'])) ? (int)abs((int)$_GET['id']) : (int)abs((int)$_POST['id']);

if(mysql_result(mysql_query("select count(`id`) from `blogs` where `id`='$id';"),'count')==0){
head();
echo "<center>Нет такой записи!</center><br />";
foot();
}

$uid=mysql_result(mysql_query("select `uid` from `blogs` where `id`='$id';"),'uid');
if($_SESSION['id_user'] != $uid && $user['level']<2){
header("Location: blogs.php");
exit;
}

if(!isset($_POST['but'])){
head();
$data=mysql_fetch_array(mysql_query("select * from `blogs` where `id`='$id';"));
//////////////////////////////////////////////////////////////////////////////
echo "<div class=\"str\">\n";
echo '<a href="blogs.php?">Дневники</a> &gt; ';
echo "Редактирование дневника<br/>\n";
echo "</div>\n";
//////////////////////////////////////////////////////////////////////////////
if(isset($_GET['l'])) echo "<center>Запись должна быть не менее 10 символов!</center><br />";
if(isset($_GET['m'])) echo "<center>Короткое настроение? (:</center><br />";
if(isset($_GET['a'])) echo "<center>Добавь больше симвлов (:</center><br />";
echo "<form action=\"blogs.php?act=edit\" method=\"post\">";
echo "Текст: <br />";
echo "<textarea name=\"text\" cols=\"20\" rows=\"3\">".$data['text']."</textarea><br />";
echo "<input type=\"hidden\" name=\"id\" value=\"".$id."\" />";
if(!empty($data['add'])){
echo "Добавлено:<br />";
echo "<textarea name=\"add\" cols=\"20\" rows=\"3\">".$data['add']."</textarea><br />";
}
if ($user['set_translit']==1) echo "<label><input type=\"checkbox\" name=\"translit\" value=\"1\" /> Транслит</label><br />\n";
echo "<input type=\"submit\" name=\"but\" value=\"Изменить\" /></form>";
foot();
}
else{
$text=trim($_POST['text']);
$msg = strtr($msg, array('B'=>'.', 'b'=>'.', 'C'=>'.', 'c'=>'.', 'D'=>'.', 'd'=>'.', 'F'=>'.', 'f'=>'.', 'G'=>'.', 'g'=>'.', 'H'=>'.', 'h'=>'.', 'I'=>'.', 'i'=>'.', 'J'=>'.', 'j'=>'.', 'K'=>'.', 'k'=>'.', 'L'=>'.', 'l'=>'.', 'M'=>'.', 'm'=>'.', 'N'=>'...', 'n'=>'.', 'O'=>'.', 'o'=>'.', 'P'=>'.', 'p'=>'.', 'Q'=>'.', 'q'=>'.', 'R'=>'.', 'r'=>'.', 'S'=>'.', 's'=>'.', 'T'=>'.', 't'=>'.', 'U'=>'.', 'u'=>'.', 'V'=>'.', 'v'=>'.', 'W'=>'.', 'w'=>'.', 'X'=>'.', 'x'=>'.', 'Y'=>'.', 'y'=>'.', 'Z'=>'.', 'z'=>'.'));

if($_POST['translit']==1){
$text=translit($text);
}
$text=mysql_escape_string(htmlspecialchars(stripslashes($text)));
if(strlen($text)<10){
header("Location: blogs.php?act=edit&id=".$id."&l&SESS=".session_id());
exit;
}
if(mysql_result(mysql_query("select `id` from `blogs` where `uid`='$uid' and `text` like $text;"),'id')!=0){
header("Location: blogs.php?act=see&id=".$uid."&SESS=".session_id());
exit;
}
$check_add=@mysql_result(mysql_query("select `add_date` from `blogs` where `id`='$id';"),'add_date');
if(!empty($check_add)){
$add=trim($_POST['add']);
if(strlen($add)<6){
header("Location: blogs.php?act=edit&id=".$id."&a&SESS=".session_id());
exit;
}
$msg = strtr($msg, array('B'=>'.', 'b'=>'.', 'C'=>'.', 'c'=>'.', 'D'=>'.', 'd'=>'.', 'F'=>'.', 'f'=>'.', 'G'=>'.', 'g'=>'.', 'H'=>'.', 'h'=>'.', 'I'=>'.', 'i'=>'.', 'J'=>'.', 'j'=>'.', 'K'=>'.', 'k'=>'.', 'L'=>'.', 'l'=>'.', 'M'=>'.', 'm'=>'.', 'N'=>'...', 'n'=>'.', 'O'=>'.', 'o'=>'.', 'P'=>'.', 'p'=>'.', 'Q'=>'.', 'q'=>'.', 'R'=>'.', 'r'=>'.', 'S'=>'.', 's'=>'.', 'T'=>'.', 't'=>'.', 'U'=>'.', 'u'=>'.', 'V'=>'.', 'v'=>'.', 'W'=>'.', 'w'=>'.', 'X'=>'.', 'x'=>'.', 'Y'=>'.', 'y'=>'.', 'Z'=>'.', 'z'=>'.'));

if($_POST['translit']==1) $add=translit($add);
$add=mysql_escape_string(htmlspecialchars(stripslashes($add)));
$query="`add`='$add',";
}
else $query=null;


$date=time();
mysql_query("update `blogs` set `text`='$text', ".$query." `update`='$date', `id_update`='".$_SESSION['id_user']."' where `id`='$id' ;");
header("Location: blogs.php?act=rec&id=".$id."&SESS=".session_id());
}

break;

case 'toadd':

if(!isset($user)){
header("location: blogs.php");
exit;
}

$id=(isset($_GET['id'])) ? (int)abs((int)$_GET['id']) : (int)abs((int)$_POST['id']);

$res=mysql_query("select `uid`,`text` from `blogs` where `id`='$id';");
if(mysql_num_rows($res)==0){
header("Location: blogs.php?SESS=".session_id());
exit;
}

$data=mysql_fetch_array($res);
if($_SESSION['id_user']!=$data['uid']){
header("Location: blogs.php?SESS=".session_id());
exit;
}

if(!isset($_POST['but'])){
head();

echo "Добавить к:<div>".esc(trim(br(bbcode(smiles(links(substr($data['name'],0,100)))))))."</div><hr />";

if(isset($_GET['l'])) echo "Что так мало дописываешь? (:<br />";

echo "<div><form action=\"blogs.php?act=toadd\" method=\"post\">";
echo "Текст: <a href=\"/smiles\"><small>Смайлы</small></a><br />";
echo "<textarea name=\"add\" rows=\"3\" cols=\"20\"></textarea><br />";
echo "<input type=\"hidden\" name=\"id\" value=\"".$id."\" />";
$msg = strtr($msg, array('B'=>'.', 'b'=>'.', 'C'=>'.', 'c'=>'.', 'D'=>'.', 'd'=>'.', 'F'=>'.', 'f'=>'.', 'G'=>'.', 'g'=>'.', 'H'=>'.', 'h'=>'.', 'I'=>'.', 'i'=>'.', 'J'=>'.', 'j'=>'.', 'K'=>'.', 'k'=>'.', 'L'=>'.', 'l'=>'.', 'M'=>'.', 'm'=>'.', 'N'=>'...', 'n'=>'.', 'O'=>'.', 'o'=>'.', 'P'=>'.', 'p'=>'.', 'Q'=>'.', 'q'=>'.', 'R'=>'.', 'r'=>'.', 'S'=>'.', 's'=>'.', 'T'=>'.', 't'=>'.', 'U'=>'.', 'u'=>'.', 'V'=>'.', 'v'=>'.', 'W'=>'.', 'w'=>'.', 'X'=>'.', 'x'=>'.', 'Y'=>'.', 'y'=>'.', 'Z'=>'.', 'z'=>'.'));

if ($user['set_translit']==1) echo "<label><input type=\"checkbox\" name=\"translit\" value=\"1\" /> Транслит</label><br />\n";
echo "<input type=\"submit\" name=\"but\" value=\"Добавить\" /></form></div>";
echo "<br />&#187; <a href=\"?act=rec&amp;id=".$id."\">Назад</a>";
echo "<br />&#187; <a href=\"blogs.php\">К блогам</a>";

foot();
}
else{
$add=trim($_POST['add']);
if(strlen($add)<6){
header("Location: blogs.php?act=toadd&id=".$id."&l&SESS=".session_id());
exit;
}
$msg = strtr($msg, array('B'=>'.', 'b'=>'.', 'C'=>'.', 'c'=>'.', 'D'=>'.', 'd'=>'.', 'F'=>'.', 'f'=>'.', 'G'=>'.', 'g'=>'.', 'H'=>'.', 'h'=>'.', 'I'=>'.', 'i'=>'.', 'J'=>'.', 'j'=>'.', 'K'=>'.', 'k'=>'.', 'L'=>'.', 'l'=>'.', 'M'=>'.', 'm'=>'.', 'N'=>'...', 'n'=>'.', 'O'=>'.', 'o'=>'.', 'P'=>'.', 'p'=>'.', 'Q'=>'.', 'q'=>'.', 'R'=>'.', 'r'=>'.', 'S'=>'.', 's'=>'.', 'T'=>'.', 't'=>'.', 'U'=>'.', 'u'=>'.', 'V'=>'.', 'v'=>'.', 'W'=>'.', 'w'=>'.', 'X'=>'.', 'x'=>'.', 'Y'=>'.', 'y'=>'.', 'Z'=>'.', 'z'=>'.'));

if($_POST['translit']==1) $add=translit($add);
$add=mysql_escape_string(htmlspecialchars(stripslashes($add)));
mysql_query("update `blogs` set `add_date`='".time()."', `add`='$add' where `id`='$id';");
header("Location: blogs.php?act=rec&id=".$id."&SESS=".session_id());
}

break;

default:

$page = ( isset($_GET['p']) ) ? (int)abs((int)$_GET['p']) : 1;
$page = ( $page <= 1 ) ? 1 : $page;
$start = ( $page == 1 ) ? 0 : ($page-1)*$p_str;

$c = mysql_result(mysql_query("select count(`id`) from `blogs_title`;"), 'count');
$all = ceil($c/$p_str);

if ( $page > $all && $all != 0 ){
header("Location: blogs.php?p=$all&SESS=".session_id());
exit;
}
else{
head();
if(mysql_result(mysql_query("select count(`id`) from `blogs_title` where `uid`='".$_SESSION['id_user']."';"),'count')==0){
if(isset($user)) echo "<div class=\"foot\"><img src='../forum/New.gif' alt=''/> <a href=\"blogs.php?act=addblog\">Создать дневник</a></div>";
}
else echo "<div class=\"foot\"><img src='../forum/New.gif' alt=''/> <a href=\"blogs.php?act=see&amp;id=".$_SESSION['id_user']."\">Мой дневник</a></div><div class=\"foot\"><img src='/sys/img/forum.gif' alt=''/> <a href=\"blogs.php?act=add\">Написать в дневник</a></div>";

if($c==0) echo "Блоггеров пока нет<br />";
else{
//////////////////////////////////////////////////////////////

$k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `blogs`"),0);
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
$res = mysql_query("select * from `blogs` order by `date` desc limit $start,$p_str;"); 
while ($blog = mysql_fetch_array($res)){

$login=mysql_fetch_array(mysql_query("select `nick`,`pol` from `user` where `id`='".$blog['uid']."' limit 1;"));

if($num==1){ 
echo "<div class='str'>"; 
$num=0;
}else{ 
echo "<div class='main_menu'>"; 
$num=1;}

echo "".online($blog['uid'])." ";
echo "".$login['nick']." ";

echo "(".vremja($blog['date']).")<br/>";
$url = "files/blog";
if (is_file($url."/".$blog['id'].".jpg")) echo "<img src='/sys/img/photo.gif' alt=''/> ";
else
if (is_file($url."/".$blog['id'].".gif")) echo "<img src='/sys/img/photo.gif' alt=''/> ";
else
if (is_file($url."/".$blog['id'].".png")) echo "<img src='/sys/img/photo.gif' alt=''/> ";
else
echo "<img src='/img/bullet.png' alt='' /> ";

echo "<a href=\"/blogs.php?act=rec&amp;id=".$blog['id']."\">";
if ($blog['name']!=NULL)echo esc(trim(br(bbcode(smiles(links($blog['name']))))))." ";
else
echo "Без названия\n";
echo "</a> (";
echo mysql_result(mysql_query("select count(`id`) from `blogs_comments` where `record`='".$blog['id']."';"),'count')."";
echo ")\n";

echo "</div>";
}

if ($k_page>1)str("blogs.php?",$k_page,$page);

////////////////////////////////////////////
}
}
foot();


}

?>