Exemple #1
0
 /**
  * Returns the content of the newsletter with validation messages. The content
  * is also "fixed" automatically when possible.
  * @param Newsletter $newsletter
  * @param string $language language of the content of the newsletter (the 'L' parameter in TYPO3 URL)
  * @return array
  */
 public function validate(Newsletter $newsletter, $language = null)
 {
     $this->initializeLang();
     // Reset stuff
     $this->newsletter = $newsletter;
     $this->content = '';
     $this->errors = [];
     $this->warnings = [];
     $this->infos = [];
     // We need to catch the exception if domain was not found/configured properly
     // or if we can't fetch the content (eg: because of improper SSL certificates)
     try {
         $url = $this->newsletter->getContentUrl($language);
         $this->content = $this->getURL($url);
     } catch (\Exception $e) {
         $this->errors[] = $e->getMessage();
         return $this->getResult();
     }
     $this->infos[] = sprintf($this->lang->getLL('validation_content_url'), '<a target="_blank" href="' . $url . '">' . $url . '</a>');
     $this->errorTooShort();
     $this->errorPhpWarnings();
     $this->errorPhpErrors();
     $this->errorPageBeingGenerated();
     $this->infoRelativeToAbsolute();
     $this->infoLinkedCss();
     $this->warningJavascript();
     $this->errorImageInCSS();
     $this->warningCssClasses();
     $this->warningCssProperties();
     $this->infoImageAlt();
     return $this->getResult();
 }
Exemple #2
0
 /**
  * Returns the content of the newsletter with validation messages. The content
  * is also "fixed" automatically when possible.
  * @param Newsletter $newsletter
  * @param string $language language of the content of the newsletter (the 'L' parameter in TYPO3 URL)
  * @return array ('content' => $content, 'errors' => $errors, 'warnings' => $warnings, 'infos' => $infos);
  */
 public function validate(Newsletter $newsletter, $language = null)
 {
     $this->initializeLang();
     // We need to catch the exception if domain was not found/configured properly
     try {
         $url = $newsletter->getContentUrl($language);
     } catch (Exception $e) {
         return array('content' => '', 'errors' => array($e->getMessage()), 'warnings' => array(), 'infos' => array());
     }
     $content = $this->getURL($url);
     $errors = array();
     $warnings = array();
     $infos = array(sprintf($this->lang->getLL('validation_content_url'), '<a target="_blank" href="' . $url . '">' . $url . '</a>'));
     // Content should be more that just a few characters. Apache error propably occured
     if (strlen($content) < 200) {
         $errors[] = $this->lang->getLL('validation_mail_too_short');
     }
     // Content should not contain PHP-Warnings
     if (substr($content, 0, 22) == "<br />\n<b>Warning</b>:") {
         $errors[] = $this->lang->getLL('validation_mail_contains_php_warnings');
     }
     // Content should not contain PHP-Warnings
     if (substr($content, 0, 26) == "<br />\n<b>Fatal error</b>:") {
         $errors[] = $this->lang->getLL('validation_mail_contains_php_errors');
     }
     // If the page contains a "Pages is being generared" text... this is bad too
     if (strpos($content, 'Page is being generated.') && strpos($content, 'If this message does not disappear within')) {
         $errors[] = $this->lang->getLL('validation_mail_being_generated');
     }
     // Find out the absolute domain. If specified in HTML source, use it as is.
     if (preg_match('|<base[^>]*href="([^"]*)"[^>]*/>|i', $content, $match)) {
         $absoluteDomain = $match[1];
     } else {
         $absoluteDomain = $newsletter->getBaseUrl() . '/';
     }
     // Fix relative URL to absolute URL
     $urlPatterns = array('hyperlinks' => '/<a [^>]*href="(.*)"/Ui', 'stylesheets' => '/<link [^>]*href="(.*)"/Ui', 'images' => '/ src="(.*)"/Ui', 'background images' => '/ background="(.*)"/Ui');
     foreach ($urlPatterns as $type => $urlPattern) {
         preg_match_all($urlPattern, $content, $urls);
         $replacementCount = 0;
         foreach ($urls[1] as $i => $url) {
             // If this is already an absolute link, dont replace it
             $decodedUrl = html_entity_decode($url);
             if (!Uri::isAbsolute($decodedUrl)) {
                 $replace_url = str_replace($decodedUrl, $absoluteDomain . ltrim($decodedUrl, '/'), $urls[0][$i]);
                 $content = str_replace($urls[0][$i], $replace_url, $content);
                 ++$replacementCount;
             }
         }
         if ($replacementCount) {
             $infos[] = sprintf($this->lang->getLL('validation_mail_converted_relative_url'), $type);
         }
     }
     // Find linked css and convert into a style-tag
     preg_match_all('|<link rel="stylesheet" type="text/css" href="([^"]+)"[^>]+>|Ui', $content, $urls);
     foreach ($urls[1] as $i => $url) {
         $content = str_replace($urls[0][$i], "<!-- fetched URL: {$url} -->\n<style type=\"text/css\">\n<!--\n" . $this->getURL($url) . "\n-->\n</style>", $content);
     }
     if (count($urls[1])) {
         $infos[] = $this->lang->getLL('validation_mail_contains_linked_styles');
     }
     // We cant very well have attached javascript in a newsmail ... removing
     $content = preg_replace('|<script[^>]*type="text/javascript"[^>]*>[^<]*</script>|i', '', $content, -1, $count);
     if ($count) {
         $warnings[] = $this->lang->getLL('validation_mail_contains_javascript');
     }
     // Images in CSS
     if (preg_match('|background-image: url\\([^\\)]+\\)|', $content) || preg_match('|list-style-image: url\\([^\\)]+\\)|', $content)) {
         $errors[] = $this->lang->getLL('validation_mail_contains_css_images');
     }
     // CSS-classes
     if (preg_match('|<[a-z]+ [^>]*class="[^"]+"[^>]*>|', $content)) {
         $warnings[] = $this->lang->getLL('validation_mail_contains_css_classes');
     }
     // Positioning & element sizes in CSS
     $forbiddenCssProperties = array('width' => '((min|max)+-)?width', 'height' => '((min|max)+-)?height', 'margin' => 'margin(-(bottom|left|right|top)+)?', 'padding' => 'padding(-(bottom|left|right|top)+)?', 'position' => 'position');
     $forbiddenCssPropertiesWarnings = array();
     if (preg_match_all('|<[a-z]+[^>]+style="([^"]*)"|', $content, $matches)) {
         foreach ($matches[1] as $stylepart) {
             foreach ($forbiddenCssProperties as $property => $regex) {
                 if (preg_match('/(^|[^\\w-])' . $regex . '[^\\w-]/', $stylepart)) {
                     $forbiddenCssPropertiesWarnings[$property] = $property;
                 }
             }
         }
         foreach ($forbiddenCssPropertiesWarnings as $property) {
             $warnings[] = sprintf($this->lang->getLL('validation_mail_contains_css_some_property'), $property);
         }
     }
     return array('content' => $content, 'errors' => $errors, 'warnings' => $warnings, 'infos' => $infos);
 }