3 * Class Minify_Controller_MinApp
8 * Controller class for requests to /min/index.php
11 * @author Stephen Clay <steve@mrclay.org>
13 class Minify_Controller_MinApp extends Minify_Controller_Base {
16 * Set up groups of files as sources
18 * @param array $options controller and Minify options
20 * @return array Minify options
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]);
30 // filter controller options
31 $cOptions = array_merge(
34 ,'groupsOnly' => false
36 ,'noMinPattern' => '@[-\\.]min\\.(?:js|css)$@i' // matched against basename
38 ,(isset($options['minApp']) ? $options['minApp'] : array())
40 unset($options['minApp']);
42 $this->selectionId = '';
43 $firstMissingResource = null;
44 if (isset($_GET['g'])) {
46 $this->selectionId .= 'g=' . $_GET['g'];
47 $keys = explode(',', $_GET['g']);
48 if ($keys != array_unique($keys)) {
49 $this->log("Duplicate group key found.");
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");
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;
65 foreach ($files as $file) {
66 if ($file instanceof Minify_Source) {
70 if (0 === strpos($file, '//')) {
71 $file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
73 $realpath = realpath($file);
74 if ($realpath && is_file($realpath)) {
75 $sources[] = $this->_getFileSource($realpath, $cOptions);
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);
82 $secondMissingResource = basename($file);
83 $this->log("More than one file was missing: '$firstMissingResource', '$secondMissingResource'");
90 $this->checkType($sources[0]);
91 } catch (Exception $e) {
92 $this->log($e->getMessage());
98 if (! $cOptions['groupsOnly'] && isset($_GET['f'])) {
100 // The following restrictions are to limit the URLs that minify will
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)
106 || strpos($_GET['f'], '//') !== false
108 || strpos($_GET['f'], '\\') !== false
110 $this->log("GET param 'f' was invalid");
115 $this->checkType($m[1]);
116 } catch (Exception $e) {
117 $this->log($e->getMessage());
120 $files = explode(',', $_GET['f']);
121 if ($files != array_unique($files)) {
122 $this->log("Duplicate files were specified");
125 if (isset($_GET['b'])) {
126 // check for validity
127 if (preg_match('@^[^/]+(?:/[^/]+)*$@', $_GET['b'])
128 && false === strpos($_GET['b'], '..')
129 && $_GET['b'] !== '.') {
131 $base = "/{$_GET['b']}/";
133 $this->log("GET param 'b' was invalid");
139 $allowDirs = array();
140 foreach ((array)$cOptions['allowDirs'] as $allowDir) {
141 $allowDirs[] = realpath(str_replace('//', $_SERVER['DOCUMENT_ROOT'] . '/', $allowDir));
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;
154 $secondMissingResource = $uri;
155 $this->log("More than one file was missing: '$firstMissingResource', '$secondMissingResource`'");
160 parent::checkNotHidden($realpath);
161 parent::checkAllowDirs($realpath, $allowDirs, $uri);
162 } catch (Exception $e) {
163 $this->log($e->getMessage());
166 $sources[] = $this->_getFileSource($realpath, $cOptions);
167 $basenames[] = basename($realpath, $ext);
169 if ($this->selectionId) {
170 $this->selectionId .= '_f=';
172 $this->selectionId .= implode(',', $basenames) . $ext;
175 if (null !== $firstMissingResource) {
176 array_unshift($sources, new Minify_Source(array(
177 'id' => 'missingFile'
178 // should not cause cache invalidation
180 // due to caching, filename is unreliable.
181 ,'content' => "/* Minify: at least one missing file. See " . Minify::URL_DEBUG . " */\n"
185 $this->sources = $sources;
187 $this->log("No sources to serve");
193 * @param string $file
195 * @param array $cOptions
197 * @return Minify_Source
199 protected function _getFileSource($file, $cOptions)
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;
206 $spec['minifier'] = '';
209 return new Minify_Source($spec);
212 protected $_type = null;
215 * Make sure that only source files of a single type are registered
217 * @param string $sourceOrExt
221 public function checkType($sourceOrExt)
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;
232 if ($this->_type === null) {
233 $this->_type = $type;
234 } elseif ($this->_type !== $type) {
235 throw new Exception('Content-Type mismatch');