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 AXLetterModule
extends PLModule
27 'ax' => $this->make_hook('index', AUTH_COOKIE
),
28 'ax/out' => $this->make_hook('out', AUTH_PUBLIC
),
29 'ax/show' => $this->make_hook('show', AUTH_COOKIE
),
30 'ax/edit' => $this->make_hook('submit', AUTH_MDP
),
31 'ax/edit/cancel' => $this->make_hook('cancel', AUTH_MDP
),
32 'ax/edit/valid' => $this->make_hook('valid', AUTH_MDP
),
33 'admin/axletter' => $this->make_hook('admin', AUTH_MDP
, 'admin'),
37 function handler_out(&$page, $hash = null
)
43 return $this->handler_index($page, 'out');
46 $this->load('axletter.inc.php');
47 $page->changeTpl('axletter/unsubscribe.tpl');
48 $page->assign('success', AXLetter
::unsubscribe($hash, true
));
51 function handler_index(&$page, $action = null
)
53 $this->load('axletter.inc.php');
55 $page->changeTpl('axletter/index.tpl');
56 $page->setTitle('Envois de l\'AX');
59 case 'in': AXLetter
::subscribe(); break;
60 case 'out': AXLetter
::unsubscribe(); break;
63 $perm = AXLetter
::hasPerms();
65 $res = XDB
::query("SELECT * FROM axletter_ins");
66 $page->assign('count', $res->numRows());
67 $page->assign('new', AXLetter
::awaiting());
69 $page->assign('axs', AXLetter
::subscriptionState());
70 $page->assign('ax_list', AXLetter
::listSent());
71 $page->assign('ax_rights', $perm);
74 function handler_submit(&$page, $action = null
)
76 $this->load('axletter.inc.php');
77 if (!AXLetter
::hasPerms()) {
81 $page->changeTpl('axletter/edit.tpl');
83 $saved = Post
::i('saved');
86 $short_name = trim(Post
::v('short_name'));
87 $subject = trim(Post
::v('subject'));
88 $title = trim(Post
::v('title'));
89 $body = rtrim(Post
::v('body'));
90 $signature = trim(Post
::v('signature'));
91 $promo_min = Post
::i('promo_min');
92 $promo_max = Post
::i('promo_max');
93 $subset_to = preg_split("/[ ,;\:\n\r]+/", Post
::v('subset_to'), -1, PREG_SPLIT_NO_EMPTY
);
94 $subset = (count($subset_to) > 0);
95 $subset_rm = Post
::b('subset_rm');
96 $echeance = Post
::has('echeance_date') ?
97 preg_replace('/^(\d\d\d\d)(\d\d)(\d\d)$/', '\1-\2-\3', Post
::v('echeance_date')) . ' ' . Post
::v('echeance_time')
98 : Post
::v('echeance');
99 $echeance_date = Post
::v('echeance_date');
100 $echeance_time = Post
::v('echeance_time');
103 $res = XDB
::query("SELECT * FROM axletter WHERE FIND_IN_SET('new', bits)");
104 if ($res->numRows()) {
105 extract($res->fetchOneAssoc(), EXTR_OVERWRITE
);
106 $subset_to = ($subset ?
explode("\n", $subset) : array());
107 $subset = (count($subset_to) > 0);
110 XDB
::execute("INSERT INTO axletter SET id = NULL");
111 $id = XDB
::insertId();
113 if (!$echeance ||
$echeance == '0000-00-00 00:00:00') {
117 } elseif (Post
::has('valid')) {
118 S
::assert_xsrf_token();
120 if (!$subject && $title) {
123 if (!$title && $subject) {
126 if (!$subject ||
!$title ||
!$body) {
127 $page->trigError("L'article doit avoir un sujet et un contenu");
130 if (($promo_min > $promo_max && $promo_max != 0)||
131 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
132 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
134 $page->trigError("L'intervalle de promotions n'est pas valide");
137 if (empty($short_name)) {
138 $page->trigError("L'annonce doit avoir un nom raccourci pour simplifier la navigation dans les archives");
140 } elseif (!preg_match('/^[a-z][-a-z0-9]*[a-z0-9]$/', $short_name)) {
141 $page->trigError("Le nom raccourci n'est pas valide, il doit comporter au moins 2 caractères et n'être composé "
142 . "que de chiffres, lettres et tirets");
144 } elseif ($short_name != Post
::v('old_short_name')) {
145 $res = XDB
::query("SELECT id FROM axletter WHERE short_name = {?}", $short_name);
146 if ($res->numRows() && $res->fetchOneCell() != $id) {
147 $page->trigError("Le nom $short_name est déjà utilisé, merci d'en choisir un autre");
148 $short_name = Post
::v('old_short_name');
149 if (empty($short_name)) {
155 switch (@Post
::v('valid')) {
156 case 'Vérifier les emails':
157 // Same as 'preview', but performs a test of all provided emails
159 require_once 'emails.inc.php';
160 $ids = ids_from_mails($subset_to);
162 foreach ($subset_to as $e) {
163 if (!array_key_exists($e, $ids)) {
164 if ($nb_error == 0) {
165 $page->trigError("Emails inconnus :");
168 $page->trigError($e);
171 if ($nb_error == 0) {
172 if (count($subset_to) == 1) {
173 $page->trigSuccess("L'email soumis a été reconnu avec succès.");
175 $page->trigSuccess("Les " . count($subset_to) . " emails soumis ont été reconnus avec succès.");
178 $page->trigError("Total : $nb_error erreur" . ($nb_error > 1 ?
"s" : "") . " sur " . count($subset_to) . " adresses mail soumises.");
180 $page->trigSuccess("Les adresses soumises correspondent à un total de " . count(array_unique($ids)) . " camarades.");
182 // No break here, since Vérifier is a subcase of Aperçu.
184 $this->load('axletter.inc.php');
185 $al = new AXLetter(array($id, $short_name, $subject, $title, $body, $signature,
186 $promo_min, $promo_max, $subset, $subset_rm, $echeance, 0, 'new'));
187 $al->toHtml($page, S
::user());
191 XDB
::execute('INSERT INTO axletter (id, short_name, subject, title, body, signature,
192 promo_min, promo_max, echeance, subset, subset_rm)
193 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})
194 ON DUPLICATE KEY UPDATE short_name = VALUES(short_name), subject = VALUES(subject), title = VALUES(title),
195 body = VALUES(body), signature = VALUES(signature), promo_min = VALUES(promo_min),
196 promo_max = VALUES(promo_max), echeance = VALUES(echeance), subset = VALUES(subset),
197 subset_rm = VALUES(subset_rm)',
198 $id, $short_name, $subject, $title, $body, $signature, $promo_min, $promo_max, $echeance,
199 $subset ?
implode("\n", $subset_to) : null
, $subset_rm);
202 $mailer = new PlMailer();
203 $mailer->setFrom("support@" . $globals->mail
->domain
);
204 $mailer->setSubject("Un nouveau projet d'email de l'AX vient d'être proposé");
205 $mailer->setTxtBody("Un nouvel email vient d'être rédigé en prévision d'un envoi prochain. Vous pouvez "
206 . "le modifier jusqu'à ce qu'il soit verrouillé pour l'envoi\n\n"
207 . "Le sujet de l'email : $subject\n"
208 . "L'échéance d'envoi est fixée à $echeance.\n"
209 . "L'email pourra néanmoins partir avant cette échéance si un administrateur de "
210 . "Polytechnique.org le valide.\n\n"
211 . "Pour modifier, valider ou annuler l'email :\n"
212 . "https://www.polytechnique.org/ax/edit\n"
214 . "Association Polytechnique.org\n");
215 $users = User
::getBulkUsersWithUIDs(XDB
::fetchColumn('SELECT uid
216 FROM axletter_rights'));
217 foreach ($users as $user) {
218 $mailer->addTo($user);
223 $echeance_date = null
;
224 $echeance_time = null
;
229 $page->assign('id', $id);
230 $page->assign('short_name', $short_name);
231 $page->assign('subject', $subject);
232 $page->assign('title', $title);
233 $page->assign('body', $body);
234 $page->assign('signature', $signature);
235 $page->assign('promo_min', $promo_min);
236 $page->assign('promo_max', $promo_max);
237 $page->assign('subset_to', implode("\n", $subset_to));
238 $page->assign('subset', $subset);
239 $page->assign('subset_rm', $subset_rm);
240 $page->assign('echeance', $echeance);
241 $page->assign('echeance_date', $echeance_date);
242 $page->assign('echeance_time', $echeance_time);
243 $page->assign('saved', $saved);
244 $page->assign('new', $new);
245 $page->assign('is_xorg', S
::admin());
249 for ($i = 0 ; $i < 24 ; $i++
) {
250 $stamp = sprintf('%02d:00:00', $i);
251 if ($stamp == $echeance_time) {
252 $sel = ' selected="selected"';
256 $select .= "<option value=\"$stamp\"$sel>{$i}h</option>\n";
258 $page->assign('echeance_time', $select);
262 function handler_cancel(&$page, $force = null
)
264 $this->load('axletter.inc.php');
265 if (!AXLetter
::hasPerms() ||
!S
::has_xsrf_token()) {
269 $al = AXLetter
::awaiting();
271 $page->kill("Aucune lettre en attente");
274 if (!$al->invalid()) {
275 $page->kill("Une erreur est survenue lors de l'annulation de l'envoi");
279 $page->killSuccess("L'envoi de l'annonce {$al->title()} est annulé.");
282 function handler_valid(&$page, $force = null
)
284 $this->load('axletter.inc.php');
285 if (!AXLetter
::hasPerms() ||
!S
::has_xsrf_token()) {
289 $al = AXLetter
::awaiting();
291 $page->kill("Aucune lettre en attente");
295 $page->kill("Une erreur est survenue lors de la validation de l'envoi");
299 $page->killSuccess("L'envoi de l'annonce aura lieu dans l'heure qui vient.");
302 function handler_show(&$page, $nid = 'last')
304 $this->load('axletter.inc.php');
305 $page->changeTpl('axletter/show.tpl');
308 $nl = new AXLetter($nid);
310 if (Get
::has('text')) {
311 $nl->toText($page, $user);
313 $nl->toHtml($page, $user);
315 if (Post
::has('send')) {
318 } catch (MailNotFound
$e) {
323 function handler_admin(&$page, $action = null
, $uid = null
)
325 $this->load('axletter.inc.php');
326 if (Post
::has('action')) {
327 $action = Post
::v('action');
328 $uid = Post
::v('uid');
331 S
::assert_xsrf_token();
333 $uids = preg_split('/ *[,;\: ] */', $uid);
334 foreach ($uids as $uid) {
337 $res = AXLetter
::grantPerms($uid);
340 $res = AXLetter
::revokePerms($uid);
344 $page->trigError("Personne ne correspond à l'identifiant '$uid'");
349 $page->changeTpl('axletter/admin.tpl');
350 $page->assign('admins', User
::getBulkUsersWithUIDs(XDB
::fetchColumn('SELECT uid
351 FROM axletter_rights')));
353 $importer = new CSVImporter('axletter_ins');
354 $importer->registerFunction('uid', 'email vers Id X.org', array($this, 'idFromMail'));
355 $importer->forceValue('hash', array($this, 'createHash'));
356 $importer->apply($page, "admin/axletter", array('uid', 'email', 'prenom', 'nom', 'promo', 'flag', 'hash'));
359 function idFromMail($line, $key, $relation = null
)
363 if (!isset($field)) {
364 $field = array('email', 'mail', 'login', 'bestalias', 'forlife', 'flag');
365 foreach ($field as $fld) {
366 if (isset($line[$fld])) {
372 $uf = new UserFilter(new UFC_Email($line[$field]));
373 $id = $uf->getUIDs();
374 return count($id) == 1 ?
$id[0] : 0;
377 function createHash($line, $key, $relation)
379 $hash = implode(time(), $line) . rand();
385 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: