| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Utils; |
| 6 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface; |
| 7 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\FulfilledPromise; |
| 8 |
//----------------------------------------------------------------------------- |
| 9 |
// Functional functions |
| 10 |
//----------------------------------------------------------------------------- |
| 11 |
/** |
| 12 |
* Returns a function that always returns the same value; |
| 13 |
* |
| 14 |
* @param mixed $value Value to return. |
| 15 |
* |
| 16 |
* @return callable |
| 17 |
*/ |
| 18 |
function constantly($value) |
| 19 |
{ |
| 20 |
return function () use($value) { |
| 21 |
return $value; |
| 22 |
}; |
| 23 |
} |
| 24 |
/** |
| 25 |
* Filters values that do not satisfy the predicate function $pred. |
| 26 |
* |
| 27 |
* @param mixed $iterable Iterable sequence of data. |
| 28 |
* @param callable $pred Function that accepts a value and returns true/false |
| 29 |
* |
| 30 |
* @return \Generator |
| 31 |
*/ |
| 32 |
function filter($iterable, callable $pred) |
| 33 |
{ |
| 34 |
foreach ($iterable as $value) { |
| 35 |
if ($pred($value)) { |
| 36 |
(yield $value); |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
/** |
| 41 |
* Applies a map function $f to each value in a collection. |
| 42 |
* |
| 43 |
* @param mixed $iterable Iterable sequence of data. |
| 44 |
* @param callable $f Map function to apply. |
| 45 |
* |
| 46 |
* @return \Generator |
| 47 |
*/ |
| 48 |
function map($iterable, callable $f) |
| 49 |
{ |
| 50 |
foreach ($iterable as $value) { |
| 51 |
(yield $f($value)); |
| 52 |
} |
| 53 |
} |
| 54 |
/** |
| 55 |
* Creates a generator that iterates over a sequence, then iterates over each |
| 56 |
* value in the sequence and yields the application of the map function to each |
| 57 |
* value. |
| 58 |
* |
| 59 |
* @param mixed $iterable Iterable sequence of data. |
| 60 |
* @param callable $f Map function to apply. |
| 61 |
* |
| 62 |
* @return \Generator |
| 63 |
*/ |
| 64 |
function flatmap($iterable, callable $f) |
| 65 |
{ |
| 66 |
foreach (map($iterable, $f) as $outer) { |
| 67 |
foreach ($outer as $inner) { |
| 68 |
(yield $inner); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
/** |
| 73 |
* Partitions the input sequence into partitions of the specified size. |
| 74 |
* |
| 75 |
* @param mixed $iterable Iterable sequence of data. |
| 76 |
* @param int $size Size to make each partition (except possibly the last chunk) |
| 77 |
* |
| 78 |
* @return \Generator |
| 79 |
*/ |
| 80 |
function partition($iterable, $size) |
| 81 |
{ |
| 82 |
$buffer = []; |
| 83 |
foreach ($iterable as $value) { |
| 84 |
$buffer[] = $value; |
| 85 |
if (\count($buffer) === $size) { |
| 86 |
(yield $buffer); |
| 87 |
$buffer = []; |
| 88 |
} |
| 89 |
} |
| 90 |
if ($buffer) { |
| 91 |
(yield $buffer); |
| 92 |
} |
| 93 |
} |
| 94 |
/** |
| 95 |
* Returns a function that invokes the provided variadic functions one |
| 96 |
* after the other until one of the functions returns a non-null value. |
| 97 |
* The return function will call each passed function with any arguments it |
| 98 |
* is provided. |
| 99 |
* |
| 100 |
* $a = function ($x, $y) { return null; }; |
| 101 |
* $b = function ($x, $y) { return $x + $y; }; |
| 102 |
* $fn = \Aws\or_chain($a, $b); |
| 103 |
* echo $fn(1, 2); // 3 |
| 104 |
* |
| 105 |
* @return callable |
| 106 |
*/ |
| 107 |
function or_chain() |
| 108 |
{ |
| 109 |
$fns = \func_get_args(); |
| 110 |
return function () use($fns) { |
| 111 |
$args = \func_get_args(); |
| 112 |
foreach ($fns as $fn) { |
| 113 |
$result = $args ? \call_user_func_array($fn, $args) : $fn(); |
| 114 |
if ($result) { |
| 115 |
return $result; |
| 116 |
} |
| 117 |
} |
| 118 |
return null; |
| 119 |
}; |
| 120 |
} |
| 121 |
//----------------------------------------------------------------------------- |
| 122 |
// JSON compiler and loading functions |
| 123 |
//----------------------------------------------------------------------------- |
| 124 |
/** |
| 125 |
* Loads a compiled JSON file from a PHP file. |
| 126 |
* |
| 127 |
* If the JSON file has not been cached to disk as a PHP file, it will be loaded |
| 128 |
* from the JSON source file and returned. |
| 129 |
* |
| 130 |
* @param string $path Path to the JSON file on disk |
| 131 |
* |
| 132 |
* @return mixed Returns the JSON decoded data. Note that JSON objects are |
| 133 |
* decoded as associative arrays. |
| 134 |
*/ |
| 135 |
function load_compiled_json($path) |
| 136 |
{ |
| 137 |
static $compiledList = []; |
| 138 |
$compiledFilepath = "{$path}.php"; |
| 139 |
if (!isset($compiledList[$compiledFilepath])) { |
| 140 |
if (\is_readable($compiledFilepath)) { |
| 141 |
$compiledList[$compiledFilepath] = (include $compiledFilepath); |
| 142 |
} |
| 143 |
} |
| 144 |
if (isset($compiledList[$compiledFilepath])) { |
| 145 |
return $compiledList[$compiledFilepath]; |
| 146 |
} |
| 147 |
if (!\file_exists($path)) { |
| 148 |
throw new \InvalidArgumentException(\sprintf("File not found: %s", $path)); |
| 149 |
} |
| 150 |
return \json_decode(\file_get_contents($path), \true); |
| 151 |
} |
| 152 |
/** |
| 153 |
* No-op |
| 154 |
*/ |
| 155 |
function clear_compiled_json() |
| 156 |
{ |
| 157 |
// pass |
| 158 |
} |
| 159 |
//----------------------------------------------------------------------------- |
| 160 |
// Directory iterator functions. |
| 161 |
//----------------------------------------------------------------------------- |
| 162 |
/** |
| 163 |
* Iterates over the files in a directory and works with custom wrappers. |
| 164 |
* |
| 165 |
* @param string $path Path to open (e.g., "s3://foo/bar"). |
| 166 |
* @param resource $context Stream wrapper context. |
| 167 |
* |
| 168 |
* @return \Generator Yields relative filename strings. |
| 169 |
*/ |
| 170 |
function dir_iterator($path, $context = null) |
| 171 |
{ |
| 172 |
$dh = $context ? \opendir($path, $context) : \opendir($path); |
| 173 |
if (!$dh) { |
| 174 |
throw new \InvalidArgumentException('File not found: ' . $path); |
| 175 |
} |
| 176 |
while (($file = \readdir($dh)) !== \false) { |
| 177 |
(yield $file); |
| 178 |
} |
| 179 |
\closedir($dh); |
| 180 |
} |
| 181 |
/** |
| 182 |
* Returns a recursive directory iterator that yields absolute filenames. |
| 183 |
* |
| 184 |
* This iterator is not broken like PHP's built-in DirectoryIterator (which |
| 185 |
* will read the first file from a stream wrapper, then rewind, then read |
| 186 |
* it again). |
| 187 |
* |
| 188 |
* @param string $path Path to traverse (e.g., s3://bucket/key, /tmp) |
| 189 |
* @param resource $context Stream context options. |
| 190 |
* |
| 191 |
* @return \Generator Yields absolute filenames. |
| 192 |
*/ |
| 193 |
function recursive_dir_iterator($path, $context = null) |
| 194 |
{ |
| 195 |
$invalid = ['.' => \true, '..' => \true]; |
| 196 |
$pathLen = \strlen($path) + 1; |
| 197 |
$iterator = dir_iterator($path, $context); |
| 198 |
$queue = []; |
| 199 |
do { |
| 200 |
while ($iterator->valid()) { |
| 201 |
$file = $iterator->current(); |
| 202 |
$iterator->next(); |
| 203 |
if (isset($invalid[\basename($file)])) { |
| 204 |
continue; |
| 205 |
} |
| 206 |
$fullPath = "{$path}/{$file}"; |
| 207 |
(yield $fullPath); |
| 208 |
if (\is_dir($fullPath)) { |
| 209 |
$queue[] = $iterator; |
| 210 |
$iterator = map(dir_iterator($fullPath, $context), function ($file) use($fullPath, $pathLen) { |
| 211 |
return \substr("{$fullPath}/{$file}", $pathLen); |
| 212 |
}); |
| 213 |
continue; |
| 214 |
} |
| 215 |
} |
| 216 |
$iterator = \array_pop($queue); |
| 217 |
} while ($iterator); |
| 218 |
} |
| 219 |
//----------------------------------------------------------------------------- |
| 220 |
// Misc. functions. |
| 221 |
//----------------------------------------------------------------------------- |
| 222 |
/** |
| 223 |
* Debug function used to describe the provided value type and class. |
| 224 |
* |
| 225 |
* @param mixed $input |
| 226 |
* |
| 227 |
* @return string Returns a string containing the type of the variable and |
| 228 |
* if a class is provided, the class name. |
| 229 |
*/ |
| 230 |
function describe_type($input) |
| 231 |
{ |
| 232 |
switch (\gettype($input)) { |
| 233 |
case 'object': |
| 234 |
return 'object(' . \get_class($input) . ')'; |
| 235 |
case 'array': |
| 236 |
return 'array(' . \count($input) . ')'; |
| 237 |
default: |
| 238 |
\ob_start(); |
| 239 |
\var_dump($input); |
| 240 |
// normalize float vs double |
| 241 |
return \str_replace('double(', 'float(', \rtrim(\ob_get_clean())); |
| 242 |
} |
| 243 |
} |
| 244 |
/** |
| 245 |
* Creates a default HTTP handler based on the available clients. |
| 246 |
* |
| 247 |
* @return callable |
| 248 |
*/ |
| 249 |
function default_http_handler() |
| 250 |
{ |
| 251 |
return new \Dudlewebs\WPMCS\s3\Aws\Handler\Guzzle\GuzzleHandler(); |
| 252 |
} |
| 253 |
/** |
| 254 |
* Gets the default user agent string depending on the Guzzle version |
| 255 |
* |
| 256 |
* @return string |
| 257 |
*/ |
| 258 |
function default_user_agent() |
| 259 |
{ |
| 260 |
return Utils::defaultUserAgent(); |
| 261 |
} |
| 262 |
/** |
| 263 |
* Serialize a request for a command but do not send it. |
| 264 |
* |
| 265 |
* Returns a promise that is fulfilled with the serialized request. |
| 266 |
* |
| 267 |
* @param CommandInterface $command Command to serialize. |
| 268 |
* |
| 269 |
* @return RequestInterface |
| 270 |
* @throws \RuntimeException |
| 271 |
*/ |
| 272 |
function serialize(CommandInterface $command) |
| 273 |
{ |
| 274 |
$request = null; |
| 275 |
$handlerList = $command->getHandlerList(); |
| 276 |
// Return a mock result. |
| 277 |
$handlerList->setHandler(function (CommandInterface $_, RequestInterface $r) use(&$request) { |
| 278 |
$request = $r; |
| 279 |
return new FulfilledPromise(new Result([])); |
| 280 |
}); |
| 281 |
\call_user_func($handlerList->resolve(), $command)->wait(); |
| 282 |
if (!$request instanceof RequestInterface) { |
| 283 |
throw new \RuntimeException('Calling handler did not serialize request'); |
| 284 |
} |
| 285 |
return $request; |
| 286 |
} |
| 287 |
/** |
| 288 |
* Retrieves data for a service from the SDK's service manifest file. |
| 289 |
* |
| 290 |
* Manifest data is stored statically, so it does not need to be loaded more |
| 291 |
* than once per process. The JSON data is also cached in opcache. |
| 292 |
* |
| 293 |
* @param string $service Case-insensitive namespace or endpoint prefix of the |
| 294 |
* service for which you are retrieving manifest data. |
| 295 |
* |
| 296 |
* @return array |
| 297 |
* @throws \InvalidArgumentException if the service is not supported. |
| 298 |
*/ |
| 299 |
function manifest($service = null) |
| 300 |
{ |
| 301 |
// Load the manifest and create aliases for lowercased namespaces |
| 302 |
static $manifest = []; |
| 303 |
static $aliases = []; |
| 304 |
if (empty($manifest)) { |
| 305 |
$manifest = load_compiled_json(__DIR__ . '/data/manifest.json'); |
| 306 |
foreach ($manifest as $endpoint => $info) { |
| 307 |
$alias = \strtolower($info['namespace']); |
| 308 |
if ($alias !== $endpoint) { |
| 309 |
$aliases[$alias] = $endpoint; |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
// If no service specified, then return the whole manifest. |
| 314 |
if ($service === null) { |
| 315 |
return $manifest; |
| 316 |
} |
| 317 |
// Look up the service's info in the manifest data. |
| 318 |
$service = \strtolower($service); |
| 319 |
if (isset($manifest[$service])) { |
| 320 |
return $manifest[$service] + ['endpoint' => $service]; |
| 321 |
} |
| 322 |
if (isset($aliases[$service])) { |
| 323 |
return manifest($aliases[$service]); |
| 324 |
} |
| 325 |
throw new \InvalidArgumentException("The service \"{$service}\" is not provided by the AWS SDK for PHP."); |
| 326 |
} |
| 327 |
/** |
| 328 |
* Checks if supplied parameter is a valid hostname |
| 329 |
* |
| 330 |
* @param string $hostname |
| 331 |
* @return bool |
| 332 |
*/ |
| 333 |
function is_valid_hostname($hostname) |
| 334 |
{ |
| 335 |
return \preg_match("/^([a-z\\d](-*[a-z\\d])*)(\\.([a-z\\d](-*[a-z\\d])*))*\\.?\$/i", $hostname) && \preg_match("/^.{1,253}\$/", $hostname) && \preg_match("/^[^\\.]{1,63}(\\.[^\\.]{0,63})*\$/", $hostname); |
| 336 |
} |
| 337 |
/** |
| 338 |
* Checks if supplied parameter is a valid host label |
| 339 |
* |
| 340 |
* @param $label |
| 341 |
* @return bool |
| 342 |
*/ |
| 343 |
function is_valid_hostlabel($label) |
| 344 |
{ |
| 345 |
return \preg_match("/^(?!-)[a-zA-Z0-9-]{1,63}(?<!-)\$/", $label); |
| 346 |
} |
| 347 |
/** |
| 348 |
* Ignores '#' full line comments, which parse_ini_file no longer does |
| 349 |
* in PHP 7+. |
| 350 |
* |
| 351 |
* @param $filename |
| 352 |
* @param bool $process_sections |
| 353 |
* @param int $scanner_mode |
| 354 |
* @return array|bool |
| 355 |
*/ |
| 356 |
function parse_ini_file($filename, $process_sections = \false, $scanner_mode = \INI_SCANNER_NORMAL) |
| 357 |
{ |
| 358 |
return \parse_ini_string(\preg_replace('/^#.*\\n/m', "", \file_get_contents($filename)), $process_sections, $scanner_mode); |
| 359 |
} |
| 360 |
/** |
| 361 |
* Outputs boolean value of input for a select range of possible values, |
| 362 |
* null otherwise |
| 363 |
* |
| 364 |
* @param $input |
| 365 |
* @return bool|null |
| 366 |
*/ |
| 367 |
function boolean_value($input) |
| 368 |
{ |
| 369 |
if (\is_bool($input)) { |
| 370 |
return $input; |
| 371 |
} |
| 372 |
if ($input === 0) { |
| 373 |
return \false; |
| 374 |
} |
| 375 |
if ($input === 1) { |
| 376 |
return \true; |
| 377 |
} |
| 378 |
if (\is_string($input)) { |
| 379 |
switch (\strtolower($input)) { |
| 380 |
case "true": |
| 381 |
case "on": |
| 382 |
case "1": |
| 383 |
return \true; |
| 384 |
break; |
| 385 |
case "false": |
| 386 |
case "off": |
| 387 |
case "0": |
| 388 |
return \false; |
| 389 |
break; |
| 390 |
} |
| 391 |
} |
| 392 |
return null; |
| 393 |
} |
| 394 |
/** |
| 395 |
* Parses ini sections with subsections (i.e. the service section) |
| 396 |
* |
| 397 |
* @param $filename |
| 398 |
* @param $filename |
| 399 |
* @return array |
| 400 |
*/ |
| 401 |
function parse_ini_section_with_subsections($filename, $section_name) |
| 402 |
{ |
| 403 |
$config = []; |
| 404 |
$stream = \fopen($filename, 'r'); |
| 405 |
if (!$stream) { |
| 406 |
return $config; |
| 407 |
} |
| 408 |
$current_subsection = ''; |
| 409 |
while (!\feof($stream)) { |
| 410 |
$line = \trim(\fgets($stream)); |
| 411 |
if (empty($line) || \in_array($line[0], [';', '#'])) { |
| 412 |
continue; |
| 413 |
} |
| 414 |
if (\preg_match('/^\\[.*\\]$/', $line) && \trim($line, '[]') === $section_name) { |
| 415 |
while (!\feof($stream)) { |
| 416 |
$line = \trim(\fgets($stream)); |
| 417 |
if (empty($line) || \in_array($line[0], [';', '#'])) { |
| 418 |
continue; |
| 419 |
} |
| 420 |
if (\preg_match('/^\\[.*\\]$/', $line) && \trim($line, '[]') === $section_name) { |
| 421 |
continue; |
| 422 |
} elseif (\strpos($line, '[') === 0) { |
| 423 |
break; |
| 424 |
} |
| 425 |
if (\strpos($line, ' = ') !== \false) { |
| 426 |
list($key, $value) = \explode(' = ', $line, 2); |
| 427 |
if (empty($current_subsection)) { |
| 428 |
$config[$key] = $value; |
| 429 |
} else { |
| 430 |
$config[$current_subsection][$key] = $value; |
| 431 |
} |
| 432 |
} else { |
| 433 |
$current_subsection = \trim(\str_replace('=', '', $line)); |
| 434 |
$config[$current_subsection] = []; |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
\fclose($stream); |
| 440 |
return $config; |
| 441 |
} |
| 442 |
/** |
| 443 |
* Checks if an input is a valid epoch time |
| 444 |
* |
| 445 |
* @param $input |
| 446 |
* @return bool |
| 447 |
*/ |
| 448 |
function is_valid_epoch($input) |
| 449 |
{ |
| 450 |
if (\is_string($input) || \is_numeric($input)) { |
| 451 |
if (\is_string($input) && !\preg_match("/^-?[0-9]+\\.?[0-9]*\$/", $input)) { |
| 452 |
return \false; |
| 453 |
} |
| 454 |
return \true; |
| 455 |
} |
| 456 |
return \false; |
| 457 |
} |
| 458 |
/** |
| 459 |
* Checks if an input is a fips pseudo region |
| 460 |
* |
| 461 |
* @param $region |
| 462 |
* @return bool |
| 463 |
*/ |
| 464 |
function is_fips_pseudo_region($region) |
| 465 |
{ |
| 466 |
return \strpos($region, 'fips-') !== \false || \strpos($region, '-fips') !== \false; |
| 467 |
} |
| 468 |
/** |
| 469 |
* Returns a region without a fips label |
| 470 |
* |
| 471 |
* @param $region |
| 472 |
* @return string |
| 473 |
*/ |
| 474 |
function strip_fips_pseudo_regions($region) |
| 475 |
{ |
| 476 |
return \str_replace(['fips-', '-fips'], ['', ''], $region); |
| 477 |
} |
| 478 |
/** |
| 479 |
* Checks if an array is associative |
| 480 |
* |
| 481 |
* @param array $array |
| 482 |
* |
| 483 |
* @return bool |
| 484 |
*/ |
| 485 |
function is_associative(array $array) : bool |
| 486 |
{ |
| 487 |
if (empty($array)) { |
| 488 |
return \false; |
| 489 |
} |
| 490 |
return !\array_is_list($array); |
| 491 |
} |
| 492 |
|