<?php
/*
  mail("jgeiger@udsmr.org", "My Subject", "Test Message",
       "From: anonymous@thinkbling.com\r\n" .
       "Reply-To: anonymous@thinkbling.com\r\n");
*/
  include_once("background.php");
  include_once("htmlfuncs.php");
  include_once(SmartyClass());

  function DBConnect()
  {
    //$Connection = mysql_connect(DBServer(), DBUser(), DBPwd()) OR DIE (mysql_error());
    $Connection = mysql_connect("mysql.ironzog.dreamhosters.com", "mrferrier", "db16777216") OR DIE (mysql_error());
    @mysql_select_db ("thinkbling", $Connection) OR DIE ("Currently Down");
    return $Connection;
  }
  
  function TableInsert($Connection, $Table, $Fields, $Values)
  {   
    if ($Connection)
    {     
      $SQL = "INSERT INTO $Table ($Fields) VALUES ($Values)";
      @mysql_query ($SQL, $Connection);   
    }
  }

  function TableUpdate($Connection, $Table, $SetCommands, $Where)
  {
    if ($Connection)
    {     
      $SQL = "UPDATE $Table SET $SetCommands WHERE $Where";
      @mysql_query ($SQL, $Connection);   
    }
  }
  
  function IP24Count($Connection, $IP)
  {
    $SQL   = "SELECT count(*) C FROM `email` where ip = '$IP' and message_datetime - 0 > now() - 1000000";
    $Query = mysql_query($SQL);
    $Row   = mysql_fetch_assoc($Query);
    return $Row["C"];
  }

  function PRLog($URL)
  {
    //Connect to the DB.
    $Connection = DBConnect();

    //Select * where URL = $URL.
    $SQL   = "select * from pagerank where url = '$URL'";
    $Items = mysql_query($SQL, $Connection);
    if (!$Items)
    {
        die('System Currently Down');
    }
    $ItemRowCount = mysql_num_rows($Items);
    
    //If no entries returned, INSERT. 
      //Else increment the HIT field and update.    
    if ($ItemRowCount < 1)
    {
      TableInsert($Connection, "pagerank", "url, hit", "'$URL', 1");
    }
    else
    {
      $ItemRow = mysql_fetch_assoc($Items);
      $Hit     = $ItemRow["HIT"] + 1;
      TableUpdate($Connection, "pagerank", "hit = $Hit", "url = '$URL'");
    }
  }

  $IP      = $_SERVER['REMOTE_ADDR'];
  $To      = GetParam("To", "");
  $Subject = GetParam("Subject", "");
  $Message = GetParam("Message", "");

  $ErrorCount = 0;

  if ($IP == "" || $IP == "0.0.0.0")
  {
    $Errors[$ErrorCount] = "Could not find your IP address.";
    $ErrorCount ++;
  }
  if (strlen($To) > 200)
  {
    $Errors[$ErrorCount] = "The address you are trying to send to is too long.";
    $ErrorCount ++;
  }
  if (strlen($Subject) > 200)
  {
    $Errors[$ErrorCount] = "The subject is too long.";
    $ErrorCount ++;
  }
  if (strlen($Message) > 5000)
  {
    $Errors[$ErrorCount] = "The message is too long.";
    $ErrorCount ++;
  }
  
  $Connection = DBConnect();

  //Make sure the IP hasn't posted more than 5 times in 24 hours.
  $EmailCount = IP24Count($Connection, $IP);
  if ($EmailCount >= 5)
  {
    $Errors[$ErrorCount] = "Sorry, you have already sent 5 anonymous emails today.<br>If it is important, please send more tomorrow.";
    $ErrorCount ++;
  }  

  //Create template object.
  $ResultsTemplate = new Smarty;
  $ResultsTemplate->compile_dir = FullSmartyCompileDir();  

  $ResultsTemplate->Assign('Errors',  $Errors);
  $ResultsTemplate->Assign('To',      $To);
  $ResultsTemplate->Assign('Subject', $Subject);
  $ResultsTemplate->Assign('Message', $Message);

  //Send the mail if there are no errors.
  if ($ErrorCount == 0 && $Message != "")
  {
    //Note the info in the database.
    TableInsert($Connection,
                "email",
                "IP, MESSAGE_DATETIME, MESSAGE_TO, MESSAGE_SUBJECT, MESSAGE_TEXT",
                "'$IP', '".date("y-m-d H:i:s")."', '$To', '$Subject', '$Message'");
    //SendMail.
    @mail($To, $Subject, $Message,
          "From: anonymous@thinkbling.com\r\n" .
          "Reply-To: anonymous@thinkbling.com\r\n");
    
    $Result  = $ResultsTemplate->fetch('templates'.DirDelimiter().'results.tpl.htm');    
  }
  else
  {
    $Result  = $ResultsTemplate->fetch('templates'.DirDelimiter().'send.tpl.htm');
  }

  include("../header.php");
  echo $Result;
?>