[PLUGINS] +abomailman
[ptitvelo/web/www.git] / www / plugins / facteur / classes / facteur.php
diff --git a/www/plugins/facteur/classes/facteur.php b/www/plugins/facteur/classes/facteur.php
new file mode 100755 (executable)
index 0000000..daedf1e
--- /dev/null
@@ -0,0 +1,347 @@
+<?php\r
+/*\r
+ * Plugin Facteur 2\r
+ * (c) 2009-2011 Collectif SPIP\r
+ * Distribue sous licence GPL\r
+ *\r
+ */\r
+\r
+if (!defined("_ECRIRE_INC_VERSION")) return;\r
+\r
+include_spip('inc/charsets');\r
+include_spip('inc/texte');\r
+include_spip('inc/filtres');\r
+\r
+if (!class_exists('PHPMailer')) {\r
+       include_spip('phpmailer-php5/class.phpmailer');\r
+       include_spip('phpmailer-php5/class.smtp');\r
+}\r
+\r
+include_spip('facteur_fonctions');\r
+\r
+class Facteur extends PHPMailer {\r
+\r
+       /**\r
+        * @param $email\r
+        * @param $objet\r
+        * @param $message_html\r
+        * @param $message_texte\r
+        * @param array $options\r
+        *\r
+        */\r
+       function Facteur($email, $objet, $message_html, $message_texte, $options = array()) {\r
+               $defaut = array(\r
+                       'adresse_envoi' => $GLOBALS['meta']['facteur_adresse_envoi'],\r
+                       'adresse_envoi_email' => $GLOBALS['meta']['facteur_adresse_envoi_email'],\r
+                       'adresse_envoi_nom' => $GLOBALS['meta']['facteur_adresse_envoi_nom'],\r
+                       'cc' => $GLOBALS['meta']['facteur_cc'],\r
+                       'bcc' => $GLOBALS['meta']['facteur_bcc'],\r
+                       'smtp' => $GLOBALS['meta']['facteur_smtp'],\r
+                       'smtp_host' => $GLOBALS['meta']['facteur_smtp_host'],\r
+                       'smtp_port' => $GLOBALS['meta']['facteur_smtp_port'],\r
+                       'smtp_auth' => $GLOBALS['meta']['facteur_smtp_auth'],\r
+                       'smtp_username' => $GLOBALS['meta']['facteur_smtp_username'],\r
+                       'smtp_password' => $GLOBALS['meta']['facteur_smtp_password'],\r
+                       'smtp_secure' => $GLOBALS['meta']['facteur_smtp_secure'],\r
+                       'smtp_sender' => $GLOBALS['meta']['facteur_smtp_sender'],\r
+                       'filtre_images' => $GLOBALS['meta']['facteur_filtre_images'],\r
+                       'filtre_iso_8859' => $GLOBALS['meta']['facteur_filtre_iso_8859'],\r
+               );\r
+               $options = array_merge($defaut, $options);\r
+\r
+               if (defined('_FACTEUR_DEBUG_SMTP')) {\r
+                       $this->SMTPDebug = _FACTEUR_DEBUG_SMTP ;\r
+               }\r
+               if ($options['adresse_envoi'] == 'oui'\r
+                 AND $options['adresse_envoi_email'])\r
+                       $this->From = $options['adresse_envoi_email'];\r
+               else\r
+                       $this->From = (isset($GLOBALS['meta']["email_envoi"]) AND $GLOBALS['meta']["email_envoi"])?\r
+                               $GLOBALS['meta']["email_envoi"]\r
+                               :$GLOBALS['meta']['email_webmaster'];\r
+\r
+               // Si plusieurs emails dans le from, pas de Name !\r
+               if (strpos($this->From,",")===false){\r
+                       if ($options['adresse_envoi'] == 'oui'\r
+                         AND $options['adresse_envoi_nom'])\r
+                               $this->FromName = $options['adresse_envoi_nom'];\r
+                       else\r
+                               $this->FromName = strip_tags(extraire_multi($GLOBALS['meta']['nom_site']));\r
+               }\r
+\r
+               $this->CharSet = "utf-8";\r
+               $this->Mailer = 'mail';\r
+               $this->Subject = unicode_to_utf_8(charset2unicode($objet,$GLOBALS['meta']['charset']));\r
+\r
+               //Pour un envoi multiple de mail, $email doit être un tableau avec les adresses.\r
+               if (is_array($email)) {\r
+                       foreach ($email as $cle => $adresseMail) {\r
+                               if (!$this->AddAddress($adresseMail))\r
+                                       spip_log("Erreur AddAddress $adresseMail : ".print_r($this->ErrorInfo,true),'facteur');\r
+                       }\r
+               }\r
+               else\r
+                       if (!$this->AddAddress($email))\r
+                               spip_log("Erreur AddAddress $email : ".print_r($this->ErrorInfo,true),'facteur');\r
+\r
+               if (!empty($options['smtp_sender'])) {\r
+                       $this->Sender = $options['smtp_sender'];\r
+                       $this->AddCustomHeader("Errors-To: ".$this->Sender);\r
+               }\r
+\r
+               if (!defined('_TEST_EMAIL_DEST')){\r
+                       if (!empty($options['cc'])) {\r
+                               $this->AddCC( $options['cc'] );\r
+                       }\r
+                       if (!empty($options['bcc'])) {\r
+                               $this->AddBCC( $options['bcc'] );\r
+                       }\r
+               }\r
+\r
+               if (isset($options['smtp']) AND $options['smtp'] == 'oui') {\r
+                       $this->Mailer   = 'smtp';\r
+                       $this->Host     = $options['smtp_host'];\r
+                       $this->Port     = $options['smtp_port'];\r
+                       if ($options['smtp_auth'] == 'oui') {\r
+                               $this->SMTPAuth = true;\r
+                               $this->Username = $options['smtp_username'];\r
+                               $this->Password = $options['smtp_password'];\r
+                       }\r
+                       else {\r
+                               $this->SMTPAuth = false;\r
+                       }\r
+                       if (intval(phpversion()) == 5) {\r
+                       if ($options['smtp_secure'] == 'ssl')\r
+                               $this->SMTPSecure = 'ssl';\r
+                       if ($options['smtp_secure'] == 'tls')\r
+                               $this->SMTPSecure = 'tls';\r
+                       }\r
+               }\r
+\r
+               if (!empty($message_html)) {\r
+                       $message_html = unicode_to_utf_8(charset2unicode($message_html,$GLOBALS['meta']['charset']));\r
+                       $this->Body = $message_html;\r
+                       $this->IsHTML(true);\r
+                       if ($options['filtre_images'])\r
+                               $this->JoindreImagesHTML();\r
+                       $this->UrlsAbsolues();\r
+               }\r
+               if (!empty($message_texte)) {\r
+                       $message_texte = unicode_to_utf_8(charset2unicode($message_texte,$GLOBALS['meta']['charset']));\r
+                       if (!$this->Body) {\r
+                               $this->IsHTML(false);\r
+                               $this->Body = $message_texte;\r
+                       }\r
+                       else {\r
+                               $this->AltBody = $message_texte;\r
+                       }\r
+               }\r
+\r
+               if ($options['filtre_iso_8859'])\r
+                       $this->ConvertirUtf8VersIso8859();\r
+\r
+       }\r
+       \r
+       /*\r
+        * Transforme du HTML en texte brut, mais proprement\r
+        * utilise le filtre facteur_mail_html2text\r
+        * @uses facteur_mail_html2text()\r
+        *\r
+        * @param string $html Le HTML à transformer\r
+        * @return string Retourne un texte brut formaté correctement\r
+        */\r
+       function html2text($html){\r
+               return facteur_mail_html2text($html);\r
+       }\r
+       \r
+       /**\r
+        * Transformer les urls des liens et des images en url absolues\r
+        * sans toucher aux images embarquees de la forme "cid:..."\r
+        */\r
+       function UrlsAbsolues($base=null){\r
+               include_spip('inc/filtres_mini');\r
+               if (preg_match_all(',(<(a|link)[[:space:]]+[^<>]*href=["\']?)([^"\' ><[:space:]]+)([^<>]*>),imsS',\r
+                 $this->Body, $liens, PREG_SET_ORDER)) {\r
+                       foreach ($liens as $lien) {\r
+                               if (strncmp($lien[3],"cid:",4)!==0){\r
+                                       $abs = url_absolue($lien[3], $base);\r
+                                       if ($abs <> $lien[3] and !preg_match('/^#/',$lien[3]))\r
+                                               $this->Body = str_replace($lien[0], $lien[1].$abs.$lien[4], $this->Body);\r
+                               }\r
+                       }\r
+               }\r
+               if (preg_match_all(',(<(img|script)[[:space:]]+[^<>]*src=["\']?)([^"\' ><[:space:]]+)([^<>]*>),imsS',\r
+                 $this->Body, $liens, PREG_SET_ORDER)) {\r
+                       foreach ($liens as $lien) {\r
+                               if (strncmp($lien[3],"cid:",4)!==0){\r
+                                       $abs = url_absolue($lien[3], $base);\r
+                                       if ($abs <> $lien[3])\r
+                                               $this->Body = str_replace($lien[0], $lien[1].$abs.$lien[4], $this->Body);\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+\r
+       function JoindreImagesHTML() {\r
+               $image_types = array(\r
+                                                       'gif'   => 'image/gif',\r
+                                                       'jpg'   => 'image/jpeg',\r
+                                                       'jpeg'  => 'image/jpeg',\r
+                                                       'jpe'   => 'image/jpeg',\r
+                                                       'bmp'   => 'image/bmp',\r
+                                                       'png'   => 'image/png',\r
+                                                       'tif'   => 'image/tiff',\r
+                                                       'tiff'  => 'image/tiff',\r
+                                                       'swf'   => 'application/x-shockwave-flash'\r
+                                               );\r
+               $src_found = array();\r
+               $images_embeded = array();\r
+               if (preg_match_all(\r
+                       '/["\'](([^"\']+)\.('.implode('|', array_keys($image_types)).'))([?][^"\']+)?([#][^"\']+)?["\']/Uims',\r
+                       $this->Body, $images, PREG_SET_ORDER)) {\r
+\r
+                       $adresse_site = $GLOBALS['meta']['adresse_site'].'/';\r
+                       foreach($images as $im){\r
+                               $src_orig = $im[1].$im[4].$im[5];\r
+                               if (!isset($src_found[$src_orig])){ // deja remplace ? rien a faire (ie la meme image presente plusieurs fois)\r
+                                       // examiner le src et voir si embedable\r
+                                       $src = $im[1];\r
+                                       if ($src AND strncmp($src,$adresse_site,strlen($adresse_site))==0)\r
+                                               $src = _DIR_RACINE . substr($src,strlen($adresse_site));\r
+                                       if ($src\r
+                                         AND !preg_match(",^[a-z0-9]+://,i",$src)\r
+                                         AND (\r
+                                             file_exists($f=$src) // l'image a ete generee depuis le meme cote que l'envoi\r
+                                             OR (_DIR_RACINE AND file_exists($f=_DIR_RACINE.$src)) // l'image a ete generee dans le public et on est dans le prive\r
+                                             OR (!_DIR_RACINE AND file_exists($f=_DIR_RESTREINT.$src)) // l'image a ete generee dans le prive et on est dans le public\r
+                                            )\r
+                                         ){\r
+                                               if (!isset($images_embeded[$f])){\r
+                                                       $extension = strtolower($im[3]);\r
+                                                       $header_extension = $image_types[$extension];\r
+                                                       $cid = md5($f); // un id unique pour un meme fichier\r
+                                                       $images_embeded[$f] = $cid; // marquer l'image comme traitee, inutile d'y revenir\r
+                                                       $this->AddEmbeddedImage($f, $cid, basename($f),'base64',$header_extension);\r
+                                               }\r
+\r
+                                               $this->Body = str_replace($src_orig, "cid:".$images_embeded[$f], $this->Body);\r
+                                               $src_found[$src_orig] = $f;\r
+                                       }\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+\r
+\r
+       /**\r
+        * Compat ascendante, obsolete\r
+        */\r
+       function ConvertirStylesEnligne() {\r
+               $this->Body = facteur_convertir_styles_inline($this->Body);\r
+       }\r
+\r
+\r
+       function safe_utf8_decode($text,$mode='texte_brut') {\r
+               if (!is_utf8($text))\r
+                       return ($text);\r
+\r
+               if (function_exists('iconv') && $mode == 'texte_brut') {\r
+                       $text = str_replace('’',"'",$text);\r
+                       $text = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $text);\r
+                       return str_replace('&#8217;',"'",$text);\r
+               }\r
+               else {\r
+                       if ($mode == 'texte_brut') {\r
+                               $text = str_replace('’',"'",$text);\r
+                       }\r
+                       $text = unicode2charset(utf_8_to_unicode($text),'iso-8859-1');\r
+                       return str_replace('&#8217;',"'",$text);\r
+               }\r
+       }\r
+\r
+       function ConvertirUtf8VersIso8859() {\r
+               $this->CharSet  = 'iso-8859-1';\r
+               $this->Body             = str_ireplace('charset=utf-8', 'charset=iso-8859-1', $this->Body);\r
+               $this->Body             = $this->safe_utf8_decode($this->Body,'html');\r
+               $this->AltBody  = $this->safe_utf8_decode($this->AltBody);\r
+               $this->Subject  = $this->safe_utf8_decode($this->Subject);\r
+               $this->FromName = $this->safe_utf8_decode($this->FromName);\r
+       }\r
+\r
+       function ConvertirAccents() {\r
+               // tableau à compléter au fur et à mesure\r
+               $cor = array(\r
+                                               'à' => '&agrave;',\r
+                                               'â' => '&acirc;',\r
+                                               'ä' => '&auml;',\r
+                                               'ç' => '&ccedil;',\r
+                                               'é' => '&eacute;',\r
+                                               'è' => '&egrave;',\r
+                                               'ê' => '&ecirc;',\r
+                                               'ë' => '&euml;',\r
+                                               'î' => '&icirc;',\r
+                                               'ï' => '&iuml;',\r
+                                               'ò' => '&ograve;',\r
+                                               'ô' => '&ocirc;',\r
+                                               'ö' => '&ouml;',\r
+                                               'ù' => '&ugrave;',\r
+                                               'û' => '&ucirc;',\r
+                                               'œ' => '&oelig;',\r
+                                               '€' => '&euro;'\r
+                                       );\r
+\r
+               $this->Body = strtr($this->Body, $cor);\r
+       }\r
+       public function Send() {\r
+               ob_start();\r
+               $retour = parent::Send();\r
+               $error = ob_get_contents();\r
+               ob_end_clean();\r
+               if( !empty($error) ) {\r
+                       spip_log("Erreur Facteur->Send : $error",'facteur.err');\r
+               }\r
+               return $retour;\r
+       }\r
+       public function AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {\r
+               ob_start();\r
+               $retour = parent::AddAttachment($path, $name, $encoding, $type);\r
+               $error = ob_get_contents();\r
+               ob_end_clean();\r
+               if( !empty($error) ) {\r
+                       spip_log("Erreur Facteur->AddAttachment : $error",'facteur.err');\r
+               }\r
+               return $retour;\r
+       }\r
+       public function AddReplyTo($address, $name = '') {\r
+               ob_start();\r
+               $retour = parent::AddReplyTo($address, $name);\r
+               $error = ob_get_contents();\r
+               ob_end_clean();\r
+               if( !empty($error) ) {\r
+                       spip_log("Erreur Facteur->AddReplyTo : $error",'facteur.err');\r
+               }\r
+               return $retour;\r
+       }\r
+       public function AddBCC($address, $name = '') {\r
+               ob_start();\r
+               $retour = parent::AddBCC($address, $name);\r
+               $error = ob_get_contents();\r
+               ob_end_clean();\r
+               if( !empty($error) ) {\r
+                       spip_log("Erreur Facteur->AddBCC : $error",'facteur.err');\r
+               }\r
+               return $retour;\r
+       }\r
+       public function AddCC($address, $name = '') {\r
+               ob_start();\r
+               $retour = parent::AddCC($address, $name);\r
+               $error = ob_get_contents();\r
+               ob_end_clean();\r
+               if( !empty($error) ) {\r
+                       spip_log("Erreur Facteur->AddCC : $error",'facteur.err');\r
+               }\r
+               return $retour;\r
+       }\r
+}\r
+\r
+?>\r