#!/usr/local/bin/php -q
<?
  // This script can take an email and post the contents into a forum.  To do
  // this you will have to direct your mail to this script.  This is done
  // differently in different mail servers.  Consult your mail server docs or
  // your systems admin on how to redirect mail to a script.  At some point in
  // that process you will need to know what command to issue when redirecting
  // the mail.  The usage info is below.

  // usage:
  //  phorummail forum=FORUM_ID [path=PATH_TO_PHORUM]

  // FORUM_ID is the id of the forum you want the messages posted to.  You see
  // this when a new forum is created.  You can also find it in the URL for a
  // forum as the value of f (f=X, X would be the forum id).

  // PATH_TO_PHORUM is the just that.  If you have just one Phorum install you
  // can enter this in the variables below and not have to worry about it on
  // the command line later.  Putting a different value on the command line will
  // overwrite what is put in the script itself.

  // The path must be supplied in either the file or on the command line.

  // sample command line:
  // /path/to/script/phorummail forum=1 path=/usr/www/phorum

  // some vars
  $PhorumMail=true; // Do not touch.
  $phorum_path="";  // this the path to the main Phorum dir.  You can send this on the command line as well.
  $admin_email="";  // This should match the default email for Phorum.

  // read args
  $cnt=count($argv);
  for($x=1;$x<$cnt;$x++){
    if(ereg("forum=([0-9]+)", $argv[$x], $regs)){
      $num=$regs[1];
    }
    elseif(ereg("path=(.+)", $argv[$x], $regs)){
      $phorum_path=$regs[1];
    }
  }

  // Get input

  $stdin=file("php://stdin");
//  $stdin=file("/dev/stdin");  use if PHP version is less than 3.0.15
  $message=implode("", $stdin);

  // check for all we need.
  @chdir($phorum_path);
  $badpath=true;
  if(file_exists("common.php")){
    $badpath=false;
    include "common.php";
    $email=$DefaultEmail;
  }
  if($message=="" || empty($num) || $badpath){
    if(empty($num) || $badpath){
      $error ="PhorumMail could not run for the following reason(s):\n\n";
      if(empty($num)) $error.="     The forum id was not specified.\n";
      if($badpath){
        $email=$admin_email;
        $error.="     The path supplied to PhorumMail could not be found.\n";
        $error.="     If not supplied in the file, it must be on the command line.\n";
      }
      $error.="\n";
      $error.="An example of a correct PhorumMail command line is:\n\n";
      $error.="  /usr/local/bin/phorummail forum=5 path=/usr/home/www/phorum\n\n";
      $error.="A copy of the message is included below.\n\n";
      $error.="====================================================================\n\n";
      $error.="$message";
      mail($email, "PhorumMail failure", $error, "From: PhorumMail <$email>\nReturn-Path: Phorummail <$email>");
    }
    exit();
  }

  // read in headers
  $cnt=count($stdin);
  $endheaders=false;
  $x=0;
  while(!$endheaders && $x<$cnt){
    $parts=explode(": ", $stdin[$x]);
    $type=$parts[0];
    unset($parts[0]);
    $value=trim(implode(": ", $parts));
    $eHeaders[$type]=$value;
    unset($stdin[$x]);
    $x++;
    if($stdin[$x]=="\n"){
      $endheaders=true;
    }
  }

  if(!empty($eHeaders["X-Phorum-Version"])){
    exit();
  }

  // read in the body
  $body=str_replace("\n", "\r\n", trim(implode("", $stdin)));
  $body.="\n\n";
  $body.="------------------------------------------\n";
  $body.="Posted to Phorum via PhorumMail\n";

  // Some functions

  function getmonth($month){
    switch(strtolower($month)){
      case 'jan':
        $month="01";
        break;
      case 'feb':
        $month="02";
        break;
      case 'mar':
        $month="03";
        break;
      case 'apr':
        $month="04";
        break;
      case 'may':
        $month="05";
        break;
      case 'jun':
        $month="06";
        break;
      case 'jul':
        $month="07";
        break;
      case 'aug':
        $month="08";
        break;
      case 'sep':
        $month="09";
        break;
      case 'oct':
        $month=10;
        break;
      case 'nov':
        $month=11;
        break;
      case 'dec':
        $month=12;
        break;
    }
    return $month;
  }

  $dateparts=explode(" ", $eHeaders["Date"]);
  $month=getmonth($dateparts[2]);
  if($dateparts[1]<10) $dateparts[1]="0$dateparts[1]";
  $date="$dateparts[3]-$month-$dateparts[1] $dateparts[4]";

  if(@ereg("([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)", $eHeaders["Received"], $regs)){
    $ip=$regs[0];
  }
  else{
    $ip="PhorumMail";
  }

  if(ereg('"([^"]+)" <([^>]+)>', $eHeaders["From"], $regs)){
    $author=$regs[1];
    $email=$regs[2];
  }
  elseif(ereg('([^<]+)<([^>]+)>', $eHeaders["From"], $regs)){
    $author=trim($regs[1]);
    $email=$regs[2];
  }
  elseif(substr($eHeaders["From"],0,1)=="<"){
    $author=substr($eHeaders["From"], 1, -1);
    $email=$author;
  }
  else{
    $author=$eHeaders["From"];
    $email=$eHeaders["From"];
  }

  $startpos=0;
  $endpos=strlen($eHeaders["Subject"]);
  if(substr($eHeaders["Subject"], 0, 1)=="["){
    $startpos=strpos($eHeaders["Subject"], "]")+1;
  }
  if(substr(trim($eHeaders["Subject"]), -1)=="]"){
    $endpos=($endpos-strrpos($eHeaders["Subject"], "["))*-1;
    ereg("\[([0-9]+):([0-9]+):([0-9]+)\]", $eHeaders["Subject"], $regs);
    $f=$regs[1];
    $t=$regs[2];
    $p=$regs[3];
  }
  $subject=trim(substr($eHeaders["Subject"], $startpos, $endpos));

  $a="post";
  $toaddress=$eHeaders["To"];

  require "post.php";
?>