Autopromote.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * This class checks if user can get extra rights
  4. * because of conditions specified in $wgAutopromote
  5. */
  6. class Autopromote {
  7. /**
  8. * Get the groups for the given user based on $wgAutopromote.
  9. *
  10. * @param $user The user to get the groups for
  11. * @return array Array of groups to promote to.
  12. */
  13. public static function getAutopromoteGroups( User $user ) {
  14. global $wgAutopromote;
  15. $promote = array();
  16. foreach( $wgAutopromote as $group => $cond ) {
  17. if( self::recCheckCondition( $cond, $user ) )
  18. $promote[] = $group;
  19. }
  20. wfRunHooks( 'GetAutoPromoteGroups', array( $user, &$promote ) );
  21. return $promote;
  22. }
  23. /**
  24. * Recursively check a condition. Conditions are in the form
  25. * array( '&' or '|' or '^', cond1, cond2, ... )
  26. * where cond1, cond2, ... are themselves conditions; *OR*
  27. * APCOND_EMAILCONFIRMED, *OR*
  28. * array( APCOND_EMAILCONFIRMED ), *OR*
  29. * array( APCOND_EDITCOUNT, number of edits ), *OR*
  30. * array( APCOND_AGE, seconds since registration ), *OR*
  31. * similar constructs defined by extensions.
  32. * This function evaluates the former type recursively, and passes off to
  33. * self::checkCondition for evaluation of the latter type.
  34. *
  35. * @param $cond Mixed: a condition, possibly containing other conditions
  36. * @param $user The user to check the conditions against
  37. * @return bool Whether the condition is true
  38. */
  39. private static function recCheckCondition( $cond, User $user ) {
  40. $validOps = array( '&', '|', '^', '!' );
  41. if( is_array( $cond ) && count( $cond ) >= 2 && in_array( $cond[0], $validOps ) ) {
  42. # Recursive condition
  43. if( $cond[0] == '&' ) {
  44. foreach( array_slice( $cond, 1 ) as $subcond )
  45. if( !self::recCheckCondition( $subcond, $user ) )
  46. return false;
  47. return true;
  48. } elseif( $cond[0] == '|' ) {
  49. foreach( array_slice( $cond, 1 ) as $subcond )
  50. if( self::recCheckCondition( $subcond, $user ) )
  51. return true;
  52. return false;
  53. } elseif( $cond[0] == '^' ) {
  54. $res = null;
  55. foreach( array_slice( $cond, 1 ) as $subcond ) {
  56. if( is_null( $res ) )
  57. $res = self::recCheckCondition( $subcond, $user );
  58. else
  59. $res = ($res xor self::recCheckCondition( $subcond, $user ));
  60. }
  61. return $res;
  62. } elseif ( $cond[0] = '!' ) {
  63. foreach( array_slice( $cond, 1 ) as $subcond )
  64. if( self::recCheckCondition( $subcond, $user ) )
  65. return false;
  66. return true;
  67. }
  68. }
  69. # If we got here, the array presumably does not contain other condi-
  70. # tions; it's not recursive. Pass it off to self::checkCondition.
  71. if( !is_array( $cond ) )
  72. $cond = array( $cond );
  73. return self::checkCondition( $cond, $user );
  74. }
  75. /**
  76. * As recCheckCondition, but *not* recursive. The only valid conditions
  77. * are those whose first element is APCOND_EMAILCONFIRMED/APCOND_EDITCOUNT/
  78. * APCOND_AGE. Other types will throw an exception if no extension evalu-
  79. * ates them.
  80. *
  81. * @param $cond Array: A condition, which must not contain other conditions
  82. * @param $user The user to check the condition against
  83. * @return bool Whether the condition is true for the user
  84. */
  85. private static function checkCondition( $cond, User $user ) {
  86. if( count( $cond ) < 1 )
  87. return false;
  88. switch( $cond[0] ) {
  89. case APCOND_EMAILCONFIRMED:
  90. if( User::isValidEmailAddr( $user->getEmail() ) ) {
  91. global $wgEmailAuthentication;
  92. if( $wgEmailAuthentication ) {
  93. return (bool)$user->getEmailAuthenticationTimestamp();
  94. } else {
  95. return true;
  96. }
  97. }
  98. return false;
  99. case APCOND_EDITCOUNT:
  100. return $user->getEditCount() >= $cond[1];
  101. case APCOND_AGE:
  102. $age = time() - wfTimestampOrNull( TS_UNIX, $user->getRegistration() );
  103. return $age >= $cond[1];
  104. case APCOND_AGE_FROM_EDIT:
  105. $age = time() - wfTimestampOrNull( TS_UNIX, $user->getFirstEditTimestamp() );
  106. return $age >= $cond[1];
  107. case APCOND_INGROUPS:
  108. $groups = array_slice( $cond, 1 );
  109. return count( array_intersect( $groups, $user->getGroups() ) ) == count( $groups );
  110. case APCOND_ISIP:
  111. return $cond[1] == wfGetIP();
  112. case APCOND_IPINRANGE:
  113. return IP::isInRange( wfGetIP(), $cond[1] );
  114. default:
  115. $result = null;
  116. wfRunHooks( 'AutopromoteCondition', array( $cond[0], array_slice( $cond, 1 ), $user, &$result ) );
  117. if( $result === null ) {
  118. throw new MWException( "Unrecognized condition {$cond[0]} for autopromotion!" );
  119. }
  120. return $result ? true : false;
  121. }
  122. }
  123. }