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 'nl/remaining' => $this->make_hook('nl_remaining', AUTH_MDP
),
31 'admin/nls' => $this->make_hook('admin_nl_groups', AUTH_MDP
, 'admin'),
32 'admin/newsletter' => $this->make_hook('admin_nl', AUTH_MDP
, 'admin'),
33 'admin/newsletter/categories' => $this->make_hook('admin_nl_cat', AUTH_MDP
, 'admin'),
34 'admin/newsletter/edit' => $this->make_hook('admin_nl_edit', AUTH_MDP
, 'admin'),
35 'admin/newsletter/edit/delete' => $this->make_hook('delete', AUTH_MDP
, 'admin'),
36 // Automatic mailing is disabled for X.org NL
37 // 'admin/newsletter/edit/cancel' => $this->make_hook('cancel', AUTH_MDP, 'admin'),
38 // 'admin/newsletter/edit/valid' => $this->make_hook('valid', AUTH_MDP, 'admin'),
42 /** This function should return the adequate NewsLetter object for the current module.
44 protected function getNl()
46 require_once 'newsletter.inc.php';
47 return NewsLetter
::forGroup(NewsLetter
::GROUP_XORG
);
50 function handler_nl($page, $action = null
, $hash = null
)
57 $page->changeTpl('newsletter/index.tpl');
58 $page->setTitle('Lettres mensuelles');
61 case 'out': $nl->unsubscribe($hash, $hash != null
); break;
62 case 'in': $nl->subscribe(); break;
66 $page->assign_by_ref('nl', $nl);
67 $page->assign('nls', $nl->subscriptionState());
68 $page->assign('nl_list', $nl->listSentIssues(true
));
71 function handler_nl_show($page, $nid = 'last')
73 $page->changeTpl('newsletter/show.tpl');
80 $issue = $nl->getIssue($nid);
82 if (Get
::has('text')) {
83 $issue->toText($page, $user);
85 $issue->toHtml($page, $user);
87 if (Post
::has('send')) {
88 $issue->sendTo($user);
90 } catch (MailNotFound
$e) {
95 function handler_nl_submit($page)
97 $page->changeTpl('newsletter/submit.tpl');
104 $wp = new PlWikiPage('Xorg.LettreMensuelle');
107 if (Post
::has('see') ||
(Post
::has('valid') && (!trim(Post
::v('title')) ||
!trim(Post
::v('body'))))) {
108 if (!Post
::has('see')) {
109 $page->trigError("L'article doit avoir un titre et un contenu");
111 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'));
112 $page->assign('art', $art);
113 } elseif (Post
::has('valid')) {
114 $art = new NLReq(S
::user(), Post
::v('title'),
115 Post
::v('body'), Post
::v('append'));
117 $page->assign('submited', true
);
119 $page->addCssLink($nl->cssFile());
122 function handler_nl_remaining($page)
124 require_once 'newsletter.inc.php';
126 pl_content_headers('text/html');
127 $page->changeTpl('newsletter/remaining.tpl', NO_SKIN
);
129 $article = new NLArticle('', Post
::t('body'), '');
130 $rest = $article->remain();
132 $page->assign('too_long', $rest['remaining_lines'] < 0);
133 $page->assign('last_line', ($rest['remaining_lines'] == 0));
134 $page->assign('remaining', ($rest['remaining_lines'] == 0) ?
$rest['remaining_characters_for_last_line'] : $rest['remaining_lines']);
137 function handler_admin_nl($page, $new = false
) {
138 $page->changeTpl('newsletter/admin.tpl');
139 $page->setTitle('Administration - Newsletter : liste');
141 $nl = $this->getNl();
147 $id = $nl->createPending();
148 pl_redirect($nl->adminPrefix() . '/edit/' . $id);
151 $page->assign_by_ref('nl', $nl);
152 $page->assign('nl_list', $nl->listAllIssues());
155 function handler_admin_nl_groups($page)
157 require_once 'newsletter.inc.php';
159 $page->changeTpl('newsletter/admin_all.tpl');
160 $page->setTitle('Administration - Newsletters : Liste des Newsletters');
162 $page->assign('nls', Newsletter
::getAll());
165 function handler_admin_nl_edit($page, $nid = 'last', $aid = null
, $action = 'edit') {
166 $page->changeTpl('newsletter/edit.tpl');
167 $page->addCssLink('nl.Polytechnique.org.css');
168 $page->setTitle('Administration - Newsletter : Édition');
170 $nl = $this->getNl();
176 $issue = $nl->getIssue($nid, false
);
177 } catch (MailNotFound
$e) {
181 $ufb = $nl->getSubscribersUFB();
182 $ufb_keepenv = false
; // Will be set to True if there were invalid modification to the UFB.
184 // Convert NLIssue error messages to human-readable errors
186 NLIssue
::ERROR_INVALID_SHORTNAME
=> "Le nom court est invalide ou vide.",
187 NLIssue
::ERROR_INVALID_UFC
=> "Le filtre des destinataires est invalide.",
188 NLIssue
::ERROR_SQL_SAVE
=> "Une erreur est survenue en tentant de sauvegarder la lettre, merci de réessayer.",
191 // Update the current issue
192 if($aid == 'update' && Post
::has('submit')) {
194 // Save common fields
195 $issue->title
= Post
::s('title');
196 $issue->title_mail
= Post
::s('title_mail');
197 $issue->head
= Post
::s('head');
198 $issue->signature
= Post
::s('signature');
200 if ($issue->isEditable()) {
201 // Date and shortname may only be modified for pending NLs, otherwise all links get broken.
202 $issue->date
= Post
::s('date');
203 $issue->shortname
= strlen(Post
::blank('shortname')) ? null
: Post
::s('shortname');
204 $issue->sufb
->updateFromEnv($ufb->getEnv());
206 if ($nl->automaticMailingEnabled()) {
207 $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';
210 $errors = $issue->save();
211 if (count($errors)) {
212 foreach ($errors as $error_code) {
213 $page->trigError($error_msgs[$error_code]);
219 if($action == 'delete') {
220 $issue->delArticle($aid);
221 pl_redirect($nl->adminPrefix() . "/edit/$nid");
225 if(Post
::v('save')) {
226 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'),
227 $aid, Post
::v('cid'), Post
::v('pos'));
228 $issue->saveArticle($art);
229 pl_redirect($nl->adminPrefix() . "/edit/$nid");
233 if ($action == 'edit' && $aid != 'update') {
235 if (Post
::has('title')) {
236 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'),
237 $eaid, Post
::v('cid'), Post
::v('pos'));
239 $art = ($eaid == 'new') ?
new NLArticle() : $issue->getArt($eaid);
241 if ($art && !$art->check()) {
242 $page->trigError("Cet article est trop long.");
244 $page->assign('art', $art);
247 // Check blacklisted IPs
248 if ($aid == 'blacklist_check') {
250 $ips_to_check = array();
251 $blacklist_host_resolution_count = 0;
253 foreach ($issue->arts
as $key => $articles) {
254 foreach ($articles as $article) {
255 $article_ips = $article->getLinkIps($blacklist_host_resolution_count);
256 if (!empty($article_ips)) {
257 $ips_to_check[$article->title()] = $article_ips;
262 $page->assign('ips_to_check', $ips_to_check);
263 if ($blacklist_host_resolution_count >= $globals->mail
->blacklist_host_resolution_limit
) {
264 $page->trigError("Toutes les url et adresses emails de la lettre"
265 . " n'ont pas été prises en compte car la"
266 . " limite du nombre de résolutions DNS"
267 . " autorisée a été atteinte.");
271 if ($issue->state
== NLIssue
::STATE_SENT
) {
272 $page->trigWarning("Cette lettre a déjà été envoyée ; il est recommandé de limiter les modifications au maximum (orthographe, adresses web et mail).");
275 $ufb->setEnv($issue->sufb
->getEnv());
276 $page->assign_by_ref('nl', $nl);
277 $page->assign_by_ref('issue', $issue);
280 /** This handler will cancel the sending of the currently pending issue
281 * It is disabled for X.org mailings.
283 function handler_admin_nl_cancel($page, $nid, $force = null
)
285 $nl = $this->getNl();
290 if (!$nl->mayEdit() ||
!S
::has_xsrf_token()) {
295 $page->kill("La lettre n'a pas été spécifiée");
298 $issue = $nl->getIssue($nid);
300 $page->kill("La lettre {$nid} n'existe pas.");
302 if (!$issue->cancelMailing()) {
303 $page->trigErrorRedirect("Une erreur est survenue lors de l'annulation de l'envoi.", $nl->adminPrefix());
306 $page->trigSuccessRedirect("L'envoi de l'annonce {$issue->title()} est annulé.", $nl->adminPrefix());
309 /** This handler will enable the sending of the currently pending issue
310 * It is disabled for X.org mailings.
312 function handler_admin_nl_valid($page, $nid, $force = null
)
314 $nl = $this->getNl();
319 if (!$nl->mayEdit() ||
!S
::has_xsrf_token()) {
324 $page->kill("La lettre n'a pas été spécifiée.");
327 $issue = $nl->getIssue($nid);
329 $page->kill("La lettre {$nid} n'existe pas.");
331 if (!$issue->scheduleMailing()) {
332 $page->trigErrorRedirect("Une erreur est survenue lors de la validation de l'envoi.", $nl->adminPrefix());
335 $page->trigSuccessRedirect("L'envoi de la newsletter {$issue->title()} a été validé.", $nl->adminPrefix());
338 /** This handler will remove the given issue.
340 function handler_admin_nl_delete($page, $nid, $force = null
)
342 $nl = $this->getNl();
347 if (!$nl->mayEdit() ||
!S
::has_xsrf_token()) {
352 $page->kill("La lettre n'a pas été spécifiée.");
355 $issue = $nl->getIssue($nid);
357 $page->kill("La lettre {$nid} n'existe pas");
359 if (!$issue->isEditable()) {
360 $page->trigErrorRedirect("La lette a été envoyée ou est en cours d'envoi, elle ne peut être supprimée.", $nl->adminPrefix());
362 if (!$issue->delete()) {
363 $page->trigErrorRedirect("Une erreur est survenue lors de la suppression de la lettre.", $nl->adminPrefix());
366 $page->trigSuccessRedirect("La lettre a bien été supprimée.", $nl->adminPrefix());
369 function handler_admin_nl_cat($page, $action = 'list', $id = null
) {
370 $page->setTitle('Administration - Newsletter : Catégories');
371 $page->assign('title', 'Gestion des catégories de la newsletter');
372 $table_editor = new PLTableEditor('admin/newsletter/categories','newsletter_cat','cid');
373 $table_editor->describe('title','intitulé',true
);
374 $table_editor->describe('pos','position',true
);
375 $table_editor->apply($page, $action, $id);
379 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: