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