2006 => 2007 Happy New Year\!
[platal.git] / include / user.func.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
5ddeb07c 3 * Copyright (C) 2003-2007 Polytechnique.org *
0337d704 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 */
27function user_clear_all_subs($user_id, $really_del=true)
28{
dd502514 29 // keep datas in : aliases, adresses, tels, applis_ins, binets_ins, contacts, groupesx_ins, homonymes, identification_ax, photo
0337d704 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);
08cce2ff 36 $res = XDB::query("SELECT alias FROM aliases WHERE type='a_vie' AND id={?}", $uid);
0337d704 37 $alias = $res->fetchOneCell();
38
39 if ($really_del) {
c53b5964 40 XDB::execute("DELETE FROM emails WHERE uid={?}", $uid);
41 XDB::execute("DELETE FROM newsletter_ins WHERE user_id={?}", $uid);
0337d704 42 }
43
08cce2ff 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);
c53b5964 54 XDB::execute("DELETE FROM mentor_secteurs WHERE uid={?}", $uid);
08cce2ff 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);
9bb8bf21 60
c53b5964 61 $mmlist = new MMList(S::v('uid'), S::v('password'));
9bb8bf21 62 $mmlist->kill($alias, $really_del);
0337d704 63}
64
65// }}}
66// {{{ function get_user_login()
67
68function get_user_login($data, $get_forlife = false) {
69 global $globals, $page;
70
3ab288a6 71 if (is_numeric($data)) {
08cce2ff 72 $res = XDB::query("SELECT alias FROM aliases WHERE type='a_vie' AND id={?}", $data);
0337d704 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
575dd9be 87 list($mbox, $fqdn) = explode('@', $data);
0337d704 88 if ($fqdn == $globals->mail->domain || $fqdn == $globals->mail->domain2) {
89
08cce2ff 90 $res = XDB::query("SELECT a.alias
3ab288a6 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);
0337d704 94 if ($res->numRows()) {
95 return $get_forlife ? $res->fetchOneCell() : $mbox;
0337d704 96 }
97
3ab288a6 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
0337d704 111 } elseif ($fqdn == $globals->mail->alias_dom || $fqdn == $globals->mail->alias_dom2) {
112
08cce2ff 113 $res = XDB::query("SELECT redirect
3ab288a6 114 FROM virtual_redirect
115 INNER JOIN virtual USING(vid)
116 WHERE alias={?}", $mbox.'@'.$globals->mail->alias_dom);
0337d704 117 if ($redir = $res->fetchOneCell()) {
575dd9be 118 list($alias) = explode('@', $redir);
0337d704 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
08cce2ff 127 $res = XDB::query("SELECT alias
0337d704 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:
cab08090 140 if (S::has_perms()) {
0337d704 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
155function get_user_forlife($data) {
156 return get_user_login($data, true);
157}
158
159// }}}
e7545178 160// {{{ function get_users_forlife_list()
161
162function 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// }}}
8b10988d 183// {{{ function has_user_right()
184function 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// }}}
9039c94d 194// {{{ function get_user_details_pro()
195
8b10988d 196function get_user_details_pro($uid, $view = 'private')
9039c94d 197{
9039c94d 198 $sql = "SELECT e.entreprise, s.label as secteur , ss.label as sous_secteur , f.fonction_fr as fonction,
dd502514 199 e.poste, e.adr1, e.adr2, e.adr3, e.postcode, e.city, e.entrid,
9039c94d 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";
08cce2ff 210 $res = XDB::query($sql, $uid);
8b10988d 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)) {
dd502514 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 }
8b10988d 234 }
235 if (!has_user_right($pro['tel_pub'], $view)) {
dd502514 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 }
8b10988d 246 }
247 if (!has_user_right($pro['email_pub'], $view)) {
dd502514 248 if ($pro['email'] == '')
249 $all_pro[$i]['email_pub'] = $view;
250 else
251 $all_pro[$i]['email'] = '';
8b10988d 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;
9039c94d 273}
274
275// }}}
433336f3 276// {{{ function get_user_details_adr()
277
8b10988d 278function get_user_details_adr($uid, $view = 'private') {
8b10988d 279 $sql = "SELECT a.adrid, a.adr1,a.adr2,a.adr3,a.postcode,a.city,
280 gp.pays AS countrytxt,a.region, a.regiontxt,
281 FIND_IN_SET('active', a.statut) AS active, a.adrid,
282 FIND_IN_SET('res-secondaire', a.statut) AS secondaire,
283 a.pub, gp.display
284 FROM adresses AS a
285 LEFT JOIN geoloc_pays AS gp ON (gp.a2=a.country)
286 WHERE uid= {?} AND NOT FIND_IN_SET('pro',a.statut)
287 ORDER BY NOT FIND_IN_SET('active',a.statut), FIND_IN_SET('temporaire',a.statut), FIND_IN_SET('res-secondaire',a.statut)";
08cce2ff 288 $res = XDB::query($sql, $uid);
8b10988d 289 $all_adr = $res->fetchAllAssoc();
290 $adrid_index = array();
291 foreach ($all_adr as $i => $adr) {
292 if (!has_user_right($adr['pub'], $view))
293 unset($all_adr[$i]);
294 else
295 $adrid_index[$adr['adrid']] = $i;
296 }
297
298 $sql = "SELECT t.adrid, t.tel_pub, t.tel_type, t.tel, t.telid
299 FROM tels AS t
300 INNER JOIN adresses AS a ON (a.uid = t.uid) AND (a.adrid = t.adrid)
301 WHERE t.uid = {?} AND NOT FIND_IN_SET('pro',a.statut)
302 ORDER BY t.adrid, t.tel_type DESC, t.telid";
08cce2ff 303 $restel = XDB::iterator($sql, $uid);
8b10988d 304 while ($nexttel = $restel->next()) {
305 if (has_user_right($nexttel['tel_pub'], $view)) {
306 $adrid = $nexttel['adrid'];
307 unset($nexttel['adrid']);
308 if (isset($adrid_index[$adrid])) {
309 if (!isset($all_adr[$adrid_index[$adrid]]['tels']))
310 $all_adr[$adrid_index[$adrid]]['tels'] = array($nexttel);
311 else
312 $all_adr[$adrid_index[$adrid]]['tels'][] = $nexttel;
313 }
314 }
315 }
316 return $all_adr;
317}
433336f3 318
319// }}}
0337d704 320// {{{ function get_user_details()
321
8b10988d 322function &get_user_details($login, $from_uid = '', $view = 'private')
0337d704 323{
0337d704 324 $reqsql = "SELECT u.user_id, u.promo, u.promo_sortie, u.prenom, u.nom, u.nom_usage, u.date, u.cv,
f6c71069 325 u.perms IN ('admin','user','disabled') AS inscrit, FIND_IN_SET('femme', u.flags) AS sexe, u.deces != 0 AS dcd, u.deces,
0337d704 326 q.profile_nick AS nickname, q.profile_from_ax, q.profile_mobile AS mobile, q.profile_web AS web, q.profile_freetext AS freetext,
327 q.profile_mobile_pub AS mobile_pub, q.profile_web_pub AS web_pub, q.profile_freetext_pub AS freetext_pub,
328 q.profile_medals_pub AS medals_pub,
329 IF(gp.nat='',gp.pays,gp.nat) AS nationalite, gp.a2 AS iso3166,
330 a.alias AS forlife, a2.alias AS bestalias,
331 c.uid IS NOT NULL AS is_contact,
332 s.text AS section, p.x, p.y, p.pub AS photo_pub,
dd502514 333 u.matricule_ax,
d9caac6f 334 m.expertise != '' AS is_referent,
335 COUNT(e.email) > 0 AS actif
0337d704 336 FROM auth_user_md5 AS u
337 INNER JOIN auth_user_quick AS q USING(user_id)
338 INNER JOIN aliases AS a ON (u.user_id=a.id AND a.type='a_vie')
339 INNER JOIN aliases AS a2 ON (u.user_id=a2.id AND FIND_IN_SET('bestalias',a2.flags))
340 LEFT JOIN contacts AS c ON (c.uid = {?} and c.contact = u.user_id)
341 LEFT JOIN geoloc_pays AS gp ON (gp.a2 = u.nationalite)
342 INNER JOIN sections AS s ON (s.id = u.section)
343 LEFT JOIN photo AS p ON (p.uid = u.user_id)
344 LEFT JOIN mentor AS m ON (m.uid = u.user_id)
d9caac6f 345 LEFT JOIN emails AS e ON (e.uid = u.user_id AND e.flags='active')
346 WHERE a.alias = {?}
347 GROUP BY u.user_id";
08cce2ff 348 $res = XDB::query($reqsql, $from_uid, $login);
0337d704 349 $user = $res->fetchOneAssoc();
350 $uid = $user['user_id'];
8b10988d 351 // hide orange status, cv, nickname, section
352 if (!has_user_right('private', $view)) {
353 $user['promo_sortie'] = $user['promo'] + 3;
354 $user['cv'] = '';
355 $user['nickname'] = '';
356 $user['section'] = '';
357 }
358 // hide mobile
dd502514 359 if (!has_user_right($user['mobile_pub'], $view)) {
360 if ($user['mobile'] == '')
361 $user['mobile_pub'] = $view;
362 else
363 $user['mobile'] = '';
364 }
8b10988d 365 // hide web
dd502514 366 if (!has_user_right($user['web_pub'], $view)) {
367 if ($user['web'] == '')
368 $user['web_pub'] = $view;
369 else
370 $user['web'] = '';
371 }
8b10988d 372 // hide freetext
dd502514 373 if (!has_user_right($user['freetext_pub'], $view)) {
374 if ($user['freetext'] == '')
375 $user['freetext_pub'] = $view;
376 else
377 $user['freetext'] = '';
378 }
0337d704 379
8b10988d 380 $user['adr_pro'] = get_user_details_pro($uid, $view);
381 $user['adr'] = get_user_details_adr($uid, $view);
0337d704 382
8b10988d 383 if (has_user_right('private', $view)) {
384 $sql = "SELECT text
385 FROM binets_ins
386 LEFT JOIN binets_def ON binets_ins.binet_id = binets_def.id
387 WHERE user_id = {?}";
08cce2ff 388 $res = XDB::query($sql, $uid);
8b10988d 389 $user['binets'] = $res->fetchColumn();
390 $user['binets_join'] = join(', ', $user['binets']);
79a5acea 391
08cce2ff 392 $res = XDB::iterRow("SELECT text, url
ae2f9e35 393 FROM groupesx_ins
394 LEFT JOIN groupesx_def ON groupesx_ins.gid = groupesx_def.id
395 WHERE guid = {?}", $uid);
8b10988d 396 $user['gpxs'] = Array();
ae2f9e35 397 $user['gpxs_name'] = Array();
8b10988d 398 while (list($gxt, $gxu) = $res->next()) {
399 $user['gpxs'][] = $gxu ? "<a href=\"$gxu\">$gxt</a>" : $gxt;
ae2f9e35 400 $user['gpxs_name'][] = $gxt;
8b10988d 401 }
402 $user['gpxs_join'] = join(', ', $user['gpxs']);
79a5acea 403 }
0337d704 404
08cce2ff 405 $res = XDB::iterRow("SELECT applis_def.text, applis_def.url, applis_ins.type
ae2f9e35 406 FROM applis_ins
407 INNER JOIN applis_def ON applis_def.id = applis_ins.aid
408 WHERE uid={?}
409 ORDER BY ordre", $uid);
0337d704 410
411 $user['applis_fmt'] = Array();
dd502514 412 $user['formation'] = Array();
0337d704 413 while (list($txt, $url, $type) = $res->next()) {
dd502514 414 $user['formation'][] = $txt." ".$type;
0337d704 415 require_once('applis.func.inc.php');
416 $user['applis_fmt'][] = applis_fmt($type, $txt, $url);
417 }
418 $user['applis_join'] = join(', ', $user['applis_fmt']);
419
8b10988d 420 if (has_user_right($user['medals_pub'], $view)) {
08cce2ff 421 $res = XDB::iterator("SELECT m.id, m.text AS medal, m.type, m.img, s.gid, g.text AS grade
ae2f9e35 422 FROM profile_medals_sub AS s
423 INNER JOIN profile_medals AS m ON ( s.mid = m.id )
424 LEFT JOIN profile_medals_grades AS g ON ( s.mid = g.mid AND s.gid = g.gid )
425 WHERE s.uid = {?}", $uid);
8b10988d 426 $user['medals'] = Array();
427 while ($tmp = $res->next()) {
428 $user['medals'][] = $tmp;
429 }
0337d704 430 }
431
432 return $user;
433}
dd502514 434// }}}
435// {{{ function add_user_address()
436function add_user_address($uid, $adrid, $adr) {
08cce2ff 437 XDB::execute(
dd502514 438 "INSERT INTO adresses (`uid`, `adrid`, `adr1`, `adr2`, `adr3`, `postcode`, `city`, `country`, `datemaj`, `pub`) (
439 SELECT u.user_id, {?}, {?}, {?}, {?}, {?}, {?}, gp.a2, NOW(), {?}
440 FROM auth_user_md5 AS u
441 LEFT JOIN geoloc_pays AS gp ON (gp.pays LIKE {?} OR gp.country LIKE {?} OR gp.a2 LIKE {?})
442 WHERE u.user_id = {?}
443 LIMIT 1)",
444 $adrid, $adr['adr1'], $adr['adr2'], $adr['adr3'], $adr['postcode'], $adr['city'], $adr['pub'], $adr['countrytxt'], $adr['countrytxt'], $adr['countrytxt'], $uid);
445 if (isset($adr['tels']) && is_array($adr['tels'])) {
446 $telid = 0;
447 foreach ($adr['tels'] as $tel) if ($tel['tel']) {
448 add_user_tel($uid, $adrid, $telid, $tel);
449 $telid ++;
450 }
451 }
452}
453// }}}
454// {{{ function update_user_address()
455function update_user_address($uid, $adrid, $adr) {
dd502514 456 // update address
08cce2ff 457 XDB::execute(
dd502514 458 "UPDATE adresses AS a LEFT JOIN geoloc_pays AS gp ON (gp.pays = {?})
459 SET `adr1` = {?}, `adr2` = {?}, `adr3` = {?},
460 `postcode` = {?}, `city` = {?}, a.`country` = gp.a2, `datemaj` = NOW(), `pub` = {?}
461 WHERE adrid = {?} AND uid = {?}",
462 $adr['country_txt'],
463 $adr['adr1'], $adr['adr2'], $adr['adr3'],
464 $adr['postcode'], $adr['city'], $adr['pub'], $adrid, $uid);
465 if (isset($adr['tels']) && is_array($adr['tels'])) {
08cce2ff 466 $res = XDB::query("SELECT telid FROM tels WHERE uid = {?} AND adrid = {?} ORDER BY telid", $uid, $adrid);
dd502514 467 $telids = $res->fetchColumn();
468 foreach ($adr['tels'] as $tel) {
469 if (isset($tel['telid']) && isset($tel['remove']) && $tel['remove']) {
470 remove_user_tel($uid, $adrid, $tel['telid']);
471 if (isset($telids[$tel['telid']])) unset($telids[$tel['telid']]);
472 } else if (isset($tel['telid'])) {
473 update_user_tel($uid, $adrid, $tel['telid'], $tel);
474 } else {
475 for ($telid = 0; isset($telids[$telid]) && ($telids[$telid] == $telid); $telid++);
476 add_user_tel($uid, $adrid, $telid, $tel);
477 }
478 }
479 }
480}
481// }}}
482// {{{ function remove_user_address()
483function remove_user_address($uid, $adrid) {
08cce2ff 484 XDB::execute("DELETE FROM adresses WHERE adrid = {?} AND uid = {?}", $adrid, $uid);
485 XDB::execute("DELETE FROM tels WHERE adrid = {?} AND uid = {?}", $adrid, $uid);
dd502514 486}
487// }}}
488// {{{ function add_user_tel()
489function add_user_tel($uid, $adrid, $telid, $tel) {
08cce2ff 490 XDB::execute(
dd502514 491 "INSERT INTO tels SET uid = {?}, adrid = {?}, telid = {?}, tel = {?}, tel_type = {?}, tel_pub = {?}",
492 $uid, $adrid, $telid, $tel['tel'], $tel['tel_type'], $tel['tel_pub']);
493}
494// }}}
495// {{{ function update_user_tel()
496function update_user_tel($uid, $adrid, $telid, $tel) {
08cce2ff 497 XDB::execute(
dd502514 498 "UPDATE tels SET tel = {?}, tel_type = {?}, tel_pub = {?}
499 WHERE telid = {?} AND adrid = {?} AND uid = {?}",
500 $tel['tel'], $tel['tel_type'], $tel['tel_pub'],
501 $telid, $adrid, $uid);
502}
503// }}}
504// {{{ function remove_user_tel()
505function remove_user_tel($uid, $adrid, $telid) {
a3a049fc 506 XDB::execute("DELETE FROM tels WHERE telid = {?} AND adrid = {?} AND uid = {?}",
507 $telid, $adrid, $uid);
dd502514 508}
509// }}}
510// {{{ function add_user_pro()
511function add_user_pro($uid, $entrid, $pro) {
08cce2ff 512 XDB::execute(
dd502514 513 "INSERT INTO entreprises (`uid`, `entrid`, `entreprise`, `poste`, `secteur`, `ss_secteur`, `fonction`,
514 `adr1`, `adr2`, `adr3`, `postcode`, `city`, `country`, `region`, `tel`, `fax`, `mobile`, `email`, `web`, `pub`, `adr_pub`, `tel_pub`, `email_pub`)
515 SELECT u.user_id, {?}, {?}, {?}, s.id, ss.id, f.id,
516 {?}, {?}, {?}, {?}, {?}, gp.a2, gr.region, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}
517 FROM auth_user_md5 AS u
518 LEFT JOIN emploi_secteur AS s ON(s.label LIKE {?})
519 LEFT JOIN emploi_ss_secteur AS ss ON(s.id = ss.secteur AND ss.label LIKE {?})
520 LEFT JOIN fonctions_def AS f ON(f.fonction_fr LIKE {?} OR f.fonction_en LIKE {?})
521 LEFT JOIN geoloc_pays AS gp ON (gp.country LIKE {?} OR gp.pays LIKE {?})
522 LEFT JOIN geoloc_region AS gr ON (gr.a2 = gp.a2 AND gr.name LIKE {?})
523 WHERE u.user_id = {?}
524 LIMIT 1",
525 $entrid, $pro['entreprise'], $pro['poste'],
526 $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'],
527 $pro['secteur'], $pro['sous_secteur'], $pro['fonction'], $pro['fonction'],
528 $pro['countrytxt'], $pro['countrytxt'], $pro['region'],
529 $uid);
530}
531// }}}
532// {{{ function update_user_pro()
533function update_user_pro($uid, $entrid, $pro) {
dd502514 534 $join = "";
535 $set = "";
536 $args_join = array();
537 $args_set = array();
538
539 $join .= "LEFT JOIN emploi_secteur AS s ON(s.label LIKE {?})
540 LEFT JOIN emploi_ss_secteur AS ss ON(s.id = ss.secteur AND ss.label LIKE {?})
541 LEFT JOIN fonctions_def AS f ON(f.fonction_fr LIKE {?} OR f.fonction_en LIKE {?})";
542 $args_join[] = $pro['secteur'];
543 $args_join[] = $pro['sous_secteur'];
544 $args_join[] = $pro['fonction'];
545 $args_join[] = $pro['fonction'];
546 $set .= ", e.`entreprise` = {?}, e.`secteur` = s.id, e.`ss_secteur` = ss.id, e.`fonction` = f.id, e.`poste`= {?}, e.`web` = {?}, e.`pub` = {?}";
547 $args_set[] = $pro['entreprise'];
548 $args_set[] = $pro['poste'];
549 $args_set[] = $pro['web'];
550 $args_set[] = $pro['pub'];
551
552 if (isset($pro['adr1'])) {
553 $join .= "LEFT JOIN geoloc_pays AS gp ON (gp.country LIKE {?} OR gp.pays LIKE {?})
554 LEFT JOIN geoloc_region AS gr ON (gr.a2 = gp.a2 AND gr.name LIKE {?})";
555 $args_join[] = $pro['countrytxt'];
556 $args_join[] = $pro['countrytxt'];
557 $args_join[] = $pro['region'];
558 $set .= ", e.`adr1` = {?}, e.`adr2` = {?}, e.`adr3` = {?}, e.`postcode` = {?}, e.`city` = {?}, e.`country` = gp.a2, e.`region` = gr.region, e.`adr_pub` = {?}";
559 $args_set[] = $pro['adr1'];
560 $args_set[] = $pro['adr2'];
561 $args_set[] = $pro['adr3'];
562 $args_set[] = $pro['postcode'];
563 $args_set[] = $pro['city'];
564 $args_set[] = $pro['adr_pub'];
565 }
566
567 if (isset($pro['tel'])) {
568 $set .= ", e.`tel` = {?}, e.`fax` = {?}, e.`mobile` = {?}, e.tel_pub = {?}";
569 $args_set[] = $pro['tel'];
570 $args_set[] = $pro['fax'];
571 $args_set[] = $pro['mobile'];
572 $args_set[] = $pro['tel_pub'];
573 }
574 if (isset($pro['email'])) {
575 $set .= ", e.`email` = {?}, e.`email_pub` = {?}";
576 $args_set[] = $pro['email'];
577 $args_set[] = $pro['email_pub'];
578 }
579 $query = "UPDATE entreprises AS e ".$join." SET ".substr($set,1)." WHERE e.uid = {?} AND e.entrid = {?}";
580 $args_where = array($uid, $entrid);
581 $args = array_merge(array($query), $args_join, $args_set, $args_where);
a3a049fc 582 call_user_func_array(array('XDB', 'execute'), $args);
dd502514 583}
584// }}}
585// {{{ function remove_user_pro()
586function remove_user_pro($uid, $entrid) {
08cce2ff 587 XDB::execute("DELETE FROM entreprises WHERE entrid = {?} AND uid = {?}", $entrid, $uid);
dd502514 588}
589// }}}
590// {{{ function set_user_details()
591function set_user_details_addresses($uid, $adrs) {
08cce2ff 592 $res = XDB::query("SELECT adrid FROM adresses WHERE uid = {?} AND adrid >= 1 ORDER BY adrid", $uid);
dd502514 593 $adrids = $res->fetchColumn();
594 foreach ($adrs as $adr) {
595 if (isset($adr['adrid']) && isset($adr['remove']) && $adr['remove']) {
596 remove_user_address($uid, $adr['adrid']);
597 if (isset($adrids[$adr['adrid']])) unset($adrids[$adr['adrid']]);
598 } else if (isset($adr['adrid'])) {
599 update_user_address($uid, $adr['adrid'], $adr);
600 } else {
601 for ($adrid = 1; isset($adrids[$adrid-1]) && ($adrids[$adrid-1] == $adrid); $adrid++);
602 add_user_address($uid, $adrid, $adr);
603 $adrids[$adrid-1] = $adrid;
604 }
605 }
606 require_once 'geoloc.inc.php';
607 localize_addresses($uid);
608}
609// }}}
610// {{{ function set_user_details_pro()
611
612function set_user_details_pro($uid, $pros)
613{
08cce2ff 614 $res = XDB::query("SELECT entrid FROM entreprises WHERE uid = {?} ORDER BY entrid", $uid);
dd502514 615 $entrids = $res->fetchColumn();
616 foreach ($pros as $pro) {
617 if (isset($pro['entrid']) && isset($pro['remove']) && $pro['remove']) {
618 remove_user_pro($uid, $pro['entrid']);
619 if (isset($entrids[$pro['entrid']])) unset($entrids[$pro['entrid']]);
620 } else if (isset($pro['entrid'])) {
621 update_user_pro($uid, $pro['entrid'], $pro);
622 } else {
623 for ($entrid = 0; isset($entrids[$entrid]) && ($entrids[$entrid] == $entrid); $entrid++);
624 add_user_pro($uid, $entrid, $pro);
625 }
626 }
627}
0337d704 628
629// }}}
dd502514 630// {{{ function set_user_details()
631function set_user_details($uid, $details) {
dd502514 632 if (isset($details['nom_usage'])) {
08cce2ff 633 XDB::execute("UPDATE auth_user_md5 SET nom_usage = {?} WHERE user_id = {?}", strtoupper($details['nom_usage']), $uid);
dd502514 634 }
635 if (isset($details['mobile'])) {
08cce2ff 636 XDB::execute("UPDATE auth_user_quick SET profile_mobile = {?} WHERE user_id = {?}", $details['mobile'], $uid);
dd502514 637 }
638 if (isset($details['nationalite'])) {
08cce2ff 639 XDB::execute(
dd502514 640 "UPDATE auth_user_md5 AS u
641 INNER JOIN geoloc_pays AS gp
642 SET u.nationalite = gp.a2
643 WHERE (gp.a2 = {?} OR gp.nat = {?})
644 AND u.user_id = {?}", $details['nationalite'], $details['nationalite'], $uid);
645 }
646 if (isset($details['adr']) && is_array($details['adr']))
647 set_user_details_addresses($uid, $details['adr']);
648 if (isset($details['adr_pro']) && is_array($details['adr_pro']))
649 set_user_details_pro($uid, $details['adr_pro']);
650 if (isset($details['binets']) && is_array($details['binets'])) {
08cce2ff 651 XDB::execute("DELETE FROM binets_ins WHERE user_id = {?}", $uid);
dd502514 652 foreach ($details['binets'] as $binet)
08cce2ff 653 XDB::execute(
dd502514 654 "INSERT INTO binets_ins (`user_id`, `binet_id`)
655 SELECT {?}, id FROM binets_def WHERE text = {?} LIMIT 1",
656 $uid, $binet);
657 }
658 if (isset($details['gpxs']) && is_array($details['gpxs'])) {
08cce2ff 659 XDB::execute("DELETE FROM groupesx_ins WHERE user_id = {?}", $uid);
dd502514 660 foreach ($details['gpxs'] as $groupex) {
661 if (preg_match('/<a href="[^"]*">([^<]+)</a>/', $groupex, $a)) $groupex = $a[1];
08cce2ff 662 XDB::execute(
dd502514 663 "INSERT INTO groupesx_ins (`user_id`, `binet_id`)
664 SELECT {?}, id FROM groupesx_def WHERE text = {?} LIMIT 1",
665 $uid, $groupex);
666 }
667 }
668 // applis
669 // medals
670}
671// }}}
0337d704 672// {{{ function _user_reindex
673
674function _user_reindex($uid, $keys, $muls) {
0337d704 675 foreach ($keys as $i => $key) {
676 if ($key == '') {
677 continue;
678 }
679 $toks = preg_split('/[ \'\-]+/', $key);
680 $token = "";
681 $first = 5;
682 while ($toks) {
683 $token = strtolower(replace_accent(array_pop($toks) . $token));
684 $score = ($toks ? 0 : 10 + $first) * $muls[$i];
685 mysql_query("REPLACE INTO search_name (token, uid, score) VALUES('$token',$uid,$score)");
686 $first = 0;
687 }
688 }
689}
690
691// }}}
692// {{{ function user_reindex
693
694function user_reindex($uid) {
08cce2ff 695 XDB::execute("DELETE FROM search_name WHERE uid={?}", $uid);
696 $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);
0337d704 697 _user_reindex($uid, $res->fetchOneRow(), array(1,1,1,0.2));
698}
699
700// }}}
433336f3 701// {{{ function set_new_usage()
74c55fd6 702
703function set_new_usage($uid, $usage, $alias=false) {
704 XDB::execute("UPDATE auth_user_md5 set nom_usage={?} WHERE user_id={?}",$usage ,$uid);
705 XDB::execute("DELETE FROM aliases WHERE FIND_IN_SET('usage',flags) AND id={?}", $uid);
706 if ($alias && $usage) {
707 XDB::execute("UPDATE aliases SET flags=flags & 255-1 WHERE id={?}", $uid);
708 XDB::execute("INSERT INTO aliases VALUES({?}, 'alias', 'usage,bestalias', {?}, null)",
709 $alias, $uid);
710 }
711 $r = XDB::query("SELECT alias FROM aliases WHERE FIND_IN_SET('bestalias', flags) AND id = {?}", $uid);
712 if ($r->fetchOneCell() == "") {
713 XDB::execute("UPDATE aliases SET flags = 1 | flags WHERE id = {?} LIMIT 1", $uid);
714 }
715 require_once 'user.func.inc.php';
716 user_reindex($uid);
717}
718
433336f3 719// }}}
720// {{{ function get_X_mat
721function get_X_mat($ourmat)
722{
723 if (!preg_match('/^[0-9]{8}$/', $ourmat)) {
724 // le matricule de notre base doit comporter 8 chiffres
725 return 0;
726 }
727
728 $year = intval(substr($ourmat, 0, 4));
729 $rang = intval(substr($ourmat, 5, 3));
730 if ($year < 1996) {
731 return;
732 } elseif ($year < 2000) {
733 $year = intval(substr(1900 - $year, 1, 3));
734 return sprintf('%02u0%03u', $year, $rang);
735 } else {
736 $year = intval(substr(1900 - $year, 1, 3));
737 return sprintf('%03u%03u', $year, $rang);
738 }
739}
740
741// }}}
742
743
0337d704 744// vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
745?>