$context_options = []; if (!is_null($context)) { $context_options = stream_context_get_options($context); } foreach ($context_options as $stream => $options) { foreach ($options as $option => $value) { $key = strtolower($stream) . ":" . strtolower($option); switch ($key) { case "curl:curl_verify_ssl_host": curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, !$value ? 0 : 2); break; case "curl:max_redirects": curl_setopt($curl, CURLOPT_MAXREDIRS, $value); break; case "http:follow_location": curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $value); break; case "http:header": if (is_string($value)) { curl_setopt($curl, CURLOPT_HTTPHEADER, [$value]); } else { curl_setopt($curl, CURLOPT_HTTPHEADER, $value); } break; case "http:timeout": curl_setopt($curl, CURLOPT_TIMEOUT, $value); break; case "http:user_agent": curl_setopt($curl, CURLOPT_USERAGENT, $value); break; case "curl:curl_verify_ssl_peer": case "ssl:verify_peer": curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $value); break; } } } $data = curl_exec($curl); if ($data !== false && !curl_errno($curl)) { switch ($http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE)) { case 200: $raw_headers = substr($data, 0, curl_getinfo($curl, CURLINFO_HEADER_SIZE)); $headers = preg_split("/[\n\r]+/", trim($raw_headers)); $content = substr($data, curl_getinfo($curl, CURLINFO_HEADER_SIZE)); break; } } curl_close($curl); } } finally { restore_error_handler(); } return [$content, $headers]; } /** * @param string $str * @return string */ public static function mb_ucwords(string $str): string { $max_len = mb_strlen($str); if ($max_len === 1) { return mb_strtoupper($str); } $str = mb_strtoupper(mb_substr($str, 0, 1)) . mb_substr($str, 1); foreach ([' ', '.', ',', '!', '?', '-', '+'] as $s) { $pos = 0; while (($pos = mb_strpos($str, $s, $pos)) !== false) { $pos++; // Nothing to do if the separator is the last char of the string if ($pos !== false && $pos < $max_len) { // If the char we want to upper is the last char there is nothing to append behind if ($pos + 1 < $max_len) { $str = mb_substr($str, 0, $pos) . mb_strtoupper(mb_substr($str, $pos, 1)) . mb_substr($str, $pos + 1); } else { $str = mb_substr($str, 0, $pos) . mb_strtoupper(mb_substr($str, $pos, 1)); } } } } return $str; } /** * Check whether two lengths should be considered equal, accounting for * inaccuracies in float computation. * * The implementation relies on the fact that we are neither dealing with * very large, nor with very small numbers in layout. Adapted from * https://floating-point-gui.de/errors/comparison/. * * @param float $a * @param float $b * * @return bool */ public static function lengthEqual(float $a, float $b): bool { // The epsilon results in a precision of at least: // * 7 decimal digits at around 1 // * 4 decimal digits at around 1000 (around the size of common paper formats) // * 2 decimal digits at around 100,000 (100,000pt ~ 35.28m) static $epsilon = 1e-8; static $almostZero = 1e-12; $diff = abs($a - $b); if ($a === $b || $diff < $almostZero) { return true; } return $diff < $epsilon * max(abs($a), abs($b)); } /** * Check `$a < $b`, accounting for inaccuracies in float computation. */ public static function lengthLess(float $a, float $b): bool { return $a < $b && !self::lengthEqual($a, $b); } /** * Check `$a <= $b`, accounting for inaccuracies in float computation. */ public static function lengthLessOrEqual(float $a, float $b): bool { return $a <= $b || self::lengthEqual($a, $b); } /** * Check `$a > $b`, accounting for inaccuracies in float computation. */ public static function lengthGreater(float $a, float $b): bool { return $a > $b && !self::lengthEqual($a, $b); } /** * Check `$a >= $b`, accounting for inaccuracies in float computation. */ public static function lengthGreaterOrEqual(float $a, float $b): bool { return $a >= $b || self::lengthEqual($a, $b); } }