'nosniff', 'X-XSS-Protection' => '1; mode=block', 'Referrer-Policy' => 'strict-origin-when-cross-origin', 'X-Frame-Options' => 'SAMEORIGIN', ); public static function start() { ob_start(); ob_implicit_flush(0); } public static function file_exists($file) { $file = str_replace('/i/', '/images/page/', $file); $file = trim($file, '/'); $ps = explode(PATH_SEPARATOR, get_include_path()); foreach($ps as $path) { if (file_exists($path.$file)) { return $path.$file; } } return false; } public static function requireFile($file) { // Remove language dir (en,ru,fr...) if (preg_match('/^\/common\/[a-z]{2}\//', $file) && !self::file_exists($file)) { $file = preg_replace('/^\/common\/[a-z]{2}\//', '/common/', $file); } // Find real file $realFile = self::file_exists($file); if (!$realFile) { return $file; } return file_get_contents($realFile); } public static function addHash($image) { global $pxRatio, $websitePixelRatio; if (!$websitePixelRatio || $pxRatio) { if (!preg_match('/[?&]v=.+/', $image) && $file = static::file_exists(URL::clean($image))) { if ($pxRatio > 1 && is_file(preg_replace('/\.(jpg|png|gif)/', '_x2.\\1', $file))) { $file = preg_replace('/\.(jpg|png|gif)/', '_x2.\\1', $file); } $image = URL::add('v', hash('crc32b', file_get_contents($file)), $image); } } return $image; } public static function addHashes($page, $contentType = 'html') { global $pxRatio, $websitePixelRatio; if (!$websitePixelRatio || $pxRatio) { // Add hash to static images if (preg_match_all('/([\s\[]src=|\sposter=|\sdata=|\sxlink:href=|url\()"(\/(i|images\/((?!pictures).+\/)?).+?)"/', $page, $images)) { $images = array_unique($images[2]); foreach ($images as $i => $image) { $page = str_replace('"'.$image.'"', '"'.Str::format(static::addHash($image), $contentType == 'html' ? 'safe' : '').'"', $page); } } // Add hash to content images $page = preg_replace_callback('//s', function($matches) use($contentType) { $image = $matches[0]; try { if (preg_match('/\sdata-imgfile="(\d+)(_(\d+))?"/', $image, $imgInfo) && preg_match('/\ssrc="(.+?)"/', $image, $imgSrc)) { if (!preg_match('/[?&]v=/', $imgSrc[1])) { $img = PageFactory::attachment($imgInfo[1]); if ($img->found) { if ($imgInfo[3]) { $img = $img->getClass($imgInfo[3]); } $image = preg_replace_callback('/\ssrc="(.+?)"/', function($matches) use($img, $contentType) { $src = $img->src; if (preg_match('/^https?:/i', $matches[1])) { $src = URL::abs($img->src); } return ' src="'.Str::format($src, $contentType == 'html' ? 'safe' : '').'"'; }, $image); } } } } catch (Exception $e) { } return $image; }, $page); } return $page; } static function output() { global $websiteMinifying, $bWebsiteCachingApplied, $pxRatio, $websitePixelRatio, $websiteProfiling; $headers = headers_list(); // Save headers if (preg_match('/Content-Type: application\/pdf/i', implode("\n", $headers))) { exit; } $page = ob_get_clean(); if ($websiteProfiling) { unset($websiteProfiling['context']); $page = round(microtime(true) - $websiteProfiling['start'], 4).$page; $page .= print_r($websiteProfiling, true); $websiteProfiling['start'] = microtime(true); } if ( preg_match('/Content-Type: text\/html/i', implode("\n", $headers)) || ( !preg_match('/Content-Type:/i', implode("\n", $headers)) && preg_match('/\.html/', $_SERVER['REQUEST_URI']) ) ) { ini_set("pcre.backtrack_limit", "23001337"); // Move scripts from footer to head while (preg_match('/.*?(\s*)/s', $page, $pageFooterScript)) { $page = str_replace($pageFooterScript[1], '', $page); $_page = preg_replace_callback('/\s+<\/head>/', function($matches) use($pageFooterScript) { return $pageFooterScript[1].$matches[0]; }, $page); if ($_page) { $page = $_page; } else { break; } } $page = preg_replace('/\s+/', '', $page); if ($websiteMinifying['js'] && $_GET['minifying'] !== 'false') { // Minify inline Javascript try { $_page = preg_replace_callback('/(]*>)(.*?)(<\/script>)/is', function($matches) { if ( preg_match('/\stype="(.+?)"/', $matches[1], $match) && $match[1] != 'text/javascript' && $match[1] != 'application/ld+json' ) { return $matches[0]; } return $matches[1].JSMin::minify($matches[2]).$matches[3]; }, $page); } catch (Exception $e) { $_page = null; } if ($_page) { $page = $_page; } } if ($websiteMinifying['css'] && $_GET['minifying'] !== 'false') { // Minify inline CSS try { $cssMin = new CSSmin(); $_scripts = array(); $_page = preg_replace_callback('/]*>.*?<\/script>/is', function($matches) use(&$_scripts) { $index = count($_scripts); $_scripts[] = $matches[0]; return ''; }, $page); $_page = preg_replace_callback('/(]*>)(.*?)(<\/style>)/is', function($matches) use($cssMin) { return $matches[1].$cssMin->run($matches[2]).$matches[3]; }, $_page); $_page = preg_replace_callback('/