PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/w3-total-cache/lib/Minify/Minify/Controller/MinApp.php

https://bitbucket.org/openfarmtech/weblog-content
PHP | 132 lines | 100 code | 4 blank | 28 comment | 25 complexity | 917ffecaed797384eeeb594a22daf53f MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.0, LGPL-3.0, BSD-3-Clause, GPL-3.0, LGPL-2.1, AGPL-3.0, CC-BY-SA-3.0
  1. <?php
  2. /**
  3. * Class Minify_Controller_MinApp
  4. * @package Minify
  5. */
  6. require_once W3TC_LIB_MINIFY_DIR . '/Minify/Controller/Base.php';
  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. * Set up groups of files as sources
  16. *
  17. * @param array $options controller and Minify options
  18. * @return array Minify options
  19. *
  20. */
  21. public function setupSources($options) {
  22. // filter controller options
  23. $cOptions = array_merge(
  24. array(
  25. 'allowDirs' => '//'
  26. ,'groupsOnly' => false
  27. ,'groups' => array()
  28. ,'maxFiles' => 10
  29. )
  30. ,(isset($options['minApp']) ? $options['minApp'] : array())
  31. );
  32. unset($options['minApp']);
  33. $sources = array();
  34. if (isset($_GET['g'])) {
  35. // try groups
  36. if (! isset($cOptions['groups'][$_GET['g']])) {
  37. $this->log("A group configuration for \"{$_GET['g']}\" was not set");
  38. return $options;
  39. }
  40. $files = $cOptions['groups'][$_GET['g']];
  41. // if $files is a single object, casting will break it
  42. if (is_object($files)) {
  43. $files = array($files);
  44. } elseif (! is_array($files)) {
  45. $files = (array)$files;
  46. }
  47. foreach ($files as $file) {
  48. if ($file instanceof Minify_Source) {
  49. $sources[] = $file;
  50. continue;
  51. }
  52. if (0 === strpos($file, '//')) {
  53. $file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
  54. }
  55. $realPath = realpath($file);
  56. if (is_file($realPath)) {
  57. $sources[] = new Minify_Source(array(
  58. 'filepath' => $realPath
  59. ));
  60. } else {
  61. $this->log("The path \"{$file}\" could not be found (or was not a file)");
  62. continue;
  63. }
  64. }
  65. } elseif (! $cOptions['groupsOnly'] && isset($_GET['f'])) {
  66. // try user files
  67. // The following restrictions are to limit the URLs that minify will
  68. // respond to. Ideally there should be only one way to reference a file.
  69. if (// verify at least one file, files are single comma separated,
  70. // and are all same extension
  71. ! preg_match('/^[^,]+\\.(css|js)(?:,[^,]+\\.\\1)*$/', $_GET['f'])
  72. // no "//"
  73. || strpos($_GET['f'], '//') !== false
  74. // no "\"
  75. || strpos($_GET['f'], '\\') !== false
  76. // no "./"
  77. || preg_match('/(?:^|[^\\.])\\.\\//', $_GET['f'])
  78. ) {
  79. $this->log("GET param 'f' invalid (see MinApp.php line 63)");
  80. return $options;
  81. }
  82. $files = explode(',', $_GET['f']);
  83. if (count($files) > $cOptions['maxFiles'] || $files != array_unique($files)) {
  84. $this->log("Too many or duplicate files specified");
  85. return $options;
  86. }
  87. if (isset($_GET['b'])) {
  88. // check for validity
  89. if (preg_match('@^[^/]+(?:/[^/]+)*$@', $_GET['b'])
  90. && false === strpos($_GET['b'], '..')
  91. && $_GET['b'] !== '.') {
  92. // valid base
  93. $base = "/{$_GET['b']}/";
  94. } else {
  95. $this->log("GET param 'b' invalid (see MinApp.php line 84)");
  96. return $options;
  97. }
  98. } else {
  99. $base = '/';
  100. }
  101. $allowDirs = array();
  102. foreach ((array)$cOptions['allowDirs'] as $allowDir) {
  103. $allowDirs[] = realpath(str_replace('//', $_SERVER['DOCUMENT_ROOT'] . '/', $allowDir));
  104. }
  105. foreach ($files as $file) {
  106. $path = $_SERVER['DOCUMENT_ROOT'] . $base . $file;
  107. $file = realpath($path);
  108. if (false === $file) {
  109. $this->log("Path \"{$path}\" failed realpath()");
  110. return $options;
  111. } elseif (! parent::_fileIsSafe($file, $allowDirs)) {
  112. $this->log("Path \"{$path}\" failed Minify_Controller_Base::_fileIsSafe()");
  113. return $options;
  114. } else {
  115. $sources[] = new Minify_Source(array(
  116. 'filepath' => $file
  117. ));
  118. }
  119. }
  120. }
  121. if ($sources) {
  122. $this->sources = $sources;
  123. } else {
  124. $this->log("No sources to serve");
  125. }
  126. return $options;
  127. }
  128. }