nb de tels variables par adresse, wish 400
[platal.git] / include / user.func.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2004 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 */
27function 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
71function 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
575dd9be 90 list($mbox, $fqdn) = explode('@', $data);
0337d704 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()) {
575dd9be 111 list($alias) = explode('@', $redir);
0337d704 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
148function get_user_forlife($data) {
149 return get_user_login($data, true);
150}
151
152// }}}
9039c94d 153// {{{ function get_user_details_pro()
154
155function get_user_details_pro($uid)
156{
157 global $globals;
158 $sql = "SELECT e.entreprise, s.label as secteur , ss.label as sous_secteur , f.fonction_fr as fonction,
159 e.poste, e.adr1, e.adr2, e.adr3, e.postcode, e.city,
160 gp.pays AS countrytxt, gr.name AS region, e.tel, e.fax, e.mobile, e.entrid,
161 e.pub, e.adr_pub, e.tel_pub, e.email, e.email_pub, e.web
162 FROM entreprises AS e
163 LEFT JOIN emploi_secteur AS s ON(e.secteur = s.id)
164 LEFT JOIN emploi_ss_secteur AS ss ON(e.ss_secteur = ss.id AND e.secteur = ss.secteur)
165 LEFT JOIN fonctions_def AS f ON(e.fonction = f.id)
166 LEFT JOIN geoloc_pays AS gp ON (gp.a2 = e.country)
167 LEFT JOIN geoloc_region AS gr ON (gr.a2 = e.country and gr.region = e.region)
168 WHERE e.uid = {?}
169 ORDER BY e.entrid";
170 $res = $globals->xdb->query($sql, $uid);
171 return $res->fetchAllAssoc();
172}
173
174// }}}
0337d704 175// {{{ function get_user_details()
176
177function &get_user_details($login, $from_uid = '')
178{
179 global $globals;
180 $reqsql = "SELECT u.user_id, u.promo, u.promo_sortie, u.prenom, u.nom, u.nom_usage, u.date, u.cv,
181 u.perms IN ('admin','user') AS inscrit, FIND_IN_SET('femme', u.flags) AS sexe, u.deces != 0 AS dcd, u.deces,
182 q.profile_nick AS nickname, q.profile_from_ax, q.profile_mobile AS mobile, q.profile_web AS web, q.profile_freetext AS freetext,
183 q.profile_mobile_pub AS mobile_pub, q.profile_web_pub AS web_pub, q.profile_freetext_pub AS freetext_pub,
184 q.profile_medals_pub AS medals_pub,
185 IF(gp.nat='',gp.pays,gp.nat) AS nationalite, gp.a2 AS iso3166,
186 a.alias AS forlife, a2.alias AS bestalias,
187 c.uid IS NOT NULL AS is_contact,
188 s.text AS section, p.x, p.y, p.pub AS photo_pub,
189 m.expertise != '' AS is_referent
190
191 FROM auth_user_md5 AS u
192 INNER JOIN auth_user_quick AS q USING(user_id)
193 INNER JOIN aliases AS a ON (u.user_id=a.id AND a.type='a_vie')
194 INNER JOIN aliases AS a2 ON (u.user_id=a2.id AND FIND_IN_SET('bestalias',a2.flags))
195 LEFT JOIN contacts AS c ON (c.uid = {?} and c.contact = u.user_id)
196 LEFT JOIN geoloc_pays AS gp ON (gp.a2 = u.nationalite)
197 INNER JOIN sections AS s ON (s.id = u.section)
198 LEFT JOIN photo AS p ON (p.uid = u.user_id)
199 LEFT JOIN mentor AS m ON (m.uid = u.user_id)
200 WHERE a.alias = {?}";
201 $res = $globals->xdb->query($reqsql, $from_uid, $login);
202 $user = $res->fetchOneAssoc();
203 $uid = $user['user_id'];
204
9039c94d 205 $user['adr_pro'] = get_user_details_pro($uid);
0337d704 206
79a5acea 207 $sql = "SELECT a.adrid, a.adr1,a.adr2,a.adr3,a.postcode,a.city,
208 gp.pays AS countrytxt,a.region, a.regiontxt,
0337d704 209 FIND_IN_SET('active', a.statut) AS active, a.adrid,
210 FIND_IN_SET('res-secondaire', a.statut) AS secondaire,
79a5acea 211 a.pub, gp.display
0337d704 212 FROM adresses AS a
213 LEFT JOIN geoloc_pays AS gp ON (gp.a2=a.country)
0337d704 214 WHERE uid= {?} AND NOT FIND_IN_SET('pro',a.statut)
215 ORDER BY NOT FIND_IN_SET('active',a.statut), FIND_IN_SET('temporaire',a.statut), FIND_IN_SET('res-secondaire',a.statut)";
216 $res = $globals->xdb->query($sql, $uid);
217 $user['adr'] = $res->fetchAllAssoc();
79a5acea 218 $adrid_index = array();
219 foreach ($user['adr'] as $i => $adr)
220 $adrid_index[$adr['adrid']] = $i;
221
222 $sql = "SELECT t.adrid, t.tel_pub, t.tel_type, t.tel
223 FROM tels AS t
224 INNER JOIN adresses AS a ON (a.uid = t.uid) AND (a.adrid = t.adrid)
225 WHERE t.uid = {?} AND NOT FIND_IN_SET('pro',a.statut)
226 ORDER BY t.adrid, t.tel_type DESC, t.telid";
227 $restel = $globals->xdb->iterator($sql, $uid);
228 while ($nexttel = $restel->next()) {
229 $adrid = $nexttel['adrid'];
230 unset($nexttel['adrid']);
231 if (!isset($user['adr'][$adrid_index[$adrid]]['tels']))
232 $user['adr'][$adrid_index[$adrid]]['tels'] = array($nexttel);
233 else
234 $user['adr'][$adrid_index[$adrid]]['tels'][] = $nexttel;
235 }
0337d704 236
237 $sql = "SELECT text
238 FROM binets_ins
239 LEFT JOIN binets_def ON binets_ins.binet_id = binets_def.id
240 WHERE user_id = {?}";
241 $res = $globals->xdb->query($sql, $uid);
242 $user['binets'] = $res->fetchColumn();
243 $user['binets_join'] = join(', ', $user['binets']);
244
245 $res = $globals->xdb->iterRow("SELECT text, url
246 FROM groupesx_ins
247 LEFT JOIN groupesx_def ON groupesx_ins.gid = groupesx_def.id
248 WHERE guid = {?}", $uid);
249 $user['gpxs'] = Array();
250 while (list($gxt, $gxu) = $res->next()) {
251 $user['gpxs'][] = $gxu ? "<a href=\"$gxu\">$gxt</a>" : $gxt;
252 }
253 $user['gpxs_join'] = join(', ', $user['gpxs']);
254
255 $res = $globals->xdb->iterRow("SELECT applis_def.text, applis_def.url, applis_ins.type
256 FROM applis_ins
257 INNER JOIN applis_def ON applis_def.id = applis_ins.aid
258 WHERE uid={?}
259 ORDER BY ordre", $uid);
260
261 $user['applis_fmt'] = Array();
262 while (list($txt, $url, $type) = $res->next()) {
263 require_once('applis.func.inc.php');
264 $user['applis_fmt'][] = applis_fmt($type, $txt, $url);
265 }
266 $user['applis_join'] = join(', ', $user['applis_fmt']);
267
268 $res = $globals->xdb->iterator("SELECT m.id, m.text AS medal, m.type, m.img, s.gid, g.text AS grade
269 FROM profile_medals_sub AS s
270 INNER JOIN profile_medals AS m ON ( s.mid = m.id )
271 LEFT JOIN profile_medals_grades AS g ON ( s.mid = g.mid AND s.gid = g.gid )
272 WHERE s.uid = {?}", $uid);
273 $user['medals'] = Array();
274 while ($tmp = $res->next()) {
275 $user['medals'][] = $tmp;
276 }
277
278 return $user;
279}
280
281// }}}
282// {{{ function _user_reindex
283
284function _user_reindex($uid, $keys, $muls) {
285 global $globals;
286 foreach ($keys as $i => $key) {
287 if ($key == '') {
288 continue;
289 }
290 $toks = preg_split('/[ \'\-]+/', $key);
291 $token = "";
292 $first = 5;
293 while ($toks) {
294 $token = strtolower(replace_accent(array_pop($toks) . $token));
295 $score = ($toks ? 0 : 10 + $first) * $muls[$i];
296 mysql_query("REPLACE INTO search_name (token, uid, score) VALUES('$token',$uid,$score)");
297 $first = 0;
298 }
299 }
300}
301
302// }}}
303// {{{ function user_reindex
304
305function user_reindex($uid) {
306 global $globals;
307 $globals->xdb->execute("DELETE FROM search_name WHERE uid={?}", $uid);
308 $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);
309 _user_reindex($uid, $res->fetchOneRow(), array(1,1,1,0.2));
310}
311
312// }}}
313// vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
314?>