PageRenderTime 23ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/wordpress/wp-content/plugins/worker/src/Dropbox/Path.php

https://github.com/kronda/kronda
PHP | 170 lines | 87 code | 14 blank | 69 comment | 12 complexity | 2fa14d2337c2155a8a5b60569f2b0217 MD5 | raw file
  1. <?php
  2. /**
  3. * Path validation functions.
  4. */
  5. final class Dropbox_Path
  6. {
  7. /**
  8. * Return whether the given path is a valid Dropbox path.
  9. *
  10. * @param string $path
  11. * The path you want to check for validity.
  12. *
  13. * @return bool
  14. * Whether the path was valid or not.
  15. */
  16. static function isValid($path)
  17. {
  18. $error = self::findError($path);
  19. return ($error === null);
  20. }
  21. /**
  22. * Return whether the given path is a valid non-root Dropbox path.
  23. * This is the same as {@link isValid} except <code>"/"</code> is not allowed.
  24. *
  25. * @param string $path
  26. * The path you want to check for validity.
  27. *
  28. * @return bool
  29. * Whether the path was valid or not.
  30. */
  31. static function isValidNonRoot($path)
  32. {
  33. $error = self::findErrorNonRoot($path);
  34. return ($error === null);
  35. }
  36. /**
  37. * If the given path is a valid Dropbox path, return <code>null</code>,
  38. * otherwise return an English string error message describing what is wrong with the path.
  39. *
  40. * @param string $path
  41. * The path you want to check for validity.
  42. *
  43. * @return string|null
  44. * If the path was valid, return <code>null</code>. Otherwise, returns
  45. * an English string describing the problem.
  46. */
  47. static function findError($path)
  48. {
  49. Dropbox_Checker::argString("path", $path);
  50. $matchResult = preg_match('%^(?:
  51. [\x09\x0A\x0D\x20-\x7E] # ASCII
  52. | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  53. | \xE0[\xA0-\xBF][\x80-\xBD] # excluding overlongs, FFFE, and FFFF
  54. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  55. | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  56. )*$%xs', $path);
  57. if ($matchResult !== 1) {
  58. return "must be valid UTF-8; BMP only, no surrogates, no U+FFFE or U+FFFF";
  59. }
  60. if (substr_compare($path, "/", 0, 1) !== 0) return "must start with \"/\"";
  61. $l = strlen($path);
  62. if ($l === 1) return null; // Special case for "/"
  63. if ($path[$l-1] === "/") return "must not end with \"/\"";
  64. // TODO: More checks.
  65. return null;
  66. }
  67. /**
  68. * If the given path is a valid non-root Dropbox path, return <code>null</code>,
  69. * otherwise return an English string error message describing what is wrong with the path.
  70. * This is the same as {@link findError} except <code>"/"</code> will yield an error message.
  71. *
  72. * @param string $path
  73. * The path you want to check for validity.
  74. *
  75. * @return string|null
  76. * If the path was valid, return <code>null</code>. Otherwise, returns
  77. * an English string describing the problem.
  78. */
  79. static function findErrorNonRoot($path)
  80. {
  81. if ($path == "/") return "root path not allowed";
  82. return self::findError($path);
  83. }
  84. /**
  85. * Return the last component of a path (the file or folder name).
  86. *
  87. * <code>
  88. * Path::getName("/Misc/Notes.txt") // "Notes.txt"
  89. * Path::getName("/Misc") // "Misc"
  90. * Path::getName("/") // null
  91. * </code>
  92. *
  93. * @param string $path
  94. * The full path you want to get the last component of.
  95. *
  96. * @return null|string
  97. * The last component of <code>$path</code> or <code>null</code> if the given
  98. * <code>$path</code> was <code>"/"<code>.
  99. */
  100. static function getName($path)
  101. {
  102. Dropbox_Checker::argString("path", $path);
  103. if (substr_compare($path, "/", 0, 1) !== 0) {
  104. throw new InvalidArgumentException("'path' must start with \"/\"");
  105. }
  106. $l = strlen($path);
  107. if ($l === 1) return null;
  108. if ($path[$l-1] === "/") {
  109. throw new InvalidArgumentException("'path' must not end with \"/\"");
  110. }
  111. $lastSlash = strrpos($path, "/");
  112. return substr($path, $lastSlash+1);
  113. }
  114. /**
  115. * @internal
  116. *
  117. * @param string $argName
  118. * @param mixed $value
  119. * @throws InvalidArgumentException
  120. */
  121. static function checkArg($argName, $value)
  122. {
  123. if ($value === null) throw new InvalidArgumentException("'$argName' must not be null");
  124. if (!is_string($value)) throw new InvalidArgumentException("'$argName' must be a string");
  125. $error = self::findError($value);
  126. if ($error !== null) throw new InvalidArgumentException("'$argName'': bad path: $error: ".var_export($value, true));
  127. }
  128. /**
  129. * @internal
  130. *
  131. * @param string $argName
  132. * @param mixed $value
  133. * @throws InvalidArgumentException
  134. */
  135. static function checkArgOrNull($argName, $value)
  136. {
  137. if ($value === null) return;
  138. self::checkArg($argName, $value);
  139. }
  140. /**
  141. * @internal
  142. *
  143. * @param string $argName
  144. * @param mixed $value
  145. * @throws InvalidArgumentException
  146. */
  147. static function checkArgNonRoot($argName, $value)
  148. {
  149. if ($value === null) throw new InvalidArgumentException("'$argName' must not be null");
  150. if (!is_string($value)) throw new InvalidArgumentException("'$argName' must be a string");
  151. $error = self::findErrorNonRoot($value);
  152. if ($error !== null) throw new InvalidArgumentException("'$argName'': bad path: $error: ".var_export($value, true));
  153. }
  154. }