New IP watcher :
[platal.git] / modules / admin.php
CommitLineData
86f0a6af 1<?php
2/***************************************************************************
5ddeb07c 3 * Copyright (C) 2003-2007 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'),
30 'admin/deaths' => $this->make_hook('deaths', AUTH_MDP, 'admin'),
e7ae7df9 31 'admin/downtime' => $this->make_hook('downtime', AUTH_MDP, 'admin'),
e7ae7df9 32 'admin/homonyms' => $this->make_hook('homonyms', AUTH_MDP, 'admin'),
e7ae7df9 33 'admin/logger' => $this->make_hook('logger', AUTH_MDP, 'admin'),
34 'admin/logger/actions' => $this->make_hook('logger_actions', AUTH_MDP, 'admin'),
e7ae7df9 35 'admin/postfix/blacklist' => $this->make_hook('postfix_blacklist', AUTH_MDP, 'admin'),
36 'admin/postfix/delayed' => $this->make_hook('postfix_delayed', AUTH_MDP, 'admin'),
37 'admin/postfix/regexp_bounces' => $this->make_hook('postfix_regexpsbounces', AUTH_MDP, 'admin'),
38 'admin/postfix/whitelist' => $this->make_hook('postfix_whitelist', AUTH_MDP, 'admin'),
39 'admin/skins' => $this->make_hook('skins', AUTH_MDP, 'admin'),
40 'admin/synchro_ax' => $this->make_hook('synchro_ax', AUTH_MDP, 'admin'),
e7ae7df9 41 'admin/user' => $this->make_hook('user', AUTH_MDP, 'admin'),
1f53925a 42 'admin/promo' => $this->make_hook('promo', AUTH_MDP, 'admin'),
e7ae7df9 43 'admin/validate' => $this->make_hook('validate', AUTH_MDP, 'admin'),
e18888f4 44 'admin/validate/answers' => $this->make_hook('validate_answers', AUTH_MDP, 'admin'),
3aec1c21 45 'admin/wiki' => $this->make_hook('wiki', AUTH_MDP, 'admin'),
5480a216 46 'admin/ipwatch' => $this->make_hook('ipwatch', AUTH_MDP, 'admin'),
86f0a6af 47 );
48 }
49
5f9a40b4 50 function handler_phpinfo(&$page)
51 {
52 phpinfo();
53 exit;
54 }
55
86f0a6af 56 function handler_default(&$page)
57 {
58 $page->changeTpl('admin/index.tpl');
59 $page->assign('xorg_title','Polytechnique.org - Administration');
60 }
61
62 function handler_postfix_delayed(&$page)
63 {
64 $page->changeTpl('admin/postfix_delayed.tpl');
65 $page->assign('xorg_title','Polytechnique.org - Administration - Postfix : Retardés');
66
67 if (Env::has('del')) {
68 $crc = Env::v('crc');
69 XDB::execute("UPDATE postfix_mailseen SET release = 'del' WHERE crc = {?}", $crc);
70 $page->trig($crc." verra tous ses mails supprimés !");
71 } elseif (Env::has('ok')) {
72 $crc = Env::v('crc');
73 XDB::execute("UPDATE postfix_mailseen SET release = 'ok' WHERE crc = {?}", $crc);
74 $page->trig($crc." a le droit de passer !");
75 }
76
77 $sql = XDB::iterator(
78 "SELECT crc, nb, update_time, create_time,
79 FIND_IN_SET('del', release) AS del,
80 FIND_IN_SET('ok', release) AS ok
81 FROM postfix_mailseen
82 WHERE nb >= 30
83 ORDER BY release != ''");
84
85 $page->assign_by_ref('mails', $sql);
86 }
87
88 function handler_postfix_regexpsbounces(&$page, $new = null) {
89 $page->changeTpl('admin/emails_bounces_re.tpl');
90 $page->assign('xorg_title','Polytechnique.org - Administration - Postfix : Regexps Bounces');
91 $page->assign('new', $new);
92
93 if (Post::has('submit')) {
94 foreach (Env::v('lvl') as $id=>$val) {
95 XDB::query(
96 "REPLACE INTO emails_bounces_re (id,pos,lvl,re,text) VALUES ({?}, {?}, {?}, {?}, {?})",
97 $id, $_POST['pos'][$id], $_POST['lvl'][$id], $_POST['re'][$id], $_POST['text'][$id]
98 );
99 }
100 }
101
102 $page->assign('bre', XDB::iterator("SELECT * FROM emails_bounces_re ORDER BY pos"));
103 }
104
441e9e98 105 // {{{ logger view
86f0a6af 106
441e9e98 107 /** Retrieves the available days for a given year and month.
108 * Obtain a list of days of the given month in the given year
109 * that are within the range of dates that we have log entries for.
110 *
111 * @param integer year
112 * @param integer month
113 * @return array days in that month we have log entries covering.
114 * @private
115 */
116 function _getDays($year, $month)
117 {
118 // give a 'no filter' option
119 $months[0] = "----";
120
121 if ($year && $month) {
122 $day_max = Array(-1, 31, checkdate(2, 29, $year) ? 29 : 28 , 31,
123 30, 31, 30, 31, 31, 30, 31, 30, 31);
124 $res = XDB::query("SELECT YEAR (MAX(start)), YEAR (MIN(start)),
125 MONTH(MAX(start)), MONTH(MIN(start)),
126 DAYOFMONTH(MAX(start)),
127 DAYOFMONTH(MIN(start))
128 FROM logger.sessions");
129 list($ymax, $ymin, $mmax, $mmin, $dmax, $dmin) = $res->fetchOneRow();
130
131 if (($year < $ymin) || ($year == $ymin && $month < $mmin)) {
132 return array();
133 }
134
135 if (($year > $ymax) || ($year == $ymax && $month > $mmax)) {
136 return array();
137 }
138
139 $min = ($year==$ymin && $month==$mmin) ? intval($dmin) : 1;
140 $max = ($year==$ymax && $month==$mmax) ? intval($dmax) : $day_max[$month];
141
142 for($i = $min; $i<=$max; $i++) {
143 $days[$i] = $i;
144 }
145 }
146 return $days;
147 }
148
149
150 /** Retrieves the available months for a given year.
151 * Obtains a list of month numbers that are within the timeframe that
152 * we have log entries for.
153 *
154 * @param integer year
155 * @return array List of month numbers we have log info for.
156 * @private
157 */
158 function _getMonths($year)
159 {
160 // give a 'no filter' option
161 $months[0] = "----";
162
163 if ($year) {
164 $res = XDB::query("SELECT YEAR (MAX(start)), YEAR (MIN(start)),
165 MONTH(MAX(start)), MONTH(MIN(start))
166 FROM logger.sessions");
167 list($ymax, $ymin, $mmax, $mmin) = $res->fetchOneRow();
168
169 if (($year < $ymin) || ($year > $ymax)) {
170 return array();
171 }
172
173 $min = $year == $ymin ? intval($mmin) : 1;
174 $max = $year == $ymax ? intval($mmax) : 12;
175
176 for($i = $min; $i<=$max; $i++) {
177 $months[$i] = $i;
178 }
179 }
180 return $months;
181 }
182
183
184 /** Retrieves the available years.
185 * Obtains a list of years that we have log entries covering.
186 *
187 * @return array years we have log entries for.
188 * @private
189 */
190 function _getYears()
191 {
192 // give a 'no filter' option
193 $years[0] = "----";
194
195 // retrieve available years
196 $res = XDB::query("select YEAR(MAX(start)), YEAR(MIN(start)) FROM logger.sessions");
197 list($max, $min) = $res->fetchOneRow();
198
199 for($i = intval($min); $i<=$max; $i++) {
200 $years[$i] = $i;
201 }
202 return $years;
203 }
204
205
206 /** Make a where clause to get a user's sessions.
207 * Prepare the where clause request that will retrieve the sessions.
208 *
209 * @param $year INTEGER Only get log entries made during the given year.
210 * @param $month INTEGER Only get log entries made during the given month.
211 * @param $day INTEGER Only get log entries made during the given day.
212 * @param $uid INTEGER Only get log entries referring to the given user ID.
213 *
214 * @return STRING the WHERE clause of a query, including the 'WHERE' keyword
215 * @private
216 */
217 function _makeWhere($year, $month, $day, $uid)
218 {
219 // start constructing the "where" clause
220 $where = array();
221
222 if ($uid)
223 array_push($where, "uid='$uid'");
224
225 // we were given at least a year
226 if ($year) {
227 if ($day) {
228 $dmin = mktime(0, 0, 0, $month, $day, $year);
229 $dmax = mktime(0, 0, 0, $month, $day+1, $year);
230 } elseif ($month) {
231 $dmin = mktime(0, 0, 0, $month, 1, $year);
232 $dmax = mktime(0, 0, 0, $month+1, 1, $year);
233 } else {
234 $dmin = mktime(0, 0, 0, 1, 1, $year);
235 $dmax = mktime(0, 0, 0, 1, 1, $year+1);
236 }
237 $where[] = "start >= " . date("Ymd000000", $dmin);
238 $where[] = "start < " . date("Ymd000000", $dmax);
239 }
240
241 if (!empty($where)) {
242 return ' WHERE ' . implode($where, " AND ");
243 } else {
244 return '';
245 }
246 // WE know it's totally reversed, so better use array_reverse than a SORT BY start DESC
247 }
248
249 // }}}
250
251 function handler_logger(&$page, $action = null, $arg = null) {
252 if ($action == 'session') {
253
254 // we are viewing a session
255 $res = XDB::query("SELECT ls.*, a.alias AS username, sa.alias AS suer
256 FROM logger.sessions AS ls
257 LEFT JOIN aliases AS a ON (a.id = ls.uid AND a.type='a_vie')
258 LEFT JOIN aliases AS sa ON (sa.id = ls.suid AND sa.type='a_vie')
259 WHERE ls.id = {?}", $arg);
260
261 $page->assign('session', $a = $res->fetchOneAssoc());
262
504ccd57 263 $res = XDB::iterator('SELECT a.text, e.data, e.stamp
441e9e98 264 FROM logger.events AS e
265 LEFT JOIN logger.actions AS a ON e.action=a.id
266 WHERE e.session={?}', $arg);
267 while ($myarr = $res->next()) {
268 $page->append('events', $myarr);
269 }
270
271 } else {
272 $loguser = $action == 'user' ? $arg : Env::v('loguser');
273
274 $res = XDB::query('SELECT id FROM aliases WHERE alias={?}',
275 $loguser);
276 $loguid = $res->fetchOneCell();
277
278 if ($loguid) {
279 $year = Env::i('year');
280 $month = Env::i('month');
281 $day = Env::i('day');
282 } else {
283 $year = Env::i('year', intval(date('Y')));
284 $month = Env::i('month', intval(date('m')));
285 $day = Env::i('day', intval(date('d')));
286 }
287
288 if (!$year)
289 $month = 0;
290 if (!$month)
291 $day = 0;
292
293 // smarty assignments
294 // retrieve available years
295 $page->assign('years', $this->_getYears());
296 $page->assign('year', $year);
297
298 // retrieve available months for the current year
299 $page->assign('months', $this->_getMonths($year));
300 $page->assign('month', $month);
301
302 // retrieve available days for the current year and month
303 $page->assign('days', $this->_getDays($year, $month));
304 $page->assign('day', $day);
305
306 $page->assign('loguser', $loguser);
307 // smarty assignments
308
309 if ($loguid || $year) {
310
311 // get the requested sessions
312 $where = $this->_makeWhere($year, $month, $day, $loguid);
504ccd57 313 $select = "SELECT s.id, s.start, s.uid,
441e9e98 314 a.alias as username
315 FROM logger.sessions AS s
316 LEFT JOIN aliases AS a ON (a.id = s.uid AND a.type='a_vie')
317 $where
318 ORDER BY start DESC";
319 $res = XDB::iterator($select);
320
321 $sessions = array();
322 while ($mysess = $res->next()) {
323 $mysess['events'] = array();
324 $sessions[$mysess['id']] = $mysess;
325 }
326 array_reverse($sessions);
327
328 // attach events
329 $sql = "SELECT s.id, a.text
330 FROM logger.sessions AS s
331 LEFT JOIN logger.events AS e ON(e.session=s.id)
332 INNER JOIN logger.actions AS a ON(a.id=e.action)
333 $where";
334
335 $res = XDB::iterator($sql);
336 while ($event = $res->next()) {
337 array_push($sessions[$event['id']]['events'], $event['text']);
338 }
339 $page->assign_by_ref('sessions', $sessions);
340 } else {
341 $page->assign('msg_nofilters', "Sélectionner une annuée et/ou un utilisateur");
342 }
86f0a6af 343 }
344
8b1f8e12 345 $page->changeTpl('admin/logger-view.tpl');
86f0a6af 346
c4271d38 347 $page->assign('xorg_title','Polytechnique.org - Administration - Logs des sessions');
86f0a6af 348 }
349
f97f90f0 350 function handler_user(&$page, $login = false)
351 {
86f0a6af 352 $page->changeTpl('admin/utilisateurs.tpl');
353 $page->assign('xorg_title','Polytechnique.org - Administration - Edit/Su/Log');
354 require_once("emails.inc.php");
355 require_once("user.func.inc.php");
356
357 if (S::has('suid')) {
358 $page->kill("déjà en SUID !!!");
359 }
360
361 if (Env::has('user_id')) {
5c5554b7 362 $login = get_user_forlife(Env::i('user_id'));
f97f90f0 363 if (empty($login)) {
364 $login = Env::i('user_id');
365 }
86f0a6af 366 } elseif (Env::has('login')) {
5c5554b7 367 $login = get_user_forlife(Env::v('login'));
86f0a6af 368 }
369
370 if(Env::has('logs_button') && $login) {
24b5c84d 371 pl_redirect("admin/logger?loguser=$login&year=".date('Y')."&month=".date('m'));
86f0a6af 372 }
373
374 if (Env::has('ax_button') && $login) {
375 pl_redirect("admin/synchro_ax/$login");
376 }
377
378 if(Env::has('suid_button') && $login) {
379 $_SESSION['log']->log("suid_start", "login by ".S::v('forlife'));
380 $_SESSION['suid'] = $_SESSION;
381 $r = XDB::query("SELECT id FROM aliases WHERE alias={?}", $login);
382 if($uid = $r->fetchOneCell()) {
383 start_connexion($uid,true);
384 pl_redirect("");
385 }
386 }
387
388 if ($login) {
f97f90f0 389 if (is_numeric($login)) {
1517a52d 390 $r = XDB::query("SELECT *, a.alias AS forlife, u.flags AS sexe,
391 (year(naissance) > promo - 15 or year(naissance) < promo - 25) AS naiss_err
f97f90f0 392 FROM auth_user_md5 AS u
393 LEFT JOIN aliases AS a ON (a.id = u.user_id AND type= 'a_vie')
394 WHERE u.user_id = {?}", $login);
395 } else {
1517a52d 396 $r = XDB::query("SELECT *, a.alias AS forlife, u.flags AS sexe,
397 (year(naissance) > promo - 15 or year(naissance) < promo - 25) AS naiss_err
f97f90f0 398 FROM auth_user_md5 AS u
399 INNER JOIN aliases AS a ON ( a.id = u.user_id AND a.alias={?} AND type!='homonyme' )", $login);
400 }
86f0a6af 401 $mr = $r->fetchOneAssoc();
402
f97f90f0 403 if (!is_numeric($login)) { //user has a forlife
404 $redirect = new Redirect($mr['user_id']);
405 }
86f0a6af 406
407 // Check if there was a submission
408 foreach($_POST as $key => $val) {
409 switch ($key) {
410 case "add_fwd":
411 $email = trim(Env::v('email'));
412 if (!isvalid_email_redirection($email)) {
413 $page->trig("invalid email $email");
414 } else {
415 $redirect->add_email($email);
416 $page->trig("Ajout de $email effectué");
417 }
418 break;
419
420 case "del_fwd":
421 if (!empty($val)) {
422 $redirect->delete_email($val);
423 }
424 break;
425
426 case "del_alias":
427 if (!empty($val)) {
f97f90f0 428 XDB::execute("DELETE FROM aliases
429 WHERE id={?} AND alias={?}
430 AND type!='a_vie' AND type!='homonyme'", $mr['user_id'], $val);
d91e720f 431 XDB::execute("UPDATE emails
432 SET rewrite = ''
433 WHERE uid = {?} AND rewrite LIKE CONCAT({?}, '@%')",
434 $mr['user_id'], $val);
86f0a6af 435 fix_bestalias($mr['user_id']);
436 $page->trig($val." a été supprimé");
437 }
438 break;
439 case "activate_fwd":
440 if (!empty($val)) {
441 $redirect->modify_one_email($val, true);
442 }
443 break;
444 case "deactivate_fwd":
445 if (!empty($val)) {
446 $redirect->modify_one_email($val, false);
447 }
448 break;
449 case "add_alias":
450 XDB::execute("INSERT INTO aliases (id,alias,type) VALUES ({?}, {?}, 'alias')",
451 $mr['user_id'], Env::v('email'));
452 break;
453
454 case "best":
455 // 'bestalias' is the first bit of the set : 1
456 // 255 is the max for flags (8 sets max)
457 XDB::execute("UPDATE aliases SET flags= flags & (255 - 1) WHERE id={?}", $mr['user_id']);
458 XDB::execute("UPDATE aliases
459 SET flags= flags | 1
460 WHERE id={?} AND alias={?}", $mr['user_id'], $val);
461 break;
462
463
464 // Editer un profil
465 case "u_edit":
466 require_once('secure_hash.inc.php');
467 $pass_encrypted = Env::v('newpass_clair') != "********" ? hash_encrypt(Env::v('newpass_clair')) : Env::v('passw');
468 $naiss = Env::v('naissanceN');
34ffe99a 469 $deces = Env::v('decesN');
86f0a6af 470 $perms = Env::v('permsN');
471 $prenm = Env::v('prenomN');
472 $nom = Env::v('nomN');
473 $promo = Env::i('promoN');
474 $sexe = Env::v('sexeN');
475 $comm = Env::v('commentN');
476
477 $query = "UPDATE auth_user_md5 SET
478 naissance = '$naiss',
34ffe99a 479 deces = '$deces',
86f0a6af 480 password = '$pass_encrypted',
481 perms = '$perms',
482 prenom = '".addslashes($prenm)."',
483 nom = '".addslashes($nom)."',
484 flags = '$sexe',
485 promo = $promo,
486 comment = '".addslashes($comm)."'
487 WHERE user_id = '{$mr['user_id']}'";
488 if (XDB::execute($query)) {
489 user_reindex($mr['user_id']);
490
1e33266a 491 $mailer = new PlMailer();
86f0a6af 492 $mailer->setFrom("webmaster@polytechnique.org");
493 $mailer->addTo("web@polytechnique.org");
494 $mailer->setSubject("INTERVENTION de ".S::v('forlife'));
495 $mailer->setTxtBody(preg_replace("/[ \t]+/", ' ', $query));
496 $mailer->send();
497
498 $page->trig("updaté correctement.");
499 }
500 if (Env::v('nomusageN') != $mr['nom_usage']) {
86f0a6af 501 set_new_usage($mr['user_id'], Env::v('nomusageN'), make_username(Env::v('prenomN'), Env::v('nomusageN')));
502 }
c53b5964 503 if (Env::v('decesN') != $mr['deces']) {
504 user_clear_all_subs($mr['user_id'], false);
505 }
f97f90f0 506 $r = XDB::query("SELECT *, a.alias AS forlife, u.flags AS sexe
507 FROM auth_user_md5 AS u
508 LEFT JOIN aliases AS a ON (a.id = u.user_id AND type= 'a_vie')
509 WHERE u.user_id = {?}", $mr['user_id']);
86f0a6af 510 $mr = $r->fetchOneAssoc();
511 break;
512
513 // DELETE FROM auth_user_md5
514 case "u_kill":
515 user_clear_all_subs($mr['user_id']);
516 $page->trig("'{$mr['user_id']}' a été désinscrit !");
1e33266a 517 $mailer = new PlMailer();
86f0a6af 518 $mailer->setFrom("webmaster@polytechnique.org");
519 $mailer->addTo("web@polytechnique.org");
520 $mailer->setSubject("INTERVENTION de ".S::v('forlife'));
16cd99fb 521 $mailer->setTxtBody("\nUtilisateur $login désinscrit");
86f0a6af 522 $mailer->send();
523 break;
524 }
525 }
526
b7d19878 527 $res = XDB::query("SELECT start, host
528 FROM logger.sessions
529 WHERE uid={?} AND suid=0
530 ORDER BY start DESC
531 LIMIT 1", $mr['user_id']);
86f0a6af 532 list($lastlogin,$host) = $res->fetchOneRow();
533 $page->assign('lastlogin', $lastlogin);
534 $page->assign('host', $host);
535
5c5554b7 536 $res = XDB::query("SELECT alias
537 FROM virtual
538 INNER JOIN virtual_redirect USING(vid)
539 WHERE type = 'user' AND redirect LIKE '" . $login . "@%'");
540 if ($res->numRows()) {
541 $page->assign('virtual', $res->fetchOneCell());
542 }
543
86f0a6af 544 $page->assign('aliases', XDB::iterator(
545 "SELECT alias, type='a_vie' AS for_life,FIND_IN_SET('bestalias',flags) AS best,expire
546 FROM aliases
547 WHERE id = {?} AND type!='homonyme'
548 ORDER BY type!= 'a_vie'", $mr["user_id"]));
f97f90f0 549 if ($mr['perms'] != 'pending') {
550 $page->assign('emails',$redirect->emails);
551 }
86f0a6af 552
553 $page->assign('mr',$mr);
554 }
555 }
1f53925a 556
557 function getMatricule($line, $key)
558 {
559 $mat = $line['matricule'];
560 $year = intval(substr($mat, 0, 3));
561 $rang = intval(substr($mat, 3, 3));
562 if ($year > 200) { $year /= 10; };
563 if ($year < 96) {
564 return null;
565 } else {
566 return sprintf('%04u%04u', 1900+$year, $rang);
567 }
568 }
569
570 function handler_promo(&$page, $action = null, $promo = null)
571 {
572 if (Env::has('promo')) {
573 if(Env::i('promo') > 1900 && Env::i('promo') < 2050) {
574 $action = Env::v('valid_promo') == 'Ajouter des membres' ? 'add' : 'ax';
575 pl_redirect('admin/promo/' . $action . '/' . Env::i('promo'));
576 } else {
577 $page->trig('Promo non valide');
578 }
579 }
580
581 $page->changeTpl('admin/promo.tpl');
582 if ($promo > 1900 && $promo < 2050 && ($action == 'add' || $action == 'ax')) {
583 $page->assign('promo', $promo);
584 } else {
585 return;
586 }
587
588 $importer = new CSVImporter('auth_user_md5', 'matricule');
589 $importer->registerFunction('matricule', 'matricle Ecole vers X.org', array($this, 'getMatricule'));
590 switch ($action) {
591 case 'add':
592 $fields = array('nom', 'nom_ini', 'prenom',
593 'prenom_ini', 'promo', 'promo_sortie', 'flags',
594 'matricule', 'matricule_ax', 'perms');
595 $importer->forceValue('promo', $promo);
596 $importer->forceValue('promo_sortie', $promo + 3);
597 break;
598 case 'ax':
599 $fields = array('matricule', 'matricule_ax');
600 break;
601 }
602 $importer->apply($page, "admin/promo/$action/$promo", $fields);
603 }
604
86f0a6af 605 function handler_homonyms(&$page, $op = 'list', $target = null) {
606 $page->changeTpl('admin/homonymes.tpl');
607 $page->assign('xorg_title','Polytechnique.org - Administration - Homonymes');
608 require_once("homonymes.inc.php");
74c55fd6 609
86f0a6af 610 if ($target) {
611 if (! list($prenom,$nom,$forlife,$loginbis) = select_if_homonyme($target)) {
612 $target=0;
613 } else {
614 $page->assign('nom',$nom);
615 $page->assign('prenom',$prenom);
616 $page->assign('forlife',$forlife);
617 $page->assign('loginbis',$loginbis);
618 }
619 }
74c55fd6 620
86f0a6af 621 $page->assign('op',$op);
622 $page->assign('target',$target);
74c55fd6 623
86f0a6af 624 // on a un $target valide, on prepare les mails
625 if ($target) {
74c55fd6 626
86f0a6af 627 // on examine l'op a effectuer
628 switch ($op) {
629 case 'mail':
b7d19878 630 send_warning_homonyme($prenom, $nom, $forlife, $loginbis);
631 switch_bestalias($target, $loginbis);
86f0a6af 632 $op = 'list';
633 break;
634 case 'correct':
b7d19878 635 switch_bestalias($target, $loginbis);
86f0a6af 636 XDB::execute("UPDATE aliases SET type='homonyme',expire=NOW() WHERE alias={?}", $loginbis);
637 XDB::execute("REPLACE INTO homonymes (homonyme_id,user_id) VALUES({?},{?})", $target, $target);
b7d19878 638 send_robot_homonyme($prenom, $nom, $forlife, $loginbis);
86f0a6af 639 $op = 'list';
640 break;
641 }
642 }
74c55fd6 643
86f0a6af 644 if ($op == 'list') {
645 $res = XDB::iterator(
646 "SELECT a.alias AS homonyme,s.id AS user_id,s.alias AS forlife,
647 promo,prenom,nom,
648 IF(h.homonyme_id=s.id, a.expire, NULL) AS expire,
649 IF(h.homonyme_id=s.id, a.type, NULL) AS type
650 FROM aliases AS a
651 LEFT JOIN homonymes AS h ON (h.homonyme_id = a.id)
652 INNER JOIN aliases AS s ON (s.id = h.user_id AND s.type='a_vie')
653 INNER JOIN auth_user_md5 AS u ON (s.id=u.user_id)
654 WHERE a.type='homonyme' OR a.expire!=''
655 ORDER BY a.alias,promo");
656 $hnymes = Array();
657 while ($tab = $res->next()) {
658 $hnymes[$tab['homonyme']][] = $tab;
659 }
660 $page->assign_by_ref('hnymes',$hnymes);
661 }
662 }
74c55fd6 663
86f0a6af 664 function handler_ax_xorg(&$page) {
665 $page->changeTpl('admin/ax-xorg.tpl');
666 $page->assign('xorg_title','Polytechnique.org - Administration - AX/X.org');
74c55fd6 667
86f0a6af 668 // liste des différences
669 $res = XDB::query(
670 '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
671 FROM auth_user_md5 AS u
672 INNER JOIN identification_ax AS ia ON u.matricule_ax = ia.matricule_ax
673 WHERE (SOUNDEX(u.nom) != SOUNDEX(ia.nom) AND SOUNDEX(CONCAT(ia.particule,u.nom)) != SOUNDEX(ia.nom)
674 AND SOUNDEX(u.nom) != SOUNDEX(ia.nom_patro) AND SOUNDEX(CONCAT(ia.particule,u.nom)) != SOUNDEX(ia.nom_patro))
675 OR u.prenom != ia.prenom OR (u.promo != ia.promo AND u.promo != ia.promo+1 AND u.promo != ia.promo-1)
676 ORDER BY u.promo,u.nom,u.prenom');
677 $page->assign('diffs', $res->fetchAllAssoc());
74c55fd6 678
86f0a6af 679 // gens à l'ax mais pas chez nous
680 $res = XDB::query(
681 'SELECT ia.promo,ia.nom,ia.nom_patro,ia.prenom
682 FROM identification_ax as ia
683 LEFT JOIN auth_user_md5 AS u ON u.matricule_ax = ia.matricule_ax
684 WHERE u.nom IS NULL');
685 $page->assign('mank', $res->fetchAllAssoc());
74c55fd6 686
86f0a6af 687 // gens chez nous et pas à l'ax
688 $res = XDB::query('SELECT promo,nom,prenom FROM auth_user_md5 WHERE matricule_ax IS NULL');
689 $page->assign('plus', $res->fetchAllAssoc());
690 }
74c55fd6 691
86f0a6af 692 function handler_deaths(&$page, $promo = 0, $validate = false) {
693 $page->changeTpl('admin/deces_promo.tpl');
694 $page->assign('xorg_title','Polytechnique.org - Administration - Deces');
74c55fd6 695
86f0a6af 696 if (!$promo)
697 $promo = Env::i('promo');
698 if (Env::has('sub10')) $promo -= 10;
699 if (Env::has('sub01')) $promo -= 1;
700 if (Env::has('add01')) $promo += 1;
701 if (Env::has('add10')) $promo += 10;
74c55fd6 702
86f0a6af 703 $page->assign('promo',$promo);
74c55fd6 704
86f0a6af 705 if ($validate) {
706 $new_deces = array();
707 $res = XDB::iterRow("SELECT user_id,matricule,nom,prenom,deces FROM auth_user_md5 WHERE promo = {?}", $promo);
708 while (list($uid,$mat,$nom,$prenom,$deces) = $res->next()) {
709 $val = Env::v($mat);
b7d19878 710 if($val == $deces || empty($val)) continue;
711 XDB::execute('UPDATE auth_user_md5 SET deces={?} WHERE matricule = {?}', $val, $mat);
712 $new_deces[] = array('name' => "$prenom $nom", 'date' => "$val");
713 if($deces=='0000-00-00' or empty($deces)) {
714 require_once('notifs.inc.php');
715 register_watch_op($uid, WATCH_DEATH, $val);
716 require_once('user.func.inc.php');
717 user_clear_all_subs($uid, false); // by default, dead ppl do not loose their email
718 }
86f0a6af 719 }
720 $page->assign('new_deces',$new_deces);
721 }
74c55fd6 722
86f0a6af 723 $res = XDB::iterator('SELECT matricule, nom, prenom, deces FROM auth_user_md5 WHERE promo = {?} ORDER BY nom,prenom', $promo);
724 $page->assign('decedes', $res);
725 }
74c55fd6 726
86f0a6af 727 function handler_synchro_ax(&$page, $user = null, $action = null) {
728 $page->changeTpl('admin/synchro_ax.tpl');
729 $page->assign('xorg_title','Polytechnique.org - Administration - Synchro AX');
74c55fd6 730
86f0a6af 731 require_once('synchro_ax.inc.php');
74c55fd6 732
86f0a6af 733 if (is_ax_key_missing()) {
734 $page->assign('no_private_key', true);
735 $page->run();
736 }
74c55fd6 737
86f0a6af 738 require_once('user.func.inc.php');
739
740 if ($user)
741 $login = get_user_forlife($user);
742
743 if (Env::has('user')) {
744 $login = get_user_forlife(Env::v('user'));
745 if ($login === false) {
746 return;
747 }
748 }
74c55fd6 749
86f0a6af 750 if (Env::has('mat')) {
751 $res = XDB::query(
74c55fd6 752 "SELECT alias
86f0a6af 753 FROM aliases AS a
754 INNER JOIN auth_user_md5 AS u ON (a.id=u.user_id AND a.type='a_vie')
755 WHERE matricule={?}", Env::i('mat'));
756 $login = $res->fetchOneCell();
757 }
74c55fd6 758
86f0a6af 759 if ($login) {
760 if ($action == 'import') {
761 ax_synchronize($login, S::v('uid'));
762 }
763 // get details from user, but looking only info that can be seen by ax
764 $user = get_user_details($login, S::v('uid'), 'ax');
765 $userax= get_user_ax($user['matricule_ax']);
766 require_once 'profil.func.inc.php';
767 $diff = diff_user_details($userax, $user, 'ax');
74c55fd6 768
86f0a6af 769 $page->assign('x', $user);
770 $page->assign('diff', $diff);
771 }
772 }
74c55fd6 773
ed5b9703 774 function handler_validate(&$page, $action = 'list', $id = null) {
86f0a6af 775 $page->changeTpl('admin/valider.tpl');
776 $page->assign('xorg_title','Polytechnique.org - Administration - Valider une demande');
777 require_once("validations.inc.php");
74c55fd6 778
ed5b9703 779 if ($action == 'edit' and !is_null($id)) {
780 $page->assign('preview_id', $id);
781 }
782
86f0a6af 783 if(Env::has('uid') && Env::has('type') && Env::has('stamp')) {
20d7932b 784 $req = Validate::get_typed_request(Env::v('uid'), Env::v('type'), Env::v('stamp'));
86f0a6af 785 if($req) { $req->handle_formu(); }
786 }
74c55fd6 787
cfb96867 788 $r = XDB::iterator('SHOW COLUMNS FROM requests_answers');
789 while (($a = $r->next()) && $a['Field'] != 'category');
790 $page->assign('categories', $categories = explode(',', str_replace("'", '', substr($a['Type'], 5, -1))));
74c55fd6 791
cfb96867 792 $hidden = array();
793 if (Post::has('hide')) {
74c55fd6 794 $hide = array();
795 foreach ($categories as $cat)
cfb96867 796 if (!Post::v($cat)) {
797 $hidden[$cat] = 1;
798 $hide[] = $cat;
799 }
800 setcookie('hide_requests', join(',',$hide), time()+(count($hide)?25920000:(-3600)), '/', '', 0);
801 } elseif (Env::has('hide_requests')) {
802 foreach (explode(',',Env::v('hide_requests')) as $hide_type)
803 $hidden[$hide_type] = true;
804 }
805 $page->assign('hide_requests', $hidden);
74c55fd6 806
86f0a6af 807 $page->assign('vit', new ValidateIterator());
808 }
e18888f4 809 function handler_validate_answers(&$page, $action = 'list', $id = null) {
e18888f4 810 $page->assign('xorg_title','Polytechnique.org - Administration - Réponses automatiques de validation');
811 $page->assign('title', 'Gestion des réponses automatiques');
812 $table_editor = new PLTableEditor('admin/validate/answers','requests_answers','id');
813 $table_editor->describe('category','catégorie',true);
814 $table_editor->describe('title','titre',true);
815 $table_editor->describe('answer','texte',false);
816 $table_editor->apply($page, $action, $id);
817 }
e7ae7df9 818 function handler_skins(&$page, $action = 'list', $id = null) {
e7ae7df9 819 $page->assign('xorg_title','Polytechnique.org - Administration - Skins');
820 $page->assign('title', 'Gestion des skins');
821 $table_editor = new PLTableEditor('admin/skins','skins','id');
822 $table_editor->describe('name','nom',true);
823 $table_editor->describe('skin_tpl','nom du template',true);
824 $table_editor->describe('auteur','auteur',false);
825 $table_editor->describe('comment','commentaire',true);
826 $table_editor->describe('date','date',false);
827 $table_editor->describe('ext','extension du screenshot',false);
828 $table_editor->apply($page, $action, $id);
74c55fd6 829 }
e7ae7df9 830 function handler_postfix_blacklist(&$page, $action = 'list', $id = null) {
e7ae7df9 831 $page->assign('xorg_title','Polytechnique.org - Administration - Postfix : Blacklist');
832 $page->assign('title', 'Blacklist de postfix');
833 $table_editor = new PLTableEditor('admin/postfix/blacklist','postfix_blacklist','email', true);
834 $table_editor->describe('reject_text','Texte de rejet',true);
835 $table_editor->describe('email','email',true);
836 $table_editor->apply($page, $action, $id);
74c55fd6 837 }
e7ae7df9 838 function handler_postfix_whitelist(&$page, $action = 'list', $id = null) {
e7ae7df9 839 $page->assign('xorg_title','Polytechnique.org - Administration - Postfix : Whitelist');
840 $page->assign('title', 'Whitelist de postfix');
841 $table_editor = new PLTableEditor('admin/postfix/whitelist','postfix_whitelist','email', true);
842 $table_editor->describe('email','email',true);
843 $table_editor->apply($page, $action, $id);
74c55fd6 844 }
e7ae7df9 845 function handler_logger_actions(&$page, $action = 'list', $id = null) {
e7ae7df9 846 $page->assign('xorg_title','Polytechnique.org - Administration - Actions');
847 $page->assign('title', 'Gestion des actions de logger');
848 $table_editor = new PLTableEditor('admin/logger/actions','logger.actions','id');
849 $table_editor->describe('text','intitulé',true);
850 $table_editor->describe('description','description',true);
851 $table_editor->apply($page, $action, $id);
74c55fd6 852 }
e7ae7df9 853 function handler_downtime(&$page, $action = 'list', $id = null) {
e7ae7df9 854 $page->assign('xorg_title','Polytechnique.org - Administration - Coupures');
855 $page->assign('title', 'Gestion des coupures');
856 $table_editor = new PLTableEditor('admin/downtime','coupures','id');
857 $table_editor->describe('debut','date',true);
858 $table_editor->describe('duree','durée',false);
859 $table_editor->describe('resume','résumé',true);
860 $table_editor->describe('services','services affectés',true);
861 $table_editor->describe('description','description',false);
862 $table_editor->apply($page, $action, $id);
74c55fd6 863 }
bc0903c7 864
865 function handler_wiki(&$page, $action='list')
866 {
3aec1c21 867 require_once 'wiki.inc.php';
74c55fd6 868
3aec1c21 869 // update wiki perms
870 if ($action == 'update') {
871 $perms_read = Post::v('read');
872 $perms_edot = Post::v('edit');
873 if ($perms_read || $perms_edit) {
874 foreach ($_POST as $wiki_page => $val) if ($val == 'on') {
875 $wiki_page = str_replace('_', '/', $wiki_page);
876 if (!$perms_read || !$perms_edit)
74c55fd6 877 list($perms0, $perms1) = wiki_get_perms($wiki_page);
3aec1c21 878 if ($perms_read)
879 $perms0 = $perms_read;
880 if ($perms_edit)
881 $perms1 = $perms_edit;
882 wiki_set_perms($wiki_page, $perms0, $perms1);
883 }
884 }
885 }
74c55fd6 886
3aec1c21 887 $perms = wiki_perms_options();
74c55fd6 888
3aec1c21 889 // list wiki pages and their perms
890 $wiki_pages = array();
891 $dir = wiki_work_dir();
892 if (is_dir($dir)) {
893 if ($dh = opendir($dir)) {
894 while (($file = readdir($dh)) !== false) if (substr($file,0,1) >= 'A' && substr($file,0,1) <= 'Z') {
895 list($read,$edit) = wiki_get_perms($file);
896 $wiki_pages[$file] = array('read' => $perms[$read], 'edit' => $perms[$edit]);
897 }
898 closedir($dh);
899 }
900 }
901 ksort($wiki_pages);
bc0903c7 902 $wiki_tree = array();
903 foreach ($wiki_pages as $file => $desc) {
904 list($cat, $name) = explode('.', $file);
905 if (!isset($wiki_tree[$cat])) {
906 $wiki_tree[$cat] = array();
907 }
908 $wiki_tree[$cat][$name] = $desc;
909 }
910
911
74c55fd6 912
3aec1c21 913 $page->changeTpl('admin/wiki.tpl');
bc0903c7 914 $page->assign('wiki_pages', $wiki_tree);
3aec1c21 915 $page->assign('perms_opts', $perms);
916 }
5480a216 917
918 function handler_ipwatch(&$page, $action = 'list', $ip = null)
919 {
920 $page->changeTpl('admin/ipwatcher.tpl');
921
922 $states = array('safe' => 'Ne pas surveiller',
923 'unsafe' => 'Surveiller les inscription',
924 'dangerous' => 'Surveiller tous les accès',
925 'ban' => 'Bannir cette adresse');
926 $page->assign('states', $states);
927
928 switch (Post::v('action')) {
929 case 'create':
930 if (trim(Post::v('ipN')) != '') {
931 Xdb::execute('INSERT IGNORE INTO ip_watch (ip, state, detection, last, uid, description)
932 VALUES ({?}, {?}, CURDATE(), NOW(), {?}, {?})',
933 trim(Post::v('ipN')), Post::v('stateN'), S::i('uid'), Post::v('descriptionN'));
934 };
935 break;
936
937 case 'edit':
938 Xdb::execute('UPDATE ip_watch
939 SET state = {?}, last = NOW(), uid = {?}, description = {?}
940 WHERE ip = {?}', Post::v('stateN'), S::i('uid'), Post::v('descriptionN'), Post::v('ipN'));
941 break;
942
943 default:
944 if ($action == 'delete' && !is_null($ip)) {
945 Xdb::execute('DELETE FROM emails_watch WHERE ip = {?}', $ip);
946 }
947 }
948 if ($action != 'create' && $action != 'edit') {
949 $action = 'list';
950 }
951 $page->assign('action', $action);
952
953 if ($action == 'list') {
954 $sql = "SELECT w.ip, s.host, w.detection, w.state, a.alias AS forlife
955 FROM ip_watch AS w
956 LEFT JOIN logger.sessions AS s USING(ip)
957 LEFT JOIN aliases AS a ON (a.id = s.uid AND a.type = 'a_vie')
958 GROUP BY w.ip, a.alias
959 ORDER BY w.state, w.ip, a.alias";
960 $it = Xdb::iterRow($sql);
961
962 $table = array();
963 $props = array();
964 while (list($ip, $host, $date, $state, $forlife) = $it->next()) {
965 if (count($props) == 0 || $props['ip'] != $ip) {
966 if (count($props) > 0) {
967 $table[] = $props;
968 }
969 $props = array('ip' => $ip,
970 'host' => $host,
971 'detection' => $date,
972 'state' => $state,
973 'users' => array($forlife));
974 } else {
975 $props['users'][] = $forlife;
976 }
977 }
978 if (count($props) > 0) {
979 $table[] = $props;
980 }
981 $page->assign('table', $table);
982 } elseif ($action == 'edit') {
983 $sql = "SELECT w.detection, w.state, w.last, w.description,
984 a1.alias AS edit, a2.alias AS forlife, s.host
985 FROM ip_watch AS w
986 LEFT JOIN aliases AS a1 ON (a1.id = w.uid AND a1.type = 'a_vie')
987 LEFT JOIN logger.sessions AS s ON (w.ip = s.ip)
988 LEFT JOIN aliases AS a2 ON (a2.id = s.uid AND a2.type = 'a_vie')
989 WHERE w.ip = {?}
990 GROUP BY a2.alias
991 ORDER BY a2.alias";
992 $it = Xdb::iterRow($sql, $ip);
993
994 $props = array();
995 while (list($detection, $state, $last, $description, $edit, $forlife, $host) = $it->next()) {
996 if (count($props) == 0) {
997 $props = array('ip' => $ip,
998 'host' => $host,
999 'detection' => $detection,
1000 'state' => $state,
1001 'last' => $last,
1002 'description' => $description,
1003 'edit' => $edit,
1004 'users' => array($forlife));
1005 } else {
1006 $props['users'][] = $forlife;
1007 }
1008 }
1009 $page->assign('ip', $props);
1010 }
1011 }
86f0a6af 1012}
1013
1014?>