| 1 |
<?php |
| 2 |
/** |
| 3 |
* Docket Cache. |
| 4 |
* |
| 5 |
* @author Nawawi Jamili |
| 6 |
* @license MIT |
| 7 |
* |
| 8 |
* @see https://github.com/nawawi/docket-cache |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Nawawi\DocketCache; |
| 12 |
|
| 13 |
\defined('ABSPATH') || exit; |
| 14 |
|
| 15 |
class Bepart extends Filesystem |
| 16 |
{ |
| 17 |
/** |
| 18 |
* is_request_from_theme_editor. |
| 19 |
*/ |
| 20 |
public function is_request_from_theme_editor() |
| 21 |
{ |
| 22 |
if (!empty($_POST)) { |
| 23 |
if (!empty($_POST['_wp_http_referer'])) { |
| 24 |
$wp_referer = $_POST['_wp_http_referer']; |
| 25 |
if ((false !== strpos($wp_referer, '/theme-editor.php?file=') || false !== strpos($wp_referer, '/plugin-editor.php?file=')) && (!empty($_POST['newcontent']) && false !== strpos($_POST['newcontent'], '<?php'))) { |
| 26 |
return true; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
if (!empty($_POST['action']) && 'heartbeat' === $_POST['action'] && !empty($_POST['screen_id']) && ('theme-editor' === $_POST['screen_id'] || 'plugin-editor' === $_POST['screen_id'])) { |
| 31 |
return true; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
if (!empty($_GET) && !empty($_GET['wp_scrape_key']) && !empty($_GET['wp_scrape_nonce'])) { |
| 36 |
return true; |
| 37 |
} |
| 38 |
|
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* fastcgi_close. |
| 44 |
*/ |
| 45 |
public function fastcgi_close() |
| 46 |
{ |
| 47 |
if (\function_exists('fastcgi_finish_request') && !$this->is_request_from_theme_editor()) { |
| 48 |
return @fastcgi_finish_request(); |
| 49 |
} |
| 50 |
|
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* close_exit. |
| 56 |
*/ |
| 57 |
public function close_exit($msg = '') |
| 58 |
{ |
| 59 |
if (!empty($msg)) { |
| 60 |
echo $msg; |
| 61 |
} |
| 62 |
$this->fastcgi_close(); |
| 63 |
exit; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* json_header. |
| 68 |
*/ |
| 69 |
public function json_header() |
| 70 |
{ |
| 71 |
if (!headers_sent()) { |
| 72 |
@header('Cache-Control: no-cache, no-store, must-revalidate, max-age=0'); |
| 73 |
@header('Content-Type: application/json; charset=UTF-8'); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* send_json_continue. |
| 79 |
*/ |
| 80 |
public function send_json_continue($msg, $success = true) |
| 81 |
{ |
| 82 |
$this->json_header(); |
| 83 |
|
| 84 |
$response = ['success' => $success]; |
| 85 |
$response['data'] = $msg; |
| 86 |
echo wp_json_encode($response); |
| 87 |
$this->fastcgi_close(); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* plugin_meta. |
| 92 |
*/ |
| 93 |
public function plugin_meta($file) |
| 94 |
{ |
| 95 |
if (!@is_file($file)) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
static $cache = []; |
| 100 |
|
| 101 |
if (isset($cache[$file])) { |
| 102 |
return $cache[$file]; |
| 103 |
} |
| 104 |
|
| 105 |
$default_headers = [ |
| 106 |
'Name' => 'Plugin Name', |
| 107 |
'PluginURI' => 'Plugin URI', |
| 108 |
'Version' => 'Version', |
| 109 |
'VerPrev' => 'VerPrev', |
| 110 |
'Description' => 'Description', |
| 111 |
'Author' => 'Author', |
| 112 |
'AuthorURI' => 'Author URI', |
| 113 |
'TextDomain' => 'Text Domain', |
| 114 |
'DomainPath' => 'Domain Path', |
| 115 |
'Network' => 'Network', |
| 116 |
'RequiresWP' => 'Requires at least', |
| 117 |
'RequiresPHP' => 'Requires PHP', |
| 118 |
]; |
| 119 |
|
| 120 |
$cache[$file] = get_file_data($file, $default_headers); |
| 121 |
|
| 122 |
return $cache[$file]; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* code_worker. |
| 127 |
*/ |
| 128 |
public function code_worker($types = '') |
| 129 |
{ |
| 130 |
$types = (array) $types; |
| 131 |
if (empty($types)) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
$repeat_funcs = []; |
| 136 |
|
| 137 |
$code = '<script id="docket-cache-worker" data-noptimize="1">'.\PHP_EOL; |
| 138 |
$code .= 'if ( "undefined" !== typeof(jQuery) && "undefined" !== typeof(docket_cache_config) && "function" === typeof(docket_cache_worker) ) {'.\PHP_EOL; |
| 139 |
$code .= ' jQuery( document ).ready( function() {'.\PHP_EOL; |
| 140 |
$code .= ' var config = docket_cache_config;'.\PHP_EOL; |
| 141 |
foreach ($types as $type) { |
| 142 |
if (false !== strpos($type, 'repeat_')) { |
| 143 |
$repeat_funcs[] = str_replace('repeat_', '', $type); |
| 144 |
continue; |
| 145 |
} |
| 146 |
$code .= ' docket_cache_worker( "'.$type.'", config );'.\PHP_EOL; |
| 147 |
} |
| 148 |
|
| 149 |
if (!empty($repeat_funcs)) { |
| 150 |
foreach ($repeat_funcs as $func) { |
| 151 |
$code .= ' if ( location.href.match(/admin\.php\?page=docket\-cache/) ) {'.\PHP_EOL; |
| 152 |
$code .= ' docket_cache_worker( "'.$func.'", config );'.\PHP_EOL; |
| 153 |
$code .= ' window.setInterval(function() { docket_cache_worker( "'.$func.'", config ); }, 60000);'.\PHP_EOL; |
| 154 |
$code .= ' }'.\PHP_EOL; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
$code .= ' });'.\PHP_EOL; |
| 159 |
$code .= '}'.\PHP_EOL; |
| 160 |
$code .= '</script>'; |
| 161 |
|
| 162 |
return $code; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* get_user_ip. |
| 167 |
*/ |
| 168 |
public function get_user_ip() |
| 169 |
{ |
| 170 |
foreach ([ |
| 171 |
'HTTP_CF_CONNECTING_IP', |
| 172 |
'HTTP_CLIENT_IP', |
| 173 |
'HTTP_X_FORWARDED_FOR', |
| 174 |
'HTTP_X_FORWARDED', |
| 175 |
'HTTP_X_CLUSTER_CLIENT_IP', |
| 176 |
'HTTP_X_REAL_IP', |
| 177 |
'HTTP_FORWARDED_FOR', |
| 178 |
'HTTP_FORWARDED', |
| 179 |
'REMOTE_ADDR', |
| 180 |
] as $key) { |
| 181 |
if (!empty($_SERVER[$key])) { |
| 182 |
$ip = explode(',', $_SERVER[$key]); |
| 183 |
$ip = end($ip); |
| 184 |
|
| 185 |
if (false !== filter_var($ip, \FILTER_VALIDATE_IP)) { |
| 186 |
return $ip; |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
return '0.0.0.0'; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* get_proxy_ip. |
| 196 |
*/ |
| 197 |
public function get_proxy_ip() |
| 198 |
{ |
| 199 |
$ip = wp_cache_get('proxy-ip', 'docketcache-data'); |
| 200 |
if (false !== $ip) { |
| 201 |
return $ip; |
| 202 |
} |
| 203 |
|
| 204 |
if (!empty($_SERVER['HTTP_HOST'])) { |
| 205 |
$ip = gethostbyname($_SERVER['HTTP_HOST']); |
| 206 |
} elseif (!empty($_SERVER['SERVER_ADDR'])) { |
| 207 |
$ip = $_SERVER['SERVER_ADDR']; |
| 208 |
} |
| 209 |
|
| 210 |
if (!empty($ip)) { |
| 211 |
wp_cache_set('proxy-ip', $ip, 'docketcache-data', 3600); |
| 212 |
} |
| 213 |
|
| 214 |
return $ip; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* is_cloudflare. |
| 219 |
*/ |
| 220 |
public function is_cloudflare() |
| 221 |
{ |
| 222 |
if (!empty($_SERVER['HTTP_CF_RAY'])) { |
| 223 |
$rip = $this->get_proxy_ip(); |
| 224 |
|
| 225 |
return $rip.' [Ray ID: '.$_SERVER['HTTP_CF_RAY'].']'; |
| 226 |
} |
| 227 |
|
| 228 |
return false; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* is_behind_proxy. |
| 233 |
*/ |
| 234 |
public function is_behind_proxy() |
| 235 |
{ |
| 236 |
if (!empty($_SERVER['HTTP_CF_RAY'])) { |
| 237 |
return true; |
| 238 |
} |
| 239 |
|
| 240 |
$sipr = (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : null); |
| 241 |
$sipl = (!empty($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : null); |
| 242 |
|
| 243 |
if (null !== $sipr && null !== $sipl) { |
| 244 |
$ipr = explode(',', $sipr); |
| 245 |
$ipr = end($ipr); |
| 246 |
|
| 247 |
$ipl = $sipl; |
| 248 |
|
| 249 |
return $ipr == $ipl ? true : false; |
| 250 |
} |
| 251 |
|
| 252 |
return false; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* get_server_software. |
| 257 |
*/ |
| 258 |
public function get_server_software() |
| 259 |
{ |
| 260 |
if (!empty($_SERVER['SERVER_SOFTWARE'])) { |
| 261 |
$data = explode(' ', $_SERVER['SERVER_SOFTWARE']); |
| 262 |
|
| 263 |
return str_replace('/', ' / ', $data[0]); |
| 264 |
} |
| 265 |
|
| 266 |
return 'Unknown'; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* get_user_agent. |
| 271 |
*/ |
| 272 |
public function get_user_agent() |
| 273 |
{ |
| 274 |
return !empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Unknown'; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* base64_encode_url. |
| 279 |
*/ |
| 280 |
public function base64_encode_url($string) |
| 281 |
{ |
| 282 |
return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($string)); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* base64_decode_url. |
| 287 |
*/ |
| 288 |
public function base64_decode_url($string) |
| 289 |
{ |
| 290 |
return base64_decode(str_replace(['-', '_'], ['+', '/'], $string)); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* nw_encrypt. |
| 295 |
*/ |
| 296 |
public function nw_encrypt($string, $epad = '!!$$surya16!!') |
| 297 |
{ |
| 298 |
$mykey = '!!$'.$epad.'!!'; |
| 299 |
$pad = base64_decode($mykey); |
| 300 |
$encrypted = ''; |
| 301 |
for ($i = 0; $i < \strlen($string); ++$i) { |
| 302 |
$encrypted .= @\chr(@\ord($string[$i]) ^ @\ord($pad[$i])); |
| 303 |
} |
| 304 |
|
| 305 |
return $this->base64_encode_url($encrypted); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* nw_decrypt. |
| 310 |
*/ |
| 311 |
public function nw_decrypt($string, $epad = '!!$$surya16!!') |
| 312 |
{ |
| 313 |
$mykey = '!!$'.$epad.'!!'; |
| 314 |
$pad = base64_decode($mykey); |
| 315 |
$encrypted = $this->base64_decode_url($string); |
| 316 |
$decrypted = ''; |
| 317 |
for ($i = 0; $i < \strlen($encrypted); ++$i) { |
| 318 |
$decrypted .= @\chr(@\ord($encrypted[$i]) ^ @\ord($pad[$i])); |
| 319 |
} |
| 320 |
|
| 321 |
return $decrypted; |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* is_ssl. |
| 326 |
*/ |
| 327 |
public function is_ssl() |
| 328 |
{ |
| 329 |
// cloudflare |
| 330 |
if (!empty($_SERVER['HTTP_CF_VISITOR'])) { |
| 331 |
$cfo = json_decode($_SERVER['HTTP_CF_VISITOR']); |
| 332 |
if (isset($cfo->scheme) && 'https' === $cfo->scheme) { |
| 333 |
return true; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
// other proxy |
| 338 |
if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO']) { |
| 339 |
return true; |
| 340 |
} |
| 341 |
|
| 342 |
return \function_exists('is_ssl') ? is_ssl() : false; |
| 343 |
} |
| 344 |
|
| 345 |
public function get_network_sites(&$counts = 0, $all = false) |
| 346 |
{ |
| 347 |
$data = []; |
| 348 |
$cnt = 0; |
| 349 |
if (is_multisite()) { |
| 350 |
$args = [ |
| 351 |
'no_found_rows' => true, |
| 352 |
]; |
| 353 |
|
| 354 |
if (!$all) { |
| 355 |
$args['network_id'] = get_current_network_id(); |
| 356 |
} |
| 357 |
|
| 358 |
$sites = get_sites($args); |
| 359 |
if (!empty($sites) && \is_array($sites)) { |
| 360 |
$main_site_id = get_main_site_id(); |
| 361 |
foreach ($sites as $num => $site) { |
| 362 |
$data[$num]['id'] = $site->blog_id; |
| 363 |
switch_to_blog($site->blog_id); |
| 364 |
$data[$num]['url'] = get_option('siteurl'); |
| 365 |
restore_current_blog(); |
| 366 |
$data[$num]['is_main'] = 0; |
| 367 |
if ((int) $site->blog_id === (int) $main_site_id) { |
| 368 |
$data[$num]['is_main'] = 1; |
| 369 |
} |
| 370 |
++$cnt; |
| 371 |
} |
| 372 |
} |
| 373 |
} else { |
| 374 |
$data[0] = [ |
| 375 |
'id' => get_current_blog_id(), |
| 376 |
'url' => get_option('siteurl'), |
| 377 |
'is_main' => 1, |
| 378 |
]; |
| 379 |
$cnt = 1; |
| 380 |
} |
| 381 |
|
| 382 |
$counts = $cnt; |
| 383 |
|
| 384 |
return $data; |
| 385 |
} |
| 386 |
|
| 387 |
public function get_crons($all = false, &$count_all = 0, &$count_run = 0) |
| 388 |
{ |
| 389 |
$cron_array = _get_cron_array(); |
| 390 |
if (empty($cron_array)) { |
| 391 |
$count_all = 0; |
| 392 |
$count_run = 0; |
| 393 |
|
| 394 |
return false; |
| 395 |
} |
| 396 |
|
| 397 |
$count_all = \count($cron_array); |
| 398 |
|
| 399 |
$gmt_time = microtime(true); |
| 400 |
$crons = $cron_array; |
| 401 |
$cnt = 0; |
| 402 |
foreach ($cron_array as $timestamp => $cronhooks) { |
| 403 |
if (!$all && $timestamp > $gmt_time) { |
| 404 |
unset($crons[$timestamp]); |
| 405 |
continue; |
| 406 |
} |
| 407 |
|
| 408 |
foreach ($cronhooks as $hook => $keys) { |
| 409 |
if (!has_action($hook)) { |
| 410 |
// wp_clear_scheduled_hook($hook); |
| 411 |
unset($crons[$timestamp]); |
| 412 |
continue; |
| 413 |
} |
| 414 |
++$cnt; |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
unset($cron_array); |
| 419 |
|
| 420 |
$count_run = $cnt; |
| 421 |
|
| 422 |
return $crons; |
| 423 |
} |
| 424 |
|
| 425 |
public function get_utc_offset() |
| 426 |
{ |
| 427 |
$offset = get_option('gmt_offset', 0); |
| 428 |
|
| 429 |
if (empty($offset)) { |
| 430 |
return 'UTC'; |
| 431 |
} |
| 432 |
|
| 433 |
if (0 <= $offset) { |
| 434 |
$formatted_offset = '+'.(string) $offset; |
| 435 |
} else { |
| 436 |
$formatted_offset = (string) $offset; |
| 437 |
} |
| 438 |
$formatted_offset = str_replace( |
| 439 |
['.25', '.5', '.75'], |
| 440 |
[':15', ':30', ':45'], |
| 441 |
$formatted_offset |
| 442 |
); |
| 443 |
|
| 444 |
return 'UTC'.$formatted_offset; |
| 445 |
} |
| 446 |
|
| 447 |
public function get_timezone_name() |
| 448 |
{ |
| 449 |
$timezone_string = get_option('timezone_string', ''); |
| 450 |
$gmt_offset = get_option('gmt_offset', 0); |
| 451 |
|
| 452 |
if ('UTC' === $timezone_string || (empty($gmt_offset) && empty($timezone_string))) { |
| 453 |
return 'UTC'; |
| 454 |
} |
| 455 |
|
| 456 |
if ('' === $timezone_string) { |
| 457 |
return $this->get_utc_offset(); |
| 458 |
} |
| 459 |
|
| 460 |
return sprintf( |
| 461 |
'%s, %s', |
| 462 |
str_replace('_', ' ', $timezone_string), |
| 463 |
$this->get_utc_offset() |
| 464 |
); |
| 465 |
} |
| 466 |
|
| 467 |
public function normalize_content($content) |
| 468 |
{ |
| 469 |
return str_replace(["\r\n", "\n\r", "\r", "\n"], "\n", $content); |
| 470 |
} |
| 471 |
} |
| 472 |
|