Update calls to deprecated User::isValidEmailAddr
[lhc/web/wiklou.git] / includes / Autopromote.php
1 <?php
2 /**
3 * This class checks if user can get extra rights
4 * because of conditions specified in $wgAutopromote
5 */
6
7 class Autopromote {
8 /**
9 * Get the groups for the given user based on $wgAutopromote.
10 *
11 * @param $user User The user to get the groups for
12 * @return array Array of groups to promote to.
13 */
14 public static function getAutopromoteGroups( User $user ) {
15 global $wgAutopromote;
16
17 $promote = array();
18
19 foreach ( $wgAutopromote as $group => $cond ) {
20 if ( self::recCheckCondition( $cond, $user ) ) {
21 $promote[] = $group;
22 }
23 }
24
25 wfRunHooks( 'GetAutoPromoteGroups', array( $user, &$promote ) );
26
27 return $promote;
28 }
29
30 /**
31 * Get the groups for the given user based on the given criteria.
32 *
33 * Does not return groups the user already belongs to or has once belonged.
34 *
35 * @param $user The user to get the groups for
36 * @param $event String key in $wgAutopromoteOnce (each one has groups/criteria)
37 *
38 * @return array Groups the user should be promoted to.
39 *
40 * @see $wgAutopromoteOnce
41 */
42 public static function getAutopromoteOnceGroups( User $user, $event ) {
43 global $wgAutopromoteOnce;
44
45 $promote = array();
46
47 if ( isset( $wgAutopromoteOnce[$event] ) && count( $wgAutopromoteOnce[$event] ) ) {
48 $currentGroups = $user->getGroups();
49 foreach ( $wgAutopromoteOnce[$event] as $group => $cond ) {
50 // Do not check if the user's already a member
51 if ( in_array( $group, $currentGroups ) ) {
52 continue;
53 }
54 // Do not autopromote if the user has belonged to the group
55 $formerGroups = $user->getFormerGroups();
56 if ( in_array( $group, $formerGroups ) ) {
57 continue;
58 }
59 // Finally - check the conditions
60 if ( self::recCheckCondition( $cond, $user ) ) {
61 $promote[] = $group;
62 }
63 }
64 }
65
66 return $promote;
67 }
68
69 /**
70 * Recursively check a condition. Conditions are in the form
71 * array( '&' or '|' or '^' or '!', cond1, cond2, ... )
72 * where cond1, cond2, ... are themselves conditions; *OR*
73 * APCOND_EMAILCONFIRMED, *OR*
74 * array( APCOND_EMAILCONFIRMED ), *OR*
75 * array( APCOND_EDITCOUNT, number of edits ), *OR*
76 * array( APCOND_AGE, seconds since registration ), *OR*
77 * similar constructs defined by extensions.
78 * This function evaluates the former type recursively, and passes off to
79 * self::checkCondition for evaluation of the latter type.
80 *
81 * @param $cond Mixed: a condition, possibly containing other conditions
82 * @param $user User The user to check the conditions against
83 * @return bool Whether the condition is true
84 */
85 private static function recCheckCondition( $cond, User $user ) {
86 $validOps = array( '&', '|', '^', '!' );
87
88 if ( is_array( $cond ) && count( $cond ) >= 2 && in_array( $cond[0], $validOps ) ) {
89 # Recursive condition
90 if ( $cond[0] == '&' ) { // AND (all conds pass)
91 foreach ( array_slice( $cond, 1 ) as $subcond ) {
92 if ( !self::recCheckCondition( $subcond, $user ) ) {
93 return false;
94 }
95 }
96
97 return true;
98 } elseif ( $cond[0] == '|' ) { // OR (at least one cond passes)
99 foreach ( array_slice( $cond, 1 ) as $subcond ) {
100 if ( self::recCheckCondition( $subcond, $user ) ) {
101 return true;
102 }
103 }
104
105 return false;
106 } elseif ( $cond[0] == '^' ) { // XOR (exactly one cond passes)
107 $res = false;
108 foreach ( array_slice( $cond, 1 ) as $subcond ) {
109 if ( self::recCheckCondition( $subcond, $user ) ) {
110 if ( $res ) {
111 return false;
112 } else {
113 $res = true;
114 }
115 }
116 }
117
118 return $res;
119 } elseif ( $cond[0] == '!' ) { // NOT (no conds pass)
120 foreach ( array_slice( $cond, 1 ) as $subcond ) {
121 if ( self::recCheckCondition( $subcond, $user ) ) {
122 return false;
123 }
124 }
125
126 return true;
127 }
128 }
129 # If we got here, the array presumably does not contain other condi-
130 # tions; it's not recursive. Pass it off to self::checkCondition.
131 if ( !is_array( $cond ) ) {
132 $cond = array( $cond );
133 }
134
135 return self::checkCondition( $cond, $user );
136 }
137
138 /**
139 * As recCheckCondition, but *not* recursive. The only valid conditions
140 * are those whose first element is APCOND_EMAILCONFIRMED/APCOND_EDITCOUNT/
141 * APCOND_AGE. Other types will throw an exception if no extension evalu-
142 * ates them.
143 *
144 * @param $cond Array: A condition, which must not contain other conditions
145 * @param $user User The user to check the condition against
146 * @return bool Whether the condition is true for the user
147 */
148 private static function checkCondition( $cond, User $user ) {
149 global $wgEmailAuthentication;
150 if ( count( $cond ) < 1 ) {
151 return false;
152 }
153
154 switch( $cond[0] ) {
155 case APCOND_EMAILCONFIRMED:
156 if ( Sanitizer::validateEmail( $user->getEmail() ) ) {
157 if ( $wgEmailAuthentication ) {
158 return (bool)$user->getEmailAuthenticationTimestamp();
159 } else {
160 return true;
161 }
162 }
163 return false;
164 case APCOND_EDITCOUNT:
165 return $user->getEditCount() >= $cond[1];
166 case APCOND_AGE:
167 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getRegistration() );
168 return $age >= $cond[1];
169 case APCOND_AGE_FROM_EDIT:
170 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getFirstEditTimestamp() );
171 return $age >= $cond[1];
172 case APCOND_INGROUPS:
173 $groups = array_slice( $cond, 1 );
174 return count( array_intersect( $groups, $user->getGroups() ) ) == count( $groups );
175 case APCOND_ISIP:
176 return $cond[1] == wfGetIP();
177 case APCOND_IPINRANGE:
178 return IP::isInRange( wfGetIP(), $cond[1] );
179 case APCOND_BLOCKED:
180 return $user->isBlocked();
181 case APCOND_ISBOT:
182 return in_array( 'bot', User::getGroupPermissions( $user->getGroups() ) );
183 default:
184 $result = null;
185 wfRunHooks( 'AutopromoteCondition', array( $cond[0], array_slice( $cond, 1 ), $user, &$result ) );
186 if ( $result === null ) {
187 throw new MWException( "Unrecognized condition {$cond[0]} for autopromotion!" );
188 }
189
190 return (bool)$result;
191 }
192 }
193 }