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