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