/** * Callback function used to remove urls of css and js files in head tags * * @param array $aMatches Array of all matches * @return string Returns the url if excluded, empty string otherwise */ public function replaceScripts($aMatches, $aCBArgs) { $sUrl = $aMatches['url'] = isset($aMatches[1]) && $aMatches[1] != '' ? $aMatches[1] : (isset($aMatches[3]) ? $aMatches[3] : ''); $sDeclaration = isset($aMatches[2]) && $aMatches[2] != '' ? $aMatches[2] : (isset($aMatches[4]) ? $aMatches[4] : ''); if (preg_match('#^<!--#', $aMatches[0]) || JchOptimizeUrl::isInvalid($sUrl) && trim($sDeclaration) == '') { return $aMatches[0]; } $sType = preg_match('#^<script#i', $aMatches[0]) ? 'js' : 'css'; if ($sType == 'js' && !$this->params->get('javascript', '1')) { return $aMatches[0]; } if ($sType == 'css' && !$this->params->get('css', '1')) { return $aMatches[0]; } $this->bPreserveOrder = (bool) (!($sType == 'css' && $this->params->get('pro_optimizeCssDelivery_enable', '0') || $this->params->get('bottom_js', '0') || $sType == 'js' && $this->params->get('bottom_js', '0') == '1')); $aExcludes = array(); if (isset($aCBArgs['excludes'])) { $aExcludes = $aCBArgs['excludes']; } $aRemovals = array(); if (isset($aCBArgs['removals'])) { $aRemovals = $aCBArgs['removals']; } $sMedia = ''; if ($sType == 'css' && preg_match('#media=(?(?=["\'])(?:["\']([^"\']+))|(\\w+))#i', $aMatches[0], $aMediaTypes) > 0) { $sMedia .= $aMediaTypes[1] ? $aMediaTypes[1] : $aMediaTypes[2]; } switch (TRUE) { case $sUrl != '' && !$this->isHttpAdapterAvailable($sUrl): case $sUrl != '' && JchOptimizeUrl::isSSL($sUrl) && !extension_loaded('openssl'): case $sUrl != '' && !JchOptimizeUrl::isHttpScheme($sUrl): case $sUrl != '' && !empty($aExcludes[$sType]) && JchOptimizeHelper::findExcludes($aExcludes[$sType], $sUrl): case $sDeclaration != '' && $this->excludeDeclaration($sType): case $sDeclaration != '' && JchOptimizeHelper::findExcludes($aExcludes[$sType . '_script'], $sDeclaration, $sType): case $sUrl != '' && $this->excludeExternalExtensions($sUrl): $this->{'bExclude_' . $sType} = TRUE; return $aMatches[0]; case $sUrl != '' && $this->isDuplicated($sUrl): case $sUrl != '' && !empty($aRemovals[$sType]) && JchOptimizeHelper::findExcludes($aRemovals[$sType], $sUrl): return ''; default: $return = ''; if ($this->{'bExclude_' . $sType} && $this->bPreserveOrder) { $this->{'bExclude_' . $sType} = FALSE; $iIndex = ++$this->{'iIndex_' . $sType}; $return = '<JCH_' . strtoupper($sType) . $iIndex . '>'; } elseif (!$this->bPreserveOrder) { $iIndex = 0; } else { $iIndex = $this->{'iIndex_' . $sType}; } $array = array(); $array['match'] = $aMatches[0]; if ($sUrl == '' && trim($sDeclaration) != '') { $content = JchOptimize\HTML_Optimize::cleanScript($sDeclaration, $sType); $array['content'] = $content; } else { $array['url'] = $sUrl; } if ($this->sFileHash != '') { $array['id'] = $this->getFileID($aMatches); } if ($sType == 'css') { $array['media'] = $sMedia; } $this->aLinks[$sType][$iIndex][] = $array; return $return; } }
/** * 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; }
/** * * @param type $aImgs */ public function getCachedImgAttributes($aImgs) { $aImgAttributes = array(); $total = count($aImgs[0]); for ($i = 0; $i < $total; $i++) { $sUrl = !empty($aImgs[1][$i]) ? $aImgs[1][$i] : (!empty($aImgs[2][$i]) ? $aImgs[2][$i] : $aImgs[3][$i]); if (JchOptimizeUrl::isInvalid($sUrl) || !$this->oParser->isHttpAdapterAvailable($sUrl) || JchOptimizeUrl::isSSL($sUrl) && !extension_loaded('openssl') || !JchOptimizeUrl::isHttpScheme($sUrl)) { $aImgAttributes[] = $aImgs[0][$i]; continue; } $sPath = JchOptimizeHelper::getFilePath($sUrl); if (file_exists($sPath)) { $aSize = getimagesize($sPath); if ($aSize === false || empty($aSize) || $aSize[0] == '1' && $aSize[1] == '1') { $aImgAttributes[] = $aImgs[0][$i]; continue; } $sImg = preg_replace('#(?:width|height)\\s*+=(?:\\s*+"([^">]*+)"|\\s*+\'([^\'>]*+)\'|([^\\s>]++))#i', '', $aImgs[0][$i]); $aImgAttributes[] = preg_replace('#\\s*+/?>#', ' ' . $aSize[3] . ' />', $sImg); } else { $aImgAttributes[] = $aImgs[0][$i]; continue; } } return $aImgAttributes; }