Base.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * Class Minify_Controller_Base
  4. * @package Minify
  5. */
  6. /**
  7. * Base class for Minify controller
  8. *
  9. * The controller class validates a request and uses it to create sources
  10. * for minification and set options like contentType. It's also responsible
  11. * for loading minifier code upon request.
  12. *
  13. * @package Minify
  14. * @author Stephen Clay <steve@mrclay.org>
  15. */
  16. abstract class Minify_Controller_Base {
  17. /**
  18. * Setup controller sources and set an needed options for Minify::source
  19. *
  20. * You must override this method in your subclass controller to set
  21. * $this->sources. If the request is NOT valid, make sure $this->sources
  22. * is left an empty array. Then strip any controller-specific options from
  23. * $options and return it. To serve files, $this->sources must be an array of
  24. * Minify_Source objects.
  25. *
  26. * @param array $options controller and Minify options
  27. *
  28. * @return array $options Minify::serve options
  29. */
  30. abstract public function setupSources($options);
  31. /**
  32. * Get default Minify options for this controller.
  33. *
  34. * Override in subclass to change defaults
  35. *
  36. * @return array options for Minify
  37. */
  38. public function getDefaultMinifyOptions() {
  39. return array(
  40. 'isPublic' => true
  41. ,'encodeOutput' => function_exists('gzdeflate')
  42. ,'encodeMethod' => null // determine later
  43. ,'encodeLevel' => 9
  44. ,'minifierOptions' => array() // no minifier options
  45. ,'contentTypeCharset' => 'utf-8'
  46. ,'maxAge' => 1800 // 30 minutes
  47. ,'rewriteCssUris' => true
  48. ,'bubbleCssImports' => false
  49. ,'quiet' => false // serve() will send headers and output
  50. ,'debug' => false
  51. // if you override these, the response codes MUST be directly after
  52. // the first space.
  53. ,'badRequestHeader' => 'HTTP/1.0 400 Bad Request'
  54. ,'errorHeader' => 'HTTP/1.0 500 Internal Server Error'
  55. // callback function to see/modify content of all sources
  56. ,'postprocessor' => null
  57. // file to require to load preprocessor
  58. ,'postprocessorRequire' => null
  59. );
  60. }
  61. /**
  62. * Get default minifiers for this controller.
  63. *
  64. * Override in subclass to change defaults
  65. *
  66. * @return array minifier callbacks for common types
  67. */
  68. public function getDefaultMinifers() {
  69. $ret[Minify::TYPE_JS] = array('JSMin', 'minify');
  70. $ret[Minify::TYPE_CSS] = array('Minify_CSS', 'minify');
  71. $ret[Minify::TYPE_HTML] = array('Minify_HTML', 'minify');
  72. return $ret;
  73. }
  74. /**
  75. * Is a user-given file within an allowable directory, existing,
  76. * and having an extension js/css/html/txt ?
  77. *
  78. * This is a convenience function for controllers that have to accept
  79. * user-given paths
  80. *
  81. * @param string $file full file path (already processed by realpath())
  82. *
  83. * @param array $safeDirs directories where files are safe to serve. Files can also
  84. * be in subdirectories of these directories.
  85. *
  86. * @return bool file is safe
  87. *
  88. * @deprecated use checkAllowDirs, checkNotHidden instead
  89. */
  90. public static function _fileIsSafe($file, $safeDirs)
  91. {
  92. $pathOk = false;
  93. foreach ((array)$safeDirs as $safeDir) {
  94. if (strpos($file, $safeDir) === 0) {
  95. $pathOk = true;
  96. break;
  97. }
  98. }
  99. $base = basename($file);
  100. if (! $pathOk || ! is_file($file) || $base[0] === '.') {
  101. return false;
  102. }
  103. list($revExt) = explode('.', strrev($base));
  104. return in_array(strrev($revExt), array('js', 'css', 'html', 'txt'));
  105. }
  106. /**
  107. * @param string $file
  108. * @param array $allowDirs
  109. * @param string $uri
  110. * @return bool
  111. * @throws Exception
  112. */
  113. public static function checkAllowDirs($file, $allowDirs, $uri)
  114. {
  115. foreach ((array)$allowDirs as $allowDir) {
  116. if (strpos($file, $allowDir) === 0) {
  117. return true;
  118. }
  119. }
  120. throw new Exception("File '$file' is outside \$allowDirs. If the path is"
  121. . " resolved via an alias/symlink, look into the \$min_symlinks option."
  122. . " E.g. \$min_symlinks['/" . dirname($uri) . "'] = '" . dirname($file) . "';");
  123. }
  124. /**
  125. * @param string $file
  126. * @throws Exception
  127. */
  128. public static function checkNotHidden($file)
  129. {
  130. $b = basename($file);
  131. if (0 === strpos($b, '.')) {
  132. throw new Exception("Filename '$b' starts with period (may be hidden)");
  133. }
  134. }
  135. /**
  136. * instances of Minify_Source, which provide content and any individual minification needs.
  137. *
  138. * @var array
  139. *
  140. * @see Minify_Source
  141. */
  142. public $sources = array();
  143. /**
  144. * Short name to place inside cache id
  145. *
  146. * The setupSources() method may choose to set this, making it easier to
  147. * recognize a particular set of sources/settings in the cache folder. It
  148. * will be filtered and truncated to make the final cache id <= 250 bytes.
  149. *
  150. * @var string
  151. */
  152. public $selectionId = '';
  153. /**
  154. * Mix in default controller options with user-given options
  155. *
  156. * @param array $options user options
  157. *
  158. * @return array mixed options
  159. */
  160. public final function mixInDefaultOptions($options)
  161. {
  162. $ret = array_merge(
  163. $this->getDefaultMinifyOptions(), $options
  164. );
  165. if (! isset($options['minifiers'])) {
  166. $options['minifiers'] = array();
  167. }
  168. $ret['minifiers'] = array_merge(
  169. $this->getDefaultMinifers(), $options['minifiers']
  170. );
  171. return $ret;
  172. }
  173. /**
  174. * Analyze sources (if there are any) and set $options 'contentType'
  175. * and 'lastModifiedTime' if they already aren't.
  176. *
  177. * @param array $options options for Minify
  178. *
  179. * @return array options for Minify
  180. */
  181. public final function analyzeSources($options = array())
  182. {
  183. if ($this->sources) {
  184. if (! isset($options['contentType'])) {
  185. $options['contentType'] = Minify_Source::getContentType($this->sources);
  186. }
  187. // last modified is needed for caching, even if setExpires is set
  188. if (! isset($options['lastModifiedTime'])) {
  189. $max = 0;
  190. foreach ($this->sources as $source) {
  191. $max = max($source->lastModified, $max);
  192. }
  193. $options['lastModifiedTime'] = $max;
  194. }
  195. }
  196. return $options;
  197. }
  198. /**
  199. * Send message to the Minify logger
  200. *
  201. * @param string $msg
  202. *
  203. * @return null
  204. */
  205. public function log($msg) {
  206. Minify_Logger::log($msg);
  207. }
  208. }