2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 $echeance = Post
::has('echeance_date') ?
94 preg_replace('/^(\d\d\d\d)(\d\d)(\d\d)$/', '\1-\2-\3', Post
::v('echeance_date')) . ' ' . Post
::v('echeance_time')
95 : Post
::v('echeance');
96 $echeance_date = Post
::v('echeance_date');
97 $echeance_time = Post
::v('echeance_time');
100 $res = XDB
::query("SELECT * FROM axletter WHERE FIND_IN_SET('new', bits)");
101 if ($res->numRows()) {
102 extract($res->fetchOneAssoc(), EXTR_OVERWRITE
);
105 XDB
::execute("INSERT INTO axletter SET id = NULL");
106 $id = XDB
::insertId();
108 if (!$echeance ||
$echeance == '0000-00-00 00:00:00') {
112 } elseif (Post
::has('valid')) {
113 S
::assert_xsrf_token();
115 if (!$subject && $title) {
118 if (!$title && $subject) {
121 if (!$subject ||
!$title ||
!$body) {
122 $page->trigError("L'article doit avoir un sujet et un contenu");
125 if (($promo_min > $promo_max && $promo_max != 0)||
126 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
127 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
129 $page->trigError("L'intervalle de promotions n'est pas valide");
132 if (empty($short_name)) {
133 $page->trigError("L'annonce doit avoir un nom raccourci pour simplifier la navigation dans les archives");
135 } elseif (!preg_match('/^[a-z][-a-z0-9]*[a-z0-9]$/', $short_name)) {
136 $page->trigError("Le nom raccourci n'est pas valide, il doit comporter au moins 2 caractères et n'être composé "
137 . "que de chiffres, lettres et tirets");
139 } elseif ($short_name != Post
::v('old_short_name')) {
140 $res = XDB
::query("SELECT id FROM axletter WHERE short_name = {?}", $short_name);
141 if ($res->numRows() && $res->fetchOneCell() != $id) {
142 $page->trigError("Le nom $short_name est déjà utilisé, merci d'en choisir un autre");
143 $short_name = Post
::v('old_short_name');
144 if (empty($short_name)) {
150 switch (@Post
::v('valid')) {
152 $this->load('axletter.inc.php');
153 $al = new AXLetter(array($id, $short_name, $subject, $title, $body, $signature,
154 $promo_min, $promo_max, $echeance, 0, 'new'));
155 $al->toHtml($page, S
::v('prenom'), S
::v('nom'), S
::v('femme'));
159 XDB
::execute("REPLACE INTO axletter
160 SET id = {?}, short_name = {?}, subject = {?}, title = {?}, body = {?},
161 signature = {?}, promo_min = {?}, promo_max = {?}, echeance = {?}",
162 $id, $short_name, $subject, $title, $body, $signature, $promo_min, $promo_max, $echeance);
165 $mailer = new PlMailer();
166 $mailer->setFrom("support@" . $globals->mail
->domain
);
167 $mailer->setSubject("Un nouveau projet d'email de l'AX vient d'être proposé");
168 $mailer->setTxtBody("Un nouvel email vient d'être rédigé en prévision d'un envoi prochain. Vous pouvez "
169 . "le modifier jusqu'à ce qu'il soit verrouillé pour l'envoi\n\n"
170 . "Le sujet de l'email : $subject\n"
171 . "L'échéance d'envoi est fixée à $echeance.\n"
172 . "L'email pourra néanmoins partir avant cette échéance si un administrateur de "
173 . "Polytechnique.org le valide.\n\n"
174 . "Pour modifier, valider ou annuler l'email :\n"
175 . "https://www.polytechnique.org/ax/edit\n"
177 . "Association Polytechnique.org\n");
178 $res = XDB
::iterRow("SELECT IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom,
179 u.prenom, a.alias AS bestalias
180 FROM axletter_rights AS ar
181 INNER JOIN auth_user_md5 AS u USING(user_id)
182 INNER JOIN aliases AS a ON (u.user_id = a.id
183 AND FIND_IN_SET('bestalias', a.flags))");
184 while (list($nom, $prenom, $alias) = $res->next()) {
185 $mailer->addTo("$nom $prenom <$alias@{$globals->mail->domain}>");
190 $echeance_date = null
;
191 $echeance_time = null
;
196 $page->assign('id', $id);
197 $page->assign('short_name', $short_name);
198 $page->assign('subject', $subject);
199 $page->assign('title', $title);
200 $page->assign('body', $body);
201 $page->assign('signature', $signature);
202 $page->assign('promo_min', $promo_min);
203 $page->assign('promo_max', $promo_max);
204 $page->assign('echeance', $echeance);
205 $page->assign('echeance_date', $echeance_date);
206 $page->assign('echeance_time', $echeance_time);
207 $page->assign('saved', $saved);
208 $page->assign('new', $new);
209 $page->assign('is_xorg', S
::has_perms());
213 for ($i = 0 ; $i < 24 ; $i++
) {
214 $stamp = sprintf('%02d:00:00', $i);
215 if ($stamp == $echeance_time) {
216 $sel = ' selected="selected"';
220 $select .= "<option value=\"$stamp\"$sel>{$i}h</option>\n";
222 $page->assign('echeance_time', $select);
226 function handler_cancel(&$page, $force = null
)
228 $this->load('axletter.inc.php');
229 if (!AXLetter
::hasPerms() ||
!S
::has_xsrf_token()) {
233 $al = AXLetter
::awaiting();
235 $page->kill("Aucune lettre en attente");
238 if (!$al->invalid()) {
239 $page->kill("Une erreur est survenue lors de l'annulation de l'envoi");
243 $page->kill("L'envoi de l'annonce {$al->title()} est annulé");
246 function handler_valid(&$page, $force = null
)
248 $this->load('axletter.inc.php');
249 if (!AXLetter
::hasPerms() ||
!S
::has_xsrf_token()) {
253 $al = AXLetter
::awaiting();
255 $page->kill("Aucune lettre en attente");
259 $page->kill("Une erreur est survenue lors de la validation de l'envoi");
263 $page->kill("L'envoi de l'annonce aura lieu dans l'heure qui vient.");
266 function handler_show(&$page, $nid = 'last')
268 $this->load('axletter.inc.php');
269 $page->changeTpl('axletter/show.tpl');
271 $nl = new AXLetter($nid);
272 if (Get
::has('text')) {
273 $nl->toText($page, S
::v('prenom'), S
::v('nom'), S
::v('femme'));
275 $nl->toHtml($page, S
::v('prenom'), S
::v('nom'), S
::v('femme'));
277 if (Post
::has('send')) {
278 $nl->sendTo(S
::user()->login(), S
::user()->bestEmail(),
279 S
::v('prenom'), S
::v('nom'),
280 S
::v('femme'), S
::v('mail_fmt') != 'texte');
284 function handler_admin(&$page, $action = null
, $uid = null
)
286 $this->load('axletter.inc.php');
287 if (Post
::has('action')) {
288 $action = Post
::v('action');
289 $uid = Post
::v('uid');
292 S
::assert_xsrf_token();
294 $uids = preg_split('/ *[,;\: ] */', $uid);
295 foreach ($uids as $uid) {
298 $res = AXLetter
::grantPerms($uid);
301 $res = AXLetter
::revokePerms($uid);
305 $page->trigError("Personne ne correspond à l'identifiant '$uid'");
310 $page->changeTpl('axletter/admin.tpl');
311 $res = XDB
::iterator("SELECT IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom,
312 u.prenom, u.promo, u.hruid
313 FROM axletter_rights AS ar
314 INNER JOIN auth_user_md5 AS u USING(user_id)");
315 $page->assign('admins', $res);
317 $importer = new CSVImporter('axletter_ins');
318 $importer->registerFunction('user_id', 'email vers Id X.org', array($this, 'idFromMail'));
319 $importer->forceValue('hash', array($this, 'createHash'));
320 $importer->apply($page, "admin/axletter", array('user_id', 'email', 'prenom', 'nom', 'promo', 'flag', 'hash'));
323 function idFromMail($line, $key)
327 if (!isset($field)) {
328 $field = array('email', 'mail', 'login', 'bestalias', 'forlife', 'flag');
329 foreach ($field as $fld) {
330 if (isset($line[$fld])) {
336 $email = $line[$field];
337 if (strpos($email, '@') === false
) {
339 $domain = $globals->mail
->domain2
;
341 list($user, $domain) = explode('@', $email);
343 if ($domain != $globals->mail
->domain
&& $domain != $globals->mail
->domain2
344 && $domain != $globals->mail
->alias_dom
&& $domain != $globals->mail
->alias_dom2
) {
345 $res = XDB
::query("SELECT uid FROM emails WHERE email = {?}", $email);
346 if ($res->numRows() == 1) {
347 return $res->fetchOneCell();
351 list($user) = explode('+', $user);
352 list($user) = explode('_', $user);
353 if ($domain == $globals->mail
->alias_dom ||
$domain == $globals->mail
->alias_dom2
) {
354 $res = XDB
::query("SELECT a.id
356 INNER JOIN virtual_redirect AS r USING(vid)
357 INNER JOIN aliases AS a ON (a.type = 'a_vie'
358 AND r.redirect = CONCAT(a.alias, '@{$globals->mail->domain2}'))
359 WHERE v.alias = CONCAT({?}, '@{$globals->mail->alias_dom}')", $user);
360 $id = $res->fetchOneCell();
361 return $id ?
$id : '0';
363 $res = XDB
::query("SELECT id FROM aliases WHERE alias = {?}", $user);
364 $id = $res->fetchOneCell();
365 return $id ?
$id : '0';
368 function createHash($line, $key)
370 $hash = implode(time(), $line) . rand();
376 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: