/** * * @return type */ public static function getCombinedFile() { $aGet = self::getArray(array('f' => 'alnum', 'd' => 'int', 'i' => 'int', 'type' => 'word')); $iLifetime = (int) $aGet['d'] * 24 * 60 * 60; $aCache = JchPlatformCache::getCache($aGet['f'], $iLifetime); if ($aCache === FALSE) { die('File not found'); } $aTimeMFile = JchPlatformUtility::RFC1123DateAdd($aCache['filemtime'], $aGet['d']); $sTimeMFile = $aTimeMFile['filemtime'] . ' GMT'; $sExpiryDate = $aTimeMFile['expiry'] . ' GMT'; $headers = array(); if (function_exists('apache_request_headers')) { $headers = apache_request_headers(); } if (!empty($headers) && isset($headers['If-Modified-Since']) && strtotime($headers['If-Modified-Since']) == strtotime($sTimeMFile)) { // Client's cache IS current, so we just respond '304 Not Modified'. header('HTTP/1.1 304 Not Modified'); header('Connection: close'); return; } else { header('Last-Modified: ' . $sTimeMFile); } $sFile = $aCache['file'][$aGet['i']]; $sFile = JchOptimizeOutput::getCachedFile($sFile, $iLifetime); $aSpriteCss = $aCache['spritecss']; if ($aGet['type'] == 'css') { if (!empty($aSpriteCss)) { $sFile = str_replace($aSpriteCss['needles'], $aSpriteCss['replacements'], $sFile); } if (!empty($aCache['font-face'])) { $sFile = str_replace($aCache['font-face'], '', $sFile); } $oCssParser = new JchOptimizeCssParser(); $sFile = $oCssParser->sortImports($sFile); if (function_exists('mb_convert_encoding')) { $sFile = '@charset "utf-8";' . $sFile; } } if ($aGet['type'] == 'css') { header('Content-type: text/css; charset=UTF-8'); } elseif ($aGet['type'] == 'js') { header('Content-type: text/javascript; charset=UTF-8'); } header('Expires: ' . $sExpiryDate); header('Cache-Control: Public'); header('Vary: Accept-Encoding'); $gzip = TRUE; if (isset($_SERVER['HTTP_USER_AGENT'])) { /* Facebook User Agent * facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php) * LinkedIn User Agent * LinkedInBot/1.0 (compatible; Mozilla/5.0; Jakarta Commons-HttpClient/3.1 +http://www.linkedin.com) */ $pattern = strtolower('/facebookexternalhit|LinkedInBot/x'); if (preg_match($pattern, strtolower($_SERVER['HTTP_USER_AGENT']))) { $gzip = FALSE; } } if (isset($aGet['gz']) && $aGet['gz'] == 'gz' && $gzip) { $aSupported = array('x-gzip' => 'gz', 'gzip' => 'gz', 'deflate' => 'deflate'); if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) { $aAccepted = array_map('trim', (array) explode(',', $_SERVER['HTTP_ACCEPT_ENCODING'])); $aEncodings = array_intersect($aAccepted, array_keys($aSupported)); } else { $aEncodings = array('gzip'); } if (!empty($aEncodings)) { foreach ($aEncodings as $sEncoding) { if ($aSupported[$sEncoding] == 'gz' || $aSupported[$sEncoding] == 'deflate') { $sGzFile = gzencode($sFile, 4, $aSupported[$sEncoding] == 'gz' ? FORCE_GZIP : FORCE_DEFLATE); if ($sGzFile === FALSE) { continue; } header('Content-Encoding: ' . $sEncoding); $sFile = $sGzFile; break; } } } } header('Content-Length: ' . strlen($sFile)); echo $sFile; }
/** * */ public function runCookieLessDomain() { if (trim($this->params->get('pro_cookielessdomain', ''))) { JCH_DEBUG ? JchPlatformProfiler::start('RunCookieLessDomain') : null; $static_files = implode('|', JchOptimizeCssParser::staticFiles()); $uri = clone JchPlatformUri::getInstance(); $host = preg_quote($uri->getHost()); $path = $uri->getPath(); $aPath = preg_split('#/#', $path); array_pop($aPath); $dir = trim(implode('/', $aPath), '/'); $dir = str_replace('/administrator', '', $dir); $match = '(?!data:image|[\'"])' . '(?=((?:(?:https?:)?//' . $host . ')?)((?!http|//).))' . '(?:(?<![=\'(])(?:\\g{1}|\\g{2})((?>\\.?[^.">]*+)*?\\.(?>' . $static_files . ')[^">]*+)' . '|(?<![\'=>])(?:\\g{1}|\\g{2})((?>\\.?[^.)>]*+)*?\\.(?>' . $static_files . ')[^)>]*+)' . '|(?<!=)(?:\\g{1}|\\g{2})((?>\\.?[^.\'>]*+)*?\\.(?>' . $static_files . ')[^\'>]*+)' . '|(?:\\g{1}|\\g{2})((?>\\.?[^.\\s*>]*+)*?\\.(?>' . $static_files . ')[^\\s>]*+))'; $cdn_static = JchOptimizeHelper::cookieLessDomain($this->params); $a = '(?:<(?:link|script|img))?(?>=?[^=<>]++)*?(?<=href|src)=["\']?'; $b = '(?:<style[^>]*+>|(?=(?>(?:<(?!style))?[^<]*+)?</style))(?>\\(?[^(<>]*+)*?(?<=url)\\(["\']?'; $c = '(?>=?[^=>]++)*?(?<=style)=[^(>]++(?<=url)\\(["\']?'; $sRegex = "#(?><?[^<]*+)*?(?:(?:{$a}|{$b}|{$c})\\K{$match}|\\K\$)#iS"; $obj = $this; $sProcessedHeadHtml = preg_replace_callback($sRegex, function ($m) use($cdn_static, $dir, $obj) { return $obj->cdnCB($m, $cdn_static, $dir); }, $this->getHeadHtml()); $sProcessedBodyHtml = preg_replace_callback($sRegex, function ($m) use($cdn_static, $dir, $obj) { return $obj->cdnCB($m, $cdn_static, $dir); }, $this->getBodyHtml()); if (is_null($sProcessedHeadHtml) || is_null($sProcessedBodyHtml)) { JchOptimizeLogger::log(JchPlatformUtility::translate('Cookie-less domain function failed'), $this->params); return; } if (preg_match($this->getHeadRegex(), $sProcessedHeadHtml, $aHeadMatches) === FALSE || empty($aHeadMatches)) { JchOptimizeLogger::log(JchPlatformUtility::translate('Failed retrieving head in cookie-less domain function'), $this->params); return; } if (preg_match($this->getBodyRegex(), $sProcessedBodyHtml, $aBodyMatches) === FALSE || empty($aBodyMatches)) { JchOptimizeLogger::log(JchPlatformUtility::translate('Failed retrieving body in cookie-less domain function'), $this->params); return; } $this->sHeadHtml = $aHeadMatches[0]; $this->sBodyHtml = $aBodyMatches[0]; JCH_DEBUG ? JchPlatformProfiler::stop('RunCookieLessDomain', TRUE) : null; } }