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