| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
namespace MediaWiki\Extension\ConfirmEdit; |
| 6 |
|
| 7 |
use MailAddress; |
| 8 |
use MediaWiki\Api\ApiBase; |
| 9 |
use MediaWiki\Api\Hook\APIGetAllowedParamsHook; |
| 10 |
use MediaWiki\Content\Content; |
| 11 |
use MediaWiki\Context\IContextSource; |
| 12 |
use MediaWiki\EditPage\EditPage; |
| 13 |
use MediaWiki\Extension\ConfirmEdit\FancyCaptcha\FancyCaptcha; |
| 14 |
use MediaWiki\Extension\ConfirmEdit\hCaptcha\HCaptcha; |
| 15 |
use MediaWiki\Extension\ConfirmEdit\QuestyCaptcha\QuestyCaptcha; |
| 16 |
use MediaWiki\Extension\ConfirmEdit\ReCaptchaNoCaptcha\ReCaptchaNoCaptcha; |
| 17 |
use MediaWiki\Extension\ConfirmEdit\SimpleCaptcha\SimpleCaptcha; |
| 18 |
use MediaWiki\Extension\ConfirmEdit\Turnstile\Turnstile; |
| 19 |
use MediaWiki\Hook\AlternateEditPreviewHook; |
| 20 |
use MediaWiki\Hook\EditFilterMergedContentHook; |
| 21 |
use MediaWiki\Hook\EditPage__showEditForm_fieldsHook; |
| 22 |
use MediaWiki\Hook\EditPageBeforeEditButtonsHook; |
| 23 |
use MediaWiki\Hook\EmailUserFormHook; |
| 24 |
use MediaWiki\Hook\EmailUserHook; |
| 25 |
use MediaWiki\Html\Html; |
| 26 |
use MediaWiki\HTMLForm\HTMLForm; |
| 27 |
use MediaWiki\Output\OutputPage; |
| 28 |
use MediaWiki\Parser\ParserOutput; |
| 29 |
use MediaWiki\Permissions\Hook\TitleReadWhitelistHook; |
| 30 |
use MediaWiki\Registration\ExtensionRegistry; |
| 31 |
use MediaWiki\ResourceLoader\Hook\ResourceLoaderRegisterModulesHook; |
| 32 |
use MediaWiki\ResourceLoader\ResourceLoader; |
| 33 |
use MediaWiki\Revision\RevisionRecord; |
| 34 |
use MediaWiki\SpecialPage\Hook\AuthChangeFormFieldsHook; |
| 35 |
use MediaWiki\SpecialPage\SpecialPage; |
| 36 |
use MediaWiki\Status\Status; |
| 37 |
use MediaWiki\Storage\EditResult; |
| 38 |
use MediaWiki\Storage\Hook\PageSaveCompleteHook; |
| 39 |
use MediaWiki\Title\Title; |
| 40 |
use MediaWiki\User\User; |
| 41 |
use MediaWiki\User\UserIdentity; |
| 42 |
use Wikimedia\IPUtils; |
| 43 |
use Wikimedia\Message\MessageSpecifier; |
| 44 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 45 |
use WikiPage; |
| 46 |
|
| 47 |
class Hooks implements |
| 48 |
AlternateEditPreviewHook, |
| 49 |
EditPageBeforeEditButtonsHook, |
| 50 |
EmailUserFormHook, |
| 51 |
EmailUserHook, |
| 52 |
TitleReadWhitelistHook, |
| 53 |
ResourceLoaderRegisterModulesHook, |
| 54 |
PageSaveCompleteHook, |
| 55 |
EditPage__showEditForm_fieldsHook, |
| 56 |
EditFilterMergedContentHook, |
| 57 |
APIGetAllowedParamsHook, |
| 58 |
AuthChangeFormFieldsHook |
| 59 |
{ |
| 60 |
|
| 61 |
protected static ?SimpleCaptcha $instance = null; |
| 62 |
|
| 63 |
private WANObjectCache $cache; |
| 64 |
|
| 65 |
public function __construct( |
| 66 |
WANObjectCache $cache |
| 67 |
) { |
| 68 |
$this->cache = $cache; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public static function getInstance() { |
| 77 |
global $wgCaptchaClass; |
| 78 |
static $map = [ |
| 79 |
'SimpleCaptcha' => SimpleCaptcha::class, |
| 80 |
'FancyCaptcha' => FancyCaptcha::class, |
| 81 |
'QuestyCaptcha' => QuestyCaptcha::class, |
| 82 |
'ReCaptchaNoCaptcha' => ReCaptchaNoCaptcha::class, |
| 83 |
'HCaptcha' => HCaptcha::class, |
| 84 |
'Turnstile' => Turnstile::class, |
| 85 |
]; |
| 86 |
|
| 87 |
$className = $map[$wgCaptchaClass] ?? $wgCaptchaClass; |
| 88 |
|
| 89 |
static::$instance ??= new $className; |
| 90 |
return static::$instance; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function onEditFilterMergedContent( IContextSource $context, Content $content, Status $status, |
| 103 |
$summary, User $user, $minorEdit |
| 104 |
) { |
| 105 |
$simpleCaptcha = self::getInstance( CaptchaTriggers::EDIT ); |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
$simpleCaptcha->setEditFilterMergedContentHandlerInvoked(); |
| 110 |
return $simpleCaptcha->confirmEditMerged( $context, $content, $status, $summary, |
| 111 |
$user, $minorEdit ); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function onPageSaveComplete( |
| 126 |
$wikiPage, |
| 127 |
$user, |
| 128 |
$summary, |
| 129 |
$flags, |
| 130 |
$revisionRecord, |
| 131 |
$editResult |
| 132 |
) { |
| 133 |
$title = $wikiPage->getTitle(); |
| 134 |
if ( $title->getText() === 'Captcha-ip-whitelist' && $title->getNamespace() === NS_MEDIAWIKI ) { |
| 135 |
$this->cache->delete( $this->cache->makeKey( 'confirmedit', 'ipbypasslist' ) ); |
| 136 |
} |
| 137 |
|
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public function onEditPageBeforeEditButtons( $editpage, &$buttons, &$tabindex ) { |
| 149 |
self::getInstance()->editShowCaptcha( $editpage ); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public function onEditPage__showEditForm_fields( $editPage, $out ) { |
| 157 |
self::getInstance()->showEditFormFields( $editPage, $out ); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public function onEmailUserForm( &$form ) { |
| 167 |
return self::getInstance()->injectEmailUser( $form ); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public function onEmailUser( &$to, &$from, &$subject, &$text, &$error ) { |
| 182 |
return self::getInstance()->confirmEmailUser( $from, $to, $subject, $text, $error ); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public function onAPIGetAllowedParams( $module, &$params, $flags ) { |
| 194 |
return self::getInstance()->apiGetAllowedParams( $module, $params, $flags ); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public function onAuthChangeFormFields( |
| 204 |
$requests, $fieldInfo, &$formDescriptor, $action |
| 205 |
) { |
| 206 |
self::getInstance()->onAuthChangeFormFields( $requests, $fieldInfo, $formDescriptor, $action ); |
| 207 |
} |
| 208 |
|
| 209 |
public static function confirmEditSetup() { |
| 210 |
global $wgCaptchaTriggers; |
| 211 |
|
| 212 |
|
| 213 |
if ( defined( 'MW_PHPUNIT_TEST' ) || defined( 'MW_QUIBBLE_CI' ) ) { |
| 214 |
$wgCaptchaTriggers = array_fill_keys( array_keys( $wgCaptchaTriggers ), false ); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public function onTitleReadWhitelist( $title, $user, &$whitelisted ) { |
| 227 |
$image = SpecialPage::getTitleFor( 'Captcha', 'image' ); |
| 228 |
$help = SpecialPage::getTitleFor( 'Captcha', 'help' ); |
| 229 |
if ( $title->equals( $image ) || $title->equals( $help ) ) { |
| 230 |
$whitelisted = true; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public static function onFancyCaptchaSetup() { |
| 240 |
global $wgCaptchaDirectory, $wgUploadDirectory; |
| 241 |
if ( !$wgCaptchaDirectory ) { |
| 242 |
$wgCaptchaDirectory = "$wgUploadDirectory/captcha"; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public function onAlternateEditPreview( $editPage, &$content, &$previewHTML, |
| 256 |
&$parserOutput |
| 257 |
) { |
| 258 |
$title = $editPage->getTitle(); |
| 259 |
$exceptionTitle = Title::makeTitle( NS_MEDIAWIKI, 'Captcha-ip-whitelist' ); |
| 260 |
|
| 261 |
if ( !$title->equals( $exceptionTitle ) ) { |
| 262 |
return true; |
| 263 |
} |
| 264 |
|
| 265 |
$ctx = $editPage->getArticle()->getContext(); |
| 266 |
$out = $ctx->getOutput(); |
| 267 |
$lang = $ctx->getLanguage(); |
| 268 |
|
| 269 |
$lines = explode( "\n", $content->getNativeData() ); |
| 270 |
$previewHTML .= Html::warningBox( |
| 271 |
$ctx->msg( 'confirmedit-preview-description' )->parse() |
| 272 |
) . |
| 273 |
Html::openElement( |
| 274 |
'table', |
| 275 |
[ 'class' => 'wikitable sortable' ] |
| 276 |
) . |
| 277 |
Html::openElement( 'thead' ) . |
| 278 |
Html::element( 'th', [], $ctx->msg( 'confirmedit-preview-line' )->text() ) . |
| 279 |
Html::element( 'th', [], $ctx->msg( 'confirmedit-preview-content' )->text() ) . |
| 280 |
Html::element( 'th', [], $ctx->msg( 'confirmedit-preview-validity' )->text() ) . |
| 281 |
Html::closeElement( 'thead' ); |
| 282 |
|
| 283 |
foreach ( $lines as $count => $line ) { |
| 284 |
$ip = trim( $line ); |
| 285 |
if ( $ip === '' || strpos( $ip, '#' ) !== false ) { |
| 286 |
continue; |
| 287 |
} |
| 288 |
if ( IPUtils::isIPAddress( $ip ) ) { |
| 289 |
$validity = $ctx->msg( 'confirmedit-preview-valid' )->escaped(); |
| 290 |
$css = 'valid'; |
| 291 |
} else { |
| 292 |
$validity = $ctx->msg( 'confirmedit-preview-invalid' )->escaped(); |
| 293 |
$css = 'notvalid'; |
| 294 |
} |
| 295 |
$previewHTML .= Html::openElement( 'tr' ) . |
| 296 |
Html::element( |
| 297 |
'td', |
| 298 |
[], |
| 299 |
$lang->formatNum( $count + 1 ) |
| 300 |
) . |
| 301 |
Html::element( |
| 302 |
'td', |
| 303 |
[], |
| 304 |
|
| 305 |
|
| 306 |
$lang->truncateForVisual( $ip, 50 ) |
| 307 |
) . |
| 308 |
Html::rawElement( |
| 309 |
'td', |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
[ 'class' => 'mw-confirmedit-ip-' . $css ], |
| 314 |
$validity |
| 315 |
) . |
| 316 |
Html::closeElement( 'tr' ); |
| 317 |
} |
| 318 |
$previewHTML .= Html::closeElement( 'table' ); |
| 319 |
$out->addModuleStyles( 'ext.confirmEdit.editPreview.ipwhitelist.styles' ); |
| 320 |
|
| 321 |
return false; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public function onResourceLoaderRegisterModules( ResourceLoader $resourceLoader ): void { |
| 331 |
$extensionRegistry = ExtensionRegistry::getInstance(); |
| 332 |
$messages = []; |
| 333 |
|
| 334 |
$messages[] = 'colon-separator'; |
| 335 |
$messages[] = 'captcha-edit'; |
| 336 |
$messages[] = 'captcha-label'; |
| 337 |
|
| 338 |
if ( $extensionRegistry->isLoaded( 'QuestyCaptcha' ) ) { |
| 339 |
$messages[] = 'questycaptcha-edit'; |
| 340 |
} |
| 341 |
|
| 342 |
if ( $extensionRegistry->isLoaded( 'FancyCaptcha' ) ) { |
| 343 |
$messages[] = 'fancycaptcha-edit'; |
| 344 |
$messages[] = 'fancycaptcha-reload-text'; |
| 345 |
$messages[] = 'fancycaptcha-imgcaptcha-ph'; |
| 346 |
} |
| 347 |
|
| 348 |
$resourceLoader->register( [ |
| 349 |
'ext.confirmEdit.CaptchaInputWidget' => [ |
| 350 |
'localBasePath' => dirname( __DIR__ ), |
| 351 |
'remoteExtPath' => 'ConfirmEdit', |
| 352 |
'scripts' => 'resources/libs/ext.confirmEdit.CaptchaInputWidget.js', |
| 353 |
'styles' => 'resources/libs/ext.confirmEdit.CaptchaInputWidget.less', |
| 354 |
'messages' => $messages, |
| 355 |
'dependencies' => 'oojs-ui-core', |
| 356 |
] |
| 357 |
] ); |
| 358 |
} |
| 359 |
|
| 360 |
} |
| 361 |
|
| 362 |
class_alias( Hooks::class, 'ConfirmEditHooks' ); |