[SPIP] ~v3.0.20-->v3.0.25
[lhc/web/clavette_www.git] / www / ecrire / inc / notifications.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2016 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
12
13 if (!defined('_ECRIRE_INC_VERSION')) return;
14
15
16 /**
17 * La fonction de notification de base, qui dispatche le travail
18 * http://doc.spip.org/@inc_notifications_dist
19 *
20 * @param string $quoi
21 * evenement de notification
22 * @param int $id
23 * id de l'objet en relation avec l'evenement
24 * @param array $options
25 * options de notification, interpretees en fonction de la notification
26 */
27 function inc_notifications_dist($quoi, $id=0, $options=array()) {
28
29 // charger les fichiers qui veulent ajouter des definitions
30 // ou faire des trucs aussi dans le pipeline, ca fait deux api pour le prix d'une ...
31 pipeline('notifications',array('args'=>array('quoi'=>$quoi,'id'=>$id,'options'=>$options)));
32
33 if ($notification = charger_fonction($quoi,'notifications',true)) {
34 spip_log("$notification($quoi,$id"
35 .($options?",".serialize($options):"")
36 .")",'notifications');
37 $notification($quoi, $id, $options);
38 }
39 }
40
41 /**
42 * Nettoyage des emails avant un envoi
43 * on passe par reference pour la perf
44 *
45 * les emails liste par $eclure seront exclus de la liste
46 *
47 * @param array $emails
48 * @param array $exclure
49 */
50 function notifications_nettoyer_emails(&$emails, $exclure = array()){
51 // filtrer et unifier
52 include_spip('inc/filtres');
53 $emails = array_unique(array_filter(array_map('email_valide',array_map('trim', $emails))));
54 if ($exclure AND count($exclure)){
55 // nettoyer les exclusions d'abord
56 notifications_nettoyer_emails($exclure);
57 // faire un diff
58 $emails = array_diff($emails,$exclure);
59 }
60 }
61
62 /**
63 * Envoyer un email de notification
64 * Le sujet peut etre vide, dans ce cas il reprendra la premiere ligne non vide du texte
65 *
66 * @param array/string $emails
67 * @param string $texte
68 * @param string $sujet
69 * @param string $from
70 * @param string $headers
71 */
72 function notifications_envoyer_mails($emails, $texte, $sujet="", $from = "", $headers = ""){
73 // rien a faire si pas de texte !
74 if (!strlen($texte))
75 return;
76
77 // si on ne specifie qu'un email, le mettre dans un tableau
78 if (!is_array($emails))
79 $emails = explode(',',$emails);
80
81 notifications_nettoyer_emails($emails);
82
83 // tester si le mail est deja en html
84 if (strpos($texte,"<")!==false // eviter les tests suivants si possible
85 AND $ttrim = trim($texte)
86 AND substr($ttrim,0,1)=="<"
87 AND substr($ttrim,-1,1)==">"
88 AND stripos($ttrim,"</html>")!==false){
89
90 if(!strlen($sujet)){
91 // dans ce cas on ruse un peu : extraire le sujet du title
92 if (preg_match(",<title>(.*)</title>,Uims",$texte,$m))
93 $sujet = $m[1];
94 else {
95 // fallback, on prend le body si on le trouve
96 if (preg_match(",<body[^>]*>(.*)</body>,Uims",$texte,$m))
97 $ttrim = $m[1];
98
99 // et on extrait la premiere ligne de vrai texte...
100 // nettoyer le html et les retours chariots
101 $ttrim = textebrut($ttrim);
102 $ttrim = str_replace("\r\n", "\r", $ttrim);
103 $ttrim = str_replace("\r", "\n", $ttrim);
104 // decouper
105 $ttrim = explode("\n",trim($ttrim));
106 // extraire la premiere ligne de texte brut
107 $sujet = array_shift($ttrim);
108 }
109 }
110
111 // si besoin on ajoute le content-type dans les headers
112 if (stripos($headers,"Content-Type")===false)
113 $headers .= "Content-Type: text/html\n";
114 }
115
116 // si le sujet est vide, extraire la premiere ligne du corps
117 // du mail qui est donc du texte
118 if (!strlen($sujet)){
119 // nettoyer un peu les retours chariots
120 $texte = str_replace("\r\n", "\r", $texte);
121 $texte = str_replace("\r", "\n", $texte);
122 // decouper
123 $texte = explode("\n",trim($texte));
124 // extraire la premiere ligne
125 $sujet = array_shift($texte);
126 $texte = trim(implode("\n",$texte));
127 }
128
129 $envoyer_mail = charger_fonction('envoyer_mail','inc');
130 foreach($emails as $email){
131 // passer dans un pipeline qui permet un ajout eventuel
132 // (url de suivi des notifications par exemple)
133 $envoi = pipeline('notifications_envoyer_mails',array('email'=>$email,'sujet'=>$sujet,'texte'=>$texte));
134 $email = $envoi['email'];
135
136 job_queue_add('envoyer_mail', ">$email : ".$envoi['sujet'], array($email, $envoi['sujet'], $envoi['texte'], $from, $headers), 'inc/');
137 }
138
139 }
140
141 /**
142 * Notifier un evenement sur un objet
143 * recupere le fond designe dans $modele,
144 * prend la premiere ligne comme sujet
145 * et l'interprete pour envoyer l'email
146 *
147 * @param int $id_objet
148 * @param string $type_objet
149 * @param string $modele
150 * @return string
151 */
152 function email_notification_objet($id_objet, $type_objet, $modele) {
153 $envoyer_mail = charger_fonction('envoyer_mail','inc'); // pour nettoyer_titre_email
154 $id_type = id_table_objet($type_objet);
155 return recuperer_fond($modele,array($id_type=>$id_objet,"id"=>$id_objet));
156 }
157
158 /**
159 * Notifier un evenement sur un article
160 * recupere le fond designe dans $modele,
161 * prend la premiere ligne comme sujet
162 * et l'interprete pour envoyer l'email
163 *
164 * @param int $id_article
165 * @param string $modele
166 * @return string
167 */
168 function email_notification_article($id_article, $modele) {
169 $envoyer_mail = charger_fonction('envoyer_mail','inc'); // pour nettoyer_titre_email
170
171 return recuperer_fond($modele,array('id_article'=>$id_article));
172 }
173
174 // Compatibilite, ne plus utiliser
175 // http://doc.spip.org/@notifier_publication_article
176 function notifier_publication_article($id_article) {
177 if ($GLOBALS['meta']["suivi_edito"] == "oui") {
178 $adresse_suivi = $GLOBALS['meta']["adresse_suivi"];
179 $texte = email_notification_article($id_article, "notifications/article_publie");
180 notifications_envoyer_mails($adresse_suivi, $texte);
181 }
182 }
183
184 // Compatibilite, ne plus utiliser
185 // http://doc.spip.org/@notifier_proposition_article
186 function notifier_proposition_article($id_article) {
187 if ($GLOBALS['meta']["suivi_edito"] == "oui") {
188 $adresse_suivi = $GLOBALS['meta']["adresse_suivi"];
189 $texte = email_notification_article($id_article, "notifications/article_propose");
190 notifications_envoyer_mails($adresse_suivi, $texte);
191 }
192 }
193
194 ?>