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