Debian Codegraph @

extensions/AbuseFilter/includes/Api/AbuseLogPrivateDetails.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 */
18
19 namespace MediaWiki\Extension\AbuseFilter\Api;
20
21 use MediaWiki\Api\ApiBase;
22 use MediaWiki\Api\ApiMain;
23 use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
24 use MediaWiki\Extension\AbuseFilter\Special\SpecialAbuseLog;
25 use Wikimedia\ParamValidator\ParamValidator;
26
27 /**
28 * API module to allow accessing private details (the user's IP) from AbuseLog entries
29 *
30 * @ingroup API
31 * @ingroup Extensions
32 */
33 class AbuseLogPrivateDetails extends ApiBase {
34
35 /** @var AbuseFilterPermissionManager */
36 private $afPermManager;
37
38 /**
39 * @param ApiMain $main
40 * @param string $action
41 * @param AbuseFilterPermissionManager $afPermManager
42 */
43 public function __construct(
44 ApiMain $main,
45 $action,
46 AbuseFilterPermissionManager $afPermManager
47 ) {
48 parent::__construct( $main, $action );
49 $this->afPermManager = $afPermManager;
50 }
51
52 /**
53 * @codeCoverageIgnore Merely declarative
54 * @inheritDoc
55 */
56 public function mustBePosted() {
57 return true;
58 }
59
60 /**
61 * @codeCoverageIgnore Merely declarative
62 * @inheritDoc
63 */
64 public function isWriteMode() {
65 return true;
66 }
67
68 /**
69 * @codeCoverageIgnore Merely declarative
70 * @inheritDoc
71 */
72 public function needsToken() {
73 return 'csrf';
74 }
75
76 /**
77 * @inheritDoc
78 */
79 public function execute() {
80 $user = $this->getUser();
81
82 if ( !$this->afPermManager->canSeePrivateDetails( $user ) ) {
83 $this->dieWithError( 'abusefilter-log-cannot-see-privatedetails' );
84 }
85 $params = $this->extractRequestParams();
86
87 if ( !SpecialAbuseLog::checkPrivateDetailsAccessReason( $params['reason'] ) ) {
88 // Double check, in case we add some extra validation
89 $this->dieWithError( 'abusefilter-noreason' );
90 }
91 $status = SpecialAbuseLog::getPrivateDetailsRow( $user, $params['logid'] );
92 if ( !$status->isGood() ) {
93 $this->dieStatus( $status );
94 }
95 $row = $status->getValue();
96 // Log accessing private details
97 if ( $this->getConfig()->get( 'AbuseFilterLogPrivateDetailsAccess' ) ) {
98 SpecialAbuseLog::addPrivateDetailsAccessLogEntry(
99 $params['logid'],
100 $params['reason'],
101 $user
102 );
103 }
104
105 $result = [
106 'log-id' => $params['logid'],
107 'user' => $row->afl_user_text,
108 'filter-id' => (int)$row->af_id,
109 'filter-description' => $row->af_public_comments,
110 'ip-address' => $row->afl_ip !== '' ? $row->afl_ip : null
111 ];
112 $this->getResult()->addValue( null, $this->getModuleName(), $result );
113 }
114
115 /**
116 * @codeCoverageIgnore Merely declarative
117 * @inheritDoc
118 */
119 public function getAllowedParams() {
120 return [
121 'logid' => [
122 ParamValidator::PARAM_TYPE => 'integer'
123 ],
124 'reason' => [
125 ParamValidator::PARAM_TYPE => 'string',
126 ParamValidator::PARAM_REQUIRED => $this->getConfig()->get( 'AbuseFilterPrivateDetailsForceReason' ),
127 ParamValidator::PARAM_DEFAULT => '',
128 ]
129 ];
130 }
131
132 /**
133 * @codeCoverageIgnore Merely declarative
134 * @inheritDoc
135 */
136 protected function getExamplesMessages() {
137 return [
138 'action=abuselogprivatedetails&logid=1&reason=example&token=ABC123'
139 => 'apihelp-abuselogprivatedetails-example-1'
140 ];
141 }
142 }