/** * * @param type $m * @param type $cdn * @param type $dir * @return type */ public function cdnCB($m, $dir) { $sPath = (isset($m[2]) && $m[2] != '/' ? '/' . $dir . '/' : '') . (isset($m[3]) ? $m[3] : '') . (isset($m[4]) ? $m[4] : '') . (isset($m[5]) ? $m[5] : '') . (isset($m[6]) ? $m[6] : ''); return JchOptimizeHelper::cookieLessDomain($this->params, $sPath); }
/** * Callback function to correct urls in aggregated css files * * @param array $aMatches Array of all matches * @return string Correct url of images from aggregated css file */ public function _correctUrlCB($aMatches, $aUrl) { if (empty($aMatches[1]) || preg_match('#^(?:\\(|/\\*)#', $aMatches[0])) { return $aMatches[0]; } $sImageUrl = $aMatches[1]; $sCssFileUrl = empty($aUrl['url']) ? '' : $aUrl['url']; if (JchOptimizeUrl::isHttpScheme($sImageUrl)) { if ((JchOptimizeUrl::isInternal($sCssFileUrl) || $sCssFileUrl == '') && JchOptimizeUrl::isInternal($sImageUrl)) { $sImageUrl = JchOptimizeUrl::toRootRelative($sImageUrl, $sCssFileUrl); $oImageUri = clone JchPlatformUri::getInstance($sImageUrl); $aStaticFiles = $this->params->get('pro_staticfiles', array('css', 'js', 'jpe?g', 'gif', 'png', 'ico', 'bmp', 'pdf')); unset($aStaticFiles[0]); $sStaticFiles = implode('|', $aStaticFiles); $aFontFiles = $this->fontFiles(); $sFontFiles = implode('|', $aFontFiles); if (preg_match('#\\.(?>' . $sStaticFiles . ')#', $oImageUri->getPath())) { $sImageUrl = JchOptimizeHelper::cookieLessDomain($this->params, $oImageUri->toString(array('path'))); } elseif ($this->params->get('pro_cookielessdomain_enable', '0') && preg_match('#\\.(?>' . $sFontFiles . ')#', $oImageUri->getPath())) { $oUri = clone JchPlatformUri::getInstance(); $sImageUrl = '//' . $oUri->toString(array('host', 'port')) . $oImageUri->toString(array('path')); } } else { if (!JchOptimizeUrl::isAbsolute($sImageUrl)) { $sImageUrl = JchOptimizeUrl::toAbsolute($sImageUrl, $sCssFileUrl); } } } $sImageUrl = preg_match('#(?<!\\\\)[\\s\'"(),]#', $sImageUrl) ? '"' . $sImageUrl . '"' : $sImageUrl; return $sImageUrl; }
/** * * @return type */ protected function cookieLessDomain($sType) { if ($this->params->get('pro_cookielessdomain_enable', '0') && in_array($sType, $this->params->get('pro_staticfiles', array('css', 'js', 'jpe?g', 'gif', 'png', 'ico', 'bmp', 'pdf')))) { return JchOptimizeHelper::cookieLessDomain($this->params, JchPlatformPaths::assetPath(TRUE)); } return JchPlatformPaths::assetPath(); }
/** * Callback function to correct urls in aggregated css files * * @param array $aMatches Array of all matches * @return string Correct url of images from aggregated css file */ public function _correctUrlCB($aMatches) { //JCH_DEBUG ? JchPlatformProfiler::mark('beforeCorrectUrl plgSystem (JCH Optimize)') : null; if (preg_match('#^(?:\\(|/(?:/|\\*))#', $aMatches[0])) { return $aMatches[0]; } if ($aMatches[0] == '') { return ' '; } $sUriBase = JchPlatformUri::base(TRUE); $sImageUrl = $aMatches[1]; $sCssFileUrl = isset($this->aUrl['url']) ? $this->aUrl['url'] : '/'; if (preg_match('#^data:#', $sImageUrl)) { return $sImageUrl; } if (!preg_match('#^/|://#', $sImageUrl)) { $aCssUrlArray = explode('/', $sCssFileUrl); array_pop($aCssUrlArray); $sCssRootPath = implode('/', $aCssUrlArray) . '/'; $sImagePath = $sCssRootPath . $sImageUrl; $oImageUri = clone JchPlatformUri::getInstance($sImagePath); $sUriPath = preg_replace('#^' . preg_quote($sUriBase, '#') . '/#', '', $oImageUri->getPath()); $oImageUri->setPath($sUriBase . '/' . $sUriPath); $sImageUrl = $oImageUri->toString(); } if (JchOptimizeHelper::isInternal($sCssFileUrl) && JchOptimizeHelper::isInternal($sImageUrl)) { $oImageUri = clone JchPlatformUri::getInstance($sImageUrl); $aStaticFiles = $this->staticFiles(); $sStaticFiles = implode('|', $aStaticFiles); $aFontFiles = $this->fontFiles(); $sFontFiles = implode('|', $aFontFiles); if (preg_match('#\\.(?>' . $sStaticFiles . ')#', $oImageUri->getPath())) { $sImageUrl = JchOptimizeHelper::cookieLessDomain($this->params) . $oImageUri->toString(array('path')); } elseif (preg_match('#\\.php#', $oImageUri->getPath())) { $sImageUrl = $oImageUri->toString(array('path', 'query', 'fragment')); } elseif ($this->params->get('pro_cookielessdomain', '') && preg_match('#\\.(?>' . $sFontFiles . ')#', $oImageUri->getPath())) { $oUri = clone JchPlatformUri::getInstance(); $sImageUrl = '//' . $oUri->toString(array('host', 'port')) . $oImageUri->toString(array('path')); } else { $sImageUrl = $oImageUri->toString(array('path')); } } //JCH_DEBUG ? JchPlatformProfiler::mark('afterCorrectUrl plgSystem (JCH Optimize)') : null; $sImageUrl = preg_match('#(?<!\\\\)[\\s\'"(),]#', $sImageUrl) ? '"' . $sImageUrl . '"' : $sImageUrl; return $sImageUrl; }
/** * Callback function to correct urls in aggregated css files * * @param array $aMatches Array of all matches * @return string Correct url of images from aggregated css file */ public function _correctUrlCB($aMatches, $aUrl) { if (!isset($aMatches[1]) || $aMatches[1] == '' || preg_match('#^(?:\\(|/(?:/|\\*))#', $aMatches[0])) { return $aMatches[0]; } $sUriBase = JchPlatformUri::base(TRUE); $sImageUrl = $aMatches[1]; $sCssFileUrl = isset($aUrl['url']) ? $aUrl['url'] : '/'; if (!preg_match('#^data:#', $sImageUrl)) { if (!preg_match('#^/|://#', $sImageUrl)) { $aCssUrlArray = explode('/', $sCssFileUrl); array_pop($aCssUrlArray); $sCssRootPath = implode('/', $aCssUrlArray) . '/'; $sImagePath = $sCssRootPath . $sImageUrl; $oImageUri = clone JchPlatformUri::getInstance($sImagePath); if (JchOptimizeHelper::isInternal($sCssFileUrl)) { $sUriPath = preg_replace('#^' . preg_quote($sUriBase, '#') . '/#', '', $oImageUri->getPath()); $oImageUri->setPath($sUriBase . '/' . $sUriPath); $sImageUrl = $oImageUri->toString(); } else { $oImageUri->setPath($oImageUri->getPath()); $sImageUrl = ($oImageUri->toString(array('scheme')) ? '' : '//') . $oImageUri->toString(); } } if (JchOptimizeHelper::isInternal($sCssFileUrl) && JchOptimizeHelper::isInternal($sImageUrl)) { $oImageUri = clone JchPlatformUri::getInstance($sImageUrl); $aStaticFiles = $this->params->get('pro_staticfiles', array('css', 'js', 'jpe?g', 'gif', 'png', 'ico', 'bmp', 'pdf')); $sStaticFiles = implode('|', $aStaticFiles); $aFontFiles = $this->fontFiles(); $sFontFiles = implode('|', $aFontFiles); if (preg_match('#\\.(?>' . $sStaticFiles . ')#', $oImageUri->getPath())) { $sImageUrl = JchOptimizeHelper::cookieLessDomain($this->params, $oImageUri->toString(array('path'))); } elseif (preg_match('#\\.php#', $oImageUri->getPath())) { $sImageUrl = $oImageUri->toString(array('path', 'query', 'fragment')); } elseif ($this->params->get('pro_cookielessdomain_enable', '0') && preg_match('#\\.(?>' . $sFontFiles . ')#', $oImageUri->getPath())) { $oUri = clone JchPlatformUri::getInstance(); $sImageUrl = '//' . $oUri->toString(array('host', 'port')) . $oImageUri->toString(array('path')); } else { $sImageUrl = $oImageUri->toString(array('path')); } } } $sImageUrl = preg_match('#(?<!\\\\)[\\s\'"(),]#', $sImageUrl) ? '"' . $sImageUrl . '"' : $sImageUrl; return $sImageUrl; }
/** * Generates sprite image and return background positions for image replaced with sprite * * @param array $aMatches Array of css declarations and image url to be included in sprite * @param object $oSpriteGen Object of sprite generator * @return array */ public function generateSprite($aMatches, $oSpriteGen) { $aDeclaration = $aMatches[0]; $aImages = $aMatches[1]; $oSpriteGen->CreateSprite($aImages); $aSpriteCss = $oSpriteGen->GetCssBackground(); $aPatterns = array(); $aPatterns[0] = '#background-image:#'; // Background image declaration regex $aPatterns[1] = '#(background:[^;}]*)\\b' . '((?:top|bottom|left|right|center|-?[0-9]+(?:%|[c-x]{2})?)' . '\\s(?:top|bottom|left|right|center|-?[0-9]+(?:%|[c-x]{2})?))([^;}]*[;}])#'; $aPatterns[2] = '#url\\((?=[^\\)]+\\.(?:png|gif|jpe?g))[^\\)]+\\)#'; //Background image regex $aPatterns[3] = '#background-position:[^;}]+;?#'; //Background position declaration regex $sSpriteName = $oSpriteGen->GetSpriteFilename(); $aSearch = array(); for ($i = 0; $i < count($aSpriteCss); $i++) { if (isset($aSpriteCss[$i])) { $aSearch['needles'][] = $aDeclaration[$i]; $spritepath = JchOptimizeHelper::cookieLessDomain($this->params) . JchPlatformPaths::spriteDir(TRUE) . $sSpriteName; $aReplacements = array(); $aReplacements[0] = 'background:'; $aReplacements[1] = '$1$3'; $aReplacements[2] = 'url(' . $spritepath . ') ' . $aSpriteCss[$i]; $aReplacements[3] = ''; $sReplacement = preg_replace($aPatterns, $aReplacements, $aDeclaration[$i]); if (is_null($sReplacement)) { throw new Exception(JchPlatformUtility::translate('Error finding replacements for sprite background positions')); } $aSearch['replacements'][] = $sReplacement; } } return $aSearch; }
/** * Returns url of aggregated file * * @param string $sFile Aggregated file name * @param string $sType css or js * @param mixed $bGz True (or 1) if gzip set and enabled * @param number $sTime Expire header time * @return string Url of aggregated file */ protected function buildUrl($sId, $sType) { $sPath = JchPlatformPaths::assetPath(); $iTime = (int) $this->params->get('lifetime', '30'); $bGz = $this->isGZ(); if ($this->params->get('htaccess', 0) == 1) { $sUrl = JchOptimizeHelper::cookieLessDomain($this->params) . $sPath . JchPlatformPaths::rewriteBase() . ($bGz ? 'gz/' : 'nz/') . $iTime . '/JCHI/' . $sId . '.' . $sType; } else { $oUri = clone JchPlatformUri::getInstance(); $oUri->setPath($sPath . '2/jscss.php'); $aVar = array(); $aVar['f'] = $sId; $aVar['type'] = $sType; $aVar['gz'] = $bGz ? 'gz' : 'nz'; $aVar['d'] = $iTime; $aVar['i'] = 'JCHI'; $oUri->setQuery($aVar); $sUrl = htmlentities($oUri->toString(array('path', 'query'))); } return $sUrl; }
/** * * @return type */ protected function cookieLessDomain() { if (JchOptimizeHelper::cookieLessDomain($this->params)) { return JchOptimizeHelper::cookieLessDomain($this->params) . JchPlatformPaths::assetPath(TRUE); } return JchPlatformPaths::assetPath(); }
/** * * @return type */ public function lazyLoadImages() { if ($this->params->get('pro_lazyload', '0')) { JCH_DEBUG ? JchPlatformProfiler::start('LazyLoadImages') : null; $sLazyLoadBodyHtml = preg_replace($this->getLazyLoadRegex(), 'data-src="$1" src="' . JchOptimizeHelper::cookieLessDomain($this->params) . JchPlatformPaths::imageFolder() . 'placeholder.gif" data-jchll="true"', $this->getBodyHtml()); if (is_null($sLazyLoadBodyHtml)) { JchOptimizeLogger::log(JchPlatformUtility::translate('Lazy load images function failed'), $this->params); return; } if (preg_match($this->getBodyRegex(), $sLazyLoadBodyHtml, $aBodyMatches) === FALSE || empty($aBodyMatches)) { JchOptimizeLogger::log(JchPlatformUtility::translate('Failed retrieving body in lazy load images function'), $this->params); return; } $this->sBodyHtml = $aBodyMatches[0]; JCH_DEBUG ? JchPlatformProfiler::stop('LazyLoadImages', TRUE) : null; } }