/** * Apply JsMin to $content. * * @param string $filename Name of the file being generated. * @param string $content The uncompress contents of $filename. * @throws \Exception * @return string */ public function output($filename, $content) { if (function_exists('jsmin')) { return jsmin($content); } if (!class_exists('JSMin')) { throw new \Exception(sprintf('Cannot not load filter class "%s".', 'JsMin')); } return JSMin::minify($content); }
protected function jsParser() { if (inDeveloperMode()) { return $this->text; } //If the JSMIn extension is loaded, use that as it can be as much as 1000x faster than JShrink if (extension_loaded("jsmin")) { return @jsmin($this->text); } if (!empty($GLOBALS['sugar_config']['uglify'])) { $descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w")); $process = proc_open($GLOBALS['sugar_config']['uglify'], $descriptorspec, $pipes); if (is_resource($process)) { fwrite($pipes[0], $this->text); fclose($pipes[0]); $out = stream_get_contents($pipes[1]); fclose($pipes[1]); proc_close($process); return $out; } } require_once 'jssource/Minifier.php'; return JShrink\Minifier::minify($this->text); }
/** * Smarty plugin * @package Smarty * @subpackage plugins */ function smarty_block_scripts($params, $content, &$smarty, &$repeat) { if ($repeat == true) { Registry::set('runtime.inside_scripts', 1); return; } if (Registry::get('config.tweaks.dev_js')) { $content .= smarty_helper_inline_scripts($params, $content, $smarty, $repeat); return $content; } $scripts = array(); $external_scripts = array(); $dir_root = Registry::get('config.dir.root'); $return = ''; $current_location = Registry::get('config.current_location'); if (preg_match_all('/\\<script(.*?)\\>(.*?)\\<\\/script\\>/s', $content, $m)) { $contents = ''; foreach ($m[1] as $src) { if (!empty($src) && preg_match('/src ?= ?"([^"]+)"/', $src, $_m)) { if (strpos($_m[1], $current_location) !== false) { $scripts[] = str_replace($current_location, '', preg_replace('/\\?.*?$/', '', $_m[1])); } else { $external_scripts[] = $_m[1]; } } } // Check file changes in dev mode $names = $scripts; if (Development::isEnabled('compile_check')) { foreach ($names as $index => $name) { if (is_file($dir_root . '/' . $name)) { $names[$index] .= filemtime($dir_root . '/' . $name); } } } $filename = 'js/tygh/scripts-' . md5(implode(',', $names)) . fn_get_storage_data('cache_id') . '.js'; if (!Storage::instance('assets')->isExist($filename)) { foreach ($scripts as $src) { $contents .= fn_get_contents(Registry::get('config.dir.root') . $src); } $contents = str_replace('[files]', implode("\n", $scripts), Registry::get('config.js_css_cache_msg')) . $contents; if (function_exists('jsmin')) { $contents = jsmin($contents); } else { $contents = Minifier::minify($contents, array('flaggedComments' => false)); } Storage::instance('assets')->put($filename, array('contents' => $contents, 'compress' => false, 'caching' => true)); } $return = '<script type="text/javascript" src="' . Storage::instance('assets')->getUrl($filename) . '"></script>' . "\n"; if (!empty($external_scripts)) { foreach ($external_scripts as $sc) { $return .= '<script type="text/javascript" src="' . $sc . '"></script>' . "\n"; } } foreach ($m[2] as $sc) { if (!empty($sc)) { $return .= '<script type="text/javascript">' . $sc . '</script>' . "\n"; } } } $return .= smarty_helper_inline_scripts($params, $content, $smarty, $repeat); return $return; }
/** * Minifies the given JavaScript using UglifyJS. * @param String $content The JavaScript content. * @return String Minified JavaScript. */ function _minifyJS($content) { global $useUglify, $noMinify; if ($noMinify) { return $content; } $uglify = shell_exec('which uglifyjs 2>&1'); //if -u was passed, force uglify as it will produce smaller file output if (!$useUglify || empty($uglify)) { //If the JSMIn extension is loaded, use that as it can be as much as 1000x faster than JShrink if (extension_loaded("jsmin")) { return jsmin($content); } } //next fall back to uglify if (!empty($uglify)) { $descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w")); $process = proc_open($uglify, $descriptorspec, $pipes); if (is_resource($process)) { fwrite($pipes[0], $content); fclose($pipes[0]); $out = stream_get_contents($pipes[1]); fclose($pipes[1]); proc_close($process); return $out; } } //Finally attempt to use JShrink if (file_exists('Minifier.php')) { require_once 'Minifier.php'; } elseif (file_exists('../jssource/Minifier.php')) { require_once '../jssource/Minifier.php'; } else { //No minifier found, just return the content return $content; } return JShrink\Minifier::minify($content); }
switch ($theA) { case ord('}'): case ord(']'): case ord(')'): case ord('+'): case ord('-'): case ord('"'): case ord('\''): action(1); break; default: if (isAlphanum($theA)) { action(1); } else { action(3); } } break; default: action(1); break; } } } fclose($fp); } //echo $argv[1]; //jsmin( $argv[1] ); //$file = file_get_contents("php://stdin"); jsmin("php://stdin");
function renderPage($url,$langage,$upath,$callback=false,$skeleton=defaultSkeletonName,$json=false) { global $renderInclude, $design_cache, $canonicalurl, $currentLangage, $noDesignCacheUsed, $urlPath, $designPath, $commonDesignPath, $global_current_file, $need_fix_name, $redis, $seedPath, $seedKey, $globalCacheSave, $noDesignCacheAtAll, $userCanonical; $currentLangage=$langage; $urlPath=$upath; $designPath=$url; $commonDesignPath=''; $seedPath=$urlPath.'/'.$url; $seedKey=$url; // fill global vars $cachekey = redisPrefix.':cache:'.$skeleton.':'.$currentLangage.':'.$seedPath; if($langage!=defaultLangage) // generate canonical url { $canonicalurl=site_full_url.$langage.'/'; if($upath != '') $canonicalurl.=$upath.'/'; } else { $canonicalurl=site_full_url; if($upath != '') $canonicalurl.=$upath.'/'; } // get page cache if(empty($design_cache)) { $r = $redis->multi(Redis::PIPELINE)->hgetall($cachekey)->expire($cachekey,cacheTTL); $cache = $r->exec()[0]; $design_cache = $cache; } // check cache content of .ini entry for skeleton name, if not set, define at default. if(!isset($design_cache[':skeleton'])) $design_cache[':skeleton'] = $skeleton; // if page cache contain gzip entry, directly return it. if(isset($design_cache['html'])) { if(isset($design_cache['gzip'])) { header("X-Compression: gzip"); header("Content-Encoding: gzip"); } return $design_cache['html']; } // check page skeleton was loaded, else, load and cache it. if(!isset($design_cache[':sk'])) { $path = site_full_path.'/'.template.'/'.common_path.'/skeleton/'.$design_cache[':skeleton'].'.html'; if(is_file($path)) { $page = file_get_contents($path); $redis->hset($cachekey,':sk',$page); } else { //die(site_full_path.' => '.getcwd().' => '.$path); if($skeleton !== false) $page = $skeleton; else $page = defaultSkeleton; } } else $page = $design_cache[':sk']; // launch page part parsing, seed is skeleton $page = parsePage($url,$page); // if anywhere parsing was need to let page part as this, replace parse maker with page part one. if($need_fix_name) $page = str_replace('[$$]','§', $page); if($callback !== false) $callback($page); // any last chance callback ? // verifies and assign page final info if(empty($renderInclude['title'])) { if(isset($design_cache[':title'])) $renderInclude['title'] = $design_cache[':title']; else $renderInclude['title'] = defaultTitle; } if(empty($renderInclude['keywords'])) { if(isset($design_cache[':keywords'])) $renderInclude['keywords'] = $design_cache[':keywords']; else $renderInclude['keywords'] = defaultKeywords; } if(empty($renderInclude['description'])) { if(isset($design_cache[':description'])) $renderInclude['description'] = $design_cache[':description']; else $renderInclude['description'] = defaultDescription; } if(empty($renderInclude['meta'])) { if(isset($design_cache[':meta'])) $renderInclude['meta'] = $design_cache[':meta']; else $renderInclude['meta'] = defaultMeta; } if($userCanonical === false) $canonical = $canonicalurl.$url; else $canonical = $userCanonical; // generate head insertion $renderInclude['meta'] .= '<title>'.$renderInclude['title'].'</title><meta name="keywords" content="'.$renderInclude['keywords'].'" /><meta name="description" content="'.$renderInclude['description'].'" /><meta name="generator" content="fastIce" /><link rel="canonical" href="'.$canonicalurl.$url.'" />'; $renderInclude['head'] = $renderInclude['meta'].'<script type="text/javascript" src="'.jqueryLocation.'"></script>'.$renderInclude['head'].'[js]'; if(!empty($renderInclude['style'])) $renderInclude['head'] .= '<style>'.$renderInclude['style'].'</style>'; // generate complete brut page, depend on js is used or not if(empty($renderInclude['js']) && empty($renderInclude['jquery'])) $renderInclude['js']=''; // js free else // js used { if(!$json) $renderInclude['js'] = '<script type="text/javascript">'.$renderInclude['js'].'$(document).ready(function(){'.$renderInclude['jquery'].'});</script>'; if(!$noDesignCacheUsed && extension_loaded('jsmin')) $renderInclude['js'] = jsmin($renderInclude['js']); } $completePage = str_replace(array('[head]','[body]','[js]','[url]','[lang]','[title]'),array($renderInclude['head'],$renderInclude['body'],$renderInclude['js'],site_url,$currentLangage,$renderInclude['title']),$page); if($json) { $renderInclude['html'] = $completePage; $completePage = json_encode($renderInclude); } if(!$noDesignCacheAtAll) { if(!$noDesignCacheUsed) // the page is fully in cache { global $redis,$seedPath,$currentLangage; $redis->multi(Redis::PIPELINE)->del($cachekey); // delete old key // if compression enabled, gz the output at full compression and add gz flag in the page cache. if(enable_gz_compression) { $completePage = gzencode($completePage,9); $redis->hset($cachekey,'gzip',1); header("X-Compression: gzip"); header("Content-Encoding: gzip"); // send gz header } // global page cache save and return the gz or html data. $redis->hset($cachekey,'html',$completePage)->expire($cachekey,cacheTTL)->exec(); return $completePage; } // save all the page caches in redis, in one request if(!empty($globalCacheSave)){ $redis->multi(Redis::PIPELINE)->hMset($cachekey,$globalCacheSave)->expire($cachekey,cacheTTL)->exec(); } } else { $redis->del($cachekey); } if(enable_gz_compression && gz_compression) // out gz compression is forced { $completePage = gzencode($completePage,gz_compression); header("X-Compression: gzip"); header("Content-Encoding: gzip"); // send gz header } return $completePage; }