| 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 | class ProfileModule extends PLModule |
| 23 | { |
| 24 | function handlers() |
| 25 | { |
| 26 | return array( |
| 27 | 'photo' => $this->make_hook('photo', AUTH_PUBLIC), |
| 28 | 'photo/change' => $this->make_hook('photo_change', AUTH_MDP), |
| 29 | |
| 30 | 'profile/orange' => $this->make_hook('p_orange', AUTH_MDP), |
| 31 | 'profile/usage' => $this->make_hook('p_usage', AUTH_MDP), |
| 32 | |
| 33 | 'trombi' => $this->make_hook('trombi', AUTH_COOKIE), |
| 34 | |
| 35 | 'vcard' => $this->make_hook('vcard', AUTH_COOKIE), |
| 36 | ); |
| 37 | } |
| 38 | |
| 39 | function _trombi_getlist($offset, $limit) |
| 40 | { |
| 41 | global $globals; |
| 42 | |
| 43 | $where = ( $this->promo > 0 ? "WHERE promo='{$this->promo}'" : "" ); |
| 44 | |
| 45 | $res = $globals->xdb->query( |
| 46 | "SELECT COUNT(*) |
| 47 | FROM auth_user_md5 AS u |
| 48 | RIGHT JOIN photo AS p ON u.user_id=p.uid |
| 49 | $where"); |
| 50 | $pnb = $res->fetchOneCell(); |
| 51 | |
| 52 | $res = $globals->xdb->query( |
| 53 | "SELECT promo,user_id,a.alias AS forlife,IF(nom_usage='', nom, nom_usage) AS nom,prenom |
| 54 | FROM photo AS p |
| 55 | INNER JOIN auth_user_md5 AS u ON u.user_id=p.uid |
| 56 | INNER JOIN aliases AS a ON ( u.user_id=a.id AND a.type='a_vie' ) |
| 57 | $where |
| 58 | ORDER BY promo,nom,prenom LIMIT {?}, {?}", $offset*$limit, $limit); |
| 59 | |
| 60 | return array($pnb, $res->fetchAllAssoc()); |
| 61 | } |
| 62 | |
| 63 | function handler_photo(&$page, $x = null, $req = null) |
| 64 | { |
| 65 | if (is_null($x)) { |
| 66 | return PL_NOT_FOUND; |
| 67 | } |
| 68 | |
| 69 | global $globals; |
| 70 | |
| 71 | $res = $globals->xdb->query("SELECT id, pub FROM aliases |
| 72 | LEFT JOIN photo ON(id = uid) |
| 73 | WHERE alias = {?}", $x); |
| 74 | list($uid, $photo_pub) = $res->fetchOneRow(); |
| 75 | |
| 76 | if ($req && logged()) { |
| 77 | include 'validations.inc.php'; |
| 78 | $myphoto = PhotoReq::get_request($uid); |
| 79 | Header('Content-type: image/'.$myphoto->mimetype); |
| 80 | echo $myphoto->data; |
| 81 | } else { |
| 82 | $res = $globals->xdb->query( |
| 83 | "SELECT attachmime, attach |
| 84 | FROM photo |
| 85 | WHERE uid={?}", $uid); |
| 86 | |
| 87 | if ((list($type,$data) = $res->fetchOneRow()) && ($photo_pub == 'public' || logged())) { |
| 88 | Header("Content-type: image/$type"); |
| 89 | echo $data; |
| 90 | } else { |
| 91 | Header('Content-type: image/png'); |
| 92 | echo file_get_contents(dirname(__FILE__).'/../htdocs/images/none.png'); |
| 93 | } |
| 94 | } |
| 95 | exit; |
| 96 | } |
| 97 | |
| 98 | function handler_photo_change(&$page) |
| 99 | { |
| 100 | global $globals; |
| 101 | |
| 102 | $page->changeTpl('trombino.tpl'); |
| 103 | |
| 104 | require_once('validations.inc.php'); |
| 105 | |
| 106 | $trombi_x = '/home/web/trombino/photos'.Session::get('promo') |
| 107 | .'/'.Session::get('forlife').'.jpg'; |
| 108 | |
| 109 | if (Env::has('upload')) { |
| 110 | $file = isset($_FILES['userfile']['tmp_name']) |
| 111 | ? $_FILES['userfile']['tmp_name'] |
| 112 | : Env::get('photo'); |
| 113 | if ($data = file_get_contents($file)) { |
| 114 | if ($myphoto = new PhotoReq(Session::getInt('uid'), $data)) { |
| 115 | $myphoto->submit(); |
| 116 | } |
| 117 | } else { |
| 118 | $page->trig('Fichier inexistant ou vide'); |
| 119 | } |
| 120 | } elseif (Env::has('trombi')) { |
| 121 | $myphoto = new PhotoReq(Session::getInt('uid'), |
| 122 | file_get_contents($trombi_x)); |
| 123 | if ($myphoto) { |
| 124 | $myphoto->commit(); |
| 125 | $myphoto->clean(); |
| 126 | } |
| 127 | } elseif (Env::get('suppr')) { |
| 128 | $globals->xdb->execute('DELETE FROM photo WHERE uid = {?}', |
| 129 | Session::getInt('uid')); |
| 130 | $globals->xdb->execute('DELETE FROM requests |
| 131 | WHERE user_id = {?} AND type="photo"', |
| 132 | Session::getInt('uid')); |
| 133 | } elseif (Env::get('cancel')) { |
| 134 | $sql = $globals->xdb->query('DELETE FROM requests |
| 135 | WHERE user_id={?} AND type="photo"', |
| 136 | Session::getInt('uid')); |
| 137 | } |
| 138 | |
| 139 | $sql = $globals->xdb->query('SELECT COUNT(*) FROM requests |
| 140 | WHERE user_id={?} AND type="photo"', |
| 141 | Session::getInt('uid')); |
| 142 | $page->assign('submited', $sql->fetchOneCell()); |
| 143 | $page->assign('has_trombi_x', file_exists($trombi_x)); |
| 144 | |
| 145 | return PL_OK; |
| 146 | } |
| 147 | |
| 148 | function handler_p_orange(&$page) |
| 149 | { |
| 150 | global $globals; |
| 151 | |
| 152 | $page->changeTpl('orange.tpl'); |
| 153 | |
| 154 | require_once 'validations.inc.php'; |
| 155 | require_once 'xorg.misc.inc.php'; |
| 156 | |
| 157 | $res = $globals->xdb->query( |
| 158 | "SELECT u.promo,u.promo_sortie |
| 159 | FROM auth_user_md5 AS u |
| 160 | WHERE user_id={?}", Session::getInt('uid')); |
| 161 | |
| 162 | list($promo,$promo_sortie_old) = $res->fetchOneRow(); |
| 163 | $page->assign('promo_sortie_old', $promo_sortie_old); |
| 164 | $page->assign('promo', $promo); |
| 165 | |
| 166 | if (!Env::has('promo_sortie')) { |
| 167 | return PL_OK; |
| 168 | } |
| 169 | |
| 170 | $promo_sortie = Env::getInt('promo_sortie'); |
| 171 | |
| 172 | if ($promo_sortie < 1000 || $promo_sortie > 9999) { |
| 173 | $page->trig('L\'année de sortie doit être un nombre de quatre chiffres'); |
| 174 | } |
| 175 | elseif ($promo_sortie < $promo + 3) { |
| 176 | $page->trig('Trop tôt'); |
| 177 | } |
| 178 | elseif ($promo_sortie == $promo_sortie_old) { |
| 179 | $page->trig('Tu appartiens déjà à la promotion correspondante à cette année de sortie.'); |
| 180 | } |
| 181 | elseif ($promo_sortie == $promo + 3) { |
| 182 | $globals->xdb->execute( |
| 183 | "UPDATE auth_user_md5 set promo_sortie={?} |
| 184 | WHERE user_id={?}",$promo_sortie,Session::getInt('uid')); |
| 185 | $page->trig('Ton statut "orange" a été supprimé.'); |
| 186 | $page->assign('promo_sortie_old', $promo_sortie); |
| 187 | } |
| 188 | else { |
| 189 | $page->assign('promo_sortie', $promo_sortie); |
| 190 | |
| 191 | if (Env::has('submit')) { |
| 192 | $myorange = new OrangeReq(Session::getInt('uid'), |
| 193 | $promo_sortie); |
| 194 | $myorange->submit(); |
| 195 | $page->assign('myorange', $myorange); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return PL_OK; |
| 200 | } |
| 201 | |
| 202 | function handler_p_usage(&$page) |
| 203 | { |
| 204 | global $globals; |
| 205 | |
| 206 | $page->changeTpl('nomusage.tpl'); |
| 207 | |
| 208 | require_once 'validations.inc.php'; |
| 209 | require_once 'xorg.misc.inc.php'; |
| 210 | |
| 211 | $res = $globals->xdb->query( |
| 212 | "SELECT u.nom,u.nom_usage,u.flags,e.alias |
| 213 | FROM auth_user_md5 AS u |
| 214 | LEFT JOIN aliases AS e ON(u.user_id = e.id AND FIND_IN_SET('usage',e.flags)) |
| 215 | WHERE user_id={?}", Session::getInt('uid')); |
| 216 | |
| 217 | list($nom,$usage_old,$flags,$alias_old) = $res->fetchOneRow(); |
| 218 | $flags = new flagset($flags); |
| 219 | $page->assign('usage_old', $usage_old); |
| 220 | $page->assign('alias_old', $alias_old); |
| 221 | |
| 222 | $nom_usage = replace_accent(trim(Env::get('nom_usage'))); |
| 223 | $nom_usage = strtoupper($nom_usage); |
| 224 | $page->assign('usage_req', $nom_usage); |
| 225 | |
| 226 | if (Env::has('submit') && ($nom_usage != $usage_old)) { |
| 227 | // on vient de recevoir une requete, differente de l'ancien nom d'usage |
| 228 | if ($nom_usage == $nom) { |
| 229 | $page->assign('same', true); |
| 230 | } else { // le nom de mariage est distinct du nom à l'X |
| 231 | // on calcule l'alias pour l'afficher |
| 232 | $reason = Env::get('reason'); |
| 233 | if ($reason == 'other') { |
| 234 | $reason = Env::get('other_reason'); |
| 235 | } |
| 236 | $myusage = new UsageReq(Session::getInt('uid'), $nom_usage, $reason); |
| 237 | $myusage->submit(); |
| 238 | $page->assign('myusage', $myusage); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return PL_OK; |
| 243 | } |
| 244 | |
| 245 | function handler_trombi(&$page, $promo = null) |
| 246 | { |
| 247 | require_once 'trombi.inc.php'; |
| 248 | |
| 249 | $page->changeTpl('trombipromo.tpl'); |
| 250 | $page->assign('xorg_title','Polytechnique.org - Trombi Promo'); |
| 251 | |
| 252 | if (is_null($promo)) { |
| 253 | return PL_OK; |
| 254 | } |
| 255 | |
| 256 | $this->promo = $promo = intval($promo); |
| 257 | |
| 258 | if ($promo >= 1900 && $promo < intval(date('Y')) |
| 259 | || ($promo == -1 && has_perms())) |
| 260 | { |
| 261 | $trombi = new Trombi(array($this, '_trombi_getlist')); |
| 262 | $trombi->hidePromo(); |
| 263 | $trombi->setAdmin(); |
| 264 | $page->assign_by_ref('trombi', $trombi); |
| 265 | } else { |
| 266 | $page->trig('Promotion incorrecte (saisir au format YYYY). Recommence.'); |
| 267 | } |
| 268 | |
| 269 | return PL_OK; |
| 270 | } |
| 271 | |
| 272 | function format_adr($params, &$smarty) |
| 273 | { |
| 274 | // $adr1, $adr2, $adr3, $postcode, $city, $region, $country |
| 275 | extract($params['adr']); |
| 276 | $adr = $adr1; |
| 277 | $adr = trim("$adr\n$adr2"); |
| 278 | $adr = trim("$adr\n$adr3"); |
| 279 | return quoted_printable_encode(";;$adr;$city;$region;$postcode;$country"); |
| 280 | } |
| 281 | |
| 282 | function handler_vcard(&$page, $x = null) |
| 283 | { |
| 284 | if (is_null($x)) { |
| 285 | return PL_NOT_FOUND; |
| 286 | } |
| 287 | |
| 288 | global $globals; |
| 289 | |
| 290 | if (substr($x, -4) == '.vcf') { |
| 291 | $x = substr($x, 0, strlen($x) - 4); |
| 292 | } |
| 293 | |
| 294 | new_nonhtml_page('vcard.tpl', AUTH_COOKIE); |
| 295 | require_once 'xorg.misc.inc.php'; |
| 296 | require_once 'user.func.inc.php'; |
| 297 | |
| 298 | $page->register_modifier('qp_enc', 'quoted_printable_encode'); |
| 299 | $page->register_function('format_adr', array($this, 'format_adr')); |
| 300 | |
| 301 | $login = get_user_forlife($x); |
| 302 | $user = get_user_details($login); |
| 303 | |
| 304 | // alias virtual |
| 305 | $res = $globals->xdb->query( |
| 306 | "SELECT alias |
| 307 | FROM virtual |
| 308 | INNER JOIN virtual_redirect USING(vid) |
| 309 | INNER JOIN auth_user_quick ON ( user_id = {?} AND emails_alias_pub = 'public' ) |
| 310 | WHERE ( redirect={?} OR redirect={?} ) |
| 311 | AND alias LIKE '%@{$globals->mail->alias_dom}'", |
| 312 | Session::getInt('uid'), |
| 313 | $user['forlife'].'@'.$globals->mail->domain, |
| 314 | $user['forlife'].'@'.$globals->mail->domain2); |
| 315 | |
| 316 | $user['virtualalias'] = $res->fetchOneCell(); |
| 317 | |
| 318 | $page->assign_by_ref('vcard', $user); |
| 319 | |
| 320 | header("Pragma: "); |
| 321 | header("Cache-Control: "); |
| 322 | header("Content-type: text/x-vcard\n"); |
| 323 | header("Content-Transfer-Encoding: Quoted-Printable\n"); |
| 324 | |
| 325 | return PL_OK; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | ?> |