prise en compte des pub ax ou private dans la fonction get_user_detail
[platal.git] / include / user.func.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 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, applis_ins, binets_ins, contacts, groupesx_ins, homonymes, identification_ax, photo
30 // delete in : auth_user_md5, auth_user_quick, competences_ins, emails, entreprises, langues_ins, mentor,
31 // mentor_pays, mentor_secteurs, newsletter_ins, perte_pass, requests, user_changes, virtual_redirect, watch_sub
32 // + delete maillists
33
34 global $globals;
35 $uid = intval($user_id);
36 $res = $globals->xdb->query("SELECT alias FROM aliases WHERE type='a_vie' AND id={?}", $uid);
37 $alias = $res->fetchOneCell();
38
39 if ($really_del) {
40 $globals->xdb->execute("DELETE FROM emails WHERE uid={?}", $uid);
41 $globals->xdb->execute("DELETE FROM newsletter_ins WHERE user_id={?}", $uid);
42 }
43
44 $globals->xdb->execute("DELETE FROM virtual_redirect WHERE redirect = {?}", $alias.'@'.$globals->mail->domain);
45 $globals->xdb->execute("DELETE FROM virtual_redirect WHERE redirect = {?}", $alias.'@'.$globals->mail->domain2);
46
47 $globals->xdb->execute("UPDATE auth_user_md5 SET password='',smtppass='' WHERE user_id={?}", $uid);
48 $globals->xdb->execute("UPDATE auth_user_quick SET watch_flags='' WHERE user_id={?}", $uid);
49
50 $globals->xdb->execute("DELETE FROM competences_ins WHERE uid={?}", $uid);
51 $globals->xdb->execute("DELETE FROM entreprises WHERE uid={?}", $uid);
52 $globals->xdb->execute("DELETE FROM langues_ins WHERE uid={?}", $uid);
53 $globals->xdb->execute("DELETE FROM mentor_pays WHERE uid={?}", $uid);
54 $globals->xdb->execute("DELETE FROM mentor_secteur WHERE uid={?}", $uid);
55 $globals->xdb->execute("DELETE FROM mentor WHERE uid={?}", $uid);
56 $globals->xdb->execute("DELETE FROM perte_pass WHERE uid={?}", $uid);
57 $globals->xdb->execute("DELETE FROM requests WHERE user_id={?}", $uid);
58 $globals->xdb->execute("DELETE FROM user_changes WHERE user_id={?}", $uid);
59 $globals->xdb->execute("DELETE FROM watch_sub WHERE uid={?}", $uid);
60
61 include_once('lists.inc.php');
62 if (function_exists(lists_xmlrpc)) {
63 $client =& lists_xmlrpc(Session::getInt('id'), Session::get('password'));
64 $client->kill($alias, $really_del);
65 }
66 }
67
68 // }}}
69 // {{{ function get_user_login()
70
71 function get_user_login($data, $get_forlife = false) {
72 global $globals, $page;
73
74 if (preg_match(',^[0-9]*$,', $data)) {
75 $res = $globals->xdb->query("SELECT alias FROM aliases WHERE type='a_vie' AND id={?}", $data);
76 if ($res->numRows()) {
77 return $res->fetchOneCell();
78 } else {
79 $page->trig("il n'y a pas d'utilisateur avec cet id");
80 return false;
81 }
82 }
83
84 $data = trim(strtolower($data));
85
86 if (strstr($data, '@')===false) {
87 $data = $data.'@'.$globals->mail->domain;
88 }
89
90 list($mbox, $fqdn) = explode('@', $data);
91 if ($fqdn == $globals->mail->domain || $fqdn == $globals->mail->domain2) {
92
93 $res = $globals->xdb->query("SELECT a.alias
94 FROM aliases AS a
95 INNER JOIN aliases AS b ON (a.id = b.id AND b.type IN ('alias', 'a_vie') AND b.alias={?})
96 WHERE a.type = 'a_vie'", $mbox);
97 if ($res->numRows()) {
98 return $get_forlife ? $res->fetchOneCell() : $mbox;
99 } else {
100 $page->trig("il n'y a pas d'utilisateur avec ce login");
101 return false;
102 }
103
104 } elseif ($fqdn == $globals->mail->alias_dom || $fqdn == $globals->mail->alias_dom2) {
105
106 $res = $globals->xdb->query("SELECT redirect
107 FROM virtual_redirect
108 INNER JOIN virtual USING(vid)
109 WHERE alias={?}", $mbox.'@'.$globals->mail->alias_dom);
110 if ($redir = $res->fetchOneCell()) {
111 list($alias) = explode('@', $redir);
112 } else {
113 $page->trig("il n'y a pas d'utilisateur avec cet alias");
114 $alias = false;
115 }
116 return $alias;
117
118 } else {
119
120 $res = $globals->xdb->query("SELECT alias
121 FROM aliases AS a
122 INNER JOIN emails AS e ON e.uid=a.id
123 WHERE e.email={?} AND a.type='a_vie'", $data);
124 switch ($i = $res->numRows()) {
125 case 0:
126 $page->trig("il n'y a pas d'utilisateur avec cette addresse mail");
127 return false;
128
129 case 1:
130 return $res->fetchOneCell();
131
132 default:
133 if (has_perms()) {
134 $aliases = $res->fetchColumn();
135 $page->trig("Il y a $i utilisateurs avec cette adresse mail : ".join(', ', $aliases));
136 } else {
137 $res->free();
138 }
139 }
140 }
141
142 return false;
143 }
144
145 // }}}
146 // {{{ function get_user_forlife()
147
148 function get_user_forlife($data) {
149 return get_user_login($data, true);
150 }
151
152 // }}}
153 // {{{ function has_user_right()
154 function has_user_right($pub, $view = 'private') {
155 if ($pub == $view) return true;
156 // all infos available for private
157 if ($view == 'private') return true;
158 // public infos available for all
159 if ($pub == 'public') return true;
160 // here we have view = ax or public, and pub = ax or private, and pub != view
161 return false;
162 }
163 // }}}
164 // {{{ function get_user_details_pro()
165
166 function get_user_details_pro($uid, $view = 'private')
167 {
168 global $globals;
169 $sql = "SELECT e.entreprise, s.label as secteur , ss.label as sous_secteur , f.fonction_fr as fonction,
170 e.poste, e.adr1, e.adr2, e.adr3, e.postcode, e.city,
171 gp.pays AS countrytxt, gr.name AS region, e.tel, e.fax, e.mobile, e.entrid,
172 e.pub, e.adr_pub, e.tel_pub, e.email, e.email_pub, e.web
173 FROM entreprises AS e
174 LEFT JOIN emploi_secteur AS s ON(e.secteur = s.id)
175 LEFT JOIN emploi_ss_secteur AS ss ON(e.ss_secteur = ss.id AND e.secteur = ss.secteur)
176 LEFT JOIN fonctions_def AS f ON(e.fonction = f.id)
177 LEFT JOIN geoloc_pays AS gp ON (gp.a2 = e.country)
178 LEFT JOIN geoloc_region AS gr ON (gr.a2 = e.country and gr.region = e.region)
179 WHERE e.uid = {?}
180 ORDER BY e.entrid";
181 $res = $globals->xdb->query($sql, $uid);
182 $all_pro = $res->fetchAllAssoc();
183 foreach ($all_pro as $i => $pro) {
184 if (!has_user_right($pro['pub'], $view))
185 unset($all_pro[$i]);
186 else {
187 if (!has_user_right($pro['adr_pub'], $view)) {
188 $all_pro[$i]['adr1'] = '';
189 $all_pro[$i]['adr2'] = '';
190 $all_pro[$i]['adr3'] = '';
191 $all_pro[$i]['postcode'] = '';
192 $all_pro[$i]['city'] = '';
193 $all_pro[$i]['countrytxt'] = '';
194 $all_pro[$i]['region'] = '';
195 }
196 if (!has_user_right($pro['tel_pub'], $view)) {
197 $all_pro[$i]['tel'] = '';
198 $all_pro[$i]['fax'] = '';
199 $all_pro[$i]['mobile'] = '';
200 }
201 if (!has_user_right($pro['email_pub'], $view)) {
202 $all_pro[$i]['email'] = '';
203 }
204 if ($all_pro[$i]['adr1'] == '' &&
205 $all_pro[$i]['adr2'] == '' &&
206 $all_pro[$i]['adr3'] == '' &&
207 $all_pro[$i]['postcode'] == '' &&
208 $all_pro[$i]['city'] == '' &&
209 $all_pro[$i]['countrytxt'] == '' &&
210 $all_pro[$i]['region'] == '' &&
211 $all_pro[$i]['entreprise'] == '' &&
212 $all_pro[$i]['fonction'] == '' &&
213 $all_pro[$i]['secteur'] == '' &&
214 $all_pro[$i]['poste'] == '' &&
215 $all_pro[$i]['tel'] == '' &&
216 $all_pro[$i]['fax'] == '' &&
217 $all_pro[$i]['mobile'] == '' &&
218 $all_pro[$i]['email'] == '')
219 unset($all_pro[$i]);
220 }
221 }
222 if (!count($all_pro)) return false;
223 return $all_pro;
224 }
225
226 // }}}
227 function get_user_details_adr($uid, $view = 'private') {
228 global $globals;
229 $sql = "SELECT a.adrid, a.adr1,a.adr2,a.adr3,a.postcode,a.city,
230 gp.pays AS countrytxt,a.region, a.regiontxt,
231 FIND_IN_SET('active', a.statut) AS active, a.adrid,
232 FIND_IN_SET('res-secondaire', a.statut) AS secondaire,
233 a.pub, gp.display
234 FROM adresses AS a
235 LEFT JOIN geoloc_pays AS gp ON (gp.a2=a.country)
236 WHERE uid= {?} AND NOT FIND_IN_SET('pro',a.statut)
237 ORDER BY NOT FIND_IN_SET('active',a.statut), FIND_IN_SET('temporaire',a.statut), FIND_IN_SET('res-secondaire',a.statut)";
238 $res = $globals->xdb->query($sql, $uid);
239 $all_adr = $res->fetchAllAssoc();
240 $adrid_index = array();
241 foreach ($all_adr as $i => $adr) {
242 if (!has_user_right($adr['pub'], $view))
243 unset($all_adr[$i]);
244 else
245 $adrid_index[$adr['adrid']] = $i;
246 }
247
248 $sql = "SELECT t.adrid, t.tel_pub, t.tel_type, t.tel, t.telid
249 FROM tels AS t
250 INNER JOIN adresses AS a ON (a.uid = t.uid) AND (a.adrid = t.adrid)
251 WHERE t.uid = {?} AND NOT FIND_IN_SET('pro',a.statut)
252 ORDER BY t.adrid, t.tel_type DESC, t.telid";
253 $restel = $globals->xdb->iterator($sql, $uid);
254 while ($nexttel = $restel->next()) {
255 if (has_user_right($nexttel['tel_pub'], $view)) {
256 $adrid = $nexttel['adrid'];
257 unset($nexttel['adrid']);
258 if (isset($adrid_index[$adrid])) {
259 if (!isset($all_adr[$adrid_index[$adrid]]['tels']))
260 $all_adr[$adrid_index[$adrid]]['tels'] = array($nexttel);
261 else
262 $all_adr[$adrid_index[$adrid]]['tels'][] = $nexttel;
263 }
264 }
265 }
266 return $all_adr;
267 }
268 // {{{ function get_user_details()
269
270 function &get_user_details($login, $from_uid = '', $view = 'private')
271 {
272 global $globals;
273 $reqsql = "SELECT u.user_id, u.promo, u.promo_sortie, u.prenom, u.nom, u.nom_usage, u.date, u.cv,
274 u.perms IN ('admin','user') AS inscrit, FIND_IN_SET('femme', u.flags) AS sexe, u.deces != 0 AS dcd, u.deces,
275 q.profile_nick AS nickname, q.profile_from_ax, q.profile_mobile AS mobile, q.profile_web AS web, q.profile_freetext AS freetext,
276 q.profile_mobile_pub AS mobile_pub, q.profile_web_pub AS web_pub, q.profile_freetext_pub AS freetext_pub,
277 q.profile_medals_pub AS medals_pub,
278 IF(gp.nat='',gp.pays,gp.nat) AS nationalite, gp.a2 AS iso3166,
279 a.alias AS forlife, a2.alias AS bestalias,
280 c.uid IS NOT NULL AS is_contact,
281 s.text AS section, p.x, p.y, p.pub AS photo_pub,
282 m.expertise != '' AS is_referent
283
284 FROM auth_user_md5 AS u
285 INNER JOIN auth_user_quick AS q USING(user_id)
286 INNER JOIN aliases AS a ON (u.user_id=a.id AND a.type='a_vie')
287 INNER JOIN aliases AS a2 ON (u.user_id=a2.id AND FIND_IN_SET('bestalias',a2.flags))
288 LEFT JOIN contacts AS c ON (c.uid = {?} and c.contact = u.user_id)
289 LEFT JOIN geoloc_pays AS gp ON (gp.a2 = u.nationalite)
290 INNER JOIN sections AS s ON (s.id = u.section)
291 LEFT JOIN photo AS p ON (p.uid = u.user_id)
292 LEFT JOIN mentor AS m ON (m.uid = u.user_id)
293 WHERE a.alias = {?}";
294 $res = $globals->xdb->query($reqsql, $from_uid, $login);
295 $user = $res->fetchOneAssoc();
296 $uid = $user['user_id'];
297 // hide orange status, cv, nickname, section
298 if (!has_user_right('private', $view)) {
299 $user['promo_sortie'] = $user['promo'] + 3;
300 $user['cv'] = '';
301 $user['nickname'] = '';
302 $user['section'] = '';
303 }
304 // hide mobile
305 if (!has_user_right($user['mobile_pub'], $view)) $user['mobile'] = '';
306 // hide web
307 if (!has_user_right($user['web_pub'], $view)) $user['web'] = '';
308 // hide freetext
309 if (!has_user_right($user['freetext_pub'], $view)) $user['freetext'] = '';
310
311 $user['adr_pro'] = get_user_details_pro($uid, $view);
312 $user['adr'] = get_user_details_adr($uid, $view);
313
314 if (has_user_right('private', $view)) {
315 $sql = "SELECT text
316 FROM binets_ins
317 LEFT JOIN binets_def ON binets_ins.binet_id = binets_def.id
318 WHERE user_id = {?}";
319 $res = $globals->xdb->query($sql, $uid);
320 $user['binets'] = $res->fetchColumn();
321 $user['binets_join'] = join(', ', $user['binets']);
322
323 $res = $globals->xdb->iterRow("SELECT text, url
324 FROM groupesx_ins
325 LEFT JOIN groupesx_def ON groupesx_ins.gid = groupesx_def.id
326 WHERE guid = {?}", $uid);
327 $user['gpxs'] = Array();
328 while (list($gxt, $gxu) = $res->next()) {
329 $user['gpxs'][] = $gxu ? "<a href=\"$gxu\">$gxt</a>" : $gxt;
330 }
331 $user['gpxs_join'] = join(', ', $user['gpxs']);
332 }
333
334 $res = $globals->xdb->iterRow("SELECT applis_def.text, applis_def.url, applis_ins.type
335 FROM applis_ins
336 INNER JOIN applis_def ON applis_def.id = applis_ins.aid
337 WHERE uid={?}
338 ORDER BY ordre", $uid);
339
340 $user['applis_fmt'] = Array();
341 while (list($txt, $url, $type) = $res->next()) {
342 require_once('applis.func.inc.php');
343 $user['applis_fmt'][] = applis_fmt($type, $txt, $url);
344 }
345 $user['applis_join'] = join(', ', $user['applis_fmt']);
346
347 if (has_user_right($user['medals_pub'], $view)) {
348 $res = $globals->xdb->iterator("SELECT m.id, m.text AS medal, m.type, m.img, s.gid, g.text AS grade
349 FROM profile_medals_sub AS s
350 INNER JOIN profile_medals AS m ON ( s.mid = m.id )
351 LEFT JOIN profile_medals_grades AS g ON ( s.mid = g.mid AND s.gid = g.gid )
352 WHERE s.uid = {?}", $uid);
353 $user['medals'] = Array();
354 while ($tmp = $res->next()) {
355 $user['medals'][] = $tmp;
356 }
357 }
358
359 return $user;
360 }
361
362 // }}}
363 // {{{ function _user_reindex
364
365 function _user_reindex($uid, $keys, $muls) {
366 global $globals;
367 foreach ($keys as $i => $key) {
368 if ($key == '') {
369 continue;
370 }
371 $toks = preg_split('/[ \'\-]+/', $key);
372 $token = "";
373 $first = 5;
374 while ($toks) {
375 $token = strtolower(replace_accent(array_pop($toks) . $token));
376 $score = ($toks ? 0 : 10 + $first) * $muls[$i];
377 mysql_query("REPLACE INTO search_name (token, uid, score) VALUES('$token',$uid,$score)");
378 $first = 0;
379 }
380 }
381 }
382
383 // }}}
384 // {{{ function user_reindex
385
386 function user_reindex($uid) {
387 global $globals;
388 $globals->xdb->execute("DELETE FROM search_name WHERE uid={?}", $uid);
389 $res = $globals->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);
390 _user_reindex($uid, $res->fetchOneRow(), array(1,1,1,0.2));
391 }
392
393 // }}}
394 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
395 ?>