AbstractIo.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Filesystem\Io;
  7. /**
  8. * Install and upgrade client abstract class
  9. */
  10. abstract class AbstractIo implements IoInterface
  11. {
  12. /**
  13. * If this variable is set to true, our library will be able to automaticaly
  14. * create non-existant directories
  15. *
  16. * @var bool
  17. */
  18. protected $_allowCreateFolders = false;
  19. /**
  20. * Allow automaticaly create non-existant directories
  21. *
  22. * @param bool $flag
  23. * @return $this
  24. */
  25. public function setAllowCreateFolders($flag)
  26. {
  27. $this->_allowCreateFolders = (bool)$flag;
  28. return $this;
  29. }
  30. /**
  31. * Open a connection
  32. *
  33. * @param array $args
  34. * @return false
  35. */
  36. public function open(array $args = [])
  37. {
  38. return false;
  39. }
  40. /**
  41. * @return string
  42. */
  43. public function dirsep()
  44. {
  45. return '/';
  46. }
  47. /**
  48. * @param string $path
  49. * @return string
  50. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  51. */
  52. public function getCleanPath($path)
  53. {
  54. if (empty($path)) {
  55. return './';
  56. }
  57. $path = trim(preg_replace("/\\\\/", "/", (string)$path));
  58. if (!preg_match("/(\.\w{1,4})$/", $path) && !preg_match("/\?[^\\/]+$/", $path) && !preg_match("/\\/$/", $path)
  59. ) {
  60. $path .= '/';
  61. }
  62. $matches = [];
  63. $pattern = "/^(\\/|\w:\\/|https?:\\/\\/[^\\/]+\\/)?(.*)$/i";
  64. preg_match_all($pattern, $path, $matches, PREG_SET_ORDER);
  65. $pathTokR = $matches[0][1];
  66. $pathTokP = $matches[0][2];
  67. $pathTokP = preg_replace(["/^\\/+/", "/\\/+/"], ["", "/"], $pathTokP);
  68. $pathParts = explode("/", $pathTokP);
  69. $realPathParts = [];
  70. for ($i = 0, $realPathParts = []; $i < count($pathParts); $i++) {
  71. if ($pathParts[$i] == '.') {
  72. continue;
  73. } elseif ($pathParts[$i] == '..') {
  74. if (isset($realPathParts[0]) && $realPathParts[0] != '..' || $pathTokR != "") {
  75. array_pop($realPathParts);
  76. continue;
  77. }
  78. }
  79. $realPathParts[] = $pathParts[$i];
  80. }
  81. return $pathTokR . implode('/', $realPathParts);
  82. }
  83. /**
  84. * @param string $haystackPath
  85. * @param string $needlePath
  86. * @return bool
  87. */
  88. public function allowedPath($haystackPath, $needlePath)
  89. {
  90. return strpos($this->getCleanPath($haystackPath), $this->getCleanPath($needlePath)) === 0;
  91. }
  92. }