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