Page MenuHomeIn-Portal Phabricator

in-portal
No OneTemporary

File Metadata

Created
Fri, Jul 18, 1:17 AM

in-portal

Index: trunk/kernel/include/emailmessage.php
===================================================================
--- trunk/kernel/include/emailmessage.php (revision 3514)
+++ trunk/kernel/include/emailmessage.php (revision 3515)
@@ -1,983 +1,982 @@
<?php
class clsEmailMessage extends clsParsedItem
{
var $Event = "";
var $Item;
var $headers = array();
var $subject = "";
var $body = "";
var $TemplateParsed = FALSE;
var $recipient = NULL;
var $fromuser = NULL;
function clsEmailMessage($MessageId=NULL,$Item=NULL)
{
$this->clsParsedItem();
$this->tablename = GetTablePrefix()."EmailMessage";
$this->Item = $Item;
$this->BasePermission = "EMAIL";
$this->id_field = "EmailMessageId";
$this->TagPrefix = "email";
$this->NoResourceId=1;
if($MessageId)
$this->LoadFromDatabase($MessageId);
}
function LoadEvent($event,$language=NULL)
{
global $objConfig, $objLanguages;
if(!strlen($language))
$language = $objLanguages->GetPrimary();
$sql = "SELECT * FROM ".$this->tablename." WHERE EventId = $event AND LanguageId=$language";
$rs = $this->adodbConnection->Execute($sql);
if($rs && !$rs->EOF)
{
$data = $rs->fields;
$this->SetFromArray($data);
$this->Clean();
return TRUE;
}
else
return FALSE;
}
function LoadFromDatabase($MessageId)
{
global $Errors;
$sql = sprintf("SELECT * FROM ".$this->tablename." WHERE ".$this->IdField()." = '%s'",$MessageId);
$result = $this->adodbConnection->Execute($sql);
if ($result === FALSE)
{
$Errors->AddError("error.DatabaseError",NULL,$this->adodbConnection->ErrorMsg(),"",get_class($this),"LoadFromDatabase");
return FALSE;
}
$data = $result->fields;
$this->SetFromArray($data);
return TRUE;
}
function EditTemplate()
{
}
/* read the template, split into peices */
function ReadTemplate()
{
if(!$this->TemplateParsed)
{
$this->headers = array();
$lines = explode("\n",$this->Get("Template"));
$header_end = FALSE;
$i = 0;
while(!$header_end && $i<count($lines))
{
$h = $lines[$i];
if(strlen(trim($h))==0 || ($h=="."))
{
$header_end = TRUE;
}
else
{
$parts = explode(":",$h,2);
if(strtolower($parts[0])=="subject")
{
$this->subject = $h;
}
else
$this->headers[] = $h;
}
$i++;
}
while($i<count($lines))
{
$this->body .= $lines[$i++];
}
$this->TemplateParsed=TRUE;
}
}
function ParseSection($text, $parser=null)
{
global $objUsers, $objTemplate;
$res = $this->ParseTemplateText($text);
/* parse email class tags */
if(!is_object($this->fromuser))
{
$this->fromuser = $objUsers->GetItem($this->Get("FromUserId"));
$this->fromuser->TagPrefix = "fromuser";
}
/* parse from user object */
if(is_object($this->fromuser))
{
$res = $this->fromuser->ParseTemplateText($res);
}
/* parse recipient user object */
if(is_object($this->recipient))
{
$res = $this->recipient->ParseTemplateText($res);
}
//print_pre($this->Item);
if(is_object($this->Item))
{
$res = $this->Item->ParseTemplateText($res);
}
else
{
if(!is_object($objTemplate))
$objTemplate = new clsTemplateList(" ");
$res = $objTemplate->ParseTemplateText($res);
}
return $res;
}
function SendToGroup($GroupId)
{
global $objUsers;
$users = $objUsers->Query_GroupPortalUser("GroupId=$GroupId");
if(is_array($users))
{
foreach($users as $u)
{
$this->SendToUser($u->Get("PortalUserId"));
}
}
}
function SendToAddress($EmailAddress,$name="")
{
global $objUsers, $objEmailQueue,$objConfig;
$conn = &GetADODBConnection();
//$this->recipient = $objUsers->GetUser($UserId);
//$this->recipient->TagPrefix="touser";
if(strlen($EmailAddress))
{
$to_addr = $EmailAddress;
$this->ReadTemplate();
$subject = $this->ParseSection($this->subject);
$body = $this->ParseSection($this->body);
if(is_object($this->fromuser))
{
$FromAddr = $this->fromuser->Get("Email");
$FromName = trim($this->fromuser->Get("FirstName")." ".$this->fromuser->Get("LastName"));
}
if(!strlen($FromAddr))
{
$FromName = strip_tags( $objConfig->Get('Site_Name') );
$FromAddr = $objConfig->Get("Smtp_AdminMailFrom");
}
$charset = "ascii-us";
if($this->Get("MessageType")=="html")
{
$objEmailQueue->SendMail($FromAddr,$FromName,$to_addr,$To,$subject,"",$body,$charset, $this->Get("Event"),NULL,$this->headers);
}
else
{
$body = nl2br($body);
$body = str_replace("<br />","\n",$body);
$objEmailQueue->SendMail($FromAddr,$FromName,$to_addr,$To,$subject,$body,"",$charset, $this->Get("Event"),NULL,$this->headers);
}
/*$time = adodb_mktime();
$sql = "INSERT INTO ".GetTablePrefix()."EmailLog VALUES ('', '$FromName', '$To', '$subject', $time, '')";
$conn->Execute($sql); */
return TRUE;
}
return FALSE;
}
function SendToUser($UserId)
{
global $objUsers, $objEmailQueue, $objConfig;
$conn = &GetADODBConnection();
//echo "Handling Event ".$this->Get("Event")." for user $UserId <br>\n";
$this->recipient = new clsPortalUser($UserId); // $objUsers->GetItem($UserId);
//echo "<PRE>";print_r($this->recipient); echo "</PRE>";
$this->recipient->TagPrefix="touser";
if($this->recipient->Get("PortalUserId")==$UserId)
{
$to_addr = $this->recipient->Get("Email");
$To = trim($this->recipient->Get("FirstName")." ".$this->recipient->Get("LastName"));
$this->ReadTemplate();
$subject = $this->ParseSection($this->subject, $this->recipient);
$body = $this->ParseSection($this->body);
if(!is_object($this->fromuser))
{
$this->fromuser = $objUsers->GetItem($this->Get("FromUserId"));
}
if(is_object($this->fromuser))
{
$FromAddr = $this->fromuser->Get("Email");
$FromName = trim($this->fromuser->Get("FirstName")." ".$this->fromuser->Get("LastName"));
$charset = "ascii-us";
}
if(!strlen($FromAddr))
{
$FromName = strip_tags( $objConfig->Get('Site_Name') );
$FromAddr = $objConfig->Get("Smtp_AdminMailFrom");
}
// echo $this->Event;
if($this->Get("MessageType")=="html")
{
$objEmailQueue->SendMail($FromAddr,$FromName,$to_addr,$To,$subject,"",$body,$charset, $this->Get("Event"),NULL,$this->headers);
}
else
{
$body = nl2br($body);
$body = str_replace("<br />","\n",$body);
$objEmailQueue->SendMail($FromAddr,$FromName,$to_addr,$To,$subject,$body,"",$charset, $this->Get("Event"),NULL,$this->headers);
}
/*$time = adodb_mktime();
$sql = "INSERT INTO ".GetTablePrefix()."EmailLog VALUES ('', '$FromName', '$To ($to_addr)', '$subject', $time, '')";
$conn->Execute($sql); */
return TRUE;
}
return FALSE;
}
function SendAdmin()
{
global $objUsers, $objConfig, $objEmailQueue;
$conn = &GetADODBConnection();
$this->recipient = $objUsers->GetUser($this->Get("FromUserId"));
$this->recipient->TagPrefix="touser";
if($this->recipient->Get("PortalUserId")==$this->Get("FromUserId") || strlen($this->recipient->Get("PortalUserId")) == 0)
{
$to_addr = $this->recipient->Get("Email");
$To = trim($this->recipient->Get("FirstName")." ".$this->recipient->Get("LastName"));
$this->ReadTemplate();
if (strlen($to_addr) == 0) {
$to_addr = $objConfig->Get("Smtp_AdminMailFrom");
}
$subject = $this->ParseSection($this->subject);
$body = $this->ParseSection($this->body);
$FromName = strip_tags( $objConfig->Get('Site_Name') );
$FromAddr = $objConfig->Get("Smtp_AdminMailFrom");
if(strlen($FromAddr))
{
$charset = "ascii-us";
if($this->Get("MessageType")=="html")
{
$objEmailQueue->SendMail($FromAddr,$FromName,$to_addr,$To,$subject,"",$body,$charset,$this->Get("Event"),NULL,$this->headers);
}
else
{
$body=nl2br($body);
$body = str_replace("<br />","\n",$body);
$objEmailQueue->SendMail($FromAddr,$FromName,$to_addr,$To,$subject,$body,"",$charset, $this->Get("Event"),NULL,$this->headers);
}
/* $time = adodb_mktime();
$sql = "INSERT INTO ".GetTablePrefix()."EmailLog VALUES ('', '$FromName', '$To ($to_addr)', '$subject', $time, '')";
$conn->Execute($sql);
*/
return TRUE;
}
}
return FALSE;
}
function ParseTemplateText($text)
{
$html = $text;
$search = "<inp:";
//$search = "<inp:".$this->TagPrefix;
//$next_tag = strpos($html,"<inp:");
$next_tag = strpos($html,$search);
while($next_tag)
{
$closer = strpos(strtolower($html),">",$next_tag);
$end_tag = strpos($html,"/>",$next_tag);
if($end_tag < $closer || $closer == 0)
{
$tagtext = substr($html,$next_tag,($end_tag - $next_tag)+2);
$pre = substr($html,0,$next_tag);
$post = substr($html,$end_tag+2);
$inner = $this->ParseElement($tagtext);
$html = $pre.$inner.$post;
}
else
{
$OldTagStyle = "</inp>";
## Try to find end of TagName
$TagNameEnd = strpos($html, " ", $next_tag);
## Support Old version
// $closer = strpos(strtolower($html),"</inp>",$next_tag);
if ($TagNameEnd)
{
$Tag = strtolower(substr($html, $next_tag, $TagNameEnd-$next_tag));
$TagName = explode(":", $Tag);
if (strlen($TagName[1]))
$CloserTag = "</inp:".$TagName[1].">";
}
else
{
$CloserTag = $OldTagStyle;
}
$closer = strpos(strtolower($html), $CloserTag, $next_tag);
## Try to find old tag closer
if (!$closer && ($CloserTag != $OldTagStyle))
{
$CloserTag = $OldTagStyle;
$closer = strpos(strtolower($html), $CloserTag, $next_tag);
}
$end_tag = strpos($html,">",$next_tag);
$tagtext = substr($html,$next_tag,($end_tag - $next_tag)+1);
$pre = substr($html,0,$next_tag);
$inner = substr($html,$end_tag+1,$closer-($end_tag+1));
$post = substr($html,$end_tag+1+strlen($inner) + strlen($CloserTag));
//echo "PRE:". htmlentities($pre,ENT_NOQUOTES);
//echo "INNER:". htmlentities($inner,ENT_NOQUOTES);
//echo "POST:". htmlentities($post,ENT_NOQUOTES);
$parsed = $this->ParseElement($tagtext);
if(strlen($parsed))
{
$html = $pre.$this->ParseTemplateText($inner).$post;
}
else
$html = $pre.$post;
}
$next_tag = strpos($html,$search);
}
return $html;
}
function ParseElement($raw, $inner_html ="")
{
$tag = new clsHtmlTag($raw);
$tag->inner_html = $inner_html;
if($tag->parsed)
{
if($tag->name=="include" || $tag->name=="perm_include" || $tag->name=="lang_include")
{
$output = $this->Parser->IncludeTemplate($tag);
}
else
{
if (is_object($this->Item)) {
$this->Item->TagPrefix = $tag->name;
$output = $this->Item->ParseObject($tag);
}
else {
$output = $this->ParseObject($tag);
}
if(substr($output,0,9)=="Undefined")
{
$output = $tag->Execute();
// if(substr($output,0,8)="{Unknown")
// $output = $raw;
} return $output;
}
}
else
return "";
}
}
class clsEmailMessageList extends clsItemCollection
{
function clsEmailMessageList()
{
$this->clsItemCollection();
$this->classname = "clsEmailMessage";
$this->SourceTable = GetTablePrefix()."EmailMessage";
$this->PerPageVar = "Perpage_EmailEvents";
$this->AdminSearchFields = array("Template","Description", "Module","Event");
}
function LoadLanguage($LangId=NULL)
{
global $objLanguages;
if(!$LangId)
$LangId = $objLanguages->GetPrimary();
$sql = "SELECT * FROM ".$this->SourceTable." WHERE LanguageId=$LangId";
$this->Clear();
return $this->Query_Item($sql);
}
function &AddEmailEvent($Template, $Type, $LangId, $EventId)
{
$e = new clsEmailMessage();
$e->tablename = $this->SourceTable;
$e->Set(array("Template","MessageType","LanguageId","EventId"),
array($Template,$Type,$LangId,$EventId));
$e->Dirty();
$e->Create();
return $e;
}
function DeleteLanguage($LangId)
{
$sql = "DELETE FROM ".$this->SourceTable." WHERE LanguageId=$LangId OR LanguageId = 0";
if( $GLOBALS['debuglevel'] ) echo $sql."<br>\n";
$this->adodbConnection->Execute($sql);
}
function &GetMessage($EventId,$LangId,$LoadFromDB=TRUE)
{
$found=FALSE;
if(is_array($this->Items))
{
for($x=0;$x<count($this->Items);$x++)
{
$i =& $this->GetItemRefByIndex($x);
if(is_object($i))
{
if($i->Get("EventId")==$EventId && $i->Get("LanguageId")==$LangId)
{
$found=TRUE;
break;
}
}
}
}
if(!$found)
{
if($LoadFromDB)
{
$n = NULL;
$n = new $this->classname();
$n->tablename = $this->SourceTable;
if($n->LoadEvent($EventId,$LangId))
{
array_push($this->Items, $n);
$i =& $this->Items[count($this->Items)-1];
}
else
$i = FALSE;
}
else
$i = FALSE;
}
return $i;
}
function CreateEmptyEditTable($IdList, $use_parent = false)
{
global $objSession;
if (!$use_parent) {
$edit_table = $objSession->GetEditTable($this->SourceTable);
@$this->adodbConnection->Execute("DROP TABLE IF EXISTS $edit_table");
$query = "SELECT * FROM ".$this->SourceTable." WHERE 0";
$insert = "CREATE TABLE ".$edit_table." ".$query;
if($objSession->HasSystemPermission("DEBUG.LIST"))
echo htmlentities($insert,ENT_NOQUOTES)."<br>\n";
$this->adodbConnection->Execute($insert);
$this->LoadLanguage();
$idvalue = -1;
for($i=0;$i<$this->NumItems();$i++)
{
$e =& $this->Items[$i];
$e->SourceTable = $edit_table;
if(is_array($IdList))
{
foreach($IdList as $id)
{
$e->UnsetIdField();
$e->Set("EmailMessageId",$idvalue--);
$e->Set("LanguageId",$id);
// $e->Set("Description",admin_language("la_desc_emailevent_".$e->Get("Event"),$id));
$e->Create();
}
}
else
{
$e->UnsetIdField();
$e->Set("EmailMessageId",$idvalue--);
$e->Set("LanguageId",$IdList);
// $e->Set("Description",admin_language("la_desc_emailevent_".$e->Get("Event"),$LangId));
$e->Create();
}
}
$this->Clear();
}
else {
parent::CreateEmptyEditTable($IdList);
}
}
function CopyFromEditTable()
{
global $objSession;
$GLOBALS['_CopyFromEditTable']=1;
$idfield = "EmailMessageId";
$edit_table = $objSession->GetEditTable($this->SourceTable);
$sql = "SELECT * FROM $edit_table WHERE LanguageId <> 0";
$rs = $this->adodbConnection->Execute($sql);
while($rs && !$rs->EOF)
{
$data = $rs->fields;
$c = new $this->classname;
$c->SetFromArray($data);
//$c->idfield = $idfield;
if($c->Get($idfield)<1)
{
$old_id = $c->Get($idfield);
$c->Dirty();
$c->UnsetIdField();
$c->Create();
}
else
{
$c->Dirty();
$c->Update();
}
$rs->MoveNext();
}
@$this->adodbConnection->Execute("DROP TABLE IF EXISTS $edit_table");
unset($GLOBALS['_CopyFromEditTable']);
}
function PurgeEditTable()
{
global $objSession;
$edit_table = $objSession->GetEditTable($this->SourceTable);
@$this->adodbConnection->Execute("DROP TABLE IF EXISTS $edit_table");
}
function &GetEmailEventObject($EventName,$Type=0,$LangId=NULL)
{
global $objLanguages;
if(!$LangId)
$LangId = $objLanguages->GetPrimary();
$EmailTable = $this->SourceTable;
$EventTable = GetTablePrefix()."Events";
$sql = "SELECT * FROM $EventTable INNER JOIN $EmailTable ON ($EventTable.EventId = $EmailTable.EventId) ";
$sql .="WHERE Event='$EventName' AND LanguageId=$LangId AND Type=$Type";
$result = $this->adodbConnection->Execute($sql);
if ($result === FALSE)
{
//$Errors->AddError("error.DatabaseError",NULL,$this->adodbConnection->ErrorMsg(),"","clsEvent","GetEmailEventObject");
return FALSE;
}
$data = $result->fields;
$e = new clsEmailMessage();
$e->SetFromArray($data);
$e->Clean();
return $e;
}
function ReadImportTable($TableName,$Overwrite=FALSE, $MaxInserts=100,$Offset=0)
{
$eml = new clsEmailMessageList();
$this->Clear();
$Inserts = 0;
$sql = "SELECT * FROM $TableName LIMIT $Offset,$MaxInserts";
$this->Query_Item($sql);
if($this->NumItems()>0)
{
foreach($this->Items as $i)
{
$e = $eml->GetMessage($i->Get("EventId"),$i->Get("LanguageId"));
if(is_object($e))
{
if($Overwrite)
{
$e->Set("MessageType",$i->Get("MessageType"));
$e->Set("Template",$i->Get("Template"));
$e->Update();
}
}
else
{
$i->Create();
}
$Inserts++;
}
}
$Offset = $Offset + $Inserts;
return $Offset;
}
}
function EventEnabled($e)
{
global $objConfig;
$var = "Email_".$e."_Enabled";
return ($objConfig->Get($var)=="1");
}
class clsEmailQueue
{
var $SourceTable;
var $MessagesAtOnce;
var $MessagesSent=0;
var $LogLevel = 0;
function clsEmailQueue($SourceTable=NULL,$MessagesAtOnce=NULL)
{
global $objConfig;
if($SourceTable)
{
$this->SourceTable=$SourceTable;
}
else
$this->SourceTable = GetTablePrefix()."EmailQueue";
if(!$MessagesAtOnce)
$MessagesAtOnce = (int)$objConfig->Get("Email_MaxSend");
if($MessagesAtOnce<1)
$MessagesAtOnce=1;
$this->MessagesAtOnce = $MessagesAtOnce;
$this->LogLevel = (int)$objConfig->Get("Smtp_LogLevel");
}
function WriteToMailLog($text)
{
global $pathtoroot,$admin;
//echo htmlentities($text)."<br>\n";
if($this->LogLevel>0)
{
$Logfile = $pathtoroot.$admin."/email/log.txt";
if(is_writable($Logfile))
{
$fp = fopen($Logfile,"a");
if($fp)
{
fputs($fp,$text."\n");
fclose($fp);
}
}
}
}
function AllowSockets()
{
$minver = explode(".", "4.3.0");
$curver = explode(".", phpversion());
if (($curver[0] < $minver[0])
|| (($curver[0] == $minver[0])
&& ($curver[1] < $minver[1]))
|| (($curver[0] == $minver[0]) && ($curver[1] == $minver[1])
&& ($curver[2][0] < $minver[2][0])))
return false;
else
return true;
}
- function DeliverMail($To,$From,$Subject,$Msg,$headers, $ForceSend=0)
- {
- global $MessagesSent,$objConfig;
-
- if(($this->MessagesSent >$this->MessagesAtOnce) && !$ForceSend)
- {
- $this->EnqueueMail($To,$From,$Subject,$Msg,$headers);
- return TRUE;
- }
- else
- {
-
- $this->MessagesSent++;
-
- $time = adodb_mktime();
- $conn = &GetADODBConnection();
-
-/* $sql = "INSERT INTO ".GetTablePrefix()."EmailLog VALUES ('', '".htmlspecialchars($From)."', '".htmlspecialchars($To)."', '$Subject', $time, '')";
- $conn->Execute($sql);*/
- /* ensure headers are using \r\n instead of \n */
-
- $headers = "Date: ".adodb_date("r")."\n".$headers;
- $headers = "Return-Path: ".$objConfig->Get("Smtp_AdminMailFrom")."\n".$headers;
-
- $headers = str_replace("\n\n","\r\n\r\n",$headers);
- $headers = str_replace("\r\n","\n",$headers);
- //$headers = str_replace("\n","\r\n",$headers);
-
- // if (strtoupper(substr(PHP_OS, 0, 3) == 'WIN')) {
- $Msg = str_replace("\n\n","\r\n\r\n",$Msg);
- $Msg = str_replace("\r\n","\n",$Msg);
- //$Msg = str_replace("\n","\r\n",$Msg);
- // }
-
- //echo "<PRE>"; print_r(htmlentities($headers)); echo "</PRE>";
- //echo "<PRE>"; print_r(htmlentities($Msg)); echo "</PRE>";
- $ver = phpversion();
- if(substr($Subject,0,9)=="Subject: ")
- $Subject = substr($Subject,9);
- if(!strlen($objConfig->Get("Smtp_Server")) || !$this->AllowSockets())
- {
- return mail($To,trim($Subject),$Msg, $headers);
- }
-
- $headers = "Subject: ".trim($Subject)."\n".$headers;
-
- $send_params['recipients'] = array($To); // The recipients (can be multiple)
- $send_params['from'] = $From; // This is used as in the MAIL FROM: cmd
- $send_params['headers'] = explode("\n",$headers);
- // It should end up as the Return-Path: header
- $send_params['body'] = $Msg; // The body of the email
-
- $params['host'] = $objConfig->Get("Smtp_Server"); // The smtp server host/ip
-
- $params['port'] = 25; // The smtp server port
- $params['hello'] = 'INPORTAL'; // What to use when sending the helo command. Typically, your domain/hostname
- if($objConfig->Get("Smtp_Authenticate")) // Whether to use basic authentication or not
- {
- $params['auth'] = TRUE;
- $params['user'] = $objConfig->Get("Smtp_User");
- $params['pass'] = $objConfig->get("Smtp_Pass");
- }
- else
- $params['auth'] = FALSE;
- $this->LogLevel=0;
- $SmtpServer = new smtp($params);
- if($this->LogLevel>0)
- {
- $SmtpServer->debug=1;
- foreach($params as $key=>$value)
- {
- $this->WriteToMailLog($key."=".$value);
- }
- }
- else
- {
- //$SmtpServer->debug = 1;
- }
- $connected = $SmtpServer->connect();
-
- if($connected)
- {
- if($this->LogLevel>1)
- {
- $this->WriteToMailLog("Connected to ".$params['host']);
- }
- $res = $SmtpServer->send($send_params);
- }
- $SmtpServer->disconnect();
-
- if($this->LogLevel>1)
- {
- foreach($SmtpServer->buffer as $l)
- {
- $this->WriteToMailLog($l);
- }
- }
-
- if($this->LogLevel>0)
- {
- if(count($SmtpServer->errors)>0)
- {
- foreach($SmtpServer->errors as $e)
- {
- $this->WriteToMailLog($e);
- }
- }
- else
- $this->WriteToMailLog("Message to $From Delivered Successfully");
- }
- unset($SmtpServer);
- return $res;
- }
- }
+ function DeliverMail($To,$From,$Subject,$Msg,$headers, $ForceSend=0)
+ {
+ global $MessagesSent,$objConfig;
+
+ if(($this->MessagesSent >$this->MessagesAtOnce) && !$ForceSend)
+ {
+ $this->EnqueueMail($To,$From,$Subject,$Msg,$headers);
+ return TRUE;
+ }
+ else
+ {
+ $this->MessagesSent++;
+
+ $time = adodb_mktime();
+ $conn = &GetADODBConnection();
+
+ /* $sql = "INSERT INTO ".GetTablePrefix()."EmailLog VALUES ('', '".htmlspecialchars($From)."', '".htmlspecialchars($To)."', '$Subject', $time, '')";
+ $conn->Execute($sql);*/
+ /* ensure headers are using \n instead of \n */
+
+ $headers = "Date: ".adodb_date("r")."\n".$headers;
+ $headers = "Return-Path: ".$objConfig->Get("Smtp_AdminMailFrom")."\n".$headers;
+
+ $headers = str_replace("\n\n","\r\n\r\n",$headers);
+ $headers = str_replace("\r\n","\n",$headers);
+ //$headers = str_replace("\n","\r\n",$headers);
+
+ // if (strtoupper(substr(PHP_OS, 0, 3) == 'WIN')) {
+ $Msg = str_replace("\n\n","\r\n\r\n",$Msg);
+ $Msg = str_replace("\r\n","\n",$Msg);
+ //$Msg = str_replace("\n","\r\n",$Msg);
+ // }
+
+ //echo "<PRE>"; print_r(htmlentities($headers)); echo "</PRE>";
+ //echo "<PRE>"; print_r(htmlentities($Msg)); echo "</PRE>";
+ $ver = phpversion();
+ if(substr($Subject,0,9)=="Subject: ")
+ $Subject = substr($Subject,9);
+ if(!strlen($objConfig->Get("Smtp_Server")) || !$this->AllowSockets())
+ {
+ return mail($To,trim($Subject),$Msg, $headers);
+ }
+
+ $headers = "Subject: ".trim($Subject)."\n".$headers;
+
+ $send_params['recipients'] = array($To); // The recipients (can be multiple)
+ $send_params['from'] = $From; // This is used as in the MAIL FROM: cmd
+ $send_params['headers'] = explode("\n",$headers);
+ // It should end up as the Return-Path: header
+ $send_params['body'] = $Msg; // The body of the email
+
+ $params['host'] = $objConfig->Get("Smtp_Server"); // The smtp server host/ip
+
+ $params['port'] = 25; // The smtp server port
+ $params['hello'] = 'INPORTAL'; // What to use when sending the helo command. Typically, your domain/hostname
+ if($objConfig->Get("Smtp_Authenticate")) // Whether to use basic authentication or not
+ {
+ $params['auth'] = TRUE;
+ $params['user'] = $objConfig->Get("Smtp_User");
+ $params['pass'] = $objConfig->get("Smtp_Pass");
+ }
+ else
+ $params['auth'] = FALSE;
+ $this->LogLevel=0;
+ $SmtpServer = new smtp($params);
+ if($this->LogLevel>0)
+ {
+ $SmtpServer->debug=1;
+ foreach($params as $key=>$value)
+ {
+ $this->WriteToMailLog($key."=".$value);
+ }
+ }
+ else
+ {
+ //$SmtpServer->debug = 1;
+ }
+ $connected = $SmtpServer->connect();
+
+ if($connected)
+ {
+ if($this->LogLevel>1)
+ {
+ $this->WriteToMailLog("Connected to ".$params['host']);
+ }
+ $res = $SmtpServer->send($send_params);
+ }
+ $SmtpServer->disconnect();
+
+ if($this->LogLevel>1)
+ {
+ foreach($SmtpServer->buffer as $l)
+ {
+ $this->WriteToMailLog($l);
+ }
+ }
+
+ if($this->LogLevel>0)
+ {
+ if(count($SmtpServer->errors)>0)
+ {
+ foreach($SmtpServer->errors as $e)
+ {
+ $this->WriteToMailLog($e);
+ }
+ }
+ else
+ $this->WriteToMailLog("Message to $From Delivered Successfully");
+ }
+ unset($SmtpServer);
+ return $res;
+ }
+ }
function EnqueueMail($To,$From,$Subject,$Msg,$headers)
{
global $objSession;
$ado = &GetADODBConnection();
$To = mysql_escape_string($To);
$From = mysql_escape_string($From);
$Msg = mysql_escape_string($Msg);
$headers = mysql_escape_string($headers);
$Subject = mysql_escape_string($Subject);
$sql = "INSERT INTO ".$this->SourceTable." (toaddr,fromaddr,subject,message,headers) VALUES ('$To','$From','$Subject','$Msg','$headers')";
$ado->Execute($sql);
}
function SendMailQeue()
{
global $objConfig, $objSession, $TotalMessagesSent;
$ado = &GetADODBConnection();
$MaxAllowed = $this->MessagesAtOnce;
$del_sql = array();
$NumToSend = $MaxAllowed - $this->MessagesSent;
if($NumToSend < 0) $NumToSend=1; // Don't really know why, but this could happend, so issued this temp fix
$sql = "SELECT * FROM ".$this->SourceTable." ORDER BY queued ASC LIMIT $NumToSend";
$rs = $ado->Execute($sql);
while($rs && !$rs->EOF)
{
$data = $rs->fields;
$this->DeliverMail($data["toaddr"],$data["fromaddr"],$data["Subject"],$data["headers"],$data["message"],1);
$del_sql[] = "DELETE FROM ".$this->SourceTable." WHERE queued='".$data["queued"]."'";
$rs->MoveNext();
}
$numdel = count($del_sql);
for($i=0;$i<$numdel;$i++)
{
$sql = $del_sql[$i];
if(strlen($sql))
$ado->Execute($sql);
if($objSession->HasSystemPermission("DEBUG.ITEM"))
echo htmlentities($sql,ENT_NOQUOTES)."<br>\n";
}
}
function SendMail($From, $FromName, $ToAddr, $ToName, $Subject, $Text, $Html, $charset, $SendEvent,
$FileName="",$FileLoc="",$QueueOnly=0,$extra_headers = array())
{
$HasFile = FALSE;
$HasFile = (strlen($FileName)>0);
- $OB="----=_OuterBoundary_000";
+ $OB="----=_OuterBoundary_000".md5( uniqid (rand()));
$boundary = "-----=".md5( uniqid (rand()));
$f = "\"$FromName\" <".$From.">";
- $headers = "From: $f\r\n";
- $headers .= "MIME-Version: 1.0\r\n";
+ $headers = "From: $f\n";
+ if(strlen($ToName)>0)
+ {
+ $To = "\"$ToName\" <$ToAddr>";
+ }
+ else {
+ $To = "<".$ToAddr.">";
+ }
+
+ $headers.="To: $To\n";
+ $headers .= "MIME-Version: 1.0\n";
$conn = &GetADODBConnection();
$time = adodb_mktime();
$sendTo = $ToName;
if (strlen($sendTo) > 0) {
$sendTo .= "($ToAddr)";
}
else {
$sendTo = $ToAddr;
}
$sendTo=addslashes($sendTo);
$sql = "INSERT INTO ".GetTablePrefix()."EmailLog VALUES ('', ".$conn->qstr($FromName).", ".$conn->qstr($sendTo).", ".$conn->qstr( str_replace("Subject:", "", $Subject) ).", $time, '$SendEvent')";
$conn->Execute($sql);
+ $msg .="This is a multi-part message in MIME format.\n\n";
if($HasFile)
{
//Messages start with text/html alternatives in OB
- $headers.="Content-Type: multipart/mixed;\r\n\tboundary=\"".$OB."\"\r\n\r\n";
+ $headers.="Content-Type: multipart/mixed;\n\tboundary=\"".$OB."\"\n\n";
$msg.="--".$OB."\n";
- $msg.="Content-Type: multipart/alternative; boundary=\"$boundary\"\r\n\r\n";
+ $msg.="Content-Type: multipart/alternative; boundary=\"$boundary\"\n\n\n";
+ }
+ else {
+ $headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"\n";
}
- else
- $headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"";
if(is_array($extra_headers))
{
for($i=0;$i<count($extra_headers);$i++);
{
- $headers .= $extra_headers[$i]."\r\n";
+ $headers .= $extra_headers[$i]."\n";
}
}
- $msg .="This is a multi-part message in MIME format.\r\n\r\n";
-
if(!$Text)
{
$Text=strip_tags($Html);
}
$msg .= "--" . $boundary . "\n";
- $msg .= "Content-Type: text/plain; charset=\"$charset\"\r\n";
- $msg .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
+ $msg .= "Content-Type: text/plain; charset=\"$charset\"\n";
+ $msg .= "Content-Transfer-Encoding: 8bit\n\n";
$msg .= stripslashes($Text);
- $msg .= "\r\n\r\n";
+ $msg .= "\n\n";
if(strlen($Html)>0)
{
$msg .= "--" . $boundary . "\n";
$msg .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
- $msg .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
+ $msg .= "Content-Transfer-Encoding: 8bit\n\n";
$msg .= stripslashes($Html);
- $msg .= "\r\n\r\n";
+ $msg .= "\n\n";
}
- $msg .= "--" . $boundary . "--\n\r";
+ $msg .= "--" . $boundary . "--\n";
if($HasFile)
{
if(!strlen($FileLoc)) {
$FileLoc = $FileName;
}
$FileName = basename($FileName);
$msg .= "\n--".$OB."\n";
- $msg.="Content-Type: application/octetstream;\n\tname=\"".$FileName."\"\r\n";
+ $msg.="Content-Type: application/octetstream;\n\tname=\"".$FileName."\"\n";
$msg.="Content-Transfer-Encoding: base64\n";
- $msg.="Content-Disposition: attachment;\n\tfilename=\"".$FileName."\"\r\n\r\n";
+ $msg.="Content-Disposition: attachment;\n\tfilename=\"".$FileName."\"\n\n";
//file goes here
$fd=fopen ($FileLoc, "r");
if($fd)
{
$FileContent=fread($fd,filesize($FileLoc));
fclose ($fd);
}
$FileContent=chunk_split(base64_encode($FileContent));
- $msg .= $FileContent;
- $msg .= $OB."--\r\n";
- }
-
- if(strlen($ToName)>0)
- {
- $To = "\"$ToName\" <$ToAddr>";
- }
- else {
- $To = "<".$ToAddr.">";
+ $msg .= $FileContent."\n";
+ $msg .= "--".$OB."--\n";
}
- //$headers.="To: $To\r\n";
if($this->MessagesSent>$this->MessagesAtOnce || $QueueOnly==1)
{
$this->EnqueueMail($ToAddr,$From,$Subject,$msg,$headers);
}
else
{
$this->DeliverMail($ToAddr,$From,$Subject,$msg,$headers);
}
}
}
?>
Property changes on: trunk/kernel/include/emailmessage.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.49
\ No newline at end of property
+1.50
\ No newline at end of property
Index: trunk/kernel/include/smtp.php
===================================================================
--- trunk/kernel/include/smtp.php (revision 3514)
+++ trunk/kernel/include/smtp.php (revision 3515)
@@ -1,818 +1,818 @@
<?php
/***************************************
** Filename.......: class.smtp.inc
** Project........: SMTP Class
** Version........: 1.00b
** Last Modified..: 30 September 2001
***************************************/
define('SMTP_STATUS_NOT_CONNECTED', 1, TRUE);
define('SMTP_STATUS_CONNECTED', 2, TRUE);
- $CRLF = "\r\n";
+ $CRLF = "\n";
class smtp{
var $connection;
var $recipients;
var $headers;
var $timeout;
var $errors;
var $status;
var $body;
var $from;
var $host;
var $port;
var $helo;
var $auth;
var $user;
var $pass;
var $debug;
var $buffer;
-
+
/**
* List of supported authentication methods, in preferential order.
* @var array
* @access public
*/
var $auth_methods = array('DIGEST-MD5','CRAM-MD5','LOGIN','PLAIN');
/**
* The most recent server response code.
* @var int
* @access private
*/
var $_code = -1;
-
+
/**
* The most recent server response arguments.
* @var array
* @access private
*/
var $_arguments = array();
/**
* Stores detected features of the SMTP server.
* @var array
* @access private
*/
var $_esmtp = array();
-
+
/***************************************
** Constructor function. Arguments:
** $params - An assoc array of parameters:
**
** host - The hostname of the smtp server Default: localhost
** port - The port the smtp server runs on Default: 25
** helo - What to send as the HELO command Default: localhost
** (typically the hostname of the
** machine this script runs on)
** auth - Whether to use basic authentication Default: FALSE
** user - Username for authentication Default: <blank>
** pass - Password for authentication Default: <blank>
** timeout - The timeout in seconds for the call Default: 5
** to fsockopen()
***************************************/
function smtp($params = array()){
-
+
$this->timeout = 5;
$this->status = SMTP_STATUS_NOT_CONNECTED;
$this->host = 'localhost';
$this->port = 25;
$this->helo = 'localhost';
$this->auth = FALSE;
$this->user = '';
$this->pass = '';
$this->errors = array();
$this->buffer = array();
$this->debug=0;
foreach($params as $key => $value){
$this->$key = $value;
}
}
/***************************************
** Connect function. This will, when called
- ** statically, create a new smtp object,
+ ** statically, create a new smtp object,
** call the connect function (ie this function)
** and return it. When not called statically,
** it will connect to the server and send
** the HELO command.
***************************************/
function connect($params = array()){
if(!isset($this->status))
{
$obj = new smtp($params);
if($obj->connect()){
$obj->status = SMTP_STATUS_CONNECTED;
}
return $obj;
}
else
{
$this->connection = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
if(is_resource($this->connection))
{
socket_set_timeout($this->connection, 0, 250000);
socket_set_blocking($this->connection,TRUE);
$greeting = $this->get_data();
$this->status = SMTP_STATUS_CONNECTED;
return $this->auth ? $this->ehlo() : $this->helo();
}
else
{
$this->errors[] = 'Failed to connect to server: '.$errstr;
return FALSE;
}
}
}
function disconnect()
{
if(is_resource($this->connection))
fclose($this->connection);
unset($this->connection);
$this->status=SMTP_STATUS_NOT_CONNECTED;
}
/***************************************
** Function which handles sending the mail.
** Arguments:
** $params - Optional assoc array of parameters.
** Can contain:
** recipients - Indexed array of recipients
** from - The from address. (used in MAIL FROM:),
** this will be the return path
** headers - Indexed array of headers, one header per array entry
** body - The body of the email
** It can also contain any of the parameters from the connect()
** function
***************************************/
function send($params = array()){
global $CRLF;
foreach($params as $key => $value){
$this->set($key, $value);
}
if($this->is_connected()){
// Do we auth or not? Note the distinction between the auth variable and auth() function
if($this->auth){
if(!$this->auth())
return FALSE;
}
$this->mail($this->from);
if(is_array($this->recipients))
foreach($this->recipients as $value)
$this->rcpt($value);
else
$this->rcpt($this->recipients);
if(!$this->data())
return FALSE;
-
+
// Transparency
$headers = str_replace($CRLF.'.', $CRLF.'..', trim(implode($CRLF, $this->headers)));
$body = str_replace($CRLF.'.', $CRLF.'..', $this->body);
$body = $body[0] == '.' ? '.'.$body : $body;
-
+
$this->send_data($headers);
$this->send_data('');
$this->send_data($body);
$this->send_data($CRLF.".");
return (substr(trim($this->get_data()), 0, 3) === '250');
}else{
$this->errors[] = 'Not connected!';
return FALSE;
}
}
-
+
/***************************************
** Function to implement HELO cmd
***************************************/
function helo(){
if(is_resource($this->connection)
AND $this->send_data('HELO '.$this->helo)
AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){
return TRUE;
}else{
$this->errors[] = 'HELO command failed, output: ' . trim(substr(trim($error),3));
return FALSE;
}
}
-
+
/***************************************
** Function to implement EHLO cmd
***************************************/
function ehlo()
{
$ret_status=is_resource($this->connection) AND $this->send_data('EHLO '.$this->helo);
$success=$this->_parseResponse(250);
if(!$ret_status && $success !== true)
{
$this->errors[] = 'EHLO command failed, output: ' . trim(substr(trim($error),3));
return FALSE;
}
foreach ($this->_arguments as $argument) {
$verb = strtok($argument, ' ');
$arguments = substr($argument, strlen($verb) + 1,
strlen($argument) - strlen($verb) - 1);
$this->_esmtp[$verb] = $arguments;
}
-
+
return TRUE;
-
+
}
-
+
/***************************************
** Function to implement AUTH cmd
***************************************/
-
+
function _getBestAuthMethod()
{
$available_methods = explode(' ', $this->_esmtp['AUTH']);
foreach ($this->auth_methods as $method)
{
if (in_array($method, $available_methods)) return $method;
}
return false;
}
-
-
+
+
function auth(){
if(is_resource($this->connection))
{
$method=$this->_getBestAuthMethod();
-
+
switch ($method) {
case 'DIGEST-MD5':
$result = $this->_authDigest_MD5($this->user, $this->pass);
break;
case 'CRAM-MD5':
$result = $this->_authCRAM_MD5($this->user, $this->pass);
break;
case 'LOGIN':
$result = $this->_authLogin($this->user, $this->pass);
break;
case 'PLAIN':
$result = $this->_authPlain($this->user, $this->pass);
break;
default:
$this->errors[] = 'AUTH command failed: no supported authentication methods';
return false;
break;
}
if($result!==true)
{
$this->errors[] = 'AUTH command failed: '.$result;
return FALSE;
}
return true;
}else{
$this->errors[] = 'AUTH command failed: ' . trim(substr(trim($error),3));
return FALSE;
}
}
// ============= AUTH METHODS: BEGIN ==========================
/**
* Authenticates the user using the DIGEST-MD5 method.
*
* @param string The userid to authenticate as.
* @param string The password to authenticate with.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access private
* @since 1.1.0
*/
function _authDigest_MD5($uid, $pwd)
{
$this->send_data('AUTH DIGEST-MD5');
-
+
/* 334: Continue authentication request */
if(($error=$this->_parseResponse(334)) !== true)
{
/* 503: Error: already authenticated */
if ($this->_code === 503) {
return true;
}
return $error;
}
$challenge = base64_decode($this->_arguments[0]);
$auth_str = base64_encode($this->get_digestMD5Auth($uid, $pwd, $challenge,
$this->host, "smtp"));
-
+
$this->send_data($auth_str);
-
+
/* 334: Continue authentication request */
if(($error=$this->_parseResponse(334)) !== true) return $error;
-
+
/*
* We don't use the protocol's third step because SMTP doesn't allow
* subsequent authentication, so we just silently ignore it.
*/
$this->send_data(' ');
-
+
/* 235: Authentication successful */
if(($error=$this->_parseResponse(235)) !== true) return $error;
}
-
-
+
+
/**
* Provides the (main) client response for DIGEST-MD5
* requires a few extra parameters than the other
* mechanisms, which are unavoidable.
- *
+ *
* @param string $authcid Authentication id (username)
* @param string $pass Password
* @param string $challenge The digest challenge sent by the server
* @param string $hostname The hostname of the machine you're connecting to
* @param string $service The servicename (eg. imap, pop, acap etc)
* @param string $authzid Authorization id (username to proxy as)
* @return string The digest response (NOT base64 encoded)
* @access public
*/
function get_digestMD5Auth($authcid, $pass, $challenge, $hostname, $service, $authzid = '')
{
$challenge = $this->_parseChallenge($challenge);
$authzid_string = '';
if ($authzid != '') {
- $authzid_string = ',authzid="' . $authzid . '"';
+ $authzid_string = ',authzid="' . $authzid . '"';
}
if (!empty($challenge)) {
$cnonce = $this->_getCnonce();
$digest_uri = sprintf('%s/%s', $service, $hostname);
$response_value = $this->_getResponseValue($authcid, $pass, $challenge['realm'], $challenge['nonce'], $cnonce, $digest_uri, $authzid);
return sprintf('username="%s",realm="%s"' . $authzid_string . ',nonce="%s",cnonce="%s",nc="00000001",qop=auth,digest-uri="%s",response=%s,%d', $authcid, $challenge['realm'], $challenge['nonce'], $cnonce, $digest_uri, $response_value, $challenge['maxbuf']);
} else {
return PEAR::raiseError('Invalid digest challenge');
}
}
-
+
/**
* Parses and verifies the digest challenge*
*
* @param string $challenge The digest challenge
* @return array The parsed challenge as an assoc
* array in the form "directive => value".
* @access private
*/
function _parseChallenge($challenge)
{
$tokens = array();
while (preg_match('/^([a-z-]+)=("[^"]+(?<!\\\)"|[^,]+)/i', $challenge, $matches)) {
// Ignore these as per rfc2831
if ($matches[1] == 'opaque' OR $matches[1] == 'domain') {
$challenge = substr($challenge, strlen($matches[0]) + 1);
continue;
}
// Allowed multiple "realm" and "auth-param"
if (!empty($tokens[$matches[1]]) AND ($matches[1] == 'realm' OR $matches[1] == 'auth-param')) {
if (is_array($tokens[$matches[1]])) {
$tokens[$matches[1]][] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]);
} else {
$tokens[$matches[1]] = array($tokens[$matches[1]], preg_replace('/^"(.*)"$/', '\\1', $matches[2]));
}
// Any other multiple instance = failure
} elseif (!empty($tokens[$matches[1]])) {
$tokens = array();
break;
} else {
$tokens[$matches[1]] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]);
}
// Remove the just parsed directive from the challenge
$challenge = substr($challenge, strlen($matches[0]) + 1);
}
/**
* Defaults and required directives
*/
// Realm
if (empty($tokens['realm'])) {
$uname = posix_uname();
$tokens['realm'] = $uname['nodename'];
}
-
+
// Maxbuf
if (empty($tokens['maxbuf'])) {
$tokens['maxbuf'] = 65536;
}
-
+
// Required: nonce, algorithm
if (empty($tokens['nonce']) OR empty($tokens['algorithm'])) {
return array();
}
-
+
return $tokens;
}
/**
* Creates the response= part of the digest response
*
* @param string $authcid Authentication id (username)
* @param string $pass Password
* @param string $realm Realm as provided by the server
* @param string $nonce Nonce as provided by the server
* @param string $cnonce Client nonce
* @param string $digest_uri The digest-uri= value part of the response
* @param string $authzid Authorization id
* @return string The response= part of the digest response
* @access private
- */
+ */
function _getResponseValue($authcid, $pass, $realm, $nonce, $cnonce, $digest_uri, $authzid = '')
{
if ($authzid == '') {
$A1 = sprintf('%s:%s:%s', pack('H32', md5(sprintf('%s:%s:%s', $authcid, $realm, $pass))), $nonce, $cnonce);
} else {
$A1 = sprintf('%s:%s:%s:%s', pack('H32', md5(sprintf('%s:%s:%s', $authcid, $realm, $pass))), $nonce, $cnonce, $authzid);
}
$A2 = 'AUTHENTICATE:' . $digest_uri;
return md5(sprintf('%s:%s:00000001:%s:auth:%s', md5($A1), $nonce, $cnonce, md5($A2)));
}
/**
* Creates the client nonce for the response
*
* @return string The cnonce value
* @access private
*/
function _getCnonce()
{
if (file_exists('/dev/urandom')) {
return base64_encode(fread(fopen('/dev/urandom', 'r'), 32));
} elseif (file_exists('/dev/random')) {
return base64_encode(fread(fopen('/dev/random', 'r'), 32));
} else {
$str = '';
mt_srand((double)microtime()*10000000);
for ($i=0; $i<32; $i++) {
$str .= chr(mt_rand(0, 255));
}
-
+
return base64_encode($str);
}
}
-
-
-
-
-
-
+
+
+
+
+
+
/**
* Authenticates the user using the CRAM-MD5 method.
*
* @param string The userid to authenticate as.
* @param string The password to authenticate with.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access private
* @since 1.1.0
*/
function _authCRAM_MD5($uid, $pwd)
{
$this->send_data('AUTH CRAM-MD5');
-
+
/* 334: Continue authentication request */
if(($error=$this->_parseResponse(334)) !== true)
{
/* 503: Error: already authenticated */
if ($this->_code === 503) {
return true;
}
return $error;
}
$challenge = base64_decode($this->_arguments[0]);
$auth_str = base64_encode($uid . ' ' . $this->_HMAC_MD5($pwd, $challenge));
-
+
$this->send_data($auth_str);
/* 235: Authentication successful */
if ( ($error = $this->_parseResponse(235)) ) {
return $error;
}
}
/**
* Authenticates the user using the LOGIN method.
*
* @param string The userid to authenticate as.
* @param string The password to authenticate with.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access private
* @since 1.1.0
*/
function _authLogin($uid, $pwd)
{
$this->send_data('AUTH LOGIN');
-
+
/* 334: Continue authentication request */
if(($error=$this->_parseResponse(334)) !== true)
{
/* 503: Error: already authenticated */
if ($this->_code === 503) {
return true;
}
return $error;
}
-
+
$this->send_data( base64_encode($uid) );
-
+
/* 334: Continue authentication request */
if(($error=$this->_parseResponse(334)) !== true) return $error;
-
+
$this->send_data( base64_encode($pwd) );
/* 235: Authentication successful */
if (($error=$this->_parseResponse(235)) !== true) return $error;
return true;
}
/**
* Authenticates the user using the PLAIN method.
*
* @param string The userid to authenticate as.
* @param string The password to authenticate with.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access private
* @since 1.1.0
*/
function _authPlain($uid, $pwd)
{
$this->send_data('AUTH PLAIN');
-
+
/* 334: Continue authentication request */
if(($error=$this->_parseResponse(334)) !== true)
{
/* 503: Error: already authenticated */
if ($this->_code === 503) {
return true;
}
return $error;
}
$auth_str = base64_encode(chr(0) . $uid . chr(0) . $pwd);
$this->send_data($auth_str);
/* 235: Authentication successful */
if (($error=$this->_parseResponse(235)) !== true) return $error;
return true;
}
// ============= AUTH METHODS: END ==========================
/**
* Function which implements HMAC MD5 digest
*
* @param string $key The secret key
* @param string $data The data to protect
* @return string The HMAC MD5 digest
*/
function _HMAC_MD5($key, $data)
{
if (strlen($key) > 64) {
$key = pack('H32', md5($key));
}
-
+
if (strlen($key) < 64) {
$key = str_pad($key, 64, chr(0));
}
-
+
$k_ipad = substr($key, 0, 64) ^ str_repeat(chr(0x36), 64);
$k_opad = substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64);
-
+
$inner = pack('H32', md5($k_ipad . $data));
$digest = md5($k_opad . $inner);
-
+
return $digest;
}
-
+
/**
* Read a reply from the SMTP server. The reply consists of a response
* code and a response message.
*
* @param mixed $valid The set of valid response codes. These
* may be specified as an array of integer
* values or as a single integer value.
*
* @return mixed True if the server returned a valid response code or
* a PEAR_Error object is an error condition is reached.
*
* @access private
* @since 1.1.0
*
* @see getResponse
*/
function _parseResponse($valid)
{
global $CRLF;
$this->_code = -1;
$this->_arguments = array();
-
+
if(!is_resource($this->connection)) return false;
-
+
while ($line = fgets($this->connection, 512)) {
if ($this->debug) {
echo "DEBUG: Recv: $line<br>\n";
}
-
+
/* If we receive an empty line, the connection has been closed. */
if (empty($line)) {
$this->disconnect();
return 'Connection was unexpectedly closed';
}
-
+
/* Read the code and store the rest in the arguments array. */
$code = substr($line, 0, 3);
$this->_arguments[] = trim(substr($line, 4));
-
+
/* Check the syntax of the response code. */
if (is_numeric($code)) {
$this->_code = (int)$code;
} else {
$this->_code = -1;
break;
}
-
+
/* If this is not a multiline response, we're done. */
if (substr($line, 3, 1) != '-') {
break;
}
}
-
+
/* Compare the server's response code with the valid code. */
if (is_int($valid) && ($this->_code === $valid)) {
return true;
}
-
+
/* If we were given an array of valid response codes, check each one. */
if (is_array($valid)) {
foreach ($valid as $valid_code) {
if ($this->_code === $valid_code) {
return true;
}
}
}
-
+
return 'Invalid response code received from server';
}
-
+
/***************************************
** Function that handles the MAIL FROM: cmd
***************************************/
-
+
function mail($from){
if($this->is_connected()
AND $this->send_data('MAIL FROM:'.$from.'')
AND substr(trim($this->get_data()), 0, 2) === '250' ){
return TRUE;
}else
return FALSE;
}
/***************************************
** Function that handles the RCPT TO: cmd
***************************************/
-
+
function rcpt($to){
if($this->is_connected()
AND $this->send_data('RCPT TO:'.$to.'')
AND substr(trim($error = $this->get_data()), 0, 2) === '25' ){
return TRUE;
}else{
$this->errors[] = trim(substr(trim($error), 3));
return FALSE;
}
}
/***************************************
** Function that sends the DATA cmd
***************************************/
function data(){
if($this->is_connected()
AND $this->send_data('DATA')
AND substr(trim($error = $this->get_data()), 0, 3) === '354' ){
-
+
return TRUE;
}else{
$this->errors[] = trim(substr(trim($error), 3));
return FALSE;
}
}
/***************************************
** Function to determine if this object
** is connected to the server or not.
***************************************/
function is_connected(){
return (is_resource($this->connection) AND ($this->status === SMTP_STATUS_CONNECTED));
}
/***************************************
** Function to send a bit of data
***************************************/
function send_data($data){
global $CRLF;
if($this->debug)
{
$this->buffer[] = "SEND: $data<br>\n";
}
if(is_resource($this->connection)){
return fwrite($this->connection, $data.$CRLF, strlen($data)+2);
}else
return FALSE;
}
- function bytes_left($fp)
- {
+ function bytes_left($fp)
+ {
$status = socket_get_status ($fp);
//print_r($status);
$bytes = $status["unread_bytes"];
- return $bytes;
+ return $bytes;
}
/***************************************
** Function to get data.
***************************************/
function &get_data(){
global $CRLF;
$return = '';
$line = '';
if(is_resource($this->connection))
{
while(strpos($return, $CRLF) === FALSE OR substr($line,3,1) !== ' ')
- {
+ {
$line = fgets($this->connection, 512);
- $return .= $line;
+ $return .= $line;
}
if($this->debug)
{
- $this->buffer[] = "GET: ".$return."<br>\n";
+ $this->buffer[] = "GET: ".$return."<br>\n";
}
return $return;
}else
return FALSE;
}
/***************************************
** Sets a variable
***************************************/
-
+
function set($var, $value){
$this->$var = $value;
return TRUE;
}
} // End of class
-
-
-
-
+
+
+
+
?>
Property changes on: trunk/kernel/include/smtp.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.4
\ No newline at end of property
+1.5
\ No newline at end of property

Event Timeline