/** * Optimize website by aggregating css and js * */ public function process($sHtml) { JCH_DEBUG ? JchPlatformProfiler::start('Process', TRUE) : null; JCH_DEBUG ? JchPlatformProfiler::start('LoadClass') : null; loadJchOptimizeClass(array('JchOptimizeBase', 'JchOptimizeParser', 'JchOptimizeFileRetriever', 'JchOptimizeLinkBuilder', 'JchOptimizeHelper')); JCH_DEBUG ? JchPlatformProfiler::stop('LoadClass', TRUE) : null; try { $oParser = new JchOptimizeParser($this->params, $sHtml, JchOptimizeFileRetriever::getInstance()); $oLinkBuilder = new JchOptimizeLinkBuilder($oParser); $oLinkBuilder->insertJchLinks(); $oParser->runCookieLessDomain(); $oParser->lazyLoadImages(); $this->params->set('isXhtml', $oLinkBuilder->isXhtml()); $this->params->set('isHtml5', $oParser->isHtml5()); $sOptimizedHtml = JchOptimizeHelper::minifyHtml($oParser->getHtml(), $this->params); } catch (Exception $ex) { JchOptimizeLogger::log($ex->getMessage(), $this->params); $sOptimizedHtml = $sHtml; } spl_autoload_unregister('loadJchOptimizeClass'); JCH_DEBUG ? JchPlatformProfiler::stop('Process', TRUE) : null; JCH_DEBUG ? JchPlatformProfiler::attachProfiler($sOptimizedHtml) : null; if (version_compare(PHP_VERSION, '7', '>=')) { ini_set('pcre.jit', $this->jit); } return $sOptimizedHtml; }
/** * Optimize website by aggregating css and js * */ public function process($sHtml) { JCH_DEBUG ? JchPlatformProfiler::mark('beforeProcess plgSystem (JCH Optimize)') : null; loadJchOptimizeClass(array('JchOptimizeBase', 'JchOptimizeParser', 'JchOptimizeFileRetriever', 'JchOptimizeLinkBuilder', 'JchOptimizeHelper')); JCH_DEBUG ? JchPlatformProfiler::mark('afterLoadClass plgSystem (JCH Optimize)') : null; try { $oParser = new JchOptimizeParser($this->params, $sHtml, JchOptimizeFileRetriever::getInstance()); $oLinkBuilder = new JchOptimizeLinkBuilder($oParser); $oLinkBuilder->insertJchLinks(); $sOptimizedHtml = JchOptimizeHelper::minifyHtml($oParser->getHtml(), $this->params); } catch (Exception $ex) { JchOptimizeLogger::log($ex->getMessage(), $this->params); $sOptimizedHtml = $sHtml; } spl_autoload_unregister('loadJchOptimizeClass'); JCH_DEBUG ? JchPlatformProfiler::mark('afterProcess plgSystem (JCH Optimize)') : null; return $sOptimizedHtml; }
/** * * @param type $oObj * @param type $sCss * @return type */ public function generateAdminLinks($oObj, $sCss) { JCH_DEBUG ? JchPlatformProfiler::start('GenerateAdminLinks') : null; $params = clone $this->params; $params->set('javascript', '1'); $params->set('css', '1'); $params->set('excludeAllExtensions', '0'); $params->set('css_minify', '0'); $params->set('debug', '0'); $params->set('bottom_js', '2'); ##<procode>## $params->set('pro_phpAndExternal', '1'); $params->set('pro_inlineScripts', '1'); $params->set('pro_lazyload', '0'); ##</procode>## $sHtml = $oObj->getOriginalHtml(); $oParser = new JchOptimizeParser($params, $sHtml, JchOptimizeFileRetriever::getInstance()); $aLinks = $oParser->getReplacedFiles(); if ($sCss == '' && !empty($aLinks['css'][0])) { $oCombiner = new JchOptimizeCombiner($params, $this->bBackend); $oCssParser = new JchOptimizeCssParser($params, $this->bBackend); $oCombiner->combineFiles($aLinks['css'][0], 'css', $oCssParser); $sCss = $oCombiner->css; } $oSpriteGenerator = new JchOptimizeSpriteGenerator($params); $aLinks['images'] = $oSpriteGenerator->processCssUrls($sCss, TRUE); ##<procode>## $sRegex = $oParser->getLazyLoadRegex(); preg_match_all($sRegex, $oParser->getBodyHtml(), $aMatches); $aLinks['lazyload'] = $aMatches[1]; ##</procode>## JCH_DEBUG ? JchPlatformProfiler::stop('GenerateAdminLinks', TRUE) : null; return $aLinks; }
/** * * @param type $oObj * @param type $sCss * @return type */ public function generateAdminLinks($oObj, $sCss) { JCH_DEBUG ? JchPlatformProfiler::start('GenerateAdminLinks') : null; $params = clone $this->params; $params->set('combine_files_enable', '1'); $params->set('javascript', '1'); $params->set('css', '1'); $params->set('gzip', '0'); $params->set('css_minify', '0'); $params->set('js_minify', '0'); $params->set('html_minify', '0'); $params->set('defer_js', '0'); $params->set('debug', '0'); $params->set('bottom_js', '2'); $params->set('includeAllExtensions', '1'); $params->set('excludeCss', array()); $params->set('excludeJs', array()); $params->set('excludeCssComponents', array()); $params->set('excludeJsComponents', array()); $params->set('csg_exclude_images', array()); $params->set('csg_include_images', array()); $sHtml = $oObj->getOriginalHtml(); $oParser = new JchOptimizeParser($params, $sHtml, JchOptimizeFileRetriever::getInstance()); $aLinks = $oParser->getReplacedFiles(); if ($sCss == '' && !empty($aLinks['css'][0])) { $oCombiner = new JchOptimizeCombiner($params, $this->bBackend); $oCssParser = new JchOptimizeCssParser($params, $this->bBackend); $oCombiner->combineFiles($aLinks['css'][0], 'css', $oCssParser); $sCss = $oCombiner->css; } $oSpriteGenerator = new JchOptimizeSpriteGenerator($params); $aLinks['images'] = $oSpriteGenerator->processCssUrls($sCss, TRUE); JCH_DEBUG ? JchPlatformProfiler::stop('GenerateAdminLinks', TRUE) : null; return $aLinks; }
/** * * @return type */ public static function getInstance() { if (!self::$instance) { self::$instance = new JchOptimizeFileRetriever(); } return self::$instance; }
public function getOriginalHtml() { JCH_DEBUG ? JchPlatformProfiler::mark('beforeGetHtml') : null; $url = home_url() . '/?jchbackend=1'; try { $oFileRetriever = JchOptimizeFileRetriever::getInstance(); $response = $oFileRetriever->getFileContents($url); if ($oFileRetriever->response_code != 200) { throw new Exception(JchPlatformUtility::translate('Failed fetching front end HTML with response code ' . $oFileRetriever->response_code)); } JCH_DEBUG ? JchPlatformProfiler::mark('afterGetHtml') : null; return $response; } catch (Exception $e) { JchOptimizeLogger::log($url . ': ' . $e->getMessage(), $this->params); JCH_DEBUG ? JchPlatformProfiler::mark('afterGetHtml)') : null; throw new RunTimeException(_('Load or refresh the front-end site first then refresh this page ' . 'to populate the multi select exclude lists.')); } }
/** * Optimize website by aggregating css and js * */ public function process($sHtml) { JCH_DEBUG ? JchPlatformProfiler::start('Process', TRUE) : null; JCH_DEBUG ? JchPlatformProfiler::start('LoadClass') : null; loadJchOptimizeClass(array('JchOptimizeBase', 'JchOptimizeParser', 'JchOptimizeFileRetriever', 'JchOptimizeLinkBuilder', 'JchOptimizeHelper')); JCH_DEBUG ? JchPlatformProfiler::stop('LoadClass', TRUE) : null; try { $oParser = new JchOptimizeParser($this->params, $sHtml, JchOptimizeFileRetriever::getInstance()); $oLinkBuilder = new JchOptimizeLinkBuilder($oParser); $oLinkBuilder->insertJchLinks(); $this->params->set('xhtml', $oLinkBuilder->isXhtml()); $sOptimizedHtml = JchOptimizeHelper::minifyHtml($oParser->getHtml(), $this->params); } catch (Exception $ex) { JchOptimizeLogger::log($ex->getMessage(), $this->params); $sOptimizedHtml = $sHtml; } spl_autoload_unregister('loadJchOptimizeClass'); JCH_DEBUG ? JchPlatformProfiler::stop('Process', TRUE) : null; JCH_DEBUG ? JchPlatformProfiler::attachProfiler($sOptimizedHtml) : null; return $sOptimizedHtml; }
/** * * @param type $oObj * @param type $sCss * @return type */ public function generateAdminLinks($oObj, $sCss) { JCH_DEBUG ? JchPlatformProfiler::mark('beforeGenerateAdminLinks plgSystem (JCH Optimize)') : null; $params = clone $this->params; $params->set('javascript', '1'); $params->set('css', '1'); $params->set('excludeAllExtensions', '0'); $params->set('css_minify', '0'); $params->set('debug', '0'); $params->set('bottom_js', '2'); $sHtml = $oObj->getOriginalHtml(); $oParser = new JchOptimizeParser($params, $sHtml, JchOptimizeFileRetriever::getInstance()); $aLinks = $oParser->getReplacedFiles(); if ($sCss == '') { $oCombiner = new JchOptimizeCombiner($params, $this->bBackend); $oCssParser = new JchOptimizeCssParser($params, $this->bBackend); $oCombiner->combineFiles($aLinks['css'][0], 'css', $oCssParser); $sCss = $oCombiner->css; } $oSpriteGenerator = new JchOptimizeSpriteGenerator($params); $aLinks['images'] = $oSpriteGenerator->processCssUrls($sCss, TRUE); JCH_DEBUG ? JchPlatformProfiler::mark('afterGenerateAdminLinks plgSystem (JCH Optimize)') : null; return $aLinks; }
/** * * @staticvar string $sContents * @return boolean */ public static function checkModRewriteEnabled($params) { $oFileRetriever = JchOptimizeFileRetriever::getInstance(); if (!$oFileRetriever->isHttpAdapterAvailable()) { $params->set('htaccess', '0'); } else { $oUri = JchPlatformUri::getInstance(); $sUrl = $oUri->toString(array('scheme', 'user', 'pass', 'host', 'port')) . JchPlatformPaths::assetPath() . JchPlatformPaths::rewriteBase() . 'test_mod_rewrite'; $sContents = $oFileRetriever->getFileContents($sUrl); if ($sContents == 'TRUE') { $params->set('htaccess', '1'); } else { $params->set('htaccess', '0'); } } JchPlatformPlugin::saveSettings($params); }
/** * */ private function getHttpAdapter() { if (is_null(self::$oHttpAdapter)) { self::$oHttpAdapter = JchPlatformHttp::getHttpAdapter(); } return self::$oHttpAdapter; }
/** * * @staticvar string $sContents * @return boolean */ public static function checkModRewriteEnabled($params) { JCH_DEBUG ? JchPlatformProfiler::start('CheckModRewriteEnabled') : null; $oFileRetriever = JchOptimizeFileRetriever::getInstance(); if (!$oFileRetriever->isHttpAdapterAvailable()) { $params->set('htaccess', 0); } else { $oUri = JchPlatformUri::getInstance(); $sUrl = $oUri->toString(array('scheme', 'user', 'pass', 'host', 'port')) . JchPlatformPaths::assetPath(TRUE); $sUrl2 = JchPlatformPaths::rewriteBase() . 'test_mod_rewrite'; try { $sContents = $oFileRetriever->getFileContents($sUrl . $sUrl2); if ($sContents == 'TRUE') { $params->set('htaccess', 1); } else { $sContents2 = $oFileRetriever->getFileContents($sUrl . '3' . $sUrl2); if ($sContents2 == 'TRUE') { $params->set('htaccess', 3); } else { $params->set('htaccess', 0); } } } catch (Exception $e) { $params->set('htaccess', 0); } } JchPlatformPlugin::saveSettings($params); JCH_DEBUG ? JchPlatformProfiler::stop('CheckModRewriteEnabled', TRUE) : null; }
/** * * @staticvar string $sContents * @return boolean */ public static function modRewriteEnabled() { if (function_exists('apache_get_modules')) { return in_array('mod_rewrite', apache_get_modules()); } elseif (!ini_get('open_basedir') && function_exists('shell_exec') && file_exists('/usr/local/apache/bin/apachectl')) { return strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false; } else { static $sContents = ''; if ($sContents == '') { $oFileRetriever = JchOptimizeFileRetriever::getInstance($GLOBALS['oParams']); if (!$oFileRetriever->isUrlFOpenAllowed()) { return FALSE; } $sJbase = JUri::base(true); $sBaseFolder = $sJbase == '/' ? $sJbase : $sJbase . '/'; $sUrl = JUri::base() . 'plugins/system/jch_optimize/assets' . $sBaseFolder . 'test_mod_rewrite'; $sContents = $oFileRetriever->getFileContents($sUrl); } if ($sContents == 'TRUE') { return TRUE; } else { return FALSE; } } }
/** * Aggregate contents of CSS and JS files * * @param array $aUrlArray Array of links of files * @param string $sType CSS or js * @return string Aggregarted contents * @throws Exception */ public function combineFiles($aUrlArray, $sType, $oCssParser) { $sContents = ''; $sLifetime = (int) $this->params->get('lifetime', '30') * 24 * 60 * 60; $this->bAsync = FALSE; $this->sAsyncUrl = ''; $oFileRetriever = JchOptimizeFileRetriever::getInstance(); foreach ($aUrlArray as $aUrl) { $sUrl = $this->prepareFileUrl($aUrl, $sType); JCH_DEBUG ? JchPlatformProfiler::start('CombineFile - ' . $sUrl) : null; if ($sType == 'js') { $sJsContents = $this->handleAsyncUrls($aUrl); if ($sJsContents === FALSE) { continue; } $sContents .= $sJsContents; unset($sJsContents); } if (isset($aUrl['id']) && $aUrl['id'] != '') { $function = array($this, 'cacheContent'); $args = array($aUrl, $sType, $oFileRetriever, $oCssParser, TRUE); $sCachedContent = JchPlatformCache::getCallbackCache($aUrl['id'], $sLifetime, $function, $args); $this->{$sType} .= $sCachedContent; if (!isset($aUrl['url']) && $this->sAsyncUrl != '' && $sType == 'js') { $this->sAsyncUrl = ''; } if ($this->sAsyncUrl == '') { $sContents .= $this->addCommentedUrl($sType, $aUrl) . '[[JCH_' . $aUrl['id'] . ']]' . $this->sLnEnd . 'DELIMITER'; } } else { $sContent = $this->cacheContent($aUrl, $sType, $oFileRetriever, $oCssParser, FALSE); $sContents .= $this->addCommentedUrl($sType, $aUrl) . $sContent . '|"LINE_END"|'; } JCH_DEBUG ? JchPlatformProfiler::stop('CombineFile - ' . $sUrl, TRUE) : null; } if ($this->bAsync) { $sContents = $this->getLoadScript() . $sContents; if ($this->sAsyncUrl != '') { $sContents .= $this->addCommentedUrl('js', $this->sAsyncUrl) . 'loadScript("' . $this->sAsyncUrl . '", function(){}); DELIMITER'; $this->sAsyncUrl = ''; } } return $sContents; }
/** * * @return type */ protected function getInput() { $oFileRetriever = JchOptimizeFileRetriever::getInstance($this->getParams()); if ($GLOBALS['bTextArea']) { return parent::getInput(); } if (!$oFileRetriever->isUrlFOpenAllowed()) { $GLOBALS['bTextArea'] = TRUE; JFactory::getApplication()->enqueueMessage(JText::_('Your host does not have cURL or allow_url_fopen enabled. ' . 'Will use text areas instead of multiselect options for exclude settings'), 'notice'); return parent::getInput(); } try { $aOptions = $this->getFieldOptions(); } catch (Exception $ex) { JFactory::getApplication()->enqueueMessage($ex->getMessage(), 'warning'); return parent::getInput(); } $sField = JHTML::_('select.genericlist', $aOptions, $this->name . '[]', 'class="inputbox" multiple="multiple" size="5"', 'id', 'name', $this->value, $this->id); return $sField; }
/** * Aggregate contents of CSS and JS files * * @param array $aUrlArray Array of links of files * @param string $sType CSS or js * @return string Aggregarted contents * @throws Exception */ public function combineFiles($aUrlArray, $sType, $oCssParser) { global $_PROFILER; JCH_DEBUG ? JchPlatformProfiler::mark('beforeCombineFiles - ' . $sType . ' plgSystem (JCH Optimize)') : null; $sContents = ''; $sLifetime = (int) $this->params->get('lifetime', '30') * 24 * 60 * 60; $this->bAsync = FALSE; $this->sAsyncUrl = ''; $this->{$sType} = ''; $oFileRetriever = JchOptimizeFileRetriever::getInstance(); foreach ($aUrlArray as $aUrl) { if ($sType == 'js') { $sJsContents = $this->handleAsyncUrls($aUrl); if ($sJsContents === FALSE) { continue; } $sContents .= $sJsContents; unset($sJsContents); } if (isset($aUrl['id']) && $aUrl['id'] != '') { $function = array($this, 'cacheContent'); $args = array($aUrl, $sType, $oFileRetriever, $oCssParser); $sCachedContent = JchPlatformCache::getCallbackCache($aUrl['id'], $sLifetime, $function, $args); $this->{$sType} .= $sCachedContent; if (!isset($aUrl['url']) && $this->sAsyncUrl != '' && $sType == 'js') { $this->sAsyncUrl = ''; } if ($this->sAsyncUrl == '') { $sContents .= $this->addCommentedUrl($sType, $aUrl) . '[[JCH_' . $aUrl['id'] . ']]' . 'DELIMITER'; } } else { $sContent = $this->cacheContent($aUrl, $sType, $oFileRetriever, $oCssParser); $sContents .= $this->addCommentedUrl($sType, $aUrl) . $sContent . 'DELIMITER'; } } if ($this->bAsync) { $sContents = $this->getLoadScript() . $sContents; if ($this->sAsyncUrl != '') { $sContents .= $this->addCommentedUrl('js', $this->sAsyncUrl) . 'loadScript("' . $this->sAsyncUrl . '", function(){}); DELIMITER'; $this->sAsyncUrl = ''; } } JCH_DEBUG ? JchPlatformProfiler::mark('afterCombineFiles - ' . $sType . ' plgSystem (JCH Optimize)') : null; return $sContents; }
/** * Aggregate contents of CSS and JS files * * @param array $aUrlArray Array of links of files * @param string $sType CSS or js * @return string Aggregarted contents * @throws Exception */ public function combineFiles($aUrlArray, $sType, $oCssParser, $bCssAsync = FALSE) { global $_PROFILER; JCH_DEBUG ? $_PROFILER->mark('beforeCombineFiles - ' . $sType . ' plgSystem (JCH Optimize)') : null; $sContents = ''; $bAsync = false; $sAsyncUrl = ''; $oFileRetriever = JchOptimizeFileRetriever::getInstance($this->params); foreach ($aUrlArray as $aUrl) { $sContent = ''; if (isset($aUrl['url'])) { if ($sType == 'js' && $sAsyncUrl != '') { $sContents .= $this->addCommentedUrl('js', $sAsyncUrl) . 'loadScript("' . $sAsyncUrl . '", function(){}); DELIMITER'; $sAsyncUrl = ''; } $sPath = $aUrl['path']; if ($sType == 'js' && $this->loadAsync($aUrl['url'])) { $sAsyncUrl = $aUrl['url']; $bAsync = true; continue; } else { if (!JchOptimizeHelper::fileExists($sPath)) { if ($this->params->get('log', 0)) { JchOptimizeLogger::log(JText::_('File not found: ' . $sPath)); } $sFileContents = 'COMMENT_START File does not exist COMMENT_END'; } else { JCH_DEBUG ? $_PROFILER->mark('beforegetFileContents - ' . $sPath . ' plgSystem (JCH Optimize)') : null; $sFileContents = $oFileRetriever->getFileContents($sPath); JCH_DEBUG ? $_PROFILER->mark('afterGetFileContents - ' . $sPath . ' plgSystem (JCH Optimize)') : null; } if ($sFileContents === FALSE) { throw new Exception(JText::_('Failed getting file contents from ' . $sPath)); } $sContent .= $sFileContents; unset($sFileContents); } } else { if ($sType == 'js' && $sAsyncUrl != '') { $sContents .= $this->addCommentedUrl('js', $sAsyncUrl) . 'loadScript("' . $sAsyncUrl . '", function(){' . $this->sLnEnd . $aUrl['content'] . $this->sLnEnd . '}); DELIMITER'; $sAsyncUrl = ''; } else { $sContent .= $aUrl['content']; } } if ($sType == 'css') { unset($oCssParser->sCssUrl); $oCssParser->aUrl = $aUrl; $sImportContent = preg_replace('#@import\\s(?:url\\()?[\'"]([^\'"]+)[\'"](?:\\))?#', '@import url($1)', $sContent); if (is_null($sImportContent)) { if ($this->params->get('log', 0)) { JchOptimizeLogger::log(JText::_('Error occured trying to parse for @imports in ' . $aUrl['url'])); } $sImportContent = $sContent; } $sContent = $sImportContent; $sContent = $oCssParser->correctUrl($sContent); $sContent = $this->replaceImports($sContent, $aUrl); $sContent = $oCssParser->handleMediaQueries($sContent); } if ($sType == 'js' && $sContent != '') { $sContent = $this->addSemiColon($sContent); } $sContent = $this->MinifyContent($sContent, $sType, $aUrl); $sContents .= $this->addCommentedUrl($sType, $aUrl) . $sContent . 'DELIMITER'; unset($sContent); } if ($bAsync) { $sContents = $this->getLoadScript() . $sContents; if ($sAsyncUrl != '') { $sContents .= $this->addCommentedUrl('js', $sAsyncUrl) . 'loadScript("' . $sAsyncUrl . '", function(){}); DELIMITER'; $sAsyncUrl = ''; } } JCH_DEBUG ? $_PROFILER->mark('afterCombineFiles - ' . $sType . ' plgSystem (JCH Optimize)') : null; return $sContents; }