Fetches Groupes X logos directly from the database and not from .net on .org.
[platal.git] / modules / axletter.php
CommitLineData
8da0d3c1 1<?php
2/***************************************************************************
8d84c630 3 * Copyright (C) 2003-2009 Polytechnique.org *
8da0d3c1 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
22class AXLetterModule extends PLModule
23{
24 function handlers()
25 {
26 return array(
eb5a266d 27 'ax' => $this->make_hook('index', AUTH_COOKIE),
a0f05027 28 'ax/out' => $this->make_hook('out', AUTH_PUBLIC),
8da0d3c1 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),
eb5a266d 33 'admin/axletter' => $this->make_hook('admin', AUTH_MDP, 'admin'),
8da0d3c1 34 );
35 }
36
030038cd 37 function handler_out(&$page, $hash = null)
a0f05027 38 {
39 if (!$hash) {
40 if (!S::logged()) {
41 return PL_DO_AUTH;
42 } else {
43 return $this->handler_index($page, 'out');
44 }
45 }
460d8f55 46 $this->load('axletter.inc.php');
a0f05027 47 $page->changeTpl('axletter/unsubscribe.tpl');
48 $page->assign('success', AXLetter::unsubscribe($hash, true));
49 }
50
51 function handler_index(&$page, $action = null)
8da0d3c1 52 {
460d8f55 53 $this->load('axletter.inc.php');
8da0d3c1 54
55 $page->changeTpl('axletter/index.tpl');
46f272fe 56 $page->setTitle('Envois de l\'AX');
8da0d3c1 57
58 switch ($action) {
8da0d3c1 59 case 'in': AXLetter::subscribe(); break;
a0f05027 60 case 'out': AXLetter::unsubscribe(); break;
8da0d3c1 61 }
62
63 $perm = AXLetter::hasPerms();
64 if ($perm) {
9b242f17 65 $res = XDB::query("SELECT * FROM axletter_ins");
66 $page->assign('count', $res->numRows());
a0f05027 67 $page->assign('new', AXLetter::awaiting());
8da0d3c1 68 }
69 $page->assign('axs', AXLetter::subscriptionState());
70 $page->assign('ax_list', AXLetter::listSent());
71 $page->assign('ax_rights', $perm);
72 }
73
74 function handler_submit(&$page, $action = null)
75 {
460d8f55 76 $this->load('axletter.inc.php');
8da0d3c1 77 if (!AXLetter::hasPerms()) {
78 return PL_FORBIDDEN;
79 }
80
81 $page->changeTpl('axletter/edit.tpl');
82
4ea58d4b
VZ
83 $saved = Post::i('saved');
84 $new = false;
85 $id = Post::i('id');
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');
4a08dc73 93 $subset_to = preg_split("/[ ,;\:\n\r]+/", Post::v('subset_to'), -1, PREG_SPLIT_NO_EMPTY);
180627f3 94 $subset = (count($subset_to) > 0);
4ea58d4b
VZ
95 $echeance = Post::has('echeance_date') ?
96 preg_replace('/^(\d\d\d\d)(\d\d)(\d\d)$/', '\1-\2-\3', Post::v('echeance_date')) . ' ' . Post::v('echeance_time')
97 : Post::v('echeance');
8da0d3c1 98 $echeance_date = Post::v('echeance_date');
99 $echeance_time = Post::v('echeance_time');
100
101 if (!$id) {
102 $res = XDB::query("SELECT * FROM axletter WHERE FIND_IN_SET('new', bits)");
103 if ($res->numRows()) {
104 extract($res->fetchOneAssoc(), EXTR_OVERWRITE);
c1134ef2 105 $subset_to = ($subset ? explode("\n", $subset) : null);
180627f3 106 $subset = (count($subset_to) > 0);
8da0d3c1 107 $saved = true;
108 } else {
109 XDB::execute("INSERT INTO axletter SET id = NULL");
110 $id = XDB::insertId();
111 }
112 if (!$echeance || $echeance == '0000-00-00 00:00:00') {
113 $saved = false;
114 $new = true;
115 }
40d428d8
VZ
116 } elseif (Post::has('valid')) {
117 S::assert_xsrf_token();
118
8da0d3c1 119 if (!$subject && $title) {
120 $subject = $title;
121 }
122 if (!$title && $subject) {
123 $title = $subject;
124 }
125 if (!$subject || !$title || !$body) {
a7d35093 126 $page->trigError("L'article doit avoir un sujet et un contenu");
8da0d3c1 127 Post::kill('valid');
128 }
129 if (($promo_min > $promo_max && $promo_max != 0)||
130 ($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
131 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020)))
132 {
a7d35093 133 $page->trigError("L'intervalle de promotions n'est pas valide");
8da0d3c1 134 Post::kill('valid');
135 }
4ea58d4b 136 if (empty($short_name)) {
a7d35093 137 $page->trigError("L'annonce doit avoir un nom raccourci pour simplifier la navigation dans les archives");
8da0d3c1 138 Post::kill('valid');
4ea58d4b 139 } elseif (!preg_match('/^[a-z][-a-z0-9]*[a-z0-9]$/', $short_name)) {
a7d35093 140 $page->trigError("Le nom raccourci n'est pas valide, il doit comporter au moins 2 caractères et n'être composé "
8da0d3c1 141 . "que de chiffres, lettres et tirets");
142 Post::kill('valid');
4ea58d4b
VZ
143 } elseif ($short_name != Post::v('old_short_name')) {
144 $res = XDB::query("SELECT id FROM axletter WHERE short_name = {?}", $short_name);
8da0d3c1 145 if ($res->numRows() && $res->fetchOneCell() != $id) {
a7d35093 146 $page->trigError("Le nom $short_name est déjà utilisé, merci d'en choisir un autre");
4ea58d4b
VZ
147 $short_name = Post::v('old_short_name');
148 if (empty($short_name)) {
8da0d3c1 149 Post::kill('valid');
150 }
151 }
152 }
153
154 switch (@Post::v('valid')) {
a7de4ef7 155 case 'Aperçu':
460d8f55 156 $this->load('axletter.inc.php');
4ea58d4b 157 $al = new AXLetter(array($id, $short_name, $subject, $title, $body, $signature,
2a3f4f57 158 $promo_min, $promo_max, $subset, $echeance, 0, 'new'));
8da0d3c1 159 $al->toHtml($page, S::v('prenom'), S::v('nom'), S::v('femme'));
160 break;
161
162 case 'Confirmer':
163 XDB::execute("REPLACE INTO axletter
c6f2cafe 164 SET id = {?}, short_name = {?}, subject = {?}, title = {?}, body = {?},
2a3f4f57 165 signature = {?}, promo_min = {?}, promo_max = {?}, echeance = {?}, subset = {?}",
c1134ef2 166 $id, $short_name, $subject, $title, $body, $signature, $promo_min, $promo_max, $echeance, $subset ? implode("\n", $subset_to) : null);
9b242f17 167 if (!$saved) {
115c90db 168 global $globals;
9b242f17 169 $mailer = new PlMailer();
1d55fe45 170 $mailer->setFrom("support@" . $globals->mail->domain);
faefdbb7
SJ
171 $mailer->setSubject("Un nouveau projet d'email de l'AX vient d'être proposé");
172 $mailer->setTxtBody("Un nouvel email vient d'être rédigé en prévision d'un envoi prochain. Vous pouvez "
9b242f17 173 . "le modifier jusqu'à ce qu'il soit verrouillé pour l'envoi\n\n"
faefdbb7 174 . "Le sujet de l'email : $subject\n"
9b242f17 175 . "L'échéance d'envoi est fixée à $echeance.\n"
faefdbb7 176 . "L'email pourra néanmoins partir avant cette échéance si un administrateur de "
9b242f17 177 . "Polytechnique.org le valide.\n\n"
faefdbb7 178 . "Pour modifier, valider ou annuler l'email :\n"
9b242f17 179 . "https://www.polytechnique.org/ax/edit\n"
180 . "-- \n"
181 . "Association Polytechnique.org\n");
182 $res = XDB::iterRow("SELECT IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom,
183 u.prenom, a.alias AS bestalias
184 FROM axletter_rights AS ar
185 INNER JOIN auth_user_md5 AS u USING(user_id)
186 INNER JOIN aliases AS a ON (u.user_id = a.id
187 AND FIND_IN_SET('bestalias', a.flags))");
9b242f17 188 while (list($nom, $prenom, $alias) = $res->next()) {
189 $mailer->addTo("$nom $prenom <$alias@{$globals->mail->domain}>");
190 }
191 $mailer->send();
192 }
8da0d3c1 193 $saved = true;
194 $echeance_date = null;
195 $echeance_time = null;
196 pl_redirect('ax');
197 break;
198 }
199 }
200 $page->assign('id', $id);
4ea58d4b 201 $page->assign('short_name', $short_name);
8da0d3c1 202 $page->assign('subject', $subject);
203 $page->assign('title', $title);
204 $page->assign('body', $body);
205 $page->assign('signature', $signature);
206 $page->assign('promo_min', $promo_min);
207 $page->assign('promo_max', $promo_max);
2a3f4f57
RB
208 $page->assign('subset_to', implode("\n", $subset_to));
209 $page->assign('subset', $subset);
8da0d3c1 210 $page->assign('echeance', $echeance);
211 $page->assign('echeance_date', $echeance_date);
212 $page->assign('echeance_time', $echeance_time);
213 $page->assign('saved', $saved);
214 $page->assign('new', $new);
dd70cd28 215 $page->assign('is_xorg', S::admin());
8da0d3c1 216
217 if (!$saved) {
218 $select = '';
8da0d3c1 219 for ($i = 0 ; $i < 24 ; $i++) {
220 $stamp = sprintf('%02d:00:00', $i);
221 if ($stamp == $echeance_time) {
222 $sel = ' selected="selected"';
223 } else {
224 $sel = '';
225 }
226 $select .= "<option value=\"$stamp\"$sel>{$i}h</option>\n";
227 }
228 $page->assign('echeance_time', $select);
eaf30d86 229 }
8da0d3c1 230 }
231
232 function handler_cancel(&$page, $force = null)
233 {
460d8f55 234 $this->load('axletter.inc.php');
fde3e90e 235 if (!AXLetter::hasPerms() || !S::has_xsrf_token()) {
8da0d3c1 236 return PL_FORBIDDEN;
237 }
238
a0f05027 239 $al = AXLetter::awaiting();
fde3e90e 240 if (!$al) {
8da0d3c1 241 $page->kill("Aucune lettre en attente");
242 return;
243 }
8da0d3c1 244 if (!$al->invalid()) {
245 $page->kill("Une erreur est survenue lors de l'annulation de l'envoi");
246 return;
247 }
248
8f794f88 249 $page->killSuccess("L'envoi de l'annonce {$al->title()} est annulé.");
8da0d3c1 250 }
251
252 function handler_valid(&$page, $force = null)
253 {
460d8f55 254 $this->load('axletter.inc.php');
fde3e90e 255 if (!AXLetter::hasPerms() || !S::has_xsrf_token()) {
8da0d3c1 256 return PL_FORBIDDEN;
257 }
258
a0f05027 259 $al = AXLetter::awaiting();
260 if (!$al) {
8da0d3c1 261 $page->kill("Aucune lettre en attente");
262 return;
263 }
8da0d3c1 264 if (!$al->valid()) {
265 $page->kill("Une erreur est survenue lors de la validation de l'envoi");
266 return;
267 }
268
8f794f88 269 $page->killSuccess("L'envoi de l'annonce aura lieu dans l'heure qui vient.");
8da0d3c1 270 }
271
272 function handler_show(&$page, $nid = 'last')
273 {
460d8f55 274 $this->load('axletter.inc.php');
8da0d3c1 275 $page->changeTpl('axletter/show.tpl');
276
4a71cf67
SJ
277 try {
278 $nl = new AXLetter($nid);
279 if (Get::has('text')) {
280 $nl->toText($page, S::v('prenom'), S::v('nom'), S::v('femme'));
281 } else {
282 $nl->toHtml($page, S::v('prenom'), S::v('nom'), S::v('femme'));
283 }
284 if (Post::has('send')) {
285 $nl->sendTo(S::user()->login(), S::user()->bestEmail(),
286 S::v('prenom'), S::v('nom'),
287 S::v('femme'), S::v('mail_fmt') != 'texte');
288 }
289 } catch (MailNotFound $e) {
290 return PL_NOT_FOUND;
8da0d3c1 291 }
292 }
0d75939a 293
294 function handler_admin(&$page, $action = null, $uid = null)
295 {
460d8f55 296 $this->load('axletter.inc.php');
0d75939a 297 if (Post::has('action')) {
298 $action = Post::v('action');
299 $uid = Post::v('uid');
300 }
40d428d8
VZ
301 if ($uid) {
302 S::assert_xsrf_token();
303
0d75939a 304 $uids = preg_split('/ *[,;\: ] */', $uid);
305 foreach ($uids as $uid) {
306 switch ($action) {
307 case 'add':
308 $res = AXLetter::grantPerms($uid);
309 break;
310 case 'del';
311 $res = AXLetter::revokePerms($uid);
312 break;
313 }
314 if (!$res) {
a7d35093 315 $page->trigError("Personne ne correspond à l'identifiant '$uid'");
0d75939a 316 }
317 }
318 }
319
320 $page->changeTpl('axletter/admin.tpl');
321 $res = XDB::iterator("SELECT IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom,
90c614cd 322 u.prenom, u.promo, u.hruid
0d75939a 323 FROM axletter_rights AS ar
90c614cd 324 INNER JOIN auth_user_md5 AS u USING(user_id)");
0d75939a 325 $page->assign('admins', $res);
eaf30d86 326
0d75939a 327 $importer = new CSVImporter('axletter_ins');
328 $importer->registerFunction('user_id', 'email vers Id X.org', array($this, 'idFromMail'));
329 $importer->forceValue('hash', array($this, 'createHash'));
2f95cd8f 330 $importer->apply($page, "admin/axletter", array('user_id', 'email', 'prenom', 'nom', 'promo', 'flag', 'hash'));
0d75939a 331 }
332
f6eacab0 333 function idFromMail($line, $key, $relation = null)
0d75939a 334 {
335 static $field;
cbce77b7 336 global $globals;
0d75939a 337 if (!isset($field)) {
338 $field = array('email', 'mail', 'login', 'bestalias', 'forlife', 'flag');
339 foreach ($field as $fld) {
340 if (isset($line[$fld])) {
341 $field = $fld;
342 break;
343 }
344 }
345 }
346 $email = $line[$field];
347 if (strpos($email, '@') === false) {
348 $user = $email;
cbce77b7 349 $domain = $globals->mail->domain2;
0d75939a 350 } else {
eaf30d86 351 list($user, $domain) = explode('@', $email);
cbce77b7 352 }
353 if ($domain != $globals->mail->domain && $domain != $globals->mail->domain2
0d75939a 354 && $domain != $globals->mail->alias_dom && $domain != $globals->mail->alias_dom2) {
cbce77b7 355 $res = XDB::query("SELECT uid FROM emails WHERE email = {?}", $email);
356 if ($res->numRows() == 1) {
357 return $res->fetchOneCell();
0d75939a 358 }
cbce77b7 359 return '0';
360 }
361 list($user) = explode('+', $user);
362 list($user) = explode('_', $user);
363 if ($domain == $globals->mail->alias_dom || $domain == $globals->mail->alias_dom2) {
364 $res = XDB::query("SELECT a.id
365 FROM virtual AS v
366 INNER JOIN virtual_redirect AS r USING(vid)
367 INNER JOIN aliases AS a ON (a.type = 'a_vie'
368 AND r.redirect = CONCAT(a.alias, '@{$globals->mail->domain2}'))
369 WHERE v.alias = CONCAT({?}, '@{$globals->mail->alias_dom}')", $user);
370 $id = $res->fetchOneCell();
371 return $id ? $id : '0';
0d75939a 372 }
373 $res = XDB::query("SELECT id FROM aliases WHERE alias = {?}", $user);
374 $id = $res->fetchOneCell();
375 return $id ? $id : '0';
376 }
377
f6eacab0 378 function createHash($line, $key, $relation)
0d75939a 379 {
5480a216 380 $hash = implode(time(), $line) . rand();
0d75939a 381 $hash = md5($hash);
382 return $hash;
383 }
8da0d3c1 384}
385
a7de4ef7 386// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
8da0d3c1 387?>