diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 07:56:49 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 07:56:49 +0000 |
commit | a415c29efee45520ae252d2aa28f1083a521cd7b (patch) | |
tree | f4ade4b6668ecc0765de7e1424f7c1427ad433ff /wp-includes/Requests/src/Utility | |
parent | Initial commit. (diff) | |
download | wordpress-a415c29efee45520ae252d2aa28f1083a521cd7b.tar.xz wordpress-a415c29efee45520ae252d2aa28f1083a521cd7b.zip |
Adding upstream version 6.4.3+dfsg1.upstream/6.4.3+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'wp-includes/Requests/src/Utility')
3 files changed, 333 insertions, 0 deletions
diff --git a/wp-includes/Requests/src/Utility/CaseInsensitiveDictionary.php b/wp-includes/Requests/src/Utility/CaseInsensitiveDictionary.php new file mode 100644 index 0000000..0e1a914 --- /dev/null +++ b/wp-includes/Requests/src/Utility/CaseInsensitiveDictionary.php @@ -0,0 +1,127 @@ +<?php +/** + * Case-insensitive dictionary, suitable for HTTP headers + * + * @package Requests\Utilities + */ + +namespace WpOrg\Requests\Utility; + +use ArrayAccess; +use ArrayIterator; +use IteratorAggregate; +use ReturnTypeWillChange; +use WpOrg\Requests\Exception; + +/** + * Case-insensitive dictionary, suitable for HTTP headers + * + * @package Requests\Utilities + */ +class CaseInsensitiveDictionary implements ArrayAccess, IteratorAggregate { + /** + * Actual item data + * + * @var array + */ + protected $data = []; + + /** + * Creates a case insensitive dictionary. + * + * @param array $data Dictionary/map to convert to case-insensitive + */ + public function __construct(array $data = []) { + foreach ($data as $offset => $value) { + $this->offsetSet($offset, $value); + } + } + + /** + * Check if the given item exists + * + * @param string $offset Item key + * @return boolean Does the item exist? + */ + #[ReturnTypeWillChange] + public function offsetExists($offset) { + if (is_string($offset)) { + $offset = strtolower($offset); + } + + return isset($this->data[$offset]); + } + + /** + * Get the value for the item + * + * @param string $offset Item key + * @return string|null Item value (null if the item key doesn't exist) + */ + #[ReturnTypeWillChange] + public function offsetGet($offset) { + if (is_string($offset)) { + $offset = strtolower($offset); + } + + if (!isset($this->data[$offset])) { + return null; + } + + return $this->data[$offset]; + } + + /** + * Set the given item + * + * @param string $offset Item name + * @param string $value Item value + * + * @throws \WpOrg\Requests\Exception On attempting to use dictionary as list (`invalidset`) + */ + #[ReturnTypeWillChange] + public function offsetSet($offset, $value) { + if ($offset === null) { + throw new Exception('Object is a dictionary, not a list', 'invalidset'); + } + + if (is_string($offset)) { + $offset = strtolower($offset); + } + + $this->data[$offset] = $value; + } + + /** + * Unset the given header + * + * @param string $offset The key for the item to unset. + */ + #[ReturnTypeWillChange] + public function offsetUnset($offset) { + if (is_string($offset)) { + $offset = strtolower($offset); + } + + unset($this->data[$offset]); + } + + /** + * Get an iterator for the data + * + * @return \ArrayIterator + */ + #[ReturnTypeWillChange] + public function getIterator() { + return new ArrayIterator($this->data); + } + + /** + * Get the headers as an array + * + * @return array Header data + */ + public function getAll() { + return $this->data; + } +} diff --git a/wp-includes/Requests/src/Utility/FilteredIterator.php b/wp-includes/Requests/src/Utility/FilteredIterator.php new file mode 100644 index 0000000..4865966 --- /dev/null +++ b/wp-includes/Requests/src/Utility/FilteredIterator.php @@ -0,0 +1,97 @@ +<?php +/** + * Iterator for arrays requiring filtered values + * + * @package Requests\Utilities + */ + +namespace WpOrg\Requests\Utility; + +use ArrayIterator; +use ReturnTypeWillChange; +use WpOrg\Requests\Exception\InvalidArgument; +use WpOrg\Requests\Utility\InputValidator; + +/** + * Iterator for arrays requiring filtered values + * + * @package Requests\Utilities + */ +final class FilteredIterator extends ArrayIterator { + /** + * Callback to run as a filter + * + * @var callable + */ + private $callback; + + /** + * Create a new iterator + * + * @param array $data The array or object to be iterated on. + * @param callable $callback Callback to be called on each value + * + * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $data argument is not iterable. + */ + public function __construct($data, $callback) { + if (InputValidator::is_iterable($data) === false) { + throw InvalidArgument::create(1, '$data', 'iterable', gettype($data)); + } + + parent::__construct($data); + + if (is_callable($callback)) { + $this->callback = $callback; + } + } + + /** + * Prevent unserialization of the object for security reasons. + * + * @phpcs:disable PHPCompatibility.FunctionNameRestrictions.NewMagicMethods.__unserializeFound + * + * @param array $data Restored array of data originally serialized. + * + * @return void + */ + #[ReturnTypeWillChange] + public function __unserialize($data) {} + // phpcs:enable + + /** + * Perform reinitialization tasks. + * + * Prevents a callback from being injected during unserialization of an object. + * + * @return void + */ + public function __wakeup() { + unset($this->callback); + } + + /** + * Get the current item's value after filtering + * + * @return string + */ + #[ReturnTypeWillChange] + public function current() { + $value = parent::current(); + + if (is_callable($this->callback)) { + $value = call_user_func($this->callback, $value); + } + + return $value; + } + + /** + * Prevent creating a PHP value from a stored representation of the object for security reasons. + * + * @param string $data The serialized string. + * + * @return void + */ + #[ReturnTypeWillChange] + public function unserialize($data) {} +} diff --git a/wp-includes/Requests/src/Utility/InputValidator.php b/wp-includes/Requests/src/Utility/InputValidator.php new file mode 100644 index 0000000..7c10d61 --- /dev/null +++ b/wp-includes/Requests/src/Utility/InputValidator.php @@ -0,0 +1,109 @@ +<?php +/** + * Input validation utilities. + * + * @package Requests\Utilities + */ + +namespace WpOrg\Requests\Utility; + +use ArrayAccess; +use CurlHandle; +use Traversable; + +/** + * Input validation utilities. + * + * @package Requests\Utilities + */ +final class InputValidator { + + /** + * Verify that a received input parameter is of type string or is "stringable". + * + * @param mixed $input Input parameter to verify. + * + * @return bool + */ + public static function is_string_or_stringable($input) { + return is_string($input) || self::is_stringable_object($input); + } + + /** + * Verify whether a received input parameter is usable as an integer array key. + * + * @param mixed $input Input parameter to verify. + * + * @return bool + */ + public static function is_numeric_array_key($input) { + if (is_int($input)) { + return true; + } + + if (!is_string($input)) { + return false; + } + + return (bool) preg_match('`^-?[0-9]+$`', $input); + } + + /** + * Verify whether a received input parameter is "stringable". + * + * @param mixed $input Input parameter to verify. + * + * @return bool + */ + public static function is_stringable_object($input) { + return is_object($input) && method_exists($input, '__toString'); + } + + /** + * Verify whether a received input parameter is _accessible as if it were an array_. + * + * @param mixed $input Input parameter to verify. + * + * @return bool + */ + public static function has_array_access($input) { + return is_array($input) || $input instanceof ArrayAccess; + } + + /** + * Verify whether a received input parameter is "iterable". + * + * @internal The PHP native `is_iterable()` function was only introduced in PHP 7.1 + * and this library still supports PHP 5.6. + * + * @param mixed $input Input parameter to verify. + * + * @return bool + */ + public static function is_iterable($input) { + return is_array($input) || $input instanceof Traversable; + } + + /** + * Verify whether a received input parameter is a Curl handle. + * + * The PHP Curl extension worked with resources prior to PHP 8.0 and with + * an instance of the `CurlHandle` class since PHP 8.0. + * {@link https://www.php.net/manual/en/migration80.incompatible.php#migration80.incompatible.resource2object} + * + * @param mixed $input Input parameter to verify. + * + * @return bool + */ + public static function is_curl_handle($input) { + if (is_resource($input)) { + return get_resource_type($input) === 'curl'; + } + + if (is_object($input)) { + return $input instanceof CurlHandle; + } + + return false; + } +} |