| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace MediaWiki\Extension\AbuseFilter\Hooks\Handlers; |
| 5 |
|
| 6 |
use MediaWiki\Content\Hook\JsonValidateSaveHook; |
| 7 |
use MediaWiki\Content\JsonContent; |
| 8 |
use MediaWiki\Extension\AbuseFilter\BlockedDomainStorage; |
| 9 |
use MediaWiki\MediaWikiServices; |
| 10 |
use MediaWiki\Page\PageIdentity; |
| 11 |
use MediaWiki\Permissions\Hook\GetUserPermissionsErrorsHook; |
| 12 |
use MediaWiki\Title\Title; |
| 13 |
use MediaWiki\Title\TitleValue; |
| 14 |
use MediaWiki\User\User; |
| 15 |
use StatusValue; |
| 16 |
use Wikimedia\Message\MessageSpecifier; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class EditPermissionHandler implements GetUserPermissionsErrorsHook, JsonValidateSaveHook { |
| 23 |
|
| 24 |
|
| 25 |
private const JSON_OBJECT_FIELDS = [ |
| 26 |
'domain', |
| 27 |
'notes' |
| 28 |
]; |
| 29 |
|
| 30 |
private const JSON_OPTIONAL_FIELDS = [ 'addedBy' ]; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public function onGetUserPermissionsErrors( $title, $user, $action, &$result ) { |
| 42 |
$services = MediaWikiServices::getInstance(); |
| 43 |
|
| 44 |
|
| 45 |
if ( !$services->getMainConfig()->get( 'AbuseFilterEnableBlockedExternalDomain' ) ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
if ( |
| 52 |
!( $action == 'create' || $action == 'edit' ) || |
| 53 |
!$title->inNamespace( NS_MEDIAWIKI ) || |
| 54 |
$title->getDBkey() !== BlockedDomainStorage::TARGET_PAGE |
| 55 |
) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
if ( $services->getPermissionManager()->userHasRight( $user, 'editinterface' ) ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
$result = [ 'abusefilter-blocked-domains-cannot-edit-directly', BlockedDomainStorage::TARGET_PAGE ]; |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public function onJsonValidateSave( JsonContent $content, PageIdentity $pageIdentity, StatusValue $status ) { |
| 75 |
$services = MediaWikiServices::getInstance(); |
| 76 |
|
| 77 |
|
| 78 |
if ( !$services->getMainConfig()->get( 'AbuseFilterEnableBlockedExternalDomain' ) ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$title = TitleValue::newFromPage( $pageIdentity ); |
| 83 |
if ( !$title->inNamespace( NS_MEDIAWIKI ) || $title->getText() !== BlockedDomainStorage::TARGET_PAGE ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
$data = $content->getData()->getValue(); |
| 87 |
|
| 88 |
if ( !is_array( $data ) ) { |
| 89 |
$status->fatal( 'abusefilter-blocked-domains-json-error' ); |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
$isValid = true; |
| 94 |
$entryNumber = 0; |
| 95 |
foreach ( $data as $element ) { |
| 96 |
$entryNumber++; |
| 97 |
|
| 98 |
if ( is_object( $element ) && count( get_object_vars( $element ) ) >= count( self::JSON_OBJECT_FIELDS ) ) { |
| 99 |
foreach ( self::JSON_OBJECT_FIELDS as $field ) { |
| 100 |
if ( !property_exists( $element, $field ) || !is_string( $element->{$field} ) ) { |
| 101 |
$isValid = false; |
| 102 |
break 2; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
foreach ( self::JSON_OPTIONAL_FIELDS as $field ) { |
| 107 |
if ( property_exists( $element, $field ) && !is_string( $element->{$field} ) ) { |
| 108 |
$isValid = false; |
| 109 |
break 2; |
| 110 |
} |
| 111 |
} |
| 112 |
foreach ( $element as $field => $value ) { |
| 113 |
if ( |
| 114 |
!in_array( $field, array_merge( self::JSON_OPTIONAL_FIELDS, self::JSON_OBJECT_FIELDS ) ) |
| 115 |
) { |
| 116 |
$isValid = false; |
| 117 |
break; |
| 118 |
} |
| 119 |
} |
| 120 |
} else { |
| 121 |
$isValid = false; |
| 122 |
break; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
if ( !$isValid ) { |
| 127 |
$status->fatal( 'abusefilter-blocked-domains-invalid-entry', $entryNumber ); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
} |