4 * (c) 2009-2011 Collectif SPIP
5 * Distribue sous licence GPL
9 if (!defined("_ECRIRE_INC_VERSION")) return;
12 * Transformer un mail texte ou HTML simplifie en mail HTML complet avec le wrapper emails/texte.html
13 * Si le mail est un mail texte :
14 * la premiere ligne est le sujet
15 * le reste est le corps du mail
17 * Si le mail est un mail HTML simplifie :
18 * le sujet est entre <title></title>
19 * le corps est entre <body></body>
20 * une eventuelle intro peut etre fournie entre <intro></intro>
22 * @param string $texte_ou_html
25 function facteur_email_wrap_to_html($texte_ou_html){
26 $texte_ou_html = trim($texte_ou_html);
27 // attention : si pas de contenu on renvoi du vide aussi (mail vide = mail vide)
28 if (!strlen(trim($texte_ou_html)))
29 return $texte_ou_html;
31 $contexte = array("sujet"=>"","texte"=>"","intro"=>"");
33 // tester si le mail est en html (simplifie)
34 if (substr($texte_ou_html,0,1)=="<"
35 AND substr($texte_ou_html,-1,1)==">"
36 AND stripos($texte_ou_html,"</body>")!==false){
38 // dans ce cas on ruse un peu : extraire le sujet du title
40 if (preg_match(",<title>(.*)</title>,Uims",$texte_ou_html,$m)){
41 $contexte['sujet'] = $m[1];
42 $texte_ou_html = preg_replace(",<title>(.*)</title>,Uims","",$texte_ou_html,1);
43 $texte_ou_html = trim($texte_ou_html);
45 if (preg_match(",<intro>(.*)</intro>,Uims",$texte_ou_html,$m)){
46 $contexte['intro'] = $m[1];
47 $texte_ou_html = preg_replace(",<intro>(.*)</intro>,Uims","",$texte_ou_html,1);
48 $texte_ou_html = trim($texte_ou_html);
50 $contexte['html'] = preg_replace(",</?body>,ims","",$texte_ou_html);
53 // la premiere ligne est toujours le sujet
54 $texte_ou_html = explode("\n",$texte_ou_html);
55 $contexte['sujet'] = trim(array_shift($texte_ou_html));
56 $contexte['texte'] = trim(implode("\n",$texte_ou_html));
59 // attention : si pas de contenu on renvoi du vide aussi (mail vide = mail vide)
60 if (!strlen(trim(implode("",$contexte))))
63 return recuperer_fond("emails/texte",$contexte);
68 Written by Eric Dols - edols@auditavenue.com
70 You may freely use or modify this, provided
71 you leave credits to the original coder.
72 Feedback about (un)successfull uses, bugs and improvements done
73 are much appreciated, but don't expect actual support.
75 PURPOSE OF THIS FUNCTION
76 It is designed to process html emails relying
77 on a css stylesheet placed in the <head> for layout in
78 order to enhance compatibility with email clients,
79 including webmail services.
80 Provided you use minimal css, you can keep styling separate
81 from the content in your email template, and let this function
82 "inject" those styles inline in your email html tags on-the-fly,
84 Technically, it grabs the style declarations found in the
85 <head> section and inserts each declaration inline,
86 inside the corresponding html tags in the email message.
88 Supports both HTML and XHTML markup seamlessly. Thus
89 tolerant to email message writers using non-xhtml tag,
90 even when template is xhtml compliant (e.g. they would
91 add <img ...> instead of a xhtml compliant <img ... />).
94 - code revised, including a few regexp bugs fixed.
95 - multiple class for a tag are now allowed <p class="firstclass secondclass">
96 - all unsupported css styles are now moved to the body section (not just a:hover etc...)
99 Add this function to a function library include, like "inline.inc"
100 and include it near the beginning of your php page:
101 require ("inline.inc");
103 load the html source of message into a variable
104 like $html_source and process it using:
105 $html_source = sheet2inline($html_source)
108 STYLE DEFINITIONS SUPPORTED
110 TAG1, TAG2, ... { ... }
116 CSS definitions may be freely formatted (spaces, tabs, linefeeds...),
117 they are converted to oneliners before inserting them inline in the html tags.
119 .class definitions are processed AFTER tag definitions,
120 thus appended inline after any existing tag styling to
121 preserve the normal css priority behavior.
123 Existing style="..." attributes in tags are NOT stripped. However they MUST
124 be with double quotes. If not, an addtional style="..." attribute will be added
128 - style info should be placed in <head> section. I believe
129 it shouldnt be too hard to modify to point to an external
132 * chains like P UL LI { .... } or P UL LI.class { .... }
133 * #divname p { ... } and <tag id="...">
134 * a:hover, a:visited {...} multiple class:pseudo
135 They require a significantly more complicated processing likely
136 based on stylesheet and document trees parsing.
137 Many email clients don't handle more than what is supported
138 by this script anyway.
139 - pseudo-classes like a:hover {...} can't be inserted inline
140 in the html tags: they are moved to a <style> declaration in
141 the <body> instead. This is a limitation from html, not this script.
142 - It is still up to you to check if target email clients render
143 your css styled templates correctly, especially webmail services
144 like Hotmail, in which the email becomes a sub-part of an html page,
145 with styles already in place.
147 function facteur_convertir_styles_inline($body){
148 // variables to be accessed in the callback sub-function too
149 global $styledefinition, $styletag, $styleclass;
151 // Let's first load the stylesheet information in a $styles array using a regexp
152 preg_match_all ( "/^[ \t]*([.]?)([\w, #]+)([.:])?(\S*)\s+{([^}]+)}/mi", $body , $styles);
154 $styles[1] = . or '' => .class or tag (empty)
155 $styles[2] = name of class or tag(s)
156 $styles[3] = : . or '' => followed by pseudo-element, class separator or nothing (empty)
157 $styles[4] = name of pseudo-element after a tag, if any
158 $styles[5] = the style definition itself, i.e. what's between the { }
161 // Now loop through the styles found and act accordingly;
163 // process TAG {...} & TAG1, TAG2,... {...} definitions only first by order of appearance
164 foreach ($styles[1] as $i => $type) {
165 if ($type=="" && $styles[3][$i]=="") {
166 $styledefinition = trim($styles[5][$i]);
167 $styletag = preg_replace("/ *, */", "|", trim($styles[2][$i])); //echo $styletag."<br />";
169 // process TAG {...} and TAG1, TAG2 {...} but not TAG1 TAG2 {...} or #divname styles
170 if (!preg_match("/ /", $styletag) && !preg_match("/#/", $styletag)) {
171 $pattern = "!<(".$styletag.")([^>]*(?= /)|[^>]*)( /)?>!mi";
172 $body = preg_replace_callback ($pattern, 'facteur_addstyle' , $body);
173 $styles[6][$i]=1; // mark as injected inline
178 // append additional .CLASS {...} and TAG.CLASS {...} styling by order of appearance
179 // important to do so after TAG {...} definitions, so that class attributes override TAG styles when needed
180 foreach ($styles[1] as $i => $type) {
181 if ($type!="." && $styles[3][$i]=="." ) { // class definition for a specific tag
182 $styledefinition = trim($styles[5][$i]);
183 $styletag = trim($styles[2][$i]);
184 $styleclass = trim($styles[4][$i]);
185 $pattern = "!<(".$styletag.")([^>]* class\=['\"][^'\"]*".$styleclass."[^'\"]*['\"][^>]*(?= /)|[^>]* class\=['\"][^'\"]*".$styleclass."[^'\"]*['\"][^>]*)( />)?>!mi";
186 $body = preg_replace_callback ($pattern, 'facteur_addstyle' , $body);
187 $styles[6][$i]=1; // mark as injected inline
190 elseif ($type=="." && $styles[3][$i]=="" ) { // general class definition for any tag
191 $styledefinition = trim($styles[5][$i]);
193 $styleclass = trim($styles[2][$i]);
194 $pattern = "!<(\w+)([^>]* class\=['\"]".$styleclass."['\"][^>]*(?= /)|[^>]* class\=['\"]".$styleclass."['\"][^>]*)( />)?>!mi";
195 $body = preg_replace_callback ($pattern, 'facteur_addstyle' , $body);
196 $styles[6][$i]=1; // mark as injected inline
201 /* move all style declarations that weren't injected from <head> to a <body> <style> section,
202 including but not limited to:
203 - pseudo-classes like a:hover {...} as they can't be set inline
204 - declaration chains like UL LI {...}
205 - #divname {...}. These are not supported by email clients like Mac/Entourage anyway, it seems. */
206 foreach ($styles[1] as $i => $type) {
207 if ($styles[6][$i]=="") {
208 // add a <style type="text/css"> section after <body> if there's isn't one yet
209 if (preg_match ("!<body[^>]*>\s*<style!mi", $body)==0) {
210 $body = preg_replace ("/(<body[^>]*>)/i", "\n\$1\n".'<style type="text/css">'."\n<!--\n-->\n</style>\n", $body);
212 // append a copy of the pseudo-element declaration to that body style section
213 $styledefinition = trim($styles[5][$i]);
214 $styledefinition = preg_replace ("!\s+!mi", " ", $styledefinition ); // convert style definition to a one-liner (optional)
215 $declaration = $styles[1][$i].trim($styles[2][$i]).$styles[3][$i].trim($styles[4][$i])." { ".$styledefinition." }";
216 $body = preg_replace ("!(<body[^>]*>\s*<style[^>]*>\s*<\!\-\-[^>]*)"."(\s*\-\->\s*</style>)!si", "\$1".$declaration."\n\$2", $body);
217 $styles[6][$i]= 2; // mark as moved to <style> section in <body>
221 // remove stylesheet declaration(s) from <head> section (comment following line out if not wanted)
222 //$body = preg_replace ("!(<head>.*)<style type.*</style>(.*</head>)!si", "\$1\$2" , $body);
224 // check what styles have been injected
237 function facteur_addstyle($matches) {
239 // $matches[1]=tag, $matches[2]=tag attributes (if any), $matches[3]=xhtml closing (if any)
241 // variables values set in calling function
242 global $styledefinition, $styletag, $styleclass;
244 // convert the style definition to a one-liner
245 $styledefinition = preg_replace ("!\s+!mi", " ", $styledefinition );
246 // convert all double-quotes to single-quotes
247 $styledefinition = preg_replace ('/"/','\'', $styledefinition );
249 if (preg_match ("/style\=/i", $matches[2])) {
250 // add styles to existing style attribute if any already in the tag
251 $pattern = "!(.* style\=)[\"]([^\"]*)[\"](.*)!mi";
252 $replacement = "\$1".'"'."\$2 ".$styledefinition.'"'."\$3";
253 $attributes = preg_replace ($pattern, $replacement , $matches[2]);
255 // otherwise add new style attribute to tag (none was present)
256 $attributes = $matches[2].' style="'.$styledefinition.'"';
259 if ($styleclass!="") {
260 // if we were injecting a class style, remove the now useless class attribute from the html tag
262 // Single class in tag case (class="classname"): remove class attribute altogether
263 $pattern = "!(.*) class\=['\"]".$styleclass."['\"](.*)!mi";
264 $replacement = "\$1\$2";
265 $attributes = preg_replace ( $pattern, $replacement, $attributes);
267 // Multiple classes in tag case (class="classname anotherclass..."): remove class name from class attribute.
268 // classes are injected inline and removed by order of appearance in <head> stylesheet
269 // exact same behavior as where last declared class attributes in <style> take over (IE6 tested only)
270 $pattern = "!(.* class\=['\"][^\"]*)(".$styleclass." | ".$styleclass.")([^\"]*['\"].*)!mi";
271 $replacement = "\$1\$3";
272 $attributes = preg_replace ( $pattern, $replacement, $attributes);
276 return "<".$matches[1].$attributes.$matches[3].">";
280 * Un filtre pour transformer les retour ligne texte en br si besoin (si pas autobr actif)
282 * @param string $texte
285 function facteur_nl2br_si_pas_autobr($texte){
286 return (_AUTOBR?
$texte:nl2br($texte));
290 * Transformer un mail HTML en mail Texte proprement :
291 * - les tableaux de mise en page sont utilisés pour structurer le mail texte
292 * - le reste du HTML est markdownifie car c'est un format texte lisible et conventionnel
294 * @param string $html
297 function facteur_mail_html2text($html){
298 // nettoyer les balises de mise en page html
299 $html = preg_replace(",</(td|th)>,Uims","<br/>",$html);
300 $html = preg_replace(",</(table)>,Uims","@@@hr@@@",$html);
301 $html = preg_replace(",</?(html|body|table|td|th|tbody|thead|center|article|section|span)[^>]*>,Uims","\n\n",$html);
303 // commentaires html et conditionnels
304 $html = preg_replace(",<!--.*-->,Uims","\n",$html);
305 $html = preg_replace(",<!\[.*\]>,Uims","\n",$html);
307 $html = preg_replace(",<(/?)(div|tr|caption)([^>]*>),Uims","<\\1p>",$html);
308 $html = preg_replace(",(<p>\s*)+,ims","<p>",$html);
309 $html = preg_replace(",<br/?>\s*</p>,ims","</p>",$html);
310 $html = preg_replace(",</p>\s*<br/?>,ims","</p>",$html);
311 $html = preg_replace(",(</p>\s*(@@@hr@@@)?\s*)+,ims","</p>\\2",$html);
312 $html = preg_replace(",(<p>\s*</p>),ims","",$html);
314 // succession @@@hr@@@<hr> et <hr>@@@hr@@@
315 $html = preg_replace(",@@@hr@@@\s*(<[^>]*>\s*)?<hr[^>]*>,ims","@@@hr@@@\n",$html);
316 $html = preg_replace(",<hr[^>]*>\s*(<[^>]*>\s*)?@@@hr@@@,ims","\n@@@hr@@@",$html);
318 $html = preg_replace(",<textarea[^>]*spip_cadre[^>]*>(.*)</textarea>,Uims","<code>\n\\1\n</code>",$html);
320 // vider le contenu de qqunes :
321 $html = preg_replace(",<head[^>]*>.*</head>,Uims","\n",$html);
324 // Nettoyage des liens des notes de bas de page
325 $html = preg_replace("@<a href=\"#n(b|h)[0-9]+-[0-9]+\" name=\"n(b|h)[0-9]+-[0-9]+\" class=\"spip_note\">([0-9]+)</a>@", "\\3", $html);
326 // Supprimer tous les liens internes
327 $html = preg_replace("/\<a href=['\"]#(.*?)['\"][^>]*>(.*?)<\/a>/ims","\\2", $html);
328 // Remplace tous les liens
329 preg_match_all("/\<a href=['\"](.*?)['\"][^>]*>(.*?)<\/a>/ims", $html,$matches,PREG_SET_ORDER
);
330 $prelinks = $postlinks = array();
331 foreach ($matches as $k => $match){
332 $link = "@@@link$k@@@";
333 $url = str_replace("&","&",$match[1]);
334 if ($match[2]==$match[1] OR $match[2]==$url){
335 // si le texte est l'url :
336 $prelinks[$match[0]] = "$link";
340 $prelinks[$match[0]] = $match[2] . " ($link)";
342 $postlinks[$link] = $url;
344 $html = str_replace(array_keys($prelinks), array_values($prelinks),$html);
346 // les images par leur alt ?
347 // au moins les puces
348 $html = preg_replace(',<img\s[^>]*alt="-"[^>]*>,Uims','-',$html);
350 $html = preg_replace(',<img\s[^>]*alt=[\'"]([^\'"]*)[\'"][^>]*>,Uims',"\\1",$html);
351 // on vire celles sans alt
352 $html = preg_replace(",</?(img)[^>]*>,Uims","\n",$html);
355 $html = str_replace(" "," ",$html);
356 $html = preg_replace(",<p>\s+,ims","<p>",$html);
359 include_spip("lib/markdownify/markdownify");
360 $parser = new Markdownify('inline',false,false);
361 $texte = $parser->parseString($html);
363 $texte = str_replace(array_keys($postlinks), array_values($postlinks),$texte);
366 // trim et sauts de ligne en trop ou pas assez
367 $texte = trim($texte);
368 $texte = str_replace("<br />\n","\n",$texte);
369 $texte = preg_replace(",(@@@hr@@@\s*)+\Z,ims","",$texte);
370 $texte = preg_replace(",(@@@hr@@@\s*\n)+,ims","\n\n\n".str_pad("-",75,"-")."\n\n\n",$texte);
371 $texte = preg_replace(",(\n#+\s),ims","\n\n\\1",$texte);
372 $texte = preg_replace(",(\n\s*)(\n\s*)+(\n)+,ims","\n\n\n",$texte);
375 // <p> et </p> restants
376 $texte = str_replace(array("<p>","</p>"),array("",""),$texte);
378 // entites restantes ? (dans du code...)
379 include_spip('inc/charsets');
380 $texte = unicode2charset($texte);
381 $texte = str_replace(array(''', '"'),array("'",'"'), $texte);
384 // Faire des lignes de 75 caracteres maximum
385 return trim(wordwrap($texte));