Fixes Xorg NL deletion.
[platal.git] / modules / newsletter.php
CommitLineData
e2efba7d 1<?php
2/***************************************************************************
12262f13 3 * Copyright (C) 2003-2011 Polytechnique.org *
e2efba7d 4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22class NewsletterModule extends PLModule
23{
24 function handlers()
25 {
26 return array(
84163d58
RB
27 'nl' => $this->make_hook('nl', AUTH_COOKIE),
28 'nl/show' => $this->make_hook('nl_show', AUTH_COOKIE),
5e9a29ed 29 'nl/search' => $this->make_hook('nl_search', AUTH_COOKIE),
84163d58 30 'nl/submit' => $this->make_hook('nl_submit', AUTH_MDP),
0fd5be99 31 'nl/remaining' => $this->make_hook('nl_remaining', AUTH_MDP),
84163d58
RB
32 'admin/nls' => $this->make_hook('admin_nl_groups', AUTH_MDP, 'admin'),
33 'admin/newsletter' => $this->make_hook('admin_nl', AUTH_MDP, 'admin'),
34 'admin/newsletter/categories' => $this->make_hook('admin_nl_cat', AUTH_MDP, 'admin'),
35 'admin/newsletter/edit' => $this->make_hook('admin_nl_edit', AUTH_MDP, 'admin'),
fccf0105 36 'admin/newsletter/edit/delete' => $this->make_hook('admin_nl_delete', AUTH_MDP, 'admin'),
84163d58
RB
37 // Automatic mailing is disabled for X.org NL
38// 'admin/newsletter/edit/cancel' => $this->make_hook('cancel', AUTH_MDP, 'admin'),
39// 'admin/newsletter/edit/valid' => $this->make_hook('valid', AUTH_MDP, 'admin'),
e2efba7d 40 );
41 }
42
84163d58
RB
43 /** This function should return the adequate NewsLetter object for the current module.
44 */
45 protected function getNl()
e2efba7d 46 {
47 require_once 'newsletter.inc.php';
84163d58
RB
48 return NewsLetter::forGroup(NewsLetter::GROUP_XORG);
49 }
50
26ba053e 51 function handler_nl($page, $action = null, $hash = null)
84163d58
RB
52 {
53 $nl = $this->getNl();
54 if (!$nl) {
55 return PL_NOT_FOUND;
56 }
e2efba7d 57
58 $page->changeTpl('newsletter/index.tpl');
46f272fe 59 $page->setTitle('Lettres mensuelles');
e2efba7d 60
61 switch ($action) {
84163d58
RB
62 case 'out': $nl->unsubscribe($hash, $hash != null); break;
63 case 'in': $nl->subscribe(); break;
e2efba7d 64 default: ;
65 }
66
84163d58
RB
67 $page->assign_by_ref('nl', $nl);
68 $page->assign('nls', $nl->subscriptionState());
69 $page->assign('nl_list', $nl->listSentIssues(true));
e2efba7d 70 }
71
26ba053e 72 function handler_nl_show($page, $nid = 'last')
e2efba7d 73 {
74 $page->changeTpl('newsletter/show.tpl');
84163d58
RB
75 $nl = $this->getNl();
76 if (!$nl) {
77 return PL_NOT_FOUND;
78 }
e2efba7d 79
9da70671 80 try {
84163d58 81 $issue = $nl->getIssue($nid);
6d1747b3 82 $user =& S::user();
9da70671 83 if (Get::has('text')) {
84163d58 84 $issue->toText($page, $user);
9da70671 85 } else {
84163d58 86 $issue->toHtml($page, $user);
9da70671
SJ
87 }
88 if (Post::has('send')) {
84163d58 89 $issue->sendTo($user);
9da70671
SJ
90 }
91 } catch (MailNotFound $e) {
92 return PL_NOT_FOUND;
e2efba7d 93 }
94 }
95
5e9a29ed
SJ
96 function handler_nl_search($page)
97 {
98 S::assert_xsrf_token();
99 $nl = $this->getNl();
100 if (!$nl) {
101 return PL_NOT_FOUND;
102 }
103
104 if (!Post::has('nl_search')) {
105 pl_redirect($nl->prefix());
106 }
107
108 $nl_search = Post::t('nl_search');
109 $nl_search_type = Post::t('nl_search_type');
110 if (!$nl_search || !($nl_search_type > 0 && $nl_search_type < 10)) {
111 $page->trigErrorRedirect('La recherche est vide ou erronée.', $nl->prefix());
112 }
113
114 $page->changeTpl('newsletter/search.tpl');
115 $user = S::user();
116 $fields = array(1 => 'all', 2 => 'all', 3 => 'title', 4 => 'body', 5 => 'append', 6 => 'all', 7 => 'title', 8 => 'head', 9 => 'signature');
117 $res_articles = $res_issues = array();
118 if ($nl_search_type < 6) {
119 $res_articles = $nl->articleSearch($nl_search, $fields[$nl_search_type], $user);
120 }
121 if ($nl_search_type > 5 || $nl_search_type == 1) {
122 $res_issues = $nl->issueSearch($nl_search, $fields[$nl_search_type], $user);
123 }
124
125 $articles_count = count($res_articles);
126 $issues_count = count($res_issues);
127 $results_count = $articles_count + $issues_count;
128 if ($results_count > 200) {
129 $page->trigError('Recherche trop générale.');
130 } elseif ($results_count == 0) {
131 $page->trigWarning('Aucun résultat pour cette recherche.');
132 } else {
133 $page->assign('res_articles', $res_articles);
134 $page->assign('res_issues', $res_issues);
135 $page->assign('articles_count', $articles_count);
136 $page->assign('issues_count', $issues_count);
137 }
138
139 $page->assign_by_ref('nl', $nl);
140 $page->assign('nl_search', $nl_search);
141 $page->assign('nl_search_type', $nl_search_type);
142 $page->assign('results_count', $results_count);
143 }
144
26ba053e 145 function handler_nl_submit($page)
e2efba7d 146 {
147 $page->changeTpl('newsletter/submit.tpl');
148
84163d58
RB
149 $nl = $this->getNl();
150 if (!$nl) {
151 return PL_NOT_FOUND;
152 }
153
8f201b69
FB
154 $wp = new PlWikiPage('Xorg.LettreMensuelle');
155 $wp->buildCache();
e2efba7d 156
1970c12b 157 if (Post::has('see') || (Post::has('valid') && (!trim(Post::v('title')) || !trim(Post::v('body'))))) {
158 if (!Post::has('see')) {
a7d35093 159 $page->trigError("L'article doit avoir un titre et un contenu");
1970c12b 160 }
e2efba7d 161 $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'));
162 $page->assign('art', $art);
163 } elseif (Post::has('valid')) {
5daf68f6 164 $art = new NLReq(S::user(), Post::v('title'),
e2efba7d 165 Post::v('body'), Post::v('append'));
166 $art->submit();
167 $page->assign('submited', true);
168 }
84163d58 169 $page->addCssLink($nl->cssFile());
e2efba7d 170 }
171
0fd5be99
SJ
172 function handler_nl_remaining($page)
173 {
174 require_once 'newsletter.inc.php';
175
176 pl_content_headers('text/html');
177 $page->changeTpl('newsletter/remaining.tpl', NO_SKIN);
178
179 $article = new NLArticle('', Post::t('body'), '');
180 $rest = $article->remain();
181
182 $page->assign('too_long', $rest['remaining_lines'] < 0);
183 $page->assign('last_line', ($rest['remaining_lines'] == 0));
184 $page->assign('remaining', ($rest['remaining_lines'] == 0) ? $rest['remaining_characters_for_last_line'] : $rest['remaining_lines']);
185 }
186
26ba053e 187 function handler_admin_nl($page, $new = false) {
e2efba7d 188 $page->changeTpl('newsletter/admin.tpl');
46f272fe 189 $page->setTitle('Administration - Newsletter : liste');
84163d58
RB
190
191 $nl = $this->getNl();
192 if (!$nl) {
193 return PL_NOT_FOUND;
194 }
eaf30d86 195
2d1ed661 196 if ($new == 'new') {
84163d58
RB
197 $id = $nl->createPending();
198 pl_redirect($nl->adminPrefix() . '/edit/' . $id);
e2efba7d 199 }
eaf30d86 200
84163d58
RB
201 $page->assign_by_ref('nl', $nl);
202 $page->assign('nl_list', $nl->listAllIssues());
203 }
204
26ba053e 205 function handler_admin_nl_groups($page)
84163d58
RB
206 {
207 require_once 'newsletter.inc.php';
208
209 $page->changeTpl('newsletter/admin_all.tpl');
210 $page->setTitle('Administration - Newsletters : Liste des Newsletters');
211
212 $page->assign('nls', Newsletter::getAll());
e2efba7d 213 }
eaf30d86 214
26ba053e 215 function handler_admin_nl_edit($page, $nid = 'last', $aid = null, $action = 'edit') {
e2efba7d 216 $page->changeTpl('newsletter/edit.tpl');
84163d58 217 $page->addCssLink('nl.Polytechnique.org.css');
e4705641 218 $page->setTitle('Administration - Newsletter : Édition');
eaf30d86 219
84163d58
RB
220 $nl = $this->getNl();
221 if (!$nl) {
222 return PL_NOT_FOUND;
223 }
eaf30d86 224
84163d58
RB
225 try {
226 $issue = $nl->getIssue($nid, false);
227 } catch (MailNotFound $e) {
228 return PL_NOT_FOUND;
e2efba7d 229 }
eaf30d86 230
84163d58
RB
231 $ufb = $nl->getSubscribersUFB();
232 $ufb_keepenv = false; // Will be set to True if there were invalid modification to the UFB.
233
234 // Convert NLIssue error messages to human-readable errors
235 $error_msgs = array(
236 NLIssue::ERROR_INVALID_SHORTNAME => "Le nom court est invalide ou vide.",
237 NLIssue::ERROR_INVALID_UFC => "Le filtre des destinataires est invalide.",
238 NLIssue::ERROR_SQL_SAVE => "Une erreur est survenue en tentant de sauvegarder la lettre, merci de réessayer.",
239 );
240
241 // Update the current issue
2d1ed661 242 if($aid == 'update' && Post::has('submit')) {
84163d58
RB
243
244 // Save common fields
245 $issue->title = Post::s('title');
246 $issue->title_mail = Post::s('title_mail');
247 $issue->head = Post::s('head');
248 $issue->signature = Post::s('signature');
249
250 if ($issue->isEditable()) {
251 // Date and shortname may only be modified for pending NLs, otherwise all links get broken.
252 $issue->date = Post::s('date');
253 $issue->shortname = strlen(Post::blank('shortname')) ? null : Post::s('shortname');
254 $issue->sufb->updateFromEnv($ufb->getEnv());
255
256 if ($nl->automaticMailingEnabled()) {
257 $issue->send_before = preg_replace('/^(\d\d\d\d)(\d\d)(\d\d)$/', '\1-\2-\3', Post::v('send_before_date')) . ' ' . Post::i('send_before_time_Hour') . ':00:00';
258 }
259 }
260 $errors = $issue->save();
261 if (count($errors)) {
262 foreach ($errors as $error_code) {
263 $page->trigError($error_msgs[$error_code]);
264 }
e2efba7d 265 }
266 }
eaf30d86 267
84163d58
RB
268 // Delete an article
269 if($action == 'delete') {
270 $issue->delArticle($aid);
271 pl_redirect($nl->adminPrefix() . "/edit/$nid");
272 }
273
274 // Save an article
e2efba7d 275 if(Post::v('save')) {
276 $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'),
9e2a6a32 277 $aid, Post::v('cid'), Post::v('pos'));
84163d58
RB
278 $issue->saveArticle($art);
279 pl_redirect($nl->adminPrefix() . "/edit/$nid");
e2efba7d 280 }
eaf30d86 281
84163d58 282 // Edit an article
9e2a6a32 283 if ($action == 'edit' && $aid != 'update') {
e2efba7d 284 $eaid = $aid;
e4705641 285 if (Post::has('title')) {
e2efba7d 286 $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'),
e4705641 287 $eaid, Post::v('cid'), Post::v('pos'));
e2efba7d 288 } else {
84163d58 289 $art = ($eaid == 'new') ? new NLArticle() : $issue->getArt($eaid);
e4705641
SJ
290 }
291 if ($art && !$art->check()) {
292 $page->trigError("Cet article est trop long.");
e2efba7d 293 }
294 $page->assign('art', $art);
295 }
eaf30d86 296
84163d58 297 // Check blacklisted IPs
9e2a6a32 298 if ($aid == 'blacklist_check') {
955109ba 299 global $globals;
9e2a6a32 300 $ips_to_check = array();
955109ba 301 $blacklist_host_resolution_count = 0;
9e2a6a32 302
84163d58 303 foreach ($issue->arts as $key => $articles) {
9e2a6a32 304 foreach ($articles as $article) {
955109ba 305 $article_ips = $article->getLinkIps($blacklist_host_resolution_count);
9e2a6a32
SJ
306 if (!empty($article_ips)) {
307 $ips_to_check[$article->title()] = $article_ips;
308 }
309 }
310 }
311
312 $page->assign('ips_to_check', $ips_to_check);
955109ba
SJ
313 if ($blacklist_host_resolution_count >= $globals->mail->blacklist_host_resolution_limit) {
314 $page->trigError("Toutes les url et adresses emails de la lettre"
9e2a6a32
SJ
315 . " n'ont pas été prises en compte car la"
316 . " limite du nombre de résolutions DNS"
317 . " autorisée a été atteinte.");
318 }
319 }
320
84163d58
RB
321 if ($issue->state == NLIssue::STATE_SENT) {
322 $page->trigWarning("Cette lettre a déjà été envoyée ; il est recommandé de limiter les modifications au maximum (orthographe, adresses web et mail).");
323 }
324
325 $ufb->setEnv($issue->sufb->getEnv());
9e2a6a32 326 $page->assign_by_ref('nl', $nl);
84163d58
RB
327 $page->assign_by_ref('issue', $issue);
328 }
329
330 /** This handler will cancel the sending of the currently pending issue
331 * It is disabled for X.org mailings.
332 */
26ba053e 333 function handler_admin_nl_cancel($page, $nid, $force = null)
84163d58
RB
334 {
335 $nl = $this->getNl();
336 if (!$nl) {
337 return PL_NOT_FOUND;
338 }
339
340 if (!$nl->mayEdit() || !S::has_xsrf_token()) {
341 return PL_FORBIDDEN;
342 }
343
344 if (!$nid) {
345 $page->kill("La lettre n'a pas été spécifiée");
346 }
347
348 $issue = $nl->getIssue($nid);
349 if (!$issue) {
350 $page->kill("La lettre {$nid} n'existe pas.");
351 }
352 if (!$issue->cancelMailing()) {
353 $page->trigErrorRedirect("Une erreur est survenue lors de l'annulation de l'envoi.", $nl->adminPrefix());
354 }
355
356 $page->trigSuccessRedirect("L'envoi de l'annonce {$issue->title()} est annulé.", $nl->adminPrefix());
357 }
358
359 /** This handler will enable the sending of the currently pending issue
360 * It is disabled for X.org mailings.
361 */
26ba053e 362 function handler_admin_nl_valid($page, $nid, $force = null)
84163d58
RB
363 {
364 $nl = $this->getNl();
365 if (!$nl) {
366 return PL_NOT_FOUND;
367 }
368
369 if (!$nl->mayEdit() || !S::has_xsrf_token()) {
370 return PL_FORBIDDEN;
371 }
372
373 if (!$nid) {
374 $page->kill("La lettre n'a pas été spécifiée.");
375 }
376
377 $issue = $nl->getIssue($nid);
378 if (!$issue) {
379 $page->kill("La lettre {$nid} n'existe pas.");
380 }
381 if (!$issue->scheduleMailing()) {
382 $page->trigErrorRedirect("Une erreur est survenue lors de la validation de l'envoi.", $nl->adminPrefix());
383 }
384
385 $page->trigSuccessRedirect("L'envoi de la newsletter {$issue->title()} a été validé.", $nl->adminPrefix());
386 }
387
388 /** This handler will remove the given issue.
389 */
26ba053e 390 function handler_admin_nl_delete($page, $nid, $force = null)
84163d58
RB
391 {
392 $nl = $this->getNl();
393 if (!$nl) {
394 return PL_NOT_FOUND;
395 }
396
397 if (!$nl->mayEdit() || !S::has_xsrf_token()) {
398 return PL_FORBIDDEN;
399 }
400
401 if (!$nid) {
402 $page->kill("La lettre n'a pas été spécifiée.");
403 }
404
405 $issue = $nl->getIssue($nid);
406 if (!$issue) {
407 $page->kill("La lettre {$nid} n'existe pas");
408 }
409 if (!$issue->isEditable()) {
410 $page->trigErrorRedirect("La lette a été envoyée ou est en cours d'envoi, elle ne peut être supprimée.", $nl->adminPrefix());
411 }
412 if (!$issue->delete()) {
413 $page->trigErrorRedirect("Une erreur est survenue lors de la suppression de la lettre.", $nl->adminPrefix());
414 }
415
416 $page->trigSuccessRedirect("La lettre a bien été supprimée.", $nl->adminPrefix());
e2efba7d 417 }
418
26ba053e 419 function handler_admin_nl_cat($page, $action = 'list', $id = null) {
46f272fe 420 $page->setTitle('Administration - Newsletter : Catégories');
a7de4ef7 421 $page->assign('title', 'Gestion des catégories de la newsletter');
e2efba7d 422 $table_editor = new PLTableEditor('admin/newsletter/categories','newsletter_cat','cid');
84163d58 423 $table_editor->describe('title','intitulé',true);
e2efba7d 424 $table_editor->describe('pos','position',true);
425 $table_editor->apply($page, $action, $id);
bc0903c7 426 }
e2efba7d 427}
428
a7de4ef7 429// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
e2efba7d 430?>