/** * * @param type $aUrl * @param type $sType * @param type $oFileRetriever * @return type * @throws Exception */ public function cacheContent($aUrl, $sType, $oFileRetriever, $oCssParser, $bPrepare) { $sContent = ''; if (isset($aUrl['url'])) { $sPath = JchOptimizeHelper::getFilePath($aUrl['url']); $sContent .= $oFileRetriever->getFileContents($sPath); } else { if ($this->sAsyncUrl == '') { $sContent .= $aUrl['content']; } } if ($sType == 'css') { $sContent = $oCssParser->addRightBrace($sContent); $oCssParser->aUrl = $aUrl; $sImportContent = preg_replace('#@import\\s(?:url\\()?[\'"]([^\'"]+)[\'"](?:\\))?#', '@import url($1)', $sContent); if (is_null($sImportContent)) { JchOptimizeLogger::log(sprintf(JchPlatformUtility::translate('Error occured trying to parse for @imports in %s'), $aUrl['url']), $this->params); $sImportContent = $sContent; } $sContent = $sImportContent; unset($sImportContent); $sContent = $oCssParser->correctUrl($sContent, $aUrl); $sContent = $this->replaceImports($sContent, $aUrl); $sContent = $oCssParser->handleMediaQueries($sContent, $aUrl['media']); if (function_exists('mb_convert_encoding')) { $sContent = mb_convert_encoding($sContent, 'utf-8'); } } if ($sType == 'js' && trim($sContent) != '') { if ($this->params->get('try_catch', '1')) { $sContent = $this->addErrorHandler($sContent, $aUrl); } else { $sContent = $this->addSemiColon($sContent, $aUrl); } } if ($bPrepare) { $sContent = $this->minifyContent($sContent, $sType, $aUrl); $sContent = $this->prepareContents($sContent); } return $sContent; }
/** * 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 */ protected function replaceScripts($aMatches) { if ((!isset($aMatches[1]) || trim($aMatches[1]) == '') && (!isset($aMatches[3]) || trim($aMatches[3]) == '')) { return $aMatches[0]; } $sType = $this->aCallbackArgs['type']; $sEditor = JchOptimizeHelper::getEditorName(); $sUrl = $aMatches[1]; $sFile = isset($aMatches[2]) ? $aMatches[2] : ''; $sDeclaration = isset($aMatches[3]) ? $aMatches[3] : ''; $aExcludes = array(); $sPath = ''; //global $_PROFILER; if (isset($this->aCallbackArgs['excludes'])) { $aExcludes = $this->aCallbackArgs['excludes']; } $aExcludes['script'] = array_map(function ($sScript) { return stripslashes($sScript); }, $aExcludes['script']); $aExcludes['script'] = array_merge($aExcludes['script'], array('document.write', 'var mapconfig90')); $aExcludes['js'] = array_merge($aExcludes['js'], array('.com/maps/api/js', '.com/jsapi', '.com/uds', 'plugin_googlemap3', '.com/recaptcha/api')); if ($sUrl != '') { $sPath .= JchOptimizeHelper::getFilePath($sUrl); } $sMedia = ''; if ($sType == 'css' && preg_match('#media=(?(?=["\'])(?:["\']([^"\']+))|(\\w+))#i', $aMatches[0], $aMediaTypes) > 0) { $sMedia .= $aMediaTypes[1] ? $aMediaTypes[1] : $aMediaTypes[2]; } //JCH_DEBUG ? $_PROFILER->mark('beforeReplaceScript - ' . $sFile . ' plgSystem (JCH Optimize)') : null; switch (TRUE) { case isset($aExcludes['if']) && in_array($aMatches[0], $aExcludes['if']): case $sUrl != '' && !empty($aExcludes[$sType]) && JchOptimizeHelper::findExcludes($aExcludes[$sType], $sUrl): case $sEditor == 'artofeditor' && $sFile == 'ckeditor.js': case $sType == 'js' && $sUrl != '' && $this->isEditorsExcluded($sUrl): case $sUrl != '' && $this->isUrlFopenAllowed($sUrl): case $sUrl != '' && preg_match('#^https#', $sUrl) && !extension_loaded('openssl'): case $sUrl != '' && preg_match('#^data:#', $sUrl): case $sDeclaration != '' && JchOptimizeHelper::findExcludes($aExcludes['script'], $sDeclaration, TRUE): //JCH_DEBUG ? $_PROFILER->mark('afterReplaceScript - ' . $sFile . ' plgSystem (JCH Optimize)') : null; return $aMatches[0]; case $sUrl == '' && trim($sDeclaration) != '': $content = str_replace(array('<!--', '-->'), '', $sDeclaration); $this->aLinks[$sType][] = array('content' => $content, 'match' => $aMatches[0]); //JCH_DEBUG ? $_PROFILER->mark('afterReplaceScript - ' . $sFile . ' plgSystem (JCH Optimize)') : null; return ''; case $this->aCallbackArgs['type'] == 'js': $this->aCallbackArgs['counter']++; $this->aLinks[$sType][] = array('url' => $sUrl, 'file' => $sFile, 'match' => $aMatches[0], 'path' => $sPath); //JCH_DEBUG ? $_PROFILER->mark('afterReplaceScript - ' . $sFile . ' plgSystem (JCH Optimize)') : null; return '<JCH_SCRIPT>'; case $this->aCallbackArgs['type'] == 'css': $this->aLinks[$sType][] = array('url' => $sUrl, 'file' => $sFile, 'media' => $sMedia, 'match' => $aMatches[0], 'path' => $sPath); //JCH_DEBUG ? $_PROFILER->mark('afterReplaceScript - ' . $sFile . ' plgSystem (JCH Optimize)') : null; return ''; default: JchOptimizeLogger::log(JText::_('Unknown match type')); return $aMatches[0]; } }
/** * * @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 (empty($sUrl) || !$this->oParser->isHttpAdapterAvailable($sUrl) || preg_match('#^https#', $sUrl) && !extension_loaded('openssl') || preg_match('#^data:#', $sUrl)) { $aImgAttributes[] = $aImgs[0][$i]; continue; } $sPath = JchOptimizeHelper::getFilePath($sUrl); $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); } return $aImgAttributes; }
/** * 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, $sSection) { $sUrl = isset($aMatches[1]) && $aMatches[1] != '' ? $aMatches[1] : (isset($aMatches[4]) ? $aMatches[4] : ''); $sFile = isset($aMatches[2]) && $aMatches[2] != '' ? $aMatches[2] : (isset($aMatches[5]) ? $aMatches[5] : ''); $sDeclaration = isset($aMatches[3]) && $aMatches[3] != '' ? $aMatches[3] : (isset($aMatches[6]) ? $aMatches[6] : ''); if (preg_match('#^<!--#', $aMatches[0]) || (trim($sUrl) == '' || trim($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->enableCssCompression()) { return $aMatches[0]; } $this->bPreserveOrder = (bool) (!($sType == 'css' && $this->params->get('pro_optimizeCssDelivery', '0') || $this->params->get('bottom_js', '0') || $sType == 'js' && $this->params->get('bottom_js', '0') == '1')); $aExcludes = array(); $sPath = ''; if (isset($aCBArgs['excludes'])) { $aExcludes = $aCBArgs['excludes']; } $aExcludes['script'] = array_map(function ($sScript) { return stripslashes($sScript); }, $aExcludes['script']); $aExcludes['js'] = array_merge($aExcludes['js'], array('.com/maps/api/js', '.com/jsapi', '.com/uds', 'typekit.net'), JchPlatformExcludes::head('js')); $aExcludes['css'] = array_merge($aExcludes['css'], array('fonts.googleapis.com'), JchPlatformExcludes::head('css')); if ($sSection == 'body') { $aExcludes['script'] = array_merge($aExcludes['script'], array('document.write'), JchPlatformExcludes::body('js', 'script')); $aExcludes['js'] = array_merge($aExcludes['js'], array('.com/recaptcha/api'), JchPlatformExcludes::body('js')); } if ($sUrl != '') { $sPath .= JchOptimizeHelper::getFilePath($sUrl); } $sMedia = ''; if ($sType == 'css' && preg_match('#media=(?(?=["\'])(?:["\']([^"\']+))|(\\w+))#i', $aMatches[0], $aMediaTypes) > 0) { $sMedia .= $aMediaTypes[1] ? $aMediaTypes[1] : $aMediaTypes[2]; } //JCH_DEBUG ? JchPlatformProfiler::mark('beforeReplaceScript - ' . $sFile . ' plgSystem (JCH Optimize)') : null; switch (TRUE) { case $sUrl != '' && $this->isDuplicated($sUrl): $this->{'bExclude_' . $sType} = FALSE; return ''; case $sUrl != '' && !empty($aExcludes[$sType]) && JchOptimizeHelper::findExcludes($aExcludes[$sType], $sUrl): case $sPath != '' && $this->isHttpAdapterAvailable($sPath): case $sUrl != '' && preg_match('#^https#', $sUrl) && !extension_loaded('openssl'): case $sUrl != '' && preg_match('#^data:#', $sUrl): case $sDeclaration != '' && $this->excludeDeclaration($sType): case $sDeclaration != '' && JchOptimizeHelper::findExcludes($aExcludes['script'], $sDeclaration, TRUE): case $sUrl != '' && $this->excludeExternalExtensions($sUrl): //JCH_DEBUG ? JchPlatformProfiler::mark('afterReplaceScript - ' . $sFile . ' plgSystem (JCH Optimize)') : null; $this->{'bExclude_' . $sType} = TRUE; return $aMatches[0]; 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); $array['content'] = $content; $id = $content; } else { $array['url'] = $sUrl; $array['path'] = $sPath; $array['file'] = $sFile; $id = $sUrl; } if ($this->sFileHash != '') { $array['id'] = md5($this->sFileHash . $id); } if ($sType == 'css') { $array['media'] = $sMedia; } $this->aLinks[$sType][$iIndex][] = $array; return $return; } }