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