| version 1.2, 2005/03/25 23:32:42 |
version 1.3, 2005/03/30 12:53:55 |
|
|
| <?
|
<?php |
|
|
|
| // $Platon$
|
// $Platon: mylist/backend/receiver/receiver.php,v 1.2 2005/03/25 23:32:42 nepto Exp $ |
|
|
require_once "validator.php"; |
| require_once "validator.php";
|
require_once "../../mylist.config.php"; |
| require_once "../shared/mlist.config.php";
|
require_once "../shared/emailMessage.php"; |
|
|
|
|
|
|
| class CReceiver {
|
// an object of CReceiver class processes an incomming message |
|
|
// it agregates CValidator class that returns IDs of mailinglist into which was the message sent (all conditions like senders allowed, max_message_size, etc are validated) |
| var $validator;
|
// it modifies the message according to configuration of each accepted mailinglist and generates the list of recipients |
|
|
// the message is archived and indexed for full text search, if the flag "f_archive" is set to 1 in specific mailinglist |
|
|
class CReceiver { |
| function validateEmail($filename)
|
|
| {
|
var $validator; |
| $this->validator = new CValidator();
|
|
| $this->validator->openEmailMessage($filename);
|
// object of the CEmailMessage class |
| $this->validator->parseEmail();
|
// contains modified headers (prefix, signature, etc) |
| $this->validator->getAllNeccessaryEmails();
|
var $modifiedMessage; |
| $this->validator->validateEmail();
|
|
|
|
|
| }
|
|
|
|
// this function decides wheter an incomming email message will be accepted or not |
| function processAccepted()
|
// all conditions (senders allowed, max_message_size) must be ok |
| {
|
function validateEmail($filename) |
|
|
{ |
| print_r($this->validator->arrAccepted);
|
|
| for($i = 0; $i < Count($this->validator->arrAccepted); $i++)
|
$this->validator = new CValidator(); |
| $this->generateRecipients($this->validator->arrAccepted[$i]);
|
$this->validator->openEmailMessage($filename); |
| return $i;
|
$this->validator->parseEmail(); |
| }
|
$this->validator->getAllNeccessaryEmails(); |
|
|
$this->validator->validateEmail(); |
| function removeBrackets($messageID)
|
// the result is in $this->validator->arrAccepted; |
| {
|
|
| if ( eregi("^<(.+)>$", $messageID, $textParts))
|
} |
| return $textParts[1];
|
|
|
|
function processAccepted() |
| }
|
{ |
|
|
echo "akceptovane mailinglisty: \n"; |
| function generateRecipients($id_mlist)
|
print_r($this->validator->arrAccepted); |
| {
|
// $arrAccepted is a array of ids of accepted mailinglists |
| global $db_host, $db_user, $db_password, $db_name;
|
for($i = 0; $i < Count($this->validator->arrAccepted); $i++) |
| $db = MySQL_Connect($db_host, $db_user, $db_password);
|
{ // for each mailinglist we generate queue of recipients |
| MySQL_Select_DB($db_name);
|
$queue_id = $this->processMessage($this->validator->arrAccepted[$i]); |
| echo "som tu v generate reciepient id mlist = $id_mlist\n\n";
|
$this->generateRecipients($this->validator->arrAccepted[$i], $queue_id); |
| $sql = "SELECT mlist_user.address, users.reg_email, ";
|
} |
| $sql .= "mlist_config.f_mail_back_to_sender ";
|
|
| $sql .= "FROM mlist_user, users, mlist_config ";
|
} |
| $sql .= "WHERE mlist_user.id_mlist = $id_mlist";
|
|
| $sql .= " AND ";
|
function processMessage($list_id) |
| $sql .= "users.id = mlist_user.id_user ";
|
{ |
| $sql .= " AND ";
|
$this->modifiedMessage = new CModifiedEmailMessage(); |
| $sql .= "mlist_config.id = $id_mlist";
|
$this->modifyMessage($list_id); |
|
|
$message_queue_id = $this->insertMessageIntoQueue($list_id); |
|
|
|
| $res = MySQL_Query($sql);
|
global $dbh; |
|
|
|
| while ($row = MySQL_Fetch_Array($res))
|
|
| {
|
$sql = "SELECT f_archive FROM mylist_lists WHERE id = $list_id"; |
| if (isset($this->validator->emailMessage->structure->headers["message-id"]) && isset($this->validator->emailMessage->filename))
|
|
| {
|
$f_archive =& $dbh->getOne($sql); |
|
|
|
| if (!(!$row["f_mail_back_to_sender"] && ($this->validator->emailMessage->sender == (isset($row[0]) ? $row[0] : $row[1]) )))
|
if ($f_archive) |
| {
|
{ |
| //echo $this->validator->emailMessage->sender;
|
$this->archiveMessage($list_id); |
|
|
$this->indexMessage($list_id); |
| $sql = "INSERT INTO mlist_mails2send ";
|
} |
| $sql .= "(message_id, filename, id_mlist, recipient, f_sent) ";
|
|
| $sql .= "VALUES (";
|
return $message_queue_id; |
| $sql .= "'".($this->removeBrackets($this->validator->emailMessage->structure->headers["message-id"]))."', ";
|
} |
| $sql .= "'".$this->validator->emailMessage->filename."',";
|
|
| $sql .= $id_mlist.", ";
|
|
| $sql .= "'".((isset($row[0]))? $row[0] : $row[1])."', ";
|
|
| $sql .= "0)";
|
function modifyMessage($_list_id) |
|
|
{ |
| @$ret = MySQL_Query($sql);
|
global $dbh; |
|
|
global $MLIST_CRLF, $errors_to; |
| if(!$ret)
|
|
| if (MySQL_ErrNo() != 1062)
|
$sql = "SELECT name, f_reply_to_mlist, f_prefix_subject, "; |
| {
|
$sql .= "prefix, f_append_sig, signature "; |
| echo "Error: Unable to run query";
|
$sql .= "FROM mylist_lists WHERE id = $_list_id"; |
| exit;
|
|
| }
|
$res =& $dbh->query($sql); |
| }
|
if (PEAR::isError($res)) |
| }
|
die($res->getMessage()); |
| }
|
|
|
|
$res->fetchInto($row, DB_FETCHMODE_ASSOC); |
|
|
// original message is in $this->validator->emailMessage |
| $this->prepareNewMessage($id_mlist);
|
// modified message will be in $this->modifiedMessage |
| }
|
|
|
|
|
|
|
// **************************************************** |
| function backupMessage()
|
// ************* HEADERS MODIFICATIONS **************** |
| {
|
|
|
|
$_header = "From: ".$this->validator->emailMessage->structure->headers["from"].$MLIST_CRLF; |
|
|
$_header .= "X-Mailinglist-From: ".$row["name"]."\n"; |
| global $db_host, $db_user, $db_password, $db_name;
|
$_header .= "To: ".$row["name"]."\n"; |
| $db = MySQL_Connect($db_host, $db_user, $db_password);
|
$_header .= "Errors-To: $errors_to\n"; |
| MySQL_Select_DB($db_name);
|
|
|
|
if ($row["f_reply_to_mlist"]) |
| for($i = 0; $i < Count($this->validator->arrAccepted); $i++)
|
$_header .= "Reply-To: ".$row["name"]; |
| {
|
else |
|
|
$_header .= "Reply-To: ".$this->validator->emailMessage->structure->headers["from"]; |
| $sql = "INSERT INTO mlist_archive ";
|
|
| $sql .= "(message_id, mlist_id, `from`, dt_header, dt_received, header, subject, body) ";
|
// received headers |
| $sql .= " VALUES (";
|
if (is_array($this->validator->emailMessage->structure->headers["received"])) |
| $sql .= "'".($this->removeBrackets($this->validator->emailMessage->structure->headers["message-id"]))."', ";
|
{ |
| $sql .= $this->validator->arrAccepted[$i].", ";
|
Reset($this->validator->emailMessage->structure->headers["received"]); |
| $sql .= "'".AddSlashes($this->validator->emailMessage->structure->headers["from"])."', ";
|
While(Current($this->validator->emailMessage->structure->headers["received"])) |
| $sql .= "NULL, NULL, ";
|
{ |
|
|
|
| // $sql .= "'".$this->validator->emailMessage->structure->headers["date"]."', ";
|
$_header .= $MLIST_CRLF."Received: ".Current($this->validator->emailMessage->structure->headers["received"]); |
| // $sql .= "'".$this->validator->emailMessage->structure->headers["date"]."', ";
|
|
| $sql .= "'".AddSlashes($this->validator->emailMessage->header)."', ";
|
Next($this->validator->emailMessage->structure->headers["received"]); |
| $sql .= "'".AddSlashes($this->validator->emailMessage->structure->headers["subject"])."', ";
|
} |
| $sql .= "'".AddSlashes($this->validator->emailMessage->body)."')";
|
} |
|
|
else if (isset($this->validator->emailMessage->structure->headers["received"])) |
|
|
{ |
|
|
$_header .= $MLIST_CRLF."Received: ".$this->validator->emailMessage->structure->headers["received"]; |
| @$ret = MySQL_Query($sql);
|
} |
|
|
|
| if(!$ret)
|
|
| if (MySQL_ErrNo() != 1062)
|
|
| {
|
// ************* PREXIF SUBJECT *********************** |
| echo "Error: Unable to run query";
|
|
| exit;
|
if ($row["f_prefix_subject"]) |
| }
|
{ |
|
|
$this->modifiedMessage->subject = "[".$row["prefix"]."] ".$this->validator->emailMessage->structure->headers["subject"]; |
| }
|
} |
|
|
else |
| MySQL_Close($db);
|
$this->modifiedMessage->subject = $this->validator->emailMessage->structure->headers["subject"]; |
| }
|
|
|
|
$_header .= $MLIST_CRLF."Subject: ".$this->modifiedMessage->subject; |
|
|
|
| function prepareNewMessage($id_mlist)
|
|
| {
|
$this->modifiedMessage->header = $_header; |
| global $MLIST_CRLF;
|
|
|
|
// **************************************************** |
| global $db_host, $db_user, $db_password, $db_name;
|
// **************************************************** |
| $db = MySQL_Connect($db_host, $db_user, $db_password);
|
|
| MySQL_Select_DB($db_name);
|
|
|
|
|
| $sql = "SELECT Count(recipient) FROM mlist_mails2send WHERE ";
|
|
| $sql .= "id_mlist = $id_mlist AND ";
|
// **************************************************** |
| $sql .= "message_id = ";
|
// ************* BODY MODIFICATIONS ******************* |
| $sql .= "'".($this->removeBrackets($this->validator->emailMessage->structure->headers["message-id"]))."'";
|
|
| $ret = MySQL_Query($sql);
|
if ($row["f_append_sig"]) |
| $row = MySQL_Fetch_Array($ret);
|
{ |
| $count = $row[0];
|
$this->modifiedMessage->body = $this->validator->emailMessage->body."\n_______________________________________________________________________________\n".$row["signature"]; |
|
|
} |
| $sql = "SELECT countRecipients FROM mlist_mailStructure WHERE ";
|
else |
| $sql .= "id_mlist = $id_mlist AND ";
|
$this->modifiedMessage->body = $this->validator->emailMessage->body; |
| $sql .= "message_id = ";
|
|
| $sql .= "'".($this->removeBrackets($this->validator->emailMessage->structure->headers["message-id"]))."'";
|
// **************************************************** |
|
|
// **************************************************** |
| $ret = MySQL_Query($sql);
|
} |
| $row = MySQL_Fetch_Array($ret);
|
|
|
|
|
| if (isset($row[0]) && ($row[0] != $count))
|
function insertMessageIntoQueue($_list_id) |
| {
|
{ |
| $sql = "UPDATE mlist_mailStructure ";
|
global $dbh; |
| $sql .= "SET countRecipients = $count,";
|
|
| $sql .= "f_all_sent = 0 WHERE ";
|
|
| $sql .= "id_mlist = $id_mlist AND ";
|
$sql = "INSERT INTO mylist_queue_messages (message_header, message_body) VALUES "; |
| $sql .= "message_id = ";
|
$sql .= "('".AddSlashes($this->modifiedMessage->header)."', '".AddSlashes($this->modifiedMessage->body)."')"; |
| $sql .= "'".($this->removeBrackets($this->validator->emailMessage->structure->headers["message-id"]))."'";
|
|
| $ret = MySQL_Query($sql);
|
$res =& $dbh->query($sql); |
| }
|
|
| else if (!isset($row[0]))
|
if (PEAR::isError($res)) |
| {
|
die($res->getMessage()); |
|
|
|
| $sql = "SELECT name, f_reply_to_mlist, f_prefix_subject, ";
|
$sql ="SELECT id FROM mylist_queue_messages WHERE message_header LIKE '".AddSlashes($this->modifiedMessage->header)."' AND message_body LIKE '".AddSlashes($this->modifiedMessage->body)."' ORDER BY id DESC"; |
| $sql .= "prefix, f_append_sig, signature, f_mail_back_to_sender ";
|
|
| $sql .= "FROM mlist_config WHERE id = $id_mlist";
|
|
|
|
$res =& $dbh->query($sql); |
| $ret = MySQL_Query($sql);
|
if (PEAR::isError($res)) |
| $row = MySQL_Fetch_Array($ret);
|
die($res->getMessage()); |
|
|
|
| // prefix subject
|
$res->fetchInto($row, DB_FETCHMODE_ASSOC); |
| $_subject = $this->validator->emailMessage->structure->headers["subject"];
|
return $row["id"]; |
| if ($row["f_prefix_subject"])
|
} |
| $_subject = "[".$row["prefix"]."] ".$_subject;
|
|
|
|
function removeBrackets($messageID) |
| // append signature
|
{ |
| $_body = $this->validator->emailMessage->body;
|
if ( eregi("^<(.+)>$", $messageID, $textParts)) |
| if ($row["f_append_sig"])
|
return $textParts[1]; |
| $_body = $_body."\n_______________________________________________________________________________\n".$row["signature"];
|
|
|
|
} |
|
|
|
| // setting up headers
|
function generateRecipients($list_id, $queue_id) |
| $_header = "From: ".$this->validator->emailMessage->structure->headers["from"].$MLIST_CRLF;
|
{ |
| // $_header = $_header."X-Mailinglist-From: ".$row["name"]."\n";
|
global $dbh; |
| $_header = $_header."To: ".$row["name"]."\n";
|
global $tb_users, $cl_users_id, $cl_users_email; |
|
|
|
| if ($row["f_reply_to_mlist"])
|
$sql = "SELECT mylist_list_users.email as list_email, $tb_users.$cl_users_email as reg_email, "; |
| $_header = $_header."Reply-To: ".$row["name"];
|
$sql .= "mylist_lists.f_mail_back_to_sender "; |
| else
|
$sql .= "FROM mylist_list_users, $tb_users, mylist_lists "; |
| $_header = $_header."Reply-To: ".$this->validator->emailMessage->structure->headers["from"];
|
$sql .= "WHERE mylist_list_users.list_id = $list_id"; |
|
|
$sql .= " AND "; |
|
|
$sql .= "$tb_users.$cl_users_id = mylist_list_users.user_id "; |
|
|
$sql .= " AND "; |
| if (is_array($this->validator->emailMessage->structure->headers["received"]))
|
$sql .= "mylist_lists.id = $list_id"; |
| {
|
|
| Reset($this->validator->emailMessage->structure->headers["received"]);
|
|
| While(Current($this->validator->emailMessage->structure->headers["received"]))
|
$res =& $dbh->query($sql); |
| {
|
if (PEAR::isError($res)) |
|
|
die($res->getMessage()); |
| $_header=$_header.$MLIST_CRLF."Received: ".Current($this->validator->emailMessage->structure->headers["received"]);
|
|
|
|
while ($res->fetchInto($row, DB_FETCHMODE_ASSOC)) |
| Next($this->validator->emailMessage->structure->headers["received"]);
|
{ |
| }
|
if (!(!$row["f_mail_back_to_sender"] && ($this->validator->emailMessage->sender == ((StrLen($row["list_email"]) != 0) ? $row["list_email"] : $row["reg_email"]) ))) |
| }
|
{ |
| else if (isset($this->validator->emailMessage->structure->headers["received"]))
|
$sql = "INSERT INTO mylist_queue "; |
| {
|
$sql .= "(queue_message_id, recipient, dt_queued) "; |
| $_header=$_header.$MLIST_CRLF."Received: ".$this->validator->emailMessage->structure->headers["received"];
|
$sql .= "VALUES ("; |
| }
|
$sql .= $queue_id.", "; |
|
|
$sql .= "'".((StrLen($row["list_email"]) != 0)? $row["list_email"] : $row["reg_email"])."', "; |
|
|
$sql .= "'".Date("Y-m-d H:i:s")."')"; |
|
|
|
|
|
|
|
|
$res2 =& $dbh->query($sql); |
| $sql = "INSERT INTO mlist_mailStructure (";
|
if (PEAR::isError($res2)) |
| $sql .= "message_id, id_mlist, body, subject, header, `from`, ";
|
if (StrCmp("DB Error: already exists", $res2->getMessage()) != 0) |
| $sql .= "countRecipients) ";
|
die($res2->getMessage()); |
| $sql .= "VALUES (";
|
else |
| $sql .= "'".($this->removeBrackets($this->validator->emailMessage->structure->headers["message-id"]))."', ";
|
echo "nepridavam - existuje list_id = $list_id, queue_id = $queue_id, email = ".((StrLen($row["list_email"]) != 0)? $row["list_email"] : $row["reg_email"])."\n"; |
| $sql .= "$id_mlist, ";
|
|
| $sql .= "'".AddSlashes($_body)."', ";
|
else |
| $sql .= "'".AddSlashes($_subject)."', ";
|
echo "pridavam list_id = $list_id, queue_id = $queue_id, email = ".((StrLen($row["list_email"]) != 0)? $row["list_email"] : $row["reg_email"])."\n"; |
| $sql .= "'".AddSlashes($_header)."', ";
|
|
| $sql .= "'".AddSlashes($this->validator->emailMessage->structure->headers["from"])."', ";
|
} |
| $sql .= "$count) ";
|
} |
|
|
} |
| $ret = MySQL_Query($sql);
|
|
|
|
|
|
|
|
| }
|
|
|
|
function archiveMessage($_list_id) |
| MySQL_Close($db);
|
{ |
|
|
// header, body, subject was alredy set in $this->modifyMessage(); |
|
|
$this->modifiedMessage->from = $this->validator->emailMessage->sender; |
| }
|
// $this->modifiedMessage->dt_header = $this->validator->emailMessage->structure->headers["date"]; |
|
|
$this->modifiedMessage->dt_received = NULL; |
|
|
$this->modifiedMessage->messageID = $this->validator->emailMessage->structure->headers["message-id"]; |
|
|
// print_r($this->modifiedMessage); |
|
|
// print_r($this->validator->emailMessage); |
|
|
|
|
|
global $dbh; |
|
|
|
|
|
$sql = "INSERT INTO mylist_messages "; |
|
|
$sql .= "(list_id, messageid, dt_header, dt_received, "; |
| function moveFile($accepted)
|
$sql .= "header, subject, body) VALUES "; |
| {
|
// $sql .= "($_list_id, '".$this->modifiedMessage->messageID."', '".AddSlashes($this->modifiedMessage->from)."', '".$this->modifiedMessage->dt_header."', '".$this->modifiedMessage->dt_received."', '".AddSlashes($this->modifiedMessage->header)."', '".AddSlashes($this->modifiedMessage->subject)."', '".AddSlashes($this->modifiedMessage->body)."')"; |
| global $dir_accepted, $dir_rejected;
|
$sql .= "($_list_id, '".$this->modifiedMessage->messageID."', '".$this->modifiedMessage->dt_header."', '".$this->modifiedMessage->dt_received."', '".AddSlashes($this->modifiedMessage->header)."', '".AddSlashes($this->modifiedMessage->subject)."', '".AddSlashes($this->modifiedMessage->body)."')"; |
| //exec("mv ".$this->validator->emailMessage->filename." ".(($accepted > 0) ? $dir_accepted : $dir_rejected));
|
|
| }
|
|
| }
|
$res =& $dbh->query($sql); |
|
|
if (PEAR::isError($res)) |
| ?>
|
if (StrCmp("DB Error: already exists", $res->getMessage()) != 0) |
|
|
die($res->getMessage()); |
|
|
} |
| |
|
| |
function indexMessage($_list_id) |
| |
{ |
| |
|
| |
} |
| |
|
| |
function moveFile($accepted) |
| |
{ |
| |
global $dir_accepted, $dir_rejected; |
| |
//exec("mv ".$this->validator->emailMessage->filename." ".(($accepted > 0) ? $dir_accepted : $dir_rejected)); |
| |
} |
| |
} |
| |
|
| |
?> |
| |
|
| |
|