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