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