HEX
Server: Apache/2.4.6 (CloudLinux) OpenSSL/1.0.2k-fips mod_fcgid/2.3.9 PHP/5.4.16
System: Linux s1.gigspace.ru 3.10.0-962.3.2.lve1.5.77.el7.x86_64 #1 SMP Mon Dec 12 07:06:14 EST 2022 x86_64
User: samok164 (6070)
PHP: 7.2.34
Disabled: NONE
Upload Files
File: /var/www/samok164/data/www2/obryadi.ru/rzgn/r4_common.php
<?php

$r4_error_reporting = error_reporting(0);

$r4_cfg_root = preg_replace('/([\/]+)/is', '/', dirname(__FILE__).'/');

include("{$r4_cfg_root}r4_vars.php");

# Dimoha: смысл выносить все это в конфиг мне пока не понятен
# пусть пока будут тут.
$r4_cfg_test_title_text = "R4_TITLE_PLACEHOLDER";
$r4_cfg_test_h1_text = "R4_H1_PLACEHOLDER";
$r4_cfg_default_tags_encoding = "UTF-8";

#  С версии разгонщика 3.2 мы храним файлы только в утф-8
$r4_cfg_default_encoding = "UTF-8";

if (isset($r4_cfg_error_reporting)) {
    error_reporting($r4_cfg_error_reporting);
}

include_once("{$r4_cfg_root}r4_libs.php");

if($_data_type == 'links') {
    global $sape;
    global $sapelc;
    global $sapecode;
    
    if (isset($sapecode) && $sapecode && isset($r4_cfg_sape) && $r4_cfg_sape &&
            isset($r4_cfg_sape_user) && isset($r4_cfg_sape_links_count) && $r4_cfg_sape_links_count > 0) {
        if (!defined('_SAPE_USER')) {
            define('_SAPE_USER', $r4_cfg_sape_user);

            include_once(realpath($r4_cfg_root.'../'._SAPE_USER.'/sape.php'));

            $sape = new SAPE_client();
        }

        if ($sapelc++ < $r4_cfg_sape_links_count) {
            echo iconv_link($sape->return_links(1), 'WINDOWS-1251', $r4_cfg_link_encoding);
        }

        return;
    }
}

include_once("{$r4_cfg_root}r4_getHash.php");

$r4_cfg_cms = isset($r4_cfg_cms) ? $r4_cfg_cms : '';
$r4_cfg_multidomain = isset($r4_cfg_multidomain) ? $r4_cfg_multidomain : false;

if (in_array(strtolower($r4_cfg_cms), array('elitoid'))) {
    $r4_cfg_link_encoding = _R4_PAGE_ENCODING;
}

// SAFE MODE
if (!isset($safe_mode)) {
    $safe_mode = check_safe_mode($r4_cfg_root);
}

if ($safe_mode) {
    include_once("{$r4_cfg_root}pclerror.lib.php3");
    include_once("{$r4_cfg_root}pcltrace.lib.php3");
    include_once("{$r4_cfg_root}pcltar.lib.php3");
}

$http_host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];

// MultiDomains
if ($r4_cfg_multidomain && !$safe_mode) {
    $r4_domain = strtolower(preg_replace('#^www\.#ism', '', $http_host).'/');
    $r4_domain_dir = $r4_domain;

    if (!file_exists("{$r4_cfg_root}{$r4_domain_dir}")) {
        mkdir("{$r4_cfg_root}{$r4_domain_dir}", 0755);
        chmod("{$r4_cfg_root}{$r4_domain_dir}", 0755);
    }
}
elseif ($r4_cfg_multidomain) {
    $r4_domain = '';
    $r4_domain_dir = strtolower(preg_replace('#^www\.#ism', '', $http_host).'_');
}
else {
    $r4_domain = '';
    $r4_domain_dir = $r4_domain;
}

$safe_mode_links_file = "{$r4_cfg_root}{$r4_domain_dir}links.tar";
$safe_mode_tags_file = "{$r4_cfg_root}{$r4_domain_dir}tags.tar";

// $r4_ruri
$r4_ruri = empty($_SERVER['REQUEST_URI']) ? '' : $_SERVER['REQUEST_URI'];

if (in_array(strtolower($r4_cfg_cms), array('webasyst', 'joomla'))) {
    if (strpos($_SERVER['QUERY_STRING'], '__furl_path') && strpos($_SERVER['QUERY_STRING'], 'frontend')) {
        $r4_ruri  = '/' . str_replace('&', '?', str_replace('__furl_path=', '', str_replace('&frontend=1', '', $_SERVER['QUERY_STRING'])));
    }
}

if (substr($r4_ruri, 0, 1) !== '/') {
    $r4_ruri = '/' . $r4_ruri;
}

if (!empty($r4_cfg_strip_from_uri)) {
    preg_replace($r4_cfg_strip_from_uri, '', $r4_ruri);
}

// $r4_ruri2
if (key_exists('REDIRECT_URL', $_SERVER)) {
    $r4_ruri2 = empty($_SERVER['REDIRECT_URL']) ? '' : $_SERVER['REDIRECT_URL'];
}
else {
    $request_uri = getenv('REQUEST_URI');
    $r4_ruri2 = empty($request_uri) ? '' : $request_uri;
}

if (substr($r4_ruri2, 0, 1) !== '/') {
    $r4_ruri2 = '/' . $r4_ruri2;
}

if (!empty($r4_cfg_strip_from_uri)){
    preg_replace($r4_cfg_strip_from_uri, '', $r4_ruri2);
}

// $r4_h
$r4_h = empty($_SERVER['HTTP_HOST']) ? '' : strtolower($_SERVER['HTTP_HOST']);

if ($r4_cfg_multidomain && !$safe_mode) {
    $r4_h = preg_replace('/^www\./ism', '', $r4_h);
}

// $r4_prepared_uri
$r4_prepared_uri = preg_replace(array('#[ \+,/\.]#', '#\?#'), array('_', '__'), $r4_ruri);
$r4_prepared_uri2 = getHash("http://{$r4_h}{$r4_ruri}", $r4_cfg_root);
$r4_prepared_uri3 = getHash("http://{$r4_h}{$r4_ruri2}", $r4_cfg_root);

$r4_links_file = "{$r4_cfg_root}{$r4_domain}{$r4_prepared_uri}.rzg";
$r4_links_file2 = "{$r4_cfg_root}{$r4_domain}{$r4_prepared_uri2}.rzg";
$r4_links_file3 = "{$r4_cfg_root}{$r4_domain}{$r4_prepared_uri3}.rzg";

if (!empty($_GET['r4showhash'])) {
    exit($r4_prepared_uri2);
}

// $r5_prepared_uri
$r5_prepared_uri = _relative_url_hash("{$r4_ruri}", "{$r4_cfg_root}{$r4_domain}");

if (in_array(strtolower($r4_cfg_cms), array('joomla'))) {
    if (strpos($_SERVER['REQUEST_URI'], 'option=com_content')) {
        $r5_prepared_uri = _relative_url_hash("{$r4_ruri2}", "{$r4_cfg_root}{$r4_domain}");
    }
}

if (isset($_GET['r5showhash'])) {
    exit($r5_prepared_uri);
}

$r5_links_dir = "{$r4_cfg_root}{$r4_domain}links";
$r5_links_subdir = $r5_links_dir.'/'.substr($r5_prepared_uri, 0, 2);

$r5_tags_dir = "{$r4_cfg_root}{$r4_domain}tags";
$r5_tags_subdir = $r5_tags_dir.'/'.substr($r5_prepared_uri, 0, 2);

// SAFE MODE
if (!$safe_mode) {

    if($_data_type == 'tags') {

        if (!file_exists($r5_tags_dir)) {
            mkdir($r5_tags_dir, 0777);
            chmod($r5_tags_dir, 0777);
        }
        if (!file_exists($r5_tags_subdir) && file_exists($r5_tags_subdir)) {
            mkdir($r5_tags_subdir, 0777);
            chmod($r5_tags_subdir, 0777);
        }
    }
    elseif($_data_type == 'links') {

        if (!file_exists($r5_links_dir)) {
            mkdir($r5_links_dir, 0777);
            chmod($r5_links_dir, 0777);
        }

        if (!file_exists($r5_links_subdir) && file_exists($r5_links_dir)) {
            mkdir($r5_links_subdir, 0777);
            chmod($r5_links_subdir, 0777);
        }
    }
}

$r5_links_file = "{$r5_links_subdir}/{$r5_prepared_uri}.rzg";
$r5_tags_file = "{$r5_tags_subdir}/{$r5_prepared_uri}.tgs";

// techinfo
if (isset($_GET['r5techinfo'])) {
    echo "\$r4_cfg_root={$r4_cfg_root}<br>";
    echo "\$r4_domain={$r4_domain}<br>";
    echo "\$safe_mode=". intval($safe_mode) .'<br>';
    echo "\$safe_mode_links_file={$safe_mode_links_file}(". intval(file_exists($safe_mode_links_file)) .')<br>';
    echo "\$r4_ruri={$r4_ruri}<br>";
    echo "\$r4_ruri2={$r4_ruri2}<br>";
    echo "\$r4_links_file={$r4_links_file}(". intval(file_exists($r4_links_file)) .')<br>';
    echo "\$r4_links_file2={$r4_links_file2}(". intval(file_exists($r4_links_file2)) .')<br>';
    echo "\$r4_links_file3={$r4_links_file3}(". intval(file_exists($r4_links_file3)) .')<br>';
    echo "\$r5_links_file={$r5_links_file}(". intval(file_exists($r5_links_file)) .')<br>';
    echo "\$r5_tags_file={$r5_tags_file}(". intval(file_exists($r5_tags_file)) .')<br>';
    echo "\$r4_cfg_default_encoding={$r4_cfg_default_encoding}<br>";
    echo "\$r4_cfg_link_encoding={$r4_cfg_link_encoding}<br>";
    exit();
}

// $r4_slot
if (!isset($r4_slot)) {
    if (in_array(strtolower($r4_cfg_cms), array('modx'))) {
        $r4_slot = isset($startfrom) ? $startfrom : 0;
    }
    else {
        $r4_slot = isset($_GET['startfrom']) ? $_GET['startfrom'] : 0;
    }
}

if (!isset($r4_links)) {
    $r4_links = null;
}

$link = '';
$_meta_tags = array();

$red_query_string = key_exists('REDIRECT_QUERY_STRING', $_SERVER) ? $_SERVER['REDIRECT_QUERY_STRING'] : '';

if (false !== strpos($r4_ruri, $r4_cfg_testpattern)
    || false !== strpos($r4_ruri2, $r4_cfg_testpattern)
    || !empty($_GET[str_replace('?', '', $r4_cfg_testpattern)])
    || false !== strpos($_SERVER['QUERY_STRING'], $r4_cfg_testpattern)
    || false !== strpos($red_query_string, str_replace('?', '', $r4_cfg_testpattern))
) {
    $link = "<a href=\"#\" class=\"[[R4_2_PLACEHOLDER]]\">{$r4_cfg_testtext}</a>{$r4_cfg_separator}";
    $link = iconv_link($link, $r4_cfg_default_encoding, $r4_cfg_link_encoding);
    $_meta_tags = array(
        'title' => $r4_cfg_test_title_text,
        'h1' => $r4_cfg_test_h1_text
    );
}
else {
    if($_data_type == 'links') {
        if (!$r4_links
            && (file_exists($r4_links_file) || file_exists($r4_links_file2) || file_exists($r4_links_file3) || file_exists($r5_links_file)
            || (file_exists($safe_mode_links_file) && $safe_mode))
        ) {
            $r4_links = array();

            if (file_exists($r4_links_file) && !file_exists($r4_links_file2)) {
                rename($r4_links_file, $r4_links_file2);
            }

            if (file_exists($r4_links_file2) && !file_exists($r4_links_file3)) {
                rename($r4_links_file2, $r4_links_file3);
            }

            if (file_exists($r4_links_file3) && !file_exists($r5_links_file)) {
                rename($r4_links_file3, $r5_links_file);
            }

            // SAFE MODE
            if (file_exists($safe_mode_links_file) && $safe_mode) {
                $p_tarname = $safe_mode_links_file;
                $p_list = array(basename($r5_links_file));
                $p_path = $r4_cfg_root;
                $p_remove_path = '';
                $p_mode = 'tar';

                PclTarExtractList($p_tarname, $p_list, $p_path, $p_remove_path, $p_mode);
            }

            $r5_links_basefile = "{$r4_cfg_root}".basename($r5_links_file);

            if ($safe_mode) {
                $r4_links_file_contents = file($r5_links_basefile);
            }
            else {
                $r4_links_file_contents = file($r5_links_file);
            }

            if (file_exists($r5_links_basefile) && file_exists($safe_mode_links_file) && $safe_mode) {
                unlink($r5_links_basefile);
            }

            $r4_info_str = trim(array_shift($r4_links_file_contents));
            $r4_info_fields = explode('|', $r4_info_str);
            $r4_cell_config = array();

            if (isset($r4_info_fields[1])) {
                $r4_cell_config = unserialize($r4_info_fields[1]);
            }

            foreach ($r4_links_file_contents as $r4_line) {
                $r4_line = trim($r4_line);

                if (!empty($r4_line)) {
                    $r4_links[] = $r4_line;
                }
            }
        }

        $r4_lindex = $r4_slot;

        if (!empty($r4_cell_config)) {
            $r4_lindex = array_search($r4_slot, $r4_cell_config);
        }

        if ($r4_lindex !== false && !empty($r4_links[$r4_lindex])) {
            if (in_array(strtolower($r4_cfg_cms), array('yandex'))) {
                $link_yd = explode('<', $r4_links[$r4_lindex]);
                $link_dom = explode('">', $link_yd[1]);
                $link_text = explode('>', $link_yd[2]);
                $link_dom2 = explode('="', $link_dom[0]);
                $link = '<span style="color:#4444DA;">'.$link_yd[0].'</span><br>';
                $link .= '<'.$link_yd[1].'</a>';
                $link .= $link_text[1].'<br>';
                $matches = array();

                preg_match("/^(http:\/\/)?([^\/]+)/i", $link_dom2[1], $matches); // Извлекаем имя хоста из URL

                $link .= '<span style="color:#177317;" >'.$matches[2].'</span>';
                $link .= '<br><br>';
            }
            else {
                $link = $r4_links[$r4_lindex] . $r4_cfg_separator;
            }

            $link = iconv_link($link, $r4_cfg_default_encoding, $r4_cfg_link_encoding);
        }
    }
    elseif($_data_type == 'tags') {

        if (file_exists($r5_tags_file) || (file_exists($safe_mode_tags_file) && $safe_mode)) {

            // SAFE MODE
            if (file_exists($safe_mode_tags_file) && $safe_mode) {
                $p_tarname = $safe_mode_tags_file;
                $p_list = array(basename($r5_tags_file));
                $p_path = $r4_cfg_root;
                $p_remove_path = '';
                $p_mode = 'tar';
                PclTarExtractList($p_tarname, $p_list, $p_path, $p_remove_path, $p_mode);
            }

            $r5_tags_basefile = "{$r4_cfg_root}".basename($r5_tags_file);

            if ($safe_mode) {
                $r4_tags_file_contents = file($r5_tags_basefile);
            }
            else {
                $r4_tags_file_contents = file($r5_tags_file);
            }

            if (file_exists($r5_tags_basefile) && file_exists($safe_mode_tags_file) && $safe_mode) {
                unlink($r5_tags_basefile);
            }

            $r4_info_str = trim(array_shift($r4_tags_file_contents));
            $r4_info_fields = explode('|', $r4_info_str);

            $places_map = array(
                0 => 'title',
                1 => 'h1'
            );

            $c = 0;
            foreach($r4_tags_file_contents as $r4_line) {
                $r4_line = trim($r4_line);
                if($r4_line != 'null' && isset($places_map[$c])) {
                    $_meta_tags[$places_map[$c]] = iconv_link($r4_line, $r4_cfg_default_tags_encoding, $r4_cfg_link_encoding);
                }
                $c++;
            }
        }
    }
}

$r4_ua = empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT'];

// Yandex Logging
if ($r4_cfg_logging_yandex && !isset($r4_yandex_logged)) {
    $r4_yandex_log_file = "{$r4_cfg_root}{$r4_domain}seenbyyandex.rzg";

    check_logfile($r4_yandex_log_file, "{$r4_cfg_root}{$r4_domain}", $r4_cfg_max_log_size);

    if (preg_match('~Yandex(Bot|Direct)/3\.0;\s+\+http~isx', $r4_ua)) {
        if (write_logfile($r4_yandex_log_file, "{$r4_cfg_root}{$r4_domain}", $r4_ruri)) {
            $r4_yandex_logged = true;
        }
    }
}

// Google Logging
if ($r4_cfg_logging_google && !isset($r4_google_logged)) {
    $r4_google_log_file = "{$r4_cfg_root}{$r4_domain}seenbygoogle.rzg";

    check_logfile($r4_google_log_file, "{$r4_cfg_root}{$r4_domain}", $r4_cfg_max_log_size);

    if (preg_match('~GoogleBot~isx', $r4_ua)) {
        if (write_logfile($r4_google_log_file, "{$r4_cfg_root}{$r4_domain}", $r4_ruri)) {
            $r4_google_logged = true;
        }
    }
}

if($_data_type == 'links') {
    $r4_slot++;
}

error_reporting($r4_error_reporting);