Typo.
[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),
29 'nl/submit' => $this->make_hook('nl_submit', AUTH_MDP),
30 'admin/nls' => $this->make_hook('admin_nl_groups', AUTH_MDP, 'admin'),
31 'admin/newsletter' => $this->make_hook('admin_nl', AUTH_MDP, 'admin'),
32 'admin/newsletter/categories' => $this->make_hook('admin_nl_cat', AUTH_MDP, 'admin'),
33 'admin/newsletter/edit' => $this->make_hook('admin_nl_edit', AUTH_MDP, 'admin'),
34 'admin/newsletter/edit/delete' => $this->make_hook('delete', AUTH_MDP, 'admin'),
35 // Automatic mailing is disabled for X.org NL
36// 'admin/newsletter/edit/cancel' => $this->make_hook('cancel', AUTH_MDP, 'admin'),
37// 'admin/newsletter/edit/valid' => $this->make_hook('valid', AUTH_MDP, 'admin'),
e2efba7d 38 );
39 }
40
84163d58
RB
41 /** This function should return the adequate NewsLetter object for the current module.
42 */
43 protected function getNl()
e2efba7d 44 {
45 require_once 'newsletter.inc.php';
84163d58
RB
46 return NewsLetter::forGroup(NewsLetter::GROUP_XORG);
47 }
48
49 function handler_nl(&$page, $action = null, $hash = null)
50 {
51 $nl = $this->getNl();
52 if (!$nl) {
53 return PL_NOT_FOUND;
54 }
e2efba7d 55
56 $page->changeTpl('newsletter/index.tpl');
46f272fe 57 $page->setTitle('Lettres mensuelles');
e2efba7d 58
59 switch ($action) {
84163d58
RB
60 case 'out': $nl->unsubscribe($hash, $hash != null); break;
61 case 'in': $nl->subscribe(); break;
e2efba7d 62 default: ;
63 }
64
84163d58
RB
65 $page->assign_by_ref('nl', $nl);
66 $page->assign('nls', $nl->subscriptionState());
67 $page->assign('nl_list', $nl->listSentIssues(true));
e2efba7d 68 }
69
70 function handler_nl_show(&$page, $nid = 'last')
71 {
72 $page->changeTpl('newsletter/show.tpl');
84163d58
RB
73 $nl = $this->getNl();
74 if (!$nl) {
75 return PL_NOT_FOUND;
76 }
e2efba7d 77
9da70671 78 try {
84163d58 79 $issue = $nl->getIssue($nid);
6d1747b3 80 $user =& S::user();
9da70671 81 if (Get::has('text')) {
84163d58 82 $issue->toText($page, $user);
9da70671 83 } else {
84163d58 84 $issue->toHtml($page, $user);
9da70671
SJ
85 }
86 if (Post::has('send')) {
84163d58 87 $issue->sendTo($user);
9da70671
SJ
88 }
89 } catch (MailNotFound $e) {
90 return PL_NOT_FOUND;
e2efba7d 91 }
92 }
93
94 function handler_nl_submit(&$page)
95 {
96 $page->changeTpl('newsletter/submit.tpl');
97
84163d58
RB
98 $nl = $this->getNl();
99 if (!$nl) {
100 return PL_NOT_FOUND;
101 }
102
8f201b69
FB
103 $wp = new PlWikiPage('Xorg.LettreMensuelle');
104 $wp->buildCache();
e2efba7d 105
1970c12b 106 if (Post::has('see') || (Post::has('valid') && (!trim(Post::v('title')) || !trim(Post::v('body'))))) {
107 if (!Post::has('see')) {
a7d35093 108 $page->trigError("L'article doit avoir un titre et un contenu");
1970c12b 109 }
e2efba7d 110 $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'));
111 $page->assign('art', $art);
112 } elseif (Post::has('valid')) {
5daf68f6 113 $art = new NLReq(S::user(), Post::v('title'),
e2efba7d 114 Post::v('body'), Post::v('append'));
115 $art->submit();
116 $page->assign('submited', true);
117 }
84163d58 118 $page->addCssLink($nl->cssFile());
e2efba7d 119 }
120
121 function handler_admin_nl(&$page, $new = false) {
122 $page->changeTpl('newsletter/admin.tpl');
46f272fe 123 $page->setTitle('Administration - Newsletter : liste');
84163d58
RB
124
125 $nl = $this->getNl();
126 if (!$nl) {
127 return PL_NOT_FOUND;
128 }
eaf30d86 129
e2efba7d 130 if($new) {
84163d58
RB
131 $id = $nl->createPending();
132 pl_redirect($nl->adminPrefix() . '/edit/' . $id);
e2efba7d 133 }
eaf30d86 134
84163d58
RB
135 $page->assign_by_ref('nl', $nl);
136 $page->assign('nl_list', $nl->listAllIssues());
137 }
138
139 function handler_admin_nl_groups(&$page)
140 {
141 require_once 'newsletter.inc.php';
142
143 $page->changeTpl('newsletter/admin_all.tpl');
144 $page->setTitle('Administration - Newsletters : Liste des Newsletters');
145
146 $page->assign('nls', Newsletter::getAll());
e2efba7d 147 }
eaf30d86 148
e2efba7d 149 function handler_admin_nl_edit(&$page, $nid = 'last', $aid = null, $action = 'edit') {
150 $page->changeTpl('newsletter/edit.tpl');
84163d58 151 $page->addCssLink('nl.Polytechnique.org.css');
e4705641 152 $page->setTitle('Administration - Newsletter : Édition');
eaf30d86 153
84163d58
RB
154 $nl = $this->getNl();
155 if (!$nl) {
156 return PL_NOT_FOUND;
157 }
eaf30d86 158
84163d58
RB
159 try {
160 $issue = $nl->getIssue($nid, false);
161 } catch (MailNotFound $e) {
162 return PL_NOT_FOUND;
e2efba7d 163 }
eaf30d86 164
84163d58
RB
165 $ufb = $nl->getSubscribersUFB();
166 $ufb_keepenv = false; // Will be set to True if there were invalid modification to the UFB.
167
168 // Convert NLIssue error messages to human-readable errors
169 $error_msgs = array(
170 NLIssue::ERROR_INVALID_SHORTNAME => "Le nom court est invalide ou vide.",
171 NLIssue::ERROR_INVALID_UFC => "Le filtre des destinataires est invalide.",
172 NLIssue::ERROR_SQL_SAVE => "Une erreur est survenue en tentant de sauvegarder la lettre, merci de réessayer.",
173 );
174
175 // Update the current issue
e2efba7d 176 if($aid == 'update') {
84163d58
RB
177
178 // Save common fields
179 $issue->title = Post::s('title');
180 $issue->title_mail = Post::s('title_mail');
181 $issue->head = Post::s('head');
182 $issue->signature = Post::s('signature');
183
184 if ($issue->isEditable()) {
185 // Date and shortname may only be modified for pending NLs, otherwise all links get broken.
186 $issue->date = Post::s('date');
187 $issue->shortname = strlen(Post::blank('shortname')) ? null : Post::s('shortname');
188 $issue->sufb->updateFromEnv($ufb->getEnv());
189
190 if ($nl->automaticMailingEnabled()) {
191 $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';
192 }
193 }
194 $errors = $issue->save();
195 if (count($errors)) {
196 foreach ($errors as $error_code) {
197 $page->trigError($error_msgs[$error_code]);
198 }
e2efba7d 199 }
200 }
eaf30d86 201
84163d58
RB
202 // Delete an article
203 if($action == 'delete') {
204 $issue->delArticle($aid);
205 pl_redirect($nl->adminPrefix() . "/edit/$nid");
206 }
207
208 // Save an article
e2efba7d 209 if(Post::v('save')) {
210 $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'),
9e2a6a32 211 $aid, Post::v('cid'), Post::v('pos'));
84163d58
RB
212 $issue->saveArticle($art);
213 pl_redirect($nl->adminPrefix() . "/edit/$nid");
e2efba7d 214 }
eaf30d86 215
84163d58 216 // Edit an article
9e2a6a32 217 if ($action == 'edit' && $aid != 'update') {
e2efba7d 218 $eaid = $aid;
e4705641 219 if (Post::has('title')) {
e2efba7d 220 $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'),
e4705641 221 $eaid, Post::v('cid'), Post::v('pos'));
e2efba7d 222 } else {
84163d58 223 $art = ($eaid == 'new') ? new NLArticle() : $issue->getArt($eaid);
e4705641
SJ
224 }
225 if ($art && !$art->check()) {
226 $page->trigError("Cet article est trop long.");
e2efba7d 227 }
228 $page->assign('art', $art);
229 }
eaf30d86 230
84163d58 231 // Check blacklisted IPs
9e2a6a32 232 if ($aid == 'blacklist_check') {
955109ba 233 global $globals;
9e2a6a32 234 $ips_to_check = array();
955109ba 235 $blacklist_host_resolution_count = 0;
9e2a6a32 236
84163d58 237 foreach ($issue->arts as $key => $articles) {
9e2a6a32 238 foreach ($articles as $article) {
955109ba 239 $article_ips = $article->getLinkIps($blacklist_host_resolution_count);
9e2a6a32
SJ
240 if (!empty($article_ips)) {
241 $ips_to_check[$article->title()] = $article_ips;
242 }
243 }
244 }
245
246 $page->assign('ips_to_check', $ips_to_check);
955109ba
SJ
247 if ($blacklist_host_resolution_count >= $globals->mail->blacklist_host_resolution_limit) {
248 $page->trigError("Toutes les url et adresses emails de la lettre"
9e2a6a32
SJ
249 . " n'ont pas été prises en compte car la"
250 . " limite du nombre de résolutions DNS"
251 . " autorisée a été atteinte.");
252 }
253 }
254
84163d58
RB
255 if ($issue->state == NLIssue::STATE_SENT) {
256 $page->trigWarning("Cette lettre a déjà été envoyée ; il est recommandé de limiter les modifications au maximum (orthographe, adresses web et mail).");
257 }
258
259 $ufb->setEnv($issue->sufb->getEnv());
9e2a6a32 260 $page->assign_by_ref('nl', $nl);
84163d58
RB
261 $page->assign_by_ref('issue', $issue);
262 }
263
264 /** This handler will cancel the sending of the currently pending issue
265 * It is disabled for X.org mailings.
266 */
267 function handler_admin_nl_cancel(&$page, $nid, $force = null)
268 {
269 $nl = $this->getNl();
270 if (!$nl) {
271 return PL_NOT_FOUND;
272 }
273
274 if (!$nl->mayEdit() || !S::has_xsrf_token()) {
275 return PL_FORBIDDEN;
276 }
277
278 if (!$nid) {
279 $page->kill("La lettre n'a pas été spécifiée");
280 }
281
282 $issue = $nl->getIssue($nid);
283 if (!$issue) {
284 $page->kill("La lettre {$nid} n'existe pas.");
285 }
286 if (!$issue->cancelMailing()) {
287 $page->trigErrorRedirect("Une erreur est survenue lors de l'annulation de l'envoi.", $nl->adminPrefix());
288 }
289
290 $page->trigSuccessRedirect("L'envoi de l'annonce {$issue->title()} est annulé.", $nl->adminPrefix());
291 }
292
293 /** This handler will enable the sending of the currently pending issue
294 * It is disabled for X.org mailings.
295 */
296 function handler_admin_nl_valid(&$page, $nid, $force = null)
297 {
298 $nl = $this->getNl();
299 if (!$nl) {
300 return PL_NOT_FOUND;
301 }
302
303 if (!$nl->mayEdit() || !S::has_xsrf_token()) {
304 return PL_FORBIDDEN;
305 }
306
307 if (!$nid) {
308 $page->kill("La lettre n'a pas été spécifiée.");
309 }
310
311 $issue = $nl->getIssue($nid);
312 if (!$issue) {
313 $page->kill("La lettre {$nid} n'existe pas.");
314 }
315 if (!$issue->scheduleMailing()) {
316 $page->trigErrorRedirect("Une erreur est survenue lors de la validation de l'envoi.", $nl->adminPrefix());
317 }
318
319 $page->trigSuccessRedirect("L'envoi de la newsletter {$issue->title()} a été validé.", $nl->adminPrefix());
320 }
321
322 /** This handler will remove the given issue.
323 */
324 function handler_admin_nl_delete(&$page, $nid, $force = null)
325 {
326 $nl = $this->getNl();
327 if (!$nl) {
328 return PL_NOT_FOUND;
329 }
330
331 if (!$nl->mayEdit() || !S::has_xsrf_token()) {
332 return PL_FORBIDDEN;
333 }
334
335 if (!$nid) {
336 $page->kill("La lettre n'a pas été spécifiée.");
337 }
338
339 $issue = $nl->getIssue($nid);
340 if (!$issue) {
341 $page->kill("La lettre {$nid} n'existe pas");
342 }
343 if (!$issue->isEditable()) {
344 $page->trigErrorRedirect("La lette a été envoyée ou est en cours d'envoi, elle ne peut être supprimée.", $nl->adminPrefix());
345 }
346 if (!$issue->delete()) {
347 $page->trigErrorRedirect("Une erreur est survenue lors de la suppression de la lettre.", $nl->adminPrefix());
348 }
349
350 $page->trigSuccessRedirect("La lettre a bien été supprimée.", $nl->adminPrefix());
e2efba7d 351 }
352
353 function handler_admin_nl_cat(&$page, $action = 'list', $id = null) {
46f272fe 354 $page->setTitle('Administration - Newsletter : Catégories');
a7de4ef7 355 $page->assign('title', 'Gestion des catégories de la newsletter');
e2efba7d 356 $table_editor = new PLTableEditor('admin/newsletter/categories','newsletter_cat','cid');
84163d58 357 $table_editor->describe('title','intitulé',true);
e2efba7d 358 $table_editor->describe('pos','position',true);
359 $table_editor->apply($page, $action, $id);
bc0903c7 360 }
e2efba7d 361}
362
a7de4ef7 363// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
e2efba7d 364?>