Changeset 1321

Show
Ignore:
Timestamp:
01/11/08 23:47:52 (5 years ago)
Author:
benoitg
Message:
  • Use the Dependency system to include PHPMailer and tweak Mail.php. You'll need to re-run the Dpenedency stage of the install script. This allows: Translating PHPMailer error messages, Actually having PHPMailer error messages, and displaying them properly.
Location:
trunk/wifidog-auth
Files:
1 removed
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/wifidog-auth/CHANGELOG

    r1320 r1321  
    11# $Id$ 
     22008-01-11 Benoit Grégoire  <bock@step.polymtl.ca> 
     3        * Use the Dependency system to include PHPMailer and tweak Mail.php.  You'll need to re-run the Dpenedency stage of the install script. This allows:  Translating PHPMailer error messages, Actually having PHPMailer error messages, and displaying them properly. 
     4         
    252008-01-09 Benoit Grégoire  <bock@step.polymtl.ca> 
    36        * Add gettext calls, fixes #413 
  • trunk/wifidog-auth/wifidog/classes/Dependency.php

    r1316 r1321  
    9292     * 'installMethod' => For localLib, the protocol to be used to download and install the dependency.  Currently, allowed values are: 
    9393     *  'tarball': Decompress a tarball in wifidog/lib 
    94      * 'installDestination' => For localLib, the path, relative to wifidog/lib where the dependency should be installed 
     94     * 'installDestination' => For localLib, the path, relative to wifidog/lib where the dependency should be installed.  Usually / if the tarball creates a folder. 
    9595     * 'filename' => temp download filename if sourceurl does not meet preg requirements. 
    9696     * 
     
    125125       'installDestination' => "/" 
    126126       ), 
    127  
     127       "PHPMailer" => array ( 
     128       'mandatory' => true, 
     129       "type" => "localLib", 
     130       "detectFiles" => "lib/PHPMailer_v2.0.0/class.phpmailer.php", 
     131       'description' => "Required for sending mail", 
     132       'website' => "http://phpmailer.codeworxtech.com/", 
     133       'installSourceUrl' => "http://superb-west.dl.sourceforge.net/sourceforge/phpmailer/PHPMailer_v2.0.0.tar.gz", 
     134       'installMethod' => "tarball", 
     135       'installDestination' => "/" 
     136       ), 
     137        
    128138       /* PHP extensions (optional) */ 
    129139       "simplepie" => array ( 
  • trunk/wifidog-auth/wifidog/classes/Mail.php

    r1318 r1321  
    4646 * Load required classes 
    4747 */ 
    48 require_once ('lib/PHPMailer/class.phpmailer.php'); 
    49 require_once ('lib/PHPMailer/class.smtp.php'); 
    50 require_once ('lib/PHPMailer/phpmailer.lang-en.php'); 
     48 
    5149 
    5250/** 
     
    6462     * annonymous email.  We just want them to stay reachable in case of problem 
    6563     * or abuse. 
    66      *  
     64     * 
    6765     * All domains must be lowercase 
    6866     * @note We may have to do something a little smarter to deal with the likes 
     
    125123        "woodyland.org", 
    126124        "wuzup.net" 
    127     ); 
    128     /** 
    129      * Name email will been sent from 
    130      * 
    131      * @var string 
    132      * 
    133      * @access private 
    134      */ 
    135     private $_fromName; 
    136  
    137     /** 
    138      * Address email will be sent from 
    139      * 
    140      * @var string 
    141      * 
    142      * @access private 
    143      */ 
    144     private $_fromEmail; 
    145  
    146     /** 
    147      * Name email will be sent to 
    148      * 
    149      * @var string 
    150      * 
    151      * @access private 
    152      */ 
    153     private $_toName; 
    154  
    155     /** 
    156      * Address email will be sent to 
    157      * 
    158      * @var string 
    159      * 
    160      * @access private 
    161      */ 
    162     private $_toEmail; 
    163  
    164     /** 
    165      * Subject of email 
    166      * 
    167      * @var string 
    168      * 
    169      * @access private 
    170      */ 
    171     private $_subject; 
    172  
    173     /** 
    174      * Content of email 
    175      * 
    176      * @var string 
    177      * 
    178      * @access private 
    179      */ 
    180     private $_body; 
    181  
    182     /** 
    183      * Priority of email 
    184      * 
    185      * @var boolean 
    186      * 
    187      * @access private 
    188      */ 
    189     private $_highPriority; 
    190  
    191     /** 
    192      * Encodes the MIME header 
    193      * 
    194      * @param string $header Header of email 
    195      * 
    196      * @return string Encoded MIME header 
    197      
    198      * 
    199      * @see http://www.php.net/manual/en/function.mb-send-mail.php 
    200      */ 
    201     private function _encodeMimeHeader($header) { 
    202         // BASE 64 according to the RFC 
    203         $header = preg_replace('/([^a-z ])/ie', 'sprintf("=%02x",ord(StripSlashes("\\1")))', $header); 
    204         $header = str_replace(' ', '_', $header); 
    205         return "=?utf-8?Q?$header?="; 
    206     } 
    207  
    208     /** 
    209      * Returns name of sender of email 
    210      * 
    211      * @return string Name of sender of email 
    212      * 
    213      * @access public 
    214      */ 
    215     public function getSenderName() { 
    216         return $this->_fromName; 
    217     } 
    218  
    219     /** 
    220      * Sets name of sender of email 
    221      * 
    222      * @param string $name Name of sender of email 
    223      * 
    224      * @return void 
    225      * 
    226      * @access public 
    227      */ 
    228     public function setSenderName($name) { 
    229         // Encode name 
    230         $this->_fromName = $this->_encodeMimeHeader($name); 
    231     } 
    232  
    233     /** 
    234      * Returns address of sender of email 
    235      * 
    236      * @return string Address of sender of email 
    237      * 
    238      * @access public 
    239      */ 
    240     public function getSenderEmail() { 
    241         return $this->_fromEmail; 
    242     } 
    243  
    244     /** 
    245      * Sets address of sender of email 
    246      * 
    247      * @param string $mail Address of sender of email 
    248      * 
    249      * @return void 
    250      * 
    251      * @access public 
    252      */ 
    253     public function setSenderEmail($mail) { 
    254         $this->_fromEmail = $mail; 
    255     } 
    256  
    257     /** 
    258      * Returns name of recipient of email 
    259      * 
    260      * @return string Name of recipient of email 
    261      * 
    262      * @access public 
    263      */ 
    264     public function getRecipientName() { 
    265         return $this->_toName; 
    266     } 
    267  
    268     /** 
    269      * Sets name of recipient of email 
    270      * 
    271      * @param string $name Name of recipient of email 
    272      * 
    273      * @return void 
    274      * 
    275      * @access public 
    276      */ 
    277     public function setRecipientName($name) { 
    278         // Encode name 
    279         $this->_toName = $this->_encodeMimeHeader($name); 
    280     } 
    281  
    282     /** 
    283      * Returns address of recipient of email 
    284      * 
    285      * @return string Address of recipient of email 
    286      * 
    287      * @access public 
    288      */ 
    289     public function getRecipientEmail() { 
    290         return $this->_toEmail; 
    291     } 
    292  
    293     /** 
    294      * Sets address of recipient of email 
    295      * 
    296      * @param string $mail Address of recipient of email 
    297      * 
    298      * @return void 
    299      * 
    300      * @access public 
    301      */ 
    302     public function setRecipientEmail($mail) { 
    303         $this->_toEmail = $mail; 
    304     } 
    305  
    306     /** 
    307      * Returns subject of email 
    308      * 
    309      * @return string Subject of email 
    310      * 
    311      * @access public 
    312      */ 
    313     public function getMessageSubject() { 
    314         return $this->_subject; 
    315     } 
    316  
    317     /** 
    318      * Sets subject of email 
    319      * 
    320      * @param string $subject Subject of email 
    321      * 
    322      * @return void 
    323      * 
    324      * @access public 
    325      */ 
    326     public function setMessageSubject($subject) { 
    327         $this->_subject = $this->_encodeMimeHeader($subject); 
    328     } 
    329  
    330     /** 
    331      * Returns message body of email 
    332      * 
    333      * @return string Message body of email 
    334      * 
    335      * @access public 
    336      */ 
    337     public function getMessageBody() { 
    338         return $this->_body; 
    339     } 
    340  
    341     /** 
    342      * Sets message body of email 
    343      * 
    344      * @param string $body Message body of email 
    345      * 
    346      * @return void 
    347      * 
    348      * @access public 
    349      */ 
    350     public function setMessageBody($body) { 
    351         $this->_body = $body; 
    352     } 
    353  
    354     /** 
    355      * Returns the priority of the email 
    356      * 
    357      * @return boolean Whether high priority or not 
    358      * 
    359      * @access public 
    360      */ 
    361     public function getHighPriority() { 
    362         return $this->_highPriority; 
    363     } 
    364  
    365     /** 
    366      * Sets the priority of the email 
    367      * 
    368      * @param boolean $boolean Whether high priority or not 
    369      * 
    370      * @return void 
    371      * 
    372      * @access public 
    373      */ 
    374     public function setHighPriority($boolean) { 
    375         $this->_highPriority = $boolean ? true : false; 
    376     } 
    377  
    378     /** 
    379      * Packs email and sends it according to RFC822 
    380      * 
    381      * @return bool True if email could be sent 
    382      * 
    383      * @access public 
    384      */ 
    385     public function send() { 
    386         $mail = new PHPMailer(); 
    387         $mail->CharSet = "utf-8"; 
    388         # $mail->SMTPDebug=TRUE; 
    389  
    390         $mail->Mailer = EMAIL_MAILER; 
    391         if (EMAIL_MAILER == 'smtp') { 
    392             $mail->IsSMTP(); 
    393             $mail->Host = EMAIL_HOST; 
    394             $mail->SMTPAuth = EMAIL_AUTH; 
    395  
    396             if (EMAIL_AUTH) { 
    397                 $mail->Username = EMAIL_USERNAME; 
    398                 $mail->Password = EMAIL_PASSWORD; 
     125        ); 
     126        /** 
     127         * Name email will been sent from 
     128         * 
     129         * @var string 
     130         */ 
     131        private $_fromName; 
     132 
     133        /** 
     134         * Address email will be sent from 
     135         * 
     136         * @var string 
     137         */ 
     138        private $_fromEmail; 
     139 
     140        /** 
     141         * Name email will be sent to 
     142         * 
     143         * @var string 
     144         */ 
     145        private $_toName; 
     146 
     147        /** 
     148         * Address email will be sent to 
     149         * 
     150         * @var string 
     151         */ 
     152        private $_toEmail; 
     153 
     154        /** 
     155         * Subject of email 
     156         * 
     157         * @var string 
     158         */ 
     159        private $_subject; 
     160 
     161        /** 
     162         * Content of email 
     163         * 
     164         * @var string 
     165         */ 
     166        private $_body; 
     167 
     168        /** 
     169         * Priority of email 
     170         * 
     171         * @var boolean 
     172         */ 
     173        private $_highPriority; 
     174 
     175        /** 
     176         * Encodes the MIME header 
     177         * 
     178         * @param string $header Header of email 
     179         * 
     180         * @return string Encoded MIME header 
     181 
     182         * 
     183         * @see http://www.php.net/manual/en/function.mb-send-mail.php 
     184         */ 
     185        private function _encodeMimeHeader($header) { 
     186            // BASE 64 according to the RFC 
     187            $header = preg_replace('/([^a-z ])/ie', 'sprintf("=%02x",ord(StripSlashes("\\1")))', $header); 
     188            $header = str_replace(' ', '_', $header); 
     189            return "=?utf-8?Q?$header?="; 
     190        } 
     191 
     192        /** 
     193         * Returns name of sender of email 
     194         * 
     195         * @return string Name of sender of email 
     196         * 
     197         * @access public 
     198         */ 
     199        public function getSenderName() { 
     200            return $this->_fromName; 
     201        } 
     202 
     203        /** 
     204         * Sets name of sender of email 
     205         * 
     206         * @param string $name Name of sender of email 
     207         * 
     208         * @return void 
     209         * 
     210         * @access public 
     211         */ 
     212        public function setSenderName($name) { 
     213            // Encode name 
     214            $this->_fromName = $this->_encodeMimeHeader($name); 
     215        } 
     216 
     217        /** 
     218         * Returns address of sender of email 
     219         * 
     220         * @return string Address of sender of email 
     221         * 
     222         * @access public 
     223         */ 
     224        public function getSenderEmail() { 
     225            return $this->_fromEmail; 
     226        } 
     227 
     228        /** 
     229         * Sets address of sender of email 
     230         * 
     231         * @param string $mail Address of sender of email 
     232         * 
     233         * @return void 
     234         * 
     235         * @access public 
     236         */ 
     237        public function setSenderEmail($mail) { 
     238            $this->_fromEmail = $mail; 
     239        } 
     240 
     241        /** 
     242         * Returns name of recipient of email 
     243         * 
     244         * @return string Name of recipient of email 
     245         * 
     246         * @access public 
     247         */ 
     248        public function getRecipientName() { 
     249            return $this->_toName; 
     250        } 
     251 
     252        /** 
     253         * Sets name of recipient of email 
     254         * 
     255         * @param string $name Name of recipient of email 
     256         * 
     257         * @return void 
     258         * 
     259         * @access public 
     260         */ 
     261        public function setRecipientName($name) { 
     262            // Encode name 
     263            $this->_toName = $this->_encodeMimeHeader($name); 
     264        } 
     265 
     266        /** 
     267         * Returns address of recipient of email 
     268         * 
     269         * @return string Address of recipient of email 
     270         * 
     271         * @access public 
     272         */ 
     273        public function getRecipientEmail() { 
     274            return $this->_toEmail; 
     275        } 
     276 
     277        /** 
     278         * Sets address of recipient of email 
     279         * 
     280         * @param string $mail Address of recipient of email 
     281         * 
     282         * @return void 
     283         * 
     284         * @access public 
     285         */ 
     286        public function setRecipientEmail($mail) { 
     287            $this->_toEmail = $mail; 
     288        } 
     289 
     290        /** 
     291         * Returns subject of email 
     292         * 
     293         * @return string Subject of email 
     294         * 
     295         * @access public 
     296         */ 
     297        public function getMessageSubject() { 
     298            return $this->_subject; 
     299        } 
     300 
     301        /** 
     302         * Sets subject of email 
     303         * 
     304         * @param string $subject Subject of email 
     305         * 
     306         * @return void 
     307         * 
     308         * @access public 
     309         */ 
     310        public function setMessageSubject($subject) { 
     311            $this->_subject = $this->_encodeMimeHeader($subject); 
     312        } 
     313 
     314        /** 
     315         * Returns message body of email 
     316         * 
     317         * @return string Message body of email 
     318         * 
     319         * @access public 
     320         */ 
     321        public function getMessageBody() { 
     322            return $this->_body; 
     323        } 
     324 
     325        /** 
     326         * Sets message body of email 
     327         * 
     328         * @param string $body Message body of email 
     329         * 
     330         * @return void 
     331         * 
     332         * @access public 
     333         */ 
     334        public function setMessageBody($body) { 
     335            $this->_body = $body; 
     336        } 
     337 
     338        /** 
     339         * Returns the priority of the email 
     340         * 
     341         * @return boolean Whether high priority or not 
     342         * 
     343         * @access public 
     344         */ 
     345        public function getHighPriority() { 
     346            return $this->_highPriority; 
     347        } 
     348 
     349        /** 
     350         * Sets the priority of the email 
     351         * 
     352         * @param boolean $boolean Whether high priority or not 
     353         * 
     354         * @return void 
     355         * 
     356         * @access public 
     357         */ 
     358        public function setHighPriority($boolean) { 
     359            $this->_highPriority = $boolean ? true : false; 
     360        } 
     361 
     362        /** 
     363         * Packs email and sends it according to RFC822 
     364         * 
     365         * @return bool True if email could be sent 
     366         * 
     367         * @access public 
     368         */ 
     369        public function send() { 
     370            $phpmailerPath = 'lib/PHPMailer_v2.0.0/'; 
     371            require_once ($phpmailerPath.'class.phpmailer.php'); 
     372            require_once ($phpmailerPath.'class.smtp.php'); 
     373            require_once ('classes/Session.php'); 
     374            $mail = new PHPMailer(); 
     375            $session = Session::getObject(); 
     376            $lang = substr  ($session->get(SESS_LANGUAGE_VAR),0,2); 
     377            $mail->SetLanguage($lang, $phpmailerPath.'language/'); 
     378            $mail->CharSet = "utf-8"; 
     379            # $mail->SMTPDebug=TRUE; 
     380 
     381            $mail->Mailer = EMAIL_MAILER; 
     382            if (EMAIL_MAILER == 'smtp') { 
     383                $mail->IsSMTP(); 
     384                $mail->Host = EMAIL_HOST; 
     385                $mail->SMTPAuth = EMAIL_AUTH; 
     386 
     387                if (EMAIL_AUTH) { 
     388                    $mail->Username = EMAIL_USERNAME; 
     389                    $mail->Password = EMAIL_PASSWORD; 
     390                } 
    399391            } 
    400         } 
    401  
    402         $mail->AddAddress($this->getRecipientEmail(), $this->getRecipientName()); 
    403         $mail->From = $this->getSenderEmail(); 
    404         $mail->FromName = $this->getSenderName(); 
    405         $mail->Sender = $this->getSenderEmail(); // add Sender Name 
    406         if ($this->getHighPriority()) { 
    407             $mail->Priority = 1; 
    408         } 
    409         $mail->Subject = $this->getMessageSubject(); 
    410         $mail->Body = $this->getMessageBody(); 
    411  
    412         $result = $mail->Send(); 
    413         if (!$result) { 
    414             print $mail->ErrorInfo; 
    415         } 
    416         return $result; 
    417     } 
    418  
    419     /** 
    420      * Validates an email address 
    421      * 
    422      * This function will make sure an e-mail is RFC822 compliant 
    423      * and is not black listed. 
    424      * 
    425      * @param string $mail The email address to validate 
    426      * 
    427      * @return bool Returns whether the email address is valid or not 
    428      * 
    429      * @static 
    430      * @access public 
    431      */ 
    432     public static function validateEmailAddress($email) { 
    433         // Init values 
    434         $_matches = null; 
    435         $_retVal = false; 
    436  
    437         // Test if the email address is valid 
    438         $regex = "/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i"; 
    439  
    440         if (preg_match_all($regex, $email, $_matches)) { 
    441             // If the hostname is black listed, reject the email address 
    442             $full_hostname = $_matches[2][0] . "." . $_matches[3][0]; 
    443  
    444             if (!in_array(strtolower($full_hostname), self :: $_hosts_black_list)) { 
    445                 $_retVal = true; 
     392 
     393            $mail->AddAddress($this->getRecipientEmail(), $this->getRecipientName()); 
     394            $mail->From = $this->getSenderEmail(); 
     395            $mail->FromName = $this->getSenderName(); 
     396            $mail->Sender = $this->getSenderEmail(); // add Sender Name 
     397            if ($this->getHighPriority()) { 
     398                $mail->Priority = 1; 
    446399            } 
    447         } 
    448  
    449         return $_retVal; 
    450     } 
     400            $mail->Subject = $this->getMessageSubject(); 
     401            $mail->Body = $this->getMessageBody(); 
     402 
     403            $result = $mail->Send(); 
     404            if (!$result) { 
     405                throw new exception(sprintf(_("PHPMailer couldn't sent mail.  Error was: %s"),$mail->ErrorInfo)); 
     406            } 
     407            return $result; 
     408        } 
     409 
     410        /** 
     411         * Validates an email address 
     412         * 
     413         * This function will make sure an e-mail is RFC822 compliant 
     414         * and is not black listed. 
     415         * 
     416         * @param string $mail The email address to validate 
     417         * 
     418         * @return bool Returns whether the email address is valid or not 
     419         * 
     420         * @static 
     421         * @access public 
     422         */ 
     423        public static function validateEmailAddress($email) { 
     424            // Init values 
     425            $_matches = null; 
     426            $_retVal = false; 
     427 
     428            // Test if the email address is valid 
     429            $regex = "/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i"; 
     430 
     431            if (preg_match_all($regex, $email, $_matches)) { 
     432                // If the hostname is black listed, reject the email address 
     433                $full_hostname = $_matches[2][0] . "." . $_matches[3][0]; 
     434 
     435                if (!in_array(strtolower($full_hostname), self :: $_hosts_black_list)) { 
     436                    $_retVal = true; 
     437                } 
     438            } 
     439 
     440            return $_retVal; 
     441        } 
    451442 
    452443} 
  • trunk/wifidog-auth/wifidog/classes/User.php

    r1316 r1321  
    4747 */ 
    4848require_once ('classes/Network.php'); 
    49 require_once ('classes/Mail.php'); 
    5049require_once ('classes/InterfaceElements.php'); 
    5150require_once ('classes/ProfileTemplate.php'); 
     
    576575    function sendLostUsername() { 
    577576        $network = $this->getNetwork(); 
     577        require_once ('classes/Mail.php'); 
    578578        $mail = new Mail(); 
    579579        $mail->setSenderName(_("Registration system")); 
     
    593593            } else { 
    594594                $network = $this->getNetwork(); 
    595  
     595                require_once ('classes/Mail.php'); 
    596596                $mail = new Mail(); 
    597597                $mail->setSenderName(_("Registration system")); 
     
    610610        $new_password = $this->randomPass(); 
    611611        $this->setPassword($new_password); 
    612  
     612        require_once ('classes/Mail.php'); 
    613613        $mail = new Mail(); 
    614614        $mail->setSenderName(_("Registration system"));