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