Merge commit 'origin/master' into hruid
[platal.git] / include / user.func.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 Polytechnique.org *
0337d704 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 user_clear_all_subs()
23/** kills the inscription of a user.
24 * we still keep his birthdate, adresses, and personnal stuff
25 * kills the entreprises, mentor, emails and lists subscription stuff
26 */
27function user_clear_all_subs($user_id, $really_del=true)
28{
dd502514 29 // keep datas in : aliases, adresses, tels, applis_ins, binets_ins, contacts, groupesx_ins, homonymes, identification_ax, photo
0337d704 30 // delete in : auth_user_md5, auth_user_quick, competences_ins, emails, entreprises, langues_ins, mentor,
31 // mentor_pays, mentor_secteurs, newsletter_ins, perte_pass, requests, user_changes, virtual_redirect, watch_sub
32 // + delete maillists
33
34 global $globals;
d56cb887
VZ
35 $uid = intval($user_id);
36 $user = User::getSilent($uid);
37 list($alias) = explode('@', $user->forlifeEmail());
0337d704 38
425f432d 39 $tables_to_clear = array('uid' => array('competences_ins', 'entreprises', 'langues_ins', 'mentor_pays',
40 'mentor_secteurs', 'mentor', 'perte_pass', 'watch_sub'),
41 'user_id' => array('requests', 'user_changes'));
42
0337d704 43 if ($really_del) {
425f432d 44 array_push($tables_to_clear['uid'], 'emails', 'groupex.membres', 'contacts', 'adresses', 'tels',
45 'photo', 'perte_pass', 'langues_ins', 'forums.abos', 'forums.profils');
46 array_push($tables_to_clear['user_id'], 'newsletter_ins', 'auth_user_quick', 'binets_ins');
787bb3d7 47 $tables_to_clear['id'] = array('aliases');
425f432d 48 $tables_to_clear['contact'] = array('contacts');
425f432d 49 XDB::execute("UPDATE auth_user_md5
50 SET date_ins = 0, promo_sortie = 0, nom_usage = '', password = '', perms = 'pending',
0e5ec860 51 nationalite = '', cv = '', section = 0, date = 0, smtppass = '', mail_storage = ''
425f432d 52 WHERE user_id = {?}", $uid);
53 XDB::execute("DELETE virtual.* FROM virtual INNER JOIN virtual_redirect AS r USING(vid) WHERE redirect = {?}",
54 $alias.'@'.$globals->mail->domain);
55 XDB::execute("DELETE virtual.* FROM virtual INNER JOIN virtual_redirect AS r USING(vid) WHERE redirect = {?}",
56 $alias.'@'.$globals->mail->domain2);
57 } else {
58 XDB::execute("UPDATE auth_user_md5 SET password='',smtppass='' WHERE user_id={?}", $uid);
59 XDB::execute("UPDATE auth_user_quick SET watch_flags='' WHERE user_id={?}", $uid);
0337d704 60 }
61
08cce2ff 62 XDB::execute("DELETE FROM virtual_redirect WHERE redirect = {?}", $alias.'@'.$globals->mail->domain);
63 XDB::execute("DELETE FROM virtual_redirect WHERE redirect = {?}", $alias.'@'.$globals->mail->domain2);
64
425f432d 65 foreach ($tables_to_clear as $key=>&$tables) {
66 foreach ($tables as $table) {
67 XDB::execute("DELETE FROM $table WHERE $key={?}", $uid);
68 }
69 }
9bb8bf21 70
c53b5964 71 $mmlist = new MMList(S::v('uid'), S::v('password'));
9bb8bf21 72 $mmlist->kill($alias, $really_del);
84270653
VZ
73
74 // Deactivates, when available, the Google Apps account of the user.
75 if ($globals->mailstorage->googleapps_domain) {
76 require_once 'googleapps.inc.php';
14870e88 77 if (GoogleAppsAccount::account_status($uid)) {
d56cb887 78 $account = new GoogleAppsAccount($user);
14870e88
VZ
79 $account->suspend();
80 }
84270653 81 }
0337d704 82}
83
84// }}}
85// {{{ function get_user_login()
86
0baf0741 87// Defaut callback to call when a login is not found
88function _default_user_callback($login)
89{
d7610c35 90 Platal::page()->trigError("Il n'y a pas d'utilisateur avec l'identifiant : $login");
0baf0741 91 return;
92}
93
1cc0afe7 94function _silent_user_callback($login)
95{
96 return;
97}
98
7d512b13
VZ
99// Returns an unique identifier corresponding to the @p data. This piece of data
100// can be a numerical id, an hruid, an email alias (any), or a redirection
101// email address. If @p get_forlife is set to true, the user's forlife is
102// returned, otherwise the user's hruid is returned.
103// When no user is found, calls @p callback, and eventually returns false.
0baf0741 104function get_user_login($data, $get_forlife = false, $callback = '_default_user_callback')
105{
d7610c35 106 global $globals;
0337d704 107
7d512b13
VZ
108 // In order to reduce the code size & complexity, we define once for all the
109 // field to be returned. By convention if will be "u.hruid" for the hruid
110 // (thus implying the auth_user_md5 will be aliased on u), and "a.alias" for
111 // the forlife (thus implying the forlife aliases table will be aliased on a).
112 $field = ($get_forlife ? "CONCAT(a.alias, '@" . $globals->mail->domain . "')" : "u.hruid");
113
114 // If $data is an integer, fetches directly the result.
3ab288a6 115 if (is_numeric($data)) {
7d512b13
VZ
116 $res = XDB::query("SELECT $field
117 FROM auth_user_md5 AS u
118 LEFT JOIN aliases AS a ON (a.id = u.user_id AND type = 'a_vie')
119 WHERE u.user_id = {?}", $data);
0337d704 120 if ($res->numRows()) {
121 return $res->fetchOneCell();
0337d704 122 }
7d512b13
VZ
123
124 call_user_func($callback, $data);
125 return false;
0337d704 126 }
127
7d512b13
VZ
128 // Checks whether $data is a valid hruid or not.
129 $res = XDB::query("SELECT $field
130 FROM auth_user_md5 AS u
131 LEFT JOIN aliases AS a ON (a.id = u.user_id AND a.type = 'a_vie')
132 WHERE u.hruid = {?}", $data);
133 if ($res->numRows()) {
134 return $res->fetchOneCell();
135 }
0337d704 136
7d512b13
VZ
137 // From now, $data can only by an email alias, or an email redirection.
138 // If it doesn't look like a valid address, appends the plat/al's main domain.
139 $data = trim(strtolower($data));
0337d704 140 if (strstr($data, '@')===false) {
7d512b13 141 $data = $data . '@' . $globals->mail->domain;
0337d704 142 }
787bb3d7 143
7d512b13 144 // Checks if $data is a valid alias on the main domains.
575dd9be 145 list($mbox, $fqdn) = explode('@', $data);
0337d704 146 if ($fqdn == $globals->mail->domain || $fqdn == $globals->mail->domain2) {
7d512b13
VZ
147 $res = XDB::query("SELECT $field
148 FROM auth_user_md5 AS u
149 INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type = 'a_vie')
150 INNER JOIN aliases AS b ON (b.id = u.user_id AND b.type IN ('alias', 'a_vie'))
151 WHERE b.alias = {?}", $mbox);
0337d704 152 if ($res->numRows()) {
7d512b13 153 return $res->fetchOneCell();
0337d704 154 }
155
a14159bf 156 if (preg_match('/^(.*)\.([0-9]{4})$/u', $mbox, $matches)) {
3ab288a6 157 $res = XDB::query("SELECT a.alias
7d512b13
VZ
158 FROM auth_user_md5 AS u
159 INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type = 'a_vie')
160 INNER JOIN aliases AS b ON (b.id = u.user_id AND b.type IN ('alias', 'a_vie'))
161 WHERE b.alias = {?} AND u.promo = {?}", $matches[1], $matches[2]);
3ab288a6 162 if ($res->numRows() == 1) {
163 return $res->fetchOneCell();
164 }
165 }
7d512b13 166
0baf0741 167 call_user_func($callback, $data);
3ab288a6 168 return false;
169
7d512b13 170 // Looks for $data as an email alias from the dedicated alias domain.
0337d704 171 } elseif ($fqdn == $globals->mail->alias_dom || $fqdn == $globals->mail->alias_dom2) {
08cce2ff 172 $res = XDB::query("SELECT redirect
3ab288a6 173 FROM virtual_redirect
174 INNER JOIN virtual USING(vid)
7d512b13 175 WHERE alias = {?}", $mbox . '@' . $globals->mail->alias_dom);
0337d704 176 if ($redir = $res->fetchOneCell()) {
7d512b13
VZ
177 list($alias, $alias_fqdn) = explode('@', $redir);
178 if ($get_forlife) {
179 // It might happen that the "secondary" forlife alias (the one
180 // based on the secondary domaine name) is used as a target; we
181 // then need to canonicalize it to the main domain.
182 if ($alias_fqdn == $globals->mail->domain2) {
183 return $alias . "@" . $globals->mail->domain;
184 }
185 return $redir;
186 }
787bb3d7 187
7d512b13
VZ
188 // We now have a valid alias, which has to be translated to an hruid.
189 $res = XDB::query("SELECT u.hruid
190 FROM auth_user_md5 AS u
191 LEFT JOIN aliases AS a ON (a.id = u.user_id AND a.type IN ('alias', 'a_vie'))
192 WHERE a.alias = {?}", $alias);
193 if ($res->numRows()) {
0337d704 194 return $res->fetchOneCell();
7d512b13
VZ
195 }
196 }
787bb3d7 197
7d512b13
VZ
198 call_user_func($callback, $data);
199 return false;
200
201 // Otherwise, we do suppose $data is an email redirection.
202 } else {
203 $res = XDB::query("SELECT $field
204 FROM auth_user_md5 AS u
205 LEFT JOIN aliases AS a ON (a.id = u.user_id AND a.type = 'a_vie')
206 LEFT JOIN emails AS e ON (e.uid = u.user_id)
207 WHERE e.email = {?}", $data);
208 if ($res->numRows() == 1) {
209 return $res->fetchOneCell();
210 } else if ($res->numRows() > 0) {
211 if (S::has_perms()) {
b8971a3e 212 Platal::page()->trigError("Il y a $user_count utilisateurs avec cette adresse mail : " . join(', ', $res->fetchColumn()));
7d512b13
VZ
213 } else {
214 $res->free();
215 }
216 } else {
217 call_user_func($callback, $data);
0337d704 218 }
7d512b13
VZ
219
220 return false;
0337d704 221 }
787bb3d7 222
0337d704 223 return false;
224}
225
226// }}}
7d512b13 227// {{{ function get_users_login_list()
0337d704 228
7d512b13
VZ
229// Returns an array of valid forlife/hruid based on the @p members list. The
230// list can be an array (in this case the ouput will retain the keys), or a
231// space separated list.
232// The @p strict indicates if the input alias should be retain in output when
233// no valid forlife is found (incompatible with $get_forlife = false).
234function get_users_login_list($members, $strict = false, $get_forlife = false, $callback = '_default_user_callback')
0baf0741 235{
7d512b13
VZ
236 if (!$get_forlife) {
237 $strict = true;
238 }
e7545178 239
1cc0afe7 240 if (!is_array($members)) {
241 if (strlen(trim($members)) == 0) {
242 return null;
243 }
108d1d90 244 $members = split("[; ,\r\n\|]+", $members);
e7545178 245 }
7d512b13 246
e7545178 247 if ($members) {
248 $list = array();
249 foreach ($members as $i => $alias) {
108d1d90
FB
250 $alias = trim($alias);
251 if (empty($alias)) {
252 continue;
253 }
7d512b13 254 if (($login = get_user_login($alias, $get_forlife, $callback)) !== false) {
e7545178 255 $list[$i] = $login;
1555e55e 256 } else if (!$strict) {
e7545178 257 $list[$i] = $alias;
1555e55e
FB
258 } else {
259 global $globals;
260 if (strpos($alias, '@') !== false) {
261 list($user, $dom) = explode('@', $alias);
262 if ($dom != $globals->mail->domain && $dom != $globals->mail->domain2) {
263 $list[$i] = $alias;
264 }
265 }
e7545178 266 }
267 }
268 return $list;
269 }
270 return null;
271}
272
273// }}}
7d512b13
VZ
274// {{{ function get_users_forlife_list()
275
276function get_users_forlife_list($members, $strict = false, $callback = '_default_user_callback')
277{
278 return get_users_login_list($members, $strict, true, $callback);
279}
280
281// }}}
282// {{{ function get_user_hruid()
283
284function get_user_hruid($data, $callback = '_default_user_callback')
285{
286 return get_user_login($data, false, $callback);
287}
288
289// }}}
290// {{{ function get_users_hruid_list()
291
292function get_users_hruid_list($members, $strict = false, $callback = '_default_user_callback')
293{
294 return get_users_login_list($members, true, false, $callback);
295}
296
297// }}}
8b10988d 298// {{{ function has_user_right()
299function has_user_right($pub, $view = 'private') {
300 if ($pub == $view) return true;
787bb3d7 301 // all infos available for private
8b10988d 302 if ($view == 'private') return true;
787bb3d7 303 // public infos available for all
8b10988d 304 if ($pub == 'public') return true;
305 // here we have view = ax or public, and pub = ax or private, and pub != view
787bb3d7 306 return false;
8b10988d 307}
308// }}}
0baf0741 309// {{{ function get_not_registered_user()
310
311function get_not_registered_user($login, $iterator = false)
312{
313 global $globals;
314 @list($login, $domain) = explode('@', $login);
315 if ($domain && $domain != $globals->mail->domain && $domain != $globals->mail->domain2) {
316 return null;
317 }
318 @list($prenom, $nom, $promo) = explode('.', $login);
319 $where = 'REPLACE(REPLACE(REPLACE(nom, " ", ""), "-", ""), "\'", "") LIKE CONCAT("%", {?}, "%")
320 AND REPLACE(REPLACE(REPLACE(prenom, " ", ""), "-", ""), "\'", "") LIKE CONCAT("%", {?}, "%")';
321 if ($promo) {
322 if (preg_match('/^[0-9]{2}$/', $promo)) {
323 $where .= 'AND MOD(promo, 100) = {?}';
324 } elseif (preg_match('/^[0-9]{4}$/', $promo)) {
325 $where .= 'AND promo = {?}';
326 }
327 }
328 $sql = "SELECT user_id, nom, prenom, promo
329 FROM auth_user_md5
1555e55e 330 WHERE $where AND perms = 'pending'
787bb3d7 331 ORDER BY promo, nom, prenom";
0baf0741 332 if ($iterator) {
333 return XDB::iterator($sql, $nom, $prenom, $promo);
334 } else {
335 $res = XDB::query($sql, $nom, $prenom, $promo);
336 return $res->fetchAllAssoc();
337 }
338}
339
340// }}}
9039c94d 341// {{{ function get_user_details_pro()
342
8b10988d 343function get_user_details_pro($uid, $view = 'private')
9039c94d 344{
9039c94d 345 $sql = "SELECT e.entreprise, s.label as secteur , ss.label as sous_secteur , f.fonction_fr as fonction,
dd502514 346 e.poste, e.adr1, e.adr2, e.adr3, e.postcode, e.city, e.entrid,
9039c94d 347 gp.pays AS countrytxt, gr.name AS region, e.tel, e.fax, e.mobile, e.entrid,
348 e.pub, e.adr_pub, e.tel_pub, e.email, e.email_pub, e.web
349 FROM entreprises AS e
350 LEFT JOIN emploi_secteur AS s ON(e.secteur = s.id)
351 LEFT JOIN emploi_ss_secteur AS ss ON(e.ss_secteur = ss.id AND e.secteur = ss.secteur)
352 LEFT JOIN fonctions_def AS f ON(e.fonction = f.id)
353 LEFT JOIN geoloc_pays AS gp ON (gp.a2 = e.country)
354 LEFT JOIN geoloc_region AS gr ON (gr.a2 = e.country and gr.region = e.region)
355 WHERE e.uid = {?}
356 ORDER BY e.entrid";
08cce2ff 357 $res = XDB::query($sql, $uid);
8b10988d 358 $all_pro = $res->fetchAllAssoc();
359 foreach ($all_pro as $i => $pro) {
360 if (!has_user_right($pro['pub'], $view))
361 unset($all_pro[$i]);
362 else {
363 if (!has_user_right($pro['adr_pub'], $view)) {
dd502514 364 if ($pro['adr1'] == '' &&
365 $pro['adr2'] == '' &&
366 $pro['adr3'] == '' &&
367 $pro['postcode'] == '' &&
368 $pro['city'] == '' &&
369 $pro['countrytxt'] == '' &&
370 $pro['region'] == '') {
371 $all_pro[$i]['adr_pub'] = $view;
372 } else {
373 $all_pro[$i]['adr1'] = '';
374 $all_pro[$i]['adr2'] = '';
375 $all_pro[$i]['adr3'] = '';
376 $all_pro[$i]['postcode'] = '';
377 $all_pro[$i]['city'] = '';
378 $all_pro[$i]['countrytxt'] = '';
379 $all_pro[$i]['region'] = '';
380 }
8b10988d 381 }
382 if (!has_user_right($pro['tel_pub'], $view)) {
dd502514 383 // if no tel was defined, then the viewer will be able to write it
787bb3d7 384 if ($pro['tel'] == '' &&
dd502514 385 $pro['fax'] == '' &&
386 $pro['mobile'] == '') {
387 $all_pro[$i]['tel_pub'] = $view;
388 } else {
389 $all_pro[$i]['tel'] = '';
390 $all_pro[$i]['fax'] = '';
391 $all_pro[$i]['mobile'] = '';
392 }
8b10988d 393 }
394 if (!has_user_right($pro['email_pub'], $view)) {
dd502514 395 if ($pro['email'] == '')
396 $all_pro[$i]['email_pub'] = $view;
397 else
398 $all_pro[$i]['email'] = '';
8b10988d 399 }
400 if ($all_pro[$i]['adr1'] == '' &&
401 $all_pro[$i]['adr2'] == '' &&
402 $all_pro[$i]['adr3'] == '' &&
403 $all_pro[$i]['postcode'] == '' &&
404 $all_pro[$i]['city'] == '' &&
405 $all_pro[$i]['countrytxt'] == '' &&
406 $all_pro[$i]['region'] == '' &&
407 $all_pro[$i]['entreprise'] == '' &&
408 $all_pro[$i]['fonction'] == '' &&
409 $all_pro[$i]['secteur'] == '' &&
410 $all_pro[$i]['poste'] == '' &&
411 $all_pro[$i]['tel'] == '' &&
412 $all_pro[$i]['fax'] == '' &&
413 $all_pro[$i]['mobile'] == '' &&
414 $all_pro[$i]['email'] == '')
415 unset($all_pro[$i]);
416 }
417 }
418 if (!count($all_pro)) return false;
419 return $all_pro;
9039c94d 420}
421
422// }}}
433336f3 423// {{{ function get_user_details_adr()
424
8b10988d 425function get_user_details_adr($uid, $view = 'private') {
8b10988d 426 $sql = "SELECT a.adrid, a.adr1,a.adr2,a.adr3,a.postcode,a.city,
427 gp.pays AS countrytxt,a.region, a.regiontxt,
428 FIND_IN_SET('active', a.statut) AS active, a.adrid,
429 FIND_IN_SET('res-secondaire', a.statut) AS secondaire,
430 a.pub, gp.display
431 FROM adresses AS a
432 LEFT JOIN geoloc_pays AS gp ON (gp.a2=a.country)
433 WHERE uid= {?} AND NOT FIND_IN_SET('pro',a.statut)
434 ORDER BY NOT FIND_IN_SET('active',a.statut), FIND_IN_SET('temporaire',a.statut), FIND_IN_SET('res-secondaire',a.statut)";
08cce2ff 435 $res = XDB::query($sql, $uid);
8b10988d 436 $all_adr = $res->fetchAllAssoc();
437 $adrid_index = array();
438 foreach ($all_adr as $i => $adr) {
439 if (!has_user_right($adr['pub'], $view))
440 unset($all_adr[$i]);
441 else
442 $adrid_index[$adr['adrid']] = $i;
443 }
787bb3d7 444
8b10988d 445 $sql = "SELECT t.adrid, t.tel_pub, t.tel_type, t.tel, t.telid
446 FROM tels AS t
447 INNER JOIN adresses AS a ON (a.uid = t.uid) AND (a.adrid = t.adrid)
448 WHERE t.uid = {?} AND NOT FIND_IN_SET('pro',a.statut)
449 ORDER BY t.adrid, t.tel_type DESC, t.telid";
08cce2ff 450 $restel = XDB::iterator($sql, $uid);
8b10988d 451 while ($nexttel = $restel->next()) {
452 if (has_user_right($nexttel['tel_pub'], $view)) {
453 $adrid = $nexttel['adrid'];
454 unset($nexttel['adrid']);
455 if (isset($adrid_index[$adrid])) {
787bb3d7 456 if (!isset($all_adr[$adrid_index[$adrid]]['tels']))
8b10988d 457 $all_adr[$adrid_index[$adrid]]['tels'] = array($nexttel);
458 else
459 $all_adr[$adrid_index[$adrid]]['tels'][] = $nexttel;
460 }
461 }
462 }
463 return $all_adr;
464}
433336f3 465
466// }}}
0337d704 467// {{{ function get_user_details()
468
8b10988d 469function &get_user_details($login, $from_uid = '', $view = 'private')
0337d704 470{
0337d704 471 $reqsql = "SELECT u.user_id, u.promo, u.promo_sortie, u.prenom, u.nom, u.nom_usage, u.date, u.cv,
f6c71069 472 u.perms IN ('admin','user','disabled') AS inscrit, FIND_IN_SET('femme', u.flags) AS sexe, u.deces != 0 AS dcd, u.deces,
0337d704 473 q.profile_nick AS nickname, q.profile_from_ax, q.profile_mobile AS mobile, q.profile_web AS web, q.profile_freetext AS freetext,
474 q.profile_mobile_pub AS mobile_pub, q.profile_web_pub AS web_pub, q.profile_freetext_pub AS freetext_pub,
475 q.profile_medals_pub AS medals_pub,
476 IF(gp.nat='',gp.pays,gp.nat) AS nationalite, gp.a2 AS iso3166,
477 a.alias AS forlife, a2.alias AS bestalias,
478 c.uid IS NOT NULL AS is_contact,
479 s.text AS section, p.x, p.y, p.pub AS photo_pub,
dd502514 480 u.matricule_ax,
d9caac6f 481 m.expertise != '' AS is_referent,
0e5ec860 482 (COUNT(e.email) > 0 OR FIND_IN_SET('googleapps', u.mail_storage) > 0) AS actif
0337d704 483 FROM auth_user_md5 AS u
484 INNER JOIN auth_user_quick AS q USING(user_id)
485 INNER JOIN aliases AS a ON (u.user_id=a.id AND a.type='a_vie')
486 INNER JOIN aliases AS a2 ON (u.user_id=a2.id AND FIND_IN_SET('bestalias',a2.flags))
487 LEFT JOIN contacts AS c ON (c.uid = {?} and c.contact = u.user_id)
488 LEFT JOIN geoloc_pays AS gp ON (gp.a2 = u.nationalite)
489 INNER JOIN sections AS s ON (s.id = u.section)
787bb3d7 490 LEFT JOIN photo AS p ON (p.uid = u.user_id)
0337d704 491 LEFT JOIN mentor AS m ON (m.uid = u.user_id)
d9caac6f 492 LEFT JOIN emails AS e ON (e.uid = u.user_id AND e.flags='active')
493 WHERE a.alias = {?}
494 GROUP BY u.user_id";
08cce2ff 495 $res = XDB::query($reqsql, $from_uid, $login);
0337d704 496 $user = $res->fetchOneAssoc();
497 $uid = $user['user_id'];
8b10988d 498 // hide orange status, cv, nickname, section
499 if (!has_user_right('private', $view)) {
500 $user['promo_sortie'] = $user['promo'] + 3;
501 $user['cv'] = '';
502 $user['nickname'] = '';
503 $user['section'] = '';
504 }
505 // hide mobile
dd502514 506 if (!has_user_right($user['mobile_pub'], $view)) {
507 if ($user['mobile'] == '')
508 $user['mobile_pub'] = $view;
509 else
510 $user['mobile'] = '';
511 }
8b10988d 512 // hide web
dd502514 513 if (!has_user_right($user['web_pub'], $view)) {
514 if ($user['web'] == '')
515 $user['web_pub'] = $view;
516 else
517 $user['web'] = '';
518 }
8b10988d 519 // hide freetext
dd502514 520 if (!has_user_right($user['freetext_pub'], $view)) {
521 if ($user['freetext'] == '')
522 $user['freetext_pub'] = $view;
523 else
524 $user['freetext'] = '';
525 }
0337d704 526
8b10988d 527 $user['adr_pro'] = get_user_details_pro($uid, $view);
528 $user['adr'] = get_user_details_adr($uid, $view);
0337d704 529
8b10988d 530 if (has_user_right('private', $view)) {
531 $sql = "SELECT text
532 FROM binets_ins
533 LEFT JOIN binets_def ON binets_ins.binet_id = binets_def.id
534 WHERE user_id = {?}";
08cce2ff 535 $res = XDB::query($sql, $uid);
8b10988d 536 $user['binets'] = $res->fetchColumn();
537 $user['binets_join'] = join(', ', $user['binets']);
787bb3d7 538
b37aacd9
FB
539 $res = XDB::iterRow("SELECT a.diminutif, a.nom, a.site
540 FROM groupex.asso AS a
541 LEFT JOIN groupex.membres AS m ON (m.asso_id = a.id)
542 WHERE m.uid = {?} AND (a.cat = 'GroupesX' OR a.cat = 'Institutions')
543 AND pub = 'public'", $uid);
8b10988d 544 $user['gpxs'] = Array();
ae2f9e35 545 $user['gpxs_name'] = Array();
b37aacd9
FB
546 while (list($gxd, $gxt, $gxu) = $res->next()) {
547 if (!$gxu) {
548 $gxu = 'http://www.polytechnique.net/' . $gxd;
549 }
550 $user['gpxs'][] = '<span title="' . pl_entities($gxt) . "\"><a href=\"$gxu\">$gxd</a></span>";
ae2f9e35 551 $user['gpxs_name'][] = $gxt;
787bb3d7 552 }
8b10988d 553 $user['gpxs_join'] = join(', ', $user['gpxs']);
79a5acea 554 }
0337d704 555
08cce2ff 556 $res = XDB::iterRow("SELECT applis_def.text, applis_def.url, applis_ins.type
ae2f9e35 557 FROM applis_ins
558 INNER JOIN applis_def ON applis_def.id = applis_ins.aid
559 WHERE uid={?}
560 ORDER BY ordre", $uid);
787bb3d7 561
0337d704 562 $user['applis_fmt'] = Array();
dd502514 563 $user['formation'] = Array();
0337d704 564 while (list($txt, $url, $type) = $res->next()) {
dd502514 565 $user['formation'][] = $txt." ".$type;
0337d704 566 require_once('applis.func.inc.php');
567 $user['applis_fmt'][] = applis_fmt($type, $txt, $url);
568 }
569 $user['applis_join'] = join(', ', $user['applis_fmt']);
570
8b10988d 571 if (has_user_right($user['medals_pub'], $view)) {
5e70bf24 572 $res = XDB::iterator("SELECT m.id, m.text AS medal, m.type, s.gid, g.text AS grade
ae2f9e35 573 FROM profile_medals_sub AS s
574 INNER JOIN profile_medals AS m ON ( s.mid = m.id )
575 LEFT JOIN profile_medals_grades AS g ON ( s.mid = g.mid AND s.gid = g.gid )
576 WHERE s.uid = {?}", $uid);
8b10988d 577 $user['medals'] = Array();
578 while ($tmp = $res->next()) {
579 $user['medals'][] = $tmp;
580 }
0337d704 581 }
582
583 return $user;
584}
dd502514 585// }}}
586// {{{ function add_user_address()
587function add_user_address($uid, $adrid, $adr) {
08cce2ff 588 XDB::execute(
dd502514 589 "INSERT INTO adresses (`uid`, `adrid`, `adr1`, `adr2`, `adr3`, `postcode`, `city`, `country`, `datemaj`, `pub`) (
590 SELECT u.user_id, {?}, {?}, {?}, {?}, {?}, {?}, gp.a2, NOW(), {?}
591 FROM auth_user_md5 AS u
592 LEFT JOIN geoloc_pays AS gp ON (gp.pays LIKE {?} OR gp.country LIKE {?} OR gp.a2 LIKE {?})
593 WHERE u.user_id = {?}
594 LIMIT 1)",
595 $adrid, $adr['adr1'], $adr['adr2'], $adr['adr3'], $adr['postcode'], $adr['city'], $adr['pub'], $adr['countrytxt'], $adr['countrytxt'], $adr['countrytxt'], $uid);
596 if (isset($adr['tels']) && is_array($adr['tels'])) {
597 $telid = 0;
598 foreach ($adr['tels'] as $tel) if ($tel['tel']) {
599 add_user_tel($uid, $adrid, $telid, $tel);
600 $telid ++;
601 }
602 }
603}
604// }}}
605// {{{ function update_user_address()
606function update_user_address($uid, $adrid, $adr) {
dd502514 607 // update address
08cce2ff 608 XDB::execute(
787bb3d7 609 "UPDATE adresses AS a LEFT JOIN geoloc_pays AS gp ON (gp.pays = {?})
dd502514 610 SET `adr1` = {?}, `adr2` = {?}, `adr3` = {?},
611 `postcode` = {?}, `city` = {?}, a.`country` = gp.a2, `datemaj` = NOW(), `pub` = {?}
612 WHERE adrid = {?} AND uid = {?}",
613 $adr['country_txt'],
614 $adr['adr1'], $adr['adr2'], $adr['adr3'],
615 $adr['postcode'], $adr['city'], $adr['pub'], $adrid, $uid);
616 if (isset($adr['tels']) && is_array($adr['tels'])) {
08cce2ff 617 $res = XDB::query("SELECT telid FROM tels WHERE uid = {?} AND adrid = {?} ORDER BY telid", $uid, $adrid);
dd502514 618 $telids = $res->fetchColumn();
619 foreach ($adr['tels'] as $tel) {
620 if (isset($tel['telid']) && isset($tel['remove']) && $tel['remove']) {
621 remove_user_tel($uid, $adrid, $tel['telid']);
622 if (isset($telids[$tel['telid']])) unset($telids[$tel['telid']]);
623 } else if (isset($tel['telid'])) {
624 update_user_tel($uid, $adrid, $tel['telid'], $tel);
625 } else {
626 for ($telid = 0; isset($telids[$telid]) && ($telids[$telid] == $telid); $telid++);
627 add_user_tel($uid, $adrid, $telid, $tel);
628 }
629 }
630 }
631}
632// }}}
633// {{{ function remove_user_address()
634function remove_user_address($uid, $adrid) {
08cce2ff 635 XDB::execute("DELETE FROM adresses WHERE adrid = {?} AND uid = {?}", $adrid, $uid);
636 XDB::execute("DELETE FROM tels WHERE adrid = {?} AND uid = {?}", $adrid, $uid);
dd502514 637}
638// }}}
639// {{{ function add_user_tel()
640function add_user_tel($uid, $adrid, $telid, $tel) {
08cce2ff 641 XDB::execute(
dd502514 642 "INSERT INTO tels SET uid = {?}, adrid = {?}, telid = {?}, tel = {?}, tel_type = {?}, tel_pub = {?}",
643 $uid, $adrid, $telid, $tel['tel'], $tel['tel_type'], $tel['tel_pub']);
644}
645// }}}
646// {{{ function update_user_tel()
647function update_user_tel($uid, $adrid, $telid, $tel) {
08cce2ff 648 XDB::execute(
dd502514 649 "UPDATE tels SET tel = {?}, tel_type = {?}, tel_pub = {?}
650 WHERE telid = {?} AND adrid = {?} AND uid = {?}",
651 $tel['tel'], $tel['tel_type'], $tel['tel_pub'],
652 $telid, $adrid, $uid);
653}
654// }}}
655// {{{ function remove_user_tel()
656function remove_user_tel($uid, $adrid, $telid) {
a3a049fc 657 XDB::execute("DELETE FROM tels WHERE telid = {?} AND adrid = {?} AND uid = {?}",
658 $telid, $adrid, $uid);
dd502514 659}
660// }}}
661// {{{ function add_user_pro()
662function add_user_pro($uid, $entrid, $pro) {
08cce2ff 663 XDB::execute(
dd502514 664 "INSERT INTO entreprises (`uid`, `entrid`, `entreprise`, `poste`, `secteur`, `ss_secteur`, `fonction`,
665 `adr1`, `adr2`, `adr3`, `postcode`, `city`, `country`, `region`, `tel`, `fax`, `mobile`, `email`, `web`, `pub`, `adr_pub`, `tel_pub`, `email_pub`)
666 SELECT u.user_id, {?}, {?}, {?}, s.id, ss.id, f.id,
667 {?}, {?}, {?}, {?}, {?}, gp.a2, gr.region, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}
668 FROM auth_user_md5 AS u
669 LEFT JOIN emploi_secteur AS s ON(s.label LIKE {?})
670 LEFT JOIN emploi_ss_secteur AS ss ON(s.id = ss.secteur AND ss.label LIKE {?})
671 LEFT JOIN fonctions_def AS f ON(f.fonction_fr LIKE {?} OR f.fonction_en LIKE {?})
672 LEFT JOIN geoloc_pays AS gp ON (gp.country LIKE {?} OR gp.pays LIKE {?})
673 LEFT JOIN geoloc_region AS gr ON (gr.a2 = gp.a2 AND gr.name LIKE {?})
674 WHERE u.user_id = {?}
675 LIMIT 1",
676 $entrid, $pro['entreprise'], $pro['poste'],
677 $pro['adr1'], $pro['adr2'], $pro['adr3'], $pro['postcode'], $pro['city'], $pro['tel'], $pro['fax'], $pro['mobile'], $pro['email'], $pro['web'], $pro['pub'], $pro['adr_pub'], $pro['tel_pub'], $pro['email_pub'],
678 $pro['secteur'], $pro['sous_secteur'], $pro['fonction'], $pro['fonction'],
679 $pro['countrytxt'], $pro['countrytxt'], $pro['region'],
680 $uid);
681}
682// }}}
683// {{{ function update_user_pro()
684function update_user_pro($uid, $entrid, $pro) {
dd502514 685 $join = "";
686 $set = "";
687 $args_join = array();
688 $args_set = array();
787bb3d7 689
dd502514 690 $join .= "LEFT JOIN emploi_secteur AS s ON(s.label LIKE {?})
691 LEFT JOIN emploi_ss_secteur AS ss ON(s.id = ss.secteur AND ss.label LIKE {?})
692 LEFT JOIN fonctions_def AS f ON(f.fonction_fr LIKE {?} OR f.fonction_en LIKE {?})";
693 $args_join[] = $pro['secteur'];
694 $args_join[] = $pro['sous_secteur'];
695 $args_join[] = $pro['fonction'];
696 $args_join[] = $pro['fonction'];
697 $set .= ", e.`entreprise` = {?}, e.`secteur` = s.id, e.`ss_secteur` = ss.id, e.`fonction` = f.id, e.`poste`= {?}, e.`web` = {?}, e.`pub` = {?}";
698 $args_set[] = $pro['entreprise'];
699 $args_set[] = $pro['poste'];
700 $args_set[] = $pro['web'];
701 $args_set[] = $pro['pub'];
787bb3d7 702
dd502514 703 if (isset($pro['adr1'])) {
704 $join .= "LEFT JOIN geoloc_pays AS gp ON (gp.country LIKE {?} OR gp.pays LIKE {?})
705 LEFT JOIN geoloc_region AS gr ON (gr.a2 = gp.a2 AND gr.name LIKE {?})";
706 $args_join[] = $pro['countrytxt'];
707 $args_join[] = $pro['countrytxt'];
708 $args_join[] = $pro['region'];
709 $set .= ", e.`adr1` = {?}, e.`adr2` = {?}, e.`adr3` = {?}, e.`postcode` = {?}, e.`city` = {?}, e.`country` = gp.a2, e.`region` = gr.region, e.`adr_pub` = {?}";
710 $args_set[] = $pro['adr1'];
711 $args_set[] = $pro['adr2'];
712 $args_set[] = $pro['adr3'];
713 $args_set[] = $pro['postcode'];
714 $args_set[] = $pro['city'];
715 $args_set[] = $pro['adr_pub'];
716 }
787bb3d7 717
dd502514 718 if (isset($pro['tel'])) {
719 $set .= ", e.`tel` = {?}, e.`fax` = {?}, e.`mobile` = {?}, e.tel_pub = {?}";
720 $args_set[] = $pro['tel'];
721 $args_set[] = $pro['fax'];
722 $args_set[] = $pro['mobile'];
723 $args_set[] = $pro['tel_pub'];
724 }
725 if (isset($pro['email'])) {
726 $set .= ", e.`email` = {?}, e.`email_pub` = {?}";
727 $args_set[] = $pro['email'];
728 $args_set[] = $pro['email_pub'];
787bb3d7 729 }
dd502514 730 $query = "UPDATE entreprises AS e ".$join." SET ".substr($set,1)." WHERE e.uid = {?} AND e.entrid = {?}";
731 $args_where = array($uid, $entrid);
732 $args = array_merge(array($query), $args_join, $args_set, $args_where);
a3a049fc 733 call_user_func_array(array('XDB', 'execute'), $args);
dd502514 734}
735// }}}
736// {{{ function remove_user_pro()
737function remove_user_pro($uid, $entrid) {
08cce2ff 738 XDB::execute("DELETE FROM entreprises WHERE entrid = {?} AND uid = {?}", $entrid, $uid);
dd502514 739}
740// }}}
741// {{{ function set_user_details()
742function set_user_details_addresses($uid, $adrs) {
08cce2ff 743 $res = XDB::query("SELECT adrid FROM adresses WHERE uid = {?} AND adrid >= 1 ORDER BY adrid", $uid);
dd502514 744 $adrids = $res->fetchColumn();
745 foreach ($adrs as $adr) {
746 if (isset($adr['adrid']) && isset($adr['remove']) && $adr['remove']) {
747 remove_user_address($uid, $adr['adrid']);
748 if (isset($adrids[$adr['adrid']])) unset($adrids[$adr['adrid']]);
749 } else if (isset($adr['adrid'])) {
750 update_user_address($uid, $adr['adrid'], $adr);
751 } else {
752 for ($adrid = 1; isset($adrids[$adrid-1]) && ($adrids[$adrid-1] == $adrid); $adrid++);
753 add_user_address($uid, $adrid, $adr);
754 $adrids[$adrid-1] = $adrid;
755 }
756 }
757 require_once 'geoloc.inc.php';
758 localize_addresses($uid);
759}
760// }}}
761// {{{ function set_user_details_pro()
762
763function set_user_details_pro($uid, $pros)
764{
08cce2ff 765 $res = XDB::query("SELECT entrid FROM entreprises WHERE uid = {?} ORDER BY entrid", $uid);
dd502514 766 $entrids = $res->fetchColumn();
767 foreach ($pros as $pro) {
768 if (isset($pro['entrid']) && isset($pro['remove']) && $pro['remove']) {
769 remove_user_pro($uid, $pro['entrid']);
770 if (isset($entrids[$pro['entrid']])) unset($entrids[$pro['entrid']]);
771 } else if (isset($pro['entrid'])) {
772 update_user_pro($uid, $pro['entrid'], $pro);
773 } else {
774 for ($entrid = 0; isset($entrids[$entrid]) && ($entrids[$entrid] == $entrid); $entrid++);
775 add_user_pro($uid, $entrid, $pro);
776 }
777 }
778}
0337d704 779
780// }}}
dd502514 781// {{{ function set_user_details()
782function set_user_details($uid, $details) {
dd502514 783 if (isset($details['nom_usage'])) {
08cce2ff 784 XDB::execute("UPDATE auth_user_md5 SET nom_usage = {?} WHERE user_id = {?}", strtoupper($details['nom_usage']), $uid);
dd502514 785 }
786 if (isset($details['mobile'])) {
08cce2ff 787 XDB::execute("UPDATE auth_user_quick SET profile_mobile = {?} WHERE user_id = {?}", $details['mobile'], $uid);
dd502514 788 }
789 if (isset($details['nationalite'])) {
08cce2ff 790 XDB::execute(
dd502514 791 "UPDATE auth_user_md5 AS u
792 INNER JOIN geoloc_pays AS gp
793 SET u.nationalite = gp.a2
794 WHERE (gp.a2 = {?} OR gp.nat = {?})
795 AND u.user_id = {?}", $details['nationalite'], $details['nationalite'], $uid);
796 }
797 if (isset($details['adr']) && is_array($details['adr']))
798 set_user_details_addresses($uid, $details['adr']);
799 if (isset($details['adr_pro']) && is_array($details['adr_pro']))
800 set_user_details_pro($uid, $details['adr_pro']);
801 if (isset($details['binets']) && is_array($details['binets'])) {
08cce2ff 802 XDB::execute("DELETE FROM binets_ins WHERE user_id = {?}", $uid);
dd502514 803 foreach ($details['binets'] as $binet)
08cce2ff 804 XDB::execute(
dd502514 805 "INSERT INTO binets_ins (`user_id`, `binet_id`)
806 SELECT {?}, id FROM binets_def WHERE text = {?} LIMIT 1",
787bb3d7 807 $uid, $binet);
dd502514 808 }
809 if (isset($details['gpxs']) && is_array($details['gpxs'])) {
08cce2ff 810 XDB::execute("DELETE FROM groupesx_ins WHERE user_id = {?}", $uid);
dd502514 811 foreach ($details['gpxs'] as $groupex) {
a14159bf 812 if (preg_match('/<a href="[^"]*">([^<]+)</a>/u', $groupex, $a)) $groupex = $a[1];
08cce2ff 813 XDB::execute(
dd502514 814 "INSERT INTO groupesx_ins (`user_id`, `binet_id`)
815 SELECT {?}, id FROM groupesx_def WHERE text = {?} LIMIT 1",
816 $uid, $groupex);
787bb3d7 817 }
dd502514 818 }
819 // applis
820 // medals
821}
822// }}}
0337d704 823// {{{ function _user_reindex
824
c0c9f772 825function _user_reindex($uid, $keys, $muls, $pubs)
94f6c381 826{
0337d704 827 foreach ($keys as $i => $key) {
828 if ($key == '') {
829 continue;
830 }
831 $toks = preg_split('/[ \'\-]+/', $key);
832 $token = "";
833 $first = 5;
834 while ($toks) {
835 $token = strtolower(replace_accent(array_pop($toks) . $token));
836 $score = ($toks ? 0 : 10 + $first) * $muls[$i];
c0c9f772 837 XDB::execute("REPLACE INTO search_name (token, uid, soundex, score, flags)
838 VALUES ({?}, {?}, {?}, {?}, {?})",
839 $token, $uid, soundex_fr($token), $score, $pubs[$i] ? 'public' : '');
0337d704 840 $first = 0;
841 }
842 }
94f6c381 843 $res = XDB::query("SELECT nom_ini, nom, nom_usage, prenom_ini, prenom, promo, matricule
844 FROM auth_user_md5
845 WHERE user_id = {?}", $uid);
846 if (!$res->numRows()) {
847 unset($res);
848 return;
849 }
850 $array = $res->fetchOneRow();
851 $promo = intval(array_pop($array));
852 $mat = array_shift($array);
853 array_walk($array, 'soundex_fr');
854 XDB::execute("REPLACE INTO recherche_soundex
855 SET matricule = {?}, nom1_soundex = {?}, nom2_soundex= {?}, nom3_soundex = {?},
856 prenom1_soundex = {?}, prenom2_soundex= {?}, promo = {?}",
857 $mat, $array[0], $array[1], $array[2], $array[3], $array[4], $promo);
858 unset($res);
859 unset($array);
0337d704 860}
861
862// }}}
863// {{{ function user_reindex
864
865function user_reindex($uid) {
08cce2ff 866 XDB::execute("DELETE FROM search_name WHERE uid={?}", $uid);
867 $res = XDB::query("SELECT prenom, nom, nom_usage, profile_nick FROM auth_user_md5 INNER JOIN auth_user_quick USING(user_id) WHERE auth_user_md5.user_id = {?}", $uid);
36f58fcf 868 if ($res->numRows()) {
c0c9f772 869 _user_reindex($uid, $res->fetchOneRow(), array(1,1,1,0.2), array(true, true, true, false));
36f58fcf 870 } else { // not in auth_user_quick => still "pending"
871 $res = XDB::query("SELECT prenom, nom, nom_usage FROM auth_user_md5 WHERE auth_user_md5.user_id = {?}", $uid);
872 if ($res->numRows()) {
c0c9f772 873 _user_reindex($uid, $res->fetchOneRow(), array(1,1,1), array(true, true, true));
36f58fcf 874 }
875 }
0337d704 876}
877
878// }}}
433336f3 879// {{{ function set_new_usage()
74c55fd6 880
a11b5424 881function set_new_usage($uid, $usage, $alias=false)
787bb3d7 882{
74c55fd6 883 XDB::execute("UPDATE auth_user_md5 set nom_usage={?} WHERE user_id={?}",$usage ,$uid);
884 XDB::execute("DELETE FROM aliases WHERE FIND_IN_SET('usage',flags) AND id={?}", $uid);
885 if ($alias && $usage) {
886 XDB::execute("UPDATE aliases SET flags=flags & 255-1 WHERE id={?}", $uid);
887 XDB::execute("INSERT INTO aliases VALUES({?}, 'alias', 'usage,bestalias', {?}, null)",
888 $alias, $uid);
889 }
890 $r = XDB::query("SELECT alias FROM aliases WHERE FIND_IN_SET('bestalias', flags) AND id = {?}", $uid);
a11b5424 891 if ($r->numRows() == "") {
74c55fd6 892 XDB::execute("UPDATE aliases SET flags = 1 | flags WHERE id = {?} LIMIT 1", $uid);
a11b5424 893 $r = XDB::query("SELECT alias FROM aliases WHERE FIND_IN_SET('bestalias', flags) AND id = {?}", $uid);
74c55fd6 894 }
74c55fd6 895 user_reindex($uid);
a11b5424 896 return $r->fetchOneCell();
74c55fd6 897}
898
433336f3 899// }}}
900// {{{ function get_X_mat
901function get_X_mat($ourmat)
902{
787bb3d7 903 if (!preg_match('/^[0-9]{8}$/', $ourmat)) {
433336f3 904 // le matricule de notre base doit comporter 8 chiffres
905 return 0;
787bb3d7
FB
906 }
907
433336f3 908 $year = intval(substr($ourmat, 0, 4));
909 $rang = intval(substr($ourmat, 5, 3));
910 if ($year < 1996) {
787bb3d7 911 return;
433336f3 912 } elseif ($year < 2000) {
913 $year = intval(substr(1900 - $year, 1, 3));
914 return sprintf('%02u0%03u', $year, $rang);
915 } else {
916 $year = intval(substr(1900 - $year, 1, 3));
917 return sprintf('%03u%03u', $year, $rang);
918 }
787bb3d7
FB
919}
920
433336f3 921// }}}
922
923
a7de4ef7 924// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 925?>