Merge branch 'platal-0.10.0'
[platal.git] / modules / axletter.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2009 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 $this->load('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 $this->load('axletter.inc.php');
54
55 $page->changeTpl('axletter/index.tpl');
56 $page->setTitle('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 $this->load('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 $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');
98
99 if (!$id) {
100 $res = XDB::query("SELECT * FROM axletter WHERE FIND_IN_SET('new', bits)");
101 if ($res->numRows()) {
102 extract($res->fetchOneAssoc(), EXTR_OVERWRITE);
103 $saved = true;
104 } else {
105 XDB::execute("INSERT INTO axletter SET id = NULL");
106 $id = XDB::insertId();
107 }
108 if (!$echeance || $echeance == '0000-00-00 00:00:00') {
109 $saved = false;
110 $new = true;
111 }
112 } elseif (Post::has('valid')) {
113 S::assert_xsrf_token();
114
115 if (!$subject && $title) {
116 $subject = $title;
117 }
118 if (!$title && $subject) {
119 $title = $subject;
120 }
121 if (!$subject || !$title || !$body) {
122 $page->trigError("L'article doit avoir un sujet et un contenu");
123 Post::kill('valid');
124 }
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)))
128 {
129 $page->trigError("L'intervalle de promotions n'est pas valide");
130 Post::kill('valid');
131 }
132 if (empty($short_name)) {
133 $page->trigError("L'annonce doit avoir un nom raccourci pour simplifier la navigation dans les archives");
134 Post::kill('valid');
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");
138 Post::kill('valid');
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)) {
145 Post::kill('valid');
146 }
147 }
148 }
149
150 switch (@Post::v('valid')) {
151 case 'Aperçu':
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'));
156 break;
157
158 case 'Confirmer':
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);
163 if (!$saved) {
164 global $globals;
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"
176 . "-- \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}>");
186 }
187 $mailer->send();
188 }
189 $saved = true;
190 $echeance_date = null;
191 $echeance_time = null;
192 pl_redirect('ax');
193 break;
194 }
195 }
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());
210
211 if (!$saved) {
212 $select = '';
213 for ($i = 0 ; $i < 24 ; $i++) {
214 $stamp = sprintf('%02d:00:00', $i);
215 if ($stamp == $echeance_time) {
216 $sel = ' selected="selected"';
217 } else {
218 $sel = '';
219 }
220 $select .= "<option value=\"$stamp\"$sel>{$i}h</option>\n";
221 }
222 $page->assign('echeance_time', $select);
223 }
224 }
225
226 function handler_cancel(&$page, $force = null)
227 {
228 $this->load('axletter.inc.php');
229 if (!AXLetter::hasPerms() || !S::has_xsrf_token()) {
230 return PL_FORBIDDEN;
231 }
232
233 $al = AXLetter::awaiting();
234 if (!$al) {
235 $page->kill("Aucune lettre en attente");
236 return;
237 }
238 if (!$al->invalid()) {
239 $page->kill("Une erreur est survenue lors de l'annulation de l'envoi");
240 return;
241 }
242
243 $page->killSuccess("L'envoi de l'annonce {$al->title()} est annulé.");
244 }
245
246 function handler_valid(&$page, $force = null)
247 {
248 $this->load('axletter.inc.php');
249 if (!AXLetter::hasPerms() || !S::has_xsrf_token()) {
250 return PL_FORBIDDEN;
251 }
252
253 $al = AXLetter::awaiting();
254 if (!$al) {
255 $page->kill("Aucune lettre en attente");
256 return;
257 }
258 if (!$al->valid()) {
259 $page->kill("Une erreur est survenue lors de la validation de l'envoi");
260 return;
261 }
262
263 $page->killSuccess("L'envoi de l'annonce aura lieu dans l'heure qui vient.");
264 }
265
266 function handler_show(&$page, $nid = 'last')
267 {
268 $this->load('axletter.inc.php');
269 $page->changeTpl('axletter/show.tpl');
270
271 try {
272 $nl = new AXLetter($nid);
273 if (Get::has('text')) {
274 $nl->toText($page, S::v('prenom'), S::v('nom'), S::v('femme'));
275 } else {
276 $nl->toHtml($page, S::v('prenom'), S::v('nom'), S::v('femme'));
277 }
278 if (Post::has('send')) {
279 $nl->sendTo(S::user()->login(), S::user()->bestEmail(),
280 S::v('prenom'), S::v('nom'),
281 S::v('femme'), S::v('mail_fmt') != 'texte');
282 }
283 } catch (MailNotFound $e) {
284 return PL_NOT_FOUND;
285 }
286 }
287
288 function handler_admin(&$page, $action = null, $uid = null)
289 {
290 $this->load('axletter.inc.php');
291 if (Post::has('action')) {
292 $action = Post::v('action');
293 $uid = Post::v('uid');
294 }
295 if ($uid) {
296 S::assert_xsrf_token();
297
298 $uids = preg_split('/ *[,;\: ] */', $uid);
299 foreach ($uids as $uid) {
300 switch ($action) {
301 case 'add':
302 $res = AXLetter::grantPerms($uid);
303 break;
304 case 'del';
305 $res = AXLetter::revokePerms($uid);
306 break;
307 }
308 if (!$res) {
309 $page->trigError("Personne ne correspond à l'identifiant '$uid'");
310 }
311 }
312 }
313
314 $page->changeTpl('axletter/admin.tpl');
315 $res = XDB::iterator("SELECT IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom,
316 u.prenom, u.promo, u.hruid
317 FROM axletter_rights AS ar
318 INNER JOIN auth_user_md5 AS u USING(user_id)");
319 $page->assign('admins', $res);
320
321 $importer = new CSVImporter('axletter_ins');
322 $importer->registerFunction('user_id', 'email vers Id X.org', array($this, 'idFromMail'));
323 $importer->forceValue('hash', array($this, 'createHash'));
324 $importer->apply($page, "admin/axletter", array('user_id', 'email', 'prenom', 'nom', 'promo', 'flag', 'hash'));
325 }
326
327 function idFromMail($line, $key, $relation = null)
328 {
329 static $field;
330 global $globals;
331 if (!isset($field)) {
332 $field = array('email', 'mail', 'login', 'bestalias', 'forlife', 'flag');
333 foreach ($field as $fld) {
334 if (isset($line[$fld])) {
335 $field = $fld;
336 break;
337 }
338 }
339 }
340 $email = $line[$field];
341 if (strpos($email, '@') === false) {
342 $user = $email;
343 $domain = $globals->mail->domain2;
344 } else {
345 list($user, $domain) = explode('@', $email);
346 }
347 if ($domain != $globals->mail->domain && $domain != $globals->mail->domain2
348 && $domain != $globals->mail->alias_dom && $domain != $globals->mail->alias_dom2) {
349 $res = XDB::query("SELECT uid FROM emails WHERE email = {?}", $email);
350 if ($res->numRows() == 1) {
351 return $res->fetchOneCell();
352 }
353 return '0';
354 }
355 list($user) = explode('+', $user);
356 list($user) = explode('_', $user);
357 if ($domain == $globals->mail->alias_dom || $domain == $globals->mail->alias_dom2) {
358 $res = XDB::query("SELECT a.id
359 FROM virtual AS v
360 INNER JOIN virtual_redirect AS r USING(vid)
361 INNER JOIN aliases AS a ON (a.type = 'a_vie'
362 AND r.redirect = CONCAT(a.alias, '@{$globals->mail->domain2}'))
363 WHERE v.alias = CONCAT({?}, '@{$globals->mail->alias_dom}')", $user);
364 $id = $res->fetchOneCell();
365 return $id ? $id : '0';
366 }
367 $res = XDB::query("SELECT id FROM aliases WHERE alias = {?}", $user);
368 $id = $res->fetchOneCell();
369 return $id ? $id : '0';
370 }
371
372 function createHash($line, $key, $relation)
373 {
374 $hash = implode(time(), $line) . rand();
375 $hash = md5($hash);
376 return $hash;
377 }
378 }
379
380 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
381 ?>