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