示例#1
0
 /** public static function to put the CSS in the mailing
  * @param string $body
  * @param int $templateId
  */
 public static function includeStyles(&$body, $templateId)
 {
     static $checkMe = true;
     $body = preg_replace('#< *(tr|td|table)([^>]*)(style="[^"]*)background-image *: *url\\(\'?([^)\']*)\'?\\);?#Ui', '<$1 background="$4" $2 $3', $body);
     jNews_ProcessMail::normalizeURL($body);
     $body = preg_replace('#< *img([^>]*)(style="[^"]*)(float *: *)(right|left|top|bottom|middle)#Ui', '<img$1 align="$4" hspace="5" $2$3$4', $body);
     //load template
     $template = jNews_Templates::loadOneTemplate('*', $templateId);
     $template->csstyle = jNews_Templates::cleanCSSComments($template->csstyle);
     if (!empty($template->styles) || !empty($template->csstyle)) {
         $csstyle = jNews_Templates::convertCSSInline($template->styles, $template->csstyle);
     }
     $themeCSS = array();
     if (!empty($template->styles)) {
         foreach ($template->styles as $cName => $sContent) {
             if ($cName != 'color_bg') {
             } elseif (preg_match('#^tag_(.*)$#', $cName, $result)) {
                 $mainStyle['#< *' . $result[1] . '((?:(?!style).)*)>#Ui'] = '<' . $result[1] . ' style="' . $sContent . '" $1>';
             } else {
                 $themeCSS['class="' . $cName . '"'] = 'style="' . $sContent . '"';
             }
         }
     }
     if (!empty($mainStyle)) {
         $body = preg_replace(array_keys($mainStyle), $mainStyle, $body);
     }
     if (!empty($themeCSS)) {
         $body = str_replace(array_keys($themeCSS), $themeCSS, $body);
     }
     if (!empty($template->styles['color_bg'])) {
         $pattern1 = '#^([^<]*<[^>]*background-color:)([^;">]{1,10})#i';
         $found = false;
         if (preg_match($pattern1, $body)) {
             $body = preg_replace($pattern1, '$1' . $template->styles['color_bg'], $body);
             $found = true;
         }
         $pattern2 = '#^([^<]*<[^>]*bgcolor=")([^;">]{1,10})#i';
         if (preg_match($pattern2, $body)) {
             $body = preg_replace($pattern2, '$1' . $template->styles['color_bg'], $body);
             $found = true;
         }
         if (!$found) {
             $body = '<div style="background-color:' . $template->styles['color_bg'] . ';" width="100%">' . $body . '</div>';
         }
     }
     if ($checkMe) {
         if (version_compare(PHP_VERSION, '5.0.0', '>=') && class_exists('DOMDocument') && function_exists('mb_convert_encoding')) {
             require_once JNEWS_PATH_INCLUDES . 'emogrifier' . DS . 'emogrifier.php';
         }
         $checkMe = false;
     }
     if (!empty($csstyle) && class_exists('Emogrifier')) {
         $emogrifier = new Emogrifier($body, $csstyle);
         $body = $emogrifier->emogrify();
         if (strpos($body, '<!DOCTYPE') !== false) {
             $body = preg_replace('#<\\!DOCTYPE.*<body([^>]*)>#Usi', '', $body);
             $body = preg_replace('#</body>.*$#si', '', $body);
         }
     }
 }