| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @package Mail | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author Iurii Makukh | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @copyright Copyright (c) 2017, Iurii Makukh | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace gplcart\modules\mail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use gplcart\core\Module, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     gplcart\core\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Main class for Mail module | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class Mail extends Module | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param Config $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public function __construct(Config $config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         parent::__construct($config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Implements hook "library.list" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param array $libraries | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function hookLibraryList(array &$libraries) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $libraries['phpmailer'] = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             'name' => /* @text */'PHP Mail', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             'description' => /* @text */'The classic email sending library for PHP', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             'url' => 'https://github.com/PHPMailer/PHPMailer', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             'download' => 'https://github.com/PHPMailer/PHPMailer/archive/v5.2.23.zip', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             'type' => 'php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             'version_source' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 'lines' => 100, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 'pattern' => '/.*\\$Version.*(\\d+\\.+\\d+\\.+\\d+)/', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 'file' => 'vendor/phpmailer/phpmailer/class.phpmailer.php' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             'module' => 'mail', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             'files' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 'vendor/autoload.php' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * Implements hook "route.list" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param array $routes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function hookRouteList(array &$routes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $routes['admin/module/settings/mail'] = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             'access' => 'module_edit', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             'handlers' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 'controller' => array('gplcart\\modules\\mail\\controllers\\Settings', 'editSettings') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * Implements hook "mail.send" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param array $to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param string $subject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param string $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param mixed $result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     public function hookMailSend($to, $subject, $message, $options, &$result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $this->setMailer($to, $subject, $message, $options, $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * Implements hook "module.enable.after" | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     public function hookModuleEnableAfter() | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         $this->getLibrary()->clearCache(); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * Implements hook "module.disable.after" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public function hookModuleDisableAfter() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->getLibrary()->clearCache(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * Implements hook "module.install.after" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     public function hookModuleInstallAfter() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $this->getLibrary()->clearCache(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * Implements hook "module.uninstall.after" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     public function hookModuleUninstallAfter() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $this->getLibrary()->clearCache(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * Returns PHPMailer instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @return \PHPMailer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     public function getMailerInstance() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $this->getLibrary()->load('phpmailer'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         if (class_exists('PHPMailer')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             return new \PHPMailer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         throw new \InvalidArgumentException('Class PHPMailer not found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * Send an E-mail | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @param array $to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @param string $subject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @param string $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @param array $settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @return boolean|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     public function send($to, $subject, $message, $options, $settings) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             $mailer = $this->getMailerInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         } catch (\InvalidArgumentException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             return $ex->getMessage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $mailer->isSMTP(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $mailer->isHTML(!empty($options['html'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $mailer->Body = $message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $mailer->Subject = $subject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         if (!empty($options['html'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             $mailer->AltBody = strip_tags($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $mailer->Port = $settings['port']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $mailer->setFrom($options['from']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $mailer->Username = $settings['user']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $mailer->Password = $settings['password']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         $mailer->SMTPSecure = $settings['secure']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         $mailer->SMTPAuth = !empty($settings['auth']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         $mailer->Host = implode(';', $settings['host']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         foreach ($to as $address) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             settype($address, 'array'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             call_user_func_array(array($mailer, 'addAddress'), $address); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         if (!empty($options['attachment'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             foreach ($options['attachment'] as $attachment) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                 settype($attachment, 'array'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |                 call_user_func_array(array($mailer, 'addAttachment'), $attachment); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         if ($mailer->send()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         return (string) $mailer->ErrorInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |      * @param array $to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |      * @param string $subject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |      * @param string $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |      * @param mixed $result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     protected function setMailer($to, $subject, $message, $options, &$result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         $settings = $this->config->getFromModule('mail'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |         if (!empty($settings['status']) && $result === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |             $sent = $this->send($to, $subject, $message, $options, $settings); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |             if ($sent === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |                 $result = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 200 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 201 |  |  |  |