<?php

/*************************************
             **         **           *
              **       **            *
               **  *  **             *
                ** * **              *
                 *   *               *
                                     *
        Created by Webs Studio       *
       http://webs.alfaspace.net     *
                                     *
Coding by SapienS, icq: 4780347      *
Design by S.T.A.L.K.E.R, icq: 268392 *
*************************************/

//error_reporting(0);

extract ($_REQUEST,$_SESSION,$_COOKIE);

include 'config.inc.php';

session_start();

$today = date("j.m.y, G:i");

/* Функция проверки бана */

function CheckBan($rem_ip)
{
$list_bans = file_get_contents("data/banned.web");
$test_ip = substr_count($list_bans, $rem_ip);
if ($test_ip) { return true; } else { return false; }
}

if ($is_banned = CheckBan($_SERVER['REMOTE_ADDR']))
{
  echo "<br><br><center><div style='height: 43px; width: 50%; border: 1px solid black; background-color: #CCCCCC; color: red; font-family: Tahoma; font-size: 30px;'>$ban_mess_to_user</div></center>";
}
else
{

/* Всякие функции */

function ViewPosts($posts_arr)
{
for ($n = 0; $n < count($posts_arr); $n++)
{
$nn = $n+1;
$str_exp = explode("|", $posts_arr[$n]);
if ($str_exp[4]) { $user_icq = "<a href='http://www.icq.com/whitepages/wwp.php?Uin=$str_exp[4]' ><img title='icq: $str_exp[4]' class='data_img' align='absmiddle' src='data/img/icq.gif'></a>"; } else { $user_icq = ""; }
if ($str_exp[5]) { $user_site = "<a href='http://$str_exp[5]'><img title='site: http://$str_exp[5]' class='data_img' align='absmiddle' src='data/img/site.gif'></a>"; } else { $user_site = ""; }
$old_post = "<div id='post'><table width='100%' class='sm_text1'><tr><td width='50%'><b>&nbsp;$str_exp[1]</b> <a href='mailto:$str_exp[3]'><img title='mail: $str_exp[3]' class='data_img' align='absmiddle' src='data/img/mail.gif'></a> $user_icq  $user_site</td><td align='right' width='50%'><font style='font-size: 10px;'>№$nn &nbsp; </font></td></tr></table>
<table class='sm_text3'><tr><td>&nbsp;&nbsp;$str_exp[2]</td></tr></table>
<table width='100%' class='sm_text2'><tr><td width='50%'>";
if ($_SESSION['wb_admin']) { $link_num = $n+1;  $old_post .= "<a href='admin/index.php?view=edit&edit=$link_num'>Редактировать</a> | <a href='admin/index.php?del=$link_num'>Удалить</a> | <a href='admin/index.php?view=ban&ban=$link_num'>Забанить - $str_exp[6]</a>"; }
$old_post .= "</td><td><div align='right'><i>Добавлено: $str_exp[0]</i></div></td></tr></table></div>";
$old_post_arr[$n] = $old_post;
}
$old_post_arr = array_reverse($old_post_arr);
return $old_post_arr;
}

function AddPost($nick, $mess, $today, $mail, $icq, $site, $ip)
{
if ($nick == $adm_login) { $nick = "<font color='red'>$nick</font>"; }
$fa = fopen("data/posts.web", "a+");
fputs($fa, "$today|$nick|$mess|$mail|$icq|$site|$ip|\n");
fclose($fa);
return true;
}

function AddSmiles($msg)
{
$smile_conf = file("data/smiles/smile_conf.dat");
for ($s = 0; $s <= count($smile_conf); $s++)
{
$tmp_exp = explode("##", $smile_conf[$s]);
$smile_cod_arr[$s] = $tmp_exp[0];
$smile_img_arr[$s] = $tmp_exp[1];
}
for ($s1 = 0; $s1 <= count($smile_conf); $s1++)
{
$smile_img_arr[$s1] = trim($smile_img_arr[$s1]);
$smile_img = "<img align='middle' border='0' src='data/smiles/$smile_img_arr[$s1]'>";
$msg = str_replace($smile_cod_arr[$s1], $smile_img, $msg);
}
return $msg;
}
function Check($perem)
{
$perem = trim($perem);
$perem = htmlspecialchars((stripslashes($perem)), ENT_QUOTES);
$perem = str_replace("\n", "<br>", $perem);
return $perem;
}
function AddBB($mess)
{
$mess = str_replace("&#039;", "'", $mess);
$mess = str_replace("[b]", "<b>", $mess);
$mess = str_replace("[/b]", "</b>", $mess);
$mess = str_replace("[i]", "<i>", $mess);
$mess = str_replace("[/i]", "</i>", $mess);
$mess = str_replace("[u]", "<u>", $mess);
$mess = str_replace("[/u]", "</u>", $mess);
$mess = str_replace("[left]", "<div align='left'>", $mess);
$mess = str_replace("[/left]", "</div>", $mess);
$mess = str_replace("[center]", "<center>", $mess);
$mess = str_replace("[/center]", "</center>", $mess);
$mess = str_replace("[right]", "<div align='right'>", $mess);
$mess = str_replace("[/right]", "</div>", $mess);
$mess = str_replace("[br]", "<br>", $mess);
$mess = str_replace("[font size=\"+1\"]", "<font size=\"+1\">", $mess);
$mess = str_replace("[/font]", "</font>", $mess);
$mess = str_replace("[font size=\"-1\"]", "<font size=\"-1\">", $mess);
$mess = str_replace("[url=`", "<a href=", $mess);
$mess = str_replace("`]", ">", $mess);
$mess = str_replace("[/url]", "</a>", $mess);
$mess = str_replace("[mail]", "<a href='mailto:'>", $mess);
$mess = str_replace("[/mail]", "</a>", $mess);
return $mess;
}

function ViewPages($wb_post_on_page, $posts_arr)
{
$all_posts = count ($posts_arr);
$how_many_pages = ceil($all_posts/$wb_post_on_page);
return $how_many_pages;
}

function SaveUserData($nick, $mail, $icq, $site)
{
setcookie("wb_nick", $nick, mktime(0,0,0,01,01,2010));
setcookie("wb_mail", $mail, mktime(0,0,0,01,01,2010));
if ($icq) { setcookie("wb_icq", $icq, mktime(0,0,0,01,01,2010)); }
if ($site) { setcookie("wb_site", $site, mktime(0,0,0,01,01,2010)); }
}

function DelUserData($nick, $mail, $icq, $site)
{
if ($_COOKIE["wb_nick"]) { setcookie("wb_nick"); }
if ($_COOKIE["wb_mail"]) { setcookie("wb_mail"); }
if ($_COOKIE["wb_icq"]) { setcookie("wb_icq"); }
if ($_COOKIE["wb_site"]) { setcookie("wb_site"); }
}

function GetUserData($wb_name, $wb_mail, $wb_icq, $wb_site)
{
$wb_name = trim($wb_name);
$wb_name = htmlspecialchars((stripslashes($wb_name)), ENT_QUOTES);
$wb_mail = trim($wb_mail);
$wb_mail = htmlspecialchars((stripslashes($wb_mail)), ENT_QUOTES);
$wb_icq = trim($wb_icq);
$wb_icq = htmlspecialchars((stripslashes($wb_icq)), ENT_QUOTES);
$wb_site = trim($wb_site);
$wb_site = htmlspecialchars((stripslashes($wb_site)), ENT_QUOTES);
$user_data = array(
$wb_name,
$wb_mail,
$wb_icq,
$wb_site);
return $user_data;
}

function DoSafeCod()
{
for($e = 1; $e <= 4; $e++)
{
$cod_num = rand(0,9);
$what_cod .= $cod_num;
$html_cods .= "<img src='data/img/nums/$cod_num.gif' align='absmiddle' class='safe_cod_img'>";
}
$cods = array($html_cods, $what_cod);
return $cods;
}


/* Массив сообщений об ошибках */

$error_arr = array(
"Не введено имя!",
"Не введено сообщение!",
"Имя и сообщение не введены!",
"E-mail не введен!",
"E-mail введен неверно!",
"Код не введен!",
"Введенный код не совпадает с кодом на изображении!");

$posts_arr = file("data/posts.web");
$nums_posts = count($posts_arr);
if ($posts_arr) { $old_post_arr = ViewPosts($posts_arr); } else { $no_posts = "Сообщений нет!"; }


/* Что делать? */

if ($_POST['subm'])
{
if ($_POST['new_post_nick'] && $_POST['new_post_mess'] && $_POST['new_post_mail'])
{
$nick = Check($_POST['new_post_nick']);
$mess = Check($_POST['new_post_mess']);
$mess = AddSmiles($mess);
$mess = AddBB($mess);
$mail = Check($_POST['new_post_mail']);
if (!preg_match("/[0-9a-z_]+@[0-9a-z_^\.]+\.[a-z]{2,3}/i", $mail)) { $error_mess = $error_arr[4]; }
$icq = $_POST['new_post_icq'];
if ($icq) { $icq = Check($icq); }
$site = $_POST['new_post_site'];
if ($site) { $site = Check($site); $site = str_replace("http://", "", $site); }
if (!$error_mess)
{
  $need_cod = base64_decode($_POST['need_cod']);

  if (!$_POST['user_cod']) { $error_mess = $error_arr[5]; }
  elseif($_POST['user_cod'] == $need_cod)
  {
     if ($_POST['save_data'] == "yes") { SaveUserData($nick, $mail, $icq, $site); } else { DelUserData($nick, $mail, $icq, $site); }
    $add_p = AddPost($nick, $mess, $today, $mail, $icq, $site, $_SERVER['REMOTE_ADDR']);
  }
  else { $error_mess = $error_arr[6]; }
  
}
}
elseif ((!$_POST['new_post_mail']) && $_POST['new_post_nick'] && $_POST['new_post_mess'])
{
$error_mess = $error_arr[3];
}
elseif ((!$_POST['new_post_nick']) && $_POST['new_post_mess'])
{
$error_mess = $error_arr[0];
}
elseif ($_POST['new_post_nick'] && (!$_POST['new_post_mess']))
{
$error_mess = $error_arr[1];
}
elseif ((!$_POST['new_post_nick']) && (!$_POST['new_post_mess']))
{
$error_mess = $error_arr[2];
}
}

if ($_COOKIE['wb_nick']) { $user_old_data = GetUserData($_COOKIE['wb_nick'], $_COOKIE['wb_mail'], $_COOKIE['wb_icq'], $_COOKIE['wb_site']); }

if (!$add_p)
{
echo ("<!doctype html public '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
   <head>
      <title>$wb_title</title>
   <link rel='stylesheet' href='themes/$wb_theme/style.css' type='text/css'>
   <meta http-equiv='Content-Type' content='text/html' charset='windows-1251'>
   <script language='javascript'>
   function Paste(new_post_mess, bb)
{
  new_post_mess.focus();
  new_post_mess.value = new_post_mess.value + bb;
  new_post_mess.blur();
  return;
}
   </script>");

if ($error_mess) { echo("<script language='javascript'>alert('$error_mess');</script>"); }
if ($_SESSION['wb_admin']) { $enter_to_admin = "<a href='admin'>Admin-Zone</a> |"; }

echo ("</head>
   <body>
   <center><a name='top'></a>
   <table class='main_tab'>
     <tr class='logo_tr'>
     <td class='logo_td'><div align='right'><a href='$wb_url_site'>На сайт</a> | $enter_to_admin Всего сообщений: $nums_posts | <a href='javascript:window.close()'>Выход</a> &nbsp; </div>
     </td>
     </tr>
     
     <tr class='post_tr'>
     <td class='post_td'>");
$page = $_GET['page'];
$page = Check($page);
$how_many_pages = ViewPages($wb_post_on_page, $posts_arr);

if ($no_posts) { echo "<br><center><font color='red'>$no_posts</font></center><br>"; }
else
{

/* Постраничный вывод */

if ($page == 1 || !$page)
{
for ($n = 0; $n < $wb_post_on_page; $n++)
{
if($old_post_arr[$n])
{
if ($old_post_arr[$n]) {echo $old_post_arr[$n]; echo ("<br><br>"); }
}
}
}
else
{
for ($w = $wb_post_on_page*($page - 1); $w < $wb_post_on_page*($page - 1) + $wb_post_on_page; $w++)
{
if ($old_post_arr[$w]) {echo $old_post_arr[$w]; echo ("<br><br>"); }
}
}
}

if ($how_many_pages > 1)
{
echo "<center>";
for ($f = 1; $f <= $how_many_pages; $f++)
{
if ($f == $page) { echo " $f "; }
else { echo "<a href='index.php?page=$f'>[$f]</a>"; }
}
echo "</center><br>";
}

$generate = DoSafeCod();
$generate[1] = base64_encode($generate[1]);

echo ("</td>
     </tr>
     
     <tr class='new_post_tr'>
     <td class='new_post_td'>
     <form method='POST'><br>
     <center>
     <table class='data_tab'>
     <tr>
     <td class='data_td'>Имя:<sup><font color='red'>*</font></sup> 
     </td>
     <td class='data_td'><input name='new_post_nick' size='15' maxlength='20' value='$user_old_data[0]'>
     </td>
     <td class='data_td'><div align='right'>ICQ:</div>
     </td>
     <td class='data_td'><div align='right'><input name='new_post_icq' size='15' maxlength='10' value='$user_old_data[2]'></div>
     </td>
     </tr>
     <tr>
     <td class='data_td'>E-mail:<sup><font color='red'>*</font></sup>
     </td>
     <td class='data_td'><input name='new_post_mail' size='15' maxlength='80' value='$user_old_data[1]'>
     </td>
     <td class='data_td'><div align='right'>Сайт:</div>
     </td>
     <td class='data_td'><div align='right'><input name='new_post_site' size='15' maxlength='100' value='$user_old_data[3]'></div>
     </td>
     </tr>
     <tr>
     <td colspan='4' class='data_td'><div align='right'><input checked name='save_data' value='yes' type='checkbox'>Запомнить</div>
     </td>
     </tr>
     </table>
     Сообщение:<sup><font color='red'>*</font></sup>
     <br><br>
     <a onclick='javascript: var bb; bb = \"[b][/b]\"; Paste(new_post_mess, bb)'> <img alt='Bold' src='data/img/text_bold.png' class='code_img'></a>
     <a onclick='javascript: var bb; bb = \"[i][/i]\"; Paste(new_post_mess, bb)'><img alt='It' src='data/img/text_italic.png' class='code_img'></a>
     <a onclick='javascript: var bb; bb = \"[u][/u]\"; Paste(new_post_mess, bb)'><img alt='Under' src='data/img/text_under.png' class='code_img'></a>
     &nbsp;
     <a onclick='javascript: var bb; bb = \"[left][/left]\"; Paste(new_post_mess, bb)'><img alt='Left' src='data/img/left.png' class='code_img'></a>
     <a onclick='javascript: var bb; bb = \"[center][/center]\"; Paste(new_post_mess, bb)'><img alt='Center' src='data/img/center.png' class='code_img'></a>
     <a onclick='javascript: var bb; bb = \"[right][/right]\"; Paste(new_post_mess, bb)'><img alt='Right' src='data/img/right.png' class='code_img'></a>
     &nbsp;
     <a onclick='javascript: var bb; bb = \"[br]\"; Paste(new_post_mess, bb)'><img alt='Enter' src='data/img/enter.png' class='code_img'></a>
     &nbsp;
     <a onclick='javascript: var bb; bb = \"[font size=+1][/font]\"; Paste(new_post_mess, bb)'><img alt='Up' src='data/img/up.png' class='code_img'></a>
     <a onclick='javascript: var bb; bb = \"[font size=-1][/font]\"; Paste(new_post_mess, bb)'><img alt='Down' src='data/img/down.png' class='code_img'></a>
     &nbsp;
     <a onclick='javascript: var bb; bb = \"[url=`http://`][/url]\"; Paste(new_post_mess, bb)'><img alt='Url' src='data/img/url.png' class='code_img'></a>
     <a onclick='javascript: var bb; bb = \"[mail][/mail]\"; Paste(new_post_mess, bb)'><img alt='Mail' src='data/img/mail.png' class='code_img'></a>
     
      <br>

     <a onclick='javascript: var bb; bb = \":)\"; Paste(new_post_mess, bb)'><img class='smile_img' alt=':)' title=':)' src='data/smiles/smile.gif'></a>
     <a onclick='javascript: var bb; bb = \":(\"; Paste(new_post_mess, bb)'><img class='smile_img' alt=':(' title=':(' src='data/smiles/dry.gif'></a>
     <a onclick='javascript: var bb; bb = \";)\"; Paste(new_post_mess, bb)'><img class='smile_img' alt=';)' title=';)' src='data/smiles/wink.gif'></a>
     <a onclick='javascript: var bb; bb = \":D\"; Paste(new_post_mess, bb)'><img class='smile_img' alt=':D' title=':D' src='data/smiles/biggrin.gif'></a>
     <a onclick='javascript: var bb; bb = \"**lol**\"; Paste(new_post_mess, bb)'><img class='smile_img' alt='**lol**' title='**lol**' src='data/smiles/lol.gif'></a>
     <a onclick='javascript: var bb; bb = \":P\"; Paste(new_post_mess, bb)'><img class='smile_img' alt=':P' title=':P' src='data/smiles/tongue.gif'></a>
     <a onclick='javascript: var bb; bb = \"%-)\"; Paste(new_post_mess, bb)'><img class='smile_img' alt='%-)' title='%-)' src='data/smiles/crazy.gif'></a>
     <a onclick='javascript: var bb; bb = \"**good**\"; Paste(new_post_mess, bb)'><img class='smile_img' alt='**good**' title='**good**' src='data/smiles/good.gif'></a>
     <a onclick='javascript: var bb; bb = \"8)\"; Paste(new_post_mess, bb)'><img class='smile_img' alt='8)' title='8)' src='data/smiles/dirol.gif'></a>
     <a onclick='javascript: var bb; bb = \"**unsure**\"; Paste(new_post_mess, bb)'><img class='smile_img' alt='**unsure**' title='**unsure**' src='data/smiles/unsure.gif'></a>
     <a onclick='javascript: var bb; bb = \"**shock**\"; Paste(new_post_mess, bb)'><img class='smile_img' alt='**shock**' title='**shock**' src='data/smiles/shock.gif'></a>
     <a onclick='javascript: var bb; bb = \"**diablo**\"; Paste(new_post_mess, bb)'><img class='smile_img' alt='**diablo**' title='**diablo**' src='data/smiles/diablo.gif'></a>
     <a onclick='javascript: var bb; bb = \":`(\"; Paste(new_post_mess, bb)'><img class='smile_img' alt=':`(' title=':`(' src='data/smiles/cray.gif'></a>
     <br>
     <textarea name='new_post_mess' cols='40' rows='9' wrap='virtual' maxlength='300'></textarea>
     <br><br>
     $generate[0] &nbsp; <input name='user_cod' size='4' maxlength='4'>
     <br><br>
     <input name='subm' type='submit' value='Добавить' class='submit'><br><sup><font color='red'>*</font></sup><font size='-2'> - поля, обязательные для заполнения</font><br>
     <div align='right'><a href='#top'>^ Наверх ^</a> &nbsp; </div>
     <input type='hidden' name='need_cod' value='$generate[1]'>
     </form>
     </td>
     </tr>
     
     <tr class='bottom_tr'>
     <td class='bottom_td'><font size='-2'>Webs B00k v 1.0 | Created by <a href='http://webs-studio.net.ru' target='_blank'>Webs Studio</a> | 2005 &copy;</font>
     </td>
     </tr>

   </table>
   </center>
   </body>
</html>");
}
else
{
$how_many_pages = ViewPages($wb_post_on_page, $posts_arr);
echo ("<html><head><title>$wb_title</title><meta http-equiv='refresh' content='2' url='index.php'><link rel='stylesheet' href='themes/$wb_theme/style.css' type='text/css'></head><body><center><table class='main-error'><tr><td class='error_1'><br><b>Сообщение добавлено!</b><br><br>Подождите...<br><br><font size='-2'><a href='index.php?page=$how_many_pages'>Если ваш браузер не поддерживает автоматическую переадресацию, нажмите сюда</a></font><br></td></tr></table></center></body></html>
");

}

}

?>
