wfRESTScanController.php
5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
require_once(dirname(__FILE__) . '/wfRESTBaseController.php');
class wfRESTScanController extends wfRESTBaseController {
/**
* @todo Setup routes to modify scan results.
*/
public function registerRoutes() {
register_rest_route('wordfence/v1', '/scan/issues', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'getIssuesList'),
'permission_callback' => array($this, 'verifyToken'),
'group' => array(
'description' => __('Scan result group or all results.', 'wordfence'),
'type' => 'string',
'required' => false,
),
'offset' => array(
'description' => __('Offset of scan results to return.', 'wordfence'),
'type' => 'int',
'required' => false,
),
'limit' => array(
'description' => __('Number of scan results to return.', 'wordfence'),
'type' => 'int',
'required' => false,
),
));
register_rest_route('wordfence/v1', '/scan', array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'startScan'),
'permission_callback' => array($this, 'verifyToken'),
));
register_rest_route('wordfence/v1', '/scan', array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array($this, 'stopScan'),
'permission_callback' => array($this, 'verifyToken'),
));
register_rest_route('wordfence/v1', '/scan/issue', array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array($this, 'updateIssue'),
'permission_callback' => array($this, 'verifyToken'),
));
}
/**
* @param WP_REST_Request $request
* @return mixed|WP_REST_Response
*/
public function getIssuesList($request) {
$group = $request['group'] ? $request['group'] : 'all';
$offset = absint($request['offset']);
$limit = absint($request['limit']);
if ($limit === 0) {
$limit = 100;
}
switch ($group) {
case 'pending':
$count = wfIssues::shared()->getPendingIssueCount();
$issues = wfIssues::shared()->getPendingIssues($offset, $limit);
break;
default: // Return all issues.
$count = wfIssues::shared()->getIssueCount();
$issues = wfIssues::shared()->getIssues($offset, $limit);
break;
}
$response = rest_ensure_response(array(
'count' => $count,
'last-scan-time' => wfConfig::get('scanTime'),
'issues' => $issues,
));
return $response;
}
/**
* @param WP_REST_Request $request
* @return mixed|WP_REST_Response
*/
public function startScan($request) {
wordfence::status(1, 'info', sprintf(/* translators: Localized date. */ __('Wordfence scan starting at %s from Wordfence Central', 'wordfence'),
date('l jS \of F Y h:i:s A', current_time('timestamp'))));
try {
wfScanEngine::startScan();
} catch (wfScanEngineTestCallbackFailedException $e) {
wfConfig::set('lastScanCompleted', $e->getMessage());
wfConfig::set('lastScanFailureType', wfIssues::SCAN_FAILED_CALLBACK_TEST_FAILED);
wfUtils::clearScanLock();
$response = rest_ensure_response(array(
'success' => false,
'error-code' => $e->getCode(),
'error' => $e->getMessage(),
));
return $response;
} catch (Exception $e) {
if ($e->getCode() != wfScanEngine::SCAN_MANUALLY_KILLED) {
wfConfig::set('lastScanCompleted', $e->getMessage());
wfConfig::set('lastScanFailureType', wfIssues::SCAN_FAILED_GENERAL);
$response = rest_ensure_response(array(
'success' => false,
'error-code' => $e->getCode(),
'error' => $e->getMessage(),
));
return $response;
}
}
$response = rest_ensure_response(array(
'success' => true,
));
return $response;
}
/**
* @param WP_REST_Request $request
* @return mixed|WP_REST_Response
*/
public function stopScan($request) {
wordfence::status(1, 'info', __('Scan stop request received from Wordfence Central.', 'wordfence'));
wordfence::status(10, 'info', __('SUM_KILLED:A request was received to stop the previous scan from Wordfence Central.', 'wordfence'));
wfUtils::clearScanLock(); //Clear the lock now because there may not be a scan running to pick up the kill request and clear the lock
wfScanEngine::requestKill();
wfConfig::remove('scanStartAttempt');
wfConfig::set('lastScanFailureType', false);
$response = rest_ensure_response(array(
'success' => true,
));
return $response;
}
/**
* @param WP_REST_Request $request
* @return mixed|WP_REST_Response
*/
public function updateIssue($request) {
$issue = $request['issue'];
$id = is_array($issue) && array_key_exists('id', $issue) ? $issue['id'] : null;
$status = is_array($issue) && array_key_exists('status', $issue) ? $issue['status'] : null;
if ($id) {
$wfdb = new wfDB();
$wfdb->queryWrite("update " . wfDB::networkTable('wfIssues') . " set status='%s' where id=%d", $status, $id);
$response = rest_ensure_response(array(
'success' => true,
));
return $response;
}
$response = rest_ensure_response(array(
'success' => false,
'error' => 'Issue not found.',
));
return $response;
}
}