| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2010 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 | class User extends PlUser |
| 23 | { |
| 24 | private $_profile_fetched = false; |
| 25 | private $_profile = null; |
| 26 | |
| 27 | // Additional fields (non core) |
| 28 | protected $promo = null; |
| 29 | |
| 30 | // Implementation of the login to uid method. |
| 31 | protected function getLogin($login) |
| 32 | { |
| 33 | global $globals; |
| 34 | |
| 35 | if (!$login) { |
| 36 | throw new UserNotFoundException(); |
| 37 | } |
| 38 | |
| 39 | if ($login instanceof User) { |
| 40 | $machin->id(); |
| 41 | } |
| 42 | |
| 43 | if ($login instanceof Profile) { |
| 44 | $this->_profile = $login; |
| 45 | $this->_profile_fetched = true; |
| 46 | $res = XDB::query('SELECT ap.uid |
| 47 | FROM account_profiles AS ap |
| 48 | WHERE ap.pid = {?} AND FIND_IN_SET(\'owner\', perms)', |
| 49 | $login->id()); |
| 50 | if ($res->numRows()) { |
| 51 | return $res->fetchOneCell(); |
| 52 | } |
| 53 | throw new UserNotFoundException(); |
| 54 | } |
| 55 | |
| 56 | // If $data is an integer, fetches directly the result. |
| 57 | if (is_numeric($login)) { |
| 58 | $res = XDB::query('SELECT a.uid |
| 59 | FROM accounts AS a |
| 60 | WHERE a.uid = {?}', $login); |
| 61 | if ($res->numRows()) { |
| 62 | return $res->fetchOneCell(); |
| 63 | } |
| 64 | |
| 65 | throw new UserNotFoundException(); |
| 66 | } |
| 67 | |
| 68 | // Checks whether $login is a valid hruid or not. |
| 69 | $res = XDB::query('SELECT a.uid |
| 70 | FROM accounts AS a |
| 71 | WHERE a.hruid = {?}', $login); |
| 72 | if ($res->numRows()) { |
| 73 | return $res->fetchOneCell(); |
| 74 | } |
| 75 | |
| 76 | // From now, $login can only by an email alias, or an email redirection. |
| 77 | // If it doesn't look like a valid address, appends the plat/al's main domain. |
| 78 | $login = trim(strtolower($login)); |
| 79 | if (strstr($login, '@') === false) { |
| 80 | $login = $login . '@' . $globals->mail->domain; |
| 81 | } |
| 82 | |
| 83 | // Checks if $login is a valid alias on the main domains. |
| 84 | list($mbox, $fqdn) = explode('@', $login); |
| 85 | if ($fqdn == $globals->mail->domain || $fqdn == $globals->mail->domain2) { |
| 86 | $res = XDB::query('SELECT a.uid |
| 87 | FROM accounts AS a |
| 88 | INNER JOIN aliases AS al ON (al.uid = a.uid AND al.type IN (\'alias\', \'a_vie\')) |
| 89 | WHERE al.alias = {?}', $mbox); |
| 90 | if ($res->numRows()) { |
| 91 | return $res->fetchOneCell(); |
| 92 | } |
| 93 | |
| 94 | if (preg_match('/^(.*)\.([0-9]{4})$/u', $mbox, $matches)) { |
| 95 | $res = XDB::query('SELECT a.uid |
| 96 | FROM accounts AS a |
| 97 | INNER JOIN aliases AS al ON (al.uid = a.uid AND al.type IN (\'alias\', \'a_vie\')) |
| 98 | INNER JOIN account_profiles AS ap ON (a.uid = ap.uid AND FIND_IN_SET(\'owner\', ap.perms)) |
| 99 | INNER JOIN profiles AS p ON (p.pid = ap.pid) |
| 100 | INNER JOIN profile_education AS pe ON (p.pid = pe.pid AND FIND_IN_SET(\'primary\', pe.flags)) |
| 101 | WHERE p.hrpid = {?} OR ((pe.entry_year <= {?} AND pe.grad_year >= {?}) AND al.alias = {?}) |
| 102 | GROUP BY a.uid', |
| 103 | $matches[0], $matches[2], $matches[2], $matches[1]); |
| 104 | if ($res->numRows() == 1) { |
| 105 | return $res->fetchOneCell(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | throw new UserNotFoundException(); |
| 110 | } |
| 111 | |
| 112 | // Looks for $login as an email alias from the dedicated alias domain. |
| 113 | if ($fqdn == $globals->mail->alias_dom || $fqdn == $globals->mail->alias_dom2) { |
| 114 | $res = XDB::query("SELECT redirect |
| 115 | FROM virtual_redirect |
| 116 | INNER JOIN virtual USING(vid) |
| 117 | WHERE alias = {?}", $mbox . '@' . $globals->mail->alias_dom); |
| 118 | if ($redir = $res->fetchOneCell()) { |
| 119 | // We now have a valid alias, which has to be translated to an hruid. |
| 120 | list($alias, $alias_fqdn) = explode('@', $redir); |
| 121 | $res = XDB::query("SELECT a.uid |
| 122 | FROM accounts AS a |
| 123 | LEFT JOIN aliases AS al ON (al.uid = a.uid AND al.type IN ('alias', 'a_vie')) |
| 124 | WHERE al.alias = {?}", $alias); |
| 125 | if ($res->numRows()) { |
| 126 | return $res->fetchOneCell(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | throw new UserNotFoundException(); |
| 131 | } |
| 132 | |
| 133 | // Looks for an account with the given email. |
| 134 | $res = XDB::query('SELECT a.uid |
| 135 | FROM accounts AS a |
| 136 | WHERE a.email = {?}', $login); |
| 137 | if ($res->numRows() == 1) { |
| 138 | return $res->fetchOneCell(); |
| 139 | } |
| 140 | |
| 141 | // Otherwise, we do suppose $login is an email redirection. |
| 142 | $res = XDB::query("SELECT a.uid |
| 143 | FROM accounts AS a |
| 144 | LEFT JOIN emails AS e ON (e.uid = a.uid) |
| 145 | WHERE e.email = {?}", $login); |
| 146 | if ($res->numRows() == 1) { |
| 147 | return $res->fetchOneCell(); |
| 148 | } |
| 149 | |
| 150 | throw new UserNotFoundException($res->fetchColumn(1)); |
| 151 | } |
| 152 | |
| 153 | protected static function loadMainFieldsFromUIDs(array $uids, $respect_order = true) |
| 154 | { |
| 155 | if (empty($uids)) { |
| 156 | return PlIteratorUtils::emptyIterator(); |
| 157 | } |
| 158 | |
| 159 | global $globals; |
| 160 | $joins = ''; |
| 161 | $fields = array(); |
| 162 | if ($globals->asso('id')) { |
| 163 | $joins .= XDB::format("LEFT JOIN group_members AS gpm ON (gpm.uid = a.uid AND gpm.asso_id = {?})\n", $globals->asso('id')); |
| 164 | $fields[] = 'gpm.perms AS group_perms'; |
| 165 | $fields[] = 'gpm.comm AS group_comm'; |
| 166 | } |
| 167 | if (count($fields) > 0) { |
| 168 | $fields = ', ' . implode(', ', $fields); |
| 169 | } else { |
| 170 | $fields = ''; |
| 171 | } |
| 172 | |
| 173 | if ($respect_order) { |
| 174 | $order = 'ORDER BY ' . XDB::formatCustomOrder('a.uid', $uids); |
| 175 | } else { |
| 176 | $order = ''; |
| 177 | } |
| 178 | |
| 179 | $uids = array_map(array('XDB', 'escape'), $uids); |
| 180 | |
| 181 | return XDB::iterator('SELECT a.uid, a.hruid, a.registration_date, ah.alias AS homonym, |
| 182 | IF (af.alias IS NULL, NULL, CONCAT(af.alias, \'@' . $globals->mail->domain . '\')) AS forlife, |
| 183 | IF (af.alias IS NULL, NULL, CONCAT(af.alias, \'@' . $globals->mail->domain2 . '\')) AS forlife_alternate, |
| 184 | IF (ab.alias IS NULL, NULL, CONCAT(ab.alias, \'@' . $globals->mail->domain . '\')) AS bestalias, |
| 185 | IF (ab.alias IS NULL, NULL, CONCAT(ab.alias, \'@' . $globals->mail->domain2 . '\')) AS bestalias_alternate, |
| 186 | a.email, a.full_name, a.directory_name, a.display_name, a.sex = \'female\' AS gender, |
| 187 | IF(a.state = \'active\', CONCAT(at.perms, \',\', IF(a.user_perms IS NULL, \'\', a.user_perms)), \'\') AS perms, |
| 188 | a.user_perms, a.email_format, a.is_admin, a.state, a.type, a.skin, |
| 189 | FIND_IN_SET(\'watch\', a.flags) AS watch, a.comment, |
| 190 | a.weak_password IS NOT NULL AS weak_access, g.g_account_name IS NOT NULL AS googleapps, |
| 191 | a.token IS NOT NULL AS token_access, a.token, a.last_version, |
| 192 | (e.email IS NULL AND NOT FIND_IN_SET(\'googleapps\', eo.storage)) AND a.state != \'pending\' AS lost, |
| 193 | UNIX_TIMESTAMP(s.start) AS lastlogin, s.host, UNIX_TIMESTAMP(fp.last_seen) AS banana_last |
| 194 | ' . $fields . ' |
| 195 | FROM accounts AS a |
| 196 | INNER JOIN account_types AS at ON (at.type = a.type) |
| 197 | LEFT JOIN aliases AS af ON (af.uid = a.uid AND af.type = \'a_vie\') |
| 198 | LEFT JOIN aliases AS ab ON (ab.uid = a.uid AND FIND_IN_SET(\'bestalias\', ab.flags)) |
| 199 | LEFT JOIN aliases AS ah ON (ah.uid = a.uid AND ah.type = \'homonyme\') |
| 200 | LEFT JOIN emails AS e ON (e.uid = a.uid AND e.flags = \'active\') |
| 201 | LEFT JOIN email_options AS eo ON (eo.uid = a.uid) |
| 202 | LEFT JOIN gapps_accounts AS g ON (a.uid = g.l_userid AND g.g_status = \'active\') |
| 203 | LEFT JOIN log_last_sessions AS ls ON (ls.uid = a.uid) |
| 204 | LEFT JOIN log_sessions AS s ON (s.id = ls.id) |
| 205 | LEFT JOIN forum_profiles AS fp ON (fp.uid = a.uid) |
| 206 | ' . $joins . ' |
| 207 | WHERE a.uid IN (' . implode(', ', $uids) . ') |
| 208 | GROUP BY a.uid |
| 209 | ' . $order); |
| 210 | } |
| 211 | |
| 212 | // Implementation of the data loader. |
| 213 | protected function loadMainFields() |
| 214 | { |
| 215 | if ($this->hruid !== null && $this->forlife !== null |
| 216 | && $this->bestalias !== null && $this->display_name !== null |
| 217 | && $this->full_name !== null && $this->perms !== null |
| 218 | && $this->gender !== null && $this->email_format !== null) { |
| 219 | return; |
| 220 | } |
| 221 | $this->fillFromArray(self::loadMainFieldsFromUIDs(array($this->uid))->next()); |
| 222 | } |
| 223 | |
| 224 | // Specialization of the buildPerms method |
| 225 | // This function build 'generic' permissions for the user. It does not take |
| 226 | // into account page specific permissions (e.g X.net group permissions) |
| 227 | protected function buildPerms() |
| 228 | { |
| 229 | if (!is_null($this->perm_flags)) { |
| 230 | return; |
| 231 | } |
| 232 | if ($this->perms === null) { |
| 233 | $this->loadMainFields(); |
| 234 | } |
| 235 | $this->perm_flags = self::makePerms($this->perms, $this->is_admin); |
| 236 | } |
| 237 | |
| 238 | // We do not want to store the password in the object. |
| 239 | // So, fetch it 'on demand' |
| 240 | public function password() |
| 241 | { |
| 242 | return XDB::fetchOneCell('SELECT a.password |
| 243 | FROM accounts AS a |
| 244 | WHERE a.uid = {?}', $this->id()); |
| 245 | } |
| 246 | |
| 247 | public function isActive() |
| 248 | { |
| 249 | return $this->state == 'active'; |
| 250 | } |
| 251 | |
| 252 | /** Overload PlUser::promo(): there no promo defined for a user in the current |
| 253 | * schema. The promo is a field from the profile. |
| 254 | */ |
| 255 | public function promo() |
| 256 | { |
| 257 | if (!$this->hasProfile()) { |
| 258 | return ''; |
| 259 | } |
| 260 | return $this->profile()->promo(); |
| 261 | } |
| 262 | |
| 263 | public function firstName() |
| 264 | { |
| 265 | if (!$this->hasProfile()) { |
| 266 | return $this->displayName(); |
| 267 | } |
| 268 | return $this->profile()->firstName(); |
| 269 | } |
| 270 | |
| 271 | public function lastName() |
| 272 | { |
| 273 | if (!$this->hasProfile()) { |
| 274 | return ''; |
| 275 | } |
| 276 | return $this->profile()->lastName(); |
| 277 | } |
| 278 | |
| 279 | public function displayName() |
| 280 | { |
| 281 | if (!$this->hasProfile()) { |
| 282 | return $this->display_name; |
| 283 | } |
| 284 | return $this->profile()->yourself; |
| 285 | } |
| 286 | |
| 287 | public function fullName($with_promo = false) |
| 288 | { |
| 289 | if (!$this->hasProfile()) { |
| 290 | return $this->full_name; |
| 291 | } |
| 292 | return $this->profile()->fullName($with_promo); |
| 293 | } |
| 294 | |
| 295 | public function directoryName() |
| 296 | { |
| 297 | if (!$this->hasProfile()) { |
| 298 | return $this->directory_name; |
| 299 | } |
| 300 | return $this->profile()->directory_name; |
| 301 | } |
| 302 | |
| 303 | /** Return the main profile attached with this account if any. |
| 304 | */ |
| 305 | public function profile($forceFetch = false) |
| 306 | { |
| 307 | if (!$this->_profile_fetched || $forceFetch) { |
| 308 | $this->_profile_fetched = true; |
| 309 | $this->_profile = Profile::get($this); |
| 310 | } |
| 311 | return $this->_profile; |
| 312 | } |
| 313 | |
| 314 | /** Return true if the user has an associated profile. |
| 315 | */ |
| 316 | public function hasProfile() |
| 317 | { |
| 318 | return !is_null($this->profile()); |
| 319 | } |
| 320 | |
| 321 | /** Check if the user can edit to given profile. |
| 322 | */ |
| 323 | public function canEdit(Profile $profile) |
| 324 | { |
| 325 | // XXX: Check permissions (e.g. secretary permission) |
| 326 | // and flags from the profile |
| 327 | return XDB::fetchOneCell('SELECT pid |
| 328 | FROM account_profiles |
| 329 | WHERE uid = {?} AND pid = {?}', |
| 330 | $this->id(), $profile->id()); |
| 331 | } |
| 332 | |
| 333 | /** Get the email alias of the user. |
| 334 | */ |
| 335 | public function emailAlias() |
| 336 | { |
| 337 | global $globals; |
| 338 | $data = $this->emailAliases($globals->mail->alias_dom); |
| 339 | if (count($data) > 0) { |
| 340 | return array_pop($data); |
| 341 | } |
| 342 | return null; |
| 343 | } |
| 344 | |
| 345 | /** Get all the aliases the user belongs to. |
| 346 | */ |
| 347 | public function emailAliases($domain = null, $type = 'user', $sub_state = false) |
| 348 | { |
| 349 | $join = XDB::format('(vr.redirect = {?} OR vr.redirect = {?}) ', |
| 350 | $this->forlifeEmail(), $this->m4xForlifeEmail()); |
| 351 | $where = ''; |
| 352 | if (!is_null($domain)) { |
| 353 | $where = XDB::format('WHERE v.alias LIKE CONCAT("%@", {?})', $domain); |
| 354 | } |
| 355 | if (!is_null($type)) { |
| 356 | if (empty($where)) { |
| 357 | $where = XDB::format('WHERE v.type = {?}', $type); |
| 358 | } else { |
| 359 | $where .= XDB::format(' AND v.type = {?}', $type); |
| 360 | } |
| 361 | } |
| 362 | if ($sub_state) { |
| 363 | return XDB::fetchAllAssoc('alias', 'SELECT v.alias, vr.redirect IS NOT NULL AS sub |
| 364 | FROM virtual AS v |
| 365 | LEFT JOIN virtual_redirect AS vr ON (v.vid = vr.vid AND ' . $join . ') |
| 366 | ' . $where); |
| 367 | } else { |
| 368 | return XDB::fetchColumn('SELECT v.alias |
| 369 | FROM virtual AS v |
| 370 | INNER JOIN virtual_redirect AS vr ON (v.vid = vr.vid AND ' . $join . ') |
| 371 | ' . $where); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | /** Get the alternative forlife email |
| 376 | * TODO: remove this uber-ugly hack. The issue is that you need to remove |
| 377 | * all @m4x.org addresses in virtual_redirect first. |
| 378 | * XXX: This is juste to make code more readable, to be remove as soon as possible |
| 379 | */ |
| 380 | public function m4xForlifeEmail() |
| 381 | { |
| 382 | global $globals; |
| 383 | trigger_error('USING M4X FORLIFE', E_USER_NOTICE); |
| 384 | return $this->login() . '@' . $globals->mail->domain2; |
| 385 | } |
| 386 | |
| 387 | |
| 388 | /** Get marketing informations |
| 389 | */ |
| 390 | private function fetchMarketingData() |
| 391 | { |
| 392 | if (isset($this->pending_registration_date)) { |
| 393 | return; |
| 394 | } |
| 395 | $infos = XDB::fetchOneAssoc('SELECT rp.date AS pending_registration_date, rp.email AS pending_registration_email, |
| 396 | rm.last AS last_marketing_date, rm.email AS last_marketing_email |
| 397 | FROM accounts AS a |
| 398 | LEFT JOIN register_pending AS rp ON (rp.uid = a.uid) |
| 399 | LEFT JOIN register_marketing AS rm ON (rm.uid = a.uid AND rm.last != \'0000-00-00\') |
| 400 | WHERE a.uid = {?} |
| 401 | ORDER BY rm.last DESC', $this->id()); |
| 402 | if (is_null($infos)) { |
| 403 | $infos = array( |
| 404 | 'pending_registration_date' => null, |
| 405 | 'pending_registration_email' => null, |
| 406 | 'last_marketing_date' => null, |
| 407 | 'last_marketing_email' => null |
| 408 | ); |
| 409 | } |
| 410 | $this->fillFromArray($infos); |
| 411 | } |
| 412 | |
| 413 | public function pendingRegistrationDate() |
| 414 | { |
| 415 | $this->fetchMarketingData(); |
| 416 | return $this->pending_registration_date; |
| 417 | } |
| 418 | |
| 419 | public function pendingRegistrationEmail() |
| 420 | { |
| 421 | $this->fetchMarketingData(); |
| 422 | return $this->pending_registration_email; |
| 423 | } |
| 424 | |
| 425 | public function lastMarketingDate() |
| 426 | { |
| 427 | $this->fetchMarketingData(); |
| 428 | return $this->last_marketing_date; |
| 429 | } |
| 430 | |
| 431 | public function lastMarketingEmail() |
| 432 | { |
| 433 | $this->fetchMarketingData(); |
| 434 | return $this->last_marketing_email; |
| 435 | } |
| 436 | |
| 437 | public function lastKnownEmail() |
| 438 | { |
| 439 | $this->fetchMarketingData(); |
| 440 | if ($this->pending_registration_email > $this->last_marketing_date) { |
| 441 | return $this->pending_registration_email; |
| 442 | } |
| 443 | return $this->last_marketing_email; |
| 444 | } |
| 445 | |
| 446 | |
| 447 | /** Format of the emails sent by the site |
| 448 | */ |
| 449 | public function setEmailFormat($format) |
| 450 | { |
| 451 | Platal::assert($format == self::FORMAT_HTML || $format == self::FORMAT_TEXT, |
| 452 | "Invalid email format \"$format\""); |
| 453 | XDB::execute("UPDATE accounts |
| 454 | SET email_format = {?} |
| 455 | WHERE uid = {?}", |
| 456 | $format, $this->uid); |
| 457 | $this->email_format = $format; |
| 458 | } |
| 459 | |
| 460 | /** Get watch informations |
| 461 | */ |
| 462 | private function fetchWatchData() |
| 463 | { |
| 464 | if (isset($this->watch_actions)) { |
| 465 | return; |
| 466 | } |
| 467 | $watch = XDB::fetchOneAssoc('SELECT flags AS watch_flags, actions AS watch_actions, |
| 468 | UNIX_TIMESTAMP(last) AS watch_last |
| 469 | FROM watch |
| 470 | WHERE uid = {?}', $this->id()); |
| 471 | $watch['watch_flags'] = new PlFlagSet($watch['watch_flags']); |
| 472 | $watch['watch_actions'] = new PlFlagSet($watch['watch_actions']); |
| 473 | $watch['watch_promos'] = XDB::fetchColumn('SELECT promo |
| 474 | FROM watch_promo |
| 475 | WHERE uid = {?}', $this->id()); |
| 476 | $watch['watch_users'] = XDB::fetchColumn('SELECT ni_id |
| 477 | FROM watch_nonins |
| 478 | WHERE uid = {?}', $this->id()); |
| 479 | $this->fillFromArray($watch); |
| 480 | } |
| 481 | |
| 482 | public function watchType($type) |
| 483 | { |
| 484 | $this->fetchWatchData(); |
| 485 | return $this->watch_actions->hasFlag($type); |
| 486 | } |
| 487 | |
| 488 | public function watchContacts() |
| 489 | { |
| 490 | $this->fetchWatchData(); |
| 491 | return $this->watch_flags->hasFlag('contacts'); |
| 492 | } |
| 493 | |
| 494 | public function watchEmail() |
| 495 | { |
| 496 | $this->fetchWatchData(); |
| 497 | return $this->watch_flags->hasFlag('mail'); |
| 498 | } |
| 499 | |
| 500 | public function watchPromos() |
| 501 | { |
| 502 | $this->fetchWatchData(); |
| 503 | return $this->watch_promos; |
| 504 | } |
| 505 | |
| 506 | public function watchUsers() |
| 507 | { |
| 508 | $this->fetchWatchData(); |
| 509 | return $this->watch_users; |
| 510 | } |
| 511 | |
| 512 | public function watchLast() |
| 513 | { |
| 514 | $this->fetchWatchData(); |
| 515 | return $this->watch_last; |
| 516 | } |
| 517 | |
| 518 | public function invalidWatchCache() |
| 519 | { |
| 520 | unset($this->watch_actions); |
| 521 | unset($this->watch_users); |
| 522 | unset($this->watch_last); |
| 523 | unset($this->watch_promos); |
| 524 | } |
| 525 | |
| 526 | |
| 527 | // Contacts |
| 528 | private $contacts = null; |
| 529 | private function fetchContacts() |
| 530 | { |
| 531 | if (is_null($this->contacts)) { |
| 532 | $this->contacts = XDB::fetchAllAssoc('contact', 'SELECT * |
| 533 | FROM contacts |
| 534 | WHERE uid = {?}', |
| 535 | $this->id()); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | public function iterContacts() |
| 540 | { |
| 541 | $this->fetchContacts(); |
| 542 | return Profile::iterOverPIDs(array_keys($this->contacts)); |
| 543 | } |
| 544 | |
| 545 | public function getContacts() |
| 546 | { |
| 547 | $this->fetchContacts(); |
| 548 | return Profile::getBulkProfilesWithPIDs(array_keys($this->contacts)); |
| 549 | } |
| 550 | |
| 551 | public function isContact(Profile &$profile) |
| 552 | { |
| 553 | $this->fetchContacts(); |
| 554 | return isset($this->contacts[$profile->id()]); |
| 555 | } |
| 556 | |
| 557 | public function isWatchedUser(Profile &$profile) |
| 558 | { |
| 559 | return in_array($profile->id(), $this->watchUsers()); |
| 560 | } |
| 561 | |
| 562 | // Groupes X |
| 563 | private $groups = null; |
| 564 | public function groups() |
| 565 | { |
| 566 | if (is_null($this->groups)) { |
| 567 | $this->groups = XDB::fetchAllAssoc('asso_id', 'SELECT asso_id, perms, comm |
| 568 | FROM group_members |
| 569 | WHERE uid = {?}', |
| 570 | $this->id()); |
| 571 | } |
| 572 | return $this->groups; |
| 573 | } |
| 574 | |
| 575 | public function groupNames($institutions = false) |
| 576 | { |
| 577 | if ($institutions) { |
| 578 | $where = ' AND (g.cat = \'GroupesX\' OR g.cat = \'Institutions\')'; |
| 579 | } else { |
| 580 | $where = ''; |
| 581 | } |
| 582 | return XDB::fetchAllAssoc('SELECT g.diminutif, g.nom, g.site |
| 583 | FROM group_members AS gm |
| 584 | LEFT JOIN groups AS g ON (g.id = gm.asso_id) |
| 585 | WHERE gm.uid = {?}' . $where, |
| 586 | $this->id()); |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * Clears a user. |
| 591 | * *always deletes in: account_lost_passwords, register_marketing, |
| 592 | * register_pending, register_subs, watch_nonins, watch, watch_promo |
| 593 | * *always keeps in: account_types, accounts, aliases, axletter_ins, carvas, |
| 594 | * group_members, homonyms, newsletter_ins, register_mstats, |
| 595 | * *deletes if $clearAll: account_auth_openid, announce_read, contacts, |
| 596 | * email_options, email_send_save, emails, forum_innd, forum_profiles, |
| 597 | * forum_subs, gapps_accounts, gapps_nicknames, group_announces_read, |
| 598 | * group_member_sub_requests, reminder, requests, requests_hidden, |
| 599 | * virtual, virtual_redirect, ML |
| 600 | * *modifies if $clearAll: accounts |
| 601 | * |
| 602 | * Use cases: |
| 603 | * *$clearAll == false: when a user dies, her family still needs to keep in |
| 604 | * touch with the community. |
| 605 | * *$clearAll == true: in every other case we want the account to be fully |
| 606 | * deleted so that it can not be used anymore. |
| 607 | */ |
| 608 | public function clear($clearAll = true) |
| 609 | { |
| 610 | $tables = array('account_lost_passwords', 'register_marketing', |
| 611 | 'register_pending', 'register_subs', 'watch_nonins', |
| 612 | 'watch', 'watch_promo'); |
| 613 | |
| 614 | foreach ($tables as $t) { |
| 615 | XDB::execute('DELETE FROM ' . $t . ' |
| 616 | WHERE uid = {?}', |
| 617 | $this->id()); |
| 618 | } |
| 619 | |
| 620 | if ($clearAll) { |
| 621 | $groupIds = XDB::iterator('SELECT asso_id |
| 622 | FROM group_members |
| 623 | WHERE uid = {?}', |
| 624 | $this->id()); |
| 625 | while ($groupId = $groupIds->next()) { |
| 626 | $group = Group::get($groupId); |
| 627 | if ($group->notif_unsub) { |
| 628 | $mailer = new PlMailer('xnetgrp/unsubscription-notif.mail.tpl'); |
| 629 | $admins = $group->iterAdmins(); |
| 630 | while ($admin = $admins->next()) { |
| 631 | $mailer->addTo($admin); |
| 632 | } |
| 633 | $mailer->assign('group', $group->shortname); |
| 634 | $mailer->assign('user', $this); |
| 635 | $mailer->assign('selfdone', false); |
| 636 | $mailer->send(); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | $tables = array('account_auth_openid', 'gannounce_read', 'contacts', |
| 641 | 'email_options', 'gemail_send_save', 'emails', |
| 642 | 'forum_innd', 'gforum_profiles', 'forum_subs', |
| 643 | 'gapps_accounts', 'ggapps_nicknames', 'group_announces_read', |
| 644 | 'group_members', 'ggroup_member_sub_requests', 'reminder', 'requests', |
| 645 | 'requests_hidden'); |
| 646 | |
| 647 | foreach ($tables as $t) { |
| 648 | XDB::execute('DELETE FROM ' . $t . ' |
| 649 | WHERE uid = {?}', |
| 650 | $this->id()); |
| 651 | } |
| 652 | |
| 653 | XDB::execute("UPDATE accounts |
| 654 | SET registration_date = 0, state = 'pending', password = NULL, |
| 655 | weak_password = NULL, token = NULL, is_admin = 0 |
| 656 | WHERE uid = {?}", |
| 657 | $this->id()); |
| 658 | |
| 659 | XDB::execute('DELETE v.* |
| 660 | FROM virtual AS v |
| 661 | INNER JOIN virtual_redirect AS r ON (v.vid = r.vid) |
| 662 | WHERE redirect = {?} OR redirect = {?}', |
| 663 | $this->forlifeEmail(), $this->m4xForlifeEmail()); |
| 664 | XDB::execute('DELETE FROM virtual_redirect |
| 665 | WHERE redirect = {?} OR redirect = {?}', |
| 666 | $this->forlifeEmail(), $this->m4xForlifeEmail()); |
| 667 | |
| 668 | if ($globals->mailstorage->googleapps_domain) { |
| 669 | require_once 'googleapps.inc.php'; |
| 670 | |
| 671 | if (GoogleAppsAccount::account_status($uid)) { |
| 672 | $account = new GoogleAppsAccount($user); |
| 673 | $account->suspend(); |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | $mmlist = new MMList($this); |
| 679 | $mmlist->kill($this->hruid, $clearAll); |
| 680 | } |
| 681 | |
| 682 | // Merge all infos in other user and then clean this one |
| 683 | public function mergeIn(User &$newuser) { |
| 684 | if ($this->profile()) { |
| 685 | // Don't disable user with profile in this way. |
| 686 | global $globals; |
| 687 | Platal::page()->trigError('Impossible de fusionner les comptes ' . $this->hruid . ' et ' . $newuser->hruid . |
| 688 | '. Contacte support@' . $globals->mail->domain . '.'); |
| 689 | return false; |
| 690 | } |
| 691 | |
| 692 | if ($this->forlifeEmail()) { |
| 693 | // If the new user is not registered and does not have already an email address, |
| 694 | // we need to give him the old user's email address if he has any. |
| 695 | if (!$newuser->perms) { |
| 696 | XDB::execute('UPDATE accounts |
| 697 | SET email = {?} |
| 698 | WHERE uid = {?} AND email IS NULL', |
| 699 | $this->forlifeEmail(), $newuser->id()); |
| 700 | } |
| 701 | $newemail = XDB::fetchOneCell('SELECT email |
| 702 | FROM accounts |
| 703 | WHERE uid = {?}', |
| 704 | $newuser->id()); |
| 705 | |
| 706 | // Change email used in aliases and mailing lists. |
| 707 | if ($this->forlifeEmail() != $newemail) { |
| 708 | // virtual_redirect (email aliases) |
| 709 | XDB::execute('DELETE v1 |
| 710 | FROM virtual_redirect AS v1, virtual_redirect AS v2 |
| 711 | WHERE v1.vid = v2.vid AND v1.redirect = {?} AND v2.redirect = {?}', |
| 712 | $this->forlifeEmail(), $newemail); |
| 713 | XDB::execute('UPDATE virtual_redirect |
| 714 | SET redirect = {?} |
| 715 | WHERE redirect = {?}', |
| 716 | $newemail, $this->forlifeEmail()); |
| 717 | |
| 718 | // group mailing lists |
| 719 | $group_domains = XDB::fetchColumn('SELECT g.mail_domain |
| 720 | FROM groups AS g |
| 721 | INNER JOIN group_members AS gm ON(g.id = gm.asso_id) |
| 722 | WHERE g.mail_domain != \'\' AND gm.uid = {?}', |
| 723 | $this->id()); |
| 724 | foreach ($group_domains as $mail_domain) { |
| 725 | $mmlist = new MMList($this, $mail_domain); |
| 726 | $mmlist->replace_email_in_all($this->forlifeEmail(), $newemail); |
| 727 | } |
| 728 | // main domain lists |
| 729 | $mmlist = new MMList($this); |
| 730 | $mmlist->replace_email_in_all($this->forlifeEmail(), $newemail); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | // Updates user in following tables. |
| 735 | foreach (array('group_announces', 'payment_transactions', 'log_sessions') as $table) { |
| 736 | XDB::execute('UPDATE ' . $table . ' |
| 737 | SET uid = {?} |
| 738 | WHERE uid = {?}', |
| 739 | $newuser->id(), $this->id()); |
| 740 | } |
| 741 | XDB::execute('UPDATE group_events |
| 742 | SET organisateur_uid = {?} |
| 743 | WHERE organisateur_uid = {?}', |
| 744 | $newuser->id(), $this->id()); |
| 745 | |
| 746 | // Merges user in following tables, ie updates when possible, then deletes remaining occurences of the old user. |
| 747 | foreach (array('group_announces_read', 'group_event_participants', 'group_member_sub_requests', 'group_members') as $table) { |
| 748 | XDB::execute('UPDATE IGNORE ' . $table . ' |
| 749 | SET uid = {?} |
| 750 | WHERE uid = {?}', |
| 751 | $newuser->id(), $this->id()); |
| 752 | XDB::execute('DELETE FROM ' . $table . ' |
| 753 | WHERE uid = {?}', |
| 754 | $this->id()); |
| 755 | } |
| 756 | |
| 757 | // Eventually updates last session id and deletes old user's accounts entry. |
| 758 | $lastSession = XDB::fetchOneCell('SELECT id |
| 759 | FROM log_sessions |
| 760 | WHERE uid = {?} |
| 761 | ORDER BY start DESC |
| 762 | LIMIT 1', |
| 763 | $newuser->id()); |
| 764 | XDB::execute('UPDATE log_last_sessions |
| 765 | SET id = {?} |
| 766 | WHERE uid = {?}', |
| 767 | $newuser->id()); |
| 768 | XDB::execute('DELETE FROM accounts |
| 769 | WHERE uid = {?}', |
| 770 | $this->id()); |
| 771 | |
| 772 | return true; |
| 773 | } |
| 774 | |
| 775 | // Return permission flags for a given permission level. |
| 776 | public static function makePerms($perms, $is_admin) |
| 777 | { |
| 778 | $flags = new PlFlagSet($perms); |
| 779 | $flags->addFlag(PERMS_USER); |
| 780 | if ($is_admin) { |
| 781 | $flags->addFlag(PERMS_ADMIN); |
| 782 | } |
| 783 | |
| 784 | // Access to private directory implies access to 'less'-private version. |
| 785 | if ($flags->hasFlag('directory_private')) { |
| 786 | $flags->addFlag('directory_ax'); |
| 787 | } |
| 788 | return $flags; |
| 789 | } |
| 790 | |
| 791 | // Implementation of the default user callback. |
| 792 | public static function _default_user_callback($login, $results) |
| 793 | { |
| 794 | $result_count = count($results); |
| 795 | if ($result_count == 0 || !S::admin()) { |
| 796 | Platal::page()->trigError("Il n'y a pas d'utilisateur avec l'identifiant : $login"); |
| 797 | } else { |
| 798 | Platal::page()->trigError("Il y a $result_count utilisateurs avec cet identifiant : " . join(', ', $results)); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | // Implementation of the static email locality checker. |
| 803 | public static function isForeignEmailAddress($email) |
| 804 | { |
| 805 | global $globals; |
| 806 | if (strpos($email, '@') === false) { |
| 807 | return false; |
| 808 | } |
| 809 | |
| 810 | list($user, $dom) = explode('@', $email); |
| 811 | return $dom != $globals->mail->domain && |
| 812 | $dom != $globals->mail->domain2 && |
| 813 | $dom != $globals->mail->alias_dom && |
| 814 | $dom != $globals->mail->alias_dom2; |
| 815 | } |
| 816 | |
| 817 | public static function isVirtualEmailAddress($email) |
| 818 | { |
| 819 | global $globals; |
| 820 | if (strpos($email, '@') === false) { |
| 821 | return false; |
| 822 | } |
| 823 | |
| 824 | list($user, $dom) = explode('@', $email); |
| 825 | return $dom == $globals->mail->alias_dom |
| 826 | || $dom == $globals->mail->alias_dom2; |
| 827 | } |
| 828 | |
| 829 | /* Tries to find pending accounts with an hruid close to $login. */ |
| 830 | public static function getPendingAccounts($login, $iterator = false) |
| 831 | { |
| 832 | global $globals; |
| 833 | |
| 834 | if (strpos($login, '@') === false) { |
| 835 | return null; |
| 836 | } |
| 837 | |
| 838 | list($login, $domain) = explode('@', $login); |
| 839 | |
| 840 | if ($domain && $domain != $globals->mail->domain && $domain != $globals->mail->domain2) { |
| 841 | return null; |
| 842 | } |
| 843 | |
| 844 | $sql = "SELECT uid, full_name |
| 845 | FROM accounts |
| 846 | WHERE state = 'pending' AND REPLACE(hruid, '-', '') LIKE |
| 847 | CONCAT('%', REPLACE(REPLACE(REPLACE({?}, ' ', ''), '-', ''), '\'', ''), '%') |
| 848 | ORDER BY full_name"; |
| 849 | if ($iterator) { |
| 850 | return XDB::iterator($sql, $login); |
| 851 | } else { |
| 852 | $res = XDB::query($sql, $login); |
| 853 | return $res->fetchAllAssoc(); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | |
| 858 | public static function iterOverUIDs($uids, $respect_order = true) |
| 859 | { |
| 860 | return new UserIterator(self::loadMainFieldsFromUIDs($uids, $respect_order)); |
| 861 | } |
| 862 | |
| 863 | /** Fetch a set of users from a list of UIDs |
| 864 | * @param $data The list of uids to fetch, or an array of arrays |
| 865 | * @param $orig If $data is an array of arrays, the subfield where uids are stored |
| 866 | * @param $dest If $data is an array of arrays, the subfield to fill with Users |
| 867 | * @param $fetchProfile Whether to fetch Profiles as well |
| 868 | * @return either an array of $uid => User, or $data with $data[$i][$dest] = User |
| 869 | */ |
| 870 | public static function getBulkUsersWithUIDs(array $data, $orig = null, $dest = null, $fetchProfile = true) |
| 871 | { |
| 872 | // Fetch the list of uids |
| 873 | if (is_null($orig)) { |
| 874 | $uids = $data; |
| 875 | } else { |
| 876 | if (is_null($dest)) { |
| 877 | $dest = $orig; |
| 878 | } |
| 879 | $uids = array(); |
| 880 | foreach ($data as $key=>$entry) { |
| 881 | if (isset($entry[$orig])) { |
| 882 | $uids[] = $entry[$orig]; |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | // Fetch users |
| 888 | if (count($uids) == 0) { |
| 889 | return $data; |
| 890 | } |
| 891 | $users = self::iterOverUIDs($uids, true); |
| 892 | |
| 893 | $table = array(); |
| 894 | if ($fetchProfile) { |
| 895 | $profiles = Profile::iterOverUIDS($uids, true); |
| 896 | if ($profiles != null) { |
| 897 | $profile = $profiles->next(); |
| 898 | } else { |
| 899 | $profile = null; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | /** We iterate through the users, moving in |
| 904 | * profiles when they match the user ID : |
| 905 | * there can be users without a profile, but not |
| 906 | * the other way around. |
| 907 | */ |
| 908 | while (($user = $users->next())) { |
| 909 | if ($fetchProfile) { |
| 910 | if ($profile != null && $profile->owner_id == $user->id()) { |
| 911 | $user->_profile = $profile; |
| 912 | $profile = $profiles->next(); |
| 913 | } |
| 914 | $user->_profile_fetched = true; |
| 915 | } |
| 916 | $table[$user->id()] = $user; |
| 917 | } |
| 918 | |
| 919 | // Build the result with respect to input order. |
| 920 | if (is_null($orig)) { |
| 921 | return $table; |
| 922 | } else { |
| 923 | foreach ($data as $key=>$entry) { |
| 924 | if (isset($entry[$orig])) { |
| 925 | $entry[$dest] = $table[$entry[$orig]]; |
| 926 | $data[$key] = $entry; |
| 927 | } |
| 928 | } |
| 929 | return $data; |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | public static function getBulkUsersFromDB($fetchProfile = true) |
| 934 | { |
| 935 | $args = func_get_args(); |
| 936 | $uids = call_user_func_array(array('XDB', 'fetchColumn'), $args); |
| 937 | return self::getBulkUsersWithUIDs($uids, null, null, $fetchProfile); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | /** Iterator over a set of Users |
| 942 | * @param an XDB::Iterator obtained from a User::loadMainFieldsFromUIDs |
| 943 | */ |
| 944 | class UserIterator implements PlIterator |
| 945 | { |
| 946 | private $dbiter; |
| 947 | |
| 948 | public function __construct($dbiter) |
| 949 | { |
| 950 | $this->dbiter = $dbiter; |
| 951 | } |
| 952 | |
| 953 | public function next() |
| 954 | { |
| 955 | $data = $this->dbiter->next(); |
| 956 | if ($data == null) { |
| 957 | return null; |
| 958 | } else { |
| 959 | return User::getSilentWithValues(null, $data); |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | public function total() |
| 964 | { |
| 965 | return $this->dbiter->total(); |
| 966 | } |
| 967 | |
| 968 | public function first() |
| 969 | { |
| 970 | return $this->dbiter->first(); |
| 971 | } |
| 972 | |
| 973 | public function last() |
| 974 | { |
| 975 | return $this->dbiter->last(); |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 980 | ?> |