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