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