3 * Class Minify_Controller_Version1
8 * Controller class for emulating version 1 of minify.php (mostly a proof-of-concept)
11 * Minify::serve('Version1');
15 * @author Stephen Clay <steve@mrclay.org>
17 class Minify_Controller_Version1 extends Minify_Controller_Base {
20 * Set up groups of files as sources
22 * @param array $options controller and Minify options
23 * @return array Minify options
26 public function setupSources($options) {
27 // PHP insecure by default: realpath() and other FS functions can't handle null bytes.
28 if (isset($_GET['files'])) {
29 $_GET['files'] = str_replace("\x00", '', (string)$_GET['files']);
32 self::_setupDefines();
33 if (MINIFY_USE_CACHE) {
34 $cacheDir = defined('MINIFY_CACHE_DIR')
37 Minify::setCache($cacheDir);
39 $options['badRequestHeader'] = 'HTTP/1.0 404 Not Found';
40 $options['contentTypeCharset'] = MINIFY_ENCODING;
42 // The following restrictions are to limit the URLs that minify will
43 // respond to. Ideally there should be only one way to reference a file.
44 if (! isset($_GET['files'])
45 // verify at least one file, files are single comma separated,
46 // and are all same extension
47 || ! preg_match('/^[^,]+\\.(css|js)(,[^,]+\\.\\1)*$/', $_GET['files'], $m)
48 // no "//" (makes URL rewriting easier)
49 || strpos($_GET['files'], '//') !== false
51 || strpos($_GET['files'], '\\') !== false
53 || preg_match('/(?:^|[^\\.])\\.\\//', $_GET['files'])
58 $files = explode(',', $_GET['files']);
59 if (count($files) > MINIFY_MAX_FILES) {
63 // strings for prepending to relative/absolute paths
64 $prependRelPaths = dirname($_SERVER['SCRIPT_FILENAME'])
65 . DIRECTORY_SEPARATOR;
66 $prependAbsPaths = $_SERVER['DOCUMENT_ROOT'];
69 $hasBadSource = false;
71 $allowDirs = isset($options['allowDirs'])
72 ? $options['allowDirs']
75 foreach ($files as $file) {
76 // prepend appropriate string for abs/rel paths
77 $file = ($file[0] === '/' ? $prependAbsPaths : $prependRelPaths) . $file;
78 // make sure a real file!
79 $file = realpath($file);
80 // don't allow unsafe or duplicate files
81 if (parent::_fileIsSafe($file, $allowDirs)
82 && !in_array($file, $goodFiles))
88 $this->sources[] = new Minify_Source($srcOptions);
95 $this->sources = array();
97 if (! MINIFY_REWRITE_CSS_URLS) {
98 $options['rewriteCssUris'] = false;
103 private static function _setupDefines()
106 'MINIFY_BASE_DIR' => realpath($_SERVER['DOCUMENT_ROOT'])
107 ,'MINIFY_ENCODING' => 'utf-8'
108 ,'MINIFY_MAX_FILES' => 16
109 ,'MINIFY_REWRITE_CSS_URLS' => true
110 ,'MINIFY_USE_CACHE' => true
112 foreach ($defaults as $const => $val) {
113 if (! defined($const)) {
114 define($const, $val);