Adds XSRF protection to the AXLetter module.
[platal.git] / modules / admin.php
CommitLineData
86f0a6af 1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 Polytechnique.org *
86f0a6af 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 AdminModule extends PLModule
23{
24 function handlers()
25 {
26 return array(
5f9a40b4 27 'phpinfo' => $this->make_hook('phpinfo', AUTH_MDP, 'admin'),
28 'admin' => $this->make_hook('default', AUTH_MDP, 'admin'),
86f0a6af 29 'admin/ax-xorg' => $this->make_hook('ax_xorg', AUTH_MDP, 'admin'),
1c48c2a9 30 'admin/dead-but-active' => $this->make_hook('dead_but_active', AUTH_MDP, 'admin'),
86f0a6af 31 'admin/deaths' => $this->make_hook('deaths', AUTH_MDP, 'admin'),
e7ae7df9 32 'admin/downtime' => $this->make_hook('downtime', AUTH_MDP, 'admin'),
e7ae7df9 33 'admin/homonyms' => $this->make_hook('homonyms', AUTH_MDP, 'admin'),
e7ae7df9 34 'admin/logger' => $this->make_hook('logger', AUTH_MDP, 'admin'),
35 'admin/logger/actions' => $this->make_hook('logger_actions', AUTH_MDP, 'admin'),
e7ae7df9 36 'admin/postfix/blacklist' => $this->make_hook('postfix_blacklist', AUTH_MDP, 'admin'),
37 'admin/postfix/delayed' => $this->make_hook('postfix_delayed', AUTH_MDP, 'admin'),
38 'admin/postfix/regexp_bounces' => $this->make_hook('postfix_regexpsbounces', AUTH_MDP, 'admin'),
39 'admin/postfix/whitelist' => $this->make_hook('postfix_whitelist', AUTH_MDP, 'admin'),
ccdbc270 40 'admin/mx/broken' => $this->make_hook('mx_broken', AUTH_MDP, 'admin'),
e7ae7df9 41 'admin/skins' => $this->make_hook('skins', AUTH_MDP, 'admin'),
42 'admin/synchro_ax' => $this->make_hook('synchro_ax', AUTH_MDP, 'admin'),
e7ae7df9 43 'admin/user' => $this->make_hook('user', AUTH_MDP, 'admin'),
1f53925a 44 'admin/promo' => $this->make_hook('promo', AUTH_MDP, 'admin'),
e7ae7df9 45 'admin/validate' => $this->make_hook('validate', AUTH_MDP, 'admin'),
e18888f4 46 'admin/validate/answers' => $this->make_hook('validate_answers', AUTH_MDP, 'admin'),
3aec1c21 47 'admin/wiki' => $this->make_hook('wiki', AUTH_MDP, 'admin'),
5480a216 48 'admin/ipwatch' => $this->make_hook('ipwatch', AUTH_MDP, 'admin'),
f141945d 49 'admin/icons' => $this->make_hook('icons', AUTH_MDP, 'admin'),
86f0a6af 50 );
51 }
52
5f9a40b4 53 function handler_phpinfo(&$page)
54 {
55 phpinfo();
56 exit;
57 }
58
86f0a6af 59 function handler_default(&$page)
60 {
61 $page->changeTpl('admin/index.tpl');
62 $page->assign('xorg_title','Polytechnique.org - Administration');
63 }
64
65 function handler_postfix_delayed(&$page)
66 {
67 $page->changeTpl('admin/postfix_delayed.tpl');
a7de4ef7 68 $page->assign('xorg_title','Polytechnique.org - Administration - Postfix : Retardés');
86f0a6af 69
70 if (Env::has('del')) {
71 $crc = Env::v('crc');
72 XDB::execute("UPDATE postfix_mailseen SET release = 'del' WHERE crc = {?}", $crc);
a7de4ef7 73 $page->trig($crc." verra tous ses mails supprimés !");
86f0a6af 74 } elseif (Env::has('ok')) {
75 $crc = Env::v('crc');
76 XDB::execute("UPDATE postfix_mailseen SET release = 'ok' WHERE crc = {?}", $crc);
77 $page->trig($crc." a le droit de passer !");
78 }
79
80 $sql = XDB::iterator(
81 "SELECT crc, nb, update_time, create_time,
82 FIND_IN_SET('del', release) AS del,
83 FIND_IN_SET('ok', release) AS ok
84 FROM postfix_mailseen
85 WHERE nb >= 30
86 ORDER BY release != ''");
87
88 $page->assign_by_ref('mails', $sql);
89 }
90
91 function handler_postfix_regexpsbounces(&$page, $new = null) {
92 $page->changeTpl('admin/emails_bounces_re.tpl');
93 $page->assign('xorg_title','Polytechnique.org - Administration - Postfix : Regexps Bounces');
94 $page->assign('new', $new);
95
96 if (Post::has('submit')) {
97 foreach (Env::v('lvl') as $id=>$val) {
98 XDB::query(
99 "REPLACE INTO emails_bounces_re (id,pos,lvl,re,text) VALUES ({?}, {?}, {?}, {?}, {?})",
100 $id, $_POST['pos'][$id], $_POST['lvl'][$id], $_POST['re'][$id], $_POST['text'][$id]
101 );
102 }
103 }
104
105 $page->assign('bre', XDB::iterator("SELECT * FROM emails_bounces_re ORDER BY pos"));
106 }
107
441e9e98 108 // {{{ logger view
86f0a6af 109
441e9e98 110 /** Retrieves the available days for a given year and month.
111 * Obtain a list of days of the given month in the given year
112 * that are within the range of dates that we have log entries for.
113 *
114 * @param integer year
115 * @param integer month
116 * @return array days in that month we have log entries covering.
117 * @private
118 */
119 function _getDays($year, $month)
120 {
121 // give a 'no filter' option
122 $months[0] = "----";
123
124 if ($year && $month) {
125 $day_max = Array(-1, 31, checkdate(2, 29, $year) ? 29 : 28 , 31,
126 30, 31, 30, 31, 31, 30, 31, 30, 31);
127 $res = XDB::query("SELECT YEAR (MAX(start)), YEAR (MIN(start)),
128 MONTH(MAX(start)), MONTH(MIN(start)),
129 DAYOFMONTH(MAX(start)),
130 DAYOFMONTH(MIN(start))
131 FROM logger.sessions");
132 list($ymax, $ymin, $mmax, $mmin, $dmax, $dmin) = $res->fetchOneRow();
133
134 if (($year < $ymin) || ($year == $ymin && $month < $mmin)) {
135 return array();
136 }
137
138 if (($year > $ymax) || ($year == $ymax && $month > $mmax)) {
139 return array();
140 }
141
142 $min = ($year==$ymin && $month==$mmin) ? intval($dmin) : 1;
143 $max = ($year==$ymax && $month==$mmax) ? intval($dmax) : $day_max[$month];
144
145 for($i = $min; $i<=$max; $i++) {
146 $days[$i] = $i;
147 }
148 }
149 return $days;
150 }
151
152
153 /** Retrieves the available months for a given year.
154 * Obtains a list of month numbers that are within the timeframe that
155 * we have log entries for.
156 *
157 * @param integer year
158 * @return array List of month numbers we have log info for.
159 * @private
160 */
161 function _getMonths($year)
162 {
163 // give a 'no filter' option
164 $months[0] = "----";
165
166 if ($year) {
167 $res = XDB::query("SELECT YEAR (MAX(start)), YEAR (MIN(start)),
168 MONTH(MAX(start)), MONTH(MIN(start))
169 FROM logger.sessions");
170 list($ymax, $ymin, $mmax, $mmin) = $res->fetchOneRow();
171
172 if (($year < $ymin) || ($year > $ymax)) {
173 return array();
174 }
175
176 $min = $year == $ymin ? intval($mmin) : 1;
177 $max = $year == $ymax ? intval($mmax) : 12;
178
179 for($i = $min; $i<=$max; $i++) {
180 $months[$i] = $i;
181 }
182 }
183 return $months;
184 }
185
186
187 /** Retrieves the available years.
188 * Obtains a list of years that we have log entries covering.
189 *
190 * @return array years we have log entries for.
191 * @private
192 */
193 function _getYears()
194 {
195 // give a 'no filter' option
196 $years[0] = "----";
197
198 // retrieve available years
199 $res = XDB::query("select YEAR(MAX(start)), YEAR(MIN(start)) FROM logger.sessions");
200 list($max, $min) = $res->fetchOneRow();
201
202 for($i = intval($min); $i<=$max; $i++) {
203 $years[$i] = $i;
204 }
205 return $years;
206 }
207
208
209 /** Make a where clause to get a user's sessions.
210 * Prepare the where clause request that will retrieve the sessions.
211 *
212 * @param $year INTEGER Only get log entries made during the given year.
213 * @param $month INTEGER Only get log entries made during the given month.
214 * @param $day INTEGER Only get log entries made during the given day.
215 * @param $uid INTEGER Only get log entries referring to the given user ID.
216 *
217 * @return STRING the WHERE clause of a query, including the 'WHERE' keyword
218 * @private
219 */
220 function _makeWhere($year, $month, $day, $uid)
221 {
222 // start constructing the "where" clause
223 $where = array();
224
225 if ($uid)
226 array_push($where, "uid='$uid'");
227
228 // we were given at least a year
229 if ($year) {
230 if ($day) {
231 $dmin = mktime(0, 0, 0, $month, $day, $year);
232 $dmax = mktime(0, 0, 0, $month, $day+1, $year);
233 } elseif ($month) {
234 $dmin = mktime(0, 0, 0, $month, 1, $year);
235 $dmax = mktime(0, 0, 0, $month+1, 1, $year);
236 } else {
237 $dmin = mktime(0, 0, 0, 1, 1, $year);
238 $dmax = mktime(0, 0, 0, 1, 1, $year+1);
239 }
240 $where[] = "start >= " . date("Ymd000000", $dmin);
241 $where[] = "start < " . date("Ymd000000", $dmax);
242 }
243
244 if (!empty($where)) {
245 return ' WHERE ' . implode($where, " AND ");
246 } else {
247 return '';
248 }
249 // WE know it's totally reversed, so better use array_reverse than a SORT BY start DESC
250 }
251
252 // }}}
253
254 function handler_logger(&$page, $action = null, $arg = null) {
255 if ($action == 'session') {
256
257 // we are viewing a session
258 $res = XDB::query("SELECT ls.*, a.alias AS username, sa.alias AS suer
259 FROM logger.sessions AS ls
260 LEFT JOIN aliases AS a ON (a.id = ls.uid AND a.type='a_vie')
261 LEFT JOIN aliases AS sa ON (sa.id = ls.suid AND sa.type='a_vie')
262 WHERE ls.id = {?}", $arg);
263
264 $page->assign('session', $a = $res->fetchOneAssoc());
265
504ccd57 266 $res = XDB::iterator('SELECT a.text, e.data, e.stamp
441e9e98 267 FROM logger.events AS e
268 LEFT JOIN logger.actions AS a ON e.action=a.id
269 WHERE e.session={?}', $arg);
270 while ($myarr = $res->next()) {
271 $page->append('events', $myarr);
272 }
273
274 } else {
275 $loguser = $action == 'user' ? $arg : Env::v('loguser');
276
277 $res = XDB::query('SELECT id FROM aliases WHERE alias={?}',
278 $loguser);
279 $loguid = $res->fetchOneCell();
280
281 if ($loguid) {
282 $year = Env::i('year');
283 $month = Env::i('month');
284 $day = Env::i('day');
285 } else {
286 $year = Env::i('year', intval(date('Y')));
287 $month = Env::i('month', intval(date('m')));
288 $day = Env::i('day', intval(date('d')));
289 }
290
291 if (!$year)
292 $month = 0;
293 if (!$month)
294 $day = 0;
295
296 // smarty assignments
297 // retrieve available years
298 $page->assign('years', $this->_getYears());
299 $page->assign('year', $year);
300
301 // retrieve available months for the current year
302 $page->assign('months', $this->_getMonths($year));
303 $page->assign('month', $month);
304
305 // retrieve available days for the current year and month
306 $page->assign('days', $this->_getDays($year, $month));
307 $page->assign('day', $day);
308
309 $page->assign('loguser', $loguser);
310 // smarty assignments
311
312 if ($loguid || $year) {
313
314 // get the requested sessions
315 $where = $this->_makeWhere($year, $month, $day, $loguid);
504ccd57 316 $select = "SELECT s.id, s.start, s.uid,
441e9e98 317 a.alias as username
318 FROM logger.sessions AS s
319 LEFT JOIN aliases AS a ON (a.id = s.uid AND a.type='a_vie')
320 $where
321 ORDER BY start DESC";
322 $res = XDB::iterator($select);
323
324 $sessions = array();
325 while ($mysess = $res->next()) {
326 $mysess['events'] = array();
327 $sessions[$mysess['id']] = $mysess;
328 }
329 array_reverse($sessions);
330
331 // attach events
332 $sql = "SELECT s.id, a.text
333 FROM logger.sessions AS s
334 LEFT JOIN logger.events AS e ON(e.session=s.id)
335 INNER JOIN logger.actions AS a ON(a.id=e.action)
336 $where";
337
338 $res = XDB::iterator($sql);
339 while ($event = $res->next()) {
340 array_push($sessions[$event['id']]['events'], $event['text']);
341 }
342 $page->assign_by_ref('sessions', $sessions);
343 } else {
a7de4ef7 344 $page->assign('msg_nofilters', "Sélectionner une annuée et/ou un utilisateur");
441e9e98 345 }
86f0a6af 346 }
347
8b1f8e12 348 $page->changeTpl('admin/logger-view.tpl');
86f0a6af 349
c4271d38 350 $page->assign('xorg_title','Polytechnique.org - Administration - Logs des sessions');
86f0a6af 351 }
352
f97f90f0 353 function handler_user(&$page, $login = false)
354 {
84270653 355 global $globals;
86f0a6af 356 $page->changeTpl('admin/utilisateurs.tpl');
357 $page->assign('xorg_title','Polytechnique.org - Administration - Edit/Su/Log');
358 require_once("emails.inc.php");
359 require_once("user.func.inc.php");
360
361 if (S::has('suid')) {
7cbf5d4b 362 $page->kill("Déjà en SUID !!!");
86f0a6af 363 }
364
365 if (Env::has('user_id')) {
5c5554b7 366 $login = get_user_forlife(Env::i('user_id'));
f97f90f0 367 if (empty($login)) {
368 $login = Env::i('user_id');
369 }
86f0a6af 370 } elseif (Env::has('login')) {
5c5554b7 371 $login = get_user_forlife(Env::v('login'));
86f0a6af 372 }
373
374 if(Env::has('logs_button') && $login) {
24b5c84d 375 pl_redirect("admin/logger?loguser=$login&year=".date('Y')."&month=".date('m'));
86f0a6af 376 }
377
378 if (Env::has('ax_button') && $login) {
379 pl_redirect("admin/synchro_ax/$login");
380 }
381
382 if(Env::has('suid_button') && $login) {
383 $_SESSION['log']->log("suid_start", "login by ".S::v('forlife'));
384 $_SESSION['suid'] = $_SESSION;
385 $r = XDB::query("SELECT id FROM aliases WHERE alias={?}", $login);
386 if($uid = $r->fetchOneCell()) {
b0964579 387 start_connexion($uid, true);
86f0a6af 388 pl_redirect("");
389 }
390 }
391
392 if ($login) {
f97f90f0 393 if (is_numeric($login)) {
0be07aa6 394 $r = XDB::query("SELECT *, a.alias AS forlife,
395 FIND_IN_SET('watch', u.flags) AS watch, FIND_IN_SET('femme', u.flags) AS sexe,
1517a52d 396 (year(naissance) > promo - 15 or year(naissance) < promo - 25) AS naiss_err
f97f90f0 397 FROM auth_user_md5 AS u
398 LEFT JOIN aliases AS a ON (a.id = u.user_id AND type= 'a_vie')
399 WHERE u.user_id = {?}", $login);
400 } else {
eaf30d86 401 $r = XDB::query("SELECT *, a.alias AS forlife,
0be07aa6 402 FIND_IN_SET('watch', u.flags) AS watch, FIND_IN_SET('femme', u.flags) AS sexe,
1517a52d 403 (year(naissance) > promo - 15 or year(naissance) < promo - 25) AS naiss_err
f97f90f0 404 FROM auth_user_md5 AS u
405 INNER JOIN aliases AS a ON ( a.id = u.user_id AND a.alias={?} AND type!='homonyme' )", $login);
eaf30d86 406 }
86f0a6af 407 $mr = $r->fetchOneAssoc();
408
ebd9cb89
VZ
409 // Checks the user has a forlife, as non-registered user can't have redirections.
410 if ($mr['forlife']) {
f97f90f0 411 $redirect = new Redirect($mr['user_id']);
412 }
86f0a6af 413
414 // Check if there was a submission
415 foreach($_POST as $key => $val) {
03849c3e 416 if (!S::has_xsrf_token()) {
e61326ed
VZ
417 $page->kill("L'opération de modification de l'utilisateur a échouée, merci de réessayer.");
418 }
86f0a6af 419 switch ($key) {
420 case "add_fwd":
421 $email = trim(Env::v('email'));
422 if (!isvalid_email_redirection($email)) {
423 $page->trig("invalid email $email");
424 } else {
425 $redirect->add_email($email);
a7de4ef7 426 $page->trig("Ajout de $email effectué");
86f0a6af 427 }
428 break;
429
430 case "del_fwd":
431 if (!empty($val)) {
432 $redirect->delete_email($val);
433 }
434 break;
435
436 case "del_alias":
437 if (!empty($val)) {
f97f90f0 438 XDB::execute("DELETE FROM aliases
439 WHERE id={?} AND alias={?}
440 AND type!='a_vie' AND type!='homonyme'", $mr['user_id'], $val);
d91e720f 441 XDB::execute("UPDATE emails
442 SET rewrite = ''
443 WHERE uid = {?} AND rewrite LIKE CONCAT({?}, '@%')",
eaf30d86 444 $mr['user_id'], $val);
86f0a6af 445 fix_bestalias($mr['user_id']);
a7de4ef7 446 $page->trig($val." a été supprimé");
86f0a6af 447 }
448 break;
441c2451 449 case "activate_fwd":
450 if (!empty($val)) {
451 $redirect->modify_one_email($val, true);
452 }
453 break;
454 case "deactivate_fwd":
455 if (!empty($val)) {
456 $redirect->modify_one_email($val, false);
457 }
458 break;
459 case "disable_fwd":
460 $redirect->disable();
461 break;
462 case "enable_fwd":
463 $redirect->enable();
464 break;
465 case "clean_fwd":
466 if (!empty($val)) {
11ed26e5 467 $redirect->clean_errors($val);
441c2451 468 }
469 break;
86f0a6af 470 case "add_alias":
bd5e1f3d 471 global $globals;
472 $alias = trim(Env::v('email'));
473 if (strpos($alias, '@') !== false) {
474 list($alias, $domain) = explode('@', $alias);
475 } else {
476 $domain = $globals->mail->domain;
477 }
478 if (!preg_match('/[-a-z0-9\.]+/s', $alias)) {
479 $page->trig("'$alias' n'est pas un alias valide");
480 }
481 if ($domain == $globals->mail->alias_dom || $domain == $globals->mail->alias_dom2) {
482 $req = new AliasReq($mr['user_id'], $alias, 'Admin request', false);
483 if ($req->commit()) {
484 $page->trig("Nouvel alias '$alias@$domain' attribué");
485 } else {
486 $page->trig("Impossible d'ajouter l'alias '$alias@$domain', il est probablement déjà attribué");
487 }
488 } elseif ($domain == $globals->mail->domain || $domain == $globals->mail->domain2) {
489 if (XDB::execute("INSERT INTO aliases (id,alias,type) VALUES ({?}, {?}, 'alias')",
490 $mr['user_id'], $alias)) {
491 $page->trig("Nouvel alias '$alias' ajouté");
492 } else {
493 $page->trig("Impossible d'ajouter l'alias '$alias', il est probablement déjà attribué");
494 }
495 } else {
496 $page->trig("Le domaine '$domain' n'est pas valide");
497 }
86f0a6af 498 break;
499
500 case "best":
501 // 'bestalias' is the first bit of the set : 1
502 // 255 is the max for flags (8 sets max)
503 XDB::execute("UPDATE aliases SET flags= flags & (255 - 1) WHERE id={?}", $mr['user_id']);
504 XDB::execute("UPDATE aliases
505 SET flags= flags | 1
506 WHERE id={?} AND alias={?}", $mr['user_id'], $val);
507 break;
508
509
510 // Editer un profil
511 case "u_edit":
441c2451 512 require_once('secure_hash.inc.php');
513 $pass_encrypted = Env::v('newpass_clair') != "********" ? hash_encrypt(Env::v('newpass_clair')) : Env::v('passw');
514 $naiss = Env::v('naissanceN');
515 $deces = Env::v('decesN');
516 $perms = Env::v('permsN');
517 $prenm = Env::v('prenomN');
518 $nom = Env::v('nomN');
519 $promo = Env::i('promoN');
520 $sexe = Env::v('sexeN');
521 $comm = trim(Env::v('commentN'));
522 $watch = Env::v('watchN');
523 $flags = '';
524 if ($sexe) {
525 $flags = 'femme';
526 }
527 if ($watch) {
528 if ($flags) {
529 $flags .= ',';
530 }
531 $flags .= 'watch';
0be07aa6 532 }
0be07aa6 533
441c2451 534 if ($watch && !$comm) {
535 $page->trig("Il est nécessaire de mettre un commentaire pour surveiller un compte");
536 break;
537 }
86f0a6af 538
7cbf5d4b 539 $watch = 'SELECT naissance, deces, password, perms,
540 prenom, nom, flags, promo, comment
541 FROM auth_user_md5
542 WHERE user_id = ' . $mr['user_id'];
543 $res = XDB::query($watch);
544 $old_fields = $res->fetchOneAssoc();
441c2451 545 $query = "UPDATE auth_user_md5 SET
546 naissance = '$naiss',
547 deces = '$deces',
548 password = '$pass_encrypted',
549 perms = '$perms',
550 prenom = '".addslashes($prenm)."',
551 nom = '".addslashes($nom)."',
552 flags = '$flags',
553 promo = $promo,
554 comment = '".addslashes($comm)."'
555 WHERE user_id = '{$mr['user_id']}'";
05d5ce15
FB
556 if ($perms == 'disabled' && $old_fields['perms'] != 'disabled') {
557 // A user has been banned ==> ensure his php session has been killed
558 // This solution is ugly and overkill, but, it should be efficient.
559 kill_sessions();
560 }
441c2451 561 if (XDB::execute($query)) {
86f0a6af 562 user_reindex($mr['user_id']);
563
7cbf5d4b 564 $res = XDB::query($watch);
565 $new_fields = $res->fetchOneAssoc();
eaf30d86 566
b71f7275 567 $mailer = new PlMailer("admin/useredit.mail.tpl");
d588654c 568 $mailer->assign("user", S::v('forlife'));
7cbf5d4b 569 $mailer->assign('old', $old_fields);
570 $mailer->assign('new', $new_fields);
86f0a6af 571 $mailer->send();
11ed26e5 572
328d2791
PC
573 // update number of subscribers (perms or deceased may have changed)
574 update_NbIns();
86f0a6af 575
a7de4ef7 576 $page->trig("updaté correctement.");
86f0a6af 577 }
578 if (Env::v('nomusageN') != $mr['nom_usage']) {
a11b5424 579 require_once "xorg.misc.inc.php";
86f0a6af 580 set_new_usage($mr['user_id'], Env::v('nomusageN'), make_username(Env::v('prenomN'), Env::v('nomusageN')));
581 }
c53b5964 582 if (Env::v('decesN') != $mr['deces']) {
3ebe4a0a
FB
583 require_once 'notifs.inc.php';
584 register_watch_op($mr['user_id'], WATCH_DEATH, $mr['deces']);
c53b5964 585 user_clear_all_subs($mr['user_id'], false);
586 }
0be07aa6 587 $r = XDB::query("SELECT *, a.alias AS forlife,
588 FIND_IN_SET('watch', u.flags) AS watch, FIND_IN_SET('femme', u.flags) AS sexe
f97f90f0 589 FROM auth_user_md5 AS u
590 LEFT JOIN aliases AS a ON (a.id = u.user_id AND type= 'a_vie')
591 WHERE u.user_id = {?}", $mr['user_id']);
86f0a6af 592 $mr = $r->fetchOneAssoc();
84270653
VZ
593
594 // If GoogleApps is enabled, the user did choose to use synchronized passwords,
595 // and the password was changed, updates the Google Apps password as well.
596 if ($globals->mailstorage->googleapps_domain && Env::v('newpass_clair') != "********") {
597 require_once 'googleapps.inc.php';
598 $account = new GoogleAppsAccount($mr['user_id'], $mr['forlife']);
f5c4bf30 599 if ($account->active() && $account->sync_password) {
84270653
VZ
600 $account->set_password($pass_encrypted);
601 }
602 }
603
604 // If GoogleApps is enabled, and the user is now disabled, disables the Google Apps account as well.
605 if ($globals->mailstorage->googleapps_domain &&
606 $new_fields['perms'] == 'disabled' &&
607 $new_fields['perms'] != $old_fields['perms']) {
608 require_once 'googleapps.inc.php';
609 $account = new GoogleAppsAccount($mr['user_id'], $mr['forlife']);
610 $account->suspend();
611 }
86f0a6af 612 break;
613
614 // DELETE FROM auth_user_md5
615 case "u_kill":
616 user_clear_all_subs($mr['user_id']);
328d2791
PC
617 // update number of subscribers (perms or deceased may have changed)
618 update_NbIns();
a7de4ef7 619 $page->trig("'{$mr['user_id']}' a été désinscrit !");
b71f7275 620 $mailer = new PlMailer("admin/useredit.mail.tpl");
3d01af44 621 $mailer->assign("admin", S::v('forlife'));
854aef5e 622 $mailer->assign("user", $mr['forlife']);
9565d77a 623 $mailer->assign("deletion", true);
86f0a6af 624 $mailer->send();
625 break;
669cb0e0 626
627 case "b_edit":
628 XDB::execute("DELETE FROM forums.innd WHERE uid = {?}", $mr['user_id']);
629 if (Env::v('write_perm') != "" || Env::v('read_perm') != "" || Env::v('commentaire') != "" ) {
630 XDB::execute("INSERT INTO forums.innd
631 SET ipmin = '0',
632 ipmax = '4294967295',
633 write_perm = {?},
634 read_perm = {?},
635 comment = {?},
636 priority = '200',
637 uid = {?}",
638 Env::v('write_perm'), Env::v('read_perm'), Env::v('comment'), $mr['user_id']);
639 }
640 break;
86f0a6af 641 }
642 }
643
b7d19878 644 $res = XDB::query("SELECT start, host
645 FROM logger.sessions
646 WHERE uid={?} AND suid=0
647 ORDER BY start DESC
648 LIMIT 1", $mr['user_id']);
86f0a6af 649 list($lastlogin,$host) = $res->fetchOneRow();
650 $page->assign('lastlogin', $lastlogin);
651 $page->assign('host', $host);
652
441c2451 653 $res = XDB::iterator("SELECT alias
654 FROM virtual
655 INNER JOIN virtual_redirect USING(vid)
bd5e1f3d 656 WHERE type = 'user' AND redirect LIKE '" . $mr['forlife'] . "@%'");
441c2451 657 $page->assign('virtuals', $res);
5c5554b7 658
86f0a6af 659 $page->assign('aliases', XDB::iterator(
660 "SELECT alias, type='a_vie' AS for_life,FIND_IN_SET('bestalias',flags) AS best,expire
661 FROM aliases
662 WHERE id = {?} AND type!='homonyme'
663 ORDER BY type!= 'a_vie'", $mr["user_id"]));
bd5e1f3d 664 if ($mr['perms'] != 'pending' && isset($redirect)) {
665 $page->assign('emails', $redirect->emails);
f97f90f0 666 }
86f0a6af 667
668 $page->assign('mr',$mr);
669cb0e0 669
670 // Bans forums
671 $res = XDB::query("SELECT write_perm, read_perm, comment
672 FROM forums.innd
673 WHERE uid = {?}", $mr['user_id']);
674 $bans = $res->fetchOneAssoc();
675 $page->assign('bans', $bans);
86f0a6af 676 }
677 }
1f53925a 678
679 function getMatricule($line, $key)
680 {
681 $mat = $line['matricule'];
682 $year = intval(substr($mat, 0, 3));
683 $rang = intval(substr($mat, 3, 3));
684 if ($year > 200) { $year /= 10; };
685 if ($year < 96) {
686 return null;
687 } else {
688 return sprintf('%04u%04u', 1900+$year, $rang);
689 }
690 }
691
692 function handler_promo(&$page, $action = null, $promo = null)
693 {
694 if (Env::has('promo')) {
695 if(Env::i('promo') > 1900 && Env::i('promo') < 2050) {
eaf30d86 696 $action = Env::v('valid_promo') == 'Ajouter des membres' ? 'add' : 'ax';
1f53925a 697 pl_redirect('admin/promo/' . $action . '/' . Env::i('promo'));
698 } else {
699 $page->trig('Promo non valide');
700 }
701 }
702
703 $page->changeTpl('admin/promo.tpl');
704 if ($promo > 1900 && $promo < 2050 && ($action == 'add' || $action == 'ax')) {
705 $page->assign('promo', $promo);
706 } else {
707 return;
708 }
709
710 $importer = new CSVImporter('auth_user_md5', 'matricule');
711 $importer->registerFunction('matricule', 'matricle Ecole vers X.org', array($this, 'getMatricule'));
712 switch ($action) {
713 case 'add':
440b63df 714 $fields = array('nom', 'nom_ini', 'prenom', 'naissance_ini',
1f53925a 715 'prenom_ini', 'promo', 'promo_sortie', 'flags',
716 'matricule', 'matricule_ax', 'perms');
717 $importer->forceValue('promo', $promo);
718 $importer->forceValue('promo_sortie', $promo + 3);
719 break;
720 case 'ax':
721 $fields = array('matricule', 'matricule_ax');
722 break;
723 }
724 $importer->apply($page, "admin/promo/$action/$promo", $fields);
725 }
726
86f0a6af 727 function handler_homonyms(&$page, $op = 'list', $target = null) {
728 $page->changeTpl('admin/homonymes.tpl');
729 $page->assign('xorg_title','Polytechnique.org - Administration - Homonymes');
730 require_once("homonymes.inc.php");
74c55fd6 731
86f0a6af 732 if ($target) {
733 if (! list($prenom,$nom,$forlife,$loginbis) = select_if_homonyme($target)) {
734 $target=0;
735 } else {
736 $page->assign('nom',$nom);
737 $page->assign('prenom',$prenom);
738 $page->assign('forlife',$forlife);
739 $page->assign('loginbis',$loginbis);
740 }
741 }
74c55fd6 742
86f0a6af 743 $page->assign('op',$op);
744 $page->assign('target',$target);
74c55fd6 745
86f0a6af 746 // on a un $target valide, on prepare les mails
747 if ($target) {
74c55fd6 748
86f0a6af 749 // on examine l'op a effectuer
750 switch ($op) {
751 case 'mail':
b7d19878 752 send_warning_homonyme($prenom, $nom, $forlife, $loginbis);
753 switch_bestalias($target, $loginbis);
86f0a6af 754 $op = 'list';
755 break;
756 case 'correct':
b7d19878 757 switch_bestalias($target, $loginbis);
86f0a6af 758 XDB::execute("UPDATE aliases SET type='homonyme',expire=NOW() WHERE alias={?}", $loginbis);
759 XDB::execute("REPLACE INTO homonymes (homonyme_id,user_id) VALUES({?},{?})", $target, $target);
b7d19878 760 send_robot_homonyme($prenom, $nom, $forlife, $loginbis);
86f0a6af 761 $op = 'list';
762 break;
763 }
764 }
74c55fd6 765
86f0a6af 766 if ($op == 'list') {
767 $res = XDB::iterator(
768 "SELECT a.alias AS homonyme,s.id AS user_id,s.alias AS forlife,
769 promo,prenom,nom,
770 IF(h.homonyme_id=s.id, a.expire, NULL) AS expire,
771 IF(h.homonyme_id=s.id, a.type, NULL) AS type
772 FROM aliases AS a
773 LEFT JOIN homonymes AS h ON (h.homonyme_id = a.id)
774 INNER JOIN aliases AS s ON (s.id = h.user_id AND s.type='a_vie')
775 INNER JOIN auth_user_md5 AS u ON (s.id=u.user_id)
776 WHERE a.type='homonyme' OR a.expire!=''
777 ORDER BY a.alias,promo");
778 $hnymes = Array();
779 while ($tab = $res->next()) {
780 $hnymes[$tab['homonyme']][] = $tab;
781 }
782 $page->assign_by_ref('hnymes',$hnymes);
783 }
784 }
74c55fd6 785
86f0a6af 786 function handler_ax_xorg(&$page) {
787 $page->changeTpl('admin/ax-xorg.tpl');
788 $page->assign('xorg_title','Polytechnique.org - Administration - AX/X.org');
74c55fd6 789
a7de4ef7 790 // liste des différences
86f0a6af 791 $res = XDB::query(
792 'SELECT u.promo,u.nom AS nom,u.prenom AS prenom,ia.nom AS nomax,ia.prenom AS prenomax,u.matricule AS mat,ia.matricule_ax AS matax
793 FROM auth_user_md5 AS u
794 INNER JOIN identification_ax AS ia ON u.matricule_ax = ia.matricule_ax
795 WHERE (SOUNDEX(u.nom) != SOUNDEX(ia.nom) AND SOUNDEX(CONCAT(ia.particule,u.nom)) != SOUNDEX(ia.nom)
796 AND SOUNDEX(u.nom) != SOUNDEX(ia.nom_patro) AND SOUNDEX(CONCAT(ia.particule,u.nom)) != SOUNDEX(ia.nom_patro))
797 OR u.prenom != ia.prenom OR (u.promo != ia.promo AND u.promo != ia.promo+1 AND u.promo != ia.promo-1)
798 ORDER BY u.promo,u.nom,u.prenom');
799 $page->assign('diffs', $res->fetchAllAssoc());
74c55fd6 800
7cbf5d4b 801 // gens à l'ax mais pas chez nous
86f0a6af 802 $res = XDB::query(
803 'SELECT ia.promo,ia.nom,ia.nom_patro,ia.prenom
804 FROM identification_ax as ia
805 LEFT JOIN auth_user_md5 AS u ON u.matricule_ax = ia.matricule_ax
806 WHERE u.nom IS NULL');
807 $page->assign('mank', $res->fetchAllAssoc());
74c55fd6 808
7cbf5d4b 809 // gens chez nous et pas à l'ax
86f0a6af 810 $res = XDB::query('SELECT promo,nom,prenom FROM auth_user_md5 WHERE matricule_ax IS NULL');
811 $page->assign('plus', $res->fetchAllAssoc());
812 }
74c55fd6 813
86f0a6af 814 function handler_deaths(&$page, $promo = 0, $validate = false) {
815 $page->changeTpl('admin/deces_promo.tpl');
816 $page->assign('xorg_title','Polytechnique.org - Administration - Deces');
74c55fd6 817
86f0a6af 818 if (!$promo)
819 $promo = Env::i('promo');
820 if (Env::has('sub10')) $promo -= 10;
821 if (Env::has('sub01')) $promo -= 1;
822 if (Env::has('add01')) $promo += 1;
823 if (Env::has('add10')) $promo += 10;
74c55fd6 824
86f0a6af 825 $page->assign('promo',$promo);
74c55fd6 826
03849c3e 827 if ($validate && S::has_xsrf_token()) {
86f0a6af 828 $new_deces = array();
829 $res = XDB::iterRow("SELECT user_id,matricule,nom,prenom,deces FROM auth_user_md5 WHERE promo = {?}", $promo);
830 while (list($uid,$mat,$nom,$prenom,$deces) = $res->next()) {
831 $val = Env::v($mat);
e61326ed
VZ
832 if($val == $deces || empty($val)) {
833 continue;
834 }
835
836 XDB::execute('UPDATE auth_user_md5 SET deces={?} WHERE matricule = {?}', $val, $mat);
837 $new_deces[] = array('name' => "$prenom $nom", 'date' => "$val");
838 if($deces == '0000-00-00' || empty($deces)) {
839 require_once('notifs.inc.php');
840 register_watch_op($uid, WATCH_DEATH, $val);
841 require_once('user.func.inc.php');
842 user_clear_all_subs($uid, false); // by default, dead ppl do not loose their email
843 }
86f0a6af 844 }
845 $page->assign('new_deces',$new_deces);
e61326ed
VZ
846 } else if (!$validate) {
847 $page->trig("La mise à jour des dates de decès à échouée, merci de réessayer.");
86f0a6af 848 }
74c55fd6 849
86f0a6af 850 $res = XDB::iterator('SELECT matricule, nom, prenom, deces FROM auth_user_md5 WHERE promo = {?} ORDER BY nom,prenom', $promo);
851 $page->assign('decedes', $res);
852 }
74c55fd6 853
1c48c2a9
VZ
854 function handler_dead_but_active(&$page) {
855 $page->changeTpl('admin/dead_but_active.tpl');
856 $page->assign('xorg_title','Polytechnique.org - Administration - Décédés');
857
858 $res = XDB::iterator(
f0eb8c99 859 "SELECT u.promo, u.nom, u.prenom, u.deces, u.matricule_ax, a.alias, DATE(MAX(s.start)) AS last
1c48c2a9
VZ
860 FROM auth_user_md5 AS u
861 LEFT JOIN aliases AS a ON (a.id = u.user_id AND a.type = 'a_vie')
f0eb8c99 862 LEFT JOIN logger.sessions AS s ON (s.uid = u.user_id AND suid = 0)
1c48c2a9 863 WHERE perms IN ('admin', 'user') AND deces <> 0
f0eb8c99 864 GROUP BY u.user_id
1c48c2a9
VZ
865 ORDER BY u.promo, u.nom");
866 $page->assign('dead', $res);
867 }
868
86f0a6af 869 function handler_synchro_ax(&$page, $user = null, $action = null) {
870 $page->changeTpl('admin/synchro_ax.tpl');
871 $page->assign('xorg_title','Polytechnique.org - Administration - Synchro AX');
74c55fd6 872
86f0a6af 873 require_once('synchro_ax.inc.php');
74c55fd6 874
86f0a6af 875 if (is_ax_key_missing()) {
876 $page->assign('no_private_key', true);
877 $page->run();
878 }
74c55fd6 879
86f0a6af 880 require_once('user.func.inc.php');
881
882 if ($user)
883 $login = get_user_forlife($user);
884
885 if (Env::has('user')) {
886 $login = get_user_forlife(Env::v('user'));
887 if ($login === false) {
888 return;
889 }
890 }
74c55fd6 891
86f0a6af 892 if (Env::has('mat')) {
893 $res = XDB::query(
74c55fd6 894 "SELECT alias
86f0a6af 895 FROM aliases AS a
896 INNER JOIN auth_user_md5 AS u ON (a.id=u.user_id AND a.type='a_vie')
897 WHERE matricule={?}", Env::i('mat'));
898 $login = $res->fetchOneCell();
899 }
74c55fd6 900
86f0a6af 901 if ($login) {
902 if ($action == 'import') {
903 ax_synchronize($login, S::v('uid'));
904 }
905 // get details from user, but looking only info that can be seen by ax
906 $user = get_user_details($login, S::v('uid'), 'ax');
907 $userax= get_user_ax($user['matricule_ax']);
908 require_once 'profil.func.inc.php';
909 $diff = diff_user_details($userax, $user, 'ax');
74c55fd6 910
86f0a6af 911 $page->assign('x', $user);
912 $page->assign('diff', $diff);
913 }
914 }
74c55fd6 915
e654517d 916 function handler_validate(&$page, $action = 'list', $id = null)
917 {
86f0a6af 918 $page->changeTpl('admin/valider.tpl');
919 $page->assign('xorg_title','Polytechnique.org - Administration - Valider une demande');
441c2451 920 $page->addCssLink('nl.css');
3ad44e08 921 $page->addJsLink('ajax.js');
e654517d 922 require_once("validations.inc.php");
923
74c55fd6 924
ed5b9703 925 if ($action == 'edit' and !is_null($id)) {
926 $page->assign('preview_id', $id);
927 }
928
86f0a6af 929 if(Env::has('uid') && Env::has('type') && Env::has('stamp')) {
20d7932b 930 $req = Validate::get_typed_request(Env::v('uid'), Env::v('type'), Env::v('stamp'));
03849c3e 931 if($req && S::has_xsrf_token()) {
e61326ed
VZ
932 $req->handle_formu();
933 } else if ($req) {
934 $page->trig("L'opération a échoué, merci de réessayer.");
935 }
86f0a6af 936 }
74c55fd6 937
cfb96867 938 $r = XDB::iterator('SHOW COLUMNS FROM requests_answers');
939 while (($a = $r->next()) && $a['Field'] != 'category');
940 $page->assign('categories', $categories = explode(',', str_replace("'", '', substr($a['Type'], 5, -1))));
74c55fd6 941
cfb96867 942 $hidden = array();
943 if (Post::has('hide')) {
74c55fd6 944 $hide = array();
945 foreach ($categories as $cat)
cfb96867 946 if (!Post::v($cat)) {
947 $hidden[$cat] = 1;
948 $hide[] = $cat;
949 }
950 setcookie('hide_requests', join(',',$hide), time()+(count($hide)?25920000:(-3600)), '/', '', 0);
951 } elseif (Env::has('hide_requests')) {
952 foreach (explode(',',Env::v('hide_requests')) as $hide_type)
953 $hidden[$hide_type] = true;
954 }
955 $page->assign('hide_requests', $hidden);
74c55fd6 956
90df2530
FB
957 // Update the count of item to validate here... useful in development configuration
958 // where several copies of the site use the same DB, but not the same "dynamic configuration"
959 update_NbValid();
86f0a6af 960 $page->assign('vit', new ValidateIterator());
961 }
e654517d 962
e18888f4 963 function handler_validate_answers(&$page, $action = 'list', $id = null) {
a7de4ef7 964 $page->assign('xorg_title','Polytechnique.org - Administration - Réponses automatiques de validation');
965 $page->assign('title', 'Gestion des réponses automatiques');
e18888f4 966 $table_editor = new PLTableEditor('admin/validate/answers','requests_answers','id');
a7de4ef7 967 $table_editor->describe('category','catégorie',true);
e18888f4 968 $table_editor->describe('title','titre',true);
969 $table_editor->describe('answer','texte',false);
970 $table_editor->apply($page, $action, $id);
971 }
e7ae7df9 972 function handler_skins(&$page, $action = 'list', $id = null) {
e7ae7df9 973 $page->assign('xorg_title','Polytechnique.org - Administration - Skins');
974 $page->assign('title', 'Gestion des skins');
975 $table_editor = new PLTableEditor('admin/skins','skins','id');
976 $table_editor->describe('name','nom',true);
977 $table_editor->describe('skin_tpl','nom du template',true);
978 $table_editor->describe('auteur','auteur',false);
979 $table_editor->describe('comment','commentaire',true);
980 $table_editor->describe('date','date',false);
981 $table_editor->describe('ext','extension du screenshot',false);
982 $table_editor->apply($page, $action, $id);
74c55fd6 983 }
669cb0e0 984
e7ae7df9 985 function handler_postfix_blacklist(&$page, $action = 'list', $id = null) {
e7ae7df9 986 $page->assign('xorg_title','Polytechnique.org - Administration - Postfix : Blacklist');
987 $page->assign('title', 'Blacklist de postfix');
988 $table_editor = new PLTableEditor('admin/postfix/blacklist','postfix_blacklist','email', true);
989 $table_editor->describe('reject_text','Texte de rejet',true);
990 $table_editor->describe('email','email',true);
991 $table_editor->apply($page, $action, $id);
74c55fd6 992 }
e7ae7df9 993 function handler_postfix_whitelist(&$page, $action = 'list', $id = null) {
e7ae7df9 994 $page->assign('xorg_title','Polytechnique.org - Administration - Postfix : Whitelist');
995 $page->assign('title', 'Whitelist de postfix');
996 $table_editor = new PLTableEditor('admin/postfix/whitelist','postfix_whitelist','email', true);
997 $table_editor->describe('email','email',true);
998 $table_editor->apply($page, $action, $id);
74c55fd6 999 }
ccdbc270 1000 function handler_mx_broken(&$page, $action = 'list', $id = null) {
a7de4ef7 1001 $page->assign('xorg_title', 'Polytechnique.org - Administration - MX Défaillants');
1002 $page->assign('title', 'MX Défaillant');
ccdbc270 1003 $table_editor = new PLTableEditor('admin/mx/broken', 'mx_watch', 'host', true);
1004 $table_editor->describe('host', 'Masque', true);
1005 $table_editor->describe('state', 'Niveau', true);
a7de4ef7 1006 $table_editor->describe('text', 'Description du problème', false);
ccdbc270 1007 $table_editor->apply($page, $action, $id);
1008 }
e7ae7df9 1009 function handler_logger_actions(&$page, $action = 'list', $id = null) {
e7ae7df9 1010 $page->assign('xorg_title','Polytechnique.org - Administration - Actions');
1011 $page->assign('title', 'Gestion des actions de logger');
1012 $table_editor = new PLTableEditor('admin/logger/actions','logger.actions','id');
a7de4ef7 1013 $table_editor->describe('text','intitulé',true);
e7ae7df9 1014 $table_editor->describe('description','description',true);
1015 $table_editor->apply($page, $action, $id);
74c55fd6 1016 }
e7ae7df9 1017 function handler_downtime(&$page, $action = 'list', $id = null) {
e7ae7df9 1018 $page->assign('xorg_title','Polytechnique.org - Administration - Coupures');
1019 $page->assign('title', 'Gestion des coupures');
1020 $table_editor = new PLTableEditor('admin/downtime','coupures','id');
1021 $table_editor->describe('debut','date',true);
a7de4ef7 1022 $table_editor->describe('duree','durée',false);
1023 $table_editor->describe('resume','résumé',true);
1024 $table_editor->describe('services','services affectés',true);
e7ae7df9 1025 $table_editor->describe('description','description',false);
1026 $table_editor->apply($page, $action, $id);
74c55fd6 1027 }
bc0903c7 1028
9162f4ed 1029 function handler_wiki(&$page, $action='list', $wikipage='', $wikipage2='')
bc0903c7 1030 {
3aec1c21 1031 require_once 'wiki.inc.php';
74c55fd6 1032
a2b461ce 1033 if (S::v('core_rss_hash')) {
1034 $page->setRssLink('Changement Récents',
1035 '/Site/AllRecentChanges?action=rss&user=' . S::v('forlife') . '&hash=' . S::v('core_rss_hash'));
1036 }
e61326ed 1037
3aec1c21 1038 // update wiki perms
1039 if ($action == 'update') {
1040 $perms_read = Post::v('read');
1041 $perms_edot = Post::v('edit');
1042 if ($perms_read || $perms_edit) {
1043 foreach ($_POST as $wiki_page => $val) if ($val == 'on') {
1044 $wiki_page = str_replace('_', '/', $wiki_page);
1045 if (!$perms_read || !$perms_edit)
74c55fd6 1046 list($perms0, $perms1) = wiki_get_perms($wiki_page);
3aec1c21 1047 if ($perms_read)
1048 $perms0 = $perms_read;
1049 if ($perms_edit)
1050 $perms1 = $perms_edit;
1051 wiki_set_perms($wiki_page, $perms0, $perms1);
1052 }
1053 }
1054 }
eaf30d86 1055
9162f4ed 1056 if ($action == 'delete' && $wikipage != '') {
1057 if (wiki_delete_page($wikipage)) {
1058 $page->trig("La page ".$wikipage." a été supprimée.");
1059 } else {
1060 $page->trig("Impossible de supprimer la page ".$wikipage.".");
1061 }
1062 }
eaf30d86 1063
9162f4ed 1064 if ($action == 'rename' && $wikipage != '' && $wikipage2 != '' && $wikipage != $wikipage2) {
1065 if ($changedLinks = wiki_rename_page($wikipage, $wikipage2)) {
1066 $s = 'La page <em>'.$wikipage.'</em> a été déplacée en <em>'.$wikipage2.'</em>.';
1067 if (is_numeric($changedLinks)) {
1068 $s .= $changedLinks.' lien'.(($changedLinks>1)?'s ont été modifiés.':' a été modifié.');
1069 }
1070 $page->trig($s);
1071 } else {
1072 $page->trig("Impossible de déplacer la page ".$wikipage);
1073 }
1074 }
74c55fd6 1075
3aec1c21 1076 $perms = wiki_perms_options();
74c55fd6 1077
3aec1c21 1078 // list wiki pages and their perms
1079 $wiki_pages = array();
1080 $dir = wiki_work_dir();
1081 if (is_dir($dir)) {
1082 if ($dh = opendir($dir)) {
1083 while (($file = readdir($dh)) !== false) if (substr($file,0,1) >= 'A' && substr($file,0,1) <= 'Z') {
1084 list($read,$edit) = wiki_get_perms($file);
1085 $wiki_pages[$file] = array('read' => $perms[$read], 'edit' => $perms[$edit]);
4694ce89 1086 if (is_file($dir . '/cache_' . wiki_filename($file) . '.tpl')) {
1087 $wiki_pages[$file]['cached'] = true;
1088 }
3aec1c21 1089 }
1090 closedir($dh);
1091 }
1092 }
1093 ksort($wiki_pages);
bc0903c7 1094 $wiki_tree = array();
1095 foreach ($wiki_pages as $file => $desc) {
1096 list($cat, $name) = explode('.', $file);
1097 if (!isset($wiki_tree[$cat])) {
1098 $wiki_tree[$cat] = array();
1099 }
1100 $wiki_tree[$cat][$name] = $desc;
1101 }
1102
3aec1c21 1103 $page->changeTpl('admin/wiki.tpl');
e923a0ce 1104 $page->addJsLink('jquery.js');
bc0903c7 1105 $page->assign('wiki_pages', $wiki_tree);
3aec1c21 1106 $page->assign('perms_opts', $perms);
1107 }
5480a216 1108
1109 function handler_ipwatch(&$page, $action = 'list', $ip = null)
eaf30d86 1110 {
5480a216 1111 $page->changeTpl('admin/ipwatcher.tpl');
eaf30d86 1112
5480a216 1113 $states = array('safe' => 'Ne pas surveiller',
c339246f 1114 'unsafe' => 'Surveiller les inscriptions',
a7de4ef7 1115 'dangerous' => 'Surveiller tous les accès',
5480a216 1116 'ban' => 'Bannir cette adresse');
1117 $page->assign('states', $states);
1118
1119 switch (Post::v('action')) {
e61326ed 1120 case 'create':
5480a216 1121 if (trim(Post::v('ipN')) != '') {
03849c3e 1122 if (!S::has_xsrf_token()) {
e61326ed
VZ
1123 $page->trig("L'ajout d'une IP à surveiller a échoué, merci de réessayer.");
1124 break;
1125 }
61c98f4b
FB
1126 Xdb::execute('INSERT IGNORE INTO ip_watch (ip, mask, state, detection, last, uid, description)
1127 VALUES ({?}, {?}, {?}, CURDATE(), NOW(), {?}, {?})',
1128 ip_to_uint(trim(Post::v('ipN'))), ip_to_uint(trim(Post::v('maskN'))),
1129 Post::v('stateN'), S::i('uid'), Post::v('descriptionN'));
eaf30d86
PH
1130 };
1131 break;
1132
e61326ed 1133 case 'edit':
03849c3e 1134 if (!S::has_xsrf_token()) {
e61326ed
VZ
1135 $page->trig("L'édition de l'IP a échoué, merci de réessayer.");
1136 break;
1137 }
eaf30d86 1138 Xdb::execute('UPDATE ip_watch
61c98f4b 1139 SET state = {?}, last = NOW(), uid = {?}, description = {?}, mask = {?}
9797734d 1140 WHERE ip = {?}', Post::v('stateN'), S::i('uid'), Post::v('descriptionN'),
61c98f4b 1141 ip_to_uint(Post::v('maskN')), ip_to_uint(Post::v('ipN')));
5480a216 1142 break;
eaf30d86 1143
e61326ed 1144 default:
5480a216 1145 if ($action == 'delete' && !is_null($ip)) {
03849c3e 1146 if (S::has_xsrf_token()) {
e61326ed
VZ
1147 Xdb::execute('DELETE FROM ip_watch WHERE ip = {?}', ip_to_uint($ip));
1148 } else {
1149 $page->trig("La suppression de l'adresse IP a échouée, merci de réssayer.");
1150 }
5480a216 1151 }
1152 }
1153 if ($action != 'create' && $action != 'edit') {
1154 $action = 'list';
1155 }
1156 $page->assign('action', $action);
1157
1158 if ($action == 'list') {
9797734d
FB
1159 $sql = "SELECT w.ip, IF(s.ip IS NULL,
1160 IF(w.ip = s2.ip, s2.host, s2.forward_host),
1161 IF(w.ip = s.ip, s.host, s.forward_host)),
61c98f4b 1162 w.mask, w.detection, w.state, a.alias AS forlife
5480a216 1163 FROM ip_watch AS w
9797734d
FB
1164 LEFT JOIN logger.sessions AS s ON (s.ip = w.ip)
1165 LEFT JOIN logger.sessions AS s2 ON (s2.forward_ip = w.ip)
1166 LEFT JOIN aliases AS a ON (a.id = s.uid AND a.type = 'a_vie')
5480a216 1167 GROUP BY w.ip, a.alias
1168 ORDER BY w.state, w.ip, a.alias";
1169 $it = Xdb::iterRow($sql);
1170
1171 $table = array();
1172 $props = array();
61c98f4b 1173 while (list($ip, $host, $mask, $date, $state, $forlife) = $it->next()) {
9797734d 1174 $ip = uint_to_ip($ip);
61c98f4b 1175 $mask = uint_to_ip($mask);
5480a216 1176 if (count($props) == 0 || $props['ip'] != $ip) {
1177 if (count($props) > 0) {
1178 $table[] = $props;
1179 }
1180 $props = array('ip' => $ip,
61c98f4b 1181 'mask' => $mask,
5480a216 1182 'host' => $host,
1183 'detection' => $date,
1184 'state' => $state,
1185 'users' => array($forlife));
1186 } else {
1187 $props['users'][] = $forlife;
1188 }
1189 }
1190 if (count($props) > 0) {
1191 $table[] = $props;
1192 }
1193 $page->assign('table', $table);
1194 } elseif ($action == 'edit') {
61c98f4b 1195 $sql = "SELECT w.detection, w.state, w.last, w.description, w.mask,
5480a216 1196 a1.alias AS edit, a2.alias AS forlife, s.host
1197 FROM ip_watch AS w
eaf30d86 1198 LEFT JOIN aliases AS a1 ON (a1.id = w.uid AND a1.type = 'a_vie')
5480a216 1199 LEFT JOIN logger.sessions AS s ON (w.ip = s.ip)
1200 LEFT JOIN aliases AS a2 ON (a2.id = s.uid AND a2.type = 'a_vie')
1201 WHERE w.ip = {?}
1202 GROUP BY a2.alias
1203 ORDER BY a2.alias";
9797734d 1204 $it = Xdb::iterRow($sql, ip_to_uint($ip));
5480a216 1205
1206 $props = array();
61c98f4b 1207 while (list($detection, $state, $last, $description, $mask, $edit, $forlife, $host) = $it->next()) {
5480a216 1208 if (count($props) == 0) {
1209 $props = array('ip' => $ip,
61c98f4b 1210 'mask' => uint_to_ip($mask),
5480a216 1211 'host' => $host,
1212 'detection' => $detection,
1213 'state' => $state,
1214 'last' => $last,
1215 'description' => $description,
1216 'edit' => $edit,
1217 'users' => array($forlife));
1218 } else {
1219 $props['users'][] = $forlife;
1220 }
1221 }
1222 $page->assign('ip', $props);
1223 }
1224 }
eaf30d86 1225
04148a2c 1226 function handler_icons(&$page)
f141945d 1227 {
1228 $page->changeTpl('admin/icons.tpl');
1229 $dh = opendir('../htdocs/images/icons');
1230 if (!$dh) {
1231 $page->trig('Dossier des icones introuvables.');
1232 }
1233 $icons = array();
1234 while (($file = readdir($dh)) !== false) {
1235 if (strlen($file) > 4 && substr($file,-4) == '.gif') {
1236 array_push($icons, substr($file, 0, -4));
1237 }
1238 }
1239 sort($icons);
1240 $page->assign('icons', $icons);
04148a2c 1241 }
86f0a6af 1242}
1243
a7de4ef7 1244// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
86f0a6af 1245?>