| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2006 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 | function bugize($list) |
| 23 | { |
| 24 | $list = split(',', $list); |
| 25 | $ans = array(); |
| 26 | |
| 27 | foreach ($list as $bug) { |
| 28 | $clean = str_replace('#', '', $bug); |
| 29 | $ans[] = "<a href='http://trackers.polytechnique.org/task/$clean'>$bug</a>"; |
| 30 | } |
| 31 | |
| 32 | return join(',', $ans); |
| 33 | } |
| 34 | |
| 35 | |
| 36 | class PlatalModule extends PLModule |
| 37 | { |
| 38 | function handlers() |
| 39 | { |
| 40 | return array( |
| 41 | 'index' => $this->make_hook('index', AUTH_PUBLIC), |
| 42 | 'cacert.pem' => $this->make_hook('cacert', AUTH_PUBLIC), |
| 43 | 'changelog' => $this->make_hook('changelog', AUTH_PUBLIC), |
| 44 | |
| 45 | // Preferences thingies |
| 46 | 'prefs' => $this->make_hook('prefs', AUTH_COOKIE), |
| 47 | 'prefs/rss' => $this->make_hook('prefs_rss', AUTH_COOKIE), |
| 48 | 'prefs/webredirect' |
| 49 | => $this->make_hook('webredir', AUTH_MDP), |
| 50 | 'prefs/skin' => $this->make_hook('skin', AUTH_COOKIE), |
| 51 | |
| 52 | // password related thingies |
| 53 | 'password' => $this->make_hook('password', AUTH_MDP), |
| 54 | 'tmpPWD' => $this->make_hook('tmpPWD', AUTH_PUBLIC), |
| 55 | 'password/smtp' => $this->make_hook('smtppass', AUTH_MDP), |
| 56 | 'recovery' => $this->make_hook('recovery', AUTH_PUBLIC), |
| 57 | 'exit' => $this->make_hook('exit', AUTH_PUBLIC), |
| 58 | |
| 59 | // happenings related thingies |
| 60 | 'rss' => $this->make_hook('rss', AUTH_PUBLIC), |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | function handler_index(&$page) |
| 65 | { |
| 66 | if (S::logged()) { |
| 67 | pl_redirect('events'); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | function handler_cacert(&$page) |
| 72 | { |
| 73 | $data = file_get_contents('/etc/ssl/xorgCA/cacert.pem'); |
| 74 | header('Content-Type: application/x-x509-ca-cert'); |
| 75 | header('Content-Length: '.strlen($data)); |
| 76 | echo $data; |
| 77 | exit; |
| 78 | } |
| 79 | |
| 80 | function handler_changelog(&$page) |
| 81 | { |
| 82 | $page->changeTpl('changeLog.tpl'); |
| 83 | |
| 84 | $clog = htmlentities(file_get_contents(dirname(__FILE__).'/../ChangeLog')); |
| 85 | $clog = preg_replace('!(#[0-9]+(,[0-9]+)*)!e', 'bugize("\1")', $clog); |
| 86 | $clog = preg_replace('!vim:.*$!', '', $clog); |
| 87 | $page->assign('ChangeLog', $clog); |
| 88 | } |
| 89 | |
| 90 | function __set_rss_state($state) |
| 91 | { |
| 92 | if ($state) { |
| 93 | $_SESSION['core_rss_hash'] = rand_url_id(16); |
| 94 | XDB::execute('UPDATE auth_user_quick |
| 95 | SET core_rss_hash={?} WHERE user_id={?}', |
| 96 | S::v('core_rss_hash'), S::v('uid')); |
| 97 | } else { |
| 98 | XDB::execute('UPDATE auth_user_quick |
| 99 | SET core_rss_hash="" WHERE user_id={?}', |
| 100 | S::v('uid')); |
| 101 | S::kill('core_rss_hash'); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | function handler_prefs(&$page) |
| 106 | { |
| 107 | $page->changeTpl('preferences.tpl'); |
| 108 | $page->assign('xorg_title','Polytechnique.org - Mes préférences'); |
| 109 | |
| 110 | if (Post::has('mail_fmt')) { |
| 111 | $fmt = Post::v('mail_fmt'); |
| 112 | if ($fmt != 'texte') $fmt = 'html'; |
| 113 | XDB::execute("UPDATE auth_user_quick |
| 114 | SET core_mail_fmt = '$fmt' |
| 115 | WHERE user_id = {?}", |
| 116 | S::v('uid')); |
| 117 | $_SESSION['mail_fmt'] = $fmt; |
| 118 | } |
| 119 | |
| 120 | if (Post::has('rss')) { |
| 121 | $this->__set_rss_state(Post::b('rss')); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | function handler_webredir(&$page) |
| 126 | { |
| 127 | $page->changeTpl('webredirect.tpl'); |
| 128 | |
| 129 | $page->assign('xorg_title','Polytechnique.org - Redirection de page WEB'); |
| 130 | |
| 131 | $log =& S::v('log'); |
| 132 | $url = Env::v('url'); |
| 133 | |
| 134 | if (Env::v('submit') == 'Valider' and Env::has('url')) { |
| 135 | XDB::execute('UPDATE auth_user_quick |
| 136 | SET redirecturl = {?} WHERE user_id = {?}', |
| 137 | $url, S::v('uid')); |
| 138 | $log->log('carva_add', 'http://'.Env::v('url')); |
| 139 | $page->trig("Redirection activée vers <a href='http://$url'>$url</a>"); |
| 140 | } elseif (Env::v('submit') == "Supprimer") { |
| 141 | XDB::execute("UPDATE auth_user_quick |
| 142 | SET redirecturl = '' |
| 143 | WHERE user_id = {?}", |
| 144 | S::v('uid')); |
| 145 | $log->log("carva_del", $url); |
| 146 | Post::kill('url'); |
| 147 | $page->trig('Redirection supprimée'); |
| 148 | } |
| 149 | |
| 150 | $res = XDB::query('SELECT redirecturl |
| 151 | FROM auth_user_quick |
| 152 | WHERE user_id = {?}', |
| 153 | S::v('uid')); |
| 154 | $page->assign('carva', $res->fetchOneCell()); |
| 155 | } |
| 156 | |
| 157 | function handler_prefs_rss(&$page) |
| 158 | { |
| 159 | $page->changeTpl('filrss.tpl'); |
| 160 | |
| 161 | $page->assign('goback', Env::v('referer', 'login')); |
| 162 | |
| 163 | if (Env::v('act_rss') == 'Activer') { |
| 164 | $this->__set_rss_state(true); |
| 165 | $page->trig("Ton Fil RSS est activé."); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | function handler_password(&$page) |
| 170 | { |
| 171 | if (Post::has('response2')) { |
| 172 | require_once 'secure_hash.inc.php'; |
| 173 | |
| 174 | $_SESSION['password'] = $password = Post::v('response2'); |
| 175 | |
| 176 | XDB::execute('UPDATE auth_user_md5 |
| 177 | SET password={?} |
| 178 | WHERE user_id={?}', $password, |
| 179 | S::v('uid')); |
| 180 | |
| 181 | $log =& S::v('log'); |
| 182 | $log->log('passwd', ''); |
| 183 | |
| 184 | if (Cookie::v('ORGaccess')) { |
| 185 | setcookie('ORGaccess', hash_encrypt($password), (time()+25920000), '/', '' ,0); |
| 186 | } |
| 187 | |
| 188 | $page->changeTpl('motdepasse.success.tpl'); |
| 189 | $page->run(); |
| 190 | } |
| 191 | |
| 192 | $page->changeTpl('motdepasse.tpl'); |
| 193 | $page->addJsLink('motdepasse.js'); |
| 194 | $page->assign('xorg_title','Polytechnique.org - Mon mot de passe'); |
| 195 | } |
| 196 | |
| 197 | function handler_smtppass(&$page) |
| 198 | { |
| 199 | $page->changeTpl('acces_smtp.tpl'); |
| 200 | $page->assign('xorg_title','Polytechnique.org - Acces SMTP/NNTP'); |
| 201 | |
| 202 | $uid = S::v('uid'); |
| 203 | $pass = Env::v('smtppass1'); |
| 204 | $log = S::v('log'); |
| 205 | |
| 206 | if (Env::v('op') == "Valider" && strlen($pass) >= 6 |
| 207 | && Env::v('smtppass1') == Env::v('smtppass2')) |
| 208 | { |
| 209 | XDB::execute('UPDATE auth_user_md5 SET smtppass = {?} |
| 210 | WHERE user_id = {?}', $pass, $uid); |
| 211 | $page->trig('Mot de passe enregistré'); |
| 212 | $log->log("passwd_ssl"); |
| 213 | } elseif (Env::v('op') == "Supprimer") { |
| 214 | XDB::execute('UPDATE auth_user_md5 SET smtppass = "" |
| 215 | WHERE user_id = {?}', $uid); |
| 216 | $page->trig('Compte SMTP et NNTP supprimé'); |
| 217 | $log->log("passwd_del"); |
| 218 | } |
| 219 | |
| 220 | $res = XDB::query("SELECT IF(smtppass != '', 'actif', '') |
| 221 | FROM auth_user_md5 |
| 222 | WHERE user_id = {?}", $uid); |
| 223 | $page->assign('actif', $res->fetchOneCell()); |
| 224 | } |
| 225 | |
| 226 | function handler_recovery(&$page) |
| 227 | { |
| 228 | global $globals; |
| 229 | |
| 230 | $page->changeTpl('recovery.tpl'); |
| 231 | |
| 232 | if (!Env::has('login') || !Env::has('birth')) { |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | if (!ereg('[0-3][0-9][0-1][0-9][1][9]([0-9]{2})', Env::v('birth'))) { |
| 237 | $page->trig('Date de naissance incorrecte ou incohérente'); |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | $birth = sprintf('%s-%s-%s', |
| 242 | substr(Env::v('birth'), 4, 4), |
| 243 | substr(Env::v('birth'), 2, 2), |
| 244 | substr(Env::v('birth'), 0, 2)); |
| 245 | |
| 246 | $mailorg = strtok(Env::v('login'), '@'); |
| 247 | |
| 248 | // paragraphe rajouté : si la date de naissance dans la base n'existe pas, on l'update |
| 249 | // avec celle fournie ici en espérant que c'est la bonne |
| 250 | |
| 251 | $res = XDB::query( |
| 252 | "SELECT user_id, naissance |
| 253 | FROM auth_user_md5 AS u |
| 254 | INNER JOIN aliases AS a ON (u.user_id=a.id AND type!='homonyme') |
| 255 | WHERE a.alias={?} AND u.perms IN ('admin','user') AND u.deces=0", $mailorg); |
| 256 | list($uid, $naissance) = $res->fetchOneRow(); |
| 257 | |
| 258 | if ($naissance == $birth) { |
| 259 | $page->assign('ok', true); |
| 260 | |
| 261 | $url = rand_url_id(); |
| 262 | XDB::execute('INSERT INTO perte_pass (certificat,uid,created) VALUES ({?},{?},NOW())', $url, $uid); |
| 263 | $res = XDB::query('SELECT email FROM emails WHERE uid = {?} AND NOT FIND_IN_SET("filter", flags)', $uid); |
| 264 | $mails = implode(', ', $res->fetchColumn()); |
| 265 | |
| 266 | require_once "diogenes/diogenes.hermes.inc.php"; |
| 267 | $mymail = new HermesMailer(); |
| 268 | $mymail->setFrom('"Gestion des mots de passe" <support+password@polytechnique.org>'); |
| 269 | $mymail->addTo($mails); |
| 270 | $mymail->setSubject('Ton certificat d\'authentification'); |
| 271 | $mymail->setTxtBody("Visite la page suivante qui expire dans six heures : |
| 272 | {$globals->baseurl}/tmpPWD/$url |
| 273 | |
| 274 | Si en cliquant dessus tu n'y arrives pas, copie intégralement l'adresse dans la barre de ton navigateur. |
| 275 | |
| 276 | -- |
| 277 | Polytechnique.org |
| 278 | \"Le portail des élèves & anciens élèves de l'Ecole polytechnique\"".(Post::v('email') ? " |
| 279 | |
| 280 | Adresse de secours : |
| 281 | ".Post::v('email') : "")." |
| 282 | |
| 283 | Mail envoyé à ".Env::v('login')); |
| 284 | $mymail->send(); |
| 285 | |
| 286 | // on cree un objet logger et on log l'evenement |
| 287 | $logger = $_SESSION['log'] = new CoreLogger($uid); |
| 288 | $logger->log('recovery', $emails); |
| 289 | } else { |
| 290 | $page->trig('Pas de résultat correspondant aux champs entrés dans notre base de données.'); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | function handler_tmpPWD(&$page, $certif = null) |
| 295 | { |
| 296 | XDB::execute('DELETE FROM perte_pass |
| 297 | WHERE DATE_SUB(NOW(), INTERVAL 380 MINUTE) > created'); |
| 298 | |
| 299 | $res = XDB::query('SELECT uid FROM perte_pass WHERE certificat={?}', $certif); |
| 300 | $ligne = $res->fetchOneAssoc(); |
| 301 | if (!$ligne) { |
| 302 | $page->changeTpl('index.tpl'); |
| 303 | $page->kill("Cette adresse n'existe pas ou n'existe plus sur le serveur."); |
| 304 | } |
| 305 | |
| 306 | $uid = $ligne["uid"]; |
| 307 | if (Post::has('response2')) { |
| 308 | $password = Post::v('response2'); |
| 309 | $logger = new CoreLogger($uid); |
| 310 | XDB::query('UPDATE auth_user_md5 SET password={?} |
| 311 | WHERE user_id={?} AND perms IN("admin","user")', |
| 312 | $password, $uid); |
| 313 | XDB::query('DELETE FROM perte_pass WHERE certificat={?}', $certif); |
| 314 | $logger->log("passwd",""); |
| 315 | $page->changeTpl('tmpPWD.success.tpl'); |
| 316 | } else { |
| 317 | $page->changeTpl('motdepasse.tpl'); |
| 318 | $page->addJsLink('motdepasse.js'); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | function handler_skin(&$page) |
| 323 | { |
| 324 | global $globals; |
| 325 | |
| 326 | $page->changeTpl('skins.tpl'); |
| 327 | $page->assign('xorg_title','Polytechnique.org - Skins'); |
| 328 | |
| 329 | if (Env::has('newskin')) { // formulaire soumis, traitons les données envoyées |
| 330 | XDB::execute('UPDATE auth_user_quick |
| 331 | SET skin={?} WHERE user_id={?}', |
| 332 | Env::i('newskin'), S::v('uid')); |
| 333 | S::kill('skin'); |
| 334 | set_skin(); |
| 335 | } |
| 336 | |
| 337 | $res = XDB::query('SELECT id FROM skins WHERE skin_tpl={?}', S::v('skin')); |
| 338 | $page->assign('skin_id', $res->fetchOneCell()); |
| 339 | |
| 340 | $sql = "SELECT s.*,auteur,count(*) AS nb |
| 341 | FROM skins AS s |
| 342 | LEFT JOIN auth_user_quick AS a ON s.id=a.skin |
| 343 | WHERE skin_tpl != '' AND ext != '' |
| 344 | GROUP BY id ORDER BY s.date DESC"; |
| 345 | $page->assign_by_ref('skins', XDB::iterator($sql)); |
| 346 | } |
| 347 | |
| 348 | function handler_exit(&$page, $level = null) |
| 349 | { |
| 350 | if (S::has('suid')) { |
| 351 | if (S::has('suid')) { |
| 352 | $a4l = S::v('forlife'); |
| 353 | $suid = S::v('suid'); |
| 354 | $log = S::v('log'); |
| 355 | $log->log("suid_stop", S::v('forlife') . " by " . $suid['forlife']); |
| 356 | $_SESSION = $suid; |
| 357 | S::kill('suid'); |
| 358 | pl_redirect('admin/utilisateurs.php', 'login='.$a4l); |
| 359 | } else { |
| 360 | pl_redirect('events'); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | if ($level == 'forget' || $level == 'forgetall') { |
| 365 | setcookie('ORGaccess', '', time() - 3600, '/', '', 0); |
| 366 | Cookie::kill('ORGaccess'); |
| 367 | if (isset($_SESSION['log'])) |
| 368 | $_SESSION['log']->log("cookie_off"); |
| 369 | } |
| 370 | |
| 371 | if ($level == 'forgetuid' || $level == 'forgetall') { |
| 372 | setcookie('ORGuid', '', time() - 3600, '/', '', 0); |
| 373 | Cookie::kill('ORGuid'); |
| 374 | setcookie('ORGdomain', '', time() - 3600, '/', '', 0); |
| 375 | Cookie::kill('ORGdomain'); |
| 376 | } |
| 377 | |
| 378 | if (isset($_SESSION['log'])) { |
| 379 | $ref = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; |
| 380 | $_SESSION['log']->log('deconnexion',$ref); |
| 381 | } |
| 382 | |
| 383 | XorgSession::destroy(); |
| 384 | |
| 385 | if (Get::has('redirect')) { |
| 386 | http_redirect(rawurldecode(Get::v('redirect'))); |
| 387 | } else { |
| 388 | $page->changeTpl('exit.tpl'); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | function handler_rss(&$page, $user = null, $hash = null) |
| 393 | { |
| 394 | require_once 'rss.inc.php'; |
| 395 | |
| 396 | $uid = init_rss('rss.tpl', $user, $hash); |
| 397 | |
| 398 | $rss = XDB::iterator( |
| 399 | 'SELECT e.id, e.titre, e.texte, e.creation_date |
| 400 | FROM auth_user_md5 AS u |
| 401 | INNER JOIN evenements AS e ON ( (e.promo_min = 0 || e.promo_min <= u.promo) |
| 402 | AND (e.promo_max = 0 || e.promo_max >= u.promo) ) |
| 403 | WHERE u.user_id = {?} AND FIND_IN_SET(e.flags, "valide") |
| 404 | AND peremption >= NOW()', $uid); |
| 405 | $page->assign('rss', $rss); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | ?> |