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