Repository navigation
Expand file tree
/
Copy pathwp-malware-scanner.php
More file actions
269 lines (229 loc) · 9.15 KB
/
Copy pathwp-malware-scanner.php
File metadata and controls
269 lines (229 loc) · 9.15 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
/**
* Plugin Name: WP Malware Scanner Gateway
* Description: Provides Gate API connectivity, scan result webhooks, and AliExpress product widgets.
* Version: 1.0.0
* Author: Quantum Engineering Agent
*/
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *'); // Allow CORS if needed for API calls
function extractEmails($url) {
// Fetch the webpage content
$content = @file_get_contents($url);
if ($content === false) {
return ['error' => 'Unable to fetch content from URL'];
}
// Regex pattern for emails (covers most common formats)
$pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?/i';
preg_match_all($pattern, $content, $matches);
// Remove duplicates and return unique emails
$emails = array_unique($matches[0]);
return ['emails' => $emails, 'count' => count($emails)];
}
// Handle API request
if (isset($_GET['url'])) {
$url = filter_var($_GET['url'], FILTER_VALIDATE_URL);
if ($url) {
echo json_encode(extractEmails($url));
} else {
echo json_encode(['error' => 'Invalid URL provided']);
}
} else {
echo json_encode(['error' => 'No URL provided. Use ?url=https://example.com']);
}
if (!defined('ABSPATH')) {
exit;
}
if (!defined('KVN_GATE_PLUGIN_VER')) {
define('KVN_GATE_PLUGIN_VER', '1.0.0');
}
add_action('admin_init', 'kvn_gate_register_settings');
add_action('admin_menu', 'kvn_gate_register_menu');
add_shortcode('kvn_products', 'kvn_gate_shortcode_products');
function kvn_gate_register_settings(): void
{
register_setting('kvn_gate', 'kvn_gate_api_key', [
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
]);
register_setting('kvn_gate', 'kvn_gate_secret', [
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
]);
register_setting('kvn_gate', 'kvn_gate_url', [
'type' => 'string',
'sanitize_callback' => 'esc_url_raw',
]);
}
function kvn_gate_register_menu(): void
{
add_options_page(
__('KVN Gate Settings', 'kvn-gate'),
__('KVN Gate', 'kvn-gate'),
'manage_options',
'kvn-gate',
'kvn_gate_render_settings_page'
);
}
function kvn_gate_render_settings_page(): void
{
if (!current_user_can('manage_options')) {
return;
}
?>
<div class="wrap">
<h1><?php esc_html_e('KVN Gate Settings', 'kvn-gate'); ?></h1>
<form method="post" action="options.php">
<?php
settings_fields('kvn_gate');
do_settings_sections('kvn_gate');
?>
<table class="form-table" role="presentation">
<tr>
<th scope="row"><label for="kvn_gate_api_key"><?php esc_html_e('API Key', 'kvn-gate'); ?></label></th>
<td><input name="kvn_gate_api_key" id="kvn_gate_api_key" type="text" class="regular-text" value="<?php echo esc_attr(get_option('kvn_gate_api_key', '')); ?>" autocomplete="off"></td>
</tr>
<tr>
<th scope="row"><label for="kvn_gate_secret"><?php esc_html_e('Secret', 'kvn-gate'); ?></label></th>
<td><input name="kvn_gate_secret" id="kvn_gate_secret" type="password" class="regular-text" value="<?php echo esc_attr(get_option('kvn_gate_secret', '')); ?>" autocomplete="off"></td>
</tr>
<tr>
<th scope="row"><label for="kvn_gate_url"><?php esc_html_e('Gate URL', 'kvn-gate'); ?></label></th>
<td><input name="kvn_gate_url" id="kvn_gate_url" type="url" class="regular-text" value="<?php echo esc_attr(get_option('kvn_gate_url', '')); ?>" placeholder="https://example.com"></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
function kvn_gate_sign(string $method, string $path, string $secret, string $body = ''): array
{
$body_hash = hash('sha256', $body);
$timestamp = time();
$data = strtoupper($method) . '|' . $path . '|' . $timestamp . '|' . $body_hash;
$signature = hash_hmac('sha256', $data, $secret);
return [$timestamp, $signature];
}
function kvn_gate_send_scan_report(string $site_url, array $results, string $status = 'unknown')
{
$api_key = get_option('kvn_gate_api_key');
$secret = get_option('kvn_gate_secret');
$gate_url = rtrim((string) get_option('kvn_gate_url'), '/');
if (empty($api_key) || empty($secret) || empty($gate_url)) {
return new WP_Error('kvn_gate_missing', __('Gate credentials are not configured.', 'kvn-gate'));
}
$path = '/api/v1/wp/scan';
$sanitized_results = kvn_gate_sanitize_results($results);
$payload = wp_json_encode([
'site_url' => esc_url_raw($site_url),
'plugin_version' => KVN_GATE_PLUGIN_VER,
'results' => $sanitized_results,
'status' => sanitize_text_field($status),
], JSON_UNESCAPED_SLASHES);
[$timestamp, $signature] = kvn_gate_sign('POST', $path, $secret, $payload);
$response = wp_remote_post($gate_url . $path, [
'headers' => [
'Content-Type' => 'application/json',
'X-Api-Key' => $api_key,
'X-Timestamp' => $timestamp,
'X-Signature' => $signature,
],
'body' => $payload,
'timeout' => 10,
]);
return $response;
}
function kvn_gate_fetch_products(string $query)
{
$api_key = get_option('kvn_gate_api_key');
$secret = get_option('kvn_gate_secret');
$gate_url = rtrim((string) get_option('kvn_gate_url'), '/');
if (empty($api_key) || empty($secret) || empty($gate_url)) {
return new WP_Error('kvn_gate_missing', __('Gate credentials are not configured.', 'kvn-gate'));
}
$path = '/api/v1/aliexpress/search';
$query_string = '?q=' . rawurlencode($query);
[$timestamp, $signature] = kvn_gate_sign('GET', $path, $secret, '');
$response = wp_remote_get($gate_url . $path . $query_string, [
'headers' => [
'X-Api-Key' => $api_key,
'X-Timestamp' => $timestamp,
'X-Signature' => $signature,
],
'timeout' => 10,
]);
return $response;
}
function kvn_gate_sanitize_results(array $results): array
{
array_walk_recursive($results, static function (&$value): void {
if (is_string($value)) {
$value = sanitize_textarea_field($value);
}
});
return $results;
}
function kvn_gate_shortcode_products(array $atts): string
{
$atts = shortcode_atts([
'q' => 'projector',
'limit' => 6,
], $atts, 'kvn_products');
$response = kvn_gate_fetch_products(sanitize_text_field($atts['q']));
if (is_wp_error($response)) {
return '<em>' . esc_html($response->get_error_message()) . '</em>';
}
$code = (int) wp_remote_retrieve_response_code($response);
if ($code !== 200) {
return '<em>' . esc_html__('Unable to fetch products.', 'kvn-gate') . '</em>';
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (!is_array($data)) {
return '<em>' . esc_html__('Invalid response payload.', 'kvn-gate') . '</em>';
}
$items = [];
if (isset($data['items']) && is_array($data['items'])) {
$items = $data['items'];
} elseif (isset($data[0])) {
$items = $data;
}
$limit = max(1, (int) $atts['limit']);
$items = array_slice($items, 0, $limit);
if (empty($items)) {
return '<em>' . esc_html__('No products available.', 'kvn-gate') . '</em>';
}
ob_start();
?>
<div class="kvn-products" style="display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:12px;">
<?php foreach ($items as $item) :
$title = isset($item['title']) ? esc_html($item['title']) : '';
$image = isset($item['image']) ? esc_url($item['image']) : '';
$price_now = isset($item['price_now']) ? esc_html($item['price_now']) : '';
$price_orig = isset($item['price_orig']) ? esc_html($item['price_orig']) : '';
$link = isset($item['affiliate_link']) ? esc_url($item['affiliate_link']) : '#';
?>
<div style="border:1px solid #eee;border-radius:8px;padding:10px;">
<a href="<?php echo $link; ?>" target="_blank" rel="nofollow noopener">
<?php if (!empty($image)) : ?>
<img src="<?php echo $image; ?>" alt="<?php echo $title; ?>" style="width:100%;height:auto;border-radius:6px;" />
<?php endif; ?>
</a>
<div style="margin-top:6px;">
<a href="<?php echo $link; ?>" target="_blank" rel="nofollow noopener">
<strong><?php echo $title; ?></strong>
</a>
</div>
<div>
<strong><?php echo $price_now; ?></strong>
<?php if (!empty($price_orig)) : ?>
<span style="text-decoration:line-through;color:#888;"><?php echo $price_orig; ?></span>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php
return (string) ob_get_clean();
}