public function init() { $this->args = new SimpleArgs(); if ($this->args->flag('help')) { $this->showHelp(); exit; } $this->isVerbose = $this->args->flag('v') !== false; $this->verbose("Verbose: ON"); // check parameters $webRoot = $this->args->flag('webroot'); $infile = $this->args->flag('infile'); if (!is_string($webRoot) || !is_string($infile)) { $this->throwError("Missing argument(s)"); } if (!file_exists($infile)) { $this->throwError("File not found: %s", $infile); } // set webroot qualified path $webRoot = realpath($webRoot); // constants file $constants = array(); $configFile = $this->args->flag('config'); if (!file_exists($configFile)) { $this->throwError("File not found: %s", $configFile); } else { $constants = (require $configFile); $this->verbose('Constants: %s', implode(', ', array_keys($constants))); } $options = array('VERBOSE' => $this->isVerbose, 'STRIP' => $this->args->flag('strip'), 'WEB_PATH' => $webRoot, 'WEB_EXCL' => '*.psd,*.txt,*.bak,*.css,*.js', 'CLI' => $this); // auto output file naming if (false !== strstr($infile, Juicer::FILE_PATTERN_JUICY)) { $outfile = str_replace(Juicer::FILE_PATTERN_JUICY, Juicer::FILE_PATTERN_JUICED, $infile); } else { $this->throwError('The default output file naming requires "*.juicy.*" pattern.'); } $juicer = new Juicer($options, $constants); // start profiling script speed $juicer->profileStart(); try { $contents = $juicer->juice($infile); } catch (Exception $e) { $this->throwError('EXCEPTION: ' . $e->getMessage()); } // end profiling script speed $this->verbose('Execution time: %s seconds.', $juicer->profileEnd()); $this->verbose('Output file: "%s".', $outfile); if (file_put_contents($outfile, $contents) === false) { $this->throwError("Error writing to outfile %s", $outfile); } $this->verbose('Success!'); }
function execute() { $filepath = $this->getParameter('path'); // on web server the path doesn't come with a leading slash, go figure if (strpos($filepath, DIRECTORY_SEPARATOR) !== 0) { $filepath = DIRECTORY_SEPARATOR . $filepath; } // ignore paths with a '..' if (preg_match('!\\.\\.!', $filepath)) { $this->throw404('error1'); } // does the file exist? if (!file_exists(self::RELATIVE_PATH_TO_WEB . $filepath)) { $this->throw404('error3'); } header("Expires: " . gmdate("D, d M Y H:i:s", time() + 315360000) . " GMT"); header("Cache-Control: max-age=315360000"); // output a mediatype header $extension = substr(strrchr($filepath, '.'), 1); switch ($extension) { case 'css': header("Content-type: text/css"); break; case 'js': header("Content-type: text/javascript"); break; // script should be called only for js and css files! // script should be called only for js and css files! default: $this->throw404('error4'); break; } // don't use gzip compression on IE6 SP1 (hotfix http://support.microsoft.com/default.aspx?scid=kb;en-us;823386&Product=ie600) $ua = $_SERVER['HTTP_USER_AGENT']; $IE6bug = strpos($ua, 'MSIE 6') && strpos($ua, 'Opera') == -1; // For some very odd reason, "Norton Internet Security" unsets this $_SERVER['HTTP_ACCEPT_ENCODING'] = isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : ''; if (self::USE_GZIP_ENCODING && !$IE6bug && extension_loaded('zlib') && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false || strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate') !== false)) { ob_start('ob_gzhandler'); } else { ob_start(); } // handle dynamic "juicing" of files here (dev environment only) if (false !== strstr($filepath, '.juicy.')) { $webPath = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . self::RELATIVE_PATH_TO_WEB); $infile = $webPath . $filepath; $options = array('VERBOSE' => false, 'WEB_PATH' => $webPath, 'WEB_EXCL' => '*.psd,*.txt,*.bak,*.css,*.js'); try { $config = $this->getJuicerConfig(); $juicer = new Juicer($options, $config); $contents = $juicer->juice($infile); echo $contents; } catch (Exception $e) { $this->throw404('***EXCEPTION*** ' . $e->getMessage()); } } else { // include the file as is (fastest) echo file_get_contents(self::RELATIVE_PATH_TO_WEB . $filepath); } ob_end_flush(); }