Ran stylize.php, removed trailing whitespace, updated indentation and code formatting.
[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 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 wfRunHooks( 'GetAutoPromoteGroups', array( $user, &$promote ) );
25
26 return $promote;
27 }
28
29 /**
30 * Recursively check a condition. Conditions are in the form
31 * array( '&' or '|' or '^', cond1, cond2, ... )
32 * where cond1, cond2, ... are themselves conditions; *OR*
33 * APCOND_EMAILCONFIRMED, *OR*
34 * array( APCOND_EMAILCONFIRMED ), *OR*
35 * array( APCOND_EDITCOUNT, number of edits ), *OR*
36 * array( APCOND_AGE, seconds since registration ), *OR*
37 * similar constructs defined by extensions.
38 * This function evaluates the former type recursively, and passes off to
39 * self::checkCondition for evaluation of the latter type.
40 *
41 * @param $cond Mixed: a condition, possibly containing other conditions
42 * @param $user The user to check the conditions against
43 * @return bool Whether the condition is true
44 */
45 private static function recCheckCondition( $cond, User $user ) {
46 $validOps = array( '&', '|', '^', '!' );
47
48 if ( is_array( $cond ) && count( $cond ) >= 2 && in_array( $cond[0], $validOps ) ) {
49 # Recursive condition
50 if ( $cond[0] == '&' ) {
51 foreach ( array_slice( $cond, 1 ) as $subcond ) {
52 if ( !self::recCheckCondition( $subcond, $user ) ) {
53 return false;
54 }
55 }
56
57 return true;
58 } elseif ( $cond[0] == '|' ) {
59 foreach ( array_slice( $cond, 1 ) as $subcond ) {
60 if ( self::recCheckCondition( $subcond, $user ) ) {
61 return true;
62 }
63 }
64
65 return false;
66 } elseif ( $cond[0] == '^' ) {
67 $res = null;
68 foreach ( array_slice( $cond, 1 ) as $subcond ) {
69 if ( is_null( $res ) ) {
70 $res = self::recCheckCondition( $subcond, $user );
71 } else {
72 $res = ( $res xor self::recCheckCondition( $subcond, $user ) );
73 }
74 }
75
76 return $res;
77 } elseif ( $cond[0] = '!' ) {
78 foreach ( array_slice( $cond, 1 ) as $subcond ) {
79 if ( self::recCheckCondition( $subcond, $user ) ) {
80 return false;
81 }
82 }
83
84 return true;
85 }
86 }
87 # If we got here, the array presumably does not contain other condi-
88 # tions; it's not recursive. Pass it off to self::checkCondition.
89 if ( !is_array( $cond ) ) {
90 $cond = array( $cond );
91 }
92
93 return self::checkCondition( $cond, $user );
94 }
95
96 /**
97 * As recCheckCondition, but *not* recursive. The only valid conditions
98 * are those whose first element is APCOND_EMAILCONFIRMED/APCOND_EDITCOUNT/
99 * APCOND_AGE. Other types will throw an exception if no extension evalu-
100 * ates them.
101 *
102 * @param $cond Array: A condition, which must not contain other conditions
103 * @param $user The user to check the condition against
104 * @return bool Whether the condition is true for the user
105 */
106 private static function checkCondition( $cond, User $user ) {
107 if ( count( $cond ) < 1 ) {
108 return false;
109 }
110
111 switch( $cond[0] ) {
112 case APCOND_EMAILCONFIRMED:
113 if ( User::isValidEmailAddr( $user->getEmail() ) ) {
114 global $wgEmailAuthentication;
115 if ( $wgEmailAuthentication ) {
116 return (bool)$user->getEmailAuthenticationTimestamp();
117 } else {
118 return true;
119 }
120 }
121 return false;
122 case APCOND_EDITCOUNT:
123 return $user->getEditCount() >= $cond[1];
124 case APCOND_AGE:
125 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getRegistration() );
126 return $age >= $cond[1];
127 case APCOND_AGE_FROM_EDIT:
128 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getFirstEditTimestamp() );
129 return $age >= $cond[1];
130 case APCOND_INGROUPS:
131 $groups = array_slice( $cond, 1 );
132 return count( array_intersect( $groups, $user->getGroups() ) ) == count( $groups );
133 case APCOND_ISIP:
134 return $cond[1] == wfGetIP();
135 case APCOND_IPINRANGE:
136 return IP::isInRange( wfGetIP(), $cond[1] );
137 case APCOND_BLOCKED:
138 return $user->isBlocked();
139 default:
140 $result = null;
141 wfRunHooks( 'AutopromoteCondition', array( $cond[0], array_slice( $cond, 1 ), $user, &$result ) );
142 if ( $result === null ) {
143 throw new MWException( "Unrecognized condition {$cond[0]} for autopromotion!" );
144 }
145
146 return $result ? true : false;
147 }
148 }
149 }