| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2011 Polytechnique.org * |
| 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 | |
| 22 | class NewsletterModule extends PLModule |
| 23 | { |
| 24 | function handlers() |
| 25 | { |
| 26 | return array( |
| 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'), |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | /** This function should return the adequate NewsLetter object for the current module. |
| 42 | */ |
| 43 | protected function getNl() |
| 44 | { |
| 45 | require_once 'newsletter.inc.php'; |
| 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 | } |
| 55 | |
| 56 | $page->changeTpl('newsletter/index.tpl'); |
| 57 | $page->setTitle('Lettres mensuelles'); |
| 58 | |
| 59 | switch ($action) { |
| 60 | case 'out': $nl->unsubscribe($hash, $hash != null); break; |
| 61 | case 'in': $nl->subscribe(); break; |
| 62 | default: ; |
| 63 | } |
| 64 | |
| 65 | $page->assign_by_ref('nl', $nl); |
| 66 | $page->assign('nls', $nl->subscriptionState()); |
| 67 | $page->assign('nl_list', $nl->listSentIssues(true)); |
| 68 | } |
| 69 | |
| 70 | function handler_nl_show($page, $nid = 'last') |
| 71 | { |
| 72 | $page->changeTpl('newsletter/show.tpl'); |
| 73 | $nl = $this->getNl(); |
| 74 | if (!$nl) { |
| 75 | return PL_NOT_FOUND; |
| 76 | } |
| 77 | |
| 78 | try { |
| 79 | $issue = $nl->getIssue($nid); |
| 80 | $user =& S::user(); |
| 81 | if (Get::has('text')) { |
| 82 | $issue->toText($page, $user); |
| 83 | } else { |
| 84 | $issue->toHtml($page, $user); |
| 85 | } |
| 86 | if (Post::has('send')) { |
| 87 | $issue->sendTo($user); |
| 88 | } |
| 89 | } catch (MailNotFound $e) { |
| 90 | return PL_NOT_FOUND; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | function handler_nl_submit($page) |
| 95 | { |
| 96 | $page->changeTpl('newsletter/submit.tpl'); |
| 97 | |
| 98 | $nl = $this->getNl(); |
| 99 | if (!$nl) { |
| 100 | return PL_NOT_FOUND; |
| 101 | } |
| 102 | |
| 103 | $wp = new PlWikiPage('Xorg.LettreMensuelle'); |
| 104 | $wp->buildCache(); |
| 105 | |
| 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"); |
| 109 | } |
| 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')); |
| 115 | $art->submit(); |
| 116 | $page->assign('submited', true); |
| 117 | } |
| 118 | $page->addCssLink($nl->cssFile()); |
| 119 | } |
| 120 | |
| 121 | function handler_admin_nl($page, $new = false) { |
| 122 | $page->changeTpl('newsletter/admin.tpl'); |
| 123 | $page->setTitle('Administration - Newsletter : liste'); |
| 124 | |
| 125 | $nl = $this->getNl(); |
| 126 | if (!$nl) { |
| 127 | return PL_NOT_FOUND; |
| 128 | } |
| 129 | |
| 130 | if ($new == 'new') { |
| 131 | $id = $nl->createPending(); |
| 132 | pl_redirect($nl->adminPrefix() . '/edit/' . $id); |
| 133 | } |
| 134 | |
| 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()); |
| 147 | } |
| 148 | |
| 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'); |
| 153 | |
| 154 | $nl = $this->getNl(); |
| 155 | if (!$nl) { |
| 156 | return PL_NOT_FOUND; |
| 157 | } |
| 158 | |
| 159 | try { |
| 160 | $issue = $nl->getIssue($nid, false); |
| 161 | } catch (MailNotFound $e) { |
| 162 | return PL_NOT_FOUND; |
| 163 | } |
| 164 | |
| 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 |
| 176 | if($aid == 'update' && Post::has('submit')) { |
| 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 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 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 |
| 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"); |
| 214 | } |
| 215 | |
| 216 | // Edit an article |
| 217 | if ($action == 'edit' && $aid != 'update') { |
| 218 | $eaid = $aid; |
| 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')); |
| 222 | } else { |
| 223 | $art = ($eaid == 'new') ? new NLArticle() : $issue->getArt($eaid); |
| 224 | } |
| 225 | if ($art && !$art->check()) { |
| 226 | $page->trigError("Cet article est trop long."); |
| 227 | } |
| 228 | $page->assign('art', $art); |
| 229 | } |
| 230 | |
| 231 | // Check blacklisted IPs |
| 232 | if ($aid == 'blacklist_check') { |
| 233 | global $globals; |
| 234 | $ips_to_check = array(); |
| 235 | $blacklist_host_resolution_count = 0; |
| 236 | |
| 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; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 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."); |
| 252 | } |
| 253 | } |
| 254 | |
| 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()); |
| 260 | $page->assign_by_ref('nl', $nl); |
| 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()); |
| 351 | } |
| 352 | |
| 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); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 364 | ?> |