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