| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2009 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 | |
| 23 | /****************** |
| 24 | * CONDITIONS |
| 25 | ******************/ |
| 26 | |
| 27 | interface UserFilterCondition |
| 28 | { |
| 29 | const COND_TRUE = 'TRUE'; |
| 30 | const COND_FALSE = 'FALSE'; |
| 31 | |
| 32 | /** Check that the given user matches the rule. |
| 33 | */ |
| 34 | public function buildCondition(UserFilter &$uf); |
| 35 | } |
| 36 | |
| 37 | abstract class UFC_OneChild implements UserFilterCondition |
| 38 | { |
| 39 | protected $child; |
| 40 | |
| 41 | public function __construct($child = null) |
| 42 | { |
| 43 | if (!is_null($child) && ($child instanceof UserFilterCondition)) { |
| 44 | $this->setChild($child); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | public function setChild(UserFilterCondition &$cond) |
| 49 | { |
| 50 | $this->child =& $cond; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | abstract class UFC_NChildren implements UserFilterCondition |
| 55 | { |
| 56 | protected $children = array(); |
| 57 | |
| 58 | public function __construct() |
| 59 | { |
| 60 | $children = func_get_args(); |
| 61 | foreach ($children as &$child) { |
| 62 | if (!is_null($child) && ($child instanceof UserFilterCondition)) { |
| 63 | $this->addChild($child); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public function addChild(UserFilterCondition &$cond) |
| 69 | { |
| 70 | $this->children[] =& $cond; |
| 71 | } |
| 72 | |
| 73 | protected function catConds(array $cond, $op, $fallback) |
| 74 | { |
| 75 | if (count($cond) == 0) { |
| 76 | return $fallback; |
| 77 | } else if (count($cond) == 1) { |
| 78 | return $cond[0]; |
| 79 | } else { |
| 80 | return '(' . implode(') ' . $op . ' (', $cond) . ')'; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | class UFC_True implements UserFilterCondition |
| 86 | { |
| 87 | public function buildCondition(UserFilter &$uf) |
| 88 | { |
| 89 | return self::COND_TRUE; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | class UFC_False implements UserFilterCondition |
| 94 | { |
| 95 | public function buildCondition(UserFilter &$uf) |
| 96 | { |
| 97 | return self::COND_FALSE; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | class UFC_Not extends UFC_OneChild |
| 102 | { |
| 103 | public function buildCondition(UserFilter &$uf) |
| 104 | { |
| 105 | $val = $this->child->buildCondition($uf); |
| 106 | if ($val == self::COND_TRUE) { |
| 107 | return self::COND_FALSE; |
| 108 | } else if ($val == self::COND_FALSE) { |
| 109 | return self::COND_TRUE; |
| 110 | } else { |
| 111 | return 'NOT (' . $val . ')'; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | class UFC_And extends UFC_NChildren |
| 117 | { |
| 118 | public function buildCondition(UserFilter &$uf) |
| 119 | { |
| 120 | if (empty($this->children)) { |
| 121 | return self::COND_FALSE; |
| 122 | } else { |
| 123 | $true = self::COND_FALSE; |
| 124 | $conds = array(); |
| 125 | foreach ($this->children as &$child) { |
| 126 | $val = $child->buildCondition($uf); |
| 127 | if ($val == self::COND_TRUE) { |
| 128 | $true = self::COND_TRUE; |
| 129 | } else if ($val == self::COND_FALSE) { |
| 130 | return self::COND_FALSE; |
| 131 | } else { |
| 132 | $conds[] = $val; |
| 133 | } |
| 134 | } |
| 135 | return $this->catConds($conds, 'AND', $true); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | class UFC_Or extends UFC_NChildren |
| 141 | { |
| 142 | public function buildCondition(UserFilter &$uf) |
| 143 | { |
| 144 | if (empty($this->children)) { |
| 145 | return self::COND_TRUE; |
| 146 | } else { |
| 147 | $true = self::COND_TRUE; |
| 148 | $conds = array(); |
| 149 | foreach ($this->children as &$child) { |
| 150 | $val = $child->buildCondition($uf); |
| 151 | if ($val == self::COND_TRUE) { |
| 152 | return self::COND_TRUE; |
| 153 | } else if ($val == self::COND_FALSE) { |
| 154 | $true = self::COND_FALSE; |
| 155 | } else { |
| 156 | $conds[] = $val; |
| 157 | } |
| 158 | } |
| 159 | return $this->catConds($conds, 'OR', $true); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | class UFC_Promo implements UserFilterCondition |
| 165 | { |
| 166 | |
| 167 | private $grade; |
| 168 | private $promo; |
| 169 | private $comparison; |
| 170 | |
| 171 | public function __construct($comparison, $grade, $promo) |
| 172 | { |
| 173 | $this->grade = $grade; |
| 174 | $this->comparison = $comparison; |
| 175 | $this->promo = $promo; |
| 176 | if ($this->grade != UserFilter::DISPLAY) { |
| 177 | UserFilter::assertGrade($this->grade); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | public function buildCondition(UserFilter &$uf) |
| 182 | { |
| 183 | if ($this->grade == UserFilter::DISPLAY) { |
| 184 | $sub = $uf->addDisplayFilter(); |
| 185 | return XDB::format('pd' . $sub . '.promo = {?}', $this->promo); |
| 186 | } else { |
| 187 | $sub = $uf->addEducationFilter(true, $this->grade); |
| 188 | $field = 'pe' . $sub . '.' . UserFilter::promoYear($this->grade); |
| 189 | return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison . ' ' . XDB::format('{?}', $this->promo); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | class UFC_Name implements UserFilterCondition |
| 195 | { |
| 196 | const PREFIX = 1; |
| 197 | const SUFFIX = 2; |
| 198 | const PARTICLE = 7; |
| 199 | const VARIANTS = 8; |
| 200 | const CONTAINS = 3; |
| 201 | |
| 202 | private $type; |
| 203 | private $text; |
| 204 | private $mode; |
| 205 | |
| 206 | public function __construct($type, $text, $mode) |
| 207 | { |
| 208 | $this->type = $type; |
| 209 | $this->text = $text; |
| 210 | $this->mode = $mode; |
| 211 | } |
| 212 | |
| 213 | private function buildNameQuery($type, $variant, $where, UserFilter &$uf) |
| 214 | { |
| 215 | $sub = $uf->addNameFilter($type, $variant); |
| 216 | return str_replace('$ME', 'pn' . $sub, $where); |
| 217 | } |
| 218 | |
| 219 | public function buildCondition(UserFilter &$uf) |
| 220 | { |
| 221 | $left = '$ME.name'; |
| 222 | $op = ' LIKE '; |
| 223 | if (($this->mode & self::PARTICLE) == self::PARTICLE) { |
| 224 | $left = 'CONCAT($ME.particle, \' \', $ME.name)'; |
| 225 | } |
| 226 | if (($this->mode & self::CONTAINS) == 0) { |
| 227 | $right = XDB::format('{?}', $this->text); |
| 228 | $op = ' = '; |
| 229 | } else if (($this->mode & self::CONTAINS) == self::PREFIX) { |
| 230 | $right = XDB::format('CONCAT({?}, \'%\')', $this->text); |
| 231 | } else if (($this->mode & self::CONTAINS) == self::SUFFIX) { |
| 232 | $right = XDB::format('CONCAT(\'%\', {?})', $this->text); |
| 233 | } else { |
| 234 | $right = XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->text); |
| 235 | } |
| 236 | $cond = $left . $op . $right; |
| 237 | $conds = array($this->buildNameQuery($this->type, null, $cond, $uf)); |
| 238 | if (($this->mode & self::VARIANTS) != 0 && isset(UserFilter::$name_variants[$this->type])) { |
| 239 | foreach (UserFilter::$name_variants[$this->type] as $var) { |
| 240 | $conds[] = $this->buildNameQuery($this->type, $var, $cond, $uf); |
| 241 | } |
| 242 | } |
| 243 | return implode(' OR ', $conds); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | class UFC_Dead implements UserFilterCondition |
| 248 | { |
| 249 | private $comparison; |
| 250 | private $date; |
| 251 | |
| 252 | public function __construct($comparison = null, $date = null) |
| 253 | { |
| 254 | $this->comparison = $comparison; |
| 255 | $this->date = $date; |
| 256 | } |
| 257 | |
| 258 | public function buildCondition(UserFilter &$uf) |
| 259 | { |
| 260 | $str = 'p.deathdate IS NOT NULL'; |
| 261 | if (!is_null($this->comparison)) { |
| 262 | $str .= ' AND p.deathdate ' . $this->comparison . ' ' . XDB::format('{?}', date('Y-m-d', $this->date)); |
| 263 | } |
| 264 | return $str; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | class UFC_Registered implements UserFilterCondition |
| 269 | { |
| 270 | private $active; |
| 271 | private $comparison; |
| 272 | private $date; |
| 273 | |
| 274 | public function __construct($active = false, $comparison = null, $date = null) |
| 275 | { |
| 276 | $this->only_active = $active; |
| 277 | $this->comparison = $comparison; |
| 278 | $this->date = $date; |
| 279 | } |
| 280 | |
| 281 | public function buildCondition(UserFilter &$uf) |
| 282 | { |
| 283 | if ($this->active) { |
| 284 | $date = 'a.uid IS NOT NULL AND a.state = \'active\''; |
| 285 | } else { |
| 286 | $date = 'a.uid IS NOT NULL AND a.state != \'pending\''; |
| 287 | } |
| 288 | if (!is_null($this->comparison)) { |
| 289 | $date .= ' AND a.registration_date ' . $this->comparison . ' ' . XDB::format('{?}', date('Y-m-d', $this->date)); |
| 290 | } |
| 291 | return $date; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | class UFC_Sex implements UserFilterCondition |
| 296 | { |
| 297 | private $sex; |
| 298 | public function __construct($sex) |
| 299 | { |
| 300 | $this->sex = $sex; |
| 301 | } |
| 302 | |
| 303 | public function buildCondition(UserFilter &$uf) |
| 304 | { |
| 305 | if ($this->sex != User::GENDER_MALE && $this->sex != User::GENDER_FEMALE) { |
| 306 | return self::COND_FALSE; |
| 307 | } else { |
| 308 | return XDB::format('p.sex = {?}', $this->sex == User::GENDER_FEMALE ? 'female' : 'male'); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | class UFC_Group implements UserFilterCondition |
| 314 | { |
| 315 | private $group; |
| 316 | private $admin; |
| 317 | public function __construct($group, $admin = false) |
| 318 | { |
| 319 | $this->group = $group; |
| 320 | $this->admin = $admin; |
| 321 | } |
| 322 | |
| 323 | public function buildCondition(UserFilter &$uf) |
| 324 | { |
| 325 | $sub = $uf->addGroupFilter($this->group); |
| 326 | $where = 'gpm' . $sub . '.perms IS NOT NULL'; |
| 327 | if ($this->admin) { |
| 328 | $where .= ' AND gpm' . $sub . '.perms = \'admin\''; |
| 329 | } |
| 330 | return $where; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | class UFC_Email implements UserFilterCondition |
| 335 | { |
| 336 | private $email; |
| 337 | public function __construct($email) |
| 338 | { |
| 339 | $this->email = $email; |
| 340 | } |
| 341 | |
| 342 | public function buildCondition(UserFilter &$uf) |
| 343 | { |
| 344 | if (User::isForeignEmailAddress($this->email)) { |
| 345 | $sub = $uf->addEmailRedirectFilter($this->email); |
| 346 | return XDB::format('e' . $sub . '.email IS NOT NULL OR a.email = {?}', $this->email); |
| 347 | } else if (User::isVirtualEmailAddress($this->email)) { |
| 348 | $sub = $uf->addVirtualEmailFilter($this->email); |
| 349 | return 'vr' . $sub . '.redirect IS NOT NULL'; |
| 350 | } else { |
| 351 | @list($user, $domain) = explode('@', $this->email); |
| 352 | $sub = $uf->addAliasFilter($user); |
| 353 | return 'al' . $sub . '.alias IS NOT NULL'; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | class UFC_EmailList implements UserFilterCondition |
| 359 | { |
| 360 | private $emails; |
| 361 | public function __construct($emails) |
| 362 | { |
| 363 | $this->emails = $emails; |
| 364 | } |
| 365 | |
| 366 | public function buildCondition(UserFilter &$uf) |
| 367 | { |
| 368 | $email = null; |
| 369 | $virtual = null; |
| 370 | $alias = null; |
| 371 | $cond = array(); |
| 372 | |
| 373 | if (count($this->emails) == 0) { |
| 374 | return UserFilterCondition::COND_TRUE; |
| 375 | } |
| 376 | |
| 377 | foreach ($this->emails as $entry) { |
| 378 | if (User::isForeignEmailAddress($entry)) { |
| 379 | if (is_null($email)) { |
| 380 | $email = $uf->addEmailRedirectFilter(); |
| 381 | } |
| 382 | $cond[] = XDB::format('e' . $email . '.email = {?} OR a.email = {?}', $entry, $entry); |
| 383 | } else if (User::isVirtualEmailAddress($entry)) { |
| 384 | if (is_null($virtual)) { |
| 385 | $virtual = $uf->addVirtualEmailFilter(); |
| 386 | } |
| 387 | $cond[] = XDB::format('vr' . $virtual . '.redirect IS NOT NULL AND v' . $virtual . '.alias = {?}', $entry); |
| 388 | } else { |
| 389 | if (is_null($alias)) { |
| 390 | $alias = $uf->addAliasFilter(); |
| 391 | } |
| 392 | @list($user, $domain) = explode('@', $entry); |
| 393 | $cond[] = XDB::format('al' . $alias . '.alias = {?}', $user); |
| 394 | } |
| 395 | } |
| 396 | return '(' . implode(') OR (', $cond) . ')'; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /****************** |
| 401 | * ORDERS |
| 402 | ******************/ |
| 403 | |
| 404 | abstract class UserFilterOrder |
| 405 | { |
| 406 | protected $desc = false; |
| 407 | |
| 408 | public function buildSort(UserFilter &$uf) |
| 409 | { |
| 410 | $sel = $this->getSortTokens($uf); |
| 411 | if (!is_array($sel)) { |
| 412 | $sel = array($sel); |
| 413 | } |
| 414 | if ($this->desc) { |
| 415 | foreach ($sel as $k=>$s) { |
| 416 | $sel[$k] = $s . ' DESC'; |
| 417 | } |
| 418 | } |
| 419 | return $sel; |
| 420 | } |
| 421 | |
| 422 | abstract protected function getSortTokens(UserFilter &$uf); |
| 423 | } |
| 424 | |
| 425 | class UFO_Promo extends UserFilterOrder |
| 426 | { |
| 427 | private $grade; |
| 428 | |
| 429 | public function __construct($grade = null, $desc = false) |
| 430 | { |
| 431 | $this->grade = $grade; |
| 432 | $this->desc = $desc; |
| 433 | } |
| 434 | |
| 435 | protected function getSortTokens(UserFilter &$uf) |
| 436 | { |
| 437 | if (UserFilter::isGrade($this->grade)) { |
| 438 | $sub = $uf->addEducationFilter($this->grade); |
| 439 | return 'pe' . $sub . '.' . UserFilter::promoYear($this->grade); |
| 440 | } else { |
| 441 | $sub = $uf->addDisplayFilter(); |
| 442 | return 'pd' . $sub . '.promo'; |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | class UFO_Name extends UserFilterOrder |
| 448 | { |
| 449 | private $type; |
| 450 | private $variant; |
| 451 | private $particle; |
| 452 | |
| 453 | public function __construct($type, $variant = null, $particle = false, $desc = false) |
| 454 | { |
| 455 | $this->type = $type; |
| 456 | $this->variant = $variant; |
| 457 | $this->particle = $particle; |
| 458 | $this->desc = $desc; |
| 459 | } |
| 460 | |
| 461 | protected function getSortTokens(UserFilter &$uf) |
| 462 | { |
| 463 | if (UserFilter::isDisplayName($this->type)) { |
| 464 | $sub = $uf->addDisplayFilter(); |
| 465 | return 'pd' . $sub . '.' . $this->type; |
| 466 | } else { |
| 467 | $sub = $uf->addNameFilter($this->type, $this->variant); |
| 468 | if ($this->particle) { |
| 469 | return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)'; |
| 470 | } else { |
| 471 | return 'pn' . $sub . '.name'; |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | class UFO_Registration extends UserFilterOrder |
| 478 | { |
| 479 | public function __construct($desc = false) |
| 480 | { |
| 481 | $this->desc = $desc; |
| 482 | } |
| 483 | |
| 484 | protected function getSortTokens(UserFilter &$uf) |
| 485 | { |
| 486 | return 'a.registration_date'; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | /*********************************** |
| 491 | ********************************* |
| 492 | USER FILTER CLASS |
| 493 | ********************************* |
| 494 | ***********************************/ |
| 495 | |
| 496 | class UserFilter |
| 497 | { |
| 498 | static private $joinMethods = array(); |
| 499 | |
| 500 | private $root; |
| 501 | private $sort = array(); |
| 502 | private $query = null; |
| 503 | private $orderby = null; |
| 504 | |
| 505 | private $lastcount = null; |
| 506 | |
| 507 | public function __construct($cond = null, $sort = null) |
| 508 | { |
| 509 | if (empty(self::$joinMethods)) { |
| 510 | $class = new ReflectionClass('UserFilter'); |
| 511 | foreach ($class->getMethods() as $method) { |
| 512 | $name = $method->getName(); |
| 513 | if (substr($name, -5) == 'Joins' && $name != 'buildJoins') { |
| 514 | self::$joinMethods[] = $name; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | if (!is_null($cond)) { |
| 519 | if ($cond instanceof UserFilterCondition) { |
| 520 | $this->setCondition($cond); |
| 521 | } |
| 522 | } |
| 523 | if (!is_null($sort)) { |
| 524 | if ($sort instanceof UserFilterOrder) { |
| 525 | $this->addSort($sort); |
| 526 | } else if (is_array($sort)) { |
| 527 | foreach ($sort as $s) { |
| 528 | $this->addSort($s); |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | private function buildQuery() |
| 535 | { |
| 536 | if (is_null($this->orderby)) { |
| 537 | $orders = array(); |
| 538 | foreach ($this->sort as $sort) { |
| 539 | $orders = array_merge($orders, $sort->buildSort($this)); |
| 540 | } |
| 541 | if (count($orders) == 0) { |
| 542 | $this->orderby = ''; |
| 543 | } else { |
| 544 | $this->orderby = 'ORDER BY ' . implode(', ', $orders); |
| 545 | } |
| 546 | } |
| 547 | if (is_null($this->query)) { |
| 548 | $where = $this->root->buildCondition($this); |
| 549 | $joins = $this->buildJoins(); |
| 550 | $this->query = 'FROM accounts AS a |
| 551 | INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms)) |
| 552 | INNER JOIN profiles AS p ON (p.pid = ap.pid) |
| 553 | ' . $joins . ' |
| 554 | WHERE (' . $where . ')'; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | private function formatJoin(array $joins) |
| 559 | { |
| 560 | $str = ''; |
| 561 | foreach ($joins as $key => $infos) { |
| 562 | $mode = $infos[0]; |
| 563 | $table = $infos[1]; |
| 564 | if ($mode == 'inner') { |
| 565 | $str .= 'INNER JOIN '; |
| 566 | } else if ($mode == 'left') { |
| 567 | $str .= 'LEFT JOIN '; |
| 568 | } else { |
| 569 | Platal::page()->kill("Join mode error"); |
| 570 | } |
| 571 | $str .= $table . ' AS ' . $key; |
| 572 | if (isset($infos[2])) { |
| 573 | $str .= ' ON (' . str_replace(array('$ME', '$PID', '$UID'), array($key, 'p.pid', 'a.uid'), $infos[2]) . ')'; |
| 574 | } |
| 575 | $str .= "\n"; |
| 576 | } |
| 577 | return $str; |
| 578 | } |
| 579 | |
| 580 | private function buildJoins() |
| 581 | { |
| 582 | $joins = array(); |
| 583 | foreach (self::$joinMethods as $method) { |
| 584 | $joins = array_merge($joins, $this->$method()); |
| 585 | } |
| 586 | return $this->formatJoin($joins); |
| 587 | } |
| 588 | |
| 589 | private function getUIDList($uids = null, $count = null, $offset = null) |
| 590 | { |
| 591 | $this->buildQuery(); |
| 592 | $limit = ''; |
| 593 | if (!is_null($count)) { |
| 594 | if (!is_null($offset)) { |
| 595 | $limit = XDB::format('LIMIT {?}, {?}', $offset, $count); |
| 596 | } else { |
| 597 | $limit = XDB::format('LIMIT {?}', $count); |
| 598 | } |
| 599 | } |
| 600 | $cond = ''; |
| 601 | if (!is_null($uids)) { |
| 602 | $cond = ' AND a.uid IN (' . implode(', ', $uids) . ')'; |
| 603 | } |
| 604 | $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid |
| 605 | ' . $this->query . $cond . ' |
| 606 | GROUP BY a.uid |
| 607 | ' . $this->orderby . ' |
| 608 | ' . $limit); |
| 609 | $this->lastcount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()'); |
| 610 | return $fetched; |
| 611 | } |
| 612 | |
| 613 | /** Check that the user match the given rule. |
| 614 | */ |
| 615 | public function checkUser(PlUser &$user) |
| 616 | { |
| 617 | $this->buildQuery(); |
| 618 | $count = (int)XDB::fetchOneCell('SELECT COUNT(*) |
| 619 | ' . $this->query . XDB::format(' AND a.uid = {?}', $user->id())); |
| 620 | return $count == 1; |
| 621 | } |
| 622 | |
| 623 | /** Filter a list of user to extract the users matching the rule. |
| 624 | */ |
| 625 | public function filter(array $users, $count = null, $offset = null) |
| 626 | { |
| 627 | $this->buildQuery(); |
| 628 | $table = array(); |
| 629 | $uids = array(); |
| 630 | foreach ($users as $user) { |
| 631 | $uids[] = $user->id(); |
| 632 | $table[$user->id()] = $user; |
| 633 | } |
| 634 | $fetched = $this->getUIDList($uids, $count, $offset); |
| 635 | $output = array(); |
| 636 | foreach ($fetched as $uid) { |
| 637 | $output[] = $table[$uid]; |
| 638 | } |
| 639 | return $output; |
| 640 | } |
| 641 | |
| 642 | public function getUIDs($count = null, $offset = null) |
| 643 | { |
| 644 | return $this->getUIDList(null, $count, $offset); |
| 645 | } |
| 646 | |
| 647 | public function getUsers($count = null, $offset = null) |
| 648 | { |
| 649 | return User::getBulkUsersWithUIDs($this->getUIDs($count, $offset)); |
| 650 | } |
| 651 | |
| 652 | public function getTotalCount() |
| 653 | { |
| 654 | if (is_null($this->lastcount)) { |
| 655 | $this->buildQuery(); |
| 656 | return (int)XDB::fetchOneCell('SELECT COUNT(*) |
| 657 | ' . $this->query . ' |
| 658 | GROUP BY a.uid'); |
| 659 | } else { |
| 660 | return $this->lastcount; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | public function setCondition(UserFilterCondition &$cond) |
| 665 | { |
| 666 | $this->root =& $cond; |
| 667 | $this->query = null; |
| 668 | } |
| 669 | |
| 670 | public function addSort(UserFilterOrder &$sort) |
| 671 | { |
| 672 | $this->sort[] = $sort; |
| 673 | $this->orderby = null; |
| 674 | } |
| 675 | |
| 676 | static public function getLegacy($promo_min, $promo_max) |
| 677 | { |
| 678 | if ($promo_min != 0) { |
| 679 | $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min)); |
| 680 | } else { |
| 681 | $min = new UFC_True(); |
| 682 | } |
| 683 | if ($promo_max != 0) { |
| 684 | $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max)); |
| 685 | } else { |
| 686 | $max = new UFC_True(); |
| 687 | } |
| 688 | return new UserFilter(new UFC_And($min, $max)); |
| 689 | } |
| 690 | |
| 691 | static private function getDBSuffix($string) |
| 692 | { |
| 693 | return preg_replace('/[^a-z0-9]/i', '', $string); |
| 694 | } |
| 695 | |
| 696 | |
| 697 | private $option = 0; |
| 698 | private function register_optional(array &$table, $val) |
| 699 | { |
| 700 | if (is_null($val)) { |
| 701 | $sub = $this->option++; |
| 702 | $index = null; |
| 703 | } else { |
| 704 | $sub = self::getDBSuffix($val); |
| 705 | $index = $val; |
| 706 | } |
| 707 | $sub = '_' . $sub; |
| 708 | $table[$sub] = $index; |
| 709 | return $sub; |
| 710 | } |
| 711 | |
| 712 | /** DISPLAY |
| 713 | */ |
| 714 | const DISPLAY = 'display'; |
| 715 | private $pd = false; |
| 716 | public function addDisplayFilter() |
| 717 | { |
| 718 | $this->pd = true; |
| 719 | return ''; |
| 720 | } |
| 721 | |
| 722 | private function displayJoins() |
| 723 | { |
| 724 | if ($this->pd) { |
| 725 | return array('pd' => array('left', 'profile_display', '$ME.pid = $PID')); |
| 726 | } else { |
| 727 | return array(); |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | /** NAMES |
| 732 | */ |
| 733 | /* name tokens */ |
| 734 | const LASTNAME = 'lastname'; |
| 735 | const FIRSTNAME = 'firstname'; |
| 736 | const NICKNAME = 'nickname'; |
| 737 | const PSEUDONYM = 'pseudonym'; |
| 738 | const NAME = 'name'; |
| 739 | /* name variants */ |
| 740 | const VN_MARITAL = 'marital'; |
| 741 | const VN_ORDINARY = 'ordinary'; |
| 742 | const VN_OTHER = 'other'; |
| 743 | const VN_INI = 'ini'; |
| 744 | /* display names */ |
| 745 | const DN_FULL = 'directory_name'; |
| 746 | const DN_DISPLAY = 'yourself'; |
| 747 | const DN_YOURSELF = 'yourself'; |
| 748 | const DN_DIRECTORY = 'directory_name'; |
| 749 | const DN_PRIVATE = 'private_name'; |
| 750 | const DN_PUBLIC = 'public_name'; |
| 751 | const DN_SHORT = 'short_name'; |
| 752 | const DN_SORT = 'sort_name'; |
| 753 | |
| 754 | static public $name_variants = array( |
| 755 | self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY), |
| 756 | self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER) |
| 757 | ); |
| 758 | |
| 759 | static public function assertName($name) |
| 760 | { |
| 761 | if (!Profile::getNameTypeId($name)) { |
| 762 | Platal::page()->kill('Invalid name type'); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | static public function isDisplayName($name) |
| 767 | { |
| 768 | return $name == self::DN_FULL || $name == self::DN_DISPLAY |
| 769 | || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY |
| 770 | || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC |
| 771 | || $name == self::DN_SHORT || $name == self::DN_SORT; |
| 772 | } |
| 773 | |
| 774 | private $pn = array(); |
| 775 | public function addNameFilter($type, $variant = null) |
| 776 | { |
| 777 | if (!is_null($variant)) { |
| 778 | $ft = $type . '_' . $variant; |
| 779 | } else { |
| 780 | $ft = $type; |
| 781 | } |
| 782 | $sub = '_' . $ft; |
| 783 | self::assertName($ft); |
| 784 | |
| 785 | if (!is_null($variant) && $variant == 'other') { |
| 786 | $sub .= $this->option++; |
| 787 | } |
| 788 | $this->pn[$sub] = Profile::getNameTypeId($ft); |
| 789 | return $sub; |
| 790 | } |
| 791 | |
| 792 | private function nameJoins() |
| 793 | { |
| 794 | $joins = array(); |
| 795 | foreach ($this->pn as $sub => $type) { |
| 796 | $joins['pn' . $sub] = array('left', 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type); |
| 797 | } |
| 798 | return $joins; |
| 799 | } |
| 800 | |
| 801 | |
| 802 | /** EDUCATION |
| 803 | */ |
| 804 | const GRADE_ING = 'Ing.'; |
| 805 | const GRADE_PHD = 'PhD'; |
| 806 | const GRADE_MST = 'M%'; |
| 807 | static public function isGrade($grade) |
| 808 | { |
| 809 | return $grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST; |
| 810 | } |
| 811 | |
| 812 | static public function assertGrade($grade) |
| 813 | { |
| 814 | if (!self::isGrade($grade)) { |
| 815 | Platal::page()->killError("DiplĂ´me non valide"); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | static public function promoYear($grade) |
| 820 | { |
| 821 | // XXX: Definition of promotion for phds and masters might change in near future. |
| 822 | return ($grade == UserFilter::GRADE_ING) ? 'entry_year' : 'grad_year'; |
| 823 | } |
| 824 | |
| 825 | private $pepe = array(); |
| 826 | private $with_pee = false; |
| 827 | public function addEducationFilter($x = false, $grade = null) |
| 828 | { |
| 829 | if (!$x) { |
| 830 | $index = $this->option; |
| 831 | $sub = $this->option++; |
| 832 | } else { |
| 833 | self::assertGrade($grade); |
| 834 | $index = $grade; |
| 835 | $sub = $grade[0]; |
| 836 | $this->with_pee = true; |
| 837 | } |
| 838 | $sub = '_' . $sub; |
| 839 | $this->pepe[$index] = $sub; |
| 840 | return $sub; |
| 841 | } |
| 842 | |
| 843 | private function educationJoins() |
| 844 | { |
| 845 | $joins = array(); |
| 846 | if ($this->with_pee) { |
| 847 | $joins['pee'] = array('inner', 'profile_education_enum', 'pee.abbreviation = \'X\''); |
| 848 | } |
| 849 | foreach ($this->pepe as $grade => $sub) { |
| 850 | if ($this->isGrade($grade)) { |
| 851 | $joins['pe' . $sub] = array('left', 'profile_education', '$ME.eduid = pee.id AND $ME.uid = $PID'); |
| 852 | $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' . |
| 853 | XDB::format('{?}', $grade)); |
| 854 | } else { |
| 855 | $joins['pe' . $sub] = array('left', 'profile_education', '$ME.uid = $PID'); |
| 856 | $joins['pee' . $sub] = array('inner', 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid'); |
| 857 | $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid'); |
| 858 | } |
| 859 | } |
| 860 | return $joins; |
| 861 | } |
| 862 | |
| 863 | |
| 864 | /** GROUPS |
| 865 | */ |
| 866 | private $gpm = array(); |
| 867 | public function addGroupFilter($group = null) |
| 868 | { |
| 869 | if (!is_null($group)) { |
| 870 | if (ctype_digit($group)) { |
| 871 | $index = $sub = $group; |
| 872 | } else { |
| 873 | $index = $group; |
| 874 | $sub = self::getDBSuffix($group); |
| 875 | } |
| 876 | } else { |
| 877 | $sub = 'group_' . $this->option++; |
| 878 | $index = null; |
| 879 | } |
| 880 | $sub = '_' . $sub; |
| 881 | $this->gpm[$sub] = $index; |
| 882 | return $sub; |
| 883 | } |
| 884 | |
| 885 | private function groupJoins() |
| 886 | { |
| 887 | $joins = array(); |
| 888 | foreach ($this->gpm as $sub => $key) { |
| 889 | if (is_null($key)) { |
| 890 | $joins['gpa' . $sub] = array('inner', 'groupex.asso'); |
| 891 | $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id'); |
| 892 | } else if (ctype_digit($key)) { |
| 893 | $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = ' . $key); |
| 894 | } else { |
| 895 | $joins['gpa' . $sub] = array('inner', 'groupex.asso', XDB::format('$ME.diminutif = {?}', $key)); |
| 896 | $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id'); |
| 897 | } |
| 898 | } |
| 899 | return $joins; |
| 900 | } |
| 901 | |
| 902 | /** EMAILS |
| 903 | */ |
| 904 | private $e = array(); |
| 905 | public function addEmailRedirectFilter($email = null) |
| 906 | { |
| 907 | return $this->register_optional($this->e, $email); |
| 908 | } |
| 909 | |
| 910 | private $ve = array(); |
| 911 | public function addVirtualEmailFilter($email = null) |
| 912 | { |
| 913 | $this->addAliasFilter(self::ALIAS_FORLIFE); |
| 914 | return $this->register_optional($this->ve, $email); |
| 915 | } |
| 916 | |
| 917 | const ALIAS_BEST = 'bestalias'; |
| 918 | const ALIAS_FORLIFE = 'forlife'; |
| 919 | private $al = array(); |
| 920 | public function addAliasFilter($alias = null) |
| 921 | { |
| 922 | return $this->register_optional($this->al, $alias); |
| 923 | } |
| 924 | |
| 925 | private function emailJoins() |
| 926 | { |
| 927 | global $globals; |
| 928 | $joins = array(); |
| 929 | foreach ($this->e as $sub=>$key) { |
| 930 | if (is_null($key)) { |
| 931 | $joins['e' . $sub] = array('left', 'emails', '$ME.uid = $UID AND $ME.flags != \'filter\''); |
| 932 | } else { |
| 933 | $joins['e' . $sub] = array('left', 'emails', XDB::format('$ME.uid = $UID AND $ME.flags != \'filter\' AND $ME.email = {?}', $key)); |
| 934 | } |
| 935 | } |
| 936 | foreach ($this->al as $sub=>$key) { |
| 937 | if (is_null($key)) { |
| 938 | $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\')'); |
| 939 | } else if ($key == self::ALIAS_BEST) { |
| 940 | $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)'); |
| 941 | } else if ($key == self::ALIAS_FORLIFE) { |
| 942 | $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type = \'a_vie\''); |
| 943 | } else { |
| 944 | $joins['al' . $sub] = array('left', 'aliases', XDB::format('$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND $ME.alias = {?}', $key)); |
| 945 | } |
| 946 | } |
| 947 | foreach ($this->ve as $sub=>$key) { |
| 948 | if (is_null($key)) { |
| 949 | $joins['v' . $sub] = array('left', 'virtual', '$ME.type = \'user\''); |
| 950 | } else { |
| 951 | $joins['v' . $sub] = array('left', 'virtual', XDB::format('$ME.type = \'user\' AND $ME.alias = {?}', $key)); |
| 952 | } |
| 953 | $joins['vr' . $sub] = array('left', 'virtual_redirect', XDB::format('$ME.vid = v' . $sub . '.vid |
| 954 | AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}), |
| 955 | CONCAT(al_forlife.alias, \'@\', {?}), |
| 956 | a.email))', |
| 957 | $globals->mail->domain, $globals->mail->domain2)); |
| 958 | } |
| 959 | return $joins; |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 964 | ?> |