Ignore:
Timestamp:
Jun 3, 2006 7:47:48 PM (18 years ago)
Author:
scdev
Message:

Q - Merged branches/2.0singleton into trunk. Completed updating classes to use singleton methods. Implemented tests. Fixed some bugs. Changed some interfaces.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Email.inc.php

    r119 r136  
    88 * @author  Quinn Comendant <quinn@strangecode.com>
    99 * @version 1.0
    10 -------------------------------------------------------------------------------------
     10 *
     11 
    1112// Example.
    1213$email = new Email(array(
    1314    'to' => array($frm['email'], 'q@lovemachine.local'),
    14     'from' => sprintf('%s <%s>', App::getParam('site_name'), App::getParam('site_email')),
     15    'from' => sprintf('%s <%s>', $app->getParam('site_name'), $app->getParam('site_email')),
    1516    'subject' => 'Your account has been activated',
    1617));
     
    1819// $email->setString('Or you can pass your message body as a string, also with {VARIABLES}.');
    1920$email->replace(array(
    20     'site_name' => App::getParam('site_name'),
    21     'site_url' => App::getParam('site_url'),
     21    'site_name' => $app->getParam('site_name'),
     22    'site_url' => $app->getParam('site_url'),
    2223    'username' => $frm['username'],
    2324    'password' => $frm['password1'],
    2425));
    2526if ($email->send()) {
    26     App::raiseMsg(sprintf(_("A confirmation email has been sent to %s."), $frm['email']), MSG_SUCCESS, __FILE__, __LINE__);
     27    $app->raiseMsg(sprintf(_("A confirmation email has been sent to %s."), $frm['email']), MSG_SUCCESS, __FILE__, __LINE__);
    2728} else {
    28     App::logMsg(sprintf('Error sending confirmation email to address %s', $frm['email']), LOG_NOTICE, __FILE__, __LINE__);
     29    $app->logMsg(sprintf('Error sending confirmation email to address %s', $frm['email']), LOG_NOTICE, __FILE__, __LINE__);
    2930}
    30 -------------------------------------------------------------------------------------
     31
     32 *
    3133 */
    3234class Email {
     
    8890    function setParam($params)
    8991    {
     92        $app =& App::getInstance();
     93   
    9094        if (isset($params) && is_array($params)) {
    9195            // Enforce valid email addresses.
     
    100104            $this->_params = array_merge($this->_params, $params);
    101105        } else {
    102             App::logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__);
     106            $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__);
    103107        }
    104108    }
     
    113117    function getParam($param)
    114118    {
     119        $app =& App::getInstance();
     120   
    115121        if (isset($this->_params[$param])) {
    116122            return $this->_params[$param];
    117123        } else {
    118             App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_NOTICE, __FILE__, __LINE__);
     124            $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_NOTICE, __FILE__, __LINE__);
    119125            return null;
    120126        }
     
    131137    function setTemplate($template)
    132138    {
     139        $app =& App::getInstance();
     140   
    133141        // Load file, using include_path.
    134142        if (!$this->_template = file_get_contents($template, true)) {
    135             App::logMsg(sprintf('Email template file does not exist: %s', $template), LOG_ERR, __FILE__, __LINE__);
     143            $app->logMsg(sprintf('Email template file does not exist: %s', $template), LOG_ERR, __FILE__, __LINE__);
    136144            $this->_template = null;
    137145            $this->_template_replaced = null;
     
    153161    function setString($string)
    154162    {
     163        $app =& App::getInstance();
     164   
    155165        // Load file, using include_path.
    156166        if ('' == trim($string)) {
    157             App::logMsg(sprintf('Empty string provided.', null), LOG_ERR, __FILE__, __LINE__);
     167            $app->logMsg(sprintf('Empty string provided.', null), LOG_ERR, __FILE__, __LINE__);
    158168            $this->_template_replaced = null;
    159169            return false;
     
    176186    function replace($replacements)
    177187    {
     188        $app =& App::getInstance();
     189   
    178190        // Ensure template exists.
    179191        if (!isset($this->_template)) {
    180             App::logMsg(sprintf('Cannot replace variables, no template defined.', null), LOG_ERR, __FILE__, __LINE__);
     192            $app->logMsg(sprintf('Cannot replace variables, no template defined.', null), LOG_ERR, __FILE__, __LINE__);
    181193            return false;
    182194        }
     
    184196        // Ensure replacements argument is an array.
    185197        if (!is_array($replacements)) {
    186             App::logMsg(sprintf('Cannot replace variables, invalid replacements.', null), LOG_ERR, __FILE__, __LINE__);
     198            $app->logMsg(sprintf('Cannot replace variables, invalid replacements.', null), LOG_ERR, __FILE__, __LINE__);
    187199            return false;
    188200        }
     
    211223    function send($to=null, $from=null, $subject=null, $headers=null)
    212224    {
     225        $app =& App::getInstance();
     226   
    213227        // Use arguments if provided.
    214228        if (isset($to)) {
     
    227241        // Ensure required values exist.
    228242        if (!isset($this->_params['subject'])) {
    229             App::logMsg(sprintf('Cannot send email to %s. SUBJECT not defined.', $this->_params['to']), LOG_ERR, __FILE__, __LINE__);
     243            $app->logMsg(sprintf('Cannot send email to %s. SUBJECT not defined.', $this->_params['to']), LOG_ERR, __FILE__, __LINE__);
    230244            return false;
    231245        } else if (!isset($this->_template)) {
    232             App::logMsg(sprintf('Cannot send email: "%s". Template not set.', $this->_params['subject']), LOG_ERR, __FILE__, __LINE__);
     246            $app->logMsg(sprintf('Cannot send email: "%s". Template not set.', $this->_params['subject']), LOG_ERR, __FILE__, __LINE__);
    233247            return false;
    234248        } else if (!isset($this->_params['to'])) {
    235             App::logMsg(sprintf('Cannot send email: "%s". TO not defined.', $this->_params['subject']), LOG_NOTICE, __FILE__, __LINE__);
     249            $app->logMsg(sprintf('Cannot send email: "%s". TO not defined.', $this->_params['subject']), LOG_NOTICE, __FILE__, __LINE__);
    236250            return false;
    237251        } else if (!isset($this->_params['from'])) {
    238             App::logMsg(sprintf('Cannot send email: "%s". FROM not defined.', $this->_params['subject']), LOG_ERR, __FILE__, __LINE__);
     252            $app->logMsg(sprintf('Cannot send email: "%s". FROM not defined.', $this->_params['subject']), LOG_ERR, __FILE__, __LINE__);
    239253            return false;
    240254        }
     
    245259        // Ensure all placeholders have been replaced. Find anything with {...} characters.
    246260        if (preg_match('/({[^}]+})/', $final_body, $unreplaced_match)) {
    247             App::logMsg(sprintf('Cannot send email. Variables left unreplaced in template: %s', (isset($unreplaced_match[1]) ? $unreplaced_match[1] : '')), LOG_ERR, __FILE__, __LINE__);
     261            $app->logMsg(sprintf('Cannot send email. Variables left unreplaced in template: %s', (isset($unreplaced_match[1]) ? $unreplaced_match[1] : '')), LOG_ERR, __FILE__, __LINE__);
    248262            return false;
    249263        }
     
    273287        $full_mail_content = join("\n", array($final_to, $this->_params['subject'], $final_body, $final_headers, $envelope_sender_header));
    274288        if (preg_match("/(Content-Type:|MIME-Version:|Content-Transfer-Encoding:|[\n\r]Bcc:|[\n\r]Cc:)/i", $full_mail_content)) {
    275             App::logMsg(sprintf('Mail header injection attack in content: %s', $full_mail_content), LOG_WARNING, __FILE__, __LINE__);
     289            $app->logMsg(sprintf('Mail header injection attack in content: %s', $full_mail_content), LOG_WARNING, __FILE__, __LINE__);
    276290            sleep(3);
    277291            return false;
     
    280294        // Ensure message was successfully accepted for delivery.
    281295        if (mail($final_to, $this->_params['subject'], $final_body, $final_headers, $envelope_sender_header)) {
    282             App::logMsg(sprintf('Email successfully sent to %s', $final_to), LOG_DEBUG, __FILE__, __LINE__);
     296            $app->logMsg(sprintf('Email successfully sent to %s', $final_to), LOG_DEBUG, __FILE__, __LINE__);
    283297            return true;
    284298        } else {
    285             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__);
     299            $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__);
    286300            return false;
    287301        }
     
    305319    function validEmail($email)
    306320    {
     321        $app =& App::getInstance();
     322   
    307323        // If an array, check values recursively.
    308324        if (is_array($email)) {
     
    318334                return true;
    319335            } else {
    320                 App::logMsg(sprintf('Invalid email: %s', $email), LOG_INFO, __FILE__, __LINE__);
     336                $app->logMsg(sprintf('Invalid email: %s', $email), LOG_INFO, __FILE__, __LINE__);
    321337                return false;
    322338            }
Note: See TracChangeset for help on using the changeset viewer.