Changeset 42 for trunk/lib/Email.inc.php


Ignore:
Timestamp:
Dec 18, 2005 12:16:03 AM (18 years ago)
Author:
scdev
Message:

detabbed all files ;P

File:
1 edited

Legend:

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

    r41 r42  
    4040        'regex' => null
    4141    );
    42    
     42
    4343    // String that contains the email body.
    4444    var $_template;
    45    
     45
    4646    // String that contains the email body after replacements.
    4747    var $_template_replaced;
     
    5858    {
    5959        // The regex used in validEmail(). Set here instead of in the default _params above so we can use the concatination . dot.
    60         // This matches an email address as complex as: 
     60        // This matches an email address as complex as:
    6161        //      Bob Smith <bob&smith's/dep=sales!@smith-wick.ca.us> (Sales department)
    6262        // ...and something as simple as:
    6363        //      x@x.com
    6464        $this->setParam(array('regex' => '/^(?:[^,@]*\s+|[^,@]*(<)|)'   // Display name
    65         . '((?:[^.<>\s@\",\[\]]+[^<>\s@\",\[\]])*[^.<>\s@\",\[\]]+)'    // Local-part 
     65        . '((?:[^.<>\s@\",\[\]]+[^<>\s@\",\[\]])*[^.<>\s@\",\[\]]+)'    // Local-part
    6666        . '@'                                                           // @
    6767        . '((?:(\[)|[A-Z0-9]?)'                                         // Domain, first char
     
    165165        }
    166166    }
    167    
     167
    168168    /**
    169169     * Replace variables in template with argument data.
     
    179179        if (!isset($this->_template)) {
    180180            App::logMsg(sprintf('Cannot replace variables, no template defined.', null), LOG_ERR, __FILE__, __LINE__);
    181             return false;
    182         }
    183        
     181            return false;
     182        }
     183
    184184        // Ensure replacements argument is an array.
    185185        if (!is_array($replacements)) {
    186186            App::logMsg(sprintf('Cannot replace variables, invalid replacements.', null), LOG_ERR, __FILE__, __LINE__);
    187             return false;
    188         }
    189        
     187            return false;
     188        }
     189
    190190        // Apply regex pattern to search elements.
    191191        $search = array_keys($replacements);
     
    194194        // Replacement values.
    195195        $replace = array_values($replacements);
    196        
     196
    197197        // Search and replace all values at once.
    198198        $this->_template_replaced = preg_replace($search, $replace, $this->_template);
     
    213213        // Use arguments if provided.
    214214        if (isset($to)) {
    215             $this->setParam(array('to' => $to));
     215            $this->setParam(array('to' => $to));
    216216        }
    217217        if (isset($from)) {
    218             $this->setParam(array('from' => $from));
     218            $this->setParam(array('from' => $from));
    219219        }
    220220        if (isset($subject)) {
    221             $this->setParam(array('subject' => $subject));
     221            $this->setParam(array('subject' => $subject));
    222222        }
    223223        if (isset($headers)) {
    224             $this->setParam(array('headers' => $headers));
     224            $this->setParam(array('headers' => $headers));
    225225        }
    226226
    227227        // Ensure required values exist.
    228228        if (!isset($this->_template)) {
    229             App::logMsg(sprintf('Cannot send email. Template not set.', null), LOG_ERR, __FILE__, __LINE__);
     229            App::logMsg(sprintf('Cannot send email. Template not set.', null), LOG_ERR, __FILE__, __LINE__);
    230230            return false;
    231231        } else if (!isset($this->_params['to'])) {
    232             App::logMsg(sprintf('Cannot send email. TO not defined.', null), LOG_ERR, __FILE__, __LINE__);
     232            App::logMsg(sprintf('Cannot send email. TO not defined.', null), LOG_ERR, __FILE__, __LINE__);
    233233            return false;
    234234        } else if (!isset($this->_params['from'])) {
    235             App::logMsg(sprintf('Cannot send email. FROM not defined.', null), LOG_ERR, __FILE__, __LINE__);
     235            App::logMsg(sprintf('Cannot send email. FROM not defined.', null), LOG_ERR, __FILE__, __LINE__);
    236236            return false;
    237237        } else if (!isset($this->_params['subject'])) {
    238             App::logMsg(sprintf('Cannot send email. SUBJECT not defined.', null), LOG_ERR, __FILE__, __LINE__);
     238            App::logMsg(sprintf('Cannot send email. SUBJECT not defined.', null), LOG_ERR, __FILE__, __LINE__);
    239239            return false;
    240240        }
     
    245245        // Ensure all placeholders have been replaced. Find anything with {...} characters.
    246246        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__);
    248             return false;
    249         }
    250        
     247            App::logMsg(sprintf('Cannot send email. Variables left unreplaced in template: %s', (isset($unreplaced_match[1]) ? $unreplaced_match[1] : '')), LOG_ERR, __FILE__, __LINE__);
     248            return false;
     249        }
     250
    251251        // Final "to" header can have multiple addresses if in an array.
    252252        $final_to = is_array($this->_params['to']) ? join(', ', $this->_params['to']) : $this->_params['to'];
    253        
     253
    254254        // From headers are custom headers.
    255255        $headers = array('From' => $this->_params['from']);
     
    259259            $headers = array_merge($this->_params['headers'], $headers);
    260260        }
    261        
     261
    262262        // Process headers.
    263263        $final_headers = array();
     
    266266        }
    267267        $final_headers = join("\r\n", $final_headers);
    268        
     268
    269269        // This is the address where delivery problems are sent to. We must strip off everything except the local@domain part.
    270270        $envelope_sender_header = sprintf('-f %s', preg_replace('/^.*<?([^\s@\[\]<>()]+\@[A-Za-z0-9.-]{1,}\.[A-Za-z]{2,5})>?$/iU', '$1', $this->_params['from']));
    271        
     271
    272272        // Check for mail header injection attacks.
    273273        $full_mail_content = join("\n", array($final_to, $this->_params['subject'], $final_body, $final_headers, $envelope_sender_header));
    274274        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__);
    276             sleep(3);
     275            App::logMsg(sprintf('Mail header injection attack in content: %s', $full_mail_content), LOG_WARNING, __FILE__, __LINE__);
     276            sleep(3);
    277277            return false;
    278278        }
     
    283283            return false;
    284284        }
    285        
     285
    286286        return true;
    287287    }
    288    
     288
    289289    /**
    290290     * Validates an email address based on the recommendations in RFC 3696.
    291      * Is more loose than restrictive, to allow the many valid variants of 
     291     * Is more loose than restrictive, to allow the many valid variants of
    292292     * email addresses while catching the most common mistakes. Checks an array too.
    293293     * http://www.faqs.org/rfcs/rfc822.html
Note: See TracChangeset for help on using the changeset viewer.