<?php
include("./inc/option.php");
$code1="&#67;&#111;&#112;&#121;&#114;&#105;&#103;&#104;&#116;&nbsp;&#169;&nbsp;&#65;&#114;&#116;&#45;&#83;&#111;&#102;&#116;";
$code2="&#104;&#116;&#116;&#112;&#58;&#47;&#47;&#97;&#114;&#116;&#45;&#115;&#111;&#102;&#116;&#46;&#114;&#117;";
$code3="&#119;&#119;&#119;&#46;&#97;&#114;&#116;&#45;&#115;&#111;&#102;&#116;&#46;&#114;&#117;";
$ban_html = "<SCR"."IPT LANGUAGE=\"JavaSc"."ript\">document.location.replace('".$goban."');</scr"."ipt>";
$fban=file($fb);
if ((!$code1) || (!$code2) || (!$code3)) exit;
for ($j=0;$j<count($fban);$j++) if (($fban[$j]==getenv("REMOTE_ADDR")."\n") && ($_GET["t"] != "admin")) echo $ban_html;
if (file_exists("./inc/header.html")) include("./inc/header.html");
$notes = file(GEST_DB); $count_notes = count($notes);
if (isset($_GET['pg'])) $pg=htmlspecialchars($_GET['pg']); else $pg="1"; if ($pg=="") $pg="1";
if (isset($_GET['note'])) $n=$_GET['note'];
if ($_GET['reply']=="yes") {
   $record = explode("¤", trim($notes[$n]));$record[3]=str_replace("<br />", "\n", $record[3]);
   $InsStr="[QUOTE]".$record[3]."[/QUOTE]";
   echo "<form name=\"ins\"><input type=\"hidden\" name=\"msg\" value=\"".$InsStr."\"></form>"; ?>
<?php }
   else { echo "<form name=\"ins\"><input type=\"hidden\" name=\"msg\" value=\"\"></form>"; }
$formadmin=false;$admin=$_POST["pwd"];
if (isset($_GET["t"])) $LocSearch=$_GET["t"]; else $LocSearch="public";
if (($LocSearch=="admin") && ((!isset($_POST["pwd"])) || ($admin != ADMIN_PWD))) GetAdmin ();
if (isset($_GET['action'])) {
  if ($_GET['action']=="delete") {
	  $notes[$n]="";$fopt = fopen (GEST_DB, "w");chmod(GEST_DB, 0733);
	  for ($j=0;$j<$count_notes;$j++) $result = fwrite($fopt, $notes[$j]);
	  fclose($fopt);
  }
  elseif ($_GET['action']=="ban") {
	  $record = explode("¤", trim($notes[$n]));
	  $ban_yes=false;
	  for ($i=0;$i<count($fban);$i++) if ($fban[$i]==$record[4]."\n") {$ban_yes=true;break;}
      if (!$ban_yes) 
      {
	  $fban=fopen($fb,"a");
      $result = fputs($fban, $record[4]."\n");
	  fclose($fban);
	  }
  }
 elseif ($_GET['action']=="edit") { 
    $record = explode("¤", trim($notes[$n]));
    if (!$_POST['t_body']) {
      $record[3] = str_replace("<br />", " ", $record[3]); ?>
      <center><form method="post" action="index.php?t=admin&action=edit&note=<?php echo $n; ?>"><input type="hidden" name="pwd" value="<? echo $admin; ?>"><textarea cols="50" rows="15" name="t_body" class="text" style="width:400px"><?php echo $record[3]; ?></textarea><center><input class=sb2 type="submit"></form>
      <?php exit; 
    }
    $t_body = eregi_replace("\n", " ", nl2br(htmlspecialchars($_POST['t_body'])));
    $notes[$n]=$record[0]."¤".$record[1]."¤".$record[2]."¤".$t_body."¤".$record[4]."\n";
    $fopt = fopen (GEST_DB, "w");chmod(GEST_DB, 0733);
  	for ($j=0;$j<$count_notes;$j++) $result = fwrite($fopt, $notes[$j]);
    fclose($fopt);
  }
 else  if (!$formadmin) GetAdmin ();
}
if (isset($_POST['body'])) {
    $body = eregi_replace("\n", " ", nl2br(htmlspecialchars($_POST['body'])));
	$name = htmlspecialchars(substr(trim($_POST['name']), 0, 40));
    $mail = htmlspecialchars(substr(trim($_POST['mail']), 0, 40));
    eregi("^.+@(.+\.)+.+$", $mail, $mails); 
    $mail = $mails[0];
	if (empty($name) && isset($mail)) {
		eregi("^.+@", $mail, $names);
		$name = substr($names[0], 0, strlen($names[0])-1);
	}
	elseif (empty($name)) $name = noname;
	if (empty($mail)) $mail = '';
	$ip=getenv("REMOTE_ADDR");
	save_note(date('G:i d:m:Y')."¤".$name."¤".$mail."¤".$body."¤".$ip);
}
if (file_exists(GEST_DB)) {
    $notes = file(GEST_DB);$count_notes = count($notes);$on_page = notes_on_page;$mlim = nav_pages;$count_pages = ceil($count_notes/$on_page);
	if (isset($_GET['pg']) && ereg("^[0-9]{1,3}$", $_GET['pg'])) $start = $_GET['pg']-1; else $start = 0;
	if ($start > $count_pages) $start = 0;
	$first = $count_notes - ($start * $on_page);$last = $count_notes - ($start * $on_page) - $on_page;
} ?>
<DIV class=blue><table width=100% height=18 border=0 bgcolor=<?php echo fone_bg; ?>><tr><td width="100%" align=center>
<?php
if (isset($count_pages) && $count_pages > 1) {
    $notes = file(GEST_DB);$count_notes = count($notes);$before = $start;$after = $count_pages - $start - 1;
	if ($before > $mlim) $before = $mlim;
	if ($after > $mlim) $after = $mlim;
	$mstart = $start - $before;$mfinish = $start + $after+1;	 
	for ($i=$mstart; $i<$mfinish; $i++) {
		if ($i == $start) echo "<span style=\"padding:0px;font:13px Verdana,Tahoma; COLOR: #F00000; FONT-FAMILY: Verdana,Tahoma; Monospace; LETTER-SPACING: 0px; TEXT-DECORATION: none\"><b>[".($i+1)."]</span>";
		else echo "<form action=\"index.php?pg=".($i+1)."&t=".$LocSearch."\" method=\"post\"><a class=main href=\"#`".($i+1)."\" onclick=\"submit();\"><span style=\"padding:0px;font:13px Verdana,Tahoma; color:#000CFF; FONT-FAMILY: \"Arial black\"; Monospace; LETTER-SPACING: 0px; TEXT-DECORATION: none\"><b>[".($i+1)."]</b></a><input type=\"hidden\" name=\"pwd\" value=\"".$admin."\"></form></span>";
		if ($i != $mfinish-1) echo "  \n";
	}
}
?>
<td width=100%>&nbsp;<td nowrap style="padding:0px;font:14px 'Courier new',Verdana,Tahoma;"><a class=main href="#Добавить запись" onClick="return openVMenu('OutGB');"><b><font color=blue><img src="./images/newp.gif" border=0 alt="Добавить запись" width=111 height=20 align=center></b></a></td></tr></table></div>
<div id=OutGB>
<table align=center><tr><td><DIV class=blue>
<form method="post" name="record" action="index.php?t=<?php echo $LocSearch; ?>" onsubmit="return checkform();">
<script language="jscript">
 function checkform() {
	 if (document.record.body.value=='') {alert('No message to Post !');return false}
return true;
}
</script>
<SCRIPT LANGUAGE="JavaScript">
	var _bB=true;
	var _bI=true;
	var _bU=true;
	var _bS=true;
	var _bQ=true;
	var _bC=true;
	var _bR=true;
	var _bL=true;
function getVar(_act, _var, lstr, rstr) {
        var _str = prompt(_act, _var);
		_st=_str;
        if (_str !=null) { 
			_str=lstr+_str+rstr;
		    document.record.body.value+=_str;
		}
}
function getUrl () {
   getVar("Введите ссылку:","http://","[URL=","");
   getVar("Введите описание ссылки","MyHomePage","]","[/URL]");
}
function getImg () {
   getVar("Введите ссылку c изображением:","http://","[IMG=","]");
}
function getEml () {
   getVar("Введите email:","","[URL=mailto:","]");
   if (_st != null) {
	   _st+="[/URL]"; 
	   document.record.body.value+=_st;
   }
}
function InsertTag(_tag) {
	_t=_tag.substr(0,1);
	if (_t=="B") {
	  if (_bB) {_tg='['+_tag+']';_bB=false;}
	    else {_tg='[/'+_tag+']';_bB=true;}
	}
	if (_t=="I") {
	  if (_bI) {_tg='['+_tag+']';_bI=false;}
	    else {_tg='[/'+_tag+']';_bI=true;}
	}
	if (_t=="U") {
	  if (_bU) {_tg='['+_tag+']';_bU=false;}
	    else {_tg='[/'+_tag+']';_bU=true;}
	}
	if (_t=="S") {
	  if (_bS) {_tg='['+_tag+']';_bS=false;}
	    else {_tg='[/'+_tag+']';_bS=true;}
	}
	if (_t=="Q") {
	  if (_bQ) {_tg='['+_tag+']';_bQ=false;}
	    else {_tg='[/'+_tag+']';_bQ=true;}
	}
	if (_t=="C") {
	  if (_bC) {_tg='['+_tag+']';_bC=false;}
	    else {_tg='[/'+_tag+']';_bC=true;}
	}
	if (_t=="R") {
	  if (_bR) {_tg='['+_tag+']';_bR=false;}
	    else {_tg='[/'+_tag+']';_bR=true;}
	}
	if (_t=="L") {
	  if (_bL) {_tg='['+_tag+']';_bL=false;}
	    else {_tg='[/'+_tag+']';_bL=true;}
	}
    document.record.body.value+=_tg;  
    }

</SCRIPT>
<table border="0" cellspacing="0" cellpadding="0" width=500 align=center><tr><td>
<table border="0" cellspacing="0" cellpadding="4" width=100% background="<?php echo fone_bk; ?>" bgcolor="<?php echo fone_bg; ?>">
	<tr height=22><td class=d4><b>Имя: </b>&nbsp;&nbsp;&nbsp;&nbsp;<input class=sb type="text" name="name" id="r_name" size="26" maxlength="40" class="text"></td>
	<td align=center class=d1>
<SELECT style="width:180px" Size="1" name="clr" onChange="document.record.body.value+=('[COLOR='+this.options[this.selectedIndex].value+']');">
<OPTION value="" selected>ЦВЕТ</OPTION>
<SCRIPT LANGUAGE="JavaScript">
<!--
  colors=["black", "blue", "green", "aquamarine", "chocolate", "magenta", "gold", "red", "yellow", "hotpink", "pink", "lime", "violet", "Brown", "Orange", "yellowgreen", "thistle", "silver", "salmon", "tan", "wheat", "tomato", "springgreen", "turquoise", "lightslategray", "lightgreen", "lightblue", "maroon", "olive", "teal", "purple", "aqua", "White"];
  for (j=0;j<33;j++) document.write('<OPTION style=\'BACKGROUND-COLOR: ',colors[j],';\' VALUE="',colors[j],'">',colors[j],'</OPTION>');
//-->
</script>
</SELECT> 
	<tr height=22>
		<td class=d4><b>E-mail: </b><input class=sb type="text" name="mail" id="r_mail" size="26" maxlength="40" class="text"></td>
	<td align=center class=d1>
<SELECT style="width:120px" Size="1" name="clr" onChange="document.record.body.value+=('[FONT='+this.options[this.selectedIndex].value+']');">
<OPTION value="" selected>ШРИФТ</OPTION>
<SCRIPT LANGUAGE="JavaScript">
<!--
  fontes=["Arial", "Arial black", "Book Antiqua", "Century", "Courier new", "Georgia", "Impact", "Latha", "Mangal", "Modern", "Roman", "Tahoma", "Times new Roman", "Trebucht MS", "Tunga", "Verdana"];
  for (j=0;j<16;j++) document.write('<OPTION VALUE=\'\"',fontes[j],'\"\'>',fontes[j],'</OPTION>');
//-->
</script>
</SELECT> <SELECT style="width:55px" Size="1" name="clr" onChange="document.record.body.value+=('[SIZE='+this.options[this.selectedIndex].value+']');">
<OPTION value="" selected>SIZE</OPTION>
<SCRIPT LANGUAGE="JavaScript">
<!--
  for (j=1;j<8;j++) document.write('<OPTION VALUE="',j,'">',j,'</OPTION>');
//-->
</script>
</SELECT> 
	</tr></table>
<table border="0" cellspacing="0" cellpadding="0" width=520>
	<tr><td align=center><input class=sb2 type='button' value=' B '       onclick='InsertTag("B");' style="font-weight:bold;cursor:hand;" title="Жирный[B]" />&nbsp;<input class=sb2 type='button' value=' I ' onclick='InsertTag("I");' style="width:18px;font-weight:bold;cursor:hand;" title="Наклонный[I]"  />&nbsp;<input class=sb2 type='button' value=' U ' onclick='InsertTag("U");' style="font-weight:bold;cursor:hand;"  title="Подчёркнутый[U]" />&nbsp;<input class=sb2 type='button' value=' S ' onclick='InsertTag("S");' style="font-weight:bold;cursor:hand;"  title="Зачёркнутый[S]" />&nbsp;<input class=sb2 type='button' value='QUOTE ' onclick='InsertTag("QUOTE");' style="font-weight:bold;cursor:hand;"  title="Цитата[Q]" />&nbsp;<input class=sb2 type='button' value='LEFT' onclick='InsertTag("L");' style="font-weight:bold;cursor:hand;"  title="Выравнивание в лево LEFT" />&nbsp;<input class=sb2 type='button' value='CENTER' onclick='InsertTag("C");' style="font-weight:bold;cursor:hand;"  title="Выравнивание в центр CENTER" />&nbsp;<input class=sb2 type='button' value='RIGHT' onclick='InsertTag("R");' style="font-weight:bold;cursor:hand;"  title="Выравнивание в право RIGHT" />&nbsp;<input class=sb2 type='button' value=' http:// '  style="font-weight:bold;cursor:hand;" onclick='getUrl();' title="Ссылка [H]" />&nbsp;<input class=sb2 type='button' value=' IMG '  style="font-weight:bold;cursor:hand;"" onclick='getImg();' title="Картинка[IMG]" />&nbsp;<input class=sb2 type='button' value=' @ '  style="font-weight:bold;cursor:hand;" onclick='getEml();' title="email [@]" />
	</td></tr></table>
<table border="0" cellspacing="0" cellpadding="0" width=520 nowrap>
<tr><td colspan="2">
<textarea cols="50" rows="16" name="body" id="r_body" class="text" style="width:100%">
</textarea></td></tr></table>
<table border="0" cellspacing="0" cellpadding="4" width=500 nowrap>
<tr><td width=520 nowrap align=center bgcolor="<?php echo fone_bg; ?>" background="<?php echo fone_bk; ?>">
<Script Language="JavaScript">
for (j=0;j<=78;j++) {
	document.write('<a href="#icon'+j+'" onclick="document.record.body.value+=\'[ICON('+j+').gif]\';"><img src="images/icons/icon%20('+j+').gif" border=0 align=absmiddle></a>&nbsp;');
}
</script></p>
</td></tr></table>
<table border="0" cellspacing="0" cellpadding="4" width=520><tr><td height=7>
	<tr><td colspan="2" align="center"><input class=sb2 type="submit" value="Добавить запись" style="cursor:hand;font-weight:bold;">
<input style="font-weight:bold;cursor:hand;" class=sb2 type="reset" value="Очистить" style="cursor:hand;"></td></tr></table></td></tr></table><input type="hidden" name="pwd" value="<?php echo $admin; ?>">
</form>
<SCRIPT LANGUAGE="JavaScript">
 document.record.body.value=document.ins.msg.value;
</script>
</DIV></td></tr></table></div>
<?php if ($_GET['reply']!="yes") { ?>
 <script language="JavaScript">openVMenu('OutGB');</script>
<?php } 
if (file_exists(GEST_DB)) 
	for ($i=$first; $i>$last; $i--) if (isset($notes[$i-1])) { echo "<DIV class=blue>"; print_note($notes[$i-1],($i-1));echo "</div>"; }
?>
<DIV class=blue id=copyright>
<table width=100%  bgcolor="#FFFFFF"><tr><td class=d1 align=right><?php $c="-"; echo $code1; ?>  <a href="<?php echo $code2; ?>"><?php echo $code3; ?></a> all Rights Reserved.</td></tr></table></DIV>
<?php
if (!$c) echo $ban_html; ?>
<small><a target="_top" href="index.php?pg=<?php echo $pg; if ($LocSearch!="admin") echo "&t=admin\"><b><font color=blue>&nbsp;Администратор</b></a>"; else echo "\"><b><font color=blue>&nbsp;Выход</b></a>";
if (file_exists("./inc/footer.html")) include("./inc/footer.html");
function print_note($note,$n) {
global $start,$LocSearch,$admin;
    $record = explode("¤", trim($note));
	echo "\t<table width=\"100%\" height=75 border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tr height=1><td background=".fone_bk." bgcolor=".fone_bg." align=left nowrap>";
	if ($LocSearch=="admin") echo "<form method=\"post\" action=\"index.php?pg=".$pg."&t=".$LocSearch."&action=edit&note=".$n."\"><a href=\"#`\" onclick=\"submit();\"><b>Редактировать</b></a><input type=\"hidden\" name=\"pwd\" value=\"".$admin."\"></form>/<form method=\"post\" action=\"index.php?pg=".$pg."&t=".$LocSearch."&action=delete&note=".$n."\"><a href=\"#`\" onclick=\"submit();\"><b>Удалить</b></a><input type=\"hidden\" name=\"pwd\" value=\"".$admin."\"></form>/<form method=\"post\" action=\"index.php?pg=".$pg."&t=".$LocSearch."&action=ban&note=".$n."\"><a href=\"#`\" onclick=\"submit();\"><b>Забанить</b></a><input type=\"hidden\" name=\"pwd\" value=\"".$admin."\"></form>";
    $st=$record[3];
	$st = str_replace("[QUOTE]", "<table><tr><td bgcolor=#EEEEEE class=d1>", $st);
	$st = str_replace("[/QUOTE]", "</td></tr></table>", $st);
    $st = str_replace("[ICON(", "<img src=images/icons/icon%20(", $st);
    $st = str_replace("[C]", "<center>", $st);
    $st = str_replace("[/C]", "</center>", $st);
    $st = str_replace("[R]", "<div align=right>", $st);
    $st = str_replace("[/R]", "</div>", $st);
    $st = str_replace("[L]", "<div align=left>", $st);
    $st = str_replace("[/L]", "</div>", $st);
    $st = str_replace("[", "<", $st);
    $st = str_replace("]", ">", $st);
    $st = str_replace("IMG=", "img src=", $st);
    $st = str_replace("COLOR=", "font color=", $st);
    $st = str_replace("FONT=", "font face=", $st);
    $st = str_replace("SIZE=", "font size=", $st);
    $st = str_replace("URL=", "a target=_blank href=", $st);
    $st = str_replace("/URL", "a", $st);
    echo "\t<tr><td class=d3 style=\"PADDING-RIGHT: 5px; PADDING-LEFT: 5px;\" background=".fone_bk." bgcolor=".fone_bg."><p align=right>";
	echo "<form method=\"post\" action=\"index.php?pg=".$pg."&t=".$LocSearch."&reply=yes&note=".$n."\"><a  class=main href=\"#`\" onclick=\"submit();\"><img src=\"./images/reply.gif\" border=0 width=91 height=20 alt=\"Ответить\"></a><input type=\"hidden\" name=\"pwd\" value=\"".$admin."\"></form></p>".$st."</td></tr>\n";
	echo "\t<tr><td class=d1 height=22 align=\"right\" background=".fone_bk." background=".fone_bk." bgcolor=".fone_bg."><table><tr><td><hr width=\"100%\" style=\"color:#00ACFF\">";
	echo "&nbsp;&nbsp;Добавил(а)  ";
	if ($record[2]!='') echo "<a href=\"mailto:".$record[2]."\">";
	echo "<b>".$record[1]."</b>";
	if ($record[2]!='') echo "</a>";
	echo ', '.$record[0]."&nbsp;&nbsp;\n";
	$fban=file($fb);
	for ($j=0;$j<count($fban);$j++) { if ($record[4]==$fban[$j]) echo "(baned)"; break; }
	echo "</td></tr></table></td></tr></table>\n";
}
function save_note($data_str) {
	$result = false;
	$st=$data_str;
    $st = str_replace("\&quot;", "\"", $st);
	$data_str=$st;
	if (!file_exists(GEST_DB)) {
		$fopt = fopen(GEST_DB, 'w');
		chmod(GEST_DB, 0733);
		if ($fopt) {
			$result = fwrite($fopt, $data_str);
			fclose($fopt);
		}
	}
	else {
		$notes = file(GEST_DB);
		for ($i=0; $i<count($notes); $i++) $notes[$i] = trim($notes[$i])."\n";
		$notes[] = $data_str;
		$fopt = fopen(GEST_DB, 'w');
		if ($fopt) {
			$result = fwrite($fopt, implode("", $notes));
			fclose($fopt);
		}
	}
	return $result;
}
function GetAdmin () { 
  global $admin,$formadmin;
  $formadmin=true; ?>
  <form method=post><table align=center><tr><td class=d3><b>Пароль</b></td><td><input class=sb type="password" size=25 name="pwd" style="height:19px"></td><td><input class=sb2 type="submit" value="   Вход   "></td><td></td></tr></table></form>
  <?php exit; 
} 
?>
