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