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