| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Description of SystemInfo |
| 5 |
* |
| 6 |
* @author Ali2Woo Team |
| 7 |
* |
| 8 |
* @autoload: a2wl_admin_init |
| 9 |
* |
| 10 |
* @ajax: true |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace AliNext_Lite;; |
| 14 |
|
| 15 |
class SystemInfo |
| 16 |
{ |
| 17 |
public function __construct() { |
| 18 |
add_action('wp_ajax_a2wl_ping', array($this, 'ajax_ping')); |
| 19 |
add_action('wp_ajax_nopriv_a2wl_ping', array($this, 'ajax_ping')); |
| 20 |
add_action('wp_ajax_a2wl_clear_log_file', array($this, 'ajax_clear_log_file')); |
| 21 |
add_action('wp_ajax_a2wl_clean_import_queue', [$this, 'ajax_clean_import_queue']); |
| 22 |
add_action('wp_ajax_a2wl_run_cron_import_queue', [$this, 'ajax_run_cron']); |
| 23 |
} |
| 24 |
|
| 25 |
public function ajax_clear_log_file() { |
| 26 |
Logs::getInstance()->delete(); |
| 27 |
echo wp_json_encode(array('state'=>'ok')); |
| 28 |
wp_die(); |
| 29 |
} |
| 30 |
|
| 31 |
public function ajax_clean_import_queue(): void |
| 32 |
{ |
| 33 |
$import_process = new ImportProcess(); |
| 34 |
$import_process->clean_queue(); |
| 35 |
|
| 36 |
echo wp_json_encode(['state'=>'ok']); |
| 37 |
wp_die(); |
| 38 |
} |
| 39 |
|
| 40 |
public function ajax_run_cron(): void |
| 41 |
{ |
| 42 |
$import_process = new ImportProcess(); |
| 43 |
$import_process->dispatch(); |
| 44 |
|
| 45 |
echo wp_json_encode(['state'=>'ok']); |
| 46 |
wp_die(); |
| 47 |
} |
| 48 |
|
| 49 |
public function ajax_ping() { |
| 50 |
echo wp_json_encode(array('state'=>'ok')); |
| 51 |
wp_die(); |
| 52 |
} |
| 53 |
|
| 54 |
public static function ping(): ?array |
| 55 |
{ |
| 56 |
// Keep cookies as they are required on some servers |
| 57 |
$args = [ |
| 58 |
'cookies' => $_COOKIE, |
| 59 |
]; |
| 60 |
|
| 61 |
$url = admin_url('admin-ajax.php') . '?action=a2wl_ping'; |
| 62 |
$request = wp_remote_post($url, $args); |
| 63 |
|
| 64 |
// WP-level error (DNS, SSL, timeout, etc.) |
| 65 |
if (is_wp_error($request)) { |
| 66 |
return ResultBuilder::buildError($request->get_error_message()); |
| 67 |
} |
| 68 |
|
| 69 |
$code = intval($request['response']['code']); |
| 70 |
$body = isset($request['body']) ? $request['body'] : ''; |
| 71 |
|
| 72 |
// Detect Cloudflare challenge page |
| 73 |
if ($code === 403 && self::isCloudflareChallenge($body)) { |
| 74 |
return ResultBuilder::buildError( |
| 75 |
'Cloudflare is blocking this AJAX request. ' |
| 76 |
. 'To fix this, please add a Firewall Rule in Cloudflare that allows POST ' |
| 77 |
. 'requests to /wp-admin/admin-ajax.php. ' |
| 78 |
. 'This is required for WordPress AJAX to work correctly.' |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
// Non-200 HTTP response |
| 83 |
if ($code !== 200) { |
| 84 |
return ResultBuilder::buildError( |
| 85 |
$code . ' ' . $request['response']['message'] |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
// Decode JSON response |
| 90 |
return json_decode($body, true); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Detects Cloudflare challenge pages by scanning the HTML body. |
| 95 |
* |
| 96 |
* @param string $body |
| 97 |
* @return bool |
| 98 |
*/ |
| 99 |
private static function isCloudflareChallenge(string $body): bool |
| 100 |
{ |
| 101 |
if ($body === '') { |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
// Common Cloudflare challenge markers |
| 106 |
$markers = [ |
| 107 |
'Just a moment...', |
| 108 |
'cf_chl_', |
| 109 |
'__cf_chl_', |
| 110 |
'/cdn-cgi/challenge-platform/', |
| 111 |
'Ray ID', |
| 112 |
'cloudflare' |
| 113 |
]; |
| 114 |
|
| 115 |
foreach ($markers as $marker) { |
| 116 |
if (stripos($body, $marker) !== false) { |
| 117 |
return true; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
public static function server_ping(): ?array |
| 125 |
{ |
| 126 |
$ping_url = RequestHelper::build_request('ping', ['r' => wp_rand()]); |
| 127 |
$request = a2wl_remote_get($ping_url); |
| 128 |
|
| 129 |
if (is_wp_error($request)) { |
| 130 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 131 |
if (file_get_contents($ping_url)) { |
| 132 |
$result = ResultBuilder::buildError('a2wl_remote_get error'); |
| 133 |
} else { |
| 134 |
$result = ResultBuilder::buildError($request->get_error_message()); |
| 135 |
} |
| 136 |
} else if (intval($request['response']['code']) != 200) { |
| 137 |
$result = ResultBuilder::buildError( |
| 138 |
$request['response']['code']." ".$request['response']['message'] |
| 139 |
); |
| 140 |
} else { |
| 141 |
$result = json_decode($request['body'], true); |
| 142 |
} |
| 143 |
|
| 144 |
return $result; |
| 145 |
} |
| 146 |
|
| 147 |
public static function hasMbstring(): bool |
| 148 |
{ |
| 149 |
return extension_loaded('mbstring') && function_exists('mb_convert_encoding'); |
| 150 |
} |
| 151 |
|
| 152 |
public static function php_check(): array |
| 153 |
{ |
| 154 |
if (PHP_VERSION_ID < 80000) { |
| 155 |
return ResultBuilder::buildError('PHP version must be 8.0 or higher.'); |
| 156 |
} |
| 157 |
|
| 158 |
if (!self::hasMbstring()) { |
| 159 |
return ResultBuilder::buildError('mbstring extension is not available. Please enable it.'); |
| 160 |
} |
| 161 |
|
| 162 |
return ResultBuilder::buildOk(); |
| 163 |
} |
| 164 |
|
| 165 |
public static function php_dom_check(){ |
| 166 |
if (class_exists('DOMDocument')) { |
| 167 |
return ResultBuilder::buildOk(); |
| 168 |
} else{ |
| 169 |
return ResultBuilder::buildError('PHP DOM is disabled'); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|