<?php
/*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
~= YapGB - Yet Another Php Guest Book                              =~
~= v0.5.1                                                          =~
~= Nov 7th, 2004                                                   =~
~= http://www.hewop.com/~yapgb                                     =~
~= yapgb@hewop.com                                                 =~
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
~= AUTHOR:                                                         =~
~=    José Jorge Enríquez Rodríguez                                =~
~=    geo@geosoft.tk (redirected email)                            =~
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
~= YapGB is a free guestbook system written in PHP that does not   =~
~= require a database, entries are stored in a plain text file.    =~
~= Copyright (C) 2003-2004 José Jorge Enríquez Rodríguez           =~
~=                                                                 =~
~= This program is free software; you can redistribute it and/or   =~
~= modify it under the terms of the GNU General Public License     =~
~= as published by the Free Software Foundation; either version 2  =~
~= of the License, or (at your option) any later version.          =~
~=                                                                 =~
~= This program is distributed in the hope that it will be useful, =~
~= but WITHOUT ANY WARRANTY; without even the implied warranty of  =~
~= MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   =~
~= GNU General Public License for more details.                    =~
~=                                                                 =~
~= You should have received a copy of the GNU General Public       =~
~= License along with this program; if not, write to the Free      =~
~= Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, =~
~= MA  02111-1307, USA                                             =~
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
~= CHANGES                                                         =~
~= v0.5:                                                           =~
~= - Added the use of smilies and BBCode functions.                =~
~= v0.4.2:                                                         =~
~= - Improved email and url addresses checking. Now it shows the   =~
~=   appropiate message when the address is not correct.           =~
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
*/

# Disable magic quotes at runtime.
# It is used to avoid the addition of back slashes (\).
set_magic_quotes_runtime(0);

# Include configuration file.
include ("config.php");
# Include functions file.
include("func.php");

# Get action to perform and page number, it is done this way so that
# YapGB works with older versions of PHP (the $HTTP_XX_VARS array).
if ( !empty($_GET) ) {
  if ( isset($_GET["action"]) )
    $action = $_GET["action"];
  if ( isset($_GET["page"]) )
    $page = $_GET["page"];
}
else if ( !empty($HTTP_GET_VARS) ) {
  if ( isset($HTTP_GET_VARS["action"]) )
    $action = $HTTP_GET_VARS["action"];
  if ( isset($HTTP_GET_VARS["page"]) )
    $page = $HTTP_GET_VARS["page"];
}

if ( isset($_POST["action"]) )
  $action = $_POST["action"];
else if ( isset($HTTP_POST_VARS["action"]) )
  $action = $_POST["action"];


# Make sure we have page number.
if ( !isset($page) or intval($page) == 0 or $page == 0 )
  $page = 1;

# Show sign form
if ( $action == "signx") {
  # Get the signing form template
  $signTemplate = readTemplate("temp_sign.html");
  # Swap global GB_TAGS
  $signTemplate = swapGlobalTags($signTemplate);

  # Show sign form
  echo $signTemplate;
}
# Add a new entry
else if ( $action == "addx") {

  # Get entry variables
  if ( !empty($_POST) ) {
    if ( isset($_POST["name"]) )    $name = $_POST["name"];
    if ( isset($_POST["email"]) )   $email = $_POST["email"];
    if ( isset($_POST["url"]) )     $url = $_POST["url"];
    if ( isset($_POST["message"]) ) $message = $_POST["message"];
  }
  else if ( !empty($HTTP_POST_VARS) ) {
    if ( isset($HTTP_POST_VARS["name"]) )  $name = $HTTP_POST_VARS["name"];
    if ( isset($HTTP_POST_VARS["email"]) ) $email = $HTTP_POST_VARS["email"];
    if ( isset($HTTP_POST_VARS["url"]) )   $url = $HTTP_POST_VARS["url"];
    if ( isset($HTTP_POST_VARS["message"]) )
      $message = $HTTP_POST_VARS["message"];
  }

  $date = time();

  # Name and message are required, check whether they are empty.
  if ( !isset($name) or empty($name) )
    die("<b>$msgError:</b> $msgNameRequired.");
  if ( !isset($message) or empty($message) )
    die("<b>$msgError:</b> $msgMessageRequired.");

  # Email checking. First we check whether the email field is
  # not empty, if so, it does not matter if email is not
  # required, we check for a correct email address and if it is
  # not, we show an error message.
  if ( !empty($email) ) {
    if ( !checkEmail($email) ) {
      die("<b>$msgError:</b> $msgEmailIncorrect");
    }
  }
  # Else if email field is empty and it is required, then we
  # let the user know that.
  else if ($gbRequireEmail) {
    die("<b>$msgError:</b> $msgEmailRequired");
  }

  # Url checking.
  # Valid url addresses could be:
  # http://www.hewop.com/~yapgb
  # www.hewop.com/~yapgb (the script will add "http://" at the start)
  # If url field is not empty and it is not equal to just "http://"
  if ( !empty($url) and ($url != "http://") ) {
    # If url is not correct
    if ( !checkUrl($url) ) {
      # Add "http://" string to the start of the url, if it becomes a
      # valid url address, add the string definitely.
      if ( checkUrl("http://" . $url) )
        $url = "http://" . $url;
      # If the url address is not correct, show the appropiate message.
      else
        die("<b>$msgError:</b> $msgUrlIncorrect");
    }
  }
  # If the url field is empty and it is required in config.php, let the
  # user know.
  else if ($gbRequireUrl) {
    die("<b>$msgError:</b> $msgUrlRequired");
  }

  # Format fields. These functions (cleanXxx) need revision.
  $name = cleanField($name);
  $email = cleanField($email);
  $url = cleanField($url);
  $message = cleanMessage($message);

  $newEntry = $name."|".$date."|".$email."|".$url."|".$message."|[end]";

  # Store new entry
  $fp = fopen($gbFile, "a");
  fwrite($fp, $newEntry."\n");
  fclose($fp);

  # If notify of new post enabled.
  if ($gbNewPostNotify) {
    newPostNotify($name, $email, $url, $message);
  }

  # Redirect to main page.
  header("Location: $gbIndex");
  exit();

  # Following code, until die($pageContent), is never executed,
  # it could be used to show a "sign succesful" message, just
  # delete the two lines above (header and exit functions).
  $pageContent = "
    <html>\n
    <head>\n
    <meta http-equiv=\"refresh\" content=\"3; url=$gbIndex\">
    </head>
    <body>
    $msgSignSuccessful<br>
    <a href=\"$gbIndex\">$msgGoToBook</a>.
    </body></html>";

  die($pageContent);
}
# View entries
else {
  # Read entries file into an array.
  $text = file($gbFile);
  # How many entries we have.
  $lines = count($text);

  # First entry to show, according to page number.
  $limitValue = $lines - $page * $gbLimit;

  # The message template
  $entryTemplate = readTemplate("temp_message.html");

  # Execute number of entries per page times.
  for ($i = $limitValue + $gbLimit - 1; ($i >= $limitValue) && ($i >= 0); $i--) {
    list($name, $date, $email, $url, $message) = explode("|", $text[$i]);

    # Censoring functions.
    $name = swapBadWords($name);
    $email = swapBadWords($email);
    $url = swapBadWords($url);
    $message = swapBadWords($message);

    # Cutting long words to avoid bad behaviour.
    # NOTE: These function needs some retouch.
    $name = cutLongWords($name);
    $message = cutLongWords($message);

    # If BBCode is enabled.
    if ($gbEnableBBCode)
      $message = swapBBCode($message);

    # If smilies are enabled.
    if ($gbEnableSmilies)
      $message = swapSmilies($message);

    $entries .= $entryTemplate;

    # If hide email option is on.
    if ($gbHideEmail)
      $email = hideEmail($email);

    # Swap entry GB_TAGS
    $entries = swapEntryTags($entries, $i, $name, $date, $email, $url, $message);
  }

  # Load main page template.
  $bodyContent = readTemplate("temp_body.html");
  # Swap global GB_TAGS
  $bodyContent = swapGlobalTags($bodyContent);
  # Put entries into main page.
  $bodyContent = str_replace("<!--GB_ENTRIES-->", $entries, $bodyContent);
  # Pagination
  $bodyContent = swapPaginTags($bodyContent, $lines, $page);

  # Show main page
  echo $bodyContent;

}
?>
