| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2011 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 | 'get_rights' => $this->make_hook('get_rights', AUTH_COOKIE, 'admin'), |
| 29 | 'set_skin' => $this->make_hook('set_skin', AUTH_COOKIE, 'admin'), |
| 30 | 'admin' => $this->make_hook('default', AUTH_MDP, 'admin'), |
| 31 | 'admin/dead-but-active' => $this->make_hook('dead_but_active', AUTH_MDP, 'admin'), |
| 32 | 'admin/deaths' => $this->make_hook('deaths', AUTH_MDP, 'admin'), |
| 33 | 'admin/downtime' => $this->make_hook('downtime', AUTH_MDP, 'admin'), |
| 34 | 'admin/homonyms' => $this->make_hook('homonyms', AUTH_MDP, 'admin'), |
| 35 | 'admin/logger' => $this->make_hook('logger', AUTH_MDP, 'admin'), |
| 36 | 'admin/logger/actions' => $this->make_hook('logger_actions', AUTH_MDP, 'admin'), |
| 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'), |
| 41 | 'admin/mx/broken' => $this->make_hook('mx_broken', AUTH_MDP, 'admin'), |
| 42 | 'admin/skins' => $this->make_hook('skins', AUTH_MDP, 'admin'), |
| 43 | 'admin/user' => $this->make_hook('user', AUTH_MDP, 'admin'), |
| 44 | 'admin/add_accounts' => $this->make_hook('add_accounts', AUTH_MDP, 'admin'), |
| 45 | 'admin/validate' => $this->make_hook('validate', AUTH_MDP, 'admin,edit_directory'), |
| 46 | 'admin/validate/answers' => $this->make_hook('validate_answers', AUTH_MDP, 'admin'), |
| 47 | 'admin/wiki' => $this->make_hook('wiki', AUTH_MDP, 'admin'), |
| 48 | 'admin/ipwatch' => $this->make_hook('ipwatch', AUTH_MDP, 'admin'), |
| 49 | 'admin/icons' => $this->make_hook('icons', AUTH_MDP, 'admin'), |
| 50 | 'admin/geocoding' => $this->make_hook('geocoding', AUTH_MDP, 'admin'), |
| 51 | 'admin/accounts' => $this->make_hook('accounts', AUTH_MDP, 'admin'), |
| 52 | 'admin/account/watch' => $this->make_hook('account_watch', AUTH_MDP, 'admin'), |
| 53 | 'admin/account/types' => $this->make_hook('account_types', AUTH_MDP, 'admin'), |
| 54 | 'admin/xnet_without_group' => $this->make_hook('xnet_without_group', AUTH_MDP, 'admin'), |
| 55 | 'admin/jobs' => $this->make_hook('jobs', AUTH_MDP, 'admin,edit_directory'), |
| 56 | 'admin/profile' => $this->make_hook('profile', AUTH_MDP, 'admin,edit_directory'), |
| 57 | 'admin/phd' => $this->make_hook('phd', AUTH_MDP, 'admin') |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | function handler_phpinfo($page) |
| 62 | { |
| 63 | phpinfo(); |
| 64 | exit; |
| 65 | } |
| 66 | |
| 67 | function handler_get_rights($page) |
| 68 | { |
| 69 | if (S::suid()) { |
| 70 | $page->kill('Déjà en SUID'); |
| 71 | } |
| 72 | S::assert_xsrf_token(); |
| 73 | $level = Post::s('account_type'); |
| 74 | if ($level != 'admin') { |
| 75 | $user = User::getSilentWithUID(S::user()->id()); |
| 76 | $user->is_admin = false; |
| 77 | $types = DirEnum::getOptions(DirEnum::ACCOUNTTYPES); |
| 78 | if (!empty($types[$level])) { |
| 79 | $user->setPerms($types[$level]); |
| 80 | } |
| 81 | S::set('suid_startpage', $_SERVER['HTTP_REFERER']); |
| 82 | Platal::session()->startSUID($user); |
| 83 | } |
| 84 | if (!empty($_SERVER['HTTP_REFERER'])) { |
| 85 | http_redirect($_SERVER['HTTP_REFERER']); |
| 86 | } else { |
| 87 | pl_redirect('/'); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | function handler_set_skin($page) |
| 92 | { |
| 93 | S::assert_xsrf_token(); |
| 94 | S::set('skin', Post::s('change_skin')); |
| 95 | if (!empty($_SERVER['HTTP_REFERER'])) { |
| 96 | http_redirect($_SERVER['HTTP_REFERER']); |
| 97 | } else { |
| 98 | pl_redirect('/'); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | function handler_default($page) |
| 103 | { |
| 104 | $page->changeTpl('admin/index.tpl'); |
| 105 | $page->setTitle('Administration'); |
| 106 | } |
| 107 | |
| 108 | function handler_postfix_delayed($page) |
| 109 | { |
| 110 | $page->changeTpl('admin/postfix_delayed.tpl'); |
| 111 | $page->setTitle('Administration - Postfix : Retardés'); |
| 112 | |
| 113 | if (Env::has('del')) { |
| 114 | $crc = Env::v('crc'); |
| 115 | XDB::execute("UPDATE postfix_mailseen SET release = 'del' WHERE crc = {?}", $crc); |
| 116 | $page->trigSuccess($crc . " verra tous ses emails supprimés !"); |
| 117 | } elseif (Env::has('ok')) { |
| 118 | $crc = Env::v('crc'); |
| 119 | XDB::execute("UPDATE postfix_mailseen SET release = 'ok' WHERE crc = {?}", $crc); |
| 120 | $page->trigSuccess($crc . " a le droit de passer !"); |
| 121 | } |
| 122 | |
| 123 | $sql = XDB::iterator( |
| 124 | "SELECT crc, nb, update_time, create_time, |
| 125 | FIND_IN_SET('del', p.release) AS del, |
| 126 | FIND_IN_SET('ok', p.release) AS ok |
| 127 | FROM postfix_mailseen AS p |
| 128 | WHERE nb >= 30 |
| 129 | ORDER BY p.release != ''"); |
| 130 | |
| 131 | $page->assign_by_ref('mails', $sql); |
| 132 | } |
| 133 | |
| 134 | // {{{ logger view |
| 135 | |
| 136 | /** Retrieves the available days for a given year and month. |
| 137 | * Obtain a list of days of the given month in the given year |
| 138 | * that are within the range of dates that we have log entries for. |
| 139 | * |
| 140 | * @param integer year |
| 141 | * @param integer month |
| 142 | * @return array days in that month we have log entries covering. |
| 143 | * @private |
| 144 | */ |
| 145 | function _getDays($year, $month) |
| 146 | { |
| 147 | // give a 'no filter' option |
| 148 | $days = array(); |
| 149 | $days[0] = "----"; |
| 150 | |
| 151 | if ($year && $month) { |
| 152 | $day_max = Array(-1, 31, checkdate(2, 29, $year) ? 29 : 28 , 31, |
| 153 | 30, 31, 30, 31, 31, 30, 31, 30, 31); |
| 154 | $res = XDB::query("SELECT YEAR (MAX(start)), YEAR (MIN(start)), |
| 155 | MONTH(MAX(start)), MONTH(MIN(start)), |
| 156 | DAYOFMONTH(MAX(start)), |
| 157 | DAYOFMONTH(MIN(start)) |
| 158 | FROM log_sessions"); |
| 159 | list($ymax, $ymin, $mmax, $mmin, $dmax, $dmin) = $res->fetchOneRow(); |
| 160 | |
| 161 | if (($year < $ymin) || ($year == $ymin && $month < $mmin)) { |
| 162 | return array(); |
| 163 | } |
| 164 | |
| 165 | if (($year > $ymax) || ($year == $ymax && $month > $mmax)) { |
| 166 | return array(); |
| 167 | } |
| 168 | |
| 169 | $min = ($year==$ymin && $month==$mmin) ? intval($dmin) : 1; |
| 170 | $max = ($year==$ymax && $month==$mmax) ? intval($dmax) : $day_max[$month]; |
| 171 | |
| 172 | for($i = $min; $i<=$max; $i++) { |
| 173 | $days[$i] = $i; |
| 174 | } |
| 175 | } |
| 176 | return $days; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /** Retrieves the available months for a given year. |
| 181 | * Obtains a list of month numbers that are within the timeframe that |
| 182 | * we have log entries for. |
| 183 | * |
| 184 | * @param integer year |
| 185 | * @return array List of month numbers we have log info for. |
| 186 | * @private |
| 187 | */ |
| 188 | function _getMonths($year) |
| 189 | { |
| 190 | // give a 'no filter' option |
| 191 | $months = array(); |
| 192 | $months[0] = "----"; |
| 193 | |
| 194 | if ($year) { |
| 195 | $res = XDB::query("SELECT YEAR (MAX(start)), YEAR (MIN(start)), |
| 196 | MONTH(MAX(start)), MONTH(MIN(start)) |
| 197 | FROM log_sessions"); |
| 198 | list($ymax, $ymin, $mmax, $mmin) = $res->fetchOneRow(); |
| 199 | |
| 200 | if (($year < $ymin) || ($year > $ymax)) { |
| 201 | return array(); |
| 202 | } |
| 203 | |
| 204 | $min = $year == $ymin ? intval($mmin) : 1; |
| 205 | $max = $year == $ymax ? intval($mmax) : 12; |
| 206 | |
| 207 | for($i = $min; $i<=$max; $i++) { |
| 208 | $months[$i] = $i; |
| 209 | } |
| 210 | } |
| 211 | return $months; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | /** Retrieves the available years. |
| 216 | * Obtains a list of years that we have log entries covering. |
| 217 | * |
| 218 | * @return array years we have log entries for. |
| 219 | * @private |
| 220 | */ |
| 221 | function _getYears() |
| 222 | { |
| 223 | // give a 'no filter' option |
| 224 | $years = array(); |
| 225 | $years[0] = "----"; |
| 226 | |
| 227 | // retrieve available years |
| 228 | $res = XDB::query("select YEAR(MAX(start)), YEAR(MIN(start)) FROM log_sessions"); |
| 229 | list($max, $min) = $res->fetchOneRow(); |
| 230 | |
| 231 | for($i = intval($min); $i<=$max; $i++) { |
| 232 | $years[$i] = $i; |
| 233 | } |
| 234 | return $years; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | /** Make a where clause to get a user's sessions. |
| 239 | * Prepare the where clause request that will retrieve the sessions. |
| 240 | * |
| 241 | * @param $year INTEGER Only get log entries made during the given year. |
| 242 | * @param $month INTEGER Only get log entries made during the given month. |
| 243 | * @param $day INTEGER Only get log entries made during the given day. |
| 244 | * @param $uid INTEGER Only get log entries referring to the given user ID. |
| 245 | * |
| 246 | * @return STRING the WHERE clause of a query, including the 'WHERE' keyword |
| 247 | * @private |
| 248 | */ |
| 249 | function _makeWhere($year, $month, $day, $uid) |
| 250 | { |
| 251 | // start constructing the "where" clause |
| 252 | $where = array(); |
| 253 | |
| 254 | if ($uid) |
| 255 | array_push($where, "s.uid='$uid'"); |
| 256 | |
| 257 | // we were given at least a year |
| 258 | if ($year) { |
| 259 | if ($day) { |
| 260 | $dmin = mktime(0, 0, 0, $month, $day, $year); |
| 261 | $dmax = mktime(0, 0, 0, $month, $day+1, $year); |
| 262 | } elseif ($month) { |
| 263 | $dmin = mktime(0, 0, 0, $month, 1, $year); |
| 264 | $dmax = mktime(0, 0, 0, $month+1, 1, $year); |
| 265 | } else { |
| 266 | $dmin = mktime(0, 0, 0, 1, 1, $year); |
| 267 | $dmax = mktime(0, 0, 0, 1, 1, $year+1); |
| 268 | } |
| 269 | $where[] = "start >= " . date("Ymd000000", $dmin); |
| 270 | $where[] = "start < " . date("Ymd000000", $dmax); |
| 271 | } |
| 272 | |
| 273 | if (!empty($where)) { |
| 274 | return ' WHERE ' . implode($where, " AND "); |
| 275 | } else { |
| 276 | return ''; |
| 277 | } |
| 278 | // WE know it's totally reversed, so better use array_reverse than a SORT BY start DESC |
| 279 | } |
| 280 | |
| 281 | // }}} |
| 282 | |
| 283 | function handler_logger($page, $action = null, $arg = null) { |
| 284 | if ($action == 'session') { |
| 285 | |
| 286 | // we are viewing a session |
| 287 | $res = XDB::query("SELECT ls.*, a.hruid AS username, sa.hruid AS suer |
| 288 | FROM log_sessions AS ls |
| 289 | INNER JOIN accounts AS a ON (a.uid = ls.uid) |
| 290 | LEFT JOIN accounts AS sa ON (sa.uid = ls.suid) |
| 291 | WHERE ls.id = {?}", $arg); |
| 292 | |
| 293 | $page->assign('session', $a = $res->fetchOneAssoc()); |
| 294 | |
| 295 | $res = XDB::iterator('SELECT a.text, e.data, e.stamp |
| 296 | FROM log_events AS e |
| 297 | LEFT JOIN log_actions AS a ON e.action=a.id |
| 298 | WHERE e.session={?}', $arg); |
| 299 | while ($myarr = $res->next()) { |
| 300 | $page->append('events', $myarr); |
| 301 | } |
| 302 | |
| 303 | } else { |
| 304 | $loguser = $action == 'user' ? $arg : Env::v('loguser'); |
| 305 | |
| 306 | if ($loguser) { |
| 307 | $user = User::get($loguser); |
| 308 | $loguid = $user->id(); |
| 309 | } else { |
| 310 | $loguid = null; |
| 311 | } |
| 312 | |
| 313 | if ($loguid) { |
| 314 | $year = Env::i('year'); |
| 315 | $month = Env::i('month'); |
| 316 | $day = Env::i('day'); |
| 317 | } else { |
| 318 | $year = Env::i('year', intval(date('Y'))); |
| 319 | $month = Env::i('month', intval(date('m'))); |
| 320 | $day = Env::i('day', intval(date('d'))); |
| 321 | } |
| 322 | |
| 323 | if (!$year) |
| 324 | $month = 0; |
| 325 | if (!$month) |
| 326 | $day = 0; |
| 327 | |
| 328 | // smarty assignments |
| 329 | // retrieve available years |
| 330 | $page->assign('years', $this->_getYears()); |
| 331 | $page->assign('year', $year); |
| 332 | |
| 333 | // retrieve available months for the current year |
| 334 | $page->assign('months', $this->_getMonths($year)); |
| 335 | $page->assign('month', $month); |
| 336 | |
| 337 | // retrieve available days for the current year and month |
| 338 | $page->assign('days', $this->_getDays($year, $month)); |
| 339 | $page->assign('day', $day); |
| 340 | |
| 341 | $page->assign('loguser', $loguser); |
| 342 | // smarty assignments |
| 343 | |
| 344 | if ($loguid || $year) { |
| 345 | |
| 346 | // get the requested sessions |
| 347 | $where = $this->_makeWhere($year, $month, $day, $loguid); |
| 348 | $select = "SELECT s.id, s.start, s.uid, |
| 349 | a.hruid as username |
| 350 | FROM log_sessions AS s |
| 351 | INNER JOIN accounts AS a ON (a.uid = s.uid) |
| 352 | $where |
| 353 | ORDER BY start DESC"; |
| 354 | $res = XDB::iterator($select); |
| 355 | |
| 356 | $sessions = array(); |
| 357 | while ($mysess = $res->next()) { |
| 358 | $mysess['events'] = array(); |
| 359 | $sessions[$mysess['id']] = $mysess; |
| 360 | } |
| 361 | array_reverse($sessions); |
| 362 | |
| 363 | // attach events |
| 364 | $sql = "SELECT s.id, a.text |
| 365 | FROM log_sessions AS s |
| 366 | LEFT JOIN log_events AS e ON(e.session=s.id) |
| 367 | INNER JOIN log_actions AS a ON(a.id=e.action) |
| 368 | $where"; |
| 369 | |
| 370 | $res = XDB::iterator($sql); |
| 371 | while ($event = $res->next()) { |
| 372 | array_push($sessions[$event['id']]['events'], $event['text']); |
| 373 | } |
| 374 | $page->assign_by_ref('sessions', $sessions); |
| 375 | } else { |
| 376 | $page->assign('msg_nofilters', "Sélectionner une année et/ou un utilisateur"); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | $page->changeTpl('admin/logger-view.tpl'); |
| 381 | |
| 382 | $page->setTitle('Administration - Logs des sessions'); |
| 383 | } |
| 384 | |
| 385 | function handler_user($page, $login = false) |
| 386 | { |
| 387 | global $globals; |
| 388 | $page->changeTpl('admin/user.tpl'); |
| 389 | $page->setTitle('Administration - Compte'); |
| 390 | |
| 391 | if (S::suid()) { |
| 392 | $page->kill("Déjà en SUID !!!"); |
| 393 | } |
| 394 | |
| 395 | // Loads the user identity using the environment. |
| 396 | if ($login) { |
| 397 | $user = User::get($login); |
| 398 | } |
| 399 | if (empty($user)) { |
| 400 | pl_redirect('admin/accounts'); |
| 401 | } |
| 402 | |
| 403 | $listClient = new MMList(S::user()); |
| 404 | $login = $user->login(); |
| 405 | $registered = ($user->state != 'pending'); |
| 406 | |
| 407 | // Form processing |
| 408 | if (!empty($_POST)) { |
| 409 | S::assert_xsrf_token(); |
| 410 | if (Post::has('uid') && Post::i('uid') != $user->id()) { |
| 411 | $page->kill('Une erreur s\'est produite'); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | // Handles specific requests (AX sync, su, ...). |
| 416 | if(Post::has('log_account')) { |
| 417 | pl_redirect("admin/logger?loguser=$login&year=".date('Y')."&month=".date('m')); |
| 418 | } |
| 419 | |
| 420 | if(Post::has('su_account') && $registered) { |
| 421 | if (!Platal::session()->startSUID($user)) { |
| 422 | $page->trigError('Impossible d\'effectuer un SUID sur ' . $user->login()); |
| 423 | } else { |
| 424 | pl_redirect(""); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // Handles account deletion. |
| 429 | if (Post::has('account_deletion_confirmation')) { |
| 430 | $uid = $user->id(); |
| 431 | $name = $user->fullName(); |
| 432 | $profile = $user->profile(); |
| 433 | if ($profile && Post::b('clear_profile')) { |
| 434 | $user->profile()->clear(); |
| 435 | } |
| 436 | $user->clear(true); |
| 437 | $page->trigSuccess("L'utilisateur $name ($uid) a bien été désinscrit."); |
| 438 | if (Post::b('erase_account')) { |
| 439 | XDB::execute('DELETE FROM accounts |
| 440 | WHERE uid = {?}', |
| 441 | $uid); |
| 442 | $page->trigSuccess("L'utilisateur $name ($uid) a été supprimé de la base de données"); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | // Account Form {{{ |
| 447 | $to_update = array(); |
| 448 | if (Post::has('disable_weak_access')) { |
| 449 | $to_update['weak_password'] = null; |
| 450 | } else if (Post::has('update_account')) { |
| 451 | if (!$user->hasProfile()) { |
| 452 | if (Post::s('full_name') != $user->fullName()) { |
| 453 | $to_update['full_name'] = Post::s('full_name'); |
| 454 | } |
| 455 | if (Post::s('display_name') != $user->displayName()) { |
| 456 | $to_update['display_name'] = Post::s('display_name'); |
| 457 | } |
| 458 | if (Post::s('directory_name') != $user->directoryName()) { |
| 459 | $to_update['directory_name'] = Post::s('directory_name'); |
| 460 | } |
| 461 | } |
| 462 | if (Post::s('sex') != ($user->isFemale() ? 'female' : 'male')) { |
| 463 | $to_update['sex'] = Post::s('sex'); |
| 464 | if ($user->hasProfile()) { |
| 465 | XDB::execute('UPDATE profiles |
| 466 | SET sex = {?} |
| 467 | WHERE pid = {?}', |
| 468 | Post::s('sex'), $user->profile()->id()); |
| 469 | } |
| 470 | } |
| 471 | if (!Post::blank('pwhash')) { |
| 472 | $to_update['password'] = Post::s('pwhash'); |
| 473 | require_once 'googleapps.inc.php'; |
| 474 | $account = new GoogleAppsAccount($user); |
| 475 | if ($account->active() && $account->sync_password) { |
| 476 | $account->set_password(Post::s('pwhash')); |
| 477 | } |
| 478 | } |
| 479 | if (!Post::blank('weak_password')) { |
| 480 | $to_update['weak_password'] = Post::s('weak_password'); |
| 481 | } |
| 482 | if (Post::i('token_access', 0) != ($user->token_access ? 1 : 0)) { |
| 483 | $to_update['token'] = Post::i('token_access') ? rand_url_id(16) : null; |
| 484 | } |
| 485 | if (Post::i('skin') != $user->skin) { |
| 486 | $to_update['skin'] = Post::i('skin'); |
| 487 | if ($to_update['skin'] == 0) { |
| 488 | $to_update['skin'] = null; |
| 489 | } |
| 490 | } |
| 491 | if (Post::s('state') != $user->state) { |
| 492 | $to_update['state'] = Post::s('state'); |
| 493 | } |
| 494 | if (Post::i('is_admin', 0) != ($user->is_admin ? 1 : 0)) { |
| 495 | $to_update['is_admin'] = Post::b('is_admin'); |
| 496 | } |
| 497 | if (Post::s('type') != $user->type) { |
| 498 | $to_update['type'] = Post::s('type'); |
| 499 | } |
| 500 | if (Post::i('watch', 0) != ($user->watch ? 1 : 0)) { |
| 501 | $to_update['flags'] = new PlFlagset(); |
| 502 | $to_update['flags']->addFlag('watch', Post::i('watch')); |
| 503 | } |
| 504 | if (Post::t('comment') != $user->comment) { |
| 505 | $to_update['comment'] = Post::blank('comment') ? null : Post::t('comment'); |
| 506 | } |
| 507 | if (!$user->checkPerms(User::PERM_MAIL) && Post::t('email') != $user->forlifeEmail()) { |
| 508 | $to_update['email'] = Post::t('email'); |
| 509 | $listClient->change_user_email($user->forlifeEmail(), Post::t('email')); |
| 510 | } |
| 511 | } |
| 512 | if (!empty($to_update)) { |
| 513 | $res = XDB::query('SELECT * |
| 514 | FROM accounts |
| 515 | WHERE uid = {?}', $user->id()); |
| 516 | $oldValues = $res->fetchAllAssoc(); |
| 517 | $oldValues = $oldValues[0]; |
| 518 | |
| 519 | $set = array(); |
| 520 | $diff = array(); |
| 521 | foreach ($to_update as $k => $value) { |
| 522 | $value = XDB::format('{?}', $value); |
| 523 | $set[] = $k . ' = ' . $value; |
| 524 | $diff[$k] = array($oldValues[$k], trim($value, "'")); |
| 525 | unset($oldValues[$k]); |
| 526 | } |
| 527 | XDB::rawExecute('UPDATE accounts |
| 528 | SET ' . implode(', ', $set) . ' |
| 529 | WHERE uid = ' . XDB::format('{?}', $user->id())); |
| 530 | $page->trigSuccess('Données du compte mise à jour avec succès'); |
| 531 | $user = User::getWithUID($user->id()); |
| 532 | |
| 533 | /* Formats the $diff and send it to the site administrators. The rules are the folowing: |
| 534 | * -formats: password, token, weak_password |
| 535 | */ |
| 536 | foreach (array('password', 'token', 'weak_password') as $key) { |
| 537 | if (isset($diff[$key])) { |
| 538 | $diff[$key] = array('old value', 'new value'); |
| 539 | } else { |
| 540 | $oldValues[$key] = 'old value'; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | $mail = new PlMailer('admin/useredit.mail.tpl'); |
| 545 | $mail->assign('admin', S::user()->hruid); |
| 546 | $mail->assign('hruid', $user->hruid); |
| 547 | $mail->assign('diff', $diff); |
| 548 | $mail->assign('oldValues', $oldValues); |
| 549 | $mail->send(); |
| 550 | } |
| 551 | // }}} |
| 552 | |
| 553 | // Profile form {{{ |
| 554 | if (Post::has('add_profile') || Post::has('del_profile') || Post::has('owner')) { |
| 555 | if (Post::i('del_profile', 0) != 0) { |
| 556 | XDB::execute('DELETE FROM account_profiles |
| 557 | WHERE uid = {?} AND pid = {?}', |
| 558 | $user->id(), Post::i('del_profile')); |
| 559 | } else if (!Post::blank('new_profile')) { |
| 560 | $profile = Profile::get(Post::t('new_profile')); |
| 561 | if (!$profile) { |
| 562 | $page->trigError('Le profil ' . Post::t('new_profile') . ' n\'existe pas'); |
| 563 | } else { |
| 564 | XDB::execute('INSERT IGNORE INTO account_profiles (uid, pid) |
| 565 | VALUES ({?}, {?})', |
| 566 | $user->id(), $profile->id()); |
| 567 | } |
| 568 | } |
| 569 | XDB::execute('UPDATE account_profiles |
| 570 | SET perms = IF(pid = {?}, CONCAT(perms, \',owner\'), REPLACE(perms, \'owner\', \'\')) |
| 571 | WHERE uid = {?}', |
| 572 | Post::i('owner'), $user->id()); |
| 573 | } |
| 574 | // }}} |
| 575 | |
| 576 | // Email forwards form {{{ |
| 577 | require_once("emails.inc.php"); |
| 578 | $redirect = ($registered ? new Redirect($user) : null); |
| 579 | if (Post::has('add_fwd')) { |
| 580 | $email = Post::t('email'); |
| 581 | if (!isvalid_email_redirection($email)) { |
| 582 | $page->trigError("Email non valide: $email"); |
| 583 | } else { |
| 584 | $redirect->add_email($email); |
| 585 | $page->trigSuccess("Ajout de $email effectué"); |
| 586 | } |
| 587 | } else if (!Post::blank('del_fwd')) { |
| 588 | $redirect->delete_email(Post::t('del_fwd')); |
| 589 | } else if (!Post::blank('activate_fwd')) { |
| 590 | $redirect->modify_one_email(Post::t('activate_fwd'), true); |
| 591 | } else if (!Post::blank('deactivate_fwd')) { |
| 592 | $redirect->modify_one_email(Post::t('deactivate_fwd'), false); |
| 593 | } else if (Post::has('disable_fwd')) { |
| 594 | $redirect->disable(); |
| 595 | } else if (Post::has('enable_fwd')) { |
| 596 | $redirect->enable(); |
| 597 | } else if (!Post::blank('clean_fwd')) { |
| 598 | $redirect->clean_errors(Post::t('clean_fwd')); |
| 599 | } |
| 600 | // }}} |
| 601 | |
| 602 | // Email alias form {{{ |
| 603 | if (Post::has('add_alias')) { |
| 604 | // Splits new alias in user and fqdn. |
| 605 | $alias = Env::t('email'); |
| 606 | if (strpos($alias, '@') !== false) { |
| 607 | list($alias, $domain) = explode('@', $alias); |
| 608 | } else { |
| 609 | $domain = $user->mainEmailDomain(); |
| 610 | } |
| 611 | |
| 612 | // Checks for alias' user validity. |
| 613 | if (!preg_match('/[-a-z0-9\.]+/s', $alias)) { |
| 614 | $page->trigError("'$alias' n'est pas un alias valide"); |
| 615 | } |
| 616 | |
| 617 | // Eventually adds the alias to the right domain. |
| 618 | if ($domain == $globals->mail->alias_dom || $domain == $globals->mail->alias_dom2) { |
| 619 | $req = new AliasReq($user, $alias, 'Admin request', false); |
| 620 | if ($req->commit()) { |
| 621 | $page->trigSuccess("Nouvel alias '$alias@$domain' attribué."); |
| 622 | } else { |
| 623 | $page->trigError("Impossible d'ajouter l'alias '$alias@$domain', il est probablement déjà attribué."); |
| 624 | } |
| 625 | } elseif ($domain == $user->mainEmailDomain()) { |
| 626 | XDB::execute('INSERT INTO email_source_account (email, uid, domain, type, flags) |
| 627 | SELECT {?}, {?}, id, \'alias\', \'\' |
| 628 | FROM email_virtual_domains |
| 629 | WHERE name = {?}', |
| 630 | $alias, $user->id(), $domain); |
| 631 | $page->trigSuccess("Nouvel alias '$alias' ajouté"); |
| 632 | } else { |
| 633 | $page->trigError("Le domaine '$domain' n'est pas valide pour cet utilisateur."); |
| 634 | } |
| 635 | } else if (!Post::blank('del_alias')) { |
| 636 | $delete_alias = Post::t('del_alias'); |
| 637 | list($email, $domain) = explode('@', $delete_alias); |
| 638 | XDB::execute('DELETE s |
| 639 | FROM email_source_account AS s |
| 640 | INNER JOIN email_virtual_domains AS m ON (s.domain = m.id) |
| 641 | INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id) |
| 642 | WHERE s.email = {?} AND s.uid = {?} AND d.name = {?} AND type != \'forlife\'', |
| 643 | $email, $user->id(), $domain); |
| 644 | XDB::execute('UPDATE email_redirect_account AS r |
| 645 | INNER JOIN email_virtual_domains AS m ON (m.name = {?}) |
| 646 | INNER JOIN email_virtual_domains AS d ON (d.aliasing = m.id) |
| 647 | SET r.rewrite = \'\' |
| 648 | WHERE r.uid = {?} AND r.rewrite = CONCAT({?}, \'@\', d.name)', |
| 649 | $domain, $user->id(), $email); |
| 650 | fix_bestalias($user); |
| 651 | $page->trigSuccess("L'alias '$delete_alias' a été supprimé"); |
| 652 | } else if (!Post::blank('best')) { |
| 653 | $best_alias = Post::t('best'); |
| 654 | // First delete the bestalias flag from all this user's emails. |
| 655 | XDB::execute("UPDATE email_source_account |
| 656 | SET flags = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', flags, ','), ',bestalias,', ',')) |
| 657 | WHERE uid = {?}", $user->id()); |
| 658 | // Then gives the bestalias flag to the given email. |
| 659 | list($email, $domain) = explode('@', $best_alias); |
| 660 | XDB::execute("UPDATE email_source_account |
| 661 | SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'bestalias') |
| 662 | WHERE uid = {?} AND email = {?}", $user->id(), $email); |
| 663 | |
| 664 | // As having a non-null bestalias value is critical in |
| 665 | // plat/al's code, we do an a posteriori check on the |
| 666 | // validity of the bestalias. |
| 667 | fix_bestalias($user); |
| 668 | } |
| 669 | // }}} |
| 670 | |
| 671 | // OpenId form {{{ |
| 672 | if (Post::has('del_openid')) { |
| 673 | XDB::execute('DELETE FROM account_auth_openid |
| 674 | WHERE id = {?}', Post::i('del_openid')); |
| 675 | } |
| 676 | // }}} |
| 677 | |
| 678 | // Forum form {{{ |
| 679 | if (Post::has('b_edit')) { |
| 680 | XDB::execute("DELETE FROM forum_innd |
| 681 | WHERE uid = {?}", $user->id()); |
| 682 | if (Env::v('write_perm') != "" || Env::v('read_perm') != "" || Env::v('commentaire') != "" ) { |
| 683 | XDB::execute("INSERT INTO forum_innd |
| 684 | SET ipmin = '0', ipmax = '4294967295', |
| 685 | write_perm = {?}, read_perm = {?}, |
| 686 | comment = {?}, priority = '200', uid = {?}", |
| 687 | Env::v('write_perm'), Env::v('read_perm'), Env::v('comment'), $user->id()); |
| 688 | } |
| 689 | } |
| 690 | // }}} |
| 691 | |
| 692 | |
| 693 | $page->addJsLink('jquery.ui.xorg.js'); |
| 694 | |
| 695 | // Displays last login and last host information. |
| 696 | $res = XDB::query("SELECT start, host |
| 697 | FROM log_sessions |
| 698 | WHERE uid = {?} AND suid = 0 |
| 699 | ORDER BY start DESC |
| 700 | LIMIT 1", $user->id()); |
| 701 | list($lastlogin,$host) = $res->fetchOneRow(); |
| 702 | $page->assign('lastlogin', $lastlogin); |
| 703 | $page->assign('host', $host); |
| 704 | |
| 705 | // Display mailing lists |
| 706 | $page->assign('mlists', $listClient->get_all_user_lists($user->forlifeEmail())); |
| 707 | |
| 708 | // Display active aliases. |
| 709 | $page->assign('virtuals', $user->emailGroupAliases()); |
| 710 | $aliases = XDB::iterator("SELECT CONCAT(s.email, '@', d.name) AS email, (s.type = 'forlife') AS forlife, |
| 711 | (s.email REGEXP '\\\\.[0-9]{2}$') AS hundred_year, |
| 712 | FIND_IN_SET('bestalias', s.flags) AS bestalias, s.expire, |
| 713 | (s.type = 'alias_aux') AS alias |
| 714 | FROM email_source_account AS s |
| 715 | INNER JOIN email_virtual_domains AS d ON (s.domain = d.id) |
| 716 | WHERE s.uid = {?} |
| 717 | ORDER BY !alias, s.email", |
| 718 | $user->id()); |
| 719 | $page->assign('aliases', $aliases); |
| 720 | $page->assign('account_types', XDB::iterator('SELECT * FROM account_types ORDER BY type')); |
| 721 | $page->assign('skins', XDB::iterator('SELECT id, name FROM skins ORDER BY name')); |
| 722 | $page->assign('profiles', XDB::iterator('SELECT p.pid, p.hrpid, FIND_IN_SET(\'owner\', ap.perms) AS owner |
| 723 | FROM account_profiles AS ap |
| 724 | INNER JOIN profiles AS p ON (ap.pid = p.pid) |
| 725 | WHERE ap.uid = {?}', $user->id())); |
| 726 | $page->assign('openid', XDB::iterator('SELECT id, url |
| 727 | FROM account_auth_openid |
| 728 | WHERE uid = {?}', $user->id())); |
| 729 | |
| 730 | // Displays email redirection and the general profile. |
| 731 | if ($registered && $redirect) { |
| 732 | $page->assign('emails', $redirect->emails); |
| 733 | } |
| 734 | |
| 735 | $page->assign('user', $user); |
| 736 | $page->assign('hasProfile', $user->hasProfile()); |
| 737 | |
| 738 | // Displays forum bans. |
| 739 | $res = XDB::query("SELECT write_perm, read_perm, comment |
| 740 | FROM forum_innd |
| 741 | WHERE uid = {?}", $user->id()); |
| 742 | $bans = $res->fetchOneAssoc(); |
| 743 | $page->assign('bans', $bans); |
| 744 | } |
| 745 | |
| 746 | private static function getHrid($firstname, $lastname, $promo) |
| 747 | { |
| 748 | if ($firstname != null && $lastname != null && $promo != null) { |
| 749 | return User::makeHrid($firstname, $lastname, $promo); |
| 750 | } |
| 751 | return null; |
| 752 | } |
| 753 | |
| 754 | private static function formatNewUser($page, $infosLine, $separator, $promo, $size) |
| 755 | { |
| 756 | $infos = explode($separator, $infosLine); |
| 757 | if (sizeof($infos) > $size || sizeof($infos) < 2) { |
| 758 | $page->trigError("La ligne $infosLine n'a pas été ajoutée."); |
| 759 | return false; |
| 760 | } |
| 761 | |
| 762 | $infos = array_map('trim', $infos); |
| 763 | $hrid = self::getHrid($infos[1], $infos[0], $promo); |
| 764 | $res1 = XDB::query('SELECT COUNT(*) |
| 765 | FROM accounts |
| 766 | WHERE hruid = {?}', $hrid); |
| 767 | $res2 = XDB::query('SELECT COUNT(*) |
| 768 | FROM profiles |
| 769 | WHERE hrpid = {?}', $hrid); |
| 770 | if (is_null($hrid) || $res1->fetchOneCell() > 0 || $res2->fetchOneCell() > 0) { |
| 771 | $page->trigError("La ligne $infosLine n'a pas été ajoutée: une entrée similaire existe déjà"); |
| 772 | return false; |
| 773 | } |
| 774 | $infos['hrid'] = $hrid; |
| 775 | return $infos; |
| 776 | } |
| 777 | |
| 778 | private static function formatSex($page, $sex, $line) |
| 779 | { |
| 780 | switch ($sex) { |
| 781 | case 'F': |
| 782 | return 'female'; |
| 783 | case 'M': |
| 784 | return 'male'; |
| 785 | default: |
| 786 | $page->trigError("La ligne $line n'a pas été ajoutée car le sexe $sex n'est pas pris en compte."); |
| 787 | return null; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | private static function formatBirthDate($birthDate) |
| 792 | { |
| 793 | // strtotime believes dd/mm/yyyy to be an US date (i.e mm/dd/yyyy), and |
| 794 | // dd-mm-yyyy to be a normal date (i.e dd-mm-yyyy)... |
| 795 | return date("Y-m-d", strtotime(str_replace('/', '-', $birthDate))); |
| 796 | } |
| 797 | |
| 798 | function handler_add_accounts($page, $action = null, $promo = null) |
| 799 | { |
| 800 | $page->changeTpl('admin/add_accounts.tpl'); |
| 801 | |
| 802 | if (Env::has('add_type') && Env::has('people')) { |
| 803 | $lines = explode("\n", Env::t('people')); |
| 804 | $separator = Env::t('separator'); |
| 805 | $promotion = Env::i('promotion'); |
| 806 | $nameTypes = DirEnum::getOptions(DirEnum::NAMETYPES); |
| 807 | $nameTypes = array_flip($nameTypes); |
| 808 | |
| 809 | if (Env::t('add_type') == 'promo') { |
| 810 | $eduSchools = DirEnum::getOptions(DirEnum::EDUSCHOOLS); |
| 811 | $eduSchools = array_flip($eduSchools); |
| 812 | $eduDegrees = DirEnum::getOptions(DirEnum::EDUDEGREES); |
| 813 | $eduDegrees = array_flip($eduDegrees); |
| 814 | switch (Env::t('edu_type')) { |
| 815 | case 'X': |
| 816 | $degreeid = $eduDegrees[Profile::DEGREE_X]; |
| 817 | $entry_year = $promotion; |
| 818 | $grad_year = $promotion + 3; |
| 819 | $promo = 'X' . $promotion; |
| 820 | $hrpromo = $promotion; |
| 821 | $type = 'x'; |
| 822 | break; |
| 823 | case 'M': |
| 824 | $degreeid = $eduDegrees[Profile::DEGREE_M]; |
| 825 | $grad_year = $promotion; |
| 826 | $entry_year = $promotion - 2; |
| 827 | $promo = 'M' . $promotion; |
| 828 | $hrpromo = $promo; |
| 829 | $type = 'master'; |
| 830 | break; |
| 831 | case 'D': |
| 832 | $degreeid = $eduDegrees[Profile::DEGREE_D]; |
| 833 | $grad_year = $promotion; |
| 834 | $entry_year = $promotion - 3; |
| 835 | $promo = 'D (en cours)'; |
| 836 | $hrpromo = $promo; |
| 837 | $type = 'phd'; |
| 838 | break; |
| 839 | default: |
| 840 | $page->killError("La formation n'est pas reconnue : " . Env::t('edu_type') . '.'); |
| 841 | } |
| 842 | |
| 843 | XDB::startTransaction(); |
| 844 | foreach ($lines as $line) { |
| 845 | if ($infos = self::formatNewUser($page, $line, $separator, $hrpromo, 6)) { |
| 846 | $sex = self::formatSex($page, $infos[3], $line); |
| 847 | if (!is_null($sex)) { |
| 848 | $fullName = $infos[1] . ' ' . $infos[0]; |
| 849 | $directoryName = $infos[0] . ' ' . $infos[1]; |
| 850 | $birthDate = self::formatBirthDate($infos[2]); |
| 851 | if ($type == 'x') { |
| 852 | $xorgId = Profile::getXorgId($infos[4]); |
| 853 | } elseif (isset($infos[4])) { |
| 854 | $xorgId = trim($infos[4]); |
| 855 | } else { |
| 856 | $xorgId = 0; |
| 857 | } |
| 858 | if (is_null($xorgId)) { |
| 859 | $page->trigError("La ligne $line n'a pas été ajoutée car le matricule École est mal renseigné."); |
| 860 | continue; |
| 861 | } |
| 862 | |
| 863 | XDB::execute('INSERT INTO profiles (hrpid, xorg_id, ax_id, birthdate_ref, sex) |
| 864 | VALUES ({?}, {?}, {?}, {?}, {?})', |
| 865 | $infos['hrid'], $xorgId, (isset($infos[5]) ? $infos[5] : null), $birthDate, $sex); |
| 866 | $pid = XDB::insertId(); |
| 867 | XDB::execute('INSERT INTO profile_name (pid, name, typeid) |
| 868 | VALUES ({?}, {?}, {?}), |
| 869 | ({?}, {?}, {?}), |
| 870 | ({?}, {?}, {?}), |
| 871 | ({?}, {?}, {?})', |
| 872 | $pid, $infos[0], $nameTypes['name_ini'], |
| 873 | $pid, $infos[0], $nameTypes['lastname'], |
| 874 | $pid, $infos[1], $nameTypes['firstname_ini'], |
| 875 | $pid, $infos[1], $nameTypes['firstname']); |
| 876 | XDB::execute('INSERT INTO profile_display (pid, yourself, public_name, private_name, |
| 877 | directory_name, short_name, sort_name, promo) |
| 878 | VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})', |
| 879 | $pid, $infos[1], $fullName, $fullName, $directoryName, $fullName, $directoryName, $promo); |
| 880 | XDB::execute('INSERT INTO profile_education (id, pid, eduid, degreeid, entry_year, grad_year, flags) |
| 881 | VALUES (100, {?}, {?}, {?}, {?}, {?}, \'primary\')', |
| 882 | $pid, $eduSchools[Profile::EDU_X], $degreeid, $entry_year, $grad_year); |
| 883 | XDB::execute('INSERT INTO accounts (hruid, type, is_admin, state, full_name, directory_name, display_name, sex) |
| 884 | VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})', |
| 885 | $infos['hrid'], $type, 0, 'pending', $fullName, $directoryName, $infos[1], $sex); |
| 886 | $uid = XDB::insertId(); |
| 887 | XDB::execute('INSERT INTO account_profiles (uid, pid, perms) |
| 888 | VALUES ({?}, {?}, {?})', |
| 889 | $uid, $pid, 'owner'); |
| 890 | Profile::rebuildSearchTokens($pid, false); |
| 891 | } |
| 892 | } |
| 893 | } |
| 894 | XDB::commit(); |
| 895 | } else if (Env::t('add_type') == 'account') { |
| 896 | $type = Env::t('type'); |
| 897 | $newAccounts = array(); |
| 898 | foreach ($lines as $line) { |
| 899 | if ($infos = self::formatNewUser($page, $line, $separator, $type, 4)) { |
| 900 | $sex = self::formatSex($page, $infos[3], $line); |
| 901 | if (!is_null($sex)) { |
| 902 | $fullName = $infos[1] . ' ' . $infos[0]; |
| 903 | $directoryName = $infos[0] . ' ' . $infos[1]; |
| 904 | XDB::execute('INSERT INTO accounts (hruid, type, is_admin, state, |
| 905 | email, full_name, directory_name, |
| 906 | display_name, sex) |
| 907 | VALUES ({?}, {?}, {?}, {?}, |
| 908 | {?}, {?}, {?}, {?}, {?})', |
| 909 | $infos['hrid'], $type, 0, 'pending', |
| 910 | $infos[2], $fullName, $directoryName, |
| 911 | $infos[1], $sex); |
| 912 | $newAccounts[$infos['hrid']] = $infos[1] . ' ' . $infos[0]; |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | if (!empty($newAccounts)) { |
| 917 | $page->assign('newAccounts', $newAccounts); |
| 918 | } |
| 919 | } else if (Env::t('add_type') == 'ax_id') { |
| 920 | $type = 'x'; |
| 921 | foreach ($lines as $line) { |
| 922 | if ($infos = self::formatNewUser($page, $line, $separator, $promotion, 3)) { |
| 923 | XDB::execute('UPDATE profiles |
| 924 | SET ax_id = {?} |
| 925 | WHERE hrpid = {?}', |
| 926 | $infos[2], $infos['hrid']); |
| 927 | } |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | $errors = $page->nb_errs(); |
| 932 | if ($errors == 0) { |
| 933 | $page->trigSuccess("L'opération a été effectuée avec succès."); |
| 934 | } else { |
| 935 | $page->trigSuccess('L\'opération a été effectuée avec succès, sauf pour ' |
| 936 | . (($errors == 1) ? 'l\'erreur signalée' : "les $errors erreurs signalées") . ' ci-dessus.'); |
| 937 | } |
| 938 | } else if (Env::has('add_type')) { |
| 939 | $res = XDB::query('SELECT type |
| 940 | FROM account_types'); |
| 941 | $page->assign('account_types', $res->fetchColumn()); |
| 942 | $page->assign('add_type', Env::s('add_type')); |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | function handler_homonyms($page, $op = 'list', $target = null) |
| 947 | { |
| 948 | $page->changeTpl('admin/homonymes.tpl'); |
| 949 | $page->setTitle('Administration - Homonymes'); |
| 950 | $this->load("homonyms.inc.php"); |
| 951 | |
| 952 | if ($target) { |
| 953 | $user = User::getSilentWithUID($target); |
| 954 | if (!$user || !($loginbis = select_if_homonym($user))) { |
| 955 | $target = 0; |
| 956 | } else { |
| 957 | $page->assign('user', $user); |
| 958 | $page->assign('loginbis',$loginbis); |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | $page->assign('op', $op); |
| 963 | $page->assign('target', $target); |
| 964 | |
| 965 | // When we have a valid target, prepare emails. |
| 966 | if ($target) { |
| 967 | require_once 'emails.inc.php'; |
| 968 | // Examine what operation needs to be performed. |
| 969 | switch ($op) { |
| 970 | case 'mail': |
| 971 | S::assert_xsrf_token(); |
| 972 | |
| 973 | send_warning_homonyme($user, $loginbis); |
| 974 | fix_bestalias($user); |
| 975 | $op = 'list'; |
| 976 | $page->trigSuccess('Email envoyé à ' . $user->forlifeEmail() . '.'); |
| 977 | break; |
| 978 | |
| 979 | case 'correct': |
| 980 | S::assert_xsrf_token(); |
| 981 | |
| 982 | XDB::execute('DELETE FROM email_source_account |
| 983 | WHERE email = {?} AND type = \'alias\'', |
| 984 | $loginbis); |
| 985 | XDB::execute('INSERT INTO email_source_other (hrmid, email, domain, type, expire) |
| 986 | SELECT {?}, {?}, id, \'homonym\', NOW() |
| 987 | FROM email_virtual_domains |
| 988 | WHERE name = {?}', |
| 989 | User::makeHomonymHrmid($loginbis), $loginbis, $user->mainEmailDomain()); |
| 990 | fix_bestalias($user); |
| 991 | send_robot_homonyme($user, $loginbis); |
| 992 | $op = 'list'; |
| 993 | $page->trigSuccess('Email envoyé à ' . $user->forlifeEmail() . ', alias supprimé.'); |
| 994 | break; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | if ($op == 'list') { |
| 999 | // Retrieves homonyms that are already been fixed. |
| 1000 | $res = XDB::iterator('SELECT o.email AS homonym, f.email AS forlife, o.expire, f.uid |
| 1001 | FROM email_source_other AS o |
| 1002 | INNER JOIN homonyms_list AS h ON (o.hrmid = h.hrmid) |
| 1003 | INNER JOIN email_source_account AS f ON (h.uid = f.uid AND f.type = \'forlife\') |
| 1004 | WHERE o.expire IS NOT NULL |
| 1005 | ORDER BY homonym, forlife'); |
| 1006 | $homonyms = array(); |
| 1007 | while ($item = $res->next()) { |
| 1008 | $homonyms[$item['homonym']][] = $item; |
| 1009 | } |
| 1010 | $page->assign_by_ref('homonyms', $homonyms); |
| 1011 | |
| 1012 | // Retrieves homonyms that needs to be fixed. |
| 1013 | $res = XDB::iterator('SELECT e.email AS homonym, f.email AS forlife, e.expire, e.uid, (e.expire < NOW()) AS urgent |
| 1014 | FROM email_source_account AS e |
| 1015 | INNER JOIN homonyms_list AS l ON (e.uid = l.uid) |
| 1016 | INNER JOIN homonyms_list AS h ON (l.hrmid = h.hrmid) |
| 1017 | INNER JOIN email_source_account AS f ON (h.uid = f.uid AND f.type = \'forlife\') |
| 1018 | WHERE e.expire IS NOT NULL |
| 1019 | ORDER BY homonym, forlife'); |
| 1020 | $homonyms_to_fix = array(); |
| 1021 | while ($item = $res->next()) { |
| 1022 | $homonyms_to_fix[$item['homonym']][] = $item; |
| 1023 | } |
| 1024 | $page->assign_by_ref('homonyms_to_fix', $homonyms_to_fix); |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | function handler_deaths($page, $promo = 0, $validate = false) |
| 1029 | { |
| 1030 | $page->changeTpl('admin/deces_promo.tpl'); |
| 1031 | $page->setTitle('Administration - Deces'); |
| 1032 | |
| 1033 | if (!$promo) { |
| 1034 | $promo = Env::t('promo', 'X1923'); |
| 1035 | } |
| 1036 | $page->assign('promo', $promo); |
| 1037 | if (!$promo) { |
| 1038 | return; |
| 1039 | } |
| 1040 | |
| 1041 | if ($validate) { |
| 1042 | S::assert_xsrf_token(); |
| 1043 | |
| 1044 | $res = XDB::iterRow('SELECT p.pid, pd.directory_name, p.deathdate |
| 1045 | FROM profiles AS p |
| 1046 | INNER JOIN profile_display AS pd ON (p.pid = pd.pid) |
| 1047 | WHERE pd.promo = {?}', $promo); |
| 1048 | while (list($pid, $name, $death) = $res->next()) { |
| 1049 | $val = Env::v('death_' . $pid); |
| 1050 | if ($val == $death) { |
| 1051 | continue; |
| 1052 | } |
| 1053 | |
| 1054 | if (empty($val)) { |
| 1055 | $val = null; |
| 1056 | } |
| 1057 | XDB::execute('UPDATE profiles |
| 1058 | SET deathdate = {?}, deathdate_rec = NOW() |
| 1059 | WHERE pid = {?}', $val, $pid); |
| 1060 | |
| 1061 | $page->trigSuccess('Édition du décès de ' . $name . ' (' . ($val ? $val : 'ressuscité') . ').'); |
| 1062 | if ($val && ($death == '0000-00-00' || empty($death))) { |
| 1063 | $profile = Profile::get($pid); |
| 1064 | $profile->clear(); |
| 1065 | $profile->owner()->clear(false); |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | $res = XDB::iterator('SELECT p.pid, pd.directory_name, p.deathdate |
| 1071 | FROM profiles AS p |
| 1072 | INNER JOIN profile_display AS pd ON (p.pid = pd.pid) |
| 1073 | WHERE pd.promo = {?} |
| 1074 | ORDER BY pd.sort_name', $promo); |
| 1075 | $page->assign('profileList', $res); |
| 1076 | } |
| 1077 | |
| 1078 | function handler_dead_but_active($page) |
| 1079 | { |
| 1080 | $page->changeTpl('admin/dead_but_active.tpl'); |
| 1081 | $page->setTitle('Administration - Décédés'); |
| 1082 | |
| 1083 | $res = XDB::iterator( |
| 1084 | "SELECT a.hruid, pd.promo, p.ax_id, pd.directory_name, p.deathdate, DATE(MAX(s.start)) AS last |
| 1085 | FROM accounts AS a |
| 1086 | INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms)) |
| 1087 | INNER JOIN profiles AS p ON (p.pid = ap.pid) |
| 1088 | INNER JOIN profile_display AS pd ON (pd.pid = p.pid) |
| 1089 | LEFT JOIN log_sessions AS s ON (s.uid = a.uid AND suid = 0) |
| 1090 | WHERE a.state = 'active' AND p.deathdate IS NOT NULL |
| 1091 | GROUP BY a.uid |
| 1092 | ORDER BY pd.promo, pd.sort_name"); |
| 1093 | $page->assign('dead', $res); |
| 1094 | } |
| 1095 | |
| 1096 | function handler_validate($page, $action = 'list', $id = null) |
| 1097 | { |
| 1098 | $page->changeTpl('admin/validation.tpl'); |
| 1099 | $page->setTitle('Administration - Valider une demande'); |
| 1100 | $page->addCssLink('nl.Polytechnique.org.css'); |
| 1101 | |
| 1102 | if ($action == 'edit' && !is_null($id)) { |
| 1103 | $page->assign('preview_id', $id); |
| 1104 | } else { |
| 1105 | $page->assign('preview_id', null); |
| 1106 | } |
| 1107 | |
| 1108 | if(Env::has('uid') && Env::has('type') && Env::has('stamp')) { |
| 1109 | S::assert_xsrf_token(); |
| 1110 | |
| 1111 | $req = Validate::get_typed_request(Env::v('uid'), Env::v('type'), Env::v('stamp')); |
| 1112 | if ($req) { |
| 1113 | $req->handle_formu(); |
| 1114 | } else { |
| 1115 | $page->trigWarning('La validation a déjà été effectuée.'); |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | $r = XDB::iterator('SHOW COLUMNS FROM requests_answers'); |
| 1120 | while (($a = $r->next()) && $a['Field'] != 'category'); |
| 1121 | $page->assign('categories', $categories = explode(',', str_replace("'", '', substr($a['Type'], 5, -1)))); |
| 1122 | |
| 1123 | $hidden = array(); |
| 1124 | $res = XDB::query('SELECT hidden_requests |
| 1125 | FROM requests_hidden |
| 1126 | WHERE uid = {?}', S::v('uid')); |
| 1127 | $hide_requests = $res->fetchOneCell(); |
| 1128 | if (Post::has('hide')) { |
| 1129 | $hide = array(); |
| 1130 | foreach ($categories as $cat) |
| 1131 | if (!Post::v($cat)) { |
| 1132 | $hidden[$cat] = 1; |
| 1133 | $hide[] = $cat; |
| 1134 | } |
| 1135 | $hide_requests = join(',', $hide); |
| 1136 | XDB::query('INSERT INTO requests_hidden (uid, hidden_requests) |
| 1137 | VALUES ({?}, {?}) |
| 1138 | ON DUPLICATE KEY UPDATE hidden_requests = VALUES(hidden_requests)', |
| 1139 | S::v('uid'), $hide_requests); |
| 1140 | } elseif ($hide_requests) { |
| 1141 | foreach (explode(',', $hide_requests) as $hide_type) |
| 1142 | $hidden[$hide_type] = true; |
| 1143 | } |
| 1144 | $page->assign('hide_requests', $hidden); |
| 1145 | |
| 1146 | // Update the count of item to validate here... useful in development configuration |
| 1147 | // where several copies of the site use the same DB, but not the same "dynamic configuration" |
| 1148 | global $globals; |
| 1149 | $globals->updateNbValid(); |
| 1150 | $page->assign('vit', Validate::iterate()); |
| 1151 | $page->assign('isAdmin', S::admin()); |
| 1152 | } |
| 1153 | |
| 1154 | function handler_validate_answers($page, $action = 'list', $id = null) |
| 1155 | { |
| 1156 | $page->setTitle('Administration - Réponses automatiques de validation'); |
| 1157 | $page->assign('title', 'Gestion des réponses automatiques'); |
| 1158 | $table_editor = new PLTableEditor('admin/validate/answers','requests_answers','id'); |
| 1159 | $table_editor->describe('category','catégorie',true); |
| 1160 | $table_editor->describe('title','titre',true); |
| 1161 | $table_editor->describe('answer','texte',false); |
| 1162 | $table_editor->apply($page, $action, $id); |
| 1163 | } |
| 1164 | |
| 1165 | function handler_skins($page, $action = 'list', $id = null) |
| 1166 | { |
| 1167 | $page->setTitle('Administration - Skins'); |
| 1168 | $page->assign('title', 'Gestion des skins'); |
| 1169 | $table_editor = new PLTableEditor('admin/skins','skins','id'); |
| 1170 | $table_editor->describe('name','nom',true); |
| 1171 | $table_editor->describe('skin_tpl','nom du template',true); |
| 1172 | $table_editor->describe('auteur','auteur',false); |
| 1173 | $table_editor->describe('comment','commentaire',true); |
| 1174 | $table_editor->describe('date','date',false); |
| 1175 | $table_editor->describe('ext','extension du screenshot',false); |
| 1176 | $table_editor->apply($page, $action, $id); |
| 1177 | } |
| 1178 | |
| 1179 | function handler_postfix_blacklist($page, $action = 'list', $id = null) |
| 1180 | { |
| 1181 | $page->setTitle('Administration - Postfix : Blacklist'); |
| 1182 | $page->assign('title', 'Blacklist de postfix'); |
| 1183 | $table_editor = new PLTableEditor('admin/postfix/blacklist','postfix_blacklist','email', true); |
| 1184 | $table_editor->describe('reject_text','Texte de rejet',true); |
| 1185 | $table_editor->describe('email','email',true); |
| 1186 | $table_editor->apply($page, $action, $id); |
| 1187 | } |
| 1188 | |
| 1189 | function handler_postfix_whitelist($page, $action = 'list', $id = null) |
| 1190 | { |
| 1191 | $page->setTitle('Administration - Postfix : Whitelist'); |
| 1192 | $page->assign('title', 'Whitelist de postfix'); |
| 1193 | $table_editor = new PLTableEditor('admin/postfix/whitelist','postfix_whitelist','email', true); |
| 1194 | $table_editor->describe('email','email',true); |
| 1195 | $table_editor->apply($page, $action, $id); |
| 1196 | } |
| 1197 | |
| 1198 | function handler_mx_broken($page, $action = 'list', $id = null) |
| 1199 | { |
| 1200 | $page->setTitle('Administration - MX Défaillants'); |
| 1201 | $page->assign('title', 'MX Défaillant'); |
| 1202 | $table_editor = new PLTableEditor('admin/mx/broken', 'mx_watch', 'host', true); |
| 1203 | $table_editor->describe('host', 'Masque', true); |
| 1204 | $table_editor->describe('state', 'Niveau', true); |
| 1205 | $table_editor->describe('text', 'Description du problème', false); |
| 1206 | $table_editor->apply($page, $action, $id); |
| 1207 | } |
| 1208 | |
| 1209 | function handler_logger_actions($page, $action = 'list', $id = null) |
| 1210 | { |
| 1211 | $page->setTitle('Administration - Actions'); |
| 1212 | $page->assign('title', 'Gestion des actions de logger'); |
| 1213 | $table_editor = new PLTableEditor('admin/logger/actions','log_actions','id'); |
| 1214 | $table_editor->describe('text','intitulé',true); |
| 1215 | $table_editor->describe('description','description',true); |
| 1216 | $table_editor->apply($page, $action, $id); |
| 1217 | } |
| 1218 | |
| 1219 | function handler_downtime($page, $action = 'list', $id = null) |
| 1220 | { |
| 1221 | $page->setTitle('Administration - Coupures'); |
| 1222 | $page->assign('title', 'Gestion des coupures'); |
| 1223 | $table_editor = new PLTableEditor('admin/downtime','downtimes','id'); |
| 1224 | $table_editor->describe('debut','date',true); |
| 1225 | $table_editor->describe('duree','durée',false); |
| 1226 | $table_editor->describe('resume','résumé',true); |
| 1227 | $table_editor->describe('services','services affectés',true); |
| 1228 | $table_editor->describe('description','description',false); |
| 1229 | $table_editor->apply($page, $action, $id); |
| 1230 | } |
| 1231 | |
| 1232 | private static function isCountryIncomplete(array &$item) |
| 1233 | { |
| 1234 | $warning = false; |
| 1235 | foreach (array('worldRegion', 'country', 'capital', 'phonePrefix', 'licensePlate', 'countryPlain') as $field) { |
| 1236 | if ($item[$field] == '') { |
| 1237 | $item[$field . '_warning'] = true; |
| 1238 | $warning = true; |
| 1239 | } |
| 1240 | } |
| 1241 | if (is_null($item['belongsTo'])) { |
| 1242 | foreach (array('nationality', 'nationalityEn') as $field) { |
| 1243 | if ($item[$field] == '') { |
| 1244 | $item[$field . '_warning'] = true; |
| 1245 | $warning = true; |
| 1246 | } |
| 1247 | } |
| 1248 | } |
| 1249 | return $warning; |
| 1250 | } |
| 1251 | |
| 1252 | private static function updateCountry(array $item) |
| 1253 | { |
| 1254 | XDB::execute('UPDATE geoloc_countries |
| 1255 | SET countryPlain = {?} |
| 1256 | WHERE iso_3166_1_a2 = {?}', |
| 1257 | mb_strtoupper(replace_accent($item['country'])), $item['iso_3166_1_a2']); |
| 1258 | } |
| 1259 | |
| 1260 | private static function isLanguageIncomplete(array &$item) |
| 1261 | { |
| 1262 | if ($item['language'] == '') { |
| 1263 | $item['language_warning'] = true; |
| 1264 | return true; |
| 1265 | } |
| 1266 | return false; |
| 1267 | } |
| 1268 | |
| 1269 | private static function updateLanguage(array $item) {} |
| 1270 | |
| 1271 | function handler_geocoding($page, $category = null, $action = null, $id = null) |
| 1272 | { |
| 1273 | // Warning, this handler requires the following packages: |
| 1274 | // * pkg-isocodes |
| 1275 | // * isoquery |
| 1276 | |
| 1277 | static $properties = array( |
| 1278 | 'country' => array( |
| 1279 | 'name' => 'pays', |
| 1280 | 'isocode' => '3166', |
| 1281 | 'table' => 'geoloc_countries', |
| 1282 | 'id' => 'iso_3166_1_a2', |
| 1283 | 'main_fields' => array('iso_3166_1_a3', 'iso_3166_1_num', 'countryEn'), |
| 1284 | 'other_fields' => array('worldRegion', 'country', 'capital', 'nationality', 'nationalityEn', |
| 1285 | 'phonePrefix', 'phoneFormat', 'licensePlate', 'belongsTo') |
| 1286 | ), |
| 1287 | 'language' => array( |
| 1288 | 'name' => 'langages', |
| 1289 | 'isocode' => '639', |
| 1290 | 'table' => 'profile_langskill_enum', |
| 1291 | 'id' => 'iso_639_2b', |
| 1292 | 'main_fields' => array('iso_639_2t', 'iso_639_1', 'language_en'), |
| 1293 | 'other_fields' => array('language') |
| 1294 | |
| 1295 | ) |
| 1296 | ); |
| 1297 | |
| 1298 | if (is_null($category) || !array_key_exists($category, $properties)) { |
| 1299 | pl_redirect('admin'); |
| 1300 | } |
| 1301 | |
| 1302 | $data = $properties[$category]; |
| 1303 | |
| 1304 | if ($action == 'edit' || $action == 'add') { |
| 1305 | $main_fields = array_merge(array($data['id']), $data['main_fields']); |
| 1306 | $all_fields = array_merge($main_fields, $data['other_fields']); |
| 1307 | |
| 1308 | if (is_null($id)) { |
| 1309 | if (Post::has('new_id')) { |
| 1310 | $id = Post::v('new_id'); |
| 1311 | } else { |
| 1312 | pl_redirect('admin/geocoding/' . $category); |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | $list = array(); |
| 1317 | exec('isoquery --iso=' . $data['isocode'] . ' ' . $id, $list); |
| 1318 | if (count($list) == 1) { |
| 1319 | $array = explode("\t", $list[0]); |
| 1320 | foreach ($main_fields as $i => $field) { |
| 1321 | $iso[$field] = $array[$i]; |
| 1322 | } |
| 1323 | } else { |
| 1324 | $iso = array(); |
| 1325 | } |
| 1326 | |
| 1327 | if ($action == 'add') { |
| 1328 | if (Post::has('new_id')) { |
| 1329 | S::assert_xsrf_token(); |
| 1330 | } |
| 1331 | |
| 1332 | if (count($iso)) { |
| 1333 | $item = $iso; |
| 1334 | } else { |
| 1335 | $item = array($data['id'] => $id); |
| 1336 | } |
| 1337 | XDB::execute('INSERT INTO ' . $data['table'] . '(' . implode(', ', array_keys($item)) . ') |
| 1338 | VALUES ' . XDB::formatArray($item)); |
| 1339 | $page->trigSuccess($id . ' a bien été ajouté à la base.'); |
| 1340 | } elseif ($action == 'edit') { |
| 1341 | if (Post::has('edit')) { |
| 1342 | S::assert_xsrf_token(); |
| 1343 | |
| 1344 | $item = array(); |
| 1345 | $set = array(); |
| 1346 | foreach ($all_fields as $field) { |
| 1347 | $item[$field] = Post::t($field); |
| 1348 | $set[] = $field . XDB::format(' = {?}', ($item[$field] ? $item[$field] : null)); |
| 1349 | } |
| 1350 | XDB::execute('UPDATE ' . $data['table'] . ' |
| 1351 | SET ' . implode(', ', $set) . ' |
| 1352 | WHERE ' . $data['id'] . ' = {?}', |
| 1353 | $id); |
| 1354 | call_user_func_array(array('self', 'update' . ucfirst($category)), array($item)); |
| 1355 | $page->trigSuccess($id . ' a bien été mis à jour.'); |
| 1356 | } elseif (Post::has('del')) { |
| 1357 | S::assert_xsrf_token(); |
| 1358 | |
| 1359 | XDB::execute('DELETE FROM ' . $data['table'] . ' |
| 1360 | WHERE ' . $data['id'] . ' = {?}', |
| 1361 | $id); |
| 1362 | $page->trigSuccessRedirect($id . ' a bien été supprimé.', 'admin/geocoding/' . $category); |
| 1363 | } else { |
| 1364 | $item = XDB::fetchOneAssoc('SELECT * |
| 1365 | FROM ' . $data['table'] . ' |
| 1366 | WHERE ' . $data['id'] . ' = {?}', |
| 1367 | $id); |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | $page->changeTpl('admin/geocoding_edit.tpl'); |
| 1372 | $page->setTitle('Administration - ' . ucfirst($data['name'])); |
| 1373 | $page->assign('category', $category); |
| 1374 | $page->assign('name', $data['name']); |
| 1375 | $page->assign('all_fields', $all_fields); |
| 1376 | $page->assign('id', $id); |
| 1377 | $page->assign('iso', $iso); |
| 1378 | $page->assign('item', $item); |
| 1379 | return; |
| 1380 | } |
| 1381 | |
| 1382 | $page->changeTpl('admin/geocoding.tpl'); |
| 1383 | $page->setTitle('Administration - ' . ucfirst($data['name'])); |
| 1384 | $page->assign('category', $category); |
| 1385 | $page->assign('name', $data['name']); |
| 1386 | $page->assign('id', $data['id']); |
| 1387 | $page->assign('main_fields', $data['main_fields']); |
| 1388 | $page->assign('all_fields', array_merge($data['main_fields'], $data['other_fields'])); |
| 1389 | |
| 1390 | // First build the list provided by the iso codes. |
| 1391 | $list = array(); |
| 1392 | exec('isoquery --iso=' . $data['isocode'], $list); |
| 1393 | |
| 1394 | foreach ($list as $key => $item) { |
| 1395 | $array = explode("\t", $item); |
| 1396 | unset($list[$key]); |
| 1397 | $list[$array[0]] = array(); |
| 1398 | foreach ($data['main_fields'] as $i => $field) { |
| 1399 | $list[$array[0]][$field] = $array[$i + 1]; |
| 1400 | } |
| 1401 | } |
| 1402 | ksort($list); |
| 1403 | |
| 1404 | // Retrieve all data from the database. |
| 1405 | $db_list = XDB::rawFetchAllAssoc('SELECT * |
| 1406 | FROM ' . $data['table'] . ' |
| 1407 | ORDER BY ' . $data['id'], |
| 1408 | $data['id']); |
| 1409 | |
| 1410 | // Sort both iso and database data into 5 categories: |
| 1411 | // $missing: data from the iso list not in the database, |
| 1412 | // $non_existing: data from the database not in the iso list, |
| 1413 | // $erroneous: data that differ on main fields, |
| 1414 | // $incomplete: data with empty fields in the data base, |
| 1415 | // $remaining: remaining correct and complete data from the database. |
| 1416 | |
| 1417 | $missing = $non_existing = $erroneous = $incomplete = $remaining = array(); |
| 1418 | foreach (array_keys($list) as $id) { |
| 1419 | if (!array_key_exists($id, $db_list)) { |
| 1420 | $missing[$id] = $list[$id]; |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | foreach ($db_list as $id => $item) { |
| 1425 | if (!array_key_exists($id, $list)) { |
| 1426 | $non_existing[$id] = $item; |
| 1427 | } else { |
| 1428 | $error = false; |
| 1429 | foreach ($data['main_fields'] as $field) { |
| 1430 | if ($item[$field] != $list[$id][$field]) { |
| 1431 | $item[$field . '_error'] = true; |
| 1432 | $error = true; |
| 1433 | } |
| 1434 | } |
| 1435 | if ($error == true) { |
| 1436 | $erroneous[$id] = $item; |
| 1437 | } elseif (call_user_func_array(array('self', 'is' . ucfirst($category) . 'Incomplete'), array(&$item))) { |
| 1438 | $incomplete[$id] = $item; |
| 1439 | } else { |
| 1440 | $remaining[$id] = $item; |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | $page->assign('lists', array( |
| 1446 | 'manquant' => $missing, |
| 1447 | 'disparu' => $non_existing, |
| 1448 | 'erroné' => $erroneous, |
| 1449 | 'incomplet' => $incomplete, |
| 1450 | 'restant' => $remaining |
| 1451 | )); |
| 1452 | } |
| 1453 | |
| 1454 | function handler_accounts(PlPage $page) |
| 1455 | { |
| 1456 | $page->changeTpl('admin/accounts.tpl'); |
| 1457 | $page->setTitle('Administration - Comptes'); |
| 1458 | |
| 1459 | if (Post::has('create_account')) { |
| 1460 | S::assert_xsrf_token(); |
| 1461 | $firstname = Post::t('firstname'); |
| 1462 | $lastname = strtoupper(Post::t('lastname')); |
| 1463 | $sex = Post::s('sex'); |
| 1464 | $email = Post::t('email'); |
| 1465 | $type = Post::s('type'); |
| 1466 | $login = PlUser::makeHrid($firstname, $lastname, $type); |
| 1467 | if (!isvalid_email($email)) { |
| 1468 | $page->trigError("Invalid email address: $email"); |
| 1469 | } else if (strlen(Post::s('pwhash')) != 40) { |
| 1470 | $page->trigError("Invalid password hash"); |
| 1471 | } else { |
| 1472 | $full_name = $firstname . ' ' . $lastname; |
| 1473 | $directory_name = $lastname . ' ' . $firstname; |
| 1474 | XDB::execute("INSERT INTO accounts (hruid, type, state, password, |
| 1475 | registration_date, email, full_name, |
| 1476 | display_name, sex, directory_name) |
| 1477 | VALUES ({?}, {?}, 'active', {?}, NOW(), {?}, {?}, {?}, {?}, {?})", |
| 1478 | $login, $type, Post::s('pwhash'), $email, $full_name, $full_name, $sex, |
| 1479 | $directory_name); |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | $uf = new UserFilter(new UFC_AccountType('ax', 'school', 'fx')); |
| 1484 | $page->assign('users', $uf->iterUsers()); |
| 1485 | |
| 1486 | } |
| 1487 | |
| 1488 | function handler_account_types($page, $action = 'list', $id = null) |
| 1489 | { |
| 1490 | $page->setTitle('Administration - Types de comptes'); |
| 1491 | $page->assign('title', 'Gestion des types de comptes'); |
| 1492 | $table_editor = new PLTableEditor('admin/account/types', 'account_types', 'type', true); |
| 1493 | $table_editor->describe('type', 'Catégorie', true); |
| 1494 | $table_editor->describe('perms', 'Permissions associées', true); |
| 1495 | $table_editor->apply($page, $action, $id); |
| 1496 | } |
| 1497 | |
| 1498 | function handler_wiki($page, $action = 'list', $wikipage = null, $wikipage2 = null) |
| 1499 | { |
| 1500 | if (S::hasAuthToken()) { |
| 1501 | $page->setRssLink('Changement Récents', |
| 1502 | '/Site/AllRecentChanges?action=rss&user=' . S::v('hruid') . '&hash=' . S::user()->token); |
| 1503 | } |
| 1504 | |
| 1505 | // update wiki perms |
| 1506 | if ($action == 'update') { |
| 1507 | S::assert_xsrf_token(); |
| 1508 | |
| 1509 | $perms_read = Post::v('read'); |
| 1510 | $perms_edit = Post::v('edit'); |
| 1511 | if ($perms_read || $perms_edit) { |
| 1512 | foreach ($_POST as $wiki_page => $val) { |
| 1513 | if ($val == 'on') { |
| 1514 | $wp = new PlWikiPage(str_replace(array('_', '/'), '.', $wiki_page)); |
| 1515 | if ($wp->setPerms($perms_read ? $perms_read : $wp->readPerms(), |
| 1516 | $perms_edit ? $perms_edit : $wp->writePerms())) { |
| 1517 | $page->trigSuccess("Permission de la page $wiki_page mises à jour"); |
| 1518 | } else { |
| 1519 | $page->trigError("Impossible de mettre les permissions de la page $wiki_page à jour"); |
| 1520 | } |
| 1521 | } |
| 1522 | } |
| 1523 | } |
| 1524 | } else if ($action != 'list' && !empty($wikipage)) { |
| 1525 | $wp = new PlWikiPage($wikipage); |
| 1526 | S::assert_xsrf_token(); |
| 1527 | |
| 1528 | if ($action == 'delete') { |
| 1529 | if ($wp->delete()) { |
| 1530 | $page->trigSuccess("La page ".$wikipage." a été supprimée."); |
| 1531 | } else { |
| 1532 | $page->trigError("Impossible de supprimer la page ".$wikipage."."); |
| 1533 | } |
| 1534 | } else if ($action == 'rename' && !empty($wikipage2) && $wikipage != $wikipage2) { |
| 1535 | if ($changedLinks = $wp->rename($wikipage2)) { |
| 1536 | $s = 'La page <em>'.$wikipage.'</em> a été déplacée en <em>'.$wikipage2.'</em>.'; |
| 1537 | if (is_numeric($changedLinks)) { |
| 1538 | $s .= $changedLinks.' lien'.(($changedLinks>1)?'s ont été modifiés.':' a été modifié.'); |
| 1539 | } |
| 1540 | $page->trigSuccess($s); |
| 1541 | } else { |
| 1542 | $page->trigError("Impossible de déplacer la page ".$wikipage); |
| 1543 | } |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | $perms = PlWikiPage::permOptions(); |
| 1548 | |
| 1549 | // list wiki pages and their perms |
| 1550 | $wiki_pages = PlWikiPage::listPages(); |
| 1551 | ksort($wiki_pages); |
| 1552 | $wiki_tree = array(); |
| 1553 | foreach ($wiki_pages as $file => $desc) { |
| 1554 | list($cat, $name) = explode('.', $file); |
| 1555 | if (!isset($wiki_tree[$cat])) { |
| 1556 | $wiki_tree[$cat] = array(); |
| 1557 | } |
| 1558 | $wiki_tree[$cat][$name] = $desc; |
| 1559 | } |
| 1560 | |
| 1561 | $page->changeTpl('admin/wiki.tpl'); |
| 1562 | $page->assign('wiki_pages', $wiki_tree); |
| 1563 | $page->assign('perms_opts', $perms); |
| 1564 | } |
| 1565 | |
| 1566 | function handler_ipwatch($page, $action = 'list', $ip = null) |
| 1567 | { |
| 1568 | $page->changeTpl('admin/ipwatcher.tpl'); |
| 1569 | |
| 1570 | $states = array('safe' => 'Ne pas surveiller', |
| 1571 | 'unsafe' => 'Surveiller les inscriptions', |
| 1572 | 'dangerous' => 'Surveiller tous les accès', |
| 1573 | 'ban' => 'Bannir cette adresse'); |
| 1574 | $page->assign('states', $states); |
| 1575 | |
| 1576 | switch (Post::v('action')) { |
| 1577 | case 'create': |
| 1578 | if (trim(Post::v('ipN')) != '') { |
| 1579 | S::assert_xsrf_token(); |
| 1580 | Xdb::execute('INSERT IGNORE INTO ip_watch (ip, mask, state, detection, last, uid, description) |
| 1581 | VALUES ({?}, {?}, {?}, CURDATE(), NOW(), {?}, {?})', |
| 1582 | ip_to_uint(trim(Post::v('ipN'))), ip_to_uint(trim(Post::v('maskN'))), |
| 1583 | Post::v('stateN'), S::i('uid'), Post::v('descriptionN')); |
| 1584 | }; |
| 1585 | break; |
| 1586 | |
| 1587 | case 'edit': |
| 1588 | S::assert_xsrf_token(); |
| 1589 | Xdb::execute('UPDATE ip_watch |
| 1590 | SET state = {?}, last = NOW(), uid = {?}, description = {?}, mask = {?} |
| 1591 | WHERE ip = {?}', Post::v('stateN'), S::i('uid'), Post::v('descriptionN'), |
| 1592 | ip_to_uint(Post::v('maskN')), ip_to_uint(Post::v('ipN'))); |
| 1593 | break; |
| 1594 | |
| 1595 | default: |
| 1596 | if ($action == 'delete' && !is_null($ip)) { |
| 1597 | S::assert_xsrf_token(); |
| 1598 | Xdb::execute('DELETE FROM ip_watch WHERE ip = {?}', ip_to_uint($ip)); |
| 1599 | } |
| 1600 | } |
| 1601 | if ($action != 'create' && $action != 'edit') { |
| 1602 | $action = 'list'; |
| 1603 | } |
| 1604 | $page->assign('action', $action); |
| 1605 | |
| 1606 | if ($action == 'list') { |
| 1607 | $sql = "SELECT w.ip, IF(s.ip IS NULL, |
| 1608 | IF(w.ip = s2.ip, s2.host, s2.forward_host), |
| 1609 | IF(w.ip = s.ip, s.host, s.forward_host)), |
| 1610 | w.mask, w.detection, w.state, a.hruid |
| 1611 | FROM ip_watch AS w |
| 1612 | LEFT JOIN log_sessions AS s ON (s.ip = w.ip) |
| 1613 | LEFT JOIN log_sessions AS s2 ON (s2.forward_ip = w.ip) |
| 1614 | LEFT JOIN accounts AS a ON (a.uid = s.uid) |
| 1615 | GROUP BY w.ip, a.hruid |
| 1616 | ORDER BY w.state, w.ip, a.hruid"; |
| 1617 | $it = Xdb::iterRow($sql); |
| 1618 | |
| 1619 | $table = array(); |
| 1620 | $props = array(); |
| 1621 | while (list($ip, $host, $mask, $date, $state, $hruid) = $it->next()) { |
| 1622 | $ip = uint_to_ip($ip); |
| 1623 | $mask = uint_to_ip($mask); |
| 1624 | if (count($props) == 0 || $props['ip'] != $ip) { |
| 1625 | if (count($props) > 0) { |
| 1626 | $table[] = $props; |
| 1627 | } |
| 1628 | $props = array('ip' => $ip, |
| 1629 | 'mask' => $mask, |
| 1630 | 'host' => $host, |
| 1631 | 'detection' => $date, |
| 1632 | 'state' => $state, |
| 1633 | 'users' => array($hruid)); |
| 1634 | } else { |
| 1635 | $props['users'][] = $hruid; |
| 1636 | } |
| 1637 | } |
| 1638 | if (count($props) > 0) { |
| 1639 | $table[] = $props; |
| 1640 | } |
| 1641 | $page->assign('table', $table); |
| 1642 | } elseif ($action == 'edit') { |
| 1643 | $sql = "SELECT w.detection, w.state, w.last, w.description, w.mask, |
| 1644 | a1.hruid AS edit, a2.hruid AS hruid, s.host |
| 1645 | FROM ip_watch AS w |
| 1646 | LEFT JOIN accounts AS a1 ON (a1.uid = w.uid) |
| 1647 | LEFT JOIN log_sessions AS s ON (w.ip = s.ip) |
| 1648 | LEFT JOIN accounts AS a2 ON (a2.uid = s.uid) |
| 1649 | WHERE w.ip = {?} |
| 1650 | GROUP BY a2.hruid |
| 1651 | ORDER BY a2.hruid"; |
| 1652 | $it = Xdb::iterRow($sql, ip_to_uint($ip)); |
| 1653 | |
| 1654 | $props = array(); |
| 1655 | while (list($detection, $state, $last, $description, $mask, $edit, $hruid, $host) = $it->next()) { |
| 1656 | if (count($props) == 0) { |
| 1657 | $props = array('ip' => $ip, |
| 1658 | 'mask' => uint_to_ip($mask), |
| 1659 | 'host' => $host, |
| 1660 | 'detection' => $detection, |
| 1661 | 'state' => $state, |
| 1662 | 'last' => $last, |
| 1663 | 'description' => $description, |
| 1664 | 'edit' => $edit, |
| 1665 | 'users' => array($hruid)); |
| 1666 | } else { |
| 1667 | $props['users'][] = $hruid; |
| 1668 | } |
| 1669 | } |
| 1670 | $page->assign('ip', $props); |
| 1671 | } |
| 1672 | } |
| 1673 | |
| 1674 | function handler_icons($page) |
| 1675 | { |
| 1676 | $page->changeTpl('admin/icons.tpl'); |
| 1677 | $dh = opendir('../htdocs/images/icons'); |
| 1678 | if (!$dh) { |
| 1679 | $page->trigError('Dossier des icones introuvables.'); |
| 1680 | } |
| 1681 | $icons = array(); |
| 1682 | while (($file = readdir($dh)) !== false) { |
| 1683 | if (strlen($file) > 4 && substr($file,-4) == '.gif') { |
| 1684 | array_push($icons, substr($file, 0, -4)); |
| 1685 | } |
| 1686 | } |
| 1687 | sort($icons); |
| 1688 | $page->assign('icons', $icons); |
| 1689 | } |
| 1690 | |
| 1691 | function handler_account_watch($page) |
| 1692 | { |
| 1693 | $page->changeTpl('admin/accounts.tpl'); |
| 1694 | $page->assign('disabled', XDB::iterator('SELECT a.hruid, FIND_IN_SET(\'watch\', a.flags) AS watch, |
| 1695 | a.state = \'disabled\' AS disabled, a.comment |
| 1696 | FROM accounts AS a |
| 1697 | WHERE a.state = \'disabled\' OR FIND_IN_SET(\'watch\', a.flags) |
| 1698 | ORDER BY a.hruid')); |
| 1699 | $page->assign('admins', XDB::iterator('SELECT a.hruid |
| 1700 | FROM accounts AS a |
| 1701 | WHERE a.is_admin |
| 1702 | ORDER BY a.hruid')); |
| 1703 | } |
| 1704 | |
| 1705 | function handler_xnet_without_group($page) |
| 1706 | { |
| 1707 | $page->changeTpl('admin/xnet_without_group.tpl'); |
| 1708 | $page->assign('accounts', XDB::iterator('SELECT a.hruid, a.state |
| 1709 | FROM accounts AS a |
| 1710 | LEFT JOIN group_members AS m ON (a.uid = m.uid) |
| 1711 | WHERE a.type = \'xnet\' AND m.uid IS NULL |
| 1712 | ORDER BY a.state, a.hruid')); |
| 1713 | } |
| 1714 | |
| 1715 | function handler_jobs($page, $id = -1) |
| 1716 | { |
| 1717 | $page->changeTpl('admin/jobs.tpl'); |
| 1718 | |
| 1719 | if (Env::has('search')) { |
| 1720 | $res = XDB::query("SELECT id, name, acronym |
| 1721 | FROM profile_job_enum |
| 1722 | WHERE name LIKE CONCAT('%', {?}, '%') OR acronym LIKE CONCAT('%', {?}, '%')", |
| 1723 | Env::t('job'), Env::t('job')); |
| 1724 | |
| 1725 | if ($res->numRows() <= 20) { |
| 1726 | $page->assign('jobs', $res->fetchAllAssoc()); |
| 1727 | } else { |
| 1728 | $page->trigError("Il y a trop d'entreprises correspondant à ton choix. Affine-le !"); |
| 1729 | } |
| 1730 | |
| 1731 | $page->assign('askedJob', Env::v('job')); |
| 1732 | return; |
| 1733 | } |
| 1734 | |
| 1735 | if (Env::has('edit')) { |
| 1736 | S::assert_xsrf_token(); |
| 1737 | $selectedJob = Env::has('selectedJob'); |
| 1738 | |
| 1739 | Phone::deletePhones(0, Phone::LINK_COMPANY, $id); |
| 1740 | Address::deleteAddresses(null, Address::LINK_COMPANY, $id); |
| 1741 | if (Env::has('change')) { |
| 1742 | if (Env::has('newJobId') && Env::i('newJobId') > 0) { |
| 1743 | XDB::execute('UPDATE profile_job |
| 1744 | SET jobid = {?} |
| 1745 | WHERE jobid = {?}', |
| 1746 | Env::i('newJobId'), $id); |
| 1747 | XDB::execute('DELETE FROM profile_job_enum |
| 1748 | WHERE id = {?}', |
| 1749 | $id); |
| 1750 | |
| 1751 | $page->trigSuccess("L'entreprise a bien été remplacée."); |
| 1752 | } else { |
| 1753 | $page->trigError("L'entreprise n'a pas été remplacée car l'identifiant fourni n'est pas valide."); |
| 1754 | } |
| 1755 | } else { |
| 1756 | XDB::execute('UPDATE profile_job_enum |
| 1757 | SET name = {?}, acronym = {?}, url = {?}, email = {?}, |
| 1758 | NAF_code = {?}, AX_code = {?}, holdingid = {?} |
| 1759 | WHERE id = {?}', |
| 1760 | Env::t('name'), Env::t('acronym'), Env::t('url'), Env::t('email'), |
| 1761 | (Env::t('NAF_code') == 0 ? null : Env::t('NAF_code')), |
| 1762 | (Env::i('AX_code') == 0 ? null : Env::t('AX_code')), |
| 1763 | (Env::i('holdingId') == 0 ? null : Env::t('holdingId')), $id); |
| 1764 | |
| 1765 | $phone = new Phone(array('display' => Env::v('tel'), 'link_id' => $id, 'id' => 0, 'type' => 'fixed', |
| 1766 | 'link_type' => Phone::LINK_COMPANY, 'pub' => 'public')); |
| 1767 | $fax = new Phone(array('display' => Env::v('fax'), 'link_id' => $id, 'id' => 1, 'type' => 'fax', |
| 1768 | 'link_type' => Phone::LINK_COMPANY, 'pub' => 'public')); |
| 1769 | $address = new Address(array('jobid' => $id, 'type' => Address::LINK_COMPANY, 'text' => Env::t('address'))); |
| 1770 | $phone->save(); |
| 1771 | $fax->save(); |
| 1772 | $address->save(); |
| 1773 | |
| 1774 | $page->trigSuccess("L'entreprise a bien été mise à jour."); |
| 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | if (!Env::has('change') && $id != -1) { |
| 1779 | $res = XDB::query("SELECT e.id, e.name, e.acronym, e.url, e.email, e.NAF_code, e.AX_code, |
| 1780 | h.id AS holdingId, h.name AS holdingName, h.acronym AS holdingAcronym, |
| 1781 | t.display_tel AS tel, f.display_tel AS fax, a.text AS address |
| 1782 | FROM profile_job_enum AS e |
| 1783 | LEFT JOIN profile_job_enum AS h ON (e.holdingid = h.id) |
| 1784 | LEFT JOIN profile_phones AS t ON (t.pid = e.id AND t.link_type = 'hq' AND t.tel_id = 0) |
| 1785 | LEFT JOIN profile_phones AS f ON (f.pid = e.id AND f.link_type = 'hq' AND f.tel_id = 1) |
| 1786 | LEFT JOIN profile_addresses AS a ON (a.jobid = e.id AND a.type = 'hq') |
| 1787 | WHERE e.id = {?}", |
| 1788 | $id); |
| 1789 | |
| 1790 | if ($res->numRows() == 0) { |
| 1791 | $page->trigError('Auncune entreprise ne correspond à cet identifiant.'); |
| 1792 | } else { |
| 1793 | $page->assign('selectedJob', $res->fetchOneAssoc()); |
| 1794 | } |
| 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | function handler_profile($page) |
| 1799 | { |
| 1800 | $page->changeTpl('admin/profile.tpl'); |
| 1801 | |
| 1802 | if (Post::has('checked')) { |
| 1803 | S::assert_xsrf_token(); |
| 1804 | $res = XDB::iterator('SELECT DISTINCT(pm.pid), pd.public_name |
| 1805 | FROM profile_modifications AS pm |
| 1806 | INNER JOIN profile_display AS pd ON (pm.pid = pd.pid) |
| 1807 | WHERE pm.type = \'self\''); |
| 1808 | |
| 1809 | while ($profile = $res->next()) { |
| 1810 | if (Post::has('checked_' . $profile['pid'])) { |
| 1811 | XDB::execute('DELETE FROM profile_modifications |
| 1812 | WHERE type = \'self\' AND pid = {?}', $profile['pid']); |
| 1813 | |
| 1814 | $page->trigSuccess('Profil de ' . $profile['public_name'] . ' vérifié.'); |
| 1815 | } |
| 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | $res = XDB::iterator('SELECT p.hrpid, pm.pid, pd.directory_name, GROUP_CONCAT(pm.field SEPARATOR \', \') AS field |
| 1820 | FROM profile_modifications AS pm |
| 1821 | INNER JOIN profiles AS p ON (pm.pid = p.pid) |
| 1822 | INNER JOIN profile_display AS pd ON (pm.pid = pd.pid) |
| 1823 | WHERE pm.type = \'self\' |
| 1824 | GROUP BY pd.directory_name |
| 1825 | ORDER BY pd.directory_name'); |
| 1826 | $page->assign('updates', $res); |
| 1827 | } |
| 1828 | |
| 1829 | function handler_phd($page, $promo = null, $validate = false) |
| 1830 | { |
| 1831 | $page->changeTpl('admin/phd.tpl'); |
| 1832 | $eduDegrees = DirEnum::getOptions(DirEnum::EDUDEGREES); |
| 1833 | $eduDegrees = array_flip($eduDegrees); |
| 1834 | |
| 1835 | if (is_null($promo)) { |
| 1836 | $promo_list = XDB::fetchColumn('SELECT DISTINCT(grad_year) |
| 1837 | FROM profile_education |
| 1838 | WHERE FIND_IN_SET(\'primary\', flags) AND NOT FIND_IN_SET(\'completed\', flags) AND degreeid = {?} |
| 1839 | ORDER BY grad_year', |
| 1840 | $eduDegrees[Profile::DEGREE_D]); |
| 1841 | $page->assign('promo_list', $promo_list); |
| 1842 | $page->assign('nothing', count($promo_list) == 0); |
| 1843 | return; |
| 1844 | } |
| 1845 | |
| 1846 | if ($validate) { |
| 1847 | S::assert_xsrf_token(); |
| 1848 | |
| 1849 | $list = XDB::iterator('SELECT pe.pid, pd.directory_name |
| 1850 | FROM profile_education AS pe |
| 1851 | INNER JOIN profile_display AS pd ON (pe.pid = pd.pid) |
| 1852 | WHERE FIND_IN_SET(\'primary\', pe.flags) AND NOT FIND_IN_SET(\'completed\', pe.flags) |
| 1853 | AND pe.degreeid = {?} AND pe.grad_year = {?}', |
| 1854 | $eduDegrees[Profile::DEGREE_D], $promo); |
| 1855 | while ($res = $list->next()) { |
| 1856 | $pid = $res['pid']; |
| 1857 | $name = $res['directory_name']; |
| 1858 | if (Post::b('completed_' . $pid)) { |
| 1859 | $grad_year = Post::t('grad_year_' . $pid); |
| 1860 | XDB::execute('UPDATE profile_education |
| 1861 | SET flags = \'primary,completed\', grad_year = {?} |
| 1862 | WHERE FIND_IN_SET(\'primary\', flags) AND pid = {?}', |
| 1863 | $grad_year, $pid); |
| 1864 | XDB::execute('UPDATE profile_display |
| 1865 | SET promo = {?} |
| 1866 | WHERE pid = {?}', |
| 1867 | 'D' . $grad_year, $pid); |
| 1868 | $page->trigSuccess("Promotion de $name validée."); |
| 1869 | } |
| 1870 | } |
| 1871 | } |
| 1872 | |
| 1873 | $list = XDB::iterator('SELECT pe.pid, pd.directory_name |
| 1874 | FROM profile_education AS pe |
| 1875 | INNER JOIN profile_display AS pd ON (pe.pid = pd.pid) |
| 1876 | WHERE FIND_IN_SET(\'primary\', pe.flags) AND NOT FIND_IN_SET(\'completed\', pe.flags) |
| 1877 | AND pe.degreeid = {?} AND pe.grad_year = {?} |
| 1878 | ORDER BY pd.directory_name', |
| 1879 | $eduDegrees[Profile::DEGREE_D], $promo); |
| 1880 | $page->assign('list', $list); |
| 1881 | $page->assign('promo', $promo); |
| 1882 | } |
| 1883 | } |
| 1884 | |
| 1885 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 1886 | ?> |