2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class NewsletterModule
extends PLModule
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'),
41 /** This function should return the adequate NewsLetter object for the current module.
43 protected function getNl()
45 require_once 'newsletter.inc.php';
46 return NewsLetter
::forGroup(NewsLetter
::GROUP_XORG
);
49 function handler_nl($page, $action = null
, $hash = null
)
56 $page->changeTpl('newsletter/index.tpl');
57 $page->setTitle('Lettres mensuelles');
60 case 'out': $nl->unsubscribe($hash, $hash != null
); break;
61 case 'in': $nl->subscribe(); break;
65 $page->assign_by_ref('nl', $nl);
66 $page->assign('nls', $nl->subscriptionState());
67 $page->assign('nl_list', $nl->listSentIssues(true
));
70 function handler_nl_show($page, $nid = 'last')
72 $page->changeTpl('newsletter/show.tpl');
79 $issue = $nl->getIssue($nid);
81 if (Get
::has('text')) {
82 $issue->toText($page, $user);
84 $issue->toHtml($page, $user);
86 if (Post
::has('send')) {
87 $issue->sendTo($user);
89 } catch (MailNotFound
$e) {
94 function handler_nl_submit($page)
96 $page->changeTpl('newsletter/submit.tpl');
103 $wp = new PlWikiPage('Xorg.LettreMensuelle');
106 if (Post
::has('see') ||
(Post
::has('valid') && (!trim(Post
::v('title')) ||
!trim(Post
::v('body'))))) {
107 if (!Post
::has('see')) {
108 $page->trigError("L'article doit avoir un titre et un contenu");
110 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'));
111 $page->assign('art', $art);
112 } elseif (Post
::has('valid')) {
113 $art = new NLReq(S
::user(), Post
::v('title'),
114 Post
::v('body'), Post
::v('append'));
116 $page->assign('submited', true
);
118 $page->addCssLink($nl->cssFile());
121 function handler_admin_nl($page, $new = false
) {
122 $page->changeTpl('newsletter/admin.tpl');
123 $page->setTitle('Administration - Newsletter : liste');
125 $nl = $this->getNl();
131 $id = $nl->createPending();
132 pl_redirect($nl->adminPrefix() . '/edit/' . $id);
135 $page->assign_by_ref('nl', $nl);
136 $page->assign('nl_list', $nl->listAllIssues());
139 function handler_admin_nl_groups($page)
141 require_once 'newsletter.inc.php';
143 $page->changeTpl('newsletter/admin_all.tpl');
144 $page->setTitle('Administration - Newsletters : Liste des Newsletters');
146 $page->assign('nls', Newsletter
::getAll());
149 function handler_admin_nl_edit($page, $nid = 'last', $aid = null
, $action = 'edit') {
150 $page->changeTpl('newsletter/edit.tpl');
151 $page->addCssLink('nl.Polytechnique.org.css');
152 $page->setTitle('Administration - Newsletter : Édition');
154 $nl = $this->getNl();
160 $issue = $nl->getIssue($nid, false
);
161 } catch (MailNotFound
$e) {
165 $ufb = $nl->getSubscribersUFB();
166 $ufb_keepenv = false
; // Will be set to True if there were invalid modification to the UFB.
168 // Convert NLIssue error messages to human-readable errors
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.",
175 // Update the current issue
176 if($aid == 'update' && Post
::has('submit')) {
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');
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());
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';
194 $errors = $issue->save();
195 if (count($errors)) {
196 foreach ($errors as $error_code) {
197 $page->trigError($error_msgs[$error_code]);
203 if($action == 'delete') {
204 $issue->delArticle($aid);
205 pl_redirect($nl->adminPrefix() . "/edit/$nid");
209 if(Post
::v('save')) {
210 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'),
211 $aid, Post
::v('cid'), Post
::v('pos'));
212 $issue->saveArticle($art);
213 pl_redirect($nl->adminPrefix() . "/edit/$nid");
217 if ($action == 'edit' && $aid != 'update') {
219 if (Post
::has('title')) {
220 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'),
221 $eaid, Post
::v('cid'), Post
::v('pos'));
223 $art = ($eaid == 'new') ?
new NLArticle() : $issue->getArt($eaid);
225 if ($art && !$art->check()) {
226 $page->trigError("Cet article est trop long.");
228 $page->assign('art', $art);
231 // Check blacklisted IPs
232 if ($aid == 'blacklist_check') {
234 $ips_to_check = array();
235 $blacklist_host_resolution_count = 0;
237 foreach ($issue->arts
as $key => $articles) {
238 foreach ($articles as $article) {
239 $article_ips = $article->getLinkIps($blacklist_host_resolution_count);
240 if (!empty($article_ips)) {
241 $ips_to_check[$article->title()] = $article_ips;
246 $page->assign('ips_to_check', $ips_to_check);
247 if ($blacklist_host_resolution_count >= $globals->mail
->blacklist_host_resolution_limit
) {
248 $page->trigError("Toutes les url et adresses emails de la lettre"
249 . " n'ont pas été prises en compte car la"
250 . " limite du nombre de résolutions DNS"
251 . " autorisée a été atteinte.");
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).");
259 $ufb->setEnv($issue->sufb
->getEnv());
260 $page->assign_by_ref('nl', $nl);
261 $page->assign_by_ref('issue', $issue);
264 /** This handler will cancel the sending of the currently pending issue
265 * It is disabled for X.org mailings.
267 function handler_admin_nl_cancel($page, $nid, $force = null
)
269 $nl = $this->getNl();
274 if (!$nl->mayEdit() ||
!S
::has_xsrf_token()) {
279 $page->kill("La lettre n'a pas été spécifiée");
282 $issue = $nl->getIssue($nid);
284 $page->kill("La lettre {$nid} n'existe pas.");
286 if (!$issue->cancelMailing()) {
287 $page->trigErrorRedirect("Une erreur est survenue lors de l'annulation de l'envoi.", $nl->adminPrefix());
290 $page->trigSuccessRedirect("L'envoi de l'annonce {$issue->title()} est annulé.", $nl->adminPrefix());
293 /** This handler will enable the sending of the currently pending issue
294 * It is disabled for X.org mailings.
296 function handler_admin_nl_valid($page, $nid, $force = null
)
298 $nl = $this->getNl();
303 if (!$nl->mayEdit() ||
!S
::has_xsrf_token()) {
308 $page->kill("La lettre n'a pas été spécifiée.");
311 $issue = $nl->getIssue($nid);
313 $page->kill("La lettre {$nid} n'existe pas.");
315 if (!$issue->scheduleMailing()) {
316 $page->trigErrorRedirect("Une erreur est survenue lors de la validation de l'envoi.", $nl->adminPrefix());
319 $page->trigSuccessRedirect("L'envoi de la newsletter {$issue->title()} a été validé.", $nl->adminPrefix());
322 /** This handler will remove the given issue.
324 function handler_admin_nl_delete($page, $nid, $force = null
)
326 $nl = $this->getNl();
331 if (!$nl->mayEdit() ||
!S
::has_xsrf_token()) {
336 $page->kill("La lettre n'a pas été spécifiée.");
339 $issue = $nl->getIssue($nid);
341 $page->kill("La lettre {$nid} n'existe pas");
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());
346 if (!$issue->delete()) {
347 $page->trigErrorRedirect("Une erreur est survenue lors de la suppression de la lettre.", $nl->adminPrefix());
350 $page->trigSuccessRedirect("La lettre a bien été supprimée.", $nl->adminPrefix());
353 function handler_admin_nl_cat($page, $action = 'list', $id = null
) {
354 $page->setTitle('Administration - Newsletter : Catégories');
355 $page->assign('title', 'Gestion des catégories de la newsletter');
356 $table_editor = new PLTableEditor('admin/newsletter/categories','newsletter_cat','cid');
357 $table_editor->describe('title','intitulé',true
);
358 $table_editor->describe('pos','position',true
);
359 $table_editor->apply($page, $action, $id);
363 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: