3 * Class Minify_Controller_Version1
7 require_once 'Minify/Controller/Base.php';
10 * Controller class for emulating version 1 of minify.php
13 * Minify::serve('Version1');
17 * @author Stephen Clay <steve@mrclay.org>
19 class Minify_Controller_Version1 extends Minify_Controller_Base {
22 * Set up groups of files as sources
24 * @param array $options controller and Minify options
25 * @return array Minify options
28 public function setupSources($options) {
29 self::_setupDefines();
30 if (MINIFY_USE_CACHE) {
31 $cacheDir = defined('MINIFY_CACHE_DIR')
34 Minify::setCache($cacheDir);
36 $options['badRequestHeader'] = 'HTTP/1.0 404 Not Found';
37 $options['contentTypeCharset'] = MINIFY_ENCODING;
39 // The following restrictions are to limit the URLs that minify will
40 // respond to. Ideally there should be only one way to reference a file.
41 if (! isset($_GET['files'])
42 // verify at least one file, files are single comma separated,
43 // and are all same extension
44 || ! preg_match('/^[^,]+\\.(css|js)(,[^,]+\\.\\1)*$/', $_GET['files'], $m)
45 // no "//" (makes URL rewriting easier)
46 || strpos($_GET['files'], '//') !== false
48 || strpos($_GET['files'], '\\') !== false
50 || preg_match('/(?:^|[^\\.])\\.\\//', $_GET['files'])
56 $files = explode(',', $_GET['files']);
57 if (count($files) > MINIFY_MAX_FILES) {
61 // strings for prepending to relative/absolute paths
62 $prependRelPaths = dirname($_SERVER['SCRIPT_FILENAME'])
63 . DIRECTORY_SEPARATOR;
64 $prependAbsPaths = $_SERVER['DOCUMENT_ROOT'];
68 $hasBadSource = false;
70 $allowDirs = isset($options['allowDirs'])
71 ? $options['allowDirs']
74 foreach ($files as $file) {
75 // prepend appropriate string for abs/rel paths
76 $file = ($file[0] === '/' ? $prependAbsPaths : $prependRelPaths) . $file;
77 // make sure a real file!
78 $file = realpath($file);
79 // don't allow unsafe or duplicate files
80 if (parent::_fileIsSafe($file, $allowDirs)
81 && !in_array($file, $goodFiles))
87 $this->sources[] = new Minify_Source($srcOptions);
94 $this->sources = array();
96 if (! MINIFY_REWRITE_CSS_URLS) {
97 $options['rewriteCssUris'] = false;
102 private static function _setupDefines()
105 'MINIFY_BASE_DIR' => realpath($_SERVER['DOCUMENT_ROOT'])
106 ,'MINIFY_ENCODING' => 'utf-8'
107 ,'MINIFY_MAX_FILES' => 16
108 ,'MINIFY_REWRITE_CSS_URLS' => true
109 ,'MINIFY_USE_CACHE' => true
111 foreach ($defaults as $const => $val) {
112 if (! defined($const)) {
113 define($const, $val);