2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
27 interface UserFilterCondition
29 const COND_TRUE
= 'TRUE';
30 const COND_FALSE
= 'FALSE';
32 /** Check that the given user matches the rule.
34 public function buildCondition(UserFilter
&$uf);
37 abstract class UFC_OneChild
implements UserFilterCondition
41 public function __construct($child = null
)
43 if (!is_null($child) && ($child instanceof UserFilterCondition
)) {
44 $this->setChild($child);
48 public function setChild(UserFilterCondition
&$cond)
50 $this->child
=& $cond;
54 abstract class UFC_NChildren
implements UserFilterCondition
56 protected $children = array();
58 public function __construct()
60 $children = func_get_args();
61 foreach ($children as &$child) {
62 if (!is_null($child) && ($child instanceof UserFilterCondition
)) {
63 $this->addChild($child);
68 public function addChild(UserFilterCondition
&$cond)
70 $this->children
[] =& $cond;
73 protected function catConds(array $cond, $op, $fallback)
75 if (count($cond) == 0) {
77 } else if (count($cond) == 1) {
80 return '(' . implode(') ' . $op . ' (', $cond) . ')';
85 class UFC_True
implements UserFilterCondition
87 public function buildCondition(UserFilter
&$uf)
89 return self
::COND_TRUE
;
93 class UFC_False
implements UserFilterCondition
95 public function buildCondition(UserFilter
&$uf)
97 return self
::COND_FALSE
;
101 class UFC_Not
extends UFC_OneChild
103 public function buildCondition(UserFilter
&$uf)
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
;
111 return 'NOT (' . $val . ')';
116 class UFC_And
extends UFC_NChildren
118 public function buildCondition(UserFilter
&$uf)
120 if (empty($this->children
)) {
121 return self
::COND_FALSE
;
123 $true = self
::COND_FALSE
;
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
;
135 return $this->catConds($conds, 'AND', $true);
140 class UFC_Or
extends UFC_NChildren
142 public function buildCondition(UserFilter
&$uf)
144 if (empty($this->children
)) {
145 return self
::COND_TRUE
;
147 $true = self
::COND_TRUE
;
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
;
159 return $this->catConds($conds, 'OR', $true);
164 class UFC_Promo
implements UserFilterCondition
171 public function __construct($comparison, $grade, $promo)
173 $this->grade
= $grade;
174 $this->comparison
= $comparison;
175 $this->promo
= $promo;
176 if ($this->grade
!= UserFilter
::DISPLAY
) {
177 UserFilter
::assertGrade($this->grade
);
181 public function buildCondition(UserFilter
&$uf)
183 if ($this->grade
== UserFilter
::DISPLAY
) {
184 $sub = $uf->addDisplayFilter();
185 return XDB
::format('pd' . $sub . '.promo = {?}', $this->promo
);
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
);
194 class UFC_Name
implements UserFilterCondition
206 public function __construct($type, $text, $mode)
213 private function buildNameQuery($type, $variant, $where, UserFilter
&$uf)
215 $sub = $uf->addNameFilter($type, $variant);
216 return str_replace('$ME', 'pn' . $sub, $where);
219 public function buildCondition(UserFilter
&$uf)
223 if (($this->mode
& self
::PARTICLE
) == self
::PARTICLE
) {
224 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
226 if (($this->mode
& self
::CONTAINS
) == 0) {
227 $right = XDB
::format('{?}', $this->text
);
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
);
234 $right = XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->text
);
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);
243 return implode(' OR ', $conds);
247 class UFC_Dead
implements UserFilterCondition
252 public function __construct($comparison = null
, $date = null
)
254 $this->comparison
= $comparison;
258 public function buildCondition(UserFilter
&$uf)
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
));
268 class UFC_Registered
implements UserFilterCondition
274 public function __construct($active = false
, $comparison = null
, $date = null
)
276 $this->only_active
= $active;
277 $this->comparison
= $comparison;
281 public function buildCondition(UserFilter
&$uf)
284 $date = 'a.uid IS NOT NULL AND a.state = \'active\'';
286 $date = 'a.uid IS NOT NULL AND a.state != \'pending\'';
288 if (!is_null($this->comparison
)) {
289 $date .= ' AND a.registration_date ' . $this->comparison
. ' ' . XDB
::format('{?}', date('Y-m-d', $this->date
));
295 class UFC_Sex
implements UserFilterCondition
298 public function __construct($sex)
303 public function buildCondition(UserFilter
&$uf)
305 if ($this->sex
!= User
::GENDER_MALE
&& $this->sex
!= User
::GENDER_FEMALE
) {
306 return self
::COND_FALSE
;
308 return XDB
::format('p.sex = {?}', $this->sex
== User
::GENDER_FEMALE ?
'female' : 'male');
313 class UFC_Group
implements UserFilterCondition
317 public function __construct($group, $admin = false
)
319 $this->group
= $group;
320 $this->admin
= $admin;
323 public function buildCondition(UserFilter
&$uf)
325 $sub = $uf->addGroupFilter($this->group
);
326 $where = 'gpm' . $sub . '.perms IS NOT NULL';
328 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
334 class UFC_Email
implements UserFilterCondition
337 public function __construct($email)
339 $this->email
= $email;
342 public function buildCondition(UserFilter
&$uf)
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
);
348 if (User
::isVirtualEmailAddress($this->email
)) {
349 $sub = $uf->addVirtualEmailFilter($this->email
);
351 list($user, $domain) = explode('@', $this->email
);
352 $sub = $uf->addAliasFilter($user);
354 return 'al' . $sub . '.alias IS NOT NULL';
364 abstract class UserFilterOrder
366 protected $desc = false
;
368 public function buildSort(UserFilter
&$uf)
370 $sel = $this->getSortTokens($uf);
371 if (!is_array($sel)) {
375 foreach ($sel as $k=>$s) {
376 $sel[$k] = $s . ' DESC';
382 abstract protected function getSortTokens(UserFilter
&$uf);
385 class UFO_Promo
extends UserFilterOrder
389 public function __construct($grade = null
, $desc = false
)
391 $this->grade
= $grade;
395 protected function getSortTokens(UserFilter
&$uf)
397 if (UserFilter
::isGrade($this->grade
)) {
398 $sub = $uf->addEducationFilter($this->grade
);
399 return 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
401 $sub = $uf->addDisplayFilter();
402 return 'pd' . $sub . '.promo';
407 class UFO_Name
extends UserFilterOrder
413 public function __construct($type, $variant = null
, $particle = false
, $desc = false
)
416 $this->variant
= $variant;
417 $this->particle
= $particle;
421 protected function getSortTokens(UserFilter
&$uf)
423 if (UserFilter
::isDisplayName($this->type
)) {
424 $sub = $uf->addDisplayFilter();
425 return 'pd' . $sub . '.' . $this->type
;
427 $sub = $uf->addNameFilter($this->type
, $this->variant
);
428 if ($this->particle
) {
429 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
431 return 'pn' . $sub . '.name';
437 class UFO_Registration
extends UserFilterOrder
439 public function __construct($desc = false
)
444 protected function getSortTokens(UserFilter
&$uf)
446 return 'a.registration_date';
450 /***********************************
451 *********************************
453 *********************************
454 ***********************************/
458 static private $joinMethods = array();
461 private $sort = array();
462 private $query = null
;
463 private $orderby = null
;
465 private $lastcount = null
;
467 public function __construct($cond = null
, $sort = null
)
469 if (empty(self
::$joinMethods)) {
470 $class = new ReflectionClass('UserFilter');
471 foreach ($class->getMethods() as $method) {
472 $name = $method->getName();
473 if (substr($name, -5) == 'Joins' && $name != 'buildJoins') {
474 self
::$joinMethods[] = $name;
478 if (!is_null($cond)) {
479 if ($cond instanceof UserFilterCondition
) {
480 $this->setCondition($cond);
483 if (!is_null($sort)) {
484 if ($sort instanceof UserFilterOrder
) {
485 $this->addSort($sort);
486 } else if (is_array($sort)) {
487 foreach ($sort as $s) {
494 private function buildQuery()
496 if (is_null($this->orderby
)) {
498 foreach ($this->sort
as $sort) {
499 $orders = array_merge($orders, $sort->buildSort($this));
501 if (count($orders) == 0) {
504 $this->orderby
= 'ORDER BY ' . implode(', ', $orders);
507 if (is_null($this->query
)) {
508 $where = $this->root
->buildCondition($this);
509 $joins = $this->buildJoins();
510 $this->query
= 'FROM accounts AS a
511 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
512 INNER JOIN profiles AS p ON (p.pid = ap.pid)
514 WHERE (' . $where . ')';
518 private function formatJoin(array $joins)
521 foreach ($joins as $key => $infos) {
524 if ($mode == 'inner') {
525 $str .= 'INNER JOIN ';
526 } else if ($mode == 'left') {
527 $str .= 'LEFT JOIN ';
529 Platal
::page()->kill("Join mode error");
531 $str .= $table . ' AS ' . $key;
532 if (isset($infos[2])) {
533 $str .= ' ON (' . str_replace(array('$ME', '$PID', '$UID'), array($key, 'p.pid', 'a.uid'), $infos[2]) . ')';
540 private function buildJoins()
543 foreach (self
::$joinMethods as $method) {
544 $joins = array_merge($joins, $this->$method());
546 return $this->formatJoin($joins);
549 private function getUIDList($uids = null
, $count = null
, $offset = null
)
553 if (!is_null($count)) {
554 if (!is_null($offset)) {
555 $limit = XDB
::format('LIMIT {?}, {?}', $offset, $count);
557 $limit = XDB
::format('LIMIT {?}', $count);
561 if (!is_null($uids)) {
562 $cond = ' AND a.uid IN (' . implode(', ', $uids) . ')';
564 $fetched = XDB
::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
565 ' . $this->query
. $cond . '
567 ' . $this->orderby
. '
569 $this->lastcount
= (int)XDB
::fetchOneCell('SELECT FOUND_ROWS()');
573 /** Check that the user match the given rule.
575 public function checkUser(PlUser
&$user)
578 $count = (int)XDB
::fetchOneCell('SELECT COUNT(*)
579 ' . $this->query
. XDB
::format(' AND a.uid = {?}', $user->id()));
583 /** Filter a list of user to extract the users matching the rule.
585 public function filter(array $users, $count = null
, $offset = null
)
590 foreach ($users as $user) {
591 $uids[] = $user->id();
592 $table[$user->id()] = $user;
594 $fetched = $this->getUIDList($uids, $count, $offset);
596 foreach ($fetched as $uid) {
597 $output[] = $table[$uid];
602 public function getUIDs($count = null
, $offset = null
)
604 return $this->getUIDList(null
, $count, $offset);
607 public function getUsers($count = null
, $offset = null
)
609 return User
::getBulkUsersWithUIDs($this->getUIDs($count, $offset));
612 public function getTotalCount()
614 if (is_null($this->lastcount
)) {
616 return (int)XDB
::fetchOneCell('SELECT COUNT(*)
620 return $this->lastcount
;
624 public function setCondition(UserFilterCondition
&$cond)
626 $this->root
=& $cond;
630 public function addSort(UserFilterOrder
&$sort)
632 $this->sort
[] = $sort;
633 $this->orderby
= null
;
636 static public function getLegacy($promo_min, $promo_max)
638 if ($promo_min != 0) {
639 $min = new UFC_Promo('>=', self
::GRADE_ING
, intval($promo_min));
641 $min = new UFC_True();
643 if ($promo_max != 0) {
644 $max = new UFC_Promo('<=', self
::GRADE_ING
, intval($promo_max));
646 $max = new UFC_True();
648 return new UserFilter(new UFC_And($min, $max));
651 static private function getDBSuffix($string)
653 return preg_replace('/[^a-z0-9]/i', '', $string);
658 private function register_optional(array &$table, $val)
661 $sub = $this->option++
;
664 $sub = self
::getDBSuffix($val);
668 $table[$sub] = $index;
674 const DISPLAY
= 'display';
676 public function addDisplayFilter()
682 private function displayJoins()
685 return array('pd' => array('left', 'profile_display', '$ME.pid = $PID'));
694 const LASTNAME
= 'lastname';
695 const FIRSTNAME
= 'firstname';
696 const NICKNAME
= 'nickname';
697 const PSEUDONYM
= 'pseudonym';
700 const VN_MARITAL
= 'marital';
701 const VN_ORDINARY
= 'ordinary';
702 const VN_OTHER
= 'other';
703 const VN_INI
= 'ini';
705 const DN_FULL
= 'directory_name';
706 const DN_DISPLAY
= 'yourself';
707 const DN_YOURSELF
= 'yourself';
708 const DN_DIRECTORY
= 'directory_name';
709 const DN_PRIVATE
= 'private_name';
710 const DN_PUBLIC
= 'public_name';
711 const DN_SHORT
= 'short_name';
712 const DN_SORT
= 'sort_name';
714 static public $name_variants = array(
715 self
::LASTNAME
=> array(self
::VN_MARITAL
, self
::VN_ORDINARY
),
716 self
::FIRSTNAME
=> array(self
::VN_ORDINARY
, self
::VN_INI
, self
::VN_OTHER
)
719 static public function assertName($name)
721 if (!Profile
::getNameTypeId($name)) {
722 Platal
::page()->kill('Invalid name type');
726 static public function isDisplayName($name)
728 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
729 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
730 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
731 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
734 private $pn = array();
735 public function addNameFilter($type, $variant = null
)
737 if (!is_null($variant)) {
738 $ft = $type . '_' . $variant;
743 self
::assertName($ft);
745 if (!is_null($variant) && $variant == 'other') {
746 $sub .= $this->option++
;
748 $this->pn
[$sub] = Profile
::getNameTypeId($ft);
752 private function nameJoins()
755 foreach ($this->pn
as $sub => $type) {
756 $joins['pn' . $sub] = array('left', 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type);
764 const GRADE_ING
= 'Ing.';
765 const GRADE_PHD
= 'PhD';
766 const GRADE_MST
= 'M%';
767 static public function isGrade($grade)
769 return $grade == self
::GRADE_ING ||
$grade == self
::GRADE_PHD ||
$grade == self
::GRADE_MST
;
772 static public function assertGrade($grade)
774 if (!self
::isGrade($grade)) {
775 Platal
::page()->killError("DiplĂ´me non valide");
779 static public function promoYear($grade)
781 // XXX: Definition of promotion for phds and masters might change in near future.
782 return ($grade == UserFilter
::GRADE_ING
) ?
'entry_year' : 'grad_year';
785 private $pepe = array();
786 private $with_pee = false
;
787 public function addEducationFilter($x = false
, $grade = null
)
790 $index = $this->option
;
791 $sub = $this->option++
;
793 self
::assertGrade($grade);
796 $this->with_pee
= true
;
799 $this->pepe
[$index] = $sub;
803 private function educationJoins()
806 if ($this->with_pee
) {
807 $joins['pee'] = array('inner', 'profile_education_enum', 'pee.abbreviation = \'X\'');
809 foreach ($this->pepe
as $grade => $sub) {
810 if ($this->isGrade($grade)) {
811 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.eduid = pee.id AND $ME.uid = $PID');
812 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' .
813 XDB
::format('{?}', $grade));
815 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.uid = $PID');
816 $joins['pee' . $sub] = array('inner', 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
817 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
826 private $gpm = array();
827 public function addGroupFilter($group = null
)
829 if (!is_null($group)) {
830 if (ctype_digit($group)) {
831 $index = $sub = $group;
834 $sub = self
::getDBSuffix($group);
837 $sub = 'group_' . $this->option++
;
841 $this->gpm
[$sub] = $index;
845 private function groupJoins()
848 foreach ($this->gpm
as $sub => $key) {
850 $joins['gpa' . $sub] = array('inner', 'groupex.asso');
851 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
852 } else if (ctype_digit($key)) {
853 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
855 $joins['gpa' . $sub] = array('inner', 'groupex.asso', XDB
::format('$ME.diminutif = {?}', $key));
856 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
864 private $e = array();
865 public function addEmailRedirectFilter($email = null
)
867 return $this->register_optional($this->e
, $email);
870 private $ve = array();
871 public function addVirtualEmailFilter($email = null
)
873 return $this->register_optional($this->ve
, $email);
876 private $al = array();
877 public function addAliasFilter($alias = null
)
879 return $this->register_optional($this->al
, $alias);
882 private function emailJoins()
886 foreach ($this->e
as $sub=>$key) {
888 $joins['e' . $sub] = array('left', 'emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
890 $joins['e' . $sub] = array('left', 'emails', XDB
::format('$ME.uid = $UID AND $ME.flags != \'filter\' AND $ME.email = {?}', $key));
893 foreach ($this->ve
as $sub=>$key) {
895 $joins['v' . $sub] = array('left', 'virtual', '$ME.type = \'user\'');
897 $joins['v' . $sub] = array('left', 'virtual', XDB
::format('$ME.type = \'user\' AND $ME.alias = {?}', $key));
899 $joins['vr' . $sub] = array('inner', 'virtual_redirect', '$ME.vid = v' . $sub . '.vid');
900 $joins['al' . $sub] = array('left', 'aliases', XDB
::format('$ME.id = $UID AND (CONCAT($ME.alias, \'@\', {?}) = vr'. $sub . '.redirect
901 OR CONCAT($ME.alias, \'@\', {?}) = vr'. $sub . '.redirect)',
902 $globals->mail
->domain
, $globals->mail
->domain2
));
904 foreach ($this->al
as $sub=>$key) {
906 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID');
908 $joins['al' . $sub] = array('left', 'aliases', XDB
::format('$ME.id = $UID AND $ME.alias = {?}', $key));
915 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: