Esempio n. 1
0
 /**
  * @param \µMailPHP\Definition The name of the mail definition class.
  * @param mixed $recipient The recipient's email address, or a recipient object. If left null, the rendition must have a recipient.
  * @param array $macros An associative array of macros. These override macros from the definition.
  * @param string|null $sender The sender's email address. If left null, the rendition or default sender will be used.
  * @param \µMailPHP\Rendition An optional rendition. If left null, the latest ready rendition will be used. If false, no rendition will be used.
  * @param \µMailPHP\Template An optional template. If left null, the latest ready template will be used. If false, the default template will be used.
  */
 public function __construct($definition, $recipient = null, $macros = [], $sender = null, $rendition = null, $template = null)
 {
     if (!class_exists($definition) || !is_subclass_of($definition, '\\µMailPHP\\Definition')) {
         throw new \InvalidArgumentException('Mail definition is required.');
     }
     $config = \SciActive\RequirePHP::_('µMailPHPConfig');
     // Format recipient.
     if ($recipient && is_string($recipient)) {
         $recipient = (object) ['email' => $recipient];
     }
     // Find any renditions.
     if ($rendition === null) {
         $renditions = (array) \Nymph\Nymph::getEntities(['class' => '\\µMailPHP\\Rendition', 'reverse' => true], ['&', 'strict' => [['enabled', true], ['definition', $definition]]]);
         foreach ($renditions as $cur_rendition) {
             if ($cur_rendition->ready()) {
                 $rendition = $cur_rendition;
                 break;
             }
         }
         unset($renditions, $cur_rendition);
     }
     // Get the email sender.
     if ($rendition && !isset($sender) && !empty($rendition->from)) {
         $sender = $rendition->from;
     }
     // Get the email recipient(s).
     if (!$recipient) {
         // If it's supposed to have a recipient already, report failure.
         if ($definition::expectsRecipient) {
             throw new \UnexpectedValueException('This email definition requires a recipient.');
         }
         if ($rendition) {
             if ($rendition->to) {
                 // Check Tilmeld users/groups if Tilmeld is loaded.
                 if (class_exists('\\Tilmeld\\User') && strpos($rendition->to, ',') === false) {
                     if (preg_match('/<.+@.+>/', $rendition->to)) {
                         $check_email = trim(preg_replace('/^.*<(.+@.+)>.*$/', '$1', $rendition->to));
                     } else {
                         $check_email = trim($rendition->to);
                     }
                     // Check for a user or group with that email.
                     $user = \Nymph\Nymph::getEntity(['class' => '\\Tilmeld\\User'], ['&', 'strict' => ['email', $check_email]]);
                     if ($user) {
                         $recipient = $user;
                     } else {
                         $group = \Nymph\Nymph::getEntity(['class' => '\\Tilmeld\\Group'], ['&', 'strict' => ['email', $check_email]]);
                         if ($group) {
                             $recipient = $group;
                         }
                     }
                 }
                 if (!$recipient) {
                     $recipient = (object) ['email' => $rendition->to];
                 }
             }
         } else {
             // Send to the master address if there's no recipient.
             if (!$config->master_address['value']) {
                 throw new \UnexpectedValueException('This email needs a recipient and no master address is set.');
             }
             $recipient = (object) ['email' => $config->master_address['value']];
         }
     }
     // Get the email contents.
     $body = [];
     if ($rendition) {
         $body['subject'] = $rendition->subject;
         $body['content'] = $rendition->content;
     } else {
         $body['subject'] = $definition::getSubject();
         $body['content'] = $definition::getHTML();
     }
     // Get the template.
     if ($template === null) {
         $templates = (array) \Nymph\Nymph::getEntities(['class' => '\\µMailPHP\\Template', 'reverse' => true], ['&', 'strict' => ['enabled', true]]);
         // Get the first template that's ready.
         foreach ($templates as $cur_template) {
             if ($cur_template->ready()) {
                 $template = $cur_template;
                 break;
             }
         }
         unset($templates, $cur_template);
     }
     // If there is no template, use a default one.
     if (!$template) {
         $template = new Template();
     }
     // Build the body of the email.
     $body['content'] = str_replace('#content#', $body['content'], str_replace('#content#', $template->content, $template->document));
     // Replace macros and search strings.
     foreach ($body as &$cur_field) {
         // Some of these str_replace calls are wrapped in a strpos call,
         // because they involve some processing just to make the call. In a
         // system sending out millions of emails, that adds up to a lot of
         // wasted CPU.
         foreach ((array) $template->replacements as $cur_string) {
             if (!$cur_string['macros']) {
                 continue;
             }
             $cur_field = str_replace($cur_string['search'], $cur_string['replace'], $cur_field);
         }
         if (strpos($cur_field, '#subject#') !== false) {
             $cur_field = str_replace('#subject#', htmlspecialchars($body['subject']), $cur_field);
         }
         // Links
         if (strpos($cur_field, '#site_link#') !== false) {
             $cur_field = str_replace('#site_link#', htmlspecialchars($config->site_link['value']), $cur_field);
         }
         // Recipient
         if (strpos($cur_field, '#to_username#') !== false) {
             $cur_field = str_replace('#to_username#', htmlspecialchars(isset($recipient->username) ? $recipient->username : (isset($recipient->groupname) ? $recipient->groupname : '')), $cur_field);
         }
         if (strpos($cur_field, '#to_name#') !== false) {
             $cur_field = str_replace('#to_name#', htmlspecialchars(isset($recipient->name) ? $recipient->name : ''), $cur_field);
         }
         if (strpos($cur_field, '#to_first_name#') !== false) {
             $cur_field = str_replace('#to_first_name#', htmlspecialchars(isset($recipient->nameFirst) ? $recipient->nameFirst : ''), $cur_field);
         }
         if (strpos($cur_field, '#to_last_name#') !== false) {
             $cur_field = str_replace('#to_last_name#', htmlspecialchars(isset($recipient->nameLast) ? $recipient->nameLast : ''), $cur_field);
         }
         if (strpos($cur_field, '#to_email#') !== false) {
             $cur_field = str_replace('#to_email#', htmlspecialchars(isset($recipient->email) ? $recipient->email : ''), $cur_field);
         }
         // Current User with Tilmeld.
         if (class_exists('\\Tilmeld\\User') && \Tilmeld\User::current()) {
             if (strpos($cur_field, '#username#') !== false) {
                 $cur_field = str_replace('#username#', htmlspecialchars(\Tilmeld\User::current()->username), $cur_field);
             }
             if (strpos($cur_field, '#name#') !== false) {
                 $cur_field = str_replace('#name#', htmlspecialchars(\Tilmeld\User::current()->name), $cur_field);
             }
             if (strpos($cur_field, '#first_name#') !== false) {
                 $cur_field = str_replace('#first_name#', htmlspecialchars(\Tilmeld\User::current()->nameFirst), $cur_field);
             }
             if (strpos($cur_field, '#last_name#') !== false) {
                 $cur_field = str_replace('#last_name#', htmlspecialchars(\Tilmeld\User::current()->nameLast), $cur_field);
             }
             if (strpos($cur_field, '#email#') !== false) {
                 $cur_field = str_replace('#email#', htmlspecialchars(\Tilmeld\User::current()->email), $cur_field);
             }
         }
         // Date/Time
         if (strpos($cur_field, '#datetime_sort#') !== false) {
             $cur_field = str_replace('#datetime_sort#', htmlspecialchars(Mail::formatDate(time(), 'full_sort')), $cur_field);
         }
         if (strpos($cur_field, '#datetime_short#') !== false) {
             $cur_field = str_replace('#datetime_short#', htmlspecialchars(Mail::formatDate(time(), 'full_short')), $cur_field);
         }
         if (strpos($cur_field, '#datetime_med#') !== false) {
             $cur_field = str_replace('#datetime_med#', htmlspecialchars(Mail::formatDate(time(), 'full_med')), $cur_field);
         }
         if (strpos($cur_field, '#datetime_long#') !== false) {
             $cur_field = str_replace('#datetime_long#', htmlspecialchars(Mail::formatDate(time(), 'full_long')), $cur_field);
         }
         if (strpos($cur_field, '#date_sort#') !== false) {
             $cur_field = str_replace('#date_sort#', htmlspecialchars(Mail::formatDate(time(), 'date_sort')), $cur_field);
         }
         if (strpos($cur_field, '#date_short#') !== false) {
             $cur_field = str_replace('#date_short#', htmlspecialchars(Mail::formatDate(time(), 'date_short')), $cur_field);
         }
         if (strpos($cur_field, '#date_med#') !== false) {
             $cur_field = str_replace('#date_med#', htmlspecialchars(Mail::formatDate(time(), 'date_med')), $cur_field);
         }
         if (strpos($cur_field, '#date_long#') !== false) {
             $cur_field = str_replace('#date_long#', htmlspecialchars(Mail::formatDate(time(), 'date_long')), $cur_field);
         }
         if (strpos($cur_field, '#time_sort#') !== false) {
             $cur_field = str_replace('#time_sort#', htmlspecialchars(Mail::formatDate(time(), 'time_sort')), $cur_field);
         }
         if (strpos($cur_field, '#time_short#') !== false) {
             $cur_field = str_replace('#time_short#', htmlspecialchars(Mail::formatDate(time(), 'time_short')), $cur_field);
         }
         if (strpos($cur_field, '#time_med#') !== false) {
             $cur_field = str_replace('#time_med#', htmlspecialchars(Mail::formatDate(time(), 'time_med')), $cur_field);
         }
         if (strpos($cur_field, '#time_long#') !== false) {
             $cur_field = str_replace('#time_long#', htmlspecialchars(Mail::formatDate(time(), 'time_long')), $cur_field);
         }
         // System
         if (strpos($cur_field, '#site_name#') !== false) {
             $cur_field = str_replace('#site_name#', htmlspecialchars($config->site_name['value']), $cur_field);
         }
         // Argument Macros
         foreach ($macros as $cur_name => $cur_value) {
             $cur_field = str_replace("#{$cur_name}#", $cur_value, $cur_field);
         }
         // Definition Macros
         foreach ($definition::macros as $cur_name => $cur_desc) {
             if (strpos($cur_field, "#{$cur_name}#") !== false) {
                 $cur_field = str_replace("#{$cur_name}#", $definition::getMacro($cur_name), $cur_field);
             }
         }
         foreach ((array) $template->replacements as $cur_string) {
             if ($cur_string['macros']) {
                 continue;
             }
             $cur_field = str_replace($cur_string['search'], $cur_string['replace'], $cur_field);
         }
     }
     unset($cur_field);
     // Add additional recipients.
     if ($rendition) {
         if ($rendition->cc) {
             $email->addHeader('CC', $rendition->cc);
         }
         if ($rendition->bcc) {
             $email->addHeader('BCC', $rendition->bcc);
         }
     }
     // Get default values for missing parameters.
     if (!isset($sender)) {
         $sender = $config->from_address['value'];
     }
     $destination = isset($recipient->name) ? "\"" . str_replace('"', '', $recipient->name) . "\" <{$recipient->email}>" : (isset($recipient->email) ? $recipient->email : '');
     // Validate incoming parameters.
     if (!preg_match('/^.+@.+$/', $sender)) {
         throw new \InvalidArgumentException('Invalid value for email sender.');
     }
     if (!preg_match('/^.+@.+$/', $destination)) {
         throw new \InvalidArgumentException('Invalid value for email recipient.');
     }
     if (!isset($body['subject']) || !is_string($body['subject']) || strlen($body['subject']) > 255) {
         throw new \LengthException('Invalid length for email subject.');
     }
     if (!isset($body['content']) || !is_string($body['content']) || strlen($body['content']) < 1) {
         throw new \UnexpectedValueException('Invalid value for email message.');
     }
     // Assign the complete variables.
     $this->sender = $sender;
     $this->recipient = $destination;
     $this->subject = $body['subject'];
     $this->message = $body['content'];
     // Define some default MIME headers
     $this->headers['MIME-Version'] = '1.0';
     $this->headers['Content-Type'] = 'multipart/mixed;boundary="MIME_BOUNDRY"';
     //$this->headers['X-Mailer'] = 'PHP5';
     $this->headers['X-Priority'] = '3';
     $this->headers['User-Agent'] = 'µMailPHP ' . Mail::VERSION;
     // Define some default MIME types
     $this->mimeTypes['doc'] = 'application/msword';
     $this->mimeTypes['pdf'] = 'application/pdf';
     $this->mimeTypes['gz'] = 'application/x-gzip';
     $this->mimeTypes['exe'] = 'application/x-msdos-program';
     $this->mimeTypes['rar'] = 'application/x-rar-compressed';
     $this->mimeTypes['swf'] = 'application/x-shockwave-flash';
     $this->mimeTypes['tgz'] = 'application/x-tar-gz';
     $this->mimeTypes['tar'] = 'application/x-tar';
     $this->mimeTypes['zip'] = 'application/zip';
     $this->mimeTypes['mid'] = 'audio/midi';
     $this->mimeTypes['mp3'] = 'audio/mpeg';
     $this->mimeTypes['au'] = 'audio/ulaw';
     $this->mimeTypes['aif'] = 'audio/x-aiff';
     $this->mimeTypes['aiff'] = 'audio/x-aiff';
     $this->mimeTypes['wma'] = 'audio/x-ms-wma';
     $this->mimeTypes['wav'] = 'audio/x-wav';
     $this->mimeTypes['gif'] = 'image/gif';
     $this->mimeTypes['jpg'] = 'image/jpeg';
     $this->mimeTypes['jpeg'] = 'image/jpeg';
     $this->mimeTypes['jpe'] = 'image/jpeg';
     $this->mimeTypes['png'] = 'image/png';
     $this->mimeTypes['tif'] = 'image/tiff';
     $this->mimeTypes['tiff'] = 'image/tiff';
     $this->mimeTypes['css'] = 'text/css';
     $this->mimeTypes['htm'] = 'text/html';
     $this->mimeTypes['html'] = 'text/html';
     $this->mimeTypes['txt'] = 'text/plain';
     $this->mimeTypes['rtf'] = 'text/rtf';
     $this->mimeTypes['xml'] = 'text/xml';
     $this->mimeTypes['flv'] = 'video/flv';
     $this->mimeTypes['mpe'] = 'video/mpeg';
     $this->mimeTypes['mpeg'] = 'video/mpeg';
     $this->mimeTypes['mpg'] = 'video/mpeg';
     $this->mimeTypes['mov'] = 'video/quicktime';
     $this->mimeTypes['asf'] = 'video/x-ms-asf';
     $this->mimeTypes['wmv'] = 'video/x-ms-wmv';
     $this->mimeTypes['avi'] = 'video/x-msvideo';
 }
Esempio n. 2
0
 /**
  * @depends testCreateEntity
  */
 public function testWrongDeepSelector($arr)
 {
     $testEntity = $arr['entity'];
     // Testing wrong deep selectors...
     $resultEntity = Nymph::getEntities(['class' => 'TestModel'], ['&', ['&', '!ref' => ['ref_array', $arr['refGuid'] + 2], '!lte' => ['number', 29.99]], ['&', 'gte' => ['number', 16000]]]);
     $this->assertFalse($testEntity->inArray($resultEntity));
 }