2003-10-11 23:51:29 +00:00
< ? php
2005-04-09 12:26:45 +00:00
/**
*
* @ package phpBB3
* @ version $Id $
* @ copyright ( c ) 2005 phpBB Group
* @ license http :// opensource . org / licenses / gpl - license . php GNU Public License
*
*/
/**
* Messenger
2006-06-13 21:06:29 +00:00
* @ package phpBB3
2005-04-09 12:26:45 +00:00
*/
2003-10-11 23:51:29 +00:00
class messenger
{
2006-03-19 22:25:43 +00:00
var $vars , $msg , $extra_headers , $replyto , $from , $subject , $encoding ;
2004-11-06 14:22:04 +00:00
var $addresses = array ();
2003-10-11 23:51:29 +00:00
2004-02-01 21:45:40 +00:00
var $mail_priority = MAIL_NORMAL_PRIORITY ;
var $use_queue = true ;
2004-11-06 14:22:04 +00:00
var $tpl_msg = array ();
2003-10-11 23:51:29 +00:00
2006-06-06 20:53:46 +00:00
/**
* Constructor
*/
2004-02-01 21:45:40 +00:00
function messenger ( $use_queue = true )
2003-10-12 15:29:18 +00:00
{
global $config ;
if ( preg_match ( '#^[c-z]:\\\#i' , getenv ( 'PATH' )) && ! $config [ 'smtp_delivery' ] && phpversion () < '4.3' )
{
// We are running on windows, force delivery to use our smtp functions since php's are broken by default
$config [ 'smtp_delivery' ] = 1 ;
$config [ 'smtp_host' ] = @ ini_get ( 'SMTP' );
}
2004-02-01 21:45:40 +00:00
$this -> use_queue = $use_queue ;
2004-11-06 14:22:04 +00:00
$this -> subject = '' ;
2003-10-12 15:29:18 +00:00
}
2006-06-06 20:53:46 +00:00
/**
* Resets all the data ( address , template file , etc etc ) to default
*/
2003-10-11 23:51:29 +00:00
function reset ()
{
$this -> addresses = array ();
2004-11-06 14:22:04 +00:00
$this -> vars = $this -> msg = $this -> extra_headers = $this -> replyto = $this -> from = $this -> encoding = '' ;
2004-02-01 21:45:40 +00:00
$this -> mail_priority = MAIL_NORMAL_PRIORITY ;
2003-10-11 23:51:29 +00:00
}
2006-06-06 20:53:46 +00:00
/**
* Sets an email address to send to
*/
2003-10-11 23:51:29 +00:00
function to ( $address , $realname = '' )
{
2004-10-19 19:26:58 +00:00
$pos = isset ( $this -> addresses [ 'to' ]) ? sizeof ( $this -> addresses [ 'to' ]) : 0 ;
2003-10-11 23:51:29 +00:00
$this -> addresses [ 'to' ][ $pos ][ 'email' ] = trim ( $address );
2004-02-07 11:00:27 +00:00
$this -> addresses [ 'to' ][ $pos ][ 'name' ] = trim ( $realname );
2003-10-11 23:51:29 +00:00
}
2006-06-06 20:53:46 +00:00
/**
* Sets an cc address to send to
*/
2003-10-11 23:51:29 +00:00
function cc ( $address , $realname = '' )
{
2004-10-19 19:26:58 +00:00
$pos = isset ( $this -> addresses [ 'cc' ]) ? sizeof ( $this -> addresses [ 'cc' ]) : 0 ;
2003-10-11 23:51:29 +00:00
$this -> addresses [ 'cc' ][ $pos ][ 'email' ] = trim ( $address );
2004-02-07 11:00:27 +00:00
$this -> addresses [ 'cc' ][ $pos ][ 'name' ] = trim ( $realname );
2003-10-11 23:51:29 +00:00
}
2006-06-06 20:53:46 +00:00
/**
* Sets an bcc address to send to
*/
2003-10-11 23:51:29 +00:00
function bcc ( $address , $realname = '' )
{
2004-10-19 19:26:58 +00:00
$pos = isset ( $this -> addresses [ 'bcc' ]) ? sizeof ( $this -> addresses [ 'bcc' ]) : 0 ;
2003-10-11 23:51:29 +00:00
$this -> addresses [ 'bcc' ][ $pos ][ 'email' ] = trim ( $address );
2004-10-19 19:26:58 +00:00
$this -> addresses [ 'bcc' ][ $pos ][ 'name' ] = trim ( $realname );
2003-10-11 23:51:29 +00:00
}
2006-06-06 20:53:46 +00:00
/**
* Sets a im contact to send to
*/
2003-10-12 15:29:18 +00:00
function im ( $address , $realname = '' )
{
2004-10-19 19:26:58 +00:00
$pos = isset ( $this -> addresses [ 'im' ]) ? sizeof ( $this -> addresses [ 'im' ]) : 0 ;
2003-10-12 15:29:18 +00:00
$this -> addresses [ 'im' ][ $pos ][ 'uid' ] = trim ( $address );
$this -> addresses [ 'im' ][ $pos ][ 'name' ] = trim ( $realname );
}
2006-06-06 20:53:46 +00:00
/**
* Set the reply to address
*/
2003-10-11 23:51:29 +00:00
function replyto ( $address )
{
$this -> replyto = trim ( $address );
}
2006-06-06 20:53:46 +00:00
/**
* Set the from address
*/
2003-10-11 23:51:29 +00:00
function from ( $address )
{
$this -> from = trim ( $address );
}
2006-06-06 20:53:46 +00:00
/**
* set up subject for mail
*/
2003-10-11 23:51:29 +00:00
function subject ( $subject = '' )
{
$this -> subject = trim ( $subject );
}
2006-06-06 20:53:46 +00:00
/**
* set up extra mail headers
*/
2003-10-11 23:51:29 +00:00
function headers ( $headers )
{
2006-07-20 17:55:36 +00:00
$this -> extra_headers .= trim ( $headers ) . " \n " ;
2003-10-11 23:51:29 +00:00
}
2006-06-06 20:53:46 +00:00
/**
* Set the email priority
*/
2004-02-01 21:45:40 +00:00
function set_mail_priority ( $priority = MAIL_NORMAL_PRIORITY )
{
$this -> mail_priority = $priority ;
}
2006-06-06 20:53:46 +00:00
/**
* Set email template to use
*/
2003-10-11 23:51:29 +00:00
function template ( $template_file , $template_lang = '' )
{
global $config , $phpbb_root_path ;
if ( ! trim ( $template_file ))
{
trigger_error ( 'No template file set' , E_USER_ERROR );
}
if ( ! trim ( $template_lang ))
{
2006-06-06 20:53:46 +00:00
$template_lang = basename ( $config [ 'default_lang' ]);
2003-10-11 23:51:29 +00:00
}
2004-02-07 11:00:27 +00:00
if ( empty ( $this -> tpl_msg [ $template_lang . $template_file ]))
2003-10-11 23:51:29 +00:00
{
$tpl_file = " { $phpbb_root_path } language/ $template_lang /email/ $template_file .txt " ;
if ( ! file_exists ( $tpl_file ))
{
$tpl_file = " { $phpbb_root_path } language/ $template_lang /email/ $template_file .txt " ;
if ( ! file_exists ( $tpl_file ))
{
2006-04-30 12:57:37 +00:00
trigger_error ( " Could not find email template file [ $tpl_file ] " , E_USER_ERROR );
2003-10-11 23:51:29 +00:00
}
}
if ( ! ( $fd = @ fopen ( $tpl_file , 'r' )))
{
2006-04-30 12:57:37 +00:00
trigger_error ( " Failed opening template file [ $tpl_file ] " , E_USER_ERROR );
2003-10-11 23:51:29 +00:00
}
2004-02-07 11:00:27 +00:00
$this -> tpl_msg [ $template_lang . $template_file ] = fread ( $fd , filesize ( $tpl_file ));
2003-10-11 23:51:29 +00:00
fclose ( $fd );
}
2004-02-07 11:00:27 +00:00
$this -> msg = $this -> tpl_msg [ $template_lang . $template_file ];
2003-10-11 23:51:29 +00:00
return true ;
}
2006-06-06 20:53:46 +00:00
/**
* assign variables to email template
*/
2003-10-11 23:51:29 +00:00
function assign_vars ( $vars )
{
2006-04-15 14:48:36 +00:00
$this -> vars = ( empty ( $this -> vars )) ? $vars : $this -> vars + $vars ;
2003-10-11 23:51:29 +00:00
}
2006-06-06 20:53:46 +00:00
/**
* Send the mail out to the recipients set previously in var $this -> addresses
*/
2004-09-04 19:32:23 +00:00
function send ( $method = NOTIFY_EMAIL , $break = false )
2003-10-11 23:51:29 +00:00
{
global $config , $user ;
// Escape all quotes, else the eval will fail.
$this -> msg = str_replace ( " ' " , " \ ' " , $this -> msg );
$this -> msg = preg_replace ( '#\{([a-z0-9\-_]*?)\}#is' , " ' . $\\1 . ' " , $this -> msg );
// Set vars
foreach ( $this -> vars as $key => $val )
{
$$key = $val ;
}
eval ( " \$ this->msg = ' $this->msg '; " );
// Clear vars
foreach ( $this -> vars as $key => $val )
{
unset ( $$key );
}
// We now try and pull a subject from the email body ... if it exists,
// do this here because the subject may contain a variable
$drop_header = '' ;
$match = array ();
if ( preg_match ( '#^(Subject:(.*?))$#m' , $this -> msg , $match ))
{
$this -> subject = ( trim ( $match [ 2 ]) != '' ) ? trim ( $match [ 2 ]) : (( $this -> subject != '' ) ? $this -> subject : $user -> lang [ 'NO_SUBJECT' ]);
$drop_header .= '[\r\n]*?' . preg_quote ( $match [ 1 ], '#' );
}
else
{
$this -> subject = (( $this -> subject != '' ) ? $this -> subject : $user -> lang [ 'NO_SUBJECT' ]);
}
if ( preg_match ( '#^(Charset:(.*?))$#m' , $this -> msg , $match ))
{
$this -> encoding = ( trim ( $match [ 2 ]) != '' ) ? trim ( $match [ 2 ]) : trim ( $user -> lang [ 'ENCODING' ]);
$drop_header .= '[\r\n]*?' . preg_quote ( $match [ 1 ], '#' );
}
else
{
$this -> encoding = trim ( $user -> lang [ 'ENCODING' ]);
}
if ( $drop_header )
{
$this -> msg = trim ( preg_replace ( '#' . $drop_header . '#s' , '' , $this -> msg ));
}
2004-09-04 19:32:23 +00:00
if ( $break )
{
2006-05-26 15:04:27 +00:00
return true ;
2004-09-04 19:32:23 +00:00
}
2003-10-11 23:51:29 +00:00
switch ( $method )
{
case NOTIFY_EMAIL :
2004-05-26 20:32:51 +00:00
$result = $this -> msg_email ();
2006-04-15 14:48:36 +00:00
break ;
2003-10-11 23:51:29 +00:00
case NOTIFY_IM :
2004-02-07 11:00:27 +00:00
$result = $this -> msg_jabber ();
2006-04-15 14:48:36 +00:00
break ;
2006-06-06 20:53:46 +00:00
2003-10-11 23:51:29 +00:00
case NOTIFY_BOTH :
2004-05-26 20:32:51 +00:00
$result = $this -> msg_email ();
2003-10-11 23:51:29 +00:00
$this -> msg_jabber ();
2006-04-15 14:48:36 +00:00
break ;
2003-10-11 23:51:29 +00:00
}
$this -> reset ();
2004-02-07 11:00:27 +00:00
return $result ;
2003-10-11 23:51:29 +00:00
}
2006-06-06 20:53:46 +00:00
/**
* Add error message to log
*/
2003-10-11 23:51:29 +00:00
function error ( $type , $msg )
{
2003-10-13 17:41:27 +00:00
global $user , $phpEx , $phpbb_root_path ;
// Session doesn't exist, create it
2006-06-13 18:50:21 +00:00
if ( ! isset ( $user -> session_id ) || $user -> session_id === '' )
{
$user -> session_begin ();
}
2003-10-11 23:51:29 +00:00
2005-12-05 18:52:23 +00:00
add_log ( 'critical' , 'LOG_ERROR_' . $type , $msg );
2003-10-11 23:51:29 +00:00
}
2006-06-06 20:53:46 +00:00
/**
* Save to queue
*/
2005-04-09 12:26:45 +00:00
function save_queue ()
{
2005-08-18 12:44:24 +00:00
global $config ;
if ( $config [ 'email_package_size' ] && $this -> use_queue && ! empty ( $this -> queue ))
2005-04-09 12:26:45 +00:00
{
$this -> queue -> save ();
}
}
2003-10-11 23:51:29 +00:00
2006-06-06 20:53:46 +00:00
/**
* Send out emails
*/
2004-05-26 20:32:51 +00:00
function msg_email ()
2003-10-11 23:51:29 +00:00
{
global $config , $user ;
if ( empty ( $config [ 'email_enable' ]))
{
return false ;
}
$use_queue = false ;
2004-02-01 21:45:40 +00:00
if ( $config [ 'email_package_size' ] && $this -> use_queue )
2003-10-11 23:51:29 +00:00
{
if ( empty ( $this -> queue ))
{
$this -> queue = new queue ();
2004-02-01 21:45:40 +00:00
$this -> queue -> init ( 'email' , $config [ 'email_package_size' ]);
2003-10-11 23:51:29 +00:00
}
$use_queue = true ;
}
$to = $cc = $bcc = '' ;
// Build to, cc and bcc strings
foreach ( $this -> addresses as $type => $address_ary )
{
2004-11-06 14:22:04 +00:00
if ( $type == 'im' )
{
continue ;
}
2003-10-11 23:51:29 +00:00
foreach ( $address_ary as $which_ary )
{
2004-05-26 20:32:51 +00:00
$$type .= (( $$type != '' ) ? ', ' : '' ) . (( $which_ary [ 'name' ] != '' ) ? '"' . mail_encode ( $which_ary [ 'name' ], $this -> encoding ) . '" <' . $which_ary [ 'email' ] . '>' : $which_ary [ 'email' ]);
2003-10-11 23:51:29 +00:00
}
}
if ( empty ( $this -> replyto ))
{
$this -> replyto = '<' . $config [ 'board_email' ] . '>' ;
}
if ( empty ( $this -> from ))
{
$this -> from = '<' . $config [ 'board_email' ] . '>' ;
}
// Build header
2006-07-20 17:55:36 +00:00
$headers = 'From: ' . $this -> from . " \n " ;
$headers .= ( $cc != '' ) ? " Cc: $cc\n " : '' ;
$headers .= ( $bcc != '' ) ? " Bcc: $bcc\n " : '' ;
$headers .= 'Reply-to: ' . $this -> replyto . " \n " ;
$headers .= 'Return-Path: <' . $config [ 'board_email' ] . " > \n " ;
$headers .= 'Sender: <' . $config [ 'board_email' ] . " > \n " ;
$headers .= " MIME-Version: 1.0 \n " ;
$headers .= 'Message-ID: <' . md5 ( unique_id ( time ())) . " @ " . $config [ 'server_name' ] . " > \n " ;
$headers .= 'Date: ' . gmdate ( 'D, d M Y H:i:s T' , time ()) . " \n " ;
$headers .= " Content-type: text/plain; charset= { $this -> encoding } \n " ;
$headers .= " Content-transfer-encoding: 8bit \n " ;
$headers .= " X-Priority: { $this -> mail_priority } \n " ;
$headers .= 'X-MSMail-Priority: ' . (( $this -> mail_priority == MAIL_LOW_PRIORITY ) ? 'Low' : (( $this -> mail_priority == MAIL_NORMAL_PRIORITY ) ? 'Normal' : 'High' )) . " \n " ;
$headers .= " X-Mailer: PhpBB3 \n " ;
$headers .= " X-MimeOLE: phpBB3 \n " ;
$headers .= " X-phpBB-Origin: phpbb:// " . str_replace ( array ( 'http://' , 'https://' ), array ( '' , '' ), generate_board_url ()) . " \n " ;
2003-10-11 23:51:29 +00:00
$headers .= ( $this -> extra_headers != '' ) ? $this -> extra_headers : '' ;
// Send message ... removed $this->encode() from subject for time being
if ( ! $use_queue )
{
2003-11-23 22:25:46 +00:00
$mail_to = ( $to == '' ) ? 'Undisclosed-Recipient:;' : $to ;
2003-10-11 23:51:29 +00:00
$err_msg = '' ;
2004-02-02 13:06:18 +00:00
2006-07-20 17:55:36 +00:00
if ( $config [ 'smtp_delivery' ])
{
$result = smtpmail ( $this -> addresses , $this -> subject , wordwrap ( $this -> msg ), $err_msg , $this -> encoding , $headers );
}
else
{
$result = @ $config [ 'email_function_name' ]( $mail_to , $this -> subject , implode ( " \n " , preg_split ( " / \r ? \n / " , wordwrap ( $this -> msg ))), $headers );
}
2003-10-11 23:51:29 +00:00
if ( ! $result )
{
$message = '<u>EMAIL ERROR</u> [ ' . (( $config [ 'smtp_delivery' ]) ? 'SMTP' : 'PHP' ) . ' ]<br /><br />' . $err_msg . '<br /><br /><u>CALLING PAGE</u><br /><br />' . (( ! empty ( $_SERVER [ 'PHP_SELF' ])) ? $_SERVER [ 'PHP_SELF' ] : $_ENV [ 'PHP_SELF' ]) . '<br />' ;
$this -> error ( 'EMAIL' , $message );
2004-02-01 21:45:40 +00:00
return false ;
2003-10-11 23:51:29 +00:00
}
}
else
{
$this -> queue -> put ( 'email' , array (
'to' => $to ,
'addresses' => $this -> addresses ,
'subject' => $this -> subject ,
'msg' => $this -> msg ,
2004-05-26 20:32:51 +00:00
'encoding' => $this -> encoding ,
2003-10-11 23:51:29 +00:00
'headers' => $headers )
);
}
2004-02-07 11:00:27 +00:00
2003-10-11 23:51:29 +00:00
return true ;
}
2006-06-06 20:53:46 +00:00
/**
* Send jabber message out
*/
2003-10-11 23:51:29 +00:00
function msg_jabber ()
{
global $config , $db , $user , $phpbb_root_path , $phpEx ;
if ( empty ( $config [ 'jab_enable' ]) || empty ( $config [ 'jab_host' ]) || empty ( $config [ 'jab_username' ]) || empty ( $config [ 'jab_password' ]))
{
return false ;
}
$use_queue = false ;
2004-02-01 21:45:40 +00:00
if ( $config [ 'jab_package_size' ] && $this -> use_queue )
2003-10-11 23:51:29 +00:00
{
if ( empty ( $this -> queue ))
{
$this -> queue = new queue ();
2004-02-01 21:45:40 +00:00
$this -> queue -> init ( 'jabber' , $config [ 'jab_package_size' ]);
2003-10-11 23:51:29 +00:00
}
$use_queue = true ;
}
$addresses = array ();
2003-10-12 15:29:18 +00:00
foreach ( $this -> addresses [ 'im' ] as $type => $uid_ary )
2003-10-11 23:51:29 +00:00
{
2003-10-12 15:29:18 +00:00
$addresses [] = $uid_ary [ 'uid' ];
2003-10-11 23:51:29 +00:00
}
$addresses = array_unique ( $addresses );
if ( ! $use_queue )
{
include_once ( $phpbb_root_path . 'includes/functions_jabber.' . $phpEx );
2005-04-10 11:21:01 +00:00
$this -> jabber = new jabber ;
2003-10-11 23:51:29 +00:00
$this -> jabber -> server = $config [ 'jab_host' ];
2003-10-12 20:38:37 +00:00
$this -> jabber -> port = ( $config [ 'jab_port' ]) ? $config [ 'jab_port' ] : 5222 ;
2003-10-11 23:51:29 +00:00
$this -> jabber -> username = $config [ 'jab_username' ];
$this -> jabber -> password = $config [ 'jab_password' ];
2003-10-12 23:33:26 +00:00
$this -> jabber -> resource = ( $config [ 'jab_resource' ]) ? $config [ 'jab_resource' ] : '' ;
2006-06-09 23:46:39 +00:00
$this -> jabber -> encoding = $this -> encoding ;
2003-10-11 23:51:29 +00:00
2005-04-10 11:21:01 +00:00
if ( ! $this -> jabber -> connect ())
2003-10-11 23:51:29 +00:00
{
$this -> error ( 'JABBER' , 'Could not connect to Jabber server' );
2004-05-26 20:32:51 +00:00
return false ;
2003-10-11 23:51:29 +00:00
}
2005-04-10 11:21:01 +00:00
if ( ! $this -> jabber -> send_auth ())
2003-10-11 23:51:29 +00:00
{
$this -> error ( 'JABBER' , 'Could not authorise on Jabber server' );
2004-05-26 20:32:51 +00:00
return false ;
2003-10-11 23:51:29 +00:00
}
2005-04-10 11:21:01 +00:00
$this -> jabber -> send_presence ( NULL , NULL , 'online' );
2003-10-11 23:51:29 +00:00
foreach ( $addresses as $address )
{
2006-06-09 23:46:39 +00:00
$this -> jabber -> send_message ( $address , 'normal' , NULL , array ( 'body' => $this -> msg ));
2003-10-11 23:51:29 +00:00
}
2003-10-12 20:38:37 +00:00
sleep ( 1 );
2005-04-10 11:21:01 +00:00
$this -> jabber -> disconnect ();
2003-10-11 23:51:29 +00:00
}
else
{
$this -> queue -> put ( 'jabber' , array (
'addresses' => $addresses ,
2006-06-06 20:53:46 +00:00
'subject' => $this -> subject ,
'msg' => $this -> msg )
2003-10-11 23:51:29 +00:00
);
}
unset ( $addresses );
return true ;
}
}
2005-04-09 12:26:45 +00:00
/**
2006-06-06 20:53:46 +00:00
* handling email and jabber queue
2006-06-13 21:06:29 +00:00
* @ package phpBB3
2005-04-09 12:26:45 +00:00
*/
2003-10-11 23:51:29 +00:00
class queue
{
var $data = array ();
var $queue_data = array ();
var $package_size = 0 ;
var $cache_file = '' ;
2006-06-06 20:53:46 +00:00
/**
* constructor
*/
2003-10-11 23:51:29 +00:00
function queue ()
{
global $phpEx , $phpbb_root_path ;
$this -> data = array ();
$this -> cache_file = " { $phpbb_root_path } cache/queue. $phpEx " ;
}
2006-06-06 20:53:46 +00:00
/**
* Init a queue object
*/
2003-10-11 23:51:29 +00:00
function init ( $object , $package_size )
{
$this -> data [ $object ] = array ();
$this -> data [ $object ][ 'package_size' ] = $package_size ;
$this -> data [ $object ][ 'data' ] = array ();
}
2006-06-06 20:53:46 +00:00
/**
* Put object in queue
*/
2003-10-11 23:51:29 +00:00
function put ( $object , $scope )
{
$this -> data [ $object ][ 'data' ][] = $scope ;
}
2006-06-06 20:53:46 +00:00
/**
* Process queue
* Using lock file
*/
2003-10-11 23:51:29 +00:00
function process ()
{
global $db , $config , $phpEx , $phpbb_root_path ;
2005-04-10 11:21:01 +00:00
set_config ( 'last_queue_run' , time (), true );
2003-10-11 23:51:29 +00:00
2004-05-31 18:00:10 +00:00
// Delete stale lock file
if ( file_exists ( $this -> cache_file . '.lock' ) && ! file_exists ( $this -> cache_file ))
{
@ unlink ( $this -> cache_file . '.lock' );
return ;
}
2003-10-12 20:38:37 +00:00
if ( ! file_exists ( $this -> cache_file ) || ( file_exists ( $this -> cache_file . '.lock' ) && filemtime ( $this -> cache_file ) > time () - $config [ 'queue_interval' ]))
2003-10-11 23:51:29 +00:00
{
return ;
}
$fp = @ fopen ( $this -> cache_file . '.lock' , 'wb' );
fclose ( $fp );
include ( $this -> cache_file );
foreach ( $this -> queue_data as $object => $data_ary )
{
2005-10-02 18:17:06 +00:00
@ set_time_limit ( 0 );
if ( ! isset ( $data_ary [ 'package_size' ]))
{
$data_ary [ 'package_size' ] = 0 ;
}
2003-10-11 23:51:29 +00:00
2003-10-14 13:24:07 +00:00
$package_size = $data_ary [ 'package_size' ];
2005-04-10 18:07:12 +00:00
$num_items = ( sizeof ( $data_ary [ 'data' ]) < $package_size ) ? sizeof ( $data_ary [ 'data' ]) : $package_size ;
2003-10-11 23:51:29 +00:00
switch ( $object )
{
case 'email' :
// Delete the email queued objects if mailing is disabled
if ( ! $config [ 'email_enable' ])
{
unset ( $this -> queue_data [ 'email' ]);
2003-10-14 13:30:43 +00:00
continue 2 ;
2003-10-11 23:51:29 +00:00
}
2006-04-15 14:48:36 +00:00
break ;
2003-10-11 23:51:29 +00:00
case 'jabber' :
if ( ! $config [ 'jab_enable' ])
{
unset ( $this -> queue_data [ 'jabber' ]);
2003-10-14 13:30:43 +00:00
continue 2 ;
2003-10-11 23:51:29 +00:00
}
include_once ( $phpbb_root_path . 'includes/functions_jabber.' . $phpEx );
2005-04-10 11:21:01 +00:00
$this -> jabber = new jabber ;
2003-10-11 23:51:29 +00:00
$this -> jabber -> server = $config [ 'jab_host' ];
2003-10-12 20:38:37 +00:00
$this -> jabber -> port = ( $config [ 'jab_port' ]) ? $config [ 'jab_port' ] : 5222 ;
2003-10-11 23:51:29 +00:00
$this -> jabber -> username = $config [ 'jab_username' ];
$this -> jabber -> password = $config [ 'jab_password' ];
2003-10-12 23:33:26 +00:00
$this -> jabber -> resource = ( $config [ 'jab_resource' ]) ? $config [ 'jab_resource' ] : '' ;
2003-10-11 23:51:29 +00:00
2005-04-10 11:21:01 +00:00
if ( ! $this -> jabber -> connect ())
2003-10-11 23:51:29 +00:00
{
messenger :: error ( 'JABBER' , 'Could not connect to Jabber server' );
2003-10-14 13:30:43 +00:00
continue 2 ;
2003-10-11 23:51:29 +00:00
}
2005-04-10 11:21:01 +00:00
if ( ! $this -> jabber -> send_auth ())
2003-10-11 23:51:29 +00:00
{
messenger :: error ( 'JABBER' , 'Could not authorise on Jabber server' );
2003-10-14 13:30:43 +00:00
continue 2 ;
2003-10-11 23:51:29 +00:00
}
2005-04-10 11:21:01 +00:00
$this -> jabber -> send_presence ( NULL , NULL , 'online' );
2006-04-15 14:48:36 +00:00
break ;
2003-10-11 23:51:29 +00:00
default :
return ;
}
for ( $i = 0 ; $i < $num_items ; $i ++ )
{
2006-03-21 19:23:34 +00:00
// Make variables available...
2003-10-11 23:51:29 +00:00
extract ( array_shift ( $this -> queue_data [ $object ][ 'data' ]));
switch ( $object )
{
case 'email' :
$err_msg = '' ;
2003-11-23 22:25:46 +00:00
$to = ( ! $to ) ? 'Undisclosed-Recipient:;' : $to ;
2004-05-26 20:32:51 +00:00
$result = ( $config [ 'smtp_delivery' ]) ? smtpmail ( $addresses , $subject , wordwrap ( $msg ), $err_msg , $encoding , $headers ) : @ $config [ 'email_function_name' ]( $to , $subject , implode ( " \n " , preg_split ( " / \r ? \n / " , wordwrap ( $msg ))), $headers );
2003-10-11 23:51:29 +00:00
if ( ! $result )
{
@ unlink ( $this -> cache_file . '.lock' );
$message = 'Method: [ ' . (( $config [ 'smtp_delivery' ]) ? 'SMTP' : 'PHP' ) . ' ]<br /><br />' . $err_msg . '<br /><br /><u>CALLING PAGE</u><br /><br />' . (( ! empty ( $_SERVER [ 'PHP_SELF' ])) ? $_SERVER [ 'PHP_SELF' ] : $_ENV [ 'PHP_SELF' ]);
2005-12-05 18:52:23 +00:00
messenger :: error ( 'EMAIL' , $message );
2003-10-14 13:30:43 +00:00
continue 3 ;
2003-10-11 23:51:29 +00:00
}
2006-04-15 14:48:36 +00:00
break ;
2003-10-11 23:51:29 +00:00
case 'jabber' :
foreach ( $addresses as $address )
{
2005-04-10 11:21:01 +00:00
$this -> jabber -> send_message ( $address , 'normal' , NULL , array ( 'body' => $msg ));
2003-10-11 23:51:29 +00:00
}
2006-04-15 14:48:36 +00:00
break ;
2003-10-11 23:51:29 +00:00
}
}
// No more data for this object? Unset it
2005-04-10 18:07:12 +00:00
if ( ! sizeof ( $this -> queue_data [ $object ][ 'data' ]))
2003-10-11 23:51:29 +00:00
{
unset ( $this -> queue_data [ $object ]);
}
// Post-object processing
switch ( $object )
{
case 'jabber' :
// Hang about a couple of secs to ensure the messages are
// handled, then disconnect
2003-10-12 20:38:37 +00:00
sleep ( 1 );
2005-04-10 11:21:01 +00:00
$this -> jabber -> disconnect ();
2006-04-15 14:48:36 +00:00
break ;
2003-10-11 23:51:29 +00:00
}
}
if ( ! sizeof ( $this -> queue_data ))
{
2003-10-11 23:59:41 +00:00
@ unlink ( $this -> cache_file );
2003-10-11 23:51:29 +00:00
}
else
{
$file = '<?php $this->queue_data=' . $this -> format_array ( $this -> queue_data ) . '; ?>' ;
if ( $fp = @ fopen ( $this -> cache_file , 'w' ))
{
@ flock ( $fp , LOCK_EX );
fwrite ( $fp , $file );
@ flock ( $fp , LOCK_UN );
fclose ( $fp );
}
}
@ unlink ( $this -> cache_file . '.lock' );
}
2006-06-06 20:53:46 +00:00
/**
* Save queue
*/
2003-10-11 23:51:29 +00:00
function save ()
{
2004-02-01 21:45:40 +00:00
if ( ! sizeof ( $this -> data ))
{
return ;
}
2003-10-11 23:51:29 +00:00
if ( file_exists ( $this -> cache_file ))
{
include ( $this -> cache_file );
foreach ( $this -> queue_data as $object => $data_ary )
{
2005-04-10 18:07:12 +00:00
if ( isset ( $this -> data [ $object ]) && sizeof ( $this -> data [ $object ]))
2003-10-11 23:51:29 +00:00
{
$this -> data [ $object ][ 'data' ] = array_merge ( $data_ary [ 'data' ], $this -> data [ $object ][ 'data' ]);
}
2005-04-10 18:07:12 +00:00
else
{
$this -> data [ $object ][ 'data' ] = $data_ary [ 'data' ];
}
2003-10-11 23:51:29 +00:00
}
}
$file = '<?php $this->queue_data = ' . $this -> format_array ( $this -> data ) . '; ?>' ;
2006-05-27 16:24:21 +00:00
if ( $fp = @ fopen ( $this -> cache_file , 'w' ))
2003-10-11 23:51:29 +00:00
{
@ flock ( $fp , LOCK_EX );
fwrite ( $fp , $file );
@ flock ( $fp , LOCK_UN );
fclose ( $fp );
}
}
2006-06-06 20:53:46 +00:00
/**
* Format array
2006-08-22 21:26:06 +00:00
* @ access private
2006-06-06 20:53:46 +00:00
*/
2003-10-11 23:51:29 +00:00
function format_array ( $array )
{
$lines = array ();
foreach ( $array as $k => $v )
{
if ( is_array ( $v ))
{
$lines [] = " ' $k '=> " . $this -> format_array ( $v );
}
2005-11-30 17:48:06 +00:00
else if ( is_int ( $v ))
2003-10-11 23:51:29 +00:00
{
$lines [] = " ' $k '=> $v " ;
}
2005-11-30 17:48:06 +00:00
else if ( is_bool ( $v ))
2003-10-11 23:51:29 +00:00
{
2005-11-30 17:48:06 +00:00
$lines [] = " ' $k '=> " . (( $v ) ? 'true' : 'false' );
2003-10-11 23:51:29 +00:00
}
else
{
$lines [] = " ' $k '=>' " . str_replace ( " ' " , " \ ' " , str_replace ( '\\' , '\\\\' , $v )) . " ' " ;
}
}
2004-01-31 15:49:12 +00:00
2003-10-11 23:51:29 +00:00
return 'array(' . implode ( ',' , $lines ) . ')' ;
}
}
2005-04-09 12:26:45 +00:00
/**
* Replacement or substitute for PHP ' s mail command
*/
2004-05-26 20:32:51 +00:00
function smtpmail ( $addresses , $subject , $message , & $err_msg , $encoding , $headers = '' )
2003-10-11 23:51:29 +00:00
{
2004-02-07 11:00:27 +00:00
global $config , $user ;
2003-10-11 23:51:29 +00:00
// Fix any bare linefeeds in the message to make it RFC821 Compliant.
$message = preg_replace ( " #(?<! \r ) \n #si " , " \r \n " , $message );
if ( $headers != '' )
{
if ( is_array ( $headers ))
{
$headers = ( sizeof ( $headers ) > 1 ) ? join ( " \n " , $headers ) : $headers [ 0 ];
}
$headers = chop ( $headers );
// Make sure there are no bare linefeeds in the headers
$headers = preg_replace ( '#(?<!\r)\n#si' , " \r \n " , $headers );
// Ok this is rather confusing all things considered,
// but we have to grab bcc and cc headers and treat them differently
// Something we really didn't take into consideration originally
$header_array = explode ( " \r \n " , $headers );
$headers = '' ;
foreach ( $header_array as $header )
{
if ( preg_match ( '#^cc:#si' , $header ) || preg_match ( '#^bcc:#si' , $header ))
{
$header = '' ;
}
$headers .= ( $header != '' ) ? $header . " \r \n " : '' ;
}
$headers = chop ( $headers );
}
if ( trim ( $subject ) == '' )
{
2006-06-06 20:53:46 +00:00
$err_msg = ( isset ( $user -> lang [ 'NO_EMAIL_SUBJECT' ])) ? $user -> lang [ 'NO_EMAIL_SUBJECT' ] : 'No email subject specified' ;
2004-01-31 15:49:12 +00:00
return false ;
2003-10-11 23:51:29 +00:00
}
if ( trim ( $message ) == '' )
{
2006-06-06 20:53:46 +00:00
$err_msg = ( isset ( $user -> lang [ 'NO_EMAIL_MESSAGE' ])) ? $user -> lang [ 'NO_EMAIL_MESSAGE' ] : 'Email message was blank' ;
2004-01-31 15:49:12 +00:00
return false ;
2003-10-11 23:51:29 +00:00
}
$mail_rcpt = $mail_to = $mail_cc = array ();
// Build correct addresses for RCPT TO command and the client side display (TO, CC)
foreach ( $addresses [ 'to' ] as $which_ary )
{
2004-05-26 20:32:51 +00:00
$mail_to [] = ( $which_ary [ 'name' ] != '' ) ? mail_encode ( trim ( $which_ary [ 'name' ]), $encoding ) . ' <' . trim ( $which_ary [ 'email' ]) . '>' : '<' . trim ( $which_ary [ 'email' ]) . '>' ;
2003-10-11 23:51:29 +00:00
$mail_rcpt [ 'to' ][] = '<' . trim ( $which_ary [ 'email' ]) . '>' ;
}
2004-09-05 09:54:48 +00:00
if ( isset ( $addresses [ 'bcc' ]) && sizeof ( $addresses [ 'bcc' ]))
2003-10-11 23:51:29 +00:00
{
2004-09-05 09:54:48 +00:00
foreach ( $addresses [ 'bcc' ] as $which_ary )
{
$mail_rcpt [ 'bcc' ][] = '<' . trim ( $which_ary [ 'email' ]) . '>' ;
}
2003-10-11 23:51:29 +00:00
}
2004-09-05 09:54:48 +00:00
if ( isset ( $addresses [ 'cc' ]) && sizeof ( $addresses [ 'cc' ]))
2003-10-11 23:51:29 +00:00
{
2004-09-05 09:54:48 +00:00
foreach ( $addresses [ 'cc' ] as $which_ary )
{
$mail_cc [] = ( $which_ary [ 'name' ] != '' ) ? mail_encode ( trim ( $which_ary [ 'name' ]), $encoding ) . ' <' . trim ( $which_ary [ 'email' ]) . '>' : '<' . trim ( $which_ary [ 'email' ]) . '>' ;
$mail_rcpt [ 'cc' ][] = '<' . trim ( $which_ary [ 'email' ]) . '>' ;
}
2003-10-11 23:51:29 +00:00
}
2004-01-31 15:49:12 +00:00
$smtp = new smtp_class ;
2006-06-22 15:14:03 +00:00
$errno = 0 ;
$errstr = '' ;
2006-06-06 20:53:46 +00:00
// Ok we have error checked as much as we can to this point let's get on it already.
2006-06-22 15:14:03 +00:00
if ( ! $smtp -> socket = @ fsockopen ( $config [ 'smtp_host' ], $config [ 'smtp_port' ], $errno , $errstr , 20 ))
2003-10-11 23:51:29 +00:00
{
2006-06-06 20:53:46 +00:00
$err_msg = ( isset ( $user -> lang [ 'NO_CONNECT_TO_SMTP_HOST' ])) ? sprintf ( $user -> lang [ 'NO_CONNECT_TO_SMTP_HOST' ], $errno , $errstr ) : " Could not connect to smtp host : $errno : $errstr " ;
2004-01-31 15:49:12 +00:00
return false ;
2003-10-11 23:51:29 +00:00
}
// Wait for reply
2004-01-31 15:49:12 +00:00
if ( $err_msg = $smtp -> server_parse ( '220' , __LINE__ ))
2003-10-11 23:51:29 +00:00
{
2004-02-01 21:45:40 +00:00
$smtp -> close_session ();
2004-01-31 15:49:12 +00:00
return false ;
2003-10-11 23:51:29 +00:00
}
2004-01-31 15:49:12 +00:00
// Let me in. This function handles the complete authentication process
if ( $err_msg = $smtp -> log_into_server ( $config [ 'smtp_host' ], $config [ 'smtp_username' ], $config [ 'smtp_password' ], $config [ 'smtp_auth_method' ]))
2003-10-11 23:51:29 +00:00
{
2004-02-01 21:45:40 +00:00
$smtp -> close_session ();
2004-01-31 15:49:12 +00:00
return false ;
2003-10-11 23:51:29 +00:00
}
// From this point onward most server response codes should be 250
// Specify who the mail is from....
2004-02-01 21:45:40 +00:00
$smtp -> server_send ( 'MAIL FROM:<' . $config [ 'board_email' ] . '>' );
2004-01-31 15:49:12 +00:00
if ( $err_msg = $smtp -> server_parse ( '250' , __LINE__ ))
2003-10-11 23:51:29 +00:00
{
2004-02-01 21:45:40 +00:00
$smtp -> close_session ();
2004-01-31 15:49:12 +00:00
return false ;
2003-10-11 23:51:29 +00:00
}
// Specify each user to send to and build to header.
$to_header = implode ( ', ' , $mail_to );
$cc_header = implode ( ', ' , $mail_cc );
// Now tell the MTA to send the Message to the following people... [TO, BCC, CC]
2004-01-31 19:07:14 +00:00
$rcpt = false ;
2003-10-11 23:51:29 +00:00
foreach ( $mail_rcpt as $type => $mail_to_addresses )
{
foreach ( $mail_to_addresses as $mail_to_address )
{
// Add an additional bit of error checking to the To field.
if ( preg_match ( '#[^ ]+\@[^ ]+#' , $mail_to_address ))
{
2004-02-01 21:45:40 +00:00
$smtp -> server_send ( " RCPT TO: $mail_to_address " );
2004-01-31 15:49:12 +00:00
if ( $err_msg = $smtp -> server_parse ( '250' , __LINE__ ))
2003-10-11 23:51:29 +00:00
{
2004-01-31 19:07:14 +00:00
// We continue... if users are not resolved we do not care
if ( $smtp -> numeric_response_code != 550 )
{
2004-02-01 21:45:40 +00:00
$smtp -> close_session ();
2004-01-31 19:07:14 +00:00
return false ;
}
}
else
{
$rcpt = true ;
2003-10-11 23:51:29 +00:00
}
}
}
}
2004-01-31 19:07:14 +00:00
// We try to send messages even if a few people do not seem to have valid email addresses, but if no one has, we have to exit here.
if ( ! $rcpt )
{
2005-10-02 18:17:06 +00:00
$user -> session_begin ();
2006-06-06 20:53:46 +00:00
$err_msg .= '<br /><br />' ;
$err_msg .= ( isset ( $user -> lang [ 'INVALID_EMAIL_LOG' ])) ? sprintf ( $user -> lang [ 'INVALID_EMAIL_LOG' ], htmlspecialchars ( $mail_to_address )) : '<strong>' . htmlspecialchars ( $mail_to_address ) . '</strong> possibly an invalid email address?' ;
2004-02-01 21:45:40 +00:00
$smtp -> close_session ();
2004-01-31 19:07:14 +00:00
return false ;
}
2003-10-11 23:51:29 +00:00
// Ok now we tell the server we are ready to start sending data
2004-02-01 21:45:40 +00:00
$smtp -> server_send ( 'DATA' );
2003-10-11 23:51:29 +00:00
// This is the last response code we look for until the end of the message.
2004-01-31 15:49:12 +00:00
if ( $err_msg = $smtp -> server_parse ( '354' , __LINE__ ))
2003-10-11 23:51:29 +00:00
{
2004-02-01 21:45:40 +00:00
$smtp -> close_session ();
2004-01-31 15:49:12 +00:00
return false ;
2003-10-11 23:51:29 +00:00
}
// Send the Subject Line...
2004-02-01 21:45:40 +00:00
$smtp -> server_send ( " Subject: $subject " );
2003-10-11 23:51:29 +00:00
// Now the To Header.
$to_header = ( $to_header == '' ) ? 'Undisclosed-Recipients:;' : $to_header ;
2004-02-01 21:45:40 +00:00
$smtp -> server_send ( " To: $to_header " );
2003-10-11 23:51:29 +00:00
// Now the CC Header.
if ( $cc_header != '' )
{
2004-02-01 21:45:40 +00:00
$smtp -> server_send ( " CC: $cc_header " );
2003-10-11 23:51:29 +00:00
}
// Now any custom headers....
2004-02-01 21:45:40 +00:00
$smtp -> server_send ( " $headers\r\n " );
2003-10-11 23:51:29 +00:00
// Ok now we are ready for the message...
2004-02-01 21:45:40 +00:00
$smtp -> server_send ( $message );
2003-10-11 23:51:29 +00:00
// Ok the all the ingredients are mixed in let's cook this puppy...
2004-02-01 21:45:40 +00:00
$smtp -> server_send ( '.' );
2004-01-31 15:49:12 +00:00
if ( $err_msg = $smtp -> server_parse ( '250' , __LINE__ ))
2003-10-11 23:51:29 +00:00
{
2004-02-01 21:45:40 +00:00
$smtp -> close_session ();
2004-01-31 15:49:12 +00:00
return false ;
2003-10-11 23:51:29 +00:00
}
// Now tell the server we are done and close the socket...
2004-02-01 21:45:40 +00:00
$smtp -> server_send ( 'QUIT' );
$smtp -> close_session ();
2003-10-11 23:51:29 +00:00
2004-01-31 15:49:12 +00:00
return true ;
2003-10-11 23:51:29 +00:00
}
2005-04-09 12:26:45 +00:00
/**
* SMTP Class
* Auth Mechanisms originally taken from the AUTH Modules found within the PHP Extension and Application Repository ( PEAR )
* See docs / AUTHORS for more details
2006-06-13 21:06:29 +00:00
* @ package phpBB3
2005-04-09 12:26:45 +00:00
*/
2004-01-31 15:49:12 +00:00
class smtp_class
2003-10-11 23:51:29 +00:00
{
2004-01-31 15:49:12 +00:00
var $server_response = '' ;
var $socket = 0 ;
var $responses = array ();
var $commands = array ();
var $numeric_response_code = 0 ;
2006-06-06 20:53:46 +00:00
/**
* Send command to smtp server
*/
2004-05-26 20:32:51 +00:00
function server_send ( $command )
2004-01-31 15:49:12 +00:00
{
2004-02-01 21:45:40 +00:00
fputs ( $this -> socket , $command . " \r \n " );
2004-05-26 20:32:51 +00:00
// We could put additional code here
2004-01-31 15:49:12 +00:00
}
2006-06-06 20:53:46 +00:00
/**
* We use the line to give the support people an indication at which command the error occurred
*/
2004-01-31 15:49:12 +00:00
function server_parse ( $response , $line )
{
2006-06-06 20:53:46 +00:00
global $user ;
2004-01-31 15:49:12 +00:00
$this -> server_response = '' ;
$this -> responses = array ();
$this -> numeric_response_code = 0 ;
while ( substr ( $this -> server_response , 3 , 1 ) != ' ' )
{
if ( ! ( $this -> server_response = fgets ( $this -> socket , 256 )))
{
2006-06-06 20:53:46 +00:00
return ( isset ( $user -> lang [ 'NO_EMAIL_RESPONSE_CODE' ])) ? $user -> lang [ 'NO_EMAIL_RESPONSE_CODE' ] : 'Could not get mail server response codes' ;
2004-01-31 15:49:12 +00:00
}
$this -> responses [] = substr ( rtrim ( $this -> server_response ), 4 );
$this -> numeric_response_code = ( int ) substr ( $this -> server_response , 0 , 3 );
}
if ( ! ( substr ( $this -> server_response , 0 , 3 ) == $response ))
{
$this -> numeric_response_code = ( int ) substr ( $this -> server_response , 0 , 3 );
2006-06-06 20:53:46 +00:00
return ( isset ( $user -> lang [ 'EMAIL_SMTP_ERROR_RESPONSE' ])) ? sprintf ( $user -> lang [ 'EMAIL_SMTP_ERROR_RESPONSE' ], $line , $this -> server_response ) : " Ran into problems sending Mail at <strong>Line $line </strong>. Response: $this->server_response " ;
2004-01-31 15:49:12 +00:00
}
return 0 ;
}
2006-06-06 20:53:46 +00:00
/**
* Close session
*/
2004-02-01 21:45:40 +00:00
function close_session ()
{
fclose ( $this -> socket );
}
2006-06-06 20:53:46 +00:00
/**
* Log into server and get possible auth codes if neccessary
*/
2004-01-31 15:49:12 +00:00
function log_into_server ( $hostname , $username , $password , $default_auth_method )
{
2006-04-29 13:14:33 +00:00
global $user ;
2004-01-31 15:49:12 +00:00
$err_msg = '' ;
2006-04-30 12:50:43 +00:00
$local_host = php_uname ( 'n' );
$local_host = ( empty ( $local_host )) ? 'localhost' : $local_host ;
2004-01-31 15:49:12 +00:00
// If we are authenticating through pop-before-smtp, we
// have to login ones before we get authenticated
if ( $default_auth_method == 'POP-BEFORE-SMTP' && $username && $password )
{
$result = $this -> pop_before_smtp ( $hostname , $username , $password );
$username = $password = $default_auth_method = '' ;
}
// Try EHLO first
2006-06-11 18:13:52 +00:00
$this -> server_send ( " EHLO { $local_host } " );
2004-01-31 15:49:12 +00:00
if ( $err_msg = $this -> server_parse ( '250' , __LINE__ ))
{
// a 503 response code means that we're already authenticated
if ( $this -> numeric_response_code == 503 )
{
return false ;
}
// If EHLO fails, we try HELO
2006-06-11 18:13:52 +00:00
$this -> server_send ( " HELO { $local_host } " );
2004-01-31 15:49:12 +00:00
if ( $err_msg = $this -> server_parse ( '250' , __LINE__ ))
{
return ( $this -> numeric_response_code == 503 ) ? false : $err_msg ;
}
}
foreach ( $this -> responses as $response )
{
$response = explode ( ' ' , $response );
$response_code = $response [ 0 ];
unset ( $response [ 0 ]);
$this -> commands [ $response_code ] = implode ( ' ' , $response );
2004-01-31 19:07:14 +00:00
}
2004-01-31 15:49:12 +00:00
// If we are not authenticated yet, something might be wrong if no username and passwd passed
if ( ! $username || ! $password )
{
return false ;
}
if ( ! isset ( $this -> commands [ 'AUTH' ]))
{
2006-06-06 20:53:46 +00:00
return ( isset ( $user -> lang [ 'SMTP_NO_AUTH_SUPPORT' ])) ? $user -> lang [ 'SMTP_NO_AUTH_SUPPORT' ] : 'SMTP server does not support authentication' ;
2004-01-31 15:49:12 +00:00
}
// Get best authentication method
2004-01-31 19:07:14 +00:00
$available_methods = explode ( ' ' , $this -> commands [ 'AUTH' ]);
2004-01-31 15:49:12 +00:00
// Define the auth ordering if the default auth method was not found
2006-04-30 12:19:11 +00:00
$auth_methods = array ( 'PLAIN' , 'LOGIN' , 'CRAM-MD5' , 'DIGEST-MD5' );
2004-01-31 15:49:12 +00:00
$method = '' ;
if ( in_array ( $default_auth_method , $available_methods ))
{
$method = $default_auth_method ;
}
else
{
foreach ( $auth_methods as $_method )
{
if ( in_array ( $_method , $available_methods ))
{
$method = $_method ;
break ;
}
}
2004-01-31 19:07:14 +00:00
}
2004-01-31 15:49:12 +00:00
if ( ! $method )
{
2006-06-06 20:53:46 +00:00
return ( isset ( $user -> lang [ 'NO_SUPPORTED_AUTH_METHODS' ])) ? $user -> lang [ 'NO_SUPPORTED_AUTH_METHODS' ] : 'No supported authentication methods' ;
2004-01-31 15:49:12 +00:00
}
$method = strtolower ( str_replace ( '-' , '_' , $method ));
return $this -> $method ( $username , $password );
}
2006-06-06 20:53:46 +00:00
/**
* Pop before smtp authentication
*/
2004-01-31 15:49:12 +00:00
function pop_before_smtp ( $hostname , $username , $password )
2003-10-11 23:51:29 +00:00
{
2006-06-06 20:53:46 +00:00
global $user ;
2004-01-31 15:49:12 +00:00
$old_socket = $this -> socket ;
2006-06-06 20:53:46 +00:00
2004-01-31 15:49:12 +00:00
if ( ! $this -> socket = fsockopen ( $hostname , 110 , $errno , $errstr , 20 ))
{
$this -> socket = $old_socket ;
2006-06-06 20:53:46 +00:00
return ( isset ( $user -> lang [ 'NO_CONNECT_TO_SMTP_HOST' ])) ? sprintf ( $user -> lang [ 'NO_CONNECT_TO_SMTP_HOST' ], $errno , $errstr ) : " Could not connect to smtp host : $errno : $errstr " ;
2004-01-31 15:49:12 +00:00
}
2006-06-06 20:53:46 +00:00
2004-01-31 15:49:12 +00:00
$this -> server_parse ( '0' , __LINE__ );
if ( substr ( $this -> server_response , 0 , 3 ) == '+OK' )
2003-10-11 23:51:29 +00:00
{
2004-02-01 21:45:40 +00:00
fputs ( $this -> socket , " USER $username\r\n " );
fputs ( $this -> socket , " PASS $password\r\n " );
2003-10-11 23:51:29 +00:00
}
2004-01-31 15:49:12 +00:00
else
{
$this -> socket = $old_socket ;
return $this -> responses [ 0 ];
}
2004-02-01 21:45:40 +00:00
$this -> server_send ( 'QUIT' );
2004-01-31 15:49:12 +00:00
$this -> server_parse ( '0' , __LINE__ );
fclose ( $this -> socket );
$this -> socket = $old_socket ;
return false ;
2003-10-11 23:51:29 +00:00
}
2006-06-06 20:53:46 +00:00
/**
* Plain authentication method
*/
2004-01-31 15:49:12 +00:00
function plain ( $username , $password )
{
2004-02-01 21:45:40 +00:00
$this -> server_send ( 'AUTH PLAIN' );
2004-01-31 15:49:12 +00:00
if ( $err_msg = $this -> server_parse ( '334' , __LINE__ ))
{
return ( $this -> numeric_response_code == 503 ) ? false : $err_msg ;
}
2003-10-11 23:51:29 +00:00
2004-01-31 15:49:12 +00:00
$base64_method_plain = base64_encode ( " \0 " . $username . " \0 " . $password );
2004-05-26 20:32:51 +00:00
$this -> server_send ( $base64_method_plain );
2004-01-31 15:49:12 +00:00
if ( $err_msg = $this -> server_parse ( '235' , __LINE__ ))
{
return $err_msg ;
}
return false ;
}
2006-06-06 20:53:46 +00:00
/**
* Login authentication method
*/
2004-01-31 15:49:12 +00:00
function login ( $username , $password )
{
2004-02-01 21:45:40 +00:00
$this -> server_send ( 'AUTH LOGIN' );
2004-01-31 15:49:12 +00:00
if ( $err_msg = $this -> server_parse ( '334' , __LINE__ ))
{
return ( $this -> numeric_response_code == 503 ) ? false : $err_msg ;
}
2004-05-26 20:32:51 +00:00
$this -> server_send ( base64_encode ( $username ));
2004-01-31 15:49:12 +00:00
if ( $err_msg = $this -> server_parse ( '334' , __LINE__ ))
{
return $err_msg ;
}
2004-05-26 20:32:51 +00:00
$this -> server_send ( base64_encode ( $password ));
2004-01-31 15:49:12 +00:00
if ( $err_msg = $this -> server_parse ( '235' , __LINE__ ))
{
return $err_msg ;
}
return false ;
}
2006-06-06 20:53:46 +00:00
/**
* cram_md5 authentication method
*/
2004-01-31 15:49:12 +00:00
function cram_md5 ( $username , $password )
2003-10-11 23:51:29 +00:00
{
2004-02-01 21:45:40 +00:00
$this -> server_send ( 'AUTH CRAM-MD5' );
2004-01-31 15:49:12 +00:00
if ( $err_msg = $this -> server_parse ( '334' , __LINE__ ))
{
return ( $this -> numeric_response_code == 503 ) ? false : $err_msg ;
}
$md5_challenge = base64_decode ( $this -> responses [ 0 ]);
$password = ( strlen ( $password ) > 64 ) ? pack ( 'H32' , md5 ( $password )) : (( strlen ( $password ) < 64 ) ? str_pad ( $password , 64 , chr ( 0 )) : $password );
$md5_digest = md5 (( substr ( $password , 0 , 64 ) ^ str_repeat ( chr ( 0x5C ), 64 )) . ( pack ( 'H32' , md5 (( substr ( $password , 0 , 64 ) ^ str_repeat ( chr ( 0x36 ), 64 )) . $md5_challenge ))));
$base64_method_cram_md5 = base64_encode ( $username . ' ' . $md5_digest );
2004-05-26 20:32:51 +00:00
$this -> server_send ( $base64_method_cram_md5 );
2004-01-31 15:49:12 +00:00
if ( $err_msg = $this -> server_parse ( '235' , __LINE__ ))
{
return $err_msg ;
}
return false ;
2003-10-11 23:51:29 +00:00
}
2006-06-06 20:53:46 +00:00
/**
* digest_md5 authentication method
* A real pain in the ***
*/
2004-01-31 15:49:12 +00:00
function digest_md5 ( $username , $password )
{
2006-06-06 20:53:46 +00:00
global $config , $user ;
2004-01-31 15:49:12 +00:00
2004-02-01 21:45:40 +00:00
$this -> server_send ( 'AUTH DIGEST-MD5' );
2004-01-31 15:49:12 +00:00
if ( $err_msg = $this -> server_parse ( '334' , __LINE__ ))
{
return ( $this -> numeric_response_code == 503 ) ? false : $err_msg ;
}
$md5_challenge = base64_decode ( $this -> responses [ 0 ]);
2004-02-21 12:47:35 +00:00
// Parse the md5 challenge - from AUTH_SASL (PEAR)
2004-01-31 15:49:12 +00:00
$tokens = array ();
while ( preg_match ( '/^([a-z-]+)=("[^"]+(?<!\\\)"|[^,]+)/i' , $md5_challenge , $matches ))
{
// Ignore these as per rfc2831
if ( $matches [ 1 ] == 'opaque' || $matches [ 1 ] == 'domain' )
{
$md5_challenge = substr ( $md5_challenge , strlen ( $matches [ 0 ]) + 1 );
continue ;
}
// Allowed multiple "realm" and "auth-param"
if ( ! empty ( $tokens [ $matches [ 1 ]]) && ( $matches [ 1 ] == 'realm' || $matches [ 1 ] == 'auth-param' ))
{
if ( is_array ( $tokens [ $matches [ 1 ]]))
{
$tokens [ $matches [ 1 ]][] = preg_replace ( '/^"(.*)"$/' , '\\1' , $matches [ 2 ]);
}
else
{
2004-01-31 19:07:14 +00:00
$tokens [ $matches [ 1 ]] = array ( $tokens [ $matches [ 1 ]], preg_replace ( '/^"(.*)"$/' , '\\1' , $matches [ 2 ]));
}
2004-01-31 15:49:12 +00:00
}
else if ( ! empty ( $tokens [ $matches [ 1 ]])) // Any other multiple instance = failure
{
$tokens = array ();
break ;
}
else
{
$tokens [ $matches [ 1 ]] = preg_replace ( '/^"(.*)"$/' , '\\1' , $matches [ 2 ]);
}
// Remove the just parsed directive from the challenge
$md5_challenge = substr ( $md5_challenge , strlen ( $matches [ 0 ]) + 1 );
}
// Realm
if ( empty ( $tokens [ 'realm' ]))
{
2006-04-30 12:19:11 +00:00
$tokens [ 'realm' ] = php_uname ( 'n' );
2004-01-31 15:49:12 +00:00
}
2006-04-29 13:14:33 +00:00
2004-01-31 15:49:12 +00:00
// Maxbuf
if ( empty ( $tokens [ 'maxbuf' ]))
{
$tokens [ 'maxbuf' ] = 65536 ;
}
2004-01-31 19:07:14 +00:00
2004-01-31 15:49:12 +00:00
// Required: nonce, algorithm
if ( empty ( $tokens [ 'nonce' ]) || empty ( $tokens [ 'algorithm' ]))
{
2004-01-31 19:07:14 +00:00
$tokens = array ();
}
$md5_challenge = $tokens ;
2004-01-31 15:49:12 +00:00
if ( ! empty ( $md5_challenge ))
{
$str = '' ;
for ( $i = 0 ; $i < 32 ; $i ++ )
{
$str .= chr ( mt_rand ( 0 , 255 ));
2004-01-31 19:07:14 +00:00
}
$cnonce = base64_encode ( $str );
2004-01-31 15:49:12 +00:00
2004-01-31 19:07:14 +00:00
$digest_uri = 'smtp/' . $config [ 'smtp_host' ];
2004-01-31 15:49:12 +00:00
$auth_1 = sprintf ( '%s:%s:%s' , pack ( 'H32' , md5 ( sprintf ( '%s:%s:%s' , $username , $md5_challenge [ 'realm' ], $password ))), $md5_challenge [ 'nonce' ], $cnonce );
$auth_2 = 'AUTHENTICATE:' . $digest_uri ;
$response_value = md5 ( sprintf ( '%s:%s:00000001:%s:auth:%s' , md5 ( $auth_1 ), $md5_challenge [ 'nonce' ], $cnonce , md5 ( $auth_2 )));
$input_string = sprintf ( 'username="%s",realm="%s",nonce="%s",cnonce="%s",nc="00000001",qop=auth,digest-uri="%s",response=%s,%d' , $username , $md5_challenge [ 'realm' ], $md5_challenge [ 'nonce' ], $cnonce , $digest_uri , $response_value , $md5_challenge [ 'maxbuf' ]);
2004-01-31 19:07:14 +00:00
}
2004-01-31 15:49:12 +00:00
else
{
2006-06-06 20:53:46 +00:00
return ( isset ( $user -> lang [ 'INVALID_DIGEST_CHALLENGE' ])) ? $user -> lang [ 'INVALID_DIGEST_CHALLENGE' ] : 'Invalid digest challenge' ;
2004-01-31 19:07:14 +00:00
}
2006-06-06 20:53:46 +00:00
2004-01-31 15:49:12 +00:00
$base64_method_digest_md5 = base64_encode ( $input_string );
2004-05-26 20:32:51 +00:00
$this -> server_send ( $base64_method_digest_md5 );
2004-01-31 15:49:12 +00:00
if ( $err_msg = $this -> server_parse ( '334' , __LINE__ ))
{
return $err_msg ;
}
2004-02-01 21:45:40 +00:00
$this -> server_send ( ' ' );
2004-01-31 15:49:12 +00:00
if ( $err_msg = $this -> server_parse ( '235' , __LINE__ ))
{
return $err_msg ;
}
2006-06-06 20:53:46 +00:00
2004-01-31 15:49:12 +00:00
return false ;
}
2003-10-11 23:51:29 +00:00
}
2005-04-09 12:26:45 +00:00
/**
* Encodes the given string for proper display for this encoding ... nabbed
* from php . net and modified . There is an alternative encoding method which
* may produce less output but it ' s questionable as to its worth in this
* scenario IMO
*/
2004-05-26 20:32:51 +00:00
function mail_encode ( $str , $encoding )
2003-10-12 11:59:23 +00:00
{
2004-05-26 20:32:51 +00:00
if ( $encoding == '' )
2003-10-12 11:59:23 +00:00
{
return $str ;
}
// define start delimimter, end delimiter and spacer
$end = " ?= " ;
2004-05-31 18:00:10 +00:00
$start = " =? $encoding ?B? " ;
2003-10-12 11:59:23 +00:00
$spacer = " $end\r\n $start " ;
// determine length of encoded text within chunks and ensure length is even
$length = 75 - strlen ( $start ) - strlen ( $end );
$length = floor ( $length / 2 ) * 2 ;
// encode the string and split it into chunks with spacers after each chunk
$str = chunk_split ( base64_encode ( $str ), $length , $spacer );
// remove trailing spacer and add start and end delimiters
2006-04-27 14:20:43 +00:00
$str = preg_replace ( '#' . preg_quote ( $spacer , '#' ) . '$#' , '' , $str );
2003-10-12 11:59:23 +00:00
return $start . $str . $end ;
}
2003-10-11 23:51:29 +00:00
?>