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