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