Changeset 292 for branches/1.1dev/lib


Ignore:
Timestamp:
Dec 9, 2007 1:14:19 AM (16 years ago)
Author:
quinn
Message:

Back-ported the Email class to codebase v1.1dev.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • branches/1.1dev/lib/Email.inc.php

    r291 r292  
    77 *
    88 * @author  Quinn Comendant <quinn@strangecode.com>
    9  * @version 1.0
     9 * @version 1.0 - backported to codebase v1.1dev
    1010 *
    1111 
     
    1313$email = new Email(array(
    1414    'to' => array($frm['email'], 'q@lovemachine.local'),
    15     'from' => sprintf('%s <%s>', $app->getParam('site_name'), $app->getParam('site_email')),
     15    'from' => sprintf('%s <%s>', $CFG->site_name, $CFG->site_email),
    1616    'subject' => 'Your account has been activated',
    1717));
     
    1919// $email->setString('Or you can pass your message body as a string, also with {VARIABLES}.');
    2020$email->replace(array(
    21     'site_name' => $app->getParam('site_name'),
    22     'site_url' => $app->getParam('site_url'),
    23     'username' => $frm['username'],
    24     'password' => $frm['password1'],
     21    'SITE_NAME' => $CFG->site_name,
     22    'SITE_URL' => $CFG->site_url,
     23    'USERNAME' => $frm['username'],
     24    'PASSWORD' => $frm['password1'],
    2525));
    2626if ($email->send()) {
    27     $app->raiseMsg(sprintf(_("A confirmation email has been sent to %s."), $frm['email']), MSG_SUCCESS, __FILE__, __LINE__);
     27    raiseMsg(sprintf(_("A confirmation email has been sent to %s."), $frm['email']), MSG_SUCCESS, __FILE__, __LINE__);
    2828} else {
    29     $app->logMsg(sprintf('Error sending confirmation email to address %s', $frm['email']), LOG_NOTICE, __FILE__, __LINE__);
     29    logMsg(sprintf('Error sending confirmation email to address %s', $frm['email']), LOG_NOTICE, __FILE__, __LINE__);
    3030}
    3131
     
    9999    function setParam($params)
    100100    {
    101         $app =& App::getInstance();
    102    
    103101        if (isset($params) && is_array($params)) {
    104102            // Enforce valid email addresses.
     
    113111            $this->_params = array_merge($this->_params, $params);
    114112        } else {
    115             $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__);
     113            logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__);
    116114        }
    117115    }
     
    126124    function getParam($param)
    127125    {
    128         $app =& App::getInstance();
    129    
    130126        if (isset($this->_params[$param])) {
    131127            return $this->_params[$param];
    132128        } else {
    133             $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
     129            logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
    134130            return null;
    135131        }
     
    146142    function setTemplate($template)
    147143    {
    148         $app =& App::getInstance();
    149    
    150144        // Load file, using include_path.
    151145        if (!$this->_template = file_get_contents($template, true)) {
    152             $app->logMsg(sprintf('Email template file does not exist: %s', $template), LOG_ERR, __FILE__, __LINE__);
     146            logMsg(sprintf('Email template file does not exist: %s', $template), LOG_ERR, __FILE__, __LINE__);
    153147            $this->_template = null;
    154148            $this->_template_replaced = null;
     
    170164    function setString($string)
    171165    {
    172         $app =& App::getInstance();
    173    
    174166        // Load file, using include_path.
    175167        if ('' == trim($string)) {
    176             $app->logMsg(sprintf('Empty string provided.', null), LOG_ERR, __FILE__, __LINE__);
     168            logMsg(sprintf('Empty string provided.', null), LOG_ERR, __FILE__, __LINE__);
    177169            $this->_template_replaced = null;
    178170            return false;
     
    195187    function replace($replacements)
    196188    {
    197         $app =& App::getInstance();
    198189   
    199190        // Ensure template exists.
    200191        if (!isset($this->_template)) {
    201             $app->logMsg(sprintf('Cannot replace variables, no template defined.', null), LOG_ERR, __FILE__, __LINE__);
     192            logMsg(sprintf('Cannot replace variables, no template defined.', null), LOG_ERR, __FILE__, __LINE__);
    202193            return false;
    203194        }
     
    205196        // Ensure replacements argument is an array.
    206197        if (!is_array($replacements)) {
    207             $app->logMsg(sprintf('Cannot replace variables, invalid replacements.', null), LOG_ERR, __FILE__, __LINE__);
     198            logMsg(sprintf('Cannot replace variables, invalid replacements.', null), LOG_ERR, __FILE__, __LINE__);
    208199            return false;
    209200        }
     
    232223    function send($to=null, $from=null, $subject=null, $headers=null)
    233224    {
    234         $app =& App::getInstance();
    235 
    236225        // Use arguments if provided.
    237226        if (isset($to)) {
     
    250239        // Ensure required values exist.
    251240        if (!isset($this->_params['subject'])) {
    252             $app->logMsg(sprintf('Cannot send email to %s. SUBJECT not defined.', $this->_params['to']), LOG_ERR, __FILE__, __LINE__);
     241            logMsg(sprintf('Cannot send email to %s. SUBJECT not defined.', $this->_params['to']), LOG_ERR, __FILE__, __LINE__);
    253242            return false;
    254243        } else if (!isset($this->_template)) {
    255             $app->logMsg(sprintf('Cannot send email: "%s". Template not set.', $this->_params['subject']), LOG_ERR, __FILE__, __LINE__);
     244            logMsg(sprintf('Cannot send email: "%s". Template not set.', $this->_params['subject']), LOG_ERR, __FILE__, __LINE__);
    256245            return false;
    257246        } else if (!isset($this->_params['to'])) {
    258             $app->logMsg(sprintf('Cannot send email: "%s". TO not defined.', $this->_params['subject']), LOG_NOTICE, __FILE__, __LINE__);
     247            logMsg(sprintf('Cannot send email: "%s". TO not defined.', $this->_params['subject']), LOG_NOTICE, __FILE__, __LINE__);
    259248            return false;
    260249        } else if (!isset($this->_params['from'])) {
    261             $app->logMsg(sprintf('Cannot send email: "%s". FROM not defined.', $this->_params['subject']), LOG_ERR, __FILE__, __LINE__);
     250            logMsg(sprintf('Cannot send email: "%s". FROM not defined.', $this->_params['subject']), LOG_ERR, __FILE__, __LINE__);
    262251            return false;
    263252        }
     
    271260        // Ensure all placeholders have been replaced. Find anything with {...} characters.
    272261        if (preg_match('/({[^}]+})/', $final_body, $unreplaced_match)) {
    273             $app->logMsg(sprintf('Cannot send email. Variables left unreplaced in template: %s', (isset($unreplaced_match[1]) ? $unreplaced_match[1] : '')), LOG_ERR, __FILE__, __LINE__);
     262            logMsg(sprintf('Cannot send email. Variables left unreplaced in template: %s', (isset($unreplaced_match[1]) ? $unreplaced_match[1] : '')), LOG_ERR, __FILE__, __LINE__);
    274263            return false;
    275264        }
     
    304293        $full_mail_content = join($this->getParam('crlf'), array($final_to, $this->_params['subject'], $final_body));
    305294        if (preg_match("/(^|[\n\r])(Content-Type|MIME-Version|Content-Transfer-Encoding|Bcc|Cc):/i", $full_mail_content)) {
    306             $app->logMsg(sprintf('Mail header injection attack in content: %s', $full_mail_content), LOG_WARNING, __FILE__, __LINE__);
     295            logMsg(sprintf('Mail header injection attack in content: %s', $full_mail_content), LOG_WARNING, __FILE__, __LINE__);
    307296            sleep(3);
    308297            return false;
     
    311300        // Ensure message was successfully accepted for delivery.
    312301        if (mb_send_mail($final_to, $this->_params['subject'], $final_body, $final_headers, $envelope_sender_header)) {
    313             $app->logMsg(sprintf('Email successfully sent to %s', $final_to), LOG_INFO, __FILE__, __LINE__);
     302            logMsg(sprintf('Email successfully sent to %s', $final_to), LOG_INFO, __FILE__, __LINE__);
    314303            return true;
    315304        } else {
    316             $app->logMsg(sprintf('Email failure with parameters: %s, %s, %s, %s', $final_to, $this->_params['subject'], str_replace("\r\n", '\r\n', $final_headers), $envelope_sender_header), LOG_NOTICE, __FILE__, __LINE__);
     305            logMsg(sprintf('Email failure with parameters: %s, %s, %s, %s', $final_to, $this->_params['subject'], str_replace("\r\n", '\r\n', $final_headers), $envelope_sender_header), LOG_NOTICE, __FILE__, __LINE__);
    317306            return false;
    318307        }
     
    336325    function validEmail($email)
    337326    {
    338         $app =& App::getInstance();
    339    
    340327        // If an array, check values recursively.
    341328        if (is_array($email)) {
     
    351338                return true;
    352339            } else {
    353                 $app->logMsg(sprintf('Invalid email: %s', $email), LOG_INFO, __FILE__, __LINE__);
     340                logMsg(sprintf('Invalid email: %s', $email), LOG_INFO, __FILE__, __LINE__);
    354341                return false;
    355342            }
Note: See TracChangeset for help on using the changeset viewer.