<?php
  /*
    ********************************************************************
    *         Название:  SR Guestbook                                  *
    ********************************************************************
    *           Версия:  1.2                                           *
    *        Стоимость:  бесплатный скрипт                             *
    *       Требования:  PHP4 и выше                                   *
    *        Платформа:  любая                                         *
    *             Язык:  русский                                       *
    *            Автор:  Sad Raven (http://www.sad-raven.ru)           *
    ********************************************************************
    *           Создан:  10 июля 2002                                  *
    ********************************************************************
  */
  
error_reporting(0);
include ("config.inc.php");
     
function replace ($string)
        {
        global $exech;
        $string = " ".$string;
       // $string = eregi_replace ('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2" target="_blank">\\2</a>', $string);
        $string = eregi_replace ('([[:space:]()[{}])(http&#58;//.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="\\2" target="_blank">\\2</a>', $string);
        //$string = eregi_replace ("[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*","<a href=\"mailto:\\0\">\\0</a>", $string);
		//$string = eregi_replace ("[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*","<a href=\"mailto:\\0\">\\0</a>", $string);
        $autochange = file ($exech);
        $lines = count ($autochange);
        for($i=0;$i<$lines;$i++)
                {
                list($change1,$change2)=explode("|", $autochange[$i]);
                $string = str_replace("$change1","$change2",$string);
                }
        return trim($string);
        }

function cutty ($string)
        {
        $string = str_replace ("<", "&lt;", $string);
        $string = str_replace (">", "&gt;", $string);
        $string = ereg_replace ('\\\"', "&quot;", $string);
        $string = ereg_replace ("\\\'", "&quot;", $string);
        $string = str_replace (":", "&#58;", $string);
        $string = str_replace ("\r", "", $string);
        $string = str_replace ("\n", "<br>", $string);
        $string = str_replace ("%", "&#37;", $string);
        $string = str_replace ("!", "&#33;", $string);
        $string = str_replace ("^ +", "", $string);
        $string = str_replace (" +$", "", $string);
        $string = str_replace (" +", " ", $string);
        $string = str_replace ("|", "l", $string);
        return ($string);
        }

function mydate($date)
        {
        $min = date ($date);
        $date = getdate ($date);
        $mymon = array ("december","january","february","march","april","may","june","july","august","september","october","november");
//		array ("декабря","января","февраля","марта","апреля","мая","июня","июля","августа","сентября","октября","ноября");
        $m = $date[mon];
        $myday = array( "sunday","monday","tuesday","wednesday","thursday","friday","saturday");
//	array( "в воскресенье","в понедельник","во вторник","в среду","в четверг","в пятницу","в субботу");		
        $d = $date[wday];
        $date = " posted: ".$myday[$d].", $date[mday] ".$mymon[$m]." $date[year], $date[hours]:".date("i",$min);
        return $date;
        }

if (isset($name) and $name != "") { setcookie("cookname",$name,time()+15552000); }
if (isset($mail) and $mail != "") { setcookie("cookmail",$mail,time()+15552000); }
if (isset($url) and $url != "") { $url = ereg_replace("^http://","",$url); setcookie("cookurl",$url,time()+15552000); }
if (isset($city) and $city != "") { setcookie("cookcity",$city,time()+15552000); }

if (!isset($add)):
        if (!isset($cookname)) { $cookname = ""; }
        if (!isset($cookmail)) { $cookmail = ""; }
        if (!isset($cookurl)) { $cookurl = ""; }
        if (!isset($cookcity)) { $cookcity = ""; }
        if (!isset($name) or $name == "") { $name = $cookname; }
        if (!isset($mail) or $mail == "") { $mail = $cookmail; }
        if (!isset($url) or $url == "") { $url = $cookurl; }
        if (!isset($city) or $city == "") { $city = $cookcity; }
endif;

function mess($name,$mess,$mail,$url,$city,$date,$answer)
        {
        global $BORDER, $DARK, $LIGHT;
        echo "<table border=0 align=center cellpadding=2 cellspacing=1 width=598 >";
        echo "<tr bgcolor=$DARK class=p><td align=left>";
//        if($city=="") { echo "<b>$name</b></td></tr>"; }
        //else { 
		if($city){$city =" (".$city.")";}
		if($mail){$name ="<a href=mailto:$mail>".$name."</a>";}
		if($url)
	//	{$url =", Home Page: ".$url;}
		{$url =", Home Page: <a href=http://".$url." target=_blank>$url</a>";}
		echo "<b>$name</b>$city$url</td><td width=240 align=right>".mydate($date)."</td></tr>"; //}
        /*if($mail!=""&&$url!="") { echo "</tr><td width=299 align=center bgcolor=$DARK class=p2>E-mail : $mail</td><td width=299 align=center bgcolor=$DARK class=p2>Home page : $url</td></tr>"; }
        if($url!=""&&$mail=="") { echo "</tr><td width=299 align=center bgcolor=$DARK class=p2>E-mail : no</td><td width=299 align=center bgcolor=$DARK class=p2>Home page : $url</td></tr>"; }
        if($url==""&&$mail!="") { echo "</tr><td width=299 align=center bgcolor=$DARK class=p2>E-mail : $mail</td><td width=299 align=center bgcolor=$DARK class=p2>Home page : no</td></tr>"; }
		*/
       // echo "</td></tr>";
        echo "<tr><td colspan=2 class=p><div align=justify>$mess</div></td></tr>";
        //echo "<tr><td align=right bgcolor=$LIGHT class=p1 colspan=2>".mydate($date)."</td></tr>";
        if(!($answer=="")) {
                echo "<tr><td align=left bgcolor=$DARK class=p colspan=2>";
                echo "<b>Answer :</b> $answer";
                echo "</td></tr>"; }
        echo "</table>";
        echo "<table border=0 cellpadding=0 cellspacing=0 width=598 height=4><tr><td height=4></td></tr></table><p></p>";
        }

if(empty($ip)):
        if (getenv('HTTP_X_FORWARDED_FOR')) { $ip=getenv('HTTP_X_FORWARDED_FOR'); }
        else { $ip=getenv('REMOTE_ADDR');}
endif;

if (isset($add)):
        if ((isset($name) && $name == "") || (isset($mess) && $mess == "")) { $error[] = "Not all required fields are filled."; }
        if (isset($name) && strlen($name) > $maxword) { $error[] = "Name must not exceed $maxmail symbols."; }
        if (isset($mail) && strlen($mail) > $maxmail) { $error[] = "e-mail must not exceed $maxmail symbols."; }
        if (isset($mail) && $mail != "" && !eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$",$mail)) { $error[] = "Таких адресов e-mail не бывает."; }
        if (isset($url) && strlen($url) > $maxurl) { $error[] = "URL address must not exceed $maxurl symbols."; }
        if (isset($city) && strlen($city) > $maxcity) { $error[] = "City name must not exceed $maxcity symbols."; }
        if (isset($mess) && strlen($mess) > $maxlenght) { $error[] = "Message must not exceed $maxlenght symbols."; }
        if (isset($mess) && eregi("[^ ]{".$maxword.",}",$mess)) { $error[] = "Word length must not exceed $maxword symbols."; }
        $data_array = file("$data");
        $cnt = count($data_array) - 1;
        for ($i = $cnt; $i >= 0; $i--)
                {
                list($cname,$cmess,$cmail,$curl,$ccity,$cdate,$canswer) = explode("|", $data_array[$i]);
                $ch_name = cutty($name);
                $ch_mess = cutty($mess);
                $check = "$ch_name|$ch_mess";
                if($check == "$cname|$cmess") { $error[] = "Такое сообщение уже есть в базе данных."; }
                }
endif;

$banip=file("$banlist");
$banlines=count($banip);
for($i=0;$i<$banlines;$i++)
        {
        list($badip,$why,$who)=explode("|", $banip[$i]);
        if($ip==$badip):
                include("$header");
                echo "<table width=598 border=0 cellspacing=0 cellpadding=0 class=p1><tr><td align=center>";
                echo "<table width=598 bgcolor=$BORDER border=0 cellspacing=1 cellpadding=3 class=p><tr><td bgcolor=#FF0000 align=center style=\"color:#FFFFFF\"><b>Доступ запрещен!</b></td></tr>";
                echo "<tr><td bgcolor=$LIGHT align=center>Для Вас гостевая книга закрыта.<br>Причина: $why</td></tr></table>";
                include("$footer");
                exit;
        endif;
        }

include("$header");
echo "<table width=598 border=0 cellspacing=0 cellpadding=0 class=p1><tr><td align=center>";

if ($name != "" && $mess != "" && isset($add) && $error == ""):
        $mail_mess = $mess;
        $mail_name = $name;
        $name = cutty($name);
        $mail = cutty($mail);
        $url = cutty($url);
        $city = cutty($city);
        $mess = cutty($mess);
        $date = time();
        $f = fopen ($data,"a");
        flock ($f,2);
        fputs ($f,"$name|$mess|$mail|$url|$city|$date||$ip\r\n");
        flock ($f,3);
        fclose ($f);
        $mess = "";
elseif (isset($error) &&  $error != ""):
        echo "<table width=\"598\" border=\"0\" cellspacing=\"1\" cellpadding=\"3\" bgcolor=\"$BORDER\">";
        echo "<tr><td align=\"center\" class=\"error\" bgcolor=\"#FF0000\"><b>Error!</b></td></tr>";
        echo "<tr><td align=\"left\" class=\"p\" bgcolor=\"$LIGHT\">";
        foreach ($error as $value)
                {
                echo "<li>$value<br>";
                }
        echo "</td></tr>";
        echo "</table>";
        echo "<table border=0 cellpadding=0 cellspacing=0 width=598 height=4><tr><td height=4></td></tr></table>";
endif;

$file_array = file("$data");
$lines = count($file_array);

if (!isset($page)) { $page = 1; }

function getPages($total, $page, $link, $perpage)
        {
        $mpp = $perpage;
        $prev_page = $page - 1;
        $next_page = $page + 1;
        if ($total <= $mpp) $pages = 1;
        elseif($total % $mpp == 0) $pages = $total / $mpp;
        else $pages = $total / $mpp + 1;
        $pages = (int) $pages;
        $s_pages    = $page<7 ? 1 : floor($page / 7) * 7;
        $e_pages    = $page + 6;
        if ($e_pages > $pages) { $e_pages = $pages; }
        $s = "";
        if ($pages > 6){
        if($prev_page != 0) { $s = "<a href=\"".$link."page=1\"> &lt;&lt; </a>| \n"; }
        if($prev_page) { $s .= "<a href=\"".$link."page=$prev_page\"> &lt; </a>| \n"; }}
        for($i=$s_pages;$i<=$e_pages;$i++)
                {
                if ($i != $page) { $s .= "<a href=\"".$link."page=$i\" class=a2> $i </a>| \n"; }
                elseif ($i != 1) { $s .= " <b> $i |</b> "; }
                elseif ($page != $pages) { $s .= " <b> 1 |</b> "; }
                }
        if($page != $pages && $pages > 6)
                {
                $s .= "<a href=\"".$link."page=$next_page\"> &gt; </a>| \n";
                $s .= "<a href=\"".$link."page=$pages\"> &gt;&gt; </a>| \n";
                }
        if (!isset($s) || $s == "") { $s = " <b> 1 |</b> "; }
        return $s;
        }
$print_pages = getPages($lines, $page, "index.php?", $maxmess);

echo "<table border=0 cellpadding=2 cellspacing=1 width=598><tr><td class=p1 align=center><b>Messages total : $lines</b></td></tr><tr><td class=p1 align=center><b>Pages : |$print_pages</b></td></tr></table>";
echo("<table width=100%><tr><td><left><a href=#1>Leave a message</a></left></td></tr></table><br>");
echo "<table border=0 cellpadding=0 cellspacing=0 width=598 height=4><tr><td height=4></td></tr></table>";

$from = $lines - ($page * $maxmess - 1);
$to = $lines - (($page - 1) * $maxmess);
if($from < 0) { $from = 1; }

for($i = $to - 1; $i >= $from - 1; $i--)
        {
        list($name,$mess,$mail,$url,$city,$date,$answer) = explode("|", $file_array[$i]);
        if(!($name==""&&$mess=="")):
                $name = replace($name);
                $mess = replace($mess);
                $mail = replace($mail);
                $url = replace($url);
                $city = replace($city);
                $answer = replace($answer);
                mess($name,$mess,$mail,$url,$city,$date,$answer);
        endif;
        }
echo("<a name=1></a>");
include("$send_form");

echo "<table bgcolor=$BORDER border=0 cellpadding=2 cellspacing=1 width=598><tr><td bgcolor=$LIGHT class=p1 align=center><b>Pages : |$print_pages</b></td></tr><tr><td bgcolor=$LIGHT class=p1 align=center><b>Messages total : $lines</b></td></tr></table>";
echo "</td></tr></table>";
include("$footer");

if (isset($mail_mess) && isset($mail_mess) && $send_mail == "yes"):
$subject="Сообщение от $mail_name";
$body="Здравствуйте!
$mail_name оставил в Вашей гостевой книге сообщение:
------------------------------------
$mail_mess
------------------------------------
SR Guestbook Mailer
";
$headers = "From: sr_guestbook \n";
mail($mailto, $subject, $body, $headers);
endif;
?>