Exemple #1
0
 /**
  * Optimize css and js files
  *
  * @param array $arFile - Full path for a source css or js file
  * @param array $arSrcFile - Full path for a source css or js file with timestamp
  * @param string $site_template - Site template name
  * @param bool $unique - Create unique file for all site in a current template
  * @param string $sufix - Some sufix for css or js files
  * @param string $type - file type, css or js
  * @param bool $bXhtmlStyle - Xhtml or not
  * @return string - String for including css or js in html page
  */
 private function __OptimizeCssJs($arFile = array(), $arSrcFile = array(), $site_template, $unique = false, $sufix = 'default', $type = 'css', $bXhtmlStyle)
 {
     if (!is_array($arFile) || count($arFile) < 1) {
         return '';
     }
     if (!is_array($arSrcFile) || count($arSrcFile) < 1) {
         return '';
     }
     $res = '';
     $fileMTime = '';
     $upOptim = 'NO';
     $unsetKey = array();
     $arFilesInfo = array();
     $arFilesInfo['CUR_SEL_CNT'] = 0;
     $arFilesInfo['CUR_IE_CNT'] = 0;
     $sufix = trim($sufix);
     $sufix = strlen($sufix) < 1 ? 'default' : $sufix;
     foreach ($arSrcFile as $key => $filePath) {
         if (!$bExternalLink && strpos($filePath, '?') === false) {
             $filePath = CUtil::GetAdditionalFileURL($filePath);
             $arSrcFile[$key] = $filePath;
         }
     }
     if (!$unique) {
         $fileMTime = '_' . md5(implode('_', $arSrcFile));
         $sufix .= '_' . md5(implode('_', $arFile));
     }
     $type = $type == 'js' ? 'js' : 'css';
     $optimPath = BX_PERSONAL_ROOT . '/cache/' . $type . '/' . SITE_ID . '/' . $site_template . '/' . $sufix . '/';
     $infoFile = BX_PERSONAL_ROOT . '/managed_cache/' . $GLOBALS['DB']->type . '/' . $type . '/' . SITE_ID . '/' . $site_template . '/' . $sufix . '/info.php';
     if ($type == 'css') {
         $maxAddCssSelect = 3950;
         $maxCssSelect = 4000;
         $optimFName = $optimPath . 'styles' . $fileMTime . '.css';
         $cssFNameIE = $optimPath . 'styles' . $fileMTime . '#CNT#.css';
     } else {
         $optimFName = $optimPath . $sufix . $fileMTime . '.js';
     }
     if ($unique && file_exists($_SERVER["DOCUMENT_ROOT"] . $optimFName)) {
         if (file_exists($_SERVER['DOCUMENT_ROOT'] . $infoFile)) {
             include $_SERVER['DOCUMENT_ROOT'] . $infoFile;
             foreach ($arFile as $key => $fp) {
                 $fp = $_SERVER['DOCUMENT_ROOT'] . $fp;
                 if (array_key_exists($fp, $arFilesInfo['FILES'])) {
                     if ($_SERVER['DOCUMENT_ROOT'] . $arSrcFile[$key] != $fp . '?' . $arFilesInfo['FILES'][$fp]) {
                         $upOptim = 'NEW';
                         break;
                     } else {
                         $unsetKey[] = $key;
                     }
                 } else {
                     $upOptim = 'UP';
                 }
             }
         } else {
             $upOptim = 'NEW';
         }
     }
     if (!file_exists($_SERVER["DOCUMENT_ROOT"] . $optimFName) || $upOptim == 'NEW') {
         $upOptim = 'NEW';
         $arFilesInfo = array();
         $arFilesInfo['CUR_SEL_CNT'] = 0;
         $arFilesInfo['CUR_IE_CNT'] = 0;
         if ($unique) {
             DeleteDirFilesEx($optimPath);
         }
     }
     if ($upOptim != 'NO') {
         $contents = '';
         $arIEContent = array();
         if ($upOptim == 'UP' && file_exists($_SERVER["DOCUMENT_ROOT"] . $optimFName)) {
             foreach ($unsetKey as $key) {
                 unset($arFile[$key], $arSrcFile[$key]);
             }
             $contents .= "\n\n" . file_get_contents($_SERVER["DOCUMENT_ROOT"] . $optimFName);
             if ($type == 'css') {
                 if ($arFilesInfo['CUR_SEL_CNT'] < $maxAddCssSelect) {
                     $css = str_replace('#CNT#', $arFilesInfo['CUR_IE_CNT'], $cssFNameIE);
                     if (file_exists($_SERVER["DOCUMENT_ROOT"] . $css)) {
                         $arIEContent[$arFilesInfo['CUR_IE_CNT']] .= file_get_contents($_SERVER["DOCUMENT_ROOT"] . $css);
                         $arFilesInfo['CUR_SEL_CNT'] = CMain::__GetCssSelectCnt($arIEContent[$arFilesInfo['CUR_IE_CNT']]);
                     }
                 } elseif ($arFilesInfo['CUR_SEL_CNT'] >= $maxAddCssSelect) {
                     $arFilesInfo['CUR_IE_CNT']++;
                     $arFilesInfo['CUR_SEL_CNT'] = 0;
                 }
             }
         }
         foreach ($arFile as $key => $filename) {
             $filename = $_SERVER['DOCUMENT_ROOT'] . $filename;
             $tmp_content = file_get_contents($filename);
             if ($type == 'css') {
                 $f_cnt = CMain::__GetCssSelectCnt($tmp_content);
                 $new_cnt = $f_cnt + $arFilesInfo['CUR_SEL_CNT'];
                 $tmp_content = "\n" . preg_replace('#([;\\s:]+url\\s*\\(\\s*)([^\\)]+)\\)#sie', "'\\1'.CMain::__ReplaceUrlCSS('\\2', '" . AddSlashes($arSrcFile[$key]) . "').')'", $tmp_content) . "\n";
                 if ($new_cnt < $maxCssSelect) {
                     $arFilesInfo['CUR_SEL_CNT'] = $new_cnt;
                     $arIEContent[$arFilesInfo['CUR_IE_CNT']] .= $tmp_content;
                 } else {
                     $arFilesInfo['CUR_SEL_CNT'] = $f_cnt;
                     $arFilesInfo['CUR_IE_CNT']++;
                     $arIEContent[$arFilesInfo['CUR_IE_CNT']] .= $tmp_content;
                 }
                 $contents .= "\n\n" . $tmp_content;
             } else {
                 $contents .= "\n;\n" . $tmp_content;
             }
             if ($unique) {
                 $arFilesInfo['FILES'][$filename] = CMain::__GetCssJsTime($arSrcFile[$key]);
             }
         }
         if (CMain::__WriteCssCache($optimFName, $contents) && $unique) {
             $cacheInfo = '<? $arFilesInfo = array( \'FILES\' => array(';
             foreach ($arFilesInfo['FILES'] as $key => $time) {
                 $cacheInfo .= "'" . EscapePHPString($key) . "' => '" . intval($time) . "',";
             }
             $cacheInfo .= "), 'CUR_SEL_CNT' => '" . $arFilesInfo['CUR_SEL_CNT'] . "', 'CUR_IE_CNT' => '" . $arFilesInfo['CUR_IE_CNT'] . "'); ?>";
             CMain::__WriteCssCache($infoFile, $cacheInfo, false);
         }
         foreach ($arIEContent as $key => $ieContent) {
             $css = str_replace('#CNT#', $key, $cssFNameIE);
             CMain::__WriteCssCache($css, $ieContent);
         }
     }
     if (IsIE() && $type == 'css') {
         for ($i = 0; $i <= $arFilesInfo['CUR_IE_CNT']; $i++) {
             $css = str_replace('#CNT#', $i, $cssFNameIE);
             $res .= '<link href="' . CUtil::GetAdditionalFileURL($css) . '" type="text/css" rel="stylesheet"' . ($bXhtmlStyle ? ' /' : '') . '>' . "\n";
         }
     } else {
         if ($type == 'css') {
             $res .= '<link href="' . CUtil::GetAdditionalFileURL($optimFName) . '" type="text/css" rel="stylesheet"' . ($bXhtmlStyle ? ' /' : '') . '>' . "\n";
             if ($unique && $sufix == 'kernel') {
                 $arF = array_keys($arFilesInfo['FILES']);
                 foreach ($arF as $key => $item) {
                     $arF[$key] = str_replace($_SERVER['DOCUMENT_ROOT'], '', $item);
                 }
                 $res .= '<script type="text/javascript">' . " var arKernelCSS = new Array('" . implode("','", $arF) . "'); </script>";
             }
         } else {
             $res .= '<script type="text/javascript" src="' . CUtil::GetAdditionalFileURL($optimFName) . '"></script>' . "\n";
             if ($unique && $sufix == 'kernel') {
                 $arF = array_keys($arFilesInfo['FILES']);
                 foreach ($arF as $key => $item) {
                     $arF[$key] = str_replace($_SERVER['DOCUMENT_ROOT'], '', $item);
                 }
                 $res .= '<script type="text/javascript">' . " BX.setKernelJS(['" . implode("','", $arF) . "']); </script>";
             }
         }
     }
     return $res;
 }
Exemple #2
0
	function __OptimizeCss($cssFile, $cssSrcFile, $site_template, $addTemplateStyle, $arTemplateCss, $bXhtmlStyle)
	{
		$cssOptPath = BX_PERSONAL_ROOT."/cache/css/".SITE_ID.'/'.$site_template.'/';
		$cssFName = $cssOptPath.'styles.css';
		$cssFNameIE = $cssOptPath.'styles#CNT#.css';
		$cssTemplateFName = $cssOptPath.'template_styles.css';
		$cssInfoFile = BX_PERSONAL_ROOT."/managed_cache/".$GLOBALS['DB']->type.'/css/'.SITE_ID.'/'.$site_template.'_styles.php';

		$res = '';
		$upCSS = 'NO';
		$unsetKey = array();
		$arCSSInfo = array();
		$arCSSInfo['CUR_SEL_CNT'] = 0;
		$arCSSInfo['CUR_IE_CNT'] = 0;
		$arCSSInfo['CSS_MD5'] = 0;
		$maxAddCssSelect = 3950;
		$maxCssSelect = 4000;

		if($addTemplateStyle)
		{
			foreach($arTemplateCss as $css_path)
			{
				if(!$bExternalLink && strpos($css_path, '?') === false)
					$css_path = CUtil::GetAdditionalFileURL($css_path);
				$cssMD5 .= $css_path;
			}
			$cssMD5 = md5($cssMD5);
		}

		if(file_exists($_SERVER['DOCUMENT_ROOT'].$cssInfoFile))
		{
			include($_SERVER['DOCUMENT_ROOT'].$cssInfoFile);
			$oldCssMD5 = $arCSSInfo['CSS_MD5'];
			foreach($cssFile as $key => $css)
			{
				if(array_key_exists($css, $arCSSInfo['CSS']))
				{
					if($cssSrcFile[$key] != $css.'?'.$arCSSInfo['CSS'][$css])
					{
						$upCSS = 'NEW';
						break;
					}
					else
					{
						$unsetKey[] = $key;
					}
				}
				else
				{
					$upCSS = 'UP';
				}
			}
		}
		else
		{
			$upCSS = 'NEW';
		}

		if(!file_exists($_SERVER["DOCUMENT_ROOT"].$cssFName) || $upCSS == 'NEW')
		{
			$upCSS = 'NEW';
			$arCSSInfo = array();
			$arCSSInfo['CUR_SEL_CNT'] = 0;
			$arCSSInfo['CUR_IE_CNT'] = 0;
			$arCSSInfo['CSS_MD5'] = 0;
			DeleteDirFilesEx($cssOptPath);
		}

		if($upCSS != 'NO')
		{
			$contents = '';
			$arIEContent = array();

			if($upCSS == 'UP' && file_exists($_SERVER["DOCUMENT_ROOT"].$cssFName))
			{
				foreach($unsetKey as $key)
					unset($cssFile[$key], $cssSrcFile[$key]);

				$contents .= file_get_contents($_SERVER["DOCUMENT_ROOT"].$cssFName);

				if($arCSSInfo['CUR_SEL_CNT'] < $maxAddCssSelect)
				{
					$css = str_replace('#CNT#', $arCSSInfo['CUR_IE_CNT'], $cssFNameIE);
					if(file_exists($_SERVER["DOCUMENT_ROOT"].$css))
					{
						$arIEContent[$arCSSInfo['CUR_IE_CNT']] .= file_get_contents($_SERVER["DOCUMENT_ROOT"].$css);
						$arCSSInfo['CUR_SEL_CNT'] = CMain::__GetCssSelectCnt($arIEContent[$arCSSInfo['CUR_IE_CNT']]);
					}
				}
				elseif($arCSSInfo['CUR_SEL_CNT'] >= $maxAddCssSelect)
				{
					$arCSSInfo['CUR_IE_CNT']++;
					$arCSSInfo['CUR_SEL_CNT'] = 0;
				}
			}

			foreach($cssFile as $key => $filename)
			{
				$filename = $_SERVER['DOCUMENT_ROOT'].$filename;
				$tmp_content = file_get_contents($filename);

				$f_cnt = CMain::__GetCssSelectCnt($tmp_content);
				$new_cnt = $f_cnt + $arCSSInfo['CUR_SEL_CNT'];
				$contents .= $tmp_content = "\n".preg_replace('#([;\s:]+url\s*\(\s*)([^\)]+)\)#sie', "'\\1'.CMain::__ReplaceUrlCSS('\\2', '".AddSlashes($cssSrcFile[$key])."').')'", $tmp_content)."\n";
				if($new_cnt < $maxCssSelect)
				{
					$arCSSInfo['CUR_SEL_CNT'] = $new_cnt;
					$arIEContent[$arCSSInfo['CUR_IE_CNT']] .= $tmp_content;
				}
				else
				{
					$arCSSInfo['CUR_SEL_CNT'] = $f_cnt;
					$arCSSInfo['CUR_IE_CNT']++;
					$arIEContent[$arCSSInfo['CUR_IE_CNT']] .= $tmp_content;
				}

				$arCSSInfo['CSS'][$cssFile[$key]] = CMain::__GetCssJsTime($cssSrcFile[$key]);
			}

			if(CMain::__WriteCssCache($cssFName, $contents))
			{
				$cacheInfo = '<? $arCSSInfo = array( \'CSS\' => array(';

				foreach($arCSSInfo['CSS'] as $css => $time)
					$cacheInfo .= "'".EscapePHPString($css)."' => '".intval($time)."',";

				$cacheInfo .= "), 'CUR_SEL_CNT' => '".$arCSSInfo['CUR_SEL_CNT']."', 'CUR_IE_CNT' => '".$arCSSInfo['CUR_IE_CNT']."', ";
				$cacheInfo .= "'CSS_MD5' => '".$cssMD5."'); ?>";

				CMain::__WriteCssCache($cssInfoFile, $cacheInfo, false);
			}

			foreach($arIEContent as $key => $ieContent)
			{
				$css = str_replace('#CNT#', $key, $cssFNameIE);
				CMain::__WriteCssCache($css, $ieContent);
			}
		}

		if(IsIE())
		{
			for($i = 0; $i <= $arCSSInfo['CUR_IE_CNT']; $i++)
			{
				$css = str_replace('#CNT#', $i, $cssFNameIE);
				$res .= '<link href="'.CUtil::GetAdditionalFileURL($css).'" type="text/css" rel="stylesheet"'.($bXhtmlStyle? ' /':'').'>'."\n";
			}
		}
		else
		{
			$res .= '<link href="'.CUtil::GetAdditionalFileURL($cssFName).'" type="text/css" rel="stylesheet"'.($bXhtmlStyle? ' /':'').'>'."\n";
		}

		if($addTemplateStyle)
		{
			if($cssMD5 != $oldCssMD5 || !file_exists($_SERVER['DOCUMENT_ROOT'].$cssTemplateFName))
			{
				$contents = '';
				foreach($arTemplateCss as $css)
				{
					if(file_exists($_SERVER["DOCUMENT_ROOT"].$css))
					{
						$tmp_content = file_get_contents($_SERVER["DOCUMENT_ROOT"].$css);
						$contents .= preg_replace('#([;\s:]+url\s*\(\s*)([^\)]+)\)#sie', "'\\1'.CMain::__ReplaceUrlCSS('\\2', '".AddSlashes($css)."').')'", $tmp_content);
					}
				}
				CMain::__WriteCssCache($cssTemplateFName, $contents);
			}
			$res .= '<link href="'.$cssTemplateFName.'?'.$cssMD5.'" type="text/css" rel="stylesheet"'.($bXhtmlStyle? ' /':'').'>'."\n";
		}
		unset($content, $arIEContent, $arCSSInfo, $cacheInfo);
		return $res;
	}