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