source: branches/2.6/include/emogrifier.class.php @ 26999

Revision 26999, 20.1 KB checked in by mistic100, 6 years ago (diff)

Merged revision(s) 26972, 26998 from trunk:
replace more preg_replace callback
........
remove *_version_compare methods in languages & plugins & themes classes, unused and outdated (preg_replace /e modifier)

Line 
1<?php
2/*
3UPDATES
4
5    2008-08-10  Fixed CSS comment stripping regex to add PCRE_DOTALL (changed from '/\/\*.*\*\//U' to '/\/\*.*\*\//sU')
6    2008-08-18  Added lines instructing DOMDocument to attempt to normalize HTML before processing
7    2008-10-20  Fixed bug with bad variable name... Thanks Thomas!
8    2008-03-02  Added licensing terms under the MIT License
9                Only remove unprocessable HTML tags if they exist in the array
10    2009-06-03  Normalize existing CSS (style) attributes in the HTML before we process the CSS.
11                Made it so that the display:none stripper doesn't require a trailing semi-colon.
12    2009-08-13  Added support for subset class values (e.g. "p.class1.class2").
13                Added better protection for bad css attributes.
14                Fixed support for HTML entities.
15    2009-08-17  Fixed CSS selector processing so that selectors are processed by precedence/specificity, and not just in order.
16    2009-10-29  Fixed so that selectors appearing later in the CSS will have precedence over identical selectors appearing earlier.
17    2009-11-04  Explicitly declared static functions static to get rid of E_STRICT notices.
18    2010-05-18  Fixed bug where full url filenames with protocols wouldn't get split improperly when we explode on ':'... Thanks Mark!
19                Added two new attribute selectors
20    2010-06-16  Added static caching for less processing overhead in situations where multiple emogrification takes place
21    2010-07-26  Fixed bug where '0' values were getting discarded because of php's empty() function... Thanks Scott!
22    2010-09-03  Added checks to invisible node removal to ensure that we don't try to remove non-existent child nodes of parents that have already been deleted
23    2011-04-08  Fixed errors in CSS->XPath conversion for adjacent sibling selectors and id/class combinations... Thanks Bob V.!
24    2011-06-08  Fixed an error where CSS @media types weren't being parsed correctly... Thanks Will W.!
25    2011-08-03  Fixed an error where an empty selector at the beginning of the CSS would cause a parse error on the next selector... Thanks Alexei T.!
26    2011-10-13  Fully fixed a bug introduced in 2011-06-08 where selectors at the beginning of the CSS would be parsed incorrectly... Thanks Thomas A.!
27    2011-10-26  Added an option to allow you to output emogrified code without extended characters being turned into HTML entities.
28                Moved static references to class attributes so they can be manipulated.
29                Added the ability to clear out the (formerly) static cache when CSS is reloaded.
30    2011-12-22  Fixed a bug that was overwriting existing inline styles from the original HTML... Thanks Sagi L.!
31    2012-01-31  Fixed a bug that was introduced with the 2011-12-22 revision... Thanks Sagi L. and M. Bąkowski!
32                Added extraction of <style> blocks within the HTML due to popular demand.
33                Added several new pseudo-selectors (first-child, last-child, nth-child, and nth-of-type).
34    2012-02-07  Fixed some recent code introductions to use class constants rather than global constants.
35                Fixed some recent code introductions to make it cleaner to read.
36    2012-05-01  Made removal of invisible nodes operate in a case-insensitive manner... Thanks Juha P.!
37    2013-10-10  Add preserveStyleTag option
38    2014-01-26  PHP 5.5 compatibility (/e modifier is deprecated in preg_replace)
39*/
40
41define('CACHE_CSS', 0);
42define('CACHE_SELECTOR', 1);
43define('CACHE_XPATH', 2);
44
45class Emogrifier {
46
47    // for calculating nth-of-type and nth-child selectors
48    const INDEX = 0;
49    const MULTIPLIER = 1;
50
51    private $html = '';
52    private $css = '';
53    private $unprocessableHTMLTags = array('wbr');
54    private $caches = array();
55
56    // this attribute applies to the case where you want to preserve your original text encoding.
57    // by default, emogrifier translates your text into HTML entities for two reasons:
58    // 1. because of client incompatibilities, it is better practice to send out HTML entities rather than unicode over email
59    // 2. it translates any illegal XML characters that DOMDocument cannot work with
60    // if you would like to preserve your original encoding, set this attribute to true.
61    public $preserveEncoding = false;
62   
63    // by default, emogrifier removes <style> tags, set preserveStyleTag to true to keep them
64    public $preserveStyleTag = false;
65
66    public function __construct($html = '', $css = '') {
67        $this->html = $html;
68        $this->css  = $css;
69        $this->clearCache();
70    }
71
72    public function setHTML($html = '') { $this->html = $html; }
73    public function setCSS($css = '') {
74        $this->css = $css;
75        $this->clearCache(CACHE_CSS);
76    }
77
78    public function clearCache($key = null) {
79        if (!is_null($key)) {
80            if (isset($this->caches[$key])) $this->caches[$key] = array();
81        } else {
82            $this->caches = array(
83                CACHE_CSS       => array(),
84                CACHE_SELECTOR  => array(),
85                CACHE_XPATH     => array(),
86            );
87        }
88    }
89
90    // there are some HTML tags that DOMDocument cannot process, and will throw an error if it encounters them.
91    // in particular, DOMDocument will complain if you try to use HTML5 tags in an XHTML document.
92    // these functions allow you to add/remove them if necessary.
93    // it only strips them from the code (does not remove actual nodes).
94    public function addUnprocessableHTMLTag($tag) { $this->unprocessableHTMLTags[] = $tag; }
95    public function removeUnprocessableHTMLTag($tag) {
96        if (($key = array_search($tag,$this->unprocessableHTMLTags)) !== false)
97            unset($this->unprocessableHTMLTags[$key]);
98    }
99
100    // applies the CSS you submit to the html you submit. places the css inline
101    public function emogrify() {
102        $body = $this->html;
103
104        // remove any unprocessable HTML tags (tags that DOMDocument cannot parse; this includes wbr and many new HTML5 tags)
105        if (count($this->unprocessableHTMLTags)) {
106            $unprocessableHTMLTags = implode('|',$this->unprocessableHTMLTags);
107            $body = preg_replace("/<\/?($unprocessableHTMLTags)[^>]*>/i",'',$body);
108        }
109
110        $encoding = mb_detect_encoding($body);
111        $body = mb_convert_encoding($body, 'HTML-ENTITIES', $encoding);
112
113        $xmldoc = new DOMDocument;
114        $xmldoc->encoding = $encoding;
115        $xmldoc->strictErrorChecking = false;
116        $xmldoc->formatOutput = true;
117        $xmldoc->loadHTML($body);
118        $xmldoc->normalizeDocument();
119
120        $xpath = new DOMXPath($xmldoc);
121
122        // before be begin processing the CSS file, parse the document and normalize all existing CSS attributes (changes 'DISPLAY: none' to 'display: none');
123        // we wouldn't have to do this if DOMXPath supported XPath 2.0.
124        // also store a reference of nodes with existing inline styles so we don't overwrite them
125        $vistedNodes = $vistedNodeRef = array();
126        $nodes = @$xpath->query('//*[@style]');
127        foreach ($nodes as $node) {
128            $normalizedOrigStyle = preg_replace_callback('/[A-z\-]+(?=\:)/S',create_function('$m', 'return strtolower($m[0]);'),$node->getAttribute('style'));
129
130            // in order to not overwrite existing style attributes in the HTML, we have to save the original HTML styles
131            $nodeKey = md5($node->getNodePath());
132            if (!isset($vistedNodeRef[$nodeKey])) {
133                $vistedNodeRef[$nodeKey] = $this->cssStyleDefinitionToArray($normalizedOrigStyle);
134                $vistedNodes[$nodeKey]   = $node;
135            }
136
137            $node->setAttribute('style', $normalizedOrigStyle);
138        }
139
140        // grab any existing style blocks from the html and append them to the existing CSS
141        // (these blocks should be appended so as to have precedence over conflicting styles in the existing CSS)
142        $css = $this->css;
143        $nodes = @$xpath->query('//style');
144        foreach ($nodes as $node) {
145            // append the css
146            $css .= "\n\n{$node->nodeValue}";
147            // remove the <style> node
148            if (!$this->preserveStyleTag) {
149                $node->parentNode->removeChild($node);
150            }
151        }
152
153        // filter the CSS
154        $search = array(
155            '/\/\*.*\*\//sU', // get rid of css comment code
156            '/^\s*@import\s[^;]+;/misU', // strip out any import directives
157            '/^\s*@media\s[^{]+{\s*}/misU', // strip any empty media enclosures
158            '/^\s*@media\s+((aural|braille|embossed|handheld|print|projection|speech|tty|tv)\s*,*\s*)+{.*}\s*}/misU', // strip out all media types that are not 'screen' or 'all' (these don't apply to email)
159            '/^\s*@media\s[^{]+{(.*})\s*}/misU', // get rid of remaining media type enclosures
160        );
161
162        $replace = array(
163            '',
164            '',
165            '',
166            '',
167            '\\1',
168        );
169
170        $css = preg_replace($search, $replace, $css);
171
172        $csskey = md5($css);
173        if (!isset($this->caches[CACHE_CSS][$csskey])) {
174
175            // process the CSS file for selectors and definitions
176            preg_match_all('/(^|[^{}])\s*([^{]+){([^}]*)}/mis', $css, $matches, PREG_SET_ORDER);
177
178            $all_selectors = array();
179            foreach ($matches as $key => $selectorString) {
180                // if there is a blank definition, skip
181                if (!strlen(trim($selectorString[3]))) continue;
182
183                // else split by commas and duplicate attributes so we can sort by selector precedence
184                $selectors = explode(',',$selectorString[2]);
185                foreach ($selectors as $selector) {
186
187                    // don't process pseudo-elements and behavioral (dynamic) pseudo-classes; ONLY allow structural pseudo-classes
188                    if (strpos($selector, ':') !== false && !preg_match('/:\S+\-(child|type)\(/i', $selector)) continue;
189
190                    $all_selectors[] = array('selector' => trim($selector),
191                                             'attributes' => trim($selectorString[3]),
192                                             'line' => $key, // keep track of where it appears in the file, since order is important
193                    );
194                }
195            }
196
197            // now sort the selectors by precedence
198            usort($all_selectors, array($this,'sortBySelectorPrecedence'));
199
200            $this->caches[CACHE_CSS][$csskey] = $all_selectors;
201        }
202
203        foreach ($this->caches[CACHE_CSS][$csskey] as $value) {
204
205            // query the body for the xpath selector
206            $nodes = $xpath->query($this->translateCSStoXpath(trim($value['selector'])));
207
208            foreach($nodes as $node) {
209                // if it has a style attribute, get it, process it, and append (overwrite) new stuff
210                if ($node->hasAttribute('style')) {
211                    // break it up into an associative array
212                    $oldStyleArr = $this->cssStyleDefinitionToArray($node->getAttribute('style'));
213                    $newStyleArr = $this->cssStyleDefinitionToArray($value['attributes']);
214
215                    // new styles overwrite the old styles (not technically accurate, but close enough)
216                    $combinedArr = array_merge($oldStyleArr,$newStyleArr);
217                    $style = '';
218                    foreach ($combinedArr as $k => $v) $style .= (strtolower($k) . ':' . $v . ';');
219                } else {
220                    // otherwise create a new style
221                    $style = trim($value['attributes']);
222                }
223                $node->setAttribute('style', $style);
224            }
225        }
226
227        // now iterate through the nodes that contained inline styles in the original HTML
228        foreach ($vistedNodeRef as $nodeKey => $origStyleArr) {
229            $node = $vistedNodes[$nodeKey];
230            $currStyleArr = $this->cssStyleDefinitionToArray($node->getAttribute('style'));
231
232            $combinedArr = array_merge($currStyleArr, $origStyleArr);
233            $style = '';
234            foreach ($combinedArr as $k => $v) $style .= (strtolower($k) . ':' . $v . ';');
235
236            $node->setAttribute('style', $style);
237        }
238
239        // This removes styles from your email that contain display:none.
240        // We need to look for display:none, but we need to do a case-insensitive search. Since DOMDocument only supports XPath 1.0,
241        // lower-case() isn't available to us. We've thus far only set attributes to lowercase, not attribute values. Consequently, we need
242        // to translate() the letters that would be in 'NONE' ("NOE") to lowercase.
243        $nodes = $xpath->query('//*[contains(translate(translate(@style," ",""),"NOE","noe"),"display:none")]');
244        // The checks on parentNode and is_callable below ensure that if we've deleted the parent node,
245        // we don't try to call removeChild on a nonexistent child node
246        if ($nodes->length > 0)
247            foreach ($nodes as $node)
248                if ($node->parentNode && is_callable(array($node->parentNode,'removeChild')))
249                        $node->parentNode->removeChild($node);
250
251        if ($this->preserveEncoding) {
252            return mb_convert_encoding($xmldoc->saveHTML(), $encoding, 'HTML-ENTITIES');
253        } else {
254            return $xmldoc->saveHTML();
255        }
256    }
257
258    private function sortBySelectorPrecedence($a, $b) {
259        $precedenceA = $this->getCSSSelectorPrecedence($a['selector']);
260        $precedenceB = $this->getCSSSelectorPrecedence($b['selector']);
261
262        // we want these sorted ascendingly so selectors with lesser precedence get processed first and
263        // selectors with greater precedence get sorted last
264        return ($precedenceA == $precedenceB) ? ($a['line'] < $b['line'] ? -1 : 1) : ($precedenceA < $precedenceB ? -1 : 1);
265    }
266
267    private function getCSSSelectorPrecedence($selector) {
268        $selectorkey = md5($selector);
269        if (!isset($this->caches[CACHE_SELECTOR][$selectorkey])) {
270            $precedence = 0;
271            $value = 100;
272            $search = array('\#','\.',''); // ids: worth 100, classes: worth 10, elements: worth 1
273
274            foreach ($search as $s) {
275                if (trim($selector == '')) break;
276                $num = 0;
277                $selector = preg_replace('/'.$s.'\w+/','',$selector,-1,$num);
278                $precedence += ($value * $num);
279                $value /= 10;
280            }
281            $this->caches[CACHE_SELECTOR][$selectorkey] = $precedence;
282        }
283
284        return $this->caches[CACHE_SELECTOR][$selectorkey];
285    }
286
287    // right now we support all CSS 1 selectors and most CSS2/3 selectors.
288    // http://plasmasturm.org/log/444/
289    private function translateCSStoXpath($css_selector) {
290
291        $css_selector = trim($css_selector);
292        $xpathkey = md5($css_selector);
293        if (!isset($this->caches[CACHE_XPATH][$xpathkey])) {
294            // returns an Xpath selector
295            $search = array(
296                               '/\s+>\s+/', // Matches any element that is a child of parent.
297                               '/\s+\+\s+/', // Matches any element that is an adjacent sibling.
298                               '/\s+/', // Matches any element that is a descendant of an parent element element.
299                               '/([^\/]+):first-child/i', // first-child pseudo-selector
300                               '/([^\/]+):last-child/i', // last-child pseudo-selector
301                               '/(\w)\[(\w+)\]/', // Matches element with attribute
302                               '/(\w)\[(\w+)\=[\'"]?(\w+)[\'"]?\]/', // Matches element with EXACT attribute
303            );
304            $replace = array(
305                               '/',
306                               '/following-sibling::*[1]/self::',
307                               '//',
308                               '*[1]/self::\\1',
309                               '*[last()]/self::\\1',
310                               '\\1[@\\2]',
311                               '\\1[@\\2="\\3"]',
312            );
313
314            $css_selector = '//'.preg_replace($search, $replace, $css_selector);
315
316            // matches ids and classes
317            $css_selector = preg_replace_callback('/(\w+)?\#([\w\-]+)/', array($this, 'matchIdAttributes'), $css_selector);
318            $css_selector = preg_replace_callback('/(\w+|[\*\]])?((\.[\w\-]+)+)/', array($this, 'matchClassAttributes'), $css_selector);
319
320            // advanced selectors are going to require a bit more advanced emogrification
321            // if we required PHP 5.3 we could do this with closures
322            $css_selector = preg_replace_callback('/([^\/]+):nth-child\(\s*(odd|even|[+\-]?\d|[+\-]?\d?n(\s*[+\-]\s*\d)?)\s*\)/i', array($this, 'translateNthChild'), $css_selector);
323            $css_selector = preg_replace_callback('/([^\/]+):nth-of-type\(\s*(odd|even|[+\-]?\d|[+\-]?\d?n(\s*[+\-]\s*\d)?)\s*\)/i', array($this, 'translateNthOfType'), $css_selector);
324
325            $this->caches[CACHE_SELECTOR][$xpathkey] = $css_selector;
326        }
327        return $this->caches[CACHE_SELECTOR][$xpathkey];
328    }
329
330    private function matchIdAttributes($m) {
331      return (strlen($m[1]) ? $m[1] : '*').'[@id="'.$m[2].'"]';
332    }
333
334    private function matchClassAttributes($m) {
335      return (strlen($m[1]) ? $m[1] : '*').'[contains(concat(" ",@class," "),concat(" ","'.implode('"," "))][contains(concat(" ",@class," "),concat(" ","',explode('.',substr($m[2],1))).'"," "))]';
336    }
337
338    private function translateNthChild($match) {
339
340        $result = $this->parseNth($match);
341
342        if (isset($result[self::MULTIPLIER])) {
343            if ($result[self::MULTIPLIER] < 0) {
344                $result[self::MULTIPLIER] = abs($result[self::MULTIPLIER]);
345                return sprintf("*[(last() - position()) mod %u = %u]/self::%s", $result[self::MULTIPLIER], $result[self::INDEX], $match[1]);
346            } else {
347                return sprintf("*[position() mod %u = %u]/self::%s", $result[self::MULTIPLIER], $result[self::INDEX], $match[1]);
348            }
349        } else {
350            return sprintf("*[%u]/self::%s", $result[self::INDEX], $match[1]);
351        }
352    }
353
354    private function translateNthOfType($match) {
355
356        $result = $this->parseNth($match);
357
358        if (isset($result[self::MULTIPLIER])) {
359            if ($result[self::MULTIPLIER] < 0) {
360                $result[self::MULTIPLIER] = abs($result[self::MULTIPLIER]);
361                return sprintf("%s[(last() - position()) mod %u = %u]", $match[1], $result[self::MULTIPLIER], $result[self::INDEX]);
362            } else {
363                return sprintf("%s[position() mod %u = %u]", $match[1], $result[self::MULTIPLIER], $result[self::INDEX]);
364            }
365        } else {
366            return sprintf("%s[%u]", $match[1], $result[self::INDEX]);
367        }
368    }
369
370    private function parseNth($match) {
371
372        if (in_array(strtolower($match[2]), array('even','odd'))) {
373            $index = strtolower($match[2]) == 'even' ? 0 : 1;
374            return array(self::MULTIPLIER => 2, self::INDEX => $index);
375        // if there is a multiplier
376        } else if (stripos($match[2], 'n') === false) {
377            $index = intval(str_replace(' ', '', $match[2]));
378            return array(self::INDEX => $index);
379        } else {
380
381            if (isset($match[3])) {
382                $multiple_term = str_replace($match[3], '', $match[2]);
383                $index = intval(str_replace(' ', '', $match[3]));
384            } else {
385                $multiple_term = $match[2];
386                $index = 0;
387            }
388
389            $multiplier = str_ireplace('n', '', $multiple_term);
390
391            if (!strlen($multiplier)) $multiplier = 1;
392            elseif ($multiplier == 0) return array(self::INDEX => $index);
393            else $multiplier = intval($multiplier);
394
395            while ($index < 0) $index += abs($multiplier);
396
397            return array(self::MULTIPLIER => $multiplier, self::INDEX => $index);
398        }
399    }
400
401    private function cssStyleDefinitionToArray($style) {
402        $definitions = explode(';',$style);
403        $retArr = array();
404        foreach ($definitions as $def) {
405            if (empty($def) || strpos($def, ':') === false) continue;
406            list($key,$value) = explode(':',$def,2);
407            if (empty($key) || strlen(trim($value)) === 0) continue;
408            $retArr[trim($key)] = trim($value);
409        }
410        return $retArr;
411    }
412}
Note: See TracBrowser for help on using the repository browser.