#!/usr/bin/php -q
// GNU GENERAL PUBLIC LICENSE
// Version 3, 29 June 2007
// 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 3 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, see
//
// novacadian - Jan. 6, 2014
//DB access info
// Replace below value with the access name for the Database eg. "admin"
$dbuser = 'MySQL User Name';
// Replace below value with the password for the Database eg. "password"
$dbpass = 'MySQL Password';
// Replace below value with the database name for the Database eg. "smf_DB"
$dbname = 'MySQL Database';
// Replace below value with the location the Database eg. "localhost"
$dbhost = 'MySQL Location';
$from = "";
$subject = "";
$message = "";
$to = "";
$headers = "";
$MaxSizeInKB = 1024;
$email = "";
// Open STDIN
$fp = fopen("php://stdin", "r");
if (!$fp)
{
echo "Nothing in Standard In!";
fclose($fp);
exit();
}
// MaxSizeInKB should be adjusted if a larger size is desired.
if ($MaxSizeInKB == -1)
{
while (!feof($fp))
{
$email .= fread($fp, 1024);
}
}
else
{
while (!feof($fp) && $i_limit < $MaxSizeInKB)
{
$email .= fread($fp, 1024);
$i_limit++;
}
}
// Close STDIN
fclose($fp);
// Break STDIN's input into workable parts.
$lines = explode("\n", $email);
// Flag used to terminate below for loop.
$parseheaders = true;
// Move through the input from STDIN parsing needed data
for ($i=0; $i < count($lines); $i++)
{
if ($parseheaders)
{
$headers .= $lines[$i]."\n";
if (preg_match("/^From: (.*)/", $lines[$i], $values))
{
$from = $values[1];
$words = explode(" ", $from);
for ($x=0; $x < count($words); $x++)
{
$pos = strpos($words[$x], "@");
if ($pos == TRUE)
{
$tmp_words = str_replace ('>', '', str_replace('<', '', $words[$x]));
list($froma, $fromb) = explode('@', $tmp_words);
}
}
}
if (preg_match("/^Subject: (.*)/", $lines[$i], $values))
{
$subject = $values[1];
}
if (preg_match("/^To: (.*)/", $lines[$i], $values))
{
$to = $values[1];
}
}
else
{
$message .= $lines[$i]."\n";
}
if (trim($lines[$i])=="")
{
$parseheaders = false;
}
}
//Below will get Member Name the email is addressed to so as to check against the SFM database.
$to = str_replace ('>', '', str_replace('<', '', $to));
list($toa, $tob) = explode('@', $to);
// Place the email address of sender at the bottom of the PM
$message.="\n\n===========================================================\nThis message was sent from: ".$tmp_words;
//Open the DB
$db = mysql_connect('localhost','dbuser','$dbpass') or die("Database error");
mysql_select_db($dbname, $db);
//Get the MemberID of the receiver of the email.
$query1 = sprintf("select id_member from smf_members where member_name='$toa'");
$result = mysql_query($query1);
$row = mysql_fetch_row($result);
$memberid=$row[0];
//Check for the MemberID of the sender of the email
$query2 = sprintf("select id_member from smf_members where member_name='$froma'");
$result = mysql_query($query2);
$row = mysql_fetch_row($result);
$frommemberid=$row[0];
//If no match is found have it send from Mailman. The Mailman account should be created in SMF. It happened
//to be instance 4 on the developmental site.
if ($frommemberid == FALSE) {$frommemberid=4;}
//Get a random number to place into id_pm_head so that id_pm can be looked up.
$tmp_id_pm_head=rand(1,500000);
//Place info in smf_personal_messages
$query3 = sprintf("insert into smf_personal_messages (id_pm_head,id_member_from,from_name,subject,body) values
($tmp_id_pm_head,$frommemberid,'$froma','$subject','$message')");
//Look up id_pm using the random number as key
$query4 = sprintf("select id_pm from smf_personal_messages where id_pm_head=$tmp_id_pm_head");
$results = mysql_query($query4);
$row = mysql_fetch_row($results);
$id_pm=$row[0];
//Change id_pm_head to match id_pm
$query5 = sprintf("update smf_personal_messages set id_pm_head=$id_pm where id_pm=$id_pm");
$result = mysql_query($query5);
//Place info in smf_pm_recipients
$query6 = sprintf("insert into smf_pm_recipients (id_pm,id_member,is_new,is_read) values ($id_pm,$memberid,1,0)");
$result = mysql_query($query6);
//Close database
mysql_close($db);
// EOF
// If the write to the DB fails it would be a good idea to catch the email in caughtemails.txt to have a
// backup until the script is deemed bullet proof.
//$file = 'caughtemails.txt';
//$content.="\nFrom:".$from."\n";
//$content.="\nTo:".$to."\n";
//$content.="\nSubject:".$subject."\n";
//$content.="\nMessage:".$message."\n";
//$content.="\nTo MemberName:".$toa."\n";
//$content.="To MemberID:".$memberid."\n";
//$content.="\nFrom Name:".$froma."\n";
//$content.="From MemberID:".$frommemberid."\n";
//$content.="Sender Email:".$tmp_words."\n";
//$content.="\n\n==============================================\n\n";
//file_put_contents($file, $content);
?>
- Nova
[Edit: Had DB name hard coded.]
[Edit: This script does no longer meet the criteria of the project as far as the challenge–response that is requited by step 2. That is now being worked on and will be released once complete. - Jan 9, 2014]