Adds XSRF protection to the Profile module.
[platal.git] / modules / profile.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 'fiche.php' => $this->make_hook('fiche', AUTH_PUBLIC),
31 'profile' => $this->make_hook('profile', AUTH_PUBLIC),
32 'profile/private' => $this->make_hook('profile', AUTH_COOKIE),
33 'profile/ax' => $this->make_hook('ax', AUTH_COOKIE, 'admin'),
34 'profile/edit' => $this->make_hook('p_edit', AUTH_MDP),
35 'profile/ajax/address' => $this->make_hook('ajax_address', AUTH_COOKIE, 'user', NO_AUTH),
36 'profile/ajax/tel' => $this->make_hook('ajax_tel', AUTH_COOKIE, 'user', NO_AUTH),
37 'profile/ajax/medal' => $this->make_hook('ajax_medal', AUTH_COOKIE, 'user', NO_AUTH),
38 'profile/ajax/job' => $this->make_hook('ajax_job', AUTH_COOKIE, 'user', NO_AUTH),
39 'profile/ajax/secteur' => $this->make_hook('ajax_secteur', AUTH_COOKIE, 'user', NO_AUTH),
40 'profile/ajax/skill' => $this->make_hook('ajax_skill', AUTH_COOKIE, 'user', NO_AUTH),
41 'javascript/applis.js' => $this->make_hook('applis_js', AUTH_COOKIE),
42 'javascript/grades.js' => $this->make_hook('grades_js', AUTH_COOKIE),
43 'profile/medal' => $this->make_hook('medal', AUTH_PUBLIC),
44 'profile/orange' => $this->make_hook('p_orange', AUTH_MDP),
45 'profile/usage' => $this->make_hook('p_usage', AUTH_MDP),
46
47 'referent' => $this->make_hook('referent', AUTH_COOKIE),
48 'emploi' => $this->make_hook('ref_search', AUTH_COOKIE),
49 'referent/search' => $this->make_hook('ref_search', AUTH_COOKIE),
50 'referent/ssect' => $this->make_hook('ref_sect', AUTH_COOKIE, 'user', NO_AUTH),
51 'referent/country' => $this->make_hook('ref_country', AUTH_COOKIE, 'user', NO_AUTH),
52
53 'groupes-x' => $this->make_hook('xnet', AUTH_COOKIE),
54
55 'vcard' => $this->make_hook('vcard', AUTH_COOKIE, 'user', NO_HTTPS),
56 'admin/binets' => $this->make_hook('admin_binets', AUTH_MDP, 'admin'),
57 'admin/medals' => $this->make_hook('admin_medals', AUTH_MDP, 'admin'),
58 'admin/formations' => $this->make_hook('admin_formations', AUTH_MDP, 'admin'),
59 'admin/sections' => $this->make_hook('admin_sections', AUTH_MDP, 'admin'),
60 'admin/secteurs' => $this->make_hook('admin_secteurs', AUTH_MDP, 'admin'),
61 'admin/trombino' => $this->make_hook('admin_trombino', AUTH_MDP, 'admin'),
62 'admin/ss_secteurs' => $this->make_hook('admin_ss_secteurs', AUTH_MDP, 'admin'),
63 'admin/fonctions' => $this->make_hook('admin_fonctions', AUTH_MDP, 'admin'),
64
65 );
66 }
67
68 /* XXX COMPAT */
69 function handler_fiche(&$page)
70 {
71 return $this->handler_profile($page, Env::v('user'));
72 }
73
74 function handler_photo(&$page, $x = null, $req = null)
75 {
76 if (is_null($x)) {
77 return PL_NOT_FOUND;
78 }
79
80 $res = XDB::query("SELECT id, pub FROM aliases
81 LEFT JOIN photo ON(id = uid)
82 WHERE alias = {?}", $x);
83 list($uid, $photo_pub) = $res->fetchOneRow();
84
85 if ($req && S::logged()) {
86 include 'validations.inc.php';
87 $myphoto = PhotoReq::get_request($uid);
88 Header('Content-type: image/'.$myphoto->mimetype);
89 echo $myphoto->data;
90 } else {
91 $res = XDB::query(
92 "SELECT attachmime, attach
93 FROM photo
94 WHERE uid={?}", $uid);
95
96 if ((list($type, $data) = $res->fetchOneRow())
97 && ($photo_pub == 'public' || S::logged())) {
98 Header("Content-type: image/$type");
99 echo $data;
100 } else {
101 Header('Content-type: image/png');
102 echo file_get_contents(dirname(__FILE__).'/../htdocs/images/none.png');
103 }
104 }
105 exit;
106 }
107
108 function handler_medal(&$page, $mid)
109 {
110 $thumb = ($mid == 'thumb');
111 $mid = $thumb ? @func_get_arg(2) : $mid;
112
113 $res = XDB::query("SELECT img
114 FROM profile_medals
115 WHERE id = {?}",
116 $mid);
117 $img = $thumb ?
118 dirname(__FILE__).'/../htdocs/images/medals/thumb/' . $res->fetchOneCell() :
119 dirname(__FILE__).'/../htdocs/images/medals/' . $res->fetchOneCell();
120 $type = mime_content_type($img);
121 header("Content-Type: $type");
122 echo file_get_contents($img);
123 exit;
124 }
125
126 function handler_photo_change(&$page)
127 {
128 $page->changeTpl('profile/trombino.tpl');
129
130 require_once('validations.inc.php');
131
132 $trombi_x = '/home/web/trombino/photos'.S::v('promo')
133 .'/'.S::v('forlife').'.jpg';
134
135 if (Env::has('upload')) {
136 S::assert_xsrf_token();
137
138 $upload = new PlUpload(S::v('forlife'), 'photo');
139 if (!$upload->upload($_FILES['userfile']) && !$upload->download(Env::v('photo'))) {
140 $page->trigError('Une erreur est survenue lors du téléchargement du fichier');
141 } else {
142 $myphoto = new PhotoReq(S::v('uid'), $upload);
143 if ($myphoto->isValid()) {
144 $myphoto->submit();
145 }
146 }
147 } elseif (Env::has('trombi')) {
148 S::assert_xsrf_token();
149
150 $upload = new PlUpload(S::v('forlife'), 'photo');
151 if ($upload->copyFrom($trombi_x)) {
152 $myphoto = new PhotoReq(S::v('uid'), $upload);
153 if ($myphoto->isValid()) {
154 $myphoto->commit();
155 $myphoto->clean();
156 }
157 }
158 } elseif (Env::v('suppr')) {
159 S::assert_xsrf_token();
160
161 XDB::execute('DELETE FROM photo
162 WHERE uid = {?}',
163 S::v('uid'));
164 XDB::execute('DELETE FROM requests
165 WHERE user_id = {?} AND type="photo"',
166 S::v('uid'));
167 update_NbValid();
168 } elseif (Env::v('cancel')) {
169 S::assert_xsrf_token();
170
171 $sql = XDB::query('DELETE FROM requests
172 WHERE user_id={?} AND type="photo"',
173 S::v('uid'));
174 update_NbValid();
175 }
176
177 $sql = XDB::query('SELECT COUNT(*)
178 FROM requests
179 WHERE user_id={?} AND type="photo"',
180 S::v('uid'));
181 $page->assign('submited', $sql->fetchOneCell());
182 $page->assign('has_trombi_x', file_exists($trombi_x));
183 }
184
185 function handler_profile(&$page, $x = null)
186 {
187 if (is_null($x)) {
188 return PL_NOT_FOUND;
189 }
190
191 global $globals;
192 require_once 'user.func.inc.php';
193
194 $page->changeTpl('profile/profile.tpl', SIMPLE);
195
196 $view = 'private';
197 if (!S::logged() || Env::v('view') == 'public') $view = 'public';
198 if (S::logged() && Env::v('view') == 'ax') $view = 'ax';
199
200 if (is_numeric($x)) {
201 $res = XDB::query(
202 "SELECT alias
203 FROM aliases AS a
204 INNER JOIN auth_user_md5 AS u ON (a.id=u.user_id AND a.type='a_vie')
205 WHERE matricule={?}", $x);
206 $login = $res->fetchOneCell();
207 } else {
208 $login = get_user_forlife($x, S::logged() ? '_default_user_callback'
209 : '_silent_user_callback');
210 }
211
212 if (empty($login)) {
213 $user = get_not_registered_user($x, true);
214 if ($user->total() != 1) {
215 return PL_NOT_FOUND;
216 }
217 $user = $user->next();
218 if (S::logged()) {
219 pl_redirect('marketing/public/' . $user['user_id']);
220 }
221 $user['forlife'] = $x;
222 } else {
223 $new = Env::v('modif') == 'new';
224 $user = get_user_details($login, S::v('uid'), $view);
225 }
226
227 if (S::logged()) {
228 $_SESSION['log']->log('view_profile', $login);
229 }
230
231 $title = $user['prenom'] . ' ' . ( empty($user['nom_usage']) ? $user['nom'] : $user['nom_usage'] );
232 $page->assign('xorg_title', $title);
233
234 // photo
235
236 $photo = 'photo/'.$user['forlife'].($new ? '/req' : '');
237
238 if (!isset($user['y']) and !isset($user['x'])) {
239 list($user['x'], $user['y']) = getimagesize("images/none.png");
240 }
241 if (!isset($user['y']) or $user['y'] < 1) $user['y']=1;
242 if (!isset($user['x']) or $user['x'] < 1) $user['x']=1;
243 if ($user['x'] > 240) {
244 $user['y'] = (integer)($user['y']*240/$user['x']);
245 $user['x'] = 240;
246 }
247 if ($user['y'] > 300) {
248 $user['x'] = (integer)($user['x']*300/$user['y']);
249 $user['y'] = 300;
250 }
251 if ($user['x'] < 160) {
252 $user['y'] = (integer)($user['y']*160/$user['x']);
253 $user['x'] = 160;
254 }
255
256 $page->assign('logged', has_user_right('private', $view));
257 if (!has_user_right($user['photo_pub'], $view)) {
258 $photo = "";
259 }
260
261 $page->assign_by_ref('x', $user);
262 $page->assign('photo_url', $photo);
263 // alias virtual
264 $res = XDB::query(
265 "SELECT alias
266 FROM virtual
267 INNER JOIN virtual_redirect USING(vid)
268 INNER JOIN auth_user_quick ON ( user_id = {?} AND emails_alias_pub = 'public' )
269 WHERE ( redirect={?} OR redirect={?} )
270 AND alias LIKE '%@{$globals->mail->alias_dom}'",
271 $user['user_id'],
272 $user['forlife'].'@'.$globals->mail->domain,
273 $user['forlife'].'@'.$globals->mail->domain2);
274 $page->assign('virtualalias', $res->fetchOneCell());
275 $page->assign('view', $view);
276
277 $page->addJsLink('close_on_esc.js');
278 header('Last-Modified: ' . date('r', strtotime($user['date'])));
279 }
280
281 function handler_ax(&$page, $user = null)
282 {
283 require_once 'user.func.inc.php';
284 $user = get_user_forlife($user);
285 if (!$user) {
286 return PL_NOT_FOUND;
287 }
288 $res = XDB::query('SELECT matricule_ax
289 FROM auth_user_md5 AS u
290 INNER JOIN aliases AS a ON (a.type = "a_vie" AND a.id = u.user_id)
291 WHERE a.alias = {?}', $user);
292 $mat = $res->fetchOneCell();
293 if (!intval($mat)) {
294 $page->kill("Le matricule AX de $user est inconnu");
295 }
296 http_redirect("http://www.polytechniciens.com/?page=AX_FICHE_ANCIEN&anc_id=$mat");
297 }
298
299 function handler_p_edit(&$page, $opened_tab = null, $mode = null)
300 {
301 global $globals;
302
303 // AX Synchronization
304 require_once 'synchro_ax.inc.php';
305 if (is_ax_key_missing()) {
306 $page->assign('no_private_key', true);
307 }
308 if (Env::v('synchro_ax') == 'confirm' && !is_ax_key_missing()) {
309 ax_synchronize(S::v('bestalias'), S::v('uid'));
310 $page->trigSuccess('Ton profil a été synchronisé avec celui du site polytechniciens.com');
311 }
312
313 // Build the page
314 $page->addJsLink('ajax.js');
315 $page->addJsLink('applis.js');
316 $page->addJsLink('grades.js');
317 $page->addJsLink('profile.js');
318 $page->addJsLink('jquery.autocomplete.js');
319 $wiz = new PlWizard('Profil', 'core/plwizard.tpl', true, true);
320 require_once dirname(__FILE__) . '/profile/page.inc.php';
321 $wiz->addPage('ProfileGeneral', 'Général', 'general');
322 $wiz->addPage('ProfileAddresses', 'Adresses personnelles', 'adresses');
323 $wiz->addPage('ProfileGroups', 'Groupes X - Binets', 'poly');
324 $wiz->addPage('ProfileDecos', 'Décorations - Medailles', 'deco');
325 $wiz->addPage('ProfileJobs', 'Informations professionnelles', 'emploi');
326 $wiz->addPage('ProfileSkills', 'Compétences diverses', 'skill');
327 $wiz->addPage('ProfileMentor', 'Mentoring', 'mentor');
328 $wiz->apply($page, 'profile/edit', $opened_tab, $mode);
329
330 // Misc checks
331 $res = XDB::query("SELECT user_id
332 FROM auth_user_md5
333 WHERE user_id = {?} AND naissance = '0000-00-00'", S::i('uid'));
334 if ($res->numRows()) {
335 $page->trigWarning("Ta date de naissance n'est pas renseignée, ce qui t'empêcheras de réaliser"
336 . " la procédure de récupération de mot de passe si un jour tu le perdais");
337 }
338
339 $page->assign('xorg_title', 'Polytechnique.org - Mon Profil');
340 }
341
342 function handler_applis_js(&$page)
343 {
344 header('Content-Type: text/javascript; charset=utf-8');
345 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
346 header('Last-Modified:' . gmdate('D, d M Y H:i:s') . ' GMT');
347 header('Cache-Control: no-cache, must-revalidate');
348 header('Pragma: no-cache');
349 $page->changeTpl('profile/applis.js.tpl', NO_SKIN);
350 require_once "applis.func.inc.php";
351 }
352
353 function handler_grades_js(&$page)
354 {
355 header('Content-Type: text/javascript; charset=utf-8');
356 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
357 header('Last-Modified:' . gmdate('D, d M Y H:i:s') . ' GMT');
358 header('Cache-Control: no-cache, must-revalidate');
359 header('Pragma: no-cache');
360 $page->changeTpl('profile/grades.js.tpl', NO_SKIN);
361 $res = XDB::iterator("SELECT *
362 FROM profile_medals_grades
363 ORDER BY mid, pos");
364 $grades = array();
365 while ($tmp = $res->next()) {
366 $grades[$tmp['mid']][] = $tmp;
367 }
368 $page->assign('grades', $grades);
369
370 $res = XDB::iterator("SELECT *, FIND_IN_SET('validation', flags) AS validate
371 FROM profile_medals
372 ORDER BY type, text");
373 $mlist = array();
374 while ($tmp = $res->next()) {
375 $mlist[$tmp['type']][] = $tmp;
376 }
377 $page->assign('medal_list', $mlist);
378 }
379
380 function handler_ajax_address(&$page, $adid)
381 {
382 header('Content-Type: text/html; charset=utf-8');
383 $page->changeTpl('profile/adresses.address.tpl', NO_SKIN);
384 $page->assign('i', $adid);
385 $page->assign('adr', array());
386 }
387
388 function handler_ajax_tel(&$page, $adid, $telid)
389 {
390 header('Content-Type: text/html; charset=utf-8');
391 $page->changeTpl('profile/adresses.tel.tpl', NO_SKIN);
392 $page->assign('i', $adid);
393 $page->assign('adid', "addresses_$adid");
394 $page->assign('adpref', "addresses[$adid]");
395 $page->assign('t', $telid);
396 $page->assign('tel', array());
397 }
398
399 function handler_ajax_medal(&$page, $id)
400 {
401 header('Content-Type: text/html; charset=utf-8');
402 $page->changeTpl('profile/deco.medal.tpl', NO_SKIN);
403 $page->assign('id', $id);
404 $page->assign('medal', array('valid' => 0, 'grade' => 0));
405 }
406
407 function handler_ajax_job(&$page, $id)
408 {
409 header('Content-Type: text/html; charset=utf-8');
410 $page->changeTpl('profile/jobs.job.tpl', NO_SKIN);
411 $page->assign('i', $id);
412 $page->assign('job', array());
413 $page->assign('new', true);
414 $page->assign('secteurs', XDB::iterator("SELECT id, label
415 FROM emploi_secteur"));
416 $page->assign('fonctions', XDB::iterator("SELECT id, fonction_fr, FIND_IN_SET('titre', flags) AS title
417 FROM fonctions_def
418 ORDER BY id"));
419 }
420
421 function handler_ajax_secteur(&$page, $id, $sect, $ssect = -1)
422 {
423 header('Content-Type: text/html; charset=utf-8');
424 $res = XDB::iterator("SELECT id, label
425 FROM emploi_ss_secteur
426 WHERE secteur = {?}", $sect);
427 $page->changeTpl('profile/jobs.secteur.tpl', NO_SKIN);
428 $page->assign('id', $id);
429 $page->assign('ssecteurs', $res);
430 $page->assign('sel', $ssect);
431 }
432
433 function handler_ajax_skill(&$page, $cat, $id)
434 {
435 header('Content-Type: text/html; charset=utf-8');
436 $page->changeTpl('profile/skill.skill.tpl', NO_SKIN);
437 $page->assign('cat', $cat);
438 $page->assign('id', $id);
439 if ($cat == 'competences') {
440 $page->assign('levels', array('initié' => 'initié',
441 'bonne connaissance' => 'bonne connaissance',
442 'expert' => 'expert'));
443 } else {
444 $page->assign('levels', array(1 => 'connaissance basique',
445 2 => 'maîtrise des bases',
446 3 => 'maîtrise limitée',
447 4 => 'maîtrise générale',
448 5 => 'bonne maîtrise',
449 6 => 'maîtrise complète'));
450 }
451 }
452
453 function handler_p_orange(&$page)
454 {
455 $page->changeTpl('profile/orange.tpl');
456
457 require_once 'validations.inc.php';
458 require_once 'xorg.misc.inc.php';
459
460 $res = XDB::query(
461 "SELECT u.promo, u.promo_sortie
462 FROM auth_user_md5 AS u
463 WHERE user_id={?}", S::v('uid'));
464
465 list($promo, $promo_sortie_old) = $res->fetchOneRow();
466 $page->assign('promo_sortie_old', $promo_sortie_old);
467 $page->assign('promo', $promo);
468
469 if (!Env::has('promo_sortie')) {
470 return;
471 } else {
472 S::assert_xsrf_token();
473 }
474
475 $promo_sortie = Env::i('promo_sortie');
476
477 if ($promo_sortie < 1000 || $promo_sortie > 9999) {
478 $page->trigError('L\'année de sortie doit être un nombre de quatre chiffres');
479 }
480 elseif ($promo_sortie < $promo + 3) {
481 $page->trigError('Trop tôt');
482 }
483 elseif ($promo_sortie == $promo_sortie_old) {
484 $page->trigWarning('Tu appartiens déjà à la promotion correspondante à cette année de sortie.');
485 }
486 elseif ($promo_sortie == $promo + 3) {
487 XDB::execute(
488 "UPDATE auth_user_md5 set promo_sortie={?}
489 WHERE user_id={?}", $promo_sortie, S::v('uid'));
490 $page->trigSuccess('Ton statut "orange" a été supprimé.');
491 $page->assign('promo_sortie_old', $promo_sortie);
492 }
493 else {
494 $page->assign('promo_sortie', $promo_sortie);
495
496 if (Env::has('submit')) {
497 $myorange = new OrangeReq(S::v('uid'),
498 $promo_sortie);
499 $myorange->submit();
500 $page->assign('myorange', $myorange);
501 }
502 }
503 }
504
505 function handler_referent(&$page, $x = null)
506 {
507 require_once 'user.func.inc.php';
508
509 if (is_null($x)) {
510 return PL_NOT_FOUND;
511 }
512
513 $page->changeTpl('profile/fiche_referent.tpl', SIMPLE);
514
515 $res = XDB::query(
516 "SELECT prenom, nom, user_id, promo, cv, a.alias AS bestalias
517 FROM auth_user_md5 AS u
518 INNER JOIN aliases AS a ON (u.user_id=a.id
519 AND FIND_IN_SET('bestalias', a.flags))
520 INNER JOIN aliases AS a1 ON (u.user_id=a1.id
521 AND a1.alias = {?}
522 AND a1.type!='homonyme')", $x);
523
524 if ($res->numRows() != 1) {
525 return PL_NOT_FOUND;
526 }
527
528 list($prenom, $nom, $user_id, $promo, $cv, $bestalias) = $res->fetchOneRow();
529
530 $page->assign('prenom', $prenom);
531 $page->assign('nom', $nom);
532 $page->assign('promo', $promo);
533 $page->assign('cv', MiniWiki::WikiToHTML($cv, true));
534 $page->assign('bestalias', $bestalias);
535 $page->assign('adr_pro', get_user_details_pro($user_id));
536
537 ///// recuperations infos referent
538
539 //expertise
540 $res = XDB::query("SELECT expertise FROM mentor WHERE uid = {?}", $user_id);
541 $page->assign('expertise', $res->fetchOneCell());
542
543 //secteurs
544 $secteurs = $ss_secteurs = Array();
545 $res = XDB::iterRow(
546 "SELECT s.label, ss.label
547 FROM mentor_secteurs AS m
548 LEFT JOIN emploi_secteur AS s ON(m.secteur = s.id)
549 LEFT JOIN emploi_ss_secteur AS ss ON(m.secteur = ss.secteur AND m.ss_secteur = ss.id)
550 WHERE uid = {?}", $user_id);
551 while (list($sec, $ssec) = $res->next()) {
552 $secteurs[] = $sec;
553 $ss_secteurs[] = $ssec;
554 }
555 $page->assign_by_ref('secteurs', $secteurs);
556 $page->assign_by_ref('ss_secteurs', $ss_secteurs);
557
558 //pays
559 $res = XDB::query(
560 "SELECT gp.pays
561 FROM mentor_pays AS m
562 LEFT JOIN geoloc_pays AS gp ON(m.pid = gp.a2)
563 WHERE uid = {?}", $user_id);
564 $page->assign('pays', $res->fetchColumn());
565
566 $page->addJsLink('close_on_esc.js');
567 }
568
569 function handler_ref_search(&$page, $action = null, $subaction = null)
570 {
571 require_once 'wiki.inc.php';
572 wiki_require_page('Docs.Emploi');
573 $page->assign('xorg_title', 'Polytechnique.org - Conseil Pro');
574
575 //recuperation des noms de secteurs
576 $res = XDB::iterRow("SELECT id, label FROM emploi_secteur");
577 $secteurs[''] = '';
578 while (list($tmp_id, $tmp_label) = $res->next()) {
579 $secteurs[$tmp_id] = $tmp_label;
580 }
581 $page->assign_by_ref('secteurs', $secteurs);
582
583 // nb de mentors
584 $res = XDB::query("SELECT count(*) FROM mentor");
585 $page->assign('mentors_number', $res->fetchOneCell());
586
587 // On vient d'un formulaire
588 $where = array();
589 $pays_sel = XDB::escape(Env::v('pays_sel'));
590 $secteur_sel = XDB::escape(Env::v('secteur'));
591 $ss_secteur_sel = XDB::escape(Env::v('ss_secteur'));
592 $expertise_champ = XDB::escape(Env::v('expertise'));
593
594 if ($pays_sel != "''") {
595 $where[] = "mp.pid = $pays_sel";
596 }
597 if ($secteur_sel != "''") {
598 $where[] = "ms.secteur = $secteur_sel";
599 if ($ss_secteur_sel != "''") {
600 $where[] = "ms.ss_secteur = $ss_secteur_sel";
601 }
602 }
603 if ($expertise_champ != "''") {
604 $where[] = "MATCH(m.expertise) AGAINST($expertise_champ)";
605 }
606
607 if ($where) {
608 $where = join(' AND ', $where);
609
610 $set = new UserSet("INNER JOIN mentor AS m ON (m.uid = u.user_id)
611 LEFT JOIN mentor_pays AS mp ON (mp.uid = m.uid)
612 LEFT JOIN mentor_secteurs AS ms ON (ms.uid = m.uid)",
613 $where);
614 $set->addMod('mentor', 'Référents');
615 $set->apply('referent/search', $page, $action, $subaction);
616 if ($set->count() > 100) {
617 $page->assign('recherche_trop_large', true);
618 }
619 }
620 $page->changeTpl('profile/referent.tpl');
621 }
622
623 function handler_ref_sect(&$page, $sect)
624 {
625 header('Content-Type: text/html; charset=utf-8');
626 $page->changeTpl('include/field.select.tpl', NO_SKIN);
627 $page->assign('onchange', 'setSSecteurs()');
628 $page->assign('id', 'ssect_field');
629 $page->assign('name', 'ss_secteur');
630 $it = XDB::iterator("SELECT id,label AS field
631 FROM emploi_ss_secteur
632 WHERE secteur = {?}", $sect);
633 $page->assign('list', $it);
634 }
635
636 function handler_ref_country(&$page, $sect, $ssect = '')
637 {
638 header('Content-Type: text/html; charset=utf-8');
639 $page->changeTpl('include/field.select.tpl', NO_SKIN);
640 $page->assign('name', 'pays_sel');
641 $where = ($ssect ? ' AND ms.ss_secteur = {?}' : '');
642 $it = XDB::iterator("SELECT a2 AS id, pays AS field
643 FROM geoloc_pays AS g
644 INNER JOIN mentor_pays AS mp ON (mp.pid = g.a2)
645 INNER JOIN mentor_secteurs AS ms ON (ms.uid = mp.uid)
646 WHERE ms.secteur = {?} $where
647 GROUP BY a2
648 ORDER BY pays", $sect, $ssect);
649 $page->assign('list', $it);
650 }
651
652 function handler_p_usage(&$page)
653 {
654 $page->changeTpl('profile/nomusage.tpl');
655
656 require_once 'validations.inc.php';
657 require_once 'xorg.misc.inc.php';
658
659 $res = XDB::query(
660 "SELECT u.nom, u.nom_usage, u.flags, e.alias
661 FROM auth_user_md5 AS u
662 LEFT JOIN aliases AS e ON(u.user_id = e.id
663 AND FIND_IN_SET('usage', e.flags))
664 WHERE user_id={?}", S::v('uid'));
665
666 list($nom, $usage_old, $flags, $alias_old) = $res->fetchOneRow();
667 $flags = new flagset($flags);
668 $page->assign('usage_old', $usage_old);
669 $page->assign('alias_old', $alias_old);
670
671 $nom_usage = replace_accent(trim(Env::v('nom_usage')));
672 $nom_usage = strtoupper($nom_usage);
673 $page->assign('usage_req', $nom_usage);
674
675 if (Env::has('submit') && ($nom_usage != $usage_old)) {
676 S::assert_xsrf_token();
677
678 // on vient de recevoir une requete, differente de l'ancien nom d'usage
679 if ($nom_usage == $nom) {
680 $page->assign('same', true);
681 } else { // le nom de mariage est distinct du nom à l'X
682 // on calcule l'alias pour l'afficher
683 $reason = Env::v('reason');
684 if ($reason == 'other') {
685 $reason = Env::v('other_reason');
686 }
687 $myusage = new UsageReq(S::v('uid'), $nom_usage, $reason);
688 $myusage->submit();
689 $page->assign('myusage', $myusage);
690 }
691 }
692 }
693
694 function handler_xnet(&$page)
695 {
696 $page->changeTpl('profile/groupesx.tpl');
697 $page->assign('xorg_title', 'Polytechnique.org - Promo, Groupes X, Binets');
698
699 $req = XDB::query('
700 SELECT m.asso_id, a.nom, diminutif, a.logo IS NOT NULL AS has_logo,
701 COUNT(e.eid) AS events, mail_domain AS lists
702 FROM groupex.membres AS m
703 INNER JOIN groupex.asso AS a ON(m.asso_id = a.id)
704 LEFT JOIN groupex.evenements AS e ON(e.asso_id = m.asso_id AND e.archive = 0)
705 WHERE uid = {?} GROUP BY m.asso_id ORDER BY a.nom', S::i('uid'));
706 $page->assign('assos', $req->fetchAllAssoc());
707 }
708
709 function handler_vcard(&$page, $x = null)
710 {
711 if (is_null($x)) {
712 return PL_NOT_FOUND;
713 }
714
715 global $globals;
716
717 if (substr($x, -4) == '.vcf') {
718 $x = substr($x, 0, strlen($x) - 4);
719 }
720
721 $vcard = new VCard($x);
722 $vcard->do_page($page);
723 }
724
725 function handler_admin_trombino(&$page, $uid = null, $action = null) {
726 $page->changeTpl('profile/admin_trombino.tpl');
727 $page->assign('xorg_title','Polytechnique.org - Administration - Trombino');
728 $page->assign('uid', $uid);
729
730 $q = XDB::query(
731 "SELECT a.alias,promo
732 FROM auth_user_md5 AS u
733 INNER JOIN aliases AS a ON ( u.user_id = a.id AND type='a_vie' )
734 WHERE user_id = {?}", $uid);
735 list($forlife, $promo) = $q->fetchOneRow();
736
737 switch ($action) {
738 case "original":
739 header("Content-type: image/jpeg");
740 readfile("/home/web/trombino/photos".$promo."/".$forlife.".jpg");
741 exit;
742 break;
743
744 case "new":
745 S::assert_xsrf_token();
746
747 $data = file_get_contents($_FILES['userfile']['tmp_name']);
748 list($x, $y) = getimagesize($_FILES['userfile']['tmp_name']);
749 $mimetype = substr($_FILES['userfile']['type'], 6);
750 unlink($_FILES['userfile']['tmp_name']);
751 XDB::execute(
752 "REPLACE INTO photo SET uid={?}, attachmime = {?}, attach={?}, x={?}, y={?}",
753 $uid, $mimetype, $data, $x, $y);
754 break;
755
756 case "delete":
757 S::assert_xsrf_token();
758
759 XDB::execute('DELETE FROM photo WHERE uid = {?}', $uid);
760 break;
761 }
762
763 $page->assign('forlife', $forlife);
764 }
765 function handler_admin_binets(&$page, $action = 'list', $id = null) {
766 $page->assign('xorg_title','Polytechnique.org - Administration - Binets');
767 $page->assign('title', 'Gestion des binets');
768 $table_editor = new PLTableEditor('admin/binets', 'binets_def', 'id');
769 $table_editor->add_join_table('binets_ins','binet_id',true);
770 $table_editor->describe('text','intitulé',true);
771 $table_editor->apply($page, $action, $id);
772 }
773 function handler_admin_formations(&$page, $action = 'list', $id = null) {
774 $page->assign('xorg_title','Polytechnique.org - Administration - Formations');
775 $page->assign('title', 'Gestion des formations');
776 $table_editor = new PLTableEditor('admin/formations','applis_def','id');
777 $table_editor->add_join_table('applis_ins','aid',true);
778 $table_editor->describe('text','intitulé',true);
779 $table_editor->describe('url','site web',false);
780 $table_editor->apply($page, $action, $id);
781 }
782 function handler_admin_sections(&$page, $action = 'list', $id = null) {
783 $page->assign('xorg_title','Polytechnique.org - Administration - Sections');
784 $page->assign('title', 'Gestion des sections');
785 $table_editor = new PLTableEditor('admin/sections','sections','id');
786 $table_editor->describe('text','intitulé',true);
787 $table_editor->apply($page, $action, $id);
788 }
789 function handler_admin_ss_secteurs(&$page, $action = 'list', $id = null) {
790 $page->assign('xorg_title', 'Polytechnique.org - Administration - Sous-secteurs');
791 $page->assign('title', 'Gestion des sous-secteurs');
792 $table_editor = new PLTableEditor('admin/ss_secteurs', 'emploi_ss_secteur', 'id', true);
793 $table_editor->describe('label', 'intitulé', true);
794 $table_editor->apply($page, $action, $id);
795 }
796 function handler_admin_fonctions(&$page, $action = 'list', $id = null) {
797 $page->assign('xorg_title', 'Polytechnique.org - Administration - Fonctions');
798 $page->assign('title', 'Gestion des fonctions');
799 $table_editor = new PLTableEditor('admin/fonctions', 'fonctions_def', 'id', true);
800 $table_editor->describe('fonction_fr', 'intitulé', true);
801 $table_editor->describe('fonction_en', 'intitulé (ang)', true);
802 $table_editor->describe('flags', 'titre', true);
803 $table_editor->apply($page, $action, $id);
804 }
805 function handler_admin_secteurs(&$page, $action = 'list', $id = null) {
806 $page->assign('xorg_title', 'Polytechnique.org - Administration - Secteurs');
807 $page->assign('title', 'Gestion des secteurs');
808 $table_editor = new PLTableEditor('admin/secteurs', 'emploi_secteur', 'id', true);
809 $table_editor->describe('label', 'intitulé', true);
810 $table_editor->apply($page, $action, $id);
811 }
812 function handler_admin_medals(&$page, $action = 'list', $id = null) {
813 $page->assign('xorg_title','Polytechnique.org - Administration - Distinctions');
814 $page->assign('title', 'Gestion des Distinctions');
815 $table_editor = new PLTableEditor('admin/medals','profile_medals','id');
816 $table_editor->describe('text', 'intitulé', true);
817 $table_editor->describe('img', 'nom de l\'image', false);
818 $table_editor->describe('flags', 'valider', true);
819 $table_editor->apply($page, $action, $id);
820 if ($id && $action == 'edit') {
821 $page->changeTpl('profile/admin_decos.tpl');
822
823 $mid = $id;
824
825 if (Post::v('act') == 'del') {
826 XDB::execute('DELETE FROM profile_medals_grades
827 WHERE mid={?} AND gid={?}', $mid, Post::i('gid'));
828 } else {
829 foreach (Post::v('grades', array()) as $gid=>$text) {
830 if ($gid === 0) {
831 if (!empty($text)) {
832 $res = XDB::query('SELECT MAX(gid)
833 FROM profile_medals_grades
834 WHERE mid = {?}', $mid);
835 $gid = $res->fetchOneCell() + 1;
836
837 XDB::execute('INSERT INTO profile_medals_grades (mid, gid, text, pos)
838 VALUES ({?}, {?}, {?}, {?})',
839 $mid, $gid, $text, $_POST['pos']['0']);
840 }
841 } else {
842 XDB::execute('UPDATE profile_medals_grades
843 SET pos={?}, text={?}
844 WHERE gid={?} AND mid={?}', $_POST['pos'][$gid], $text, $gid, $mid);
845 }
846 }
847 }
848 $res = XDB::iterator('SELECT gid, text, pos FROM profile_medals_grades WHERE mid={?} ORDER BY pos', $mid);
849 $page->assign('grades', $res);
850 }
851 }
852 }
853
854 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
855 ?>