Convert source code to UTF-8
[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)
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 $page->assign('new', AXLetter::awaiting());
66 }
67 $page->assign('axs', AXLetter::subscriptionState());
68 $page->assign('ax_list', AXLetter::listSent());
69 $page->assign('ax_rights', $perm);
70 }
71
72 function handler_submit(&$page, $action = null)
73 {
74 require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
75 if (!AXLetter::hasPerms()) {
76 return PL_FORBIDDEN;
77 }
78
79 $page->changeTpl('axletter/edit.tpl');
80
81 $saved = Post::i('saved');
82 $new = false;
83 $id = Post::i('id');
84 $shortname = trim(Post::v('shortname'));
85 $subject = trim(Post::v('subject'));
86 $title = trim(Post::v('title'));
87 $body = rtrim(Post::v('body'));
88 $signature = trim(Post::v('signature'));
89 $promo_min = Post::i('promo_min');
90 $promo_max = Post::i('promo_max');
91 $echeance = Post::has('echeance_date') ? Post::v('echeance_date') . ' ' . Post::v('echeance_time')
92 : Post::v('echeance');
93 $echeance_date = Post::v('echeance_date');
94 $echeance_time = Post::v('echeance_time');
95
96 if (!$id) {
97 $res = XDB::query("SELECT * FROM axletter WHERE FIND_IN_SET('new', bits)");
98 if ($res->numRows()) {
99 extract($res->fetchOneAssoc(), EXTR_OVERWRITE);
100 $saved = true;
101 } else {
102 XDB::execute("INSERT INTO axletter SET id = NULL");
103 $id = XDB::insertId();
104 }
105 if (!$echeance || $echeance == '0000-00-00 00:00:00') {
106 $saved = false;
107 $new = true;
108 }
109 } elseif (Post::has('valid')) {
110 if (!$subject && $title) {
111 $subject = $title;
112 }
113 if (!$title && $subject) {
114 $title = $subject;
115 }
116 if (!$subject || !$title || !$body) {
117 $page->trig("L'article doit avoir un sujet et un contenu");
118 Post::kill('valid');
119 }
120 if (($promo_min > $promo_max && $promo_max != 0)||
121 ($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
122 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020)))
123 {
124 $page->trig("L'intervalle de promotions n'est pas valide");
125 Post::kill('valid');
126 }
127 if (empty($shortname)) {
128 $page->trig("L'annonce doit avoir un nom raccourci pour simplifier la navigation dans les archives");
129 Post::kill('valid');
130 } elseif (!preg_match('/^[a-z][-a-z0-9]*[a-z0-9]$/', $shortname)) {
131 $page->trig("Le nom raccourci n'est pas valide, il doit comporter au moins 2 caractères et n'être composé "
132 . "que de chiffres, lettres et tirets");
133 Post::kill('valid');
134 } elseif ($shortname != Post::v('old_shortname')) {
135 $res = XDB::query("SELECT id FROM axletter WHERE shortname = {?}", $shortname);
136 if ($res->numRows() && $res->fetchOneCell() != $id) {
137 $page->trig("Le nom $shortname est déjà utilisé, merci d'en choisir un autre");
138 $shortname = Post::v('old_shortname');
139 if (empty($shortname)) {
140 Post::kill('valid');
141 }
142 }
143 }
144
145 switch (@Post::v('valid')) {
146 case 'Aperçu':
147 require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
148 $al = new AXLetter(array($id, $shortname, $subject, $title, $body, $signature,
149 $promo_min, $promo_max, $echeance, 0, 'new'));
150 $al->toHtml($page, S::v('prenom'), S::v('nom'), S::v('femme'));
151 break;
152
153 case 'Confirmer':
154 XDB::execute("REPLACE INTO axletter
155 SET id = {?}, shortname = {?}, subject = {?}, title = {?}, body = {?},
156 signature = {?}, promo_min = {?}, promo_max = {?}, echeance = {?}",
157 $id, $shortname, $subject, $title, $body, $signature, $promo_min, $promo_max, $echeance);
158 $saved = true;
159 $echeance_date = null;
160 $echeance_time = null;
161 pl_redirect('ax');
162 break;
163 }
164 }
165 $page->assign('id', $id);
166 $page->assign('shortname', $shortname);
167 $page->assign('subject', $subject);
168 $page->assign('title', $title);
169 $page->assign('body', $body);
170 $page->assign('signature', $signature);
171 $page->assign('promo_min', $promo_min);
172 $page->assign('promo_max', $promo_max);
173 $page->assign('echeance', $echeance);
174 $page->assign('echeance_date', $echeance_date);
175 $page->assign('echeance_time', $echeance_time);
176 $page->assign('saved', $saved);
177 $page->assign('new', $new);
178 $page->assign('is_xorg', S::has_perms());
179
180 if (!$saved) {
181 $select = '';
182 $time = time() + 3600 * 24 * 2;
183 for ($i = 0 ; $i < 15 ; $i++) {
184 $time += 3600 * 24;
185 $p_stamp = date('Ymd', $time);
186 $year = date('Y', $time);
187 $month = date('m', $time);
188 $day = date('d', $time);
189
190 if ($p_stamp == $echeance_date) {
191 $sel = ' selected="selected"';
192 } else {
193 $sel = '';
194 }
195 $select .= "<option value=\"$p_stamp\"$sel> $day / $month / $year</option>\n";
196 }
197 $page->assign('echeance_date', $select);
198 $select = '';
199 for ($i = 0 ; $i < 24 ; $i++) {
200 $stamp = sprintf('%02d:00:00', $i);
201 if ($stamp == $echeance_time) {
202 $sel = ' selected="selected"';
203 } else {
204 $sel = '';
205 }
206 $select .= "<option value=\"$stamp\"$sel>{$i}h</option>\n";
207 }
208 $page->assign('echeance_time', $select);
209 }
210 }
211
212 function handler_cancel(&$page, $force = null)
213 {
214 require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
215 if (!AXLetter::hasPerms()) {
216 return PL_FORBIDDEN;
217 }
218
219 $url = parse_url($_SERVER['HTTP_REFERER']);
220 if ($force != 'force' && trim($url['path'], '/') != 'ax/edit') {
221 return PL_FORBIDDEN;
222 }
223
224 $al = AXLetter::awaiting();
225 if (!$alg) {
226 $page->kill("Aucune lettre en attente");
227 return;
228 }
229 if (!$al->invalid()) {
230 $page->kill("Une erreur est survenue lors de l'annulation de l'envoi");
231 return;
232 }
233
234 $page->kill("L'envoi de l'annonce {$al->title()} est annulé");
235 }
236
237 function handler_valid(&$page, $force = null)
238 {
239 require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
240 if (!AXLetter::hasPerms()) {
241 return PL_FORBIDDEN;
242 }
243
244 $url = parse_url($_SERVER['HTTP_REFERER']);
245 if ($force != 'force' && trim($url['path'], '/') != 'ax/edit') {
246 return PL_FORBIDDEN;
247 }
248
249 $al = AXLetter::awaiting();
250 if (!$al) {
251 $page->kill("Aucune lettre en attente");
252 return;
253 }
254 if (!$al->valid()) {
255 $page->kill("Une erreur est survenue lors de la validation de l'envoi");
256 return;
257 }
258
259 $page->kill("L'envoi de l'annonce aura lieu dans l'heure qui vient.");
260 }
261
262 function handler_show(&$page, $nid = 'last')
263 {
264 require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
265 $page->changeTpl('axletter/show.tpl');
266
267 $nl = new AXLetter($nid);
268 if (Get::has('text')) {
269 $nl->toText($page, S::v('prenom'), S::v('nom'), S::v('femme'));
270 } else {
271 $nl->toHtml($page, S::v('prenom'), S::v('nom'), S::v('femme'));
272 }
273 if (Post::has('send')) {
274 $nl->sendTo(S::v('prenom'), S::v('nom'),
275 S::v('bestalias'), S::v('femme'),
276 S::v('mail_fmt') != 'texte');
277 }
278 }
279
280 function handler_admin(&$page, $action = null, $uid = null)
281 {
282 require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
283 if (Post::has('action')) {
284 $action = Post::v('action');
285 $uid = Post::v('uid');
286 }
287 if ($uid) {
288 $uids = preg_split('/ *[,;\: ] */', $uid);
289 foreach ($uids as $uid) {
290 switch ($action) {
291 case 'add':
292 $res = AXLetter::grantPerms($uid);
293 break;
294 case 'del';
295 $res = AXLetter::revokePerms($uid);
296 break;
297 }
298 if (!$res) {
299 $page->trig("Personne ne oorrespond à l'identifiant '$uid'");
300 }
301 }
302 }
303
304 $page->changeTpl('axletter/admin.tpl');
305 $res = XDB::iterator("SELECT IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom,
306 u.prenom, u.promo, a.alias AS forlife
307 FROM axletter_rights AS ar
308 INNER JOIN auth_user_md5 AS u USING(user_id)
309 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type = 'a_vie')");
310 $page->assign('admins', $res);
311
312 $importer = new CSVImporter('axletter_ins');
313 $importer->registerFunction('user_id', 'email vers Id X.org', array($this, 'idFromMail'));
314 $importer->forceValue('hash', array($this, 'createHash'));
315 $importer->apply($page, "admin/axletter", array('user_id', 'email', 'prenom', 'nom', 'promo', 'hash'));
316 }
317
318 function idFromMail($line, $key)
319 {
320 static $field;
321 global $globals;
322 if (!isset($field)) {
323 $field = array('email', 'mail', 'login', 'bestalias', 'forlife', 'flag');
324 foreach ($field as $fld) {
325 if (isset($line[$fld])) {
326 $field = $fld;
327 break;
328 }
329 }
330 }
331 $email = $line[$field];
332 if (strpos($email, '@') === false) {
333 $user = $email;
334 $domain = $globals->mail->domain2;
335 } else {
336 list($user, $domain) = explode('@', $email);
337 }
338 if ($domain != $globals->mail->domain && $domain != $globals->mail->domain2
339 && $domain != $globals->mail->alias_dom && $domain != $globals->mail->alias_dom2) {
340 $res = XDB::query("SELECT uid FROM emails WHERE email = {?}", $email);
341 if ($res->numRows() == 1) {
342 return $res->fetchOneCell();
343 }
344 return '0';
345 }
346 list($user) = explode('+', $user);
347 list($user) = explode('_', $user);
348 if ($domain == $globals->mail->alias_dom || $domain == $globals->mail->alias_dom2) {
349 $res = XDB::query("SELECT a.id
350 FROM virtual AS v
351 INNER JOIN virtual_redirect AS r USING(vid)
352 INNER JOIN aliases AS a ON (a.type = 'a_vie'
353 AND r.redirect = CONCAT(a.alias, '@{$globals->mail->domain2}'))
354 WHERE v.alias = CONCAT({?}, '@{$globals->mail->alias_dom}')", $user);
355 $id = $res->fetchOneCell();
356 return $id ? $id : '0';
357 }
358 $res = XDB::query("SELECT id FROM aliases WHERE alias = {?}", $user);
359 $id = $res->fetchOneCell();
360 return $id ? $id : '0';
361 }
362
363 function createHash($line, $key)
364 {
365 $hash = implode(time(), $line) . rand();
366 $hash = md5($hash);
367 return $hash;
368 }
369 }
370
371 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
372 ?>