Base.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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' => $_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request'
  54. ,'errorHeader' => $_SERVER['SERVER_PROTOCOL'] . ' 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. * Load any code necessary to execute the given minifier callback.
  76. *
  77. * The controller is responsible for loading minification code on demand
  78. * via this method. This built-in function will only load classes for
  79. * static method callbacks where the class isn't already defined. It uses
  80. * the PEAR convention, so, given array('Jimmy_Minifier', 'minCss'), this
  81. * function will include 'Jimmy/Minifier.php'.
  82. *
  83. * If you need code loaded on demand and this doesn't suit you, you'll need
  84. * to override this function in your subclass.
  85. * @see Minify_Controller_Page::loadMinifier()
  86. *
  87. * @param callback $minifierCallback callback of minifier function
  88. *
  89. * @return null
  90. */
  91. public function loadMinifier($minifierCallback)
  92. {
  93. if (is_array($minifierCallback)
  94. && is_string($minifierCallback[0])
  95. && !class_exists($minifierCallback[0], false)) {
  96. require str_replace('_', '/', $minifierCallback[0]) . '.php';
  97. }
  98. }
  99. /**
  100. * Is a user-given file within an allowable directory, existing,
  101. * and having an extension js/css/html/txt ?
  102. *
  103. * This is a convenience function for controllers that have to accept
  104. * user-given paths
  105. *
  106. * @param string $file full file path (already processed by realpath())
  107. *
  108. * @param array $safeDirs directories where files are safe to serve. Files can also
  109. * be in subdirectories of these directories.
  110. *
  111. * @return bool file is safe
  112. *
  113. * @deprecated use checkAllowDirs, checkNotHidden instead
  114. */
  115. public static function _fileIsSafe($file, $safeDirs)
  116. {
  117. $pathOk = false;
  118. foreach ((array)$safeDirs as $safeDir) {
  119. if (strpos($file, $safeDir) === 0) {
  120. $pathOk = true;
  121. break;
  122. }
  123. }
  124. $base = basename($file);
  125. if (! $pathOk || ! is_file($file) || $base[0] === '.') {
  126. return false;
  127. }
  128. list($revExt) = explode('.', strrev($base));
  129. return in_array(strrev($revExt), array('js', 'css', 'html', 'txt'));
  130. }
  131. /**
  132. * @param string $file
  133. * @param array $allowDirs
  134. * @param string $uri
  135. * @return bool
  136. * @throws Exception
  137. */
  138. public static function checkAllowDirs($file, $allowDirs, $uri)
  139. {
  140. foreach ((array)$allowDirs as $allowDir) {
  141. if (strpos($file, $allowDir) === 0) {
  142. return true;
  143. }
  144. }
  145. throw new Exception("File '$file' is outside \$allowDirs. If the path is"
  146. . " resolved via an alias/symlink, look into the \$min_symlinks option."
  147. . " E.g. \$min_symlinks['/" . dirname($uri) . "'] = '" . dirname($file) . "';");
  148. }
  149. /**
  150. * @param string $file
  151. * @throws Exception
  152. */
  153. public static function checkNotHidden($file)
  154. {
  155. $b = basename($file);
  156. if (0 === strpos($b, '.')) {
  157. throw new Exception("Filename '$b' starts with period (may be hidden)");
  158. }
  159. }
  160. /**
  161. * instances of Minify_Source, which provide content and any individual minification needs.
  162. *
  163. * @var array
  164. *
  165. * @see Minify_Source
  166. */
  167. public $sources = array();
  168. /**
  169. * Short name to place inside cache id
  170. *
  171. * The setupSources() method may choose to set this, making it easier to
  172. * recognize a particular set of sources/settings in the cache folder. It
  173. * will be filtered and truncated to make the final cache id <= 250 bytes.
  174. *
  175. * @var string
  176. */
  177. public $selectionId = '';
  178. /**
  179. * Mix in default controller options with user-given options
  180. *
  181. * @param array $options user options
  182. *
  183. * @return array mixed options
  184. */
  185. public final function mixInDefaultOptions($options)
  186. {
  187. $ret = array_merge(
  188. $this->getDefaultMinifyOptions(), $options
  189. );
  190. if (! isset($options['minifiers'])) {
  191. $options['minifiers'] = array();
  192. }
  193. $ret['minifiers'] = array_merge(
  194. $this->getDefaultMinifers(), $options['minifiers']
  195. );
  196. return $ret;
  197. }
  198. /**
  199. * Analyze sources (if there are any) and set $options 'contentType'
  200. * and 'lastModifiedTime' if they already aren't.
  201. *
  202. * @param array $options options for Minify
  203. *
  204. * @return array options for Minify
  205. */
  206. public final function analyzeSources($options = array())
  207. {
  208. if ($this->sources) {
  209. if (! isset($options['contentType'])) {
  210. $options['contentType'] = Minify_Source::getContentType($this->sources);
  211. }
  212. // last modified is needed for caching, even if setExpires is set
  213. if (! isset($options['lastModifiedTime'])) {
  214. $max = 0;
  215. foreach ($this->sources as $source) {
  216. $max = max($source->lastModified, $max);
  217. }
  218. $options['lastModifiedTime'] = $max;
  219. }
  220. }
  221. return $options;
  222. }
  223. /**
  224. * Send message to the Minify logger
  225. *
  226. * @param string $msg
  227. *
  228. * @return null
  229. */
  230. public function log($msg) {
  231. require_once 'Minify/Logger.php';
  232. Minify_Logger::log($msg);
  233. }
  234. }