initial commit
[namibia] / public / min / lib / Minify / Controller / MinApp.php
1 <?php
2 /**
3  * Class Minify_Controller_MinApp  
4  * @package Minify
5  */
6
7 /**
8  * Controller class for requests to /min/index.php
9  * 
10  * @package Minify
11  * @author Stephen Clay <steve@mrclay.org>
12  */
13 class Minify_Controller_MinApp extends Minify_Controller_Base {
14     
15     /**
16      * Set up groups of files as sources
17      * 
18      * @param array $options controller and Minify options
19      *
20      * @return array Minify options
21      */
22     public function setupSources($options) {
23         // PHP insecure by default: realpath() and other FS functions can't handle null bytes.
24         foreach (array('g', 'b', 'f') as $key) {
25             if (isset($_GET[$key])) {
26                 $_GET[$key] = str_replace("\x00", '', (string)$_GET[$key]);
27             }
28         }
29
30         // filter controller options
31         $cOptions = array_merge(
32             array(
33                 'allowDirs' => '//'
34                 ,'groupsOnly' => false
35                 ,'groups' => array()
36                 ,'noMinPattern' => '@[-\\.]min\\.(?:js|css)$@i' // matched against basename
37             )
38             ,(isset($options['minApp']) ? $options['minApp'] : array())
39         );
40         unset($options['minApp']);
41         $sources = array();
42         $this->selectionId = '';
43         $firstMissingResource = null;
44         if (isset($_GET['g'])) {
45             // add group(s)
46             $this->selectionId .= 'g=' . $_GET['g'];
47             $keys = explode(',', $_GET['g']);
48             if ($keys != array_unique($keys)) {
49                 $this->log("Duplicate group key found.");
50                 return $options;
51             }
52             $keys = explode(',', $_GET['g']);
53             foreach ($keys as $key) {
54                 if (! isset($cOptions['groups'][$key])) {
55                     $this->log("A group configuration for \"{$key}\" was not found");
56                     return $options;
57                 }
58                 $files = $cOptions['groups'][$key];
59                 // if $files is a single object, casting will break it
60                 if (is_object($files)) {
61                     $files = array($files);
62                 } elseif (! is_array($files)) {
63                     $files = (array)$files;
64                 }
65                 foreach ($files as $file) {
66                     if ($file instanceof Minify_Source) {
67                         $sources[] = $file;
68                         continue;
69                     }
70                     if (0 === strpos($file, '//')) {
71                         $file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
72                     }
73                     $realpath = realpath($file);
74                     if ($realpath && is_file($realpath)) {
75                         $sources[] = $this->_getFileSource($realpath, $cOptions);
76                     } else {
77                         $this->log("The path \"{$file}\" (realpath \"{$realpath}\") could not be found (or was not a file)");
78                         if (null === $firstMissingResource) {
79                             $firstMissingResource = basename($file);
80                             continue;
81                         } else {
82                             $secondMissingResource = basename($file);
83                             $this->log("More than one file was missing: '$firstMissingResource', '$secondMissingResource'");
84                             return $options;
85                         }
86                     }
87                 }
88                 if ($sources) {
89                     try {
90                         $this->checkType($sources[0]);
91                     } catch (Exception $e) {
92                         $this->log($e->getMessage());
93                         return $options;
94                     }
95                 }
96             }
97         }
98         if (! $cOptions['groupsOnly'] && isset($_GET['f'])) {
99             // try user files
100             // The following restrictions are to limit the URLs that minify will
101             // respond to.
102             if (// verify at least one file, files are single comma separated, 
103                 // and are all same extension
104                 ! preg_match('/^[^,]+\\.(css|js)(?:,[^,]+\\.\\1)*$/', $_GET['f'], $m)
105                 // no "//"
106                 || strpos($_GET['f'], '//') !== false
107                 // no "\"
108                 || strpos($_GET['f'], '\\') !== false
109             ) {
110                 $this->log("GET param 'f' was invalid");
111                 return $options;
112             }
113             $ext = ".{$m[1]}";
114             try {
115                 $this->checkType($m[1]);
116             } catch (Exception $e) {
117                 $this->log($e->getMessage());
118                 return $options;
119             }
120             $files = explode(',', $_GET['f']);
121             if ($files != array_unique($files)) {
122                 $this->log("Duplicate files were specified");
123                 return $options;
124             }
125             if (isset($_GET['b'])) {
126                 // check for validity
127                 if (preg_match('@^[^/]+(?:/[^/]+)*$@', $_GET['b'])
128                     && false === strpos($_GET['b'], '..')
129                     && $_GET['b'] !== '.') {
130                     // valid base
131                     $base = "/{$_GET['b']}/";       
132                 } else {
133                     $this->log("GET param 'b' was invalid");
134                     return $options;
135                 }
136             } else {
137                 $base = '/';
138             }
139             $allowDirs = array();
140             foreach ((array)$cOptions['allowDirs'] as $allowDir) {
141                 $allowDirs[] = realpath(str_replace('//', $_SERVER['DOCUMENT_ROOT'] . '/', $allowDir));
142             }
143             $basenames = array(); // just for cache id
144             foreach ($files as $file) {
145                 $uri = $base . $file;
146                 $path = $_SERVER['DOCUMENT_ROOT'] . $uri;
147                 $realpath = realpath($path);
148                 if (false === $realpath || ! is_file($realpath)) {
149                     $this->log("The path \"{$path}\" (realpath \"{$realpath}\") could not be found (or was not a file)");
150                     if (null === $firstMissingResource) {
151                         $firstMissingResource = $uri;
152                         continue;
153                     } else {
154                         $secondMissingResource = $uri;
155                         $this->log("More than one file was missing: '$firstMissingResource', '$secondMissingResource`'");
156                         return $options;
157                     }
158                 }
159                 try {
160                     parent::checkNotHidden($realpath);
161                     parent::checkAllowDirs($realpath, $allowDirs, $uri);
162                 } catch (Exception $e) {
163                     $this->log($e->getMessage());
164                     return $options;
165                 }
166                 $sources[] = $this->_getFileSource($realpath, $cOptions);
167                 $basenames[] = basename($realpath, $ext);
168             }
169             if ($this->selectionId) {
170                 $this->selectionId .= '_f=';
171             }
172             $this->selectionId .= implode(',', $basenames) . $ext;
173         }
174         if ($sources) {
175             if (null !== $firstMissingResource) {
176                 array_unshift($sources, new Minify_Source(array(
177                     'id' => 'missingFile'
178                     // should not cause cache invalidation
179                     ,'lastModified' => 0
180                     // due to caching, filename is unreliable.
181                     ,'content' => "/* Minify: at least one missing file. See " . Minify::URL_DEBUG . " */\n"
182                     ,'minifier' => ''
183                 )));
184             }
185             $this->sources = $sources;
186         } else {
187             $this->log("No sources to serve");
188         }
189         return $options;
190     }
191
192     /**
193      * @param string $file
194      *
195      * @param array $cOptions
196      *
197      * @return Minify_Source
198      */
199     protected function _getFileSource($file, $cOptions)
200     {
201         $spec['filepath'] = $file;
202         if ($cOptions['noMinPattern'] && preg_match($cOptions['noMinPattern'], basename($file))) {
203             if (preg_match('~\.css$~i', $file)) {
204                 $spec['minifyOptions']['compress'] = false;
205             } else {
206                 $spec['minifier'] = '';
207             }
208         }
209         return new Minify_Source($spec);
210     }
211
212     protected $_type = null;
213
214     /**
215      * Make sure that only source files of a single type are registered
216      *
217      * @param string $sourceOrExt
218      *
219      * @throws Exception
220      */
221     public function checkType($sourceOrExt)
222     {
223         if ($sourceOrExt === 'js') {
224             $type = Minify::TYPE_JS;
225         } elseif ($sourceOrExt === 'css') {
226             $type = Minify::TYPE_CSS;
227         } elseif ($sourceOrExt->contentType !== null) {
228             $type = $sourceOrExt->contentType;
229         } else {
230             return;
231         }
232         if ($this->_type === null) {
233             $this->_type = $type;
234         } elseif ($this->_type !== $type) {
235             throw new Exception('Content-Type mismatch');
236         }
237     }
238 }