init.php
4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?
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]);
}
}
}