init.php 4.2 KB
<?
define('BASE_PATH', dirname(dirname(dirname(__FILE__))));
define('INCLUDE_PATH', BASE_PATH . '/local/include/');
define('COMPONENTS_CACHE_TTL', 86400);
define('IMG_CACHE_PATH', BASE_PATH . '/resize');
define('LOG_FILENAME', BASE_PATH . '/bitrix/ololog.txt');

include_once BASE_PATH . '/lib/Cpeople/init.php';
include_once BASE_PATH . '/lib/lib_project.php';

//error_reporting(E_ALL);

CModule::IncludeModule("iblock");

foreach (glob("$__lib_base__/SH/Traits/*.trait.php") as $__path__)
{
    include_once $__path__;
}

AddEventHandler('main', 'OnEpilog', '_Check404Error');
function _Check404Error()
{
    if (!defined('ADMIN_SECTION') && defined('ERROR_404') && ERROR_404=='Y')
    {
        require $_SERVER['DOCUMENT_ROOT'].'/404.php';
    }
}

if (isset($_REQUEST['clear_cache']) || $_REQUEST['clearcache'] == 'Y')
{
    \SH\Registry::instance()->getCacheManager()->clear();
}

AddEventHandler('main', 'OnEndBufferContent', function (&$content)
{
    if(!defined('ADMIN_SECTION')) {
        $content = parse_content_to_replace_marks($content);

        if(!defined('IS_AJAX') && !defined('NO_KEEP_STATISTIC')) {
            parseImagesInContentForSitemap($content);
        }
    }
});

function template_text($arParams = array())
{
    ob_start();
    $GLOBALS["APPLICATION"]->IncludeComponent("cpeople:text", "", $arParams);
    return ob_get_clean();
}

function template_gallery($arParams = array())
{
    ob_start();
    $GLOBALS["APPLICATION"]->IncludeComponent("cpeople:gallery", "", $arParams);
    return ob_get_clean();
}

function parse_content_to_replace_marks($tpl)
{
    if (!defined('DONT_PARSE_TEMPLATES')) {
        $count = 2;
        do {
            $oldTpl = $tpl;
            $tpl = preg_replace_callback(
                '/\{([a-zA-Z0-9][^}]+)\}/U', function ($matches) {
                $replaceFrom = $matches[0];

                $words = explode(' ', $matches[1]);

                $functionName = '\template_' . strtolower(array_shift($words));
                if (!function_exists($functionName)) return $replaceFrom;

                $arParams = array();
                foreach ($words as $word) {
                    $arWord = explode('=', $word);
                    $key = count($arWord) == 2 ? strtoupper($arWord[0]) : 'ID';
                    $val = count($arWord) == 2 ? $arWord[1] : $arWord[0];
                    $arParams[$key] = $val;
                }

                return call_user_func($functionName, $arParams);
            }, $tpl
            );
        } while($oldTpl != $tpl && $count-- > 0);
    }
    return $tpl;
}


function parseImagesInContentForSitemap($content)
{
    global $APPLICATION;
    $curUrl = $APPLICATION->GetCurPage();

    $pathCurImageInfo =  getImagesInfoPathByUrl($curUrl);

    if(!file_exists($pathCurImageInfo) || filemtime($pathCurImageInfo) < time() - 24 * 60 * 60) {
        @mkdir(dirname($pathCurImageInfo), 0777, true);
        $images = getImagesInfoInContent($content);
        file_put_contents($pathCurImageInfo, json_encode($images));
    }
}

function getImagesInfoPathByUrl($url)
{
    return BASE_PATH . '/upload/sitemap-images-info/' . str_replace('/', '-', $url) . '.json';
}

function getImagesInfoInContent($content)
{
    $result = array();

    preg_match_all('/<img[^>]+>/', $content, $imgs);

    if(!is_array($imgs) || empty($imgs[0])) return;

    foreach($imgs[0] as $img) {

        $obj = simplexml_load_string($img);

        if(!$obj) continue;

        $ar = iterator_to_array($obj->attributes());
        $ar = array(
            'src' => (String)$ar['src'],
            'alt' => (String)$ar['alt'],
        );

        if(pathinfo($ar['src'], PATHINFO_EXTENSION) == 'svg') continue;
        if(substr($ar['src'], 0, 2) == '//' || substr($ar['src'], 0, 4) == 'http') continue;

        $result[] = $ar;
    }

    return $result;
}

AddEventHandler("main", "OnAdminTabControlBegin", "OnAdminTabControlBeginHandlerRemoveAdInset");
function OnAdminTabControlBeginHandlerRemoveAdInset(&$items)
{
    foreach($items->tabs as $i => $ar) {
        if($ar['TAB'] == 'Реклама') {
            unset($items->tabs[$i]);
        }
    }
}