Version1.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Class Minify_Controller_Version1
  4. * @package Minify
  5. */
  6. require_once 'Minify/Controller/Base.php';
  7. /**
  8. * Controller class for emulating version 1 of minify.php (mostly a proof-of-concept)
  9. *
  10. * <code>
  11. * Minify::serve('Version1');
  12. * </code>
  13. *
  14. * @package Minify
  15. * @author Stephen Clay <steve@mrclay.org>
  16. */
  17. class Minify_Controller_Version1 extends Minify_Controller_Base {
  18. /**
  19. * Set up groups of files as sources
  20. *
  21. * @param array $options controller and Minify options
  22. * @return array Minify options
  23. *
  24. */
  25. public function setupSources($options) {
  26. self::_setupDefines();
  27. if (MINIFY_USE_CACHE) {
  28. $cacheDir = defined('MINIFY_CACHE_DIR')
  29. ? MINIFY_CACHE_DIR
  30. : '';
  31. Minify::setCache($cacheDir);
  32. }
  33. $options['badRequestHeader'] = $_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found';
  34. $options['contentTypeCharset'] = MINIFY_ENCODING;
  35. // The following restrictions are to limit the URLs that minify will
  36. // respond to. Ideally there should be only one way to reference a file.
  37. if (! isset($_GET['files'])
  38. // verify at least one file, files are single comma separated,
  39. // and are all same extension
  40. || ! preg_match('/^[^,]+\\.(css|js)(,[^,]+\\.\\1)*$/', $_GET['files'], $m)
  41. // no "//" (makes URL rewriting easier)
  42. || strpos($_GET['files'], '//') !== false
  43. // no "\"
  44. || strpos($_GET['files'], '\\') !== false
  45. // no "./"
  46. || preg_match('/(?:^|[^\\.])\\.\\//', $_GET['files'])
  47. ) {
  48. return $options;
  49. }
  50. $extension = $m[1];
  51. $files = explode(',', $_GET['files']);
  52. if (count($files) > MINIFY_MAX_FILES) {
  53. return $options;
  54. }
  55. // strings for prepending to relative/absolute paths
  56. $prependRelPaths = dirname($_SERVER['SCRIPT_FILENAME'])
  57. . DIRECTORY_SEPARATOR;
  58. $prependAbsPaths = $_SERVER['DOCUMENT_ROOT'];
  59. $sources = array();
  60. $goodFiles = array();
  61. $hasBadSource = false;
  62. $allowDirs = isset($options['allowDirs'])
  63. ? $options['allowDirs']
  64. : MINIFY_BASE_DIR;
  65. foreach ($files as $file) {
  66. // prepend appropriate string for abs/rel paths
  67. $file = ($file[0] === '/' ? $prependAbsPaths : $prependRelPaths) . $file;
  68. // make sure a real file!
  69. $file = realpath($file);
  70. // don't allow unsafe or duplicate files
  71. if (parent::_fileIsSafe($file, $allowDirs)
  72. && !in_array($file, $goodFiles))
  73. {
  74. $goodFiles[] = $file;
  75. $srcOptions = array(
  76. 'filepath' => $file
  77. );
  78. $this->sources[] = new Minify_Source($srcOptions);
  79. } else {
  80. $hasBadSource = true;
  81. break;
  82. }
  83. }
  84. if ($hasBadSource) {
  85. $this->sources = array();
  86. }
  87. if (! MINIFY_REWRITE_CSS_URLS) {
  88. $options['rewriteCssUris'] = false;
  89. }
  90. return $options;
  91. }
  92. private static function _setupDefines()
  93. {
  94. $defaults = array(
  95. 'MINIFY_BASE_DIR' => realpath($_SERVER['DOCUMENT_ROOT'])
  96. ,'MINIFY_ENCODING' => 'utf-8'
  97. ,'MINIFY_MAX_FILES' => 16
  98. ,'MINIFY_REWRITE_CSS_URLS' => true
  99. ,'MINIFY_USE_CACHE' => true
  100. );
  101. foreach ($defaults as $const => $val) {
  102. if (! defined($const)) {
  103. define($const, $val);
  104. }
  105. }
  106. }
  107. }