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