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