/** * * @return string */ public static function fileTree() { $root = JchPlatformPaths::rootPath(); $dir = urldecode(JchPlatformUtility::get('dir', '', 'string', 'post')); $dir = JchPlatformUtility::decrypt($dir); $response = ''; if (file_exists($root . $dir)) { $files = scandir($root . $dir); natcasesort($files); if (count($files) > 2) { /* The 2 accounts for . and .. */ $response .= '<ul class="jqueryFileTree" style="display: none; ">'; // All dirs foreach ($files as $file) { if (file_exists($root . $dir . $file) && $file != '.' && $file != '..' && is_dir($root . $dir . $file)) { $response .= '<li class="directory collapsed"><a href="#" rel="' . JchPlatformUtility::encrypt($dir . $file . '/') . '">' . htmlentities($file) . '</a></li>'; } } // All files foreach ($files as $file) { if (file_exists($root . $dir . $file) && $file != '.' && $file != '..' && !is_dir($root . $dir . $file)) { $ext = preg_replace('/^.*\\./', '', $file); $response .= '<li class="file ext_' . $ext . '"><a href="#" rel="' . JchPlatformUtility::encrypt($dir . $file) . '">' . htmlentities($file) . '</a></li>'; } } $response .= '</ul>'; } } return $response; }
/** * */ public function garbageCron() { JCH_DEBUG ? JchPlatformProfiler::start('GarbageCron') : null; $url = JchPlatformPaths::ajaxUrl('garbagecron'); JchOptimizeHelper::postAsync($url, $this->params, array('async' => '1')); JCH_DEBUG ? JchPlatformProfiler::stop('GarbageCron', TRUE) : null; }
/** * * @param type $sPath * @return type */ public function getFileContents($sPath, $aPost = array(), $sOrigPath = '') { if (strpos($sPath, 'http') === 0 || !empty($aPost)) { if (!$this->oHttpAdapter->available()) { throw new Exception(JchPlatformUtility::translate('No Http Adapter available')); } try { $response = $this->oHttpAdapter->request($sPath, $aPost); if ($response === FALSE) { throw new RuntimeException(sprintf(JchPlatformUtility::translate('Failed getting file contents from %s'), $sPath)); } } catch (RuntimeException $ex) { JchOptimizelogger::log($sPath . ': ' . $ex->getMessage(), JchPlatformPlugin::getPluginParams()); $response['code'] = 404; } catch (BadFunctionCallException $ex) { throw new Exception($ex->getMessage()); } if ($response['code'] >= 400) { $sPath = $sOrigPath == '' ? $sPath : $sOrigPath; $sContents = $this->notFound($sPath); } else { $sContents = $response['body']; } } else { if (file_exists($sPath)) { $sContents = @file_get_contents($sPath); } elseif ($this->oHttpAdapter->available()) { $sUriPath = JchPlatformPaths::path2Url($sPath); $sContents = $this->getFileContents($sUriPath, array(), $sPath); } else { $sContents = $this->notFound($sPath); } } return $sContents; }
protected function getOptions() { $plugin = JchPlatformPlugin::getPlugin(); $pluginParams = new JRegistry(); $pluginParams->loadString($plugin->params); $pluginParams->set('sprite-path', JchPlatformPaths::spriteDir()); $CssSpriteGenClass = 'JchOptimize\\CssSpriteGen'; $CssSpriteGen = new $CssSpriteGenClass(JchOptimizeSpriteGenerator::getImageLibrary(), $pluginParams); $aSpriteFormats = $CssSpriteGen->GetSpriteFormats(); $this->default = $aSpriteFormats[0]; $options = array(); foreach ($aSpriteFormats as $sSpriteFormat) { $option = JHtml::_('select.option', $sSpriteFormat, $sSpriteFormat, 'value', 'text'); $options[] = $option; } reset($options); return $options; }
/** * * @param type $sPath * @return type */ public function getFileContents($sPath, $aPost = null, $aHeader = null, $sOrigPath = '') { $oHttpAdapter = $this->getHttpAdapter(); if (strpos($sPath, 'http') === 0) { if (!$oHttpAdapter->available()) { throw new Exception('No Http Adapter available'); } $this->response_code = 0; try { $sUserAgent = !empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; $response = $oHttpAdapter->request($sPath, $aPost, $aHeader, $sUserAgent); $this->response_code = $response['code']; if (!isset($response) || $response === FALSE) { throw new RuntimeException(sprintf('Failed getting file contents from %s', $sPath)); } } catch (RuntimeException $ex) { JchOptimizelogger::log($sPath . ': ' . $ex->getMessage(), JchPlatformPlugin::getPluginParams()); } catch (Exception $ex) { throw new Exception($sPath . ': ' . $ex->getMessage()); } if ($this->response_code != 200 && !$this->allow_400) { $sPath = $sOrigPath == '' ? $sPath : $sOrigPath; $sContents = $this->notFound($sPath); } else { $sContents = $response['body']; } } else { if (file_exists($sPath)) { $sContents = @file_get_contents($sPath); } elseif ($oHttpAdapter->available()) { $sUriPath = JchPlatformPaths::path2Url($sPath); $sContents = $this->getFileContents($sUriPath, null, null, $sPath); } else { $sContents = $this->notFound($sPath); } } return $sContents; }
/** * * @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); }
/** * * @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; }
/** * * @return string */ public static function leverageBrowserCaching() { $htaccess = JchPlatformPaths::rootPath() . '.htaccess'; if (file_exists($htaccess)) { $contents = file_get_contents($htaccess); if (!preg_match('#ExpiresByType#', $contents)) { $sExpires = <<<JCHEXPIRES ## BEGIN EXPIRES CACHING - JCH OPTIMIZE ## <IfModule mod_expires.c> ExpiresActive on # Perhaps better to whitelist expires rules? Perhaps. ExpiresDefault "access plus 1 month" # cache.appcache needs re-requests in FF 3.6 (thanks Remy ~Introducing HTML5) ExpiresByType text/cache-manifest "access plus 0 seconds" # Your document html ExpiresByType text/html "access plus 0 seconds" # Data ExpiresByType text/xml "access plus 0 seconds" ExpiresByType application/xml "access plus 0 seconds" ExpiresByType application/json "access plus 0 seconds" # Feed ExpiresByType application/rss+xml "access plus 1 hour" ExpiresByType application/atom+xml "access plus 1 hour" # Favicon (cannot be renamed) ExpiresByType image/x-icon "access plus 1 week" # Media: images, video, audio ExpiresByType image/gif "access plus 1 month" ExpiresByType image/png "access plus 1 month" ExpiresByType image/jpg "access plus 1 month" ExpiresByType image/jpeg "access plus 1 month" ExpiresByType video/ogg "access plus 1 month" ExpiresByType audio/ogg "access plus 1 month" ExpiresByType video/mp4 "access plus 1 month" ExpiresByType video/webm "access plus 1 month" # HTC files (css3pie) ExpiresByType text/x-component "access plus 1 month" # Webfonts ExpiresByType application/x-font-ttf "access plus 1 month" ExpiresByType font/opentype "access plus 1 month" ExpiresByType application/x-font-woff "access plus 1 month" ExpiresByType image/svg+xml "access plus 1 month" ExpiresByType application/vnd.ms-fontobject "access plus 1 month" # CSS and JavaScript ExpiresByType text/css "access plus 1 year" ExpiresByType text/javascript "access plus 1 year" ExpiresByType application/javascript "access plus 1 year" <IfModule mod_headers.c> Header append Cache-Control "public" </IfModule> </IfModule> ## END EXPIRES CACHING - JCH OPTIMIZE ## JCHEXPIRES; return file_put_contents($htaccess, $sExpires, FILE_APPEND); } else { return 'CODEALREADYINFILE'; } } else { return 'FILEDOESNTEXIST'; } }
/** * * @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(); }
/** * * @return type */ public static function extensions() { return JchPlatformPaths::rewriteBase(); }
/** * 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; } }
private static function item($file, $dir, $view, $path) { $encrypt_dir = JchPlatformUtility::encrypt($dir . $file); $encrypt_file = JchPlatformUtility::encrypt(rtrim(JchPlatformPaths::rootPath(), '/\\') . $dir . $file); $anchor = '<a href="#" rel="' . $encrypt_dir . '">' . htmlentities($file) . '</a>'; $html = ''; if ($view == 'tree') { $html .= $anchor; } else { if ($path == 'dir') { $html .= '<span><input type="checkbox" value="' . $encrypt_dir . '"></span>'; $html .= $anchor; } else { $html .= '<span><input type="checkbox" value="' . $encrypt_file . '"></span>'; $html .= '<span>' . htmlentities($file) . '</span>' . '<span><input type="text" pattern="[0-9]*" size="10" maxlength="5" name="width" ></span>' . '<span><input type="text" pattern="[0-9]*" size="10" maxlength="5" name="height" ></span>'; } } return $html; }