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/search' => $this->make_hook('nl_search', AUTH_COOKIE
),
30 'nl/submit' => $this->make_hook('nl_submit', AUTH_MDP
),
31 'nl/remaining' => $this->make_hook('nl_remaining', AUTH_MDP
),
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'),
36 'admin/newsletter/edit/delete' => $this->make_hook('admin_nl_delete', AUTH_MDP
, 'admin'),
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'),
43 /** This function should return the adequate NewsLetter object for the current module.
45 protected function getNl()
47 require_once 'newsletter.inc.php';
48 return NewsLetter
::forGroup(NewsLetter
::GROUP_XORG
);
51 function handler_nl($page, $action = null
, $hash = null
)
58 $page->changeTpl('newsletter/index.tpl');
59 $page->setTitle('Lettres mensuelles');
62 case 'out': $nl->unsubscribe($hash, $hash != null
); break;
63 case 'in': $nl->subscribe(); break;
67 $page->assign_by_ref('nl', $nl);
68 $page->assign('nls', $nl->subscriptionState());
69 $page->assign('nl_list', $nl->listSentIssues(true
));
72 function handler_nl_show($page, $nid = 'last')
74 $page->changeTpl('newsletter/show.tpl');
81 $issue = $nl->getIssue($nid);
83 if (Get
::has('text')) {
84 $issue->toText($page, $user);
86 $issue->toHtml($page, $user);
88 if (Post
::has('send')) {
89 $issue->sendTo($user);
91 } catch (MailNotFound
$e) {
96 function handler_nl_search($page)
98 S
::assert_xsrf_token();
104 if (!Post
::has('nl_search')) {
105 pl_redirect($nl->prefix());
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());
114 $page->changeTpl('newsletter/search.tpl');
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);
121 if ($nl_search_type > 5 ||
$nl_search_type == 1) {
122 $res_issues = $nl->issueSearch($nl_search, $fields[$nl_search_type], $user);
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.');
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);
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);
145 function handler_nl_submit($page)
147 $page->changeTpl('newsletter/submit.tpl');
149 $nl = $this->getNl();
154 $wp = new PlWikiPage('Xorg.LettreMensuelle');
157 if (Post
::has('see') ||
(Post
::has('valid') && (!trim(Post
::v('title')) ||
!trim(Post
::v('body'))))) {
158 if (!Post
::has('see')) {
159 $page->trigError("L'article doit avoir un titre et un contenu");
161 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'));
162 $page->assign('art', $art);
163 } elseif (Post
::has('valid')) {
164 $art = new NLReq(S
::user(), Post
::v('title'),
165 Post
::v('body'), Post
::v('append'));
167 $page->assign('submited', true
);
169 $page->addCssLink($nl->cssFile());
172 function handler_nl_remaining($page)
174 require_once 'newsletter.inc.php';
176 pl_content_headers('text/html');
177 $page->changeTpl('newsletter/remaining.tpl', NO_SKIN
);
179 $article = new NLArticle('', Post
::t('body'), '');
180 $rest = $article->remain();
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']);
187 function handler_admin_nl($page, $new = false
) {
188 $page->changeTpl('newsletter/admin.tpl');
189 $page->setTitle('Administration - Newsletter : liste');
191 $nl = $this->getNl();
198 S
::logger()->log('nl_issue_create', $nid);
200 $id = $nl->createPending();
201 pl_redirect($nl->adminPrefix() . '/edit/' . $id);
204 $page->assign_by_ref('nl', $nl);
205 $page->assign('nl_list', $nl->listAllIssues());
208 function handler_admin_nl_groups($page)
210 require_once 'newsletter.inc.php';
212 $page->changeTpl('newsletter/admin_all.tpl');
213 $page->setTitle('Administration - Newsletters : Liste des Newsletters');
215 $page->assign('nls', Newsletter
::getAll());
218 function handler_admin_nl_edit($page, $nid = 'last', $aid = null
, $action = 'edit') {
219 $page->changeTpl('newsletter/edit.tpl');
220 $page->addCssLink('nl.Polytechnique.org.css');
221 $page->setTitle('Administration - Newsletter : Édition');
223 $nl = $this->getNl();
229 $issue = $nl->getIssue($nid, false
);
230 } catch (MailNotFound
$e) {
234 $ufb = $nl->getSubscribersUFB();
235 $ufb_keepenv = false
; // Will be set to True if there were invalid modification to the UFB.
237 // Convert NLIssue error messages to human-readable errors
239 NLIssue
::ERROR_INVALID_SHORTNAME
=> "Le nom court est invalide ou vide.",
240 NLIssue
::ERROR_INVALID_UFC
=> "Le filtre des destinataires est invalide.",
241 NLIssue
::ERROR_TOO_LONG_UFC
=> "Le nombre de matricules AX renseigné est trop élevé.",
242 NLIssue
::ERROR_SQL_SAVE
=> "Une erreur est survenue en tentant de sauvegarder la lettre, merci de réessayer.",
245 // Update the current issue
246 if($aid == 'update' && Post
::has('submit')) {
248 // Save common fields
249 $issue->title
= Post
::s('title');
250 $issue->title_mail
= Post
::s('title_mail');
251 $issue->head
= Post
::s('head');
252 $issue->signature
= Post
::s('signature');
254 if ($issue->isEditable()) {
255 // Date and shortname may only be modified for pending NLs, otherwise all links get broken.
256 $issue->date
= Post
::s('date');
257 $issue->shortname
= strlen(Post
::blank('shortname')) ? null
: Post
::s('shortname');
258 $issue->sufb
->updateFromEnv($ufb->getEnv());
260 if ($nl->automaticMailingEnabled()) {
261 $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';
264 $errors = $issue->save();
265 if (count($errors)) {
266 foreach ($errors as $error_code) {
267 $page->trigError($error_msgs[$error_code]);
273 if($action == 'delete') {
274 $issue->delArticle($aid);
275 pl_redirect($nl->adminPrefix() . "/edit/$nid");
279 if(Post
::v('save')) {
280 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'),
281 $aid, Post
::v('cid'), Post
::v('pos'));
282 $issue->saveArticle($art);
283 pl_redirect($nl->adminPrefix() . "/edit/$nid");
287 if ($action == 'edit' && $aid != 'update') {
289 if (Post
::has('title')) {
290 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'),
291 $eaid, Post
::v('cid'), Post
::v('pos'));
293 $art = ($eaid == 'new') ?
new NLArticle() : $issue->getArt($eaid);
295 if ($art && !$art->check()) {
296 $page->trigError("Cet article est trop long.");
298 $page->assign('art', $art);
301 // Check blacklisted IPs
302 if ($aid == 'blacklist_check') {
304 $ips_to_check = array();
305 $blacklist_host_resolution_count = 0;
307 foreach ($issue->arts
as $key => $articles) {
308 foreach ($articles as $article) {
309 $article_ips = $article->getLinkIps($blacklist_host_resolution_count);
310 if (!empty($article_ips)) {
311 $ips_to_check[$article->title()] = $article_ips;
316 $page->assign('ips_to_check', $ips_to_check);
317 if ($blacklist_host_resolution_count >= $globals->mail
->blacklist_host_resolution_limit
) {
318 $page->trigError("Toutes les url et adresses emails de la lettre"
319 . " n'ont pas été prises en compte car la"
320 . " limite du nombre de résolutions DNS"
321 . " autorisée a été atteinte.");
325 if ($issue->state
== NLIssue
::STATE_SENT
) {
326 $page->trigWarning("Cette lettre a déjà été envoyée ; il est recommandé de limiter les modifications au maximum (orthographe, adresses web et mail).");
329 $ufb->setEnv($issue->sufb
->getEnv());
330 $page->assign_by_ref('nl', $nl);
331 $page->assign_by_ref('issue', $issue);
334 /** This handler will cancel the sending of the currently pending issue
335 * It is disabled for X.org mailings.
337 function handler_admin_nl_cancel($page, $nid, $force = null
)
339 $nl = $this->getNl();
344 if (!$nl->mayEdit() ||
!S
::has_xsrf_token()) {
349 $page->kill("La lettre n'a pas été spécifiée");
352 $issue = $nl->getIssue($nid);
354 $page->kill("La lettre {$nid} n'existe pas.");
356 if (!$issue->cancelMailing()) {
357 $page->trigErrorRedirect("Une erreur est survenue lors de l'annulation de l'envoi.", $nl->adminPrefix());
360 // Logs NL cancelling.
361 S
::logger()->log('nl_mailing_cancel', $nid);
363 $page->trigSuccessRedirect("L'envoi de l'annonce {$issue->title()} est annulé.", $nl->adminPrefix());
366 /** This handler will enable the sending of the currently pending issue
367 * It is disabled for X.org mailings.
369 function handler_admin_nl_valid($page, $nid, $force = null
)
371 $nl = $this->getNl();
376 if (!$nl->mayEdit() ||
!S
::has_xsrf_token()) {
381 $page->kill("La lettre n'a pas été spécifiée.");
384 $issue = $nl->getIssue($nid);
386 $page->kill("La lettre {$nid} n'existe pas.");
388 if (!$issue->scheduleMailing()) {
389 $page->trigErrorRedirect("Une erreur est survenue lors de la validation de l'envoi.", $nl->adminPrefix());
392 // Logs NL validation.
393 S
::logger()->log('nl_mailing_valid', $nid);
395 $page->trigSuccessRedirect("L'envoi de la newsletter {$issue->title()} a été validé.", $nl->adminPrefix());
398 /** This handler will remove the given issue.
400 function handler_admin_nl_delete($page, $nid, $force = null
)
402 $nl = $this->getNl();
407 if (!$nl->mayEdit() ||
!S
::has_xsrf_token()) {
412 $page->kill("La lettre n'a pas été spécifiée.");
415 $issue = $nl->getIssue($nid);
417 $page->kill("La lettre {$nid} n'existe pas");
419 if (!$issue->isEditable()) {
420 $page->trigErrorRedirect("La lette a été envoyée ou est en cours d'envoi, elle ne peut être supprimée.", $nl->adminPrefix());
422 if (!$issue->delete()) {
423 $page->trigErrorRedirect("Une erreur est survenue lors de la suppression de la lettre.", $nl->adminPrefix());
427 S
::logger()->log('nl_issue_delete', $nid);
429 $page->trigSuccessRedirect("La lettre a bien été supprimée.", $nl->adminPrefix());
432 function handler_admin_nl_cat($page, $action = 'list', $id = null
) {
433 $page->setTitle('Administration - Newsletter : Catégories');
434 $page->assign('title', 'Gestion des catégories de la newsletter');
435 $table_editor = new PLTableEditor('admin/newsletter/categories','newsletter_cat','cid');
436 $table_editor->describe('title','intitulé',true
);
437 $table_editor->describe('pos','position',true
);
438 $table_editor->apply($page, $action, $id);
442 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: