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