1 | <?php |
---|
2 | /** |
---|
3 | * Plugin mailsubscribers |
---|
4 | * (c) 2012 C�dric Morin |
---|
5 | * Licence GNU/GPL v3 |
---|
6 | */ |
---|
7 | |
---|
8 | if (!defined('_ECRIRE_INC_VERSION')) return; |
---|
9 | |
---|
10 | include_spip('inc/session'); |
---|
11 | include_spip('inc/mailsubscribers'); |
---|
12 | |
---|
13 | /** |
---|
14 | * Declarer les champs postes et y integrer les valeurs par defaut |
---|
15 | */ |
---|
16 | function formulaires_importer_mailsubscribers_charger_dist() { |
---|
17 | $valeurs = array( |
---|
18 | 'file_import' => '', |
---|
19 | 'valid_subscribers' => 1, |
---|
20 | 'listes_import_subscribers' => '', |
---|
21 | 'desactiver_notif' => 1, |
---|
22 | 'vider_table' => '', |
---|
23 | ); |
---|
24 | |
---|
25 | $valeurs['_listes_dispo'] = mailsubscribers_listes(); |
---|
26 | |
---|
27 | return $valeurs; |
---|
28 | } |
---|
29 | |
---|
30 | /** |
---|
31 | * Verifier les champs postes et signaler d'eventuelles erreurs |
---|
32 | */ |
---|
33 | function formulaires_importer_mailsubscribers_verifier_dist() { |
---|
34 | $erreurs = array(); |
---|
35 | $filename = ''; |
---|
36 | if (_request('go')) { |
---|
37 | $filename = session_get('importer_mailsubscribers::tmpfilename'); |
---|
38 | } else { |
---|
39 | $files = importer_mailsubscribers_file(); |
---|
40 | if (is_string($files)) // erreur |
---|
41 | { |
---|
42 | $erreurs['file_import'] = $files; |
---|
43 | } else { |
---|
44 | $files = reset($files); |
---|
45 | $filename = _DIR_TMP . basename($files['tmp_name']); |
---|
46 | move_uploaded_file($files['tmp_name'], $filename); |
---|
47 | session_set('importer_mailsubscribers::tmpfilename', $filename); |
---|
48 | session_set('importer_mailsubscribers::filename', $files['name']); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | if (!$filename) { |
---|
53 | $erreurs['file_import'] = _T('info_obligatoire'); |
---|
54 | } elseif (!_request('go')) { |
---|
55 | $importer_csv = charger_fonction("importer_csv", "inc"); |
---|
56 | $test = importer_mailsubscribers_data($filename); |
---|
57 | $head = array_keys(reset($test)); |
---|
58 | |
---|
59 | $erreurs['test'] = "\n"; |
---|
60 | if (in_array("statut", $head) AND in_array("listes", $head)) { |
---|
61 | $erreurs['test'] .= "<p class='notice'>" . _T('mailsubscriber:texte_avertissement_import') . "</p>\n\n"; |
---|
62 | } |
---|
63 | $erreurs['test'] .= "|{{" . implode("}}|{{", $head) . "}}|\n"; |
---|
64 | $nbmax = 10; |
---|
65 | $count = count($test); |
---|
66 | while ($row = array_shift($test) AND $nbmax--) { |
---|
67 | $erreurs['test'] .= "|" . implode("|", $row) . "|\n"; |
---|
68 | } |
---|
69 | $erreurs['test'] .= "\n\n"; |
---|
70 | $erreurs['test'] .= "<p class='explication'>{{" . singulier_ou_pluriel($count, |
---|
71 | 'mailsubscriber:info_1_adresse_a_importer', 'mailsubscriber:info_nb_adresses_a_importer') . "}}</p>"; |
---|
72 | |
---|
73 | if (!in_array("statut", $head)) { |
---|
74 | $erreurs['demander_statut'] = ' '; |
---|
75 | } |
---|
76 | if (!in_array("listes", $head)) { |
---|
77 | $erreurs['demander_listes'] = ' '; |
---|
78 | } |
---|
79 | $erreurs['message_erreur'] = ''; |
---|
80 | } |
---|
81 | |
---|
82 | return $erreurs; |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * Traiter les champs postes |
---|
87 | */ |
---|
88 | function formulaires_importer_mailsubscribers_traiter_dist() { |
---|
89 | refuser_traiter_formulaire_ajax();// pour recharger toute la page |
---|
90 | |
---|
91 | if (_request('vider_table') AND autoriser('detruire')) { |
---|
92 | include_spip('base/abstract_sql'); |
---|
93 | sql_delete("spip_mailsubscribers"); |
---|
94 | sql_delete("spip_mailsubscriptions"); |
---|
95 | } |
---|
96 | |
---|
97 | $res = array('editable' => true); |
---|
98 | $options = array('listes' => array()); |
---|
99 | if (_request('valid_subscribers')) { |
---|
100 | $options['statut'] = 'valide'; |
---|
101 | } |
---|
102 | if ($l = _request('listes_import_subscribers')) { |
---|
103 | $options['listes'] = $l; |
---|
104 | } |
---|
105 | // pas de notification pour cet import |
---|
106 | if (_request('desactiver_notif')) { |
---|
107 | $options['notify'] = false; |
---|
108 | } |
---|
109 | |
---|
110 | $filename = session_get('importer_mailsubscribers::tmpfilename'); |
---|
111 | // creer une liste de diffusion correspondant a cet import (automatique) |
---|
112 | $set = array( |
---|
113 | 'titre' => basename(session_get('importer_mailsubscribers::filename')), |
---|
114 | 'identifiant' => 'import_'.substr(md5(session_get('importer_mailsubscribers::filename').$filename.date('Y-m-d H:i:s')),0,7).'_'.date('Ymd'), |
---|
115 | ); |
---|
116 | include_spip('action/editer_objet'); |
---|
117 | $id_mailsubscribinglist = objet_inserer('mailsubscribinglist'); |
---|
118 | objet_modifier('mailsubscribinglist', $id_mailsubscribinglist, $set); |
---|
119 | // et inscrire les emails a cette liste |
---|
120 | $options['listes'][] = $set['identifiant']; |
---|
121 | |
---|
122 | $r = importer_mailsubscribers_importe($filename, $options); |
---|
123 | |
---|
124 | $message = |
---|
125 | sinon( |
---|
126 | singulier_ou_pluriel($r['count'], 'mailsubscriber:info_1_mailsubscriber', |
---|
127 | 'mailsubscriber:info_nb_mailsubscribers'), |
---|
128 | _T('mailsubscriber:info_aucun_mailsubscriber') |
---|
129 | ); |
---|
130 | if (count($r['erreurs'])) { |
---|
131 | $message .= "<p>Erreurs : <br />" . implode("<br />", $r['erreurs']) . "</p>"; |
---|
132 | $res['message_erreur'] = $message; |
---|
133 | } else { |
---|
134 | $res['message_ok'] = $message; |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | return $res; |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | function importer_mailsubscribers_file() { |
---|
143 | static $files = array(); |
---|
144 | // on est appele deux fois dans un hit, resservir ce qu'on a trouve a la verif |
---|
145 | // lorsqu'on est appelle au traitement |
---|
146 | |
---|
147 | if (count($files)) { |
---|
148 | return $files; |
---|
149 | } |
---|
150 | |
---|
151 | $post = isset($_FILES) ? $_FILES : $GLOBALS['HTTP_POST_FILES']; |
---|
152 | $files = array(); |
---|
153 | if (is_array($post)) { |
---|
154 | include_spip('action/ajouter_documents'); |
---|
155 | include_spip('inc/joindre_document'); |
---|
156 | |
---|
157 | foreach ($post as $file) { |
---|
158 | if (is_array($file['name'])) { |
---|
159 | while (count($file['name'])) { |
---|
160 | $test = array( |
---|
161 | 'error' => array_shift($file['error']), |
---|
162 | 'name' => array_shift($file['name']), |
---|
163 | 'tmp_name' => array_shift($file['tmp_name']), |
---|
164 | 'type' => array_shift($file['type']), |
---|
165 | ); |
---|
166 | if (!($test['error'] == 4)) { |
---|
167 | if (is_string($err = joindre_upload_error($test['error']))) { |
---|
168 | return $err; |
---|
169 | } // un erreur upload |
---|
170 | if (!is_array(verifier_upload_autorise($test['name']))) { |
---|
171 | return _T('medias:erreur_upload_type_interdit', array('nom' => $test['name'])); |
---|
172 | } |
---|
173 | $files[] = $test; |
---|
174 | } |
---|
175 | } |
---|
176 | } else { |
---|
177 | //UPLOAD_ERR_NO_FILE |
---|
178 | if (!($file['error'] == 4)) { |
---|
179 | if (is_string($err = joindre_upload_error($file['error']))) { |
---|
180 | return $err; |
---|
181 | } // un erreur upload |
---|
182 | if (!is_array(verifier_upload_autorise($file['name']))) { |
---|
183 | return _T('medias:erreur_upload_type_interdit', array('nom' => $file['name'])); |
---|
184 | } |
---|
185 | $files[] = $file; |
---|
186 | } |
---|
187 | } |
---|
188 | } |
---|
189 | if (!count($files)) { |
---|
190 | return _T('medias:erreur_indiquez_un_fichier'); |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | return $files; |
---|
195 | } |
---|
196 | |
---|
197 | function importer_mailsubscribers_data($filename) { |
---|
198 | |
---|
199 | $header = true; |
---|
200 | $importer_csv = charger_fonction("importer_csv", "inc"); |
---|
201 | |
---|
202 | // lire la premiere ligne et voir si elle contient 'email' pour decider si entete ou non |
---|
203 | if ($handle = @fopen($filename, "r")) { |
---|
204 | $line = fgets($handle, 4096); |
---|
205 | if (!$line OR stripos($line, 'email') === false) { |
---|
206 | $header = false; |
---|
207 | } |
---|
208 | @fclose($handle); |
---|
209 | } |
---|
210 | |
---|
211 | $data_raw = $importer_csv($filename, $header, ",", '"', null); |
---|
212 | // verifier qu'on a pas affaire a un fichier avec des fins de lignes Windows mal interpretes |
---|
213 | // corrige le cas du fichier texte 1 colonne, c'est mieux que rien |
---|
214 | if (count($data_raw) == 1 |
---|
215 | AND count(reset($data_raw)) == 1 |
---|
216 | ) { |
---|
217 | $d = reset($data_raw); |
---|
218 | $d = reset($d); |
---|
219 | $d = explode("\r", $d); |
---|
220 | $d = array_map('trim', $d); |
---|
221 | $d = array_filter($d); |
---|
222 | if (count($d) > 1) { |
---|
223 | $data_raw = array(); |
---|
224 | foreach ($d as $v) { |
---|
225 | $data_raw[] = array($v); |
---|
226 | } |
---|
227 | } |
---|
228 | } |
---|
229 | // colonner : si colonne email on prend toutes les colonnes |
---|
230 | // sinon on ne prend que la premiere colonne, comme un email |
---|
231 | $data = array(); |
---|
232 | while ($data_raw AND count($data_raw)) { |
---|
233 | |
---|
234 | $row = array_shift($data_raw); |
---|
235 | $row = array_combine(array_map('strtolower', array_keys($row)), array_values($row)); |
---|
236 | |
---|
237 | $d = array(); |
---|
238 | if (isset($row['email'])) { |
---|
239 | $d['email'] = $row['email']; |
---|
240 | } else { |
---|
241 | $d['email'] = reset($row); |
---|
242 | } |
---|
243 | |
---|
244 | foreach (array('nom', 'lang', 'listes', 'statut', 'date') as $k) { |
---|
245 | if (isset($row[$k])) { |
---|
246 | $d[$k] = $row[$k]; |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | // Mailchimp |
---|
251 | if (isset($row['prenom'])) { |
---|
252 | $d['nom'] = trim($row['prenom'] . (isset($d['nom']) ? " " . $d['nom'] : "")); |
---|
253 | } |
---|
254 | if (isset($row['confirm_time']) AND !isset($d['date'])) { |
---|
255 | $d['date'] = $row['confirm_time']; |
---|
256 | if (!isset($d['statut'])) { |
---|
257 | $d['statut'] = 'valide'; |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | $data[] = $d; |
---|
262 | } |
---|
263 | |
---|
264 | return $data; |
---|
265 | } |
---|
266 | |
---|
267 | /** |
---|
268 | * |
---|
269 | * @param string $filename |
---|
270 | * @param array $options |
---|
271 | * statut |
---|
272 | * listes |
---|
273 | * @return array |
---|
274 | */ |
---|
275 | function importer_mailsubscribers_importe($filename, $options = array()) { |
---|
276 | $res = array('count' => 0, 'erreurs' => array()); |
---|
277 | |
---|
278 | $data = importer_mailsubscribers_data($filename); |
---|
279 | $newsletter_subscribe = charger_fonction('subscribe', 'newsletter'); |
---|
280 | $newsletter_unsubscribe = charger_fonction('unsubscribe', 'newsletter'); |
---|
281 | include_spip('inc/filtres'); // email_valide |
---|
282 | include_spip('action/editer_objet'); |
---|
283 | include_spip('inc/mailsubscribers'); |
---|
284 | set_request('id_auteur', ''); // pas d'auteur associe a nos inscrits |
---|
285 | $notify = true; |
---|
286 | if (isset($options['notify'])){ |
---|
287 | $notify = $options['notify']; |
---|
288 | } |
---|
289 | |
---|
290 | foreach ($data as $d) { |
---|
291 | // strategie d'import en fonction de la qualite des donnees |
---|
292 | |
---|
293 | // si pas de colonne email explicite, on prend la premiere colonne et on importe en mail si valide, tel quel |
---|
294 | // mais graceful (sans forcer le reabonnement d'un desabonne) |
---|
295 | $email = trim($d['email']); |
---|
296 | if ($email AND email_valide($email)) { |
---|
297 | // abonner directement, sans passer par demande de confirmation |
---|
298 | $set = array('notify'=>$notify, 'force'=>true); |
---|
299 | if (isset($d['nom'])) { |
---|
300 | $set['nom'] = $d['nom']; |
---|
301 | } |
---|
302 | if (isset($d['lang'])) { |
---|
303 | $set['lang'] = $d['lang']; |
---|
304 | } |
---|
305 | if (isset($d['listes'])) { |
---|
306 | $set['listes'] = explode(',', $d['listes']); |
---|
307 | $set['listes'] = importer_mailsubscribers_listes($set['listes']); |
---|
308 | } |
---|
309 | |
---|
310 | if (!isset($d['statut']) AND isset($options['statut'])) { |
---|
311 | $d['statut'] = $options['statut']; |
---|
312 | } |
---|
313 | if (!isset($set['listes']) AND isset($options['listes']) AND is_array($options['listes'])) { |
---|
314 | $set['listes'] = $options['listes']; |
---|
315 | } |
---|
316 | |
---|
317 | if (!isset($d['statut']) or !isset($d['listes'])) { |
---|
318 | if (!mailsubscribers_test_email_obfusque($email)){ |
---|
319 | $set['graceful'] = true; // ne pas reabonner un desabonne |
---|
320 | $newsletter_subscribe($email, $set); |
---|
321 | spip_log("Importer $email " . var_export($set, true), "mailsubscribers"); |
---|
322 | $res['count']++; |
---|
323 | } |
---|
324 | } // si statut explicite, il faut importer a la main pour respecter le statut demande |
---|
325 | else { |
---|
326 | unset($set['notify']); |
---|
327 | unset($set['force']); |
---|
328 | $listes = $set['listes']; |
---|
329 | unset($set['listes']); |
---|
330 | $razlistes = false; |
---|
331 | // si la liste vient des options on la merge avec l'existante |
---|
332 | if (!isset($d['listes']) AND is_array($listes)) { |
---|
333 | $razlistes = true; |
---|
334 | } |
---|
335 | |
---|
336 | if (isset($d['date'])) { |
---|
337 | $set['date'] = $d['date']; |
---|
338 | } |
---|
339 | if($razlistes){ |
---|
340 | $set['optin'] = mailsubscribers_trace_optin('raz par import csv',''); |
---|
341 | } |
---|
342 | // d'abord on cree/update les donnees dans spip_mailsubscribers |
---|
343 | $id = 0; |
---|
344 | if ($row = sql_fetsel("id_mailsubscriber", "spip_mailsubscribers", |
---|
345 | "email=" . sql_quote($email) . " OR email=" . sql_quote(mailsubscribers_obfusquer_email($email))) |
---|
346 | AND $id = $row["id_mailsubscriber"] |
---|
347 | ) { |
---|
348 | $set['email'] = $email; // si mail obfusque |
---|
349 | $set['statut'] = $d['statut']; |
---|
350 | objet_modifier("mailsubscriber", $id, $set); |
---|
351 | $res['count']++; |
---|
352 | } else { |
---|
353 | $set['email'] = $email; |
---|
354 | if ($id = objet_inserer("mailsubscriber", 0, $set)) { |
---|
355 | // on garde tous les champs car objet_inserer n'a pas forcement fait le boulot (depend de http://core.spip.org/projects/spip/repository/revisions/20021) |
---|
356 | $set['statut'] = $d['statut']; |
---|
357 | objet_modifier("mailsubscriber", $id, $set); |
---|
358 | $res['count']++; |
---|
359 | } else { |
---|
360 | $res['erreurs'][] = "erreur import \"<tt>$email</tt>\""; |
---|
361 | } |
---|
362 | } |
---|
363 | // et on appelle subscribe juste pour les listes |
---|
364 | if ($id){ |
---|
365 | if ($razlistes){ |
---|
366 | sql_delete('spip_mailsubscriptions','id_mailsubscriber='.intval($id)); |
---|
367 | } |
---|
368 | if (in_array($d['statut'],array('prop','valide','refuse'))){ |
---|
369 | $set = array('listes'=>$listes,'notify'=>false); |
---|
370 | if ($d['statut']=='valide') { |
---|
371 | $set['force'] = true; |
---|
372 | } |
---|
373 | else { |
---|
374 | $set['force'] = -1; |
---|
375 | } |
---|
376 | $newsletter_subscribe($email, $set); |
---|
377 | if ($d['statut'] == 'refuse') { |
---|
378 | $set['force'] = true; |
---|
379 | $newsletter_unsubscribe($email, $set); |
---|
380 | } |
---|
381 | } |
---|
382 | } |
---|
383 | } |
---|
384 | } else { |
---|
385 | // ne pas produire une erreur pour un email vide |
---|
386 | if ($email) { |
---|
387 | $res['erreurs'][] = "email invalide \"<tt>$email</tt>\""; |
---|
388 | } |
---|
389 | } |
---|
390 | } |
---|
391 | |
---|
392 | // debloquer les flags edition |
---|
393 | include_spip('inc/drapeau_edition'); |
---|
394 | debloquer_tous($GLOBALS['visiteur_session']['id_auteur']); |
---|
395 | effacer_meta("newsletter_subscribers_count"); |
---|
396 | |
---|
397 | |
---|
398 | return $res; |
---|
399 | } |
---|
400 | |
---|
401 | /** |
---|
402 | * Importer les listes |
---|
403 | * @param array $listes |
---|
404 | * @return array |
---|
405 | */ |
---|
406 | function importer_mailsubscribers_listes($listes){ |
---|
407 | static $existing; |
---|
408 | |
---|
409 | if (is_null($existing)){ |
---|
410 | $existing = sql_allfetsel('identifiant','spip_mailsubscribinglists'); |
---|
411 | $existing = array_map('reset',$existing); |
---|
412 | } |
---|
413 | |
---|
414 | foreach ($listes as $k=>$liste) { |
---|
415 | $listes[$k] = $liste = mailsubscribers_normaliser_nom_liste($liste); |
---|
416 | if (!in_array($liste, $existing)){ |
---|
417 | $statut = 'fermee'; |
---|
418 | $identifiant = $liste; |
---|
419 | if (!$id = sql_getfetsel('id_mailsubscribinglist','spip_mailsubscribinglists','identifiant='.sql_quote($identifiant))) { |
---|
420 | $ins = array( |
---|
421 | 'titre' => 'Liste ' . $identifiant, |
---|
422 | 'descriptif' => 'Import CSV', |
---|
423 | 'identifiant' => $identifiant, |
---|
424 | 'statut' => $statut, |
---|
425 | 'date' => date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']), |
---|
426 | ); |
---|
427 | $id = sql_insertq('spip_mailsubscribinglists', $ins); |
---|
428 | } |
---|
429 | if ($id) { |
---|
430 | $existing[] = $liste; |
---|
431 | } |
---|
432 | } |
---|
433 | } |
---|
434 | |
---|
435 | return $listes; |
---|
436 | } |
---|
437 | |
---|