|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/***************************************************************************\ |
|
4
|
|
|
* SPIP, Système de publication pour l'internet * |
|
5
|
|
|
* * |
|
6
|
|
|
* Copyright © avec tendresse depuis 2001 * |
|
7
|
|
|
* Arnaud Martin, Antoine Pitrou, Philippe Rivière, Emmanuel Saint-James * |
|
8
|
|
|
* * |
|
9
|
|
|
* Ce programme est un logiciel libre distribué sous licence GNU/GPL. * |
|
10
|
|
|
* Pour plus de détails voir le fichier COPYING.txt ou l'aide en ligne. * |
|
11
|
|
|
\***************************************************************************/ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Gestion des notifications |
|
15
|
|
|
* |
|
16
|
|
|
* @package SPIP\Core\Notifications |
|
17
|
|
|
**/ |
|
18
|
|
|
if (!defined('_ECRIRE_INC_VERSION')) { |
|
19
|
|
|
return; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* La fonction de notification de base, qui dispatche le travail |
|
25
|
|
|
* |
|
26
|
|
|
* @api |
|
27
|
|
|
* @param string $quoi |
|
28
|
|
|
* Événement de notification |
|
29
|
|
|
* @param int $id |
|
30
|
|
|
* id de l'objet en relation avec l'événement |
|
31
|
|
|
* @param array $options |
|
32
|
|
|
* Options de notification, interprétées en fonction de la notification |
|
33
|
|
|
*/ |
|
34
|
|
|
function inc_notifications_dist($quoi, $id = 0, $options = array()) { |
|
35
|
|
|
|
|
36
|
|
|
// charger les fichiers qui veulent ajouter des definitions |
|
37
|
|
|
// ou faire des trucs aussi dans le pipeline, ca fait deux api pour le prix d'une ... |
|
38
|
|
|
pipeline('notifications', array('args' => array('quoi' => $quoi, 'id' => $id, 'options' => $options))); |
|
39
|
|
|
|
|
40
|
|
|
if ($notification = charger_fonction($quoi, 'notifications', true)) { |
|
41
|
|
|
spip_log("$notification($quoi,$id" |
|
42
|
|
|
. ($options ? "," . serialize($options) : "") |
|
43
|
|
|
. ")", 'notifications'); |
|
44
|
|
|
$notification($quoi, $id, $options); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Néttoyage des emails avant un envoi |
|
50
|
|
|
* |
|
51
|
|
|
* On passe par référence pour la perf |
|
52
|
|
|
* |
|
53
|
|
|
* les emails liste par $exclure seront exclus de la liste |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $emails |
|
56
|
|
|
* @param array $exclure |
|
57
|
|
|
*/ |
|
58
|
|
|
function notifications_nettoyer_emails(&$emails, $exclure = array()) { |
|
59
|
|
|
// filtrer et unifier |
|
60
|
|
|
$emails = array_unique(array_filter(array_map('email_valide', array_map('trim', $emails)))); |
|
61
|
|
|
if ($exclure and count($exclure)) { |
|
62
|
|
|
// nettoyer les exclusions d'abord |
|
63
|
|
|
notifications_nettoyer_emails($exclure); |
|
64
|
|
|
// faire un diff |
|
65
|
|
|
$emails = array_diff($emails, $exclure); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Envoyer un email de notification |
|
71
|
|
|
* |
|
72
|
|
|
* Le sujet peut être vide, dans ce cas il reprendra la première ligne non vide du texte |
|
73
|
|
|
* |
|
74
|
|
|
* @param array|string $emails |
|
75
|
|
|
* @param string $texte |
|
76
|
|
|
* @param string $sujet |
|
77
|
|
|
* @param string $from |
|
78
|
|
|
* @param string $headers |
|
79
|
|
|
*/ |
|
80
|
|
|
function notifications_envoyer_mails($emails, $texte, $sujet = "", $from = "", $headers = "") { |
|
81
|
|
|
// rien a faire si pas de texte ! |
|
82
|
|
|
if (!strlen($texte)) { |
|
83
|
|
|
return; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// si on ne specifie qu'un email, le mettre dans un tableau |
|
87
|
|
|
if (!is_array($emails)) { |
|
88
|
|
|
$emails = explode(',', $emails); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
notifications_nettoyer_emails($emails); |
|
92
|
|
|
|
|
93
|
|
|
// tester si le mail est deja en html |
|
94
|
|
|
if (strpos($texte, "<") !== false // eviter les tests suivants si possible |
|
95
|
|
|
and $ttrim = trim($texte) |
|
96
|
|
|
and substr($ttrim, 0, 1) == "<" |
|
97
|
|
|
and substr($ttrim, -1, 1) == ">" |
|
98
|
|
|
and stripos($ttrim, "</html>") !== false |
|
99
|
|
|
) { |
|
100
|
|
|
|
|
101
|
|
|
if (!strlen($sujet)) { |
|
102
|
|
|
// dans ce cas on ruse un peu : extraire le sujet du title |
|
103
|
|
|
if (preg_match(",<title>(.*)</title>,Uims", $texte, $m)) { |
|
104
|
|
|
$sujet = $m[1]; |
|
105
|
|
|
} else { |
|
106
|
|
|
// fallback, on prend le body si on le trouve |
|
107
|
|
|
if (preg_match(",<body[^>]*>(.*)</body>,Uims", $texte, $m)) { |
|
108
|
|
|
$ttrim = $m[1]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// et on extrait la premiere ligne de vrai texte... |
|
112
|
|
|
// nettoyer le html et les retours chariots |
|
113
|
|
|
$ttrim = textebrut($ttrim); |
|
114
|
|
|
$ttrim = str_replace("\r\n", "\r", $ttrim); |
|
115
|
|
|
$ttrim = str_replace("\r", "\n", $ttrim); |
|
116
|
|
|
// decouper |
|
117
|
|
|
$ttrim = explode("\n", trim($ttrim)); |
|
118
|
|
|
// extraire la premiere ligne de texte brut |
|
119
|
|
|
$sujet = array_shift($ttrim); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
// si besoin on ajoute le content-type dans les headers |
|
124
|
|
|
if (stripos($headers, "Content-Type") === false) { |
|
125
|
|
|
$headers .= "Content-Type: text/html\n"; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
// si le sujet est vide, extraire la premiere ligne du corps |
|
130
|
|
|
// du mail qui est donc du texte |
|
131
|
|
View Code Duplication |
if (!strlen($sujet)) { |
|
|
|
|
|
|
132
|
|
|
// nettoyer un peu les retours chariots |
|
133
|
|
|
$texte = str_replace("\r\n", "\r", $texte); |
|
134
|
|
|
$texte = str_replace("\r", "\n", $texte); |
|
135
|
|
|
// decouper |
|
136
|
|
|
$texte = explode("\n", trim($texte)); |
|
137
|
|
|
// extraire la premiere ligne |
|
138
|
|
|
$sujet = array_shift($texte); |
|
139
|
|
|
$texte = trim(implode("\n", $texte)); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$envoyer_mail = charger_fonction('envoyer_mail', 'inc'); |
|
|
|
|
|
|
143
|
|
|
foreach ($emails as $email) { |
|
144
|
|
|
// passer dans un pipeline qui permet un ajout eventuel |
|
145
|
|
|
// (url de suivi des notifications par exemple) |
|
146
|
|
|
$envoi = pipeline( |
|
147
|
|
|
'notifications_envoyer_mails', |
|
148
|
|
|
array( |
|
149
|
|
|
'email' => $email, |
|
150
|
|
|
'sujet' => $sujet, |
|
151
|
|
|
'texte' => $texte, |
|
152
|
|
|
'from' => $from, |
|
153
|
|
|
'headers' => $headers, |
|
154
|
|
|
) |
|
155
|
|
|
); |
|
156
|
|
|
$email = $envoi['email']; |
|
157
|
|
|
|
|
158
|
|
|
job_queue_add('envoyer_mail', ">$email : " . $envoi['sujet'], |
|
159
|
|
|
array($email, $envoi['sujet'], $envoi['texte'], $envoi['from'], $envoi['headers']), 'inc/'); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Notifier un événement sur un objet |
|
166
|
|
|
* |
|
167
|
|
|
* Récupère le fond désigné dans $modele, |
|
168
|
|
|
* prend la première ligne comme sujet |
|
169
|
|
|
* et l'interprète pour envoyer l'email |
|
170
|
|
|
* |
|
171
|
|
|
* @param int $id_objet |
|
172
|
|
|
* @param string $type_objet |
|
173
|
|
|
* @param string $modele |
|
174
|
|
|
* @return string |
|
|
|
|
|
|
175
|
|
|
*/ |
|
176
|
|
|
function email_notification_objet($id_objet, $type_objet, $modele) { |
|
177
|
|
|
$envoyer_mail = charger_fonction('envoyer_mail', 'inc'); // pour nettoyer_titre_email |
|
|
|
|
|
|
178
|
|
|
$id_type = id_table_objet($type_objet); |
|
179
|
|
|
|
|
180
|
|
|
return recuperer_fond($modele, array($id_type => $id_objet, "id" => $id_objet)); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Notifier un événement sur un article |
|
185
|
|
|
* |
|
186
|
|
|
* Récupère le fond désigné dans $modele, |
|
187
|
|
|
* prend la première ligne comme sujet |
|
188
|
|
|
* et l'interprète pour envoyer l'email |
|
189
|
|
|
* |
|
190
|
|
|
* @param int $id_article |
|
191
|
|
|
* @param string $modele |
|
192
|
|
|
* @return string |
|
|
|
|
|
|
193
|
|
|
*/ |
|
194
|
|
|
function email_notification_article($id_article, $modele) { |
|
195
|
|
|
$envoyer_mail = charger_fonction('envoyer_mail', 'inc'); // pour nettoyer_titre_email |
|
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
return recuperer_fond($modele, array('id_article' => $id_article)); |
|
198
|
|
|
} |
|
199
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.