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