PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/ConfirmEdit/includes/ConfirmEditHooks.php

https://gitlab.com/link233/bootmw
PHP | 238 lines | 142 code | 35 blank | 61 comment | 21 complexity | e7f2f09c241b8170e0a6608c04dbf38e MD5 | raw file
  1. <?php
  2. use MediaWiki\Auth\AuthManager;
  3. class ConfirmEditHooks {
  4. protected static $instanceCreated = false;
  5. /**
  6. * Get the global Captcha instance
  7. *
  8. * @return SimpleCaptcha
  9. */
  10. public static function getInstance() {
  11. global $wgCaptcha, $wgCaptchaClass;
  12. if ( !static::$instanceCreated ) {
  13. static::$instanceCreated = true;
  14. $wgCaptcha = new $wgCaptchaClass;
  15. }
  16. return $wgCaptcha;
  17. }
  18. /**
  19. * Registers conditional hooks.
  20. */
  21. public static function onRegistration() {
  22. global $wgDisableAuthManager, $wgAuthManagerAutoConfig;
  23. if ( class_exists( AuthManager::class ) && !$wgDisableAuthManager ) {
  24. $wgAuthManagerAutoConfig['preauth'][CaptchaPreAuthenticationProvider::class] = [
  25. 'class' => CaptchaPreAuthenticationProvider::class,
  26. 'sort'=> 10, // run after preauth providers not requiring user input
  27. ];
  28. Hooks::register( 'AuthChangeFormFields', 'ConfirmEditHooks::onAuthChangeFormFields' );
  29. } else {
  30. Hooks::register( 'UserCreateForm', 'ConfirmEditHooks::injectUserCreate' );
  31. Hooks::register( 'AbortNewAccount', 'ConfirmEditHooks::confirmUserCreate' );
  32. Hooks::register( 'LoginAuthenticateAudit', 'ConfirmEditHooks::triggerUserLogin' );
  33. Hooks::register( 'UserLoginForm', 'ConfirmEditHooks::injectUserLogin' );
  34. Hooks::register( 'AbortLogin', 'ConfirmEditHooks::confirmUserLogin' );
  35. Hooks::register( 'AddNewAccountApiForm', 'ConfirmEditHooks::addNewAccountApiForm' );
  36. Hooks::register( 'AddNewAccountApiResult', 'ConfirmEditHooks::addNewAccountApiResult' );
  37. }
  38. }
  39. static function confirmEditMerged( $context, $content, $status, $summary, $user, $minorEdit ) {
  40. return self::getInstance()->confirmEditMerged( $context, $content, $status, $summary,
  41. $user, $minorEdit );
  42. }
  43. /**
  44. * PageContentSaveComplete hook handler.
  45. * Clear IP whitelist cache on page saves for [[MediaWiki:captcha-ip-whitelist]].
  46. *
  47. * @param Page $wikiPage
  48. * @param User $user
  49. * @param Content $content
  50. * @param string $summary
  51. * @param bool $isMinor
  52. * @param bool $isWatch
  53. * @param string $section
  54. * @param int $flags
  55. * @param int $revision
  56. * @param Status $status
  57. * @param int $baseRevId
  58. *
  59. * @return bool true
  60. */
  61. static function onPageContentSaveComplete( Page $wikiPage, User $user, Content $content, $summary,
  62. $isMinor, $isWatch, $section, $flags, $revision, Status $status, $baseRevId
  63. ) {
  64. $title = $wikiPage->getTitle();
  65. if ( $title->getText() === 'Captcha-ip-whitelist' && $title->getNamespace() === NS_MEDIAWIKI ) {
  66. $cache = ObjectCache::getMainWANInstance();
  67. $cache->delete( $cache->makeKey( 'confirmedit', 'ipwhitelist' ) );
  68. }
  69. return true;
  70. }
  71. static function confirmEditPage( $editpage, $buttons, $tabindex ) {
  72. self::getInstance()->editShowCaptcha( $editpage );
  73. }
  74. static function confirmEditAPI( $editPage, $newtext, &$resultArr ) {
  75. return self::getInstance()->confirmEditAPI( $editPage, $newtext, $resultArr );
  76. }
  77. static function showEditFormFields( &$editPage, &$out ) {
  78. return self::getInstance()->showEditFormFields( $editPage, $out );
  79. }
  80. static function addNewAccountApiForm( $apiModule, $loginForm ) {
  81. return self::getInstance()->addNewAccountApiForm( $apiModule, $loginForm );
  82. }
  83. static function addNewAccountApiResult( $apiModule, $loginPage, &$result ) {
  84. return self::getInstance()->addNewAccountApiResult( $apiModule, $loginPage, $result );
  85. }
  86. static function injectUserCreate( &$template ) {
  87. return self::getInstance()->injectUserCreate( $template );
  88. }
  89. static function confirmUserCreate( $u, &$message, &$status = null ) {
  90. return self::getInstance()->confirmUserCreate( $u, $message, $status );
  91. }
  92. static function triggerUserLogin( $user, $password, $retval ) {
  93. return self::getInstance()->triggerUserLogin( $user, $password, $retval );
  94. }
  95. static function injectUserLogin( &$template ) {
  96. return self::getInstance()->injectUserLogin( $template );
  97. }
  98. static function confirmUserLogin( $u, $pass, &$retval ) {
  99. return self::getInstance()->confirmUserLogin( $u, $pass, $retval );
  100. }
  101. static function injectEmailUser( &$form ) {
  102. return self::getInstance()->injectEmailUser( $form );
  103. }
  104. static function confirmEmailUser( $from, $to, $subject, $text, &$error ) {
  105. return self::getInstance()->confirmEmailUser( $from, $to, $subject, $text, $error );
  106. }
  107. // Default $flags to 1 for backwards-compatible behavior
  108. public static function APIGetAllowedParams( &$module, &$params, $flags = 1 ) {
  109. return self::getInstance()->APIGetAllowedParams( $module, $params, $flags );
  110. }
  111. public static function APIGetParamDescription( &$module, &$desc ) {
  112. return self::getInstance()->APIGetParamDescription( $module, $desc );
  113. }
  114. public static function onAuthChangeFormFields(
  115. array $requests, array $fieldInfo, array &$formDescriptor, $action
  116. ) {
  117. self::getInstance()->onAuthChangeFormFields( $requests, $fieldInfo, $formDescriptor, $action );
  118. }
  119. /**
  120. * Hook to add PHPUnit test cases.
  121. * @see https://www.mediawiki.org/wiki/Manual:Hooks/UnitTestsList
  122. *
  123. * @param array &$files
  124. * @return boolean
  125. */
  126. public static function onUnitTestsList( array &$files ) {
  127. // @codeCoverageIgnoreStart
  128. $directoryIterator = new RecursiveDirectoryIterator( dirname( __DIR__ ) . '/tests/' );
  129. /**
  130. * @var SplFileInfo $fileInfo
  131. */
  132. $ourFiles = [];
  133. foreach ( new RecursiveIteratorIterator( $directoryIterator ) as $fileInfo ) {
  134. if ( substr( $fileInfo->getFilename(), -8 ) === 'Test.php' ) {
  135. $ourFiles[] = $fileInfo->getPathname();
  136. }
  137. }
  138. $files = array_merge( $files, $ourFiles );
  139. return true;
  140. // @codeCoverageIgnoreEnd
  141. }
  142. /**
  143. * Set up $wgWhitelistRead
  144. */
  145. public static function confirmEditSetup() {
  146. global $wgGroupPermissions, $wgCaptchaTriggers, $wgWikimediaJenkinsCI;
  147. // There is no need to run (core) tests with enabled ConfirmEdit - bug T44145
  148. if ( isset( $wgWikimediaJenkinsCI ) && $wgWikimediaJenkinsCI === true ) {
  149. $wgCaptchaTriggers = array_fill_keys( array_keys( $wgCaptchaTriggers ), false );
  150. }
  151. if ( !$wgGroupPermissions['*']['read'] && $wgCaptchaTriggers['badlogin'] ) {
  152. // We need to ensure that the captcha interface is accessible
  153. // so that unauthenticated users can actually get in after a
  154. // mistaken password typing.
  155. global $wgWhitelistRead;
  156. $image = SpecialPage::getTitleFor( 'Captcha', 'image' );
  157. $help = SpecialPage::getTitleFor( 'Captcha', 'help' );
  158. $wgWhitelistRead[] = $image->getPrefixedText();
  159. $wgWhitelistRead[] = $help->getPrefixedText();
  160. }
  161. }
  162. /**
  163. * Callback for extension.json of FancyCaptcha to set a default captcha directory,
  164. * which depends on wgUploadDirectory
  165. */
  166. public static function onFancyCaptchaSetup() {
  167. global $wgCaptchaDirectory, $wgUploadDirectory;
  168. if ( !$wgCaptchaDirectory ) {
  169. $wgCaptchaDirectory = "$wgUploadDirectory/captcha";
  170. }
  171. }
  172. /**
  173. * Callback for extension.json of ReCaptcha to require the recaptcha library php file.
  174. * FIXME: This should be done in a better way, e.g. only load the libraray, if really needed.
  175. */
  176. public static function onReCaptchaSetup() {
  177. require_once ( __DIR__ . '/../ReCaptcha/recaptchalib.php' );
  178. }
  179. /**
  180. * Extension function, moved from ReCaptcha.php when that was decimated.
  181. * Make sure the keys are defined.
  182. */
  183. public static function efReCaptcha() {
  184. global $wgReCaptchaPublicKey, $wgReCaptchaPrivateKey;
  185. // @codingStandardsIgnoreStart
  186. global $recaptcha_public_key, $recaptcha_private_key;
  187. // @codingStandardsIgnoreEnd
  188. global $wgServerName;
  189. // Backwards compatibility
  190. if ( $wgReCaptchaPublicKey == '' ) {
  191. $wgReCaptchaPublicKey = $recaptcha_public_key;
  192. }
  193. if ( $wgReCaptchaPrivateKey == '' ) {
  194. $wgReCaptchaPrivateKey = $recaptcha_private_key;
  195. }
  196. if ( $wgReCaptchaPublicKey == '' || $wgReCaptchaPrivateKey == '' ) {
  197. die (
  198. 'You need to set $wgReCaptchaPrivateKey and $wgReCaptchaPublicKey in LocalSettings.php to ' .
  199. "use the reCAPTCHA plugin. You can sign up for a key <a href='" .
  200. htmlentities( recaptcha_get_signup_url( $wgServerName, "mediawiki" ) ) . "'>here</a>." );
  201. }
  202. }
  203. }