@@ -15,7 +15,8 @@ discard block |
||
| 15 | 15 | * @package Requests |
| 16 | 16 | * @subpackage Utilities |
| 17 | 17 | */ |
| 18 | -class Requests_IPv6 { |
|
| 18 | +class Requests_IPv6 |
|
| 19 | +{ |
|
| 19 | 20 | /** |
| 20 | 21 | * Uncompresses an IPv6 address |
| 21 | 22 | * |
@@ -34,8 +35,10 @@ discard block |
||
| 34 | 35 | * @param string $ip An IPv6 address |
| 35 | 36 | * @return string The uncompressed IPv6 address |
| 36 | 37 | */ |
| 37 | - public static function uncompress($ip) { |
|
| 38 | - if (substr_count($ip, '::') !== 1) { |
|
| 38 | + public static function uncompress($ip) |
|
| 39 | + { |
|
| 40 | + if (substr_count($ip, '::') !== 1) |
|
| 41 | + { |
|
| 39 | 42 | return $ip; |
| 40 | 43 | } |
| 41 | 44 | |
@@ -43,25 +46,30 @@ discard block |
||
| 43 | 46 | $c1 = ($ip1 === '') ? -1 : substr_count($ip1, ':'); |
| 44 | 47 | $c2 = ($ip2 === '') ? -1 : substr_count($ip2, ':'); |
| 45 | 48 | |
| 46 | - if (strpos($ip2, '.') !== false) { |
|
| 49 | + if (strpos($ip2, '.') !== false) |
|
| 50 | + { |
|
| 47 | 51 | $c2++; |
| 48 | 52 | } |
| 49 | 53 | // :: |
| 50 | - if ($c1 === -1 && $c2 === -1) { |
|
| 54 | + if ($c1 === -1 && $c2 === -1) |
|
| 55 | + { |
|
| 51 | 56 | $ip = '0:0:0:0:0:0:0:0'; |
| 52 | 57 | } |
| 53 | 58 | // ::xxx |
| 54 | - else if ($c1 === -1) { |
|
| 59 | + else if ($c1 === -1) |
|
| 60 | + { |
|
| 55 | 61 | $fill = str_repeat('0:', 7 - $c2); |
| 56 | 62 | $ip = str_replace('::', $fill, $ip); |
| 57 | 63 | } |
| 58 | 64 | // xxx:: |
| 59 | - else if ($c2 === -1) { |
|
| 65 | + else if ($c2 === -1) |
|
| 66 | + { |
|
| 60 | 67 | $fill = str_repeat(':0', 7 - $c1); |
| 61 | 68 | $ip = str_replace('::', $fill, $ip); |
| 62 | 69 | } |
| 63 | 70 | // xxx::xxx |
| 64 | - else { |
|
| 71 | + else |
|
| 72 | + { |
|
| 65 | 73 | $fill = ':' . str_repeat('0:', 6 - $c2 - $c1); |
| 66 | 74 | $ip = str_replace('::', $fill, $ip); |
| 67 | 75 | } |
@@ -82,7 +90,8 @@ discard block |
||
| 82 | 90 | * @param string $ip An IPv6 address |
| 83 | 91 | * @return string The compressed IPv6 address |
| 84 | 92 | */ |
| 85 | - public static function compress($ip) { |
|
| 93 | + public static function compress($ip) |
|
| 94 | + { |
|
| 86 | 95 | // Prepare the IP to be compressed |
| 87 | 96 | $ip = self::uncompress($ip); |
| 88 | 97 | $ip_parts = self::split_v6_v4($ip); |
@@ -91,11 +100,14 @@ discard block |
||
| 91 | 100 | $ip_parts[0] = preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]); |
| 92 | 101 | |
| 93 | 102 | // Find bunches of zeros |
| 94 | - if (preg_match_all('/(?:^|:)(?:0(?::|$))+/', $ip_parts[0], $matches, PREG_OFFSET_CAPTURE)) { |
|
| 103 | + if (preg_match_all('/(?:^|:)(?:0(?::|$))+/', $ip_parts[0], $matches, PREG_OFFSET_CAPTURE)) |
|
| 104 | + { |
|
| 95 | 105 | $max = 0; |
| 96 | 106 | $pos = null; |
| 97 | - foreach ($matches[0] as $match) { |
|
| 98 | - if (strlen($match[0]) > $max) { |
|
| 107 | + foreach ($matches[0] as $match) |
|
| 108 | + { |
|
| 109 | + if (strlen($match[0]) > $max) |
|
| 110 | + { |
|
| 99 | 111 | $max = strlen($match[0]); |
| 100 | 112 | $pos = $match[1]; |
| 101 | 113 | } |
@@ -104,10 +116,12 @@ discard block |
||
| 104 | 116 | $ip_parts[0] = substr_replace($ip_parts[0], '::', $pos, $max); |
| 105 | 117 | } |
| 106 | 118 | |
| 107 | - if ($ip_parts[1] !== '') { |
|
| 119 | + if ($ip_parts[1] !== '') |
|
| 120 | + { |
|
| 108 | 121 | return implode(':', $ip_parts); |
| 109 | 122 | } |
| 110 | - else { |
|
| 123 | + else |
|
| 124 | + { |
|
| 111 | 125 | return $ip_parts[0]; |
| 112 | 126 | } |
| 113 | 127 | } |
@@ -124,14 +138,17 @@ discard block |
||
| 124 | 138 | * @param string $ip An IPv6 address |
| 125 | 139 | * @return string[] [0] contains the IPv6 represented part, and [1] the IPv4 represented part |
| 126 | 140 | */ |
| 127 | - protected static function split_v6_v4($ip) { |
|
| 128 | - if (strpos($ip, '.') !== false) { |
|
| 141 | + protected static function split_v6_v4($ip) |
|
| 142 | + { |
|
| 143 | + if (strpos($ip, '.') !== false) |
|
| 144 | + { |
|
| 129 | 145 | $pos = strrpos($ip, ':'); |
| 130 | 146 | $ipv6_part = substr($ip, 0, $pos); |
| 131 | 147 | $ipv4_part = substr($ip, $pos + 1); |
| 132 | 148 | return array($ipv6_part, $ipv4_part); |
| 133 | 149 | } |
| 134 | - else { |
|
| 150 | + else |
|
| 151 | + { |
|
| 135 | 152 | return array($ip, ''); |
| 136 | 153 | } |
| 137 | 154 | } |
@@ -144,46 +161,57 @@ discard block |
||
| 144 | 161 | * @param string $ip An IPv6 address |
| 145 | 162 | * @return bool true if $ip is a valid IPv6 address |
| 146 | 163 | */ |
| 147 | - public static function check_ipv6($ip) { |
|
| 164 | + public static function check_ipv6($ip) |
|
| 165 | + { |
|
| 148 | 166 | $ip = self::uncompress($ip); |
| 149 | 167 | list($ipv6, $ipv4) = self::split_v6_v4($ip); |
| 150 | 168 | $ipv6 = explode(':', $ipv6); |
| 151 | 169 | $ipv4 = explode('.', $ipv4); |
| 152 | - if (count($ipv6) === 8 && count($ipv4) === 1 || count($ipv6) === 6 && count($ipv4) === 4) { |
|
| 153 | - foreach ($ipv6 as $ipv6_part) { |
|
| 170 | + if (count($ipv6) === 8 && count($ipv4) === 1 || count($ipv6) === 6 && count($ipv4) === 4) |
|
| 171 | + { |
|
| 172 | + foreach ($ipv6 as $ipv6_part) |
|
| 173 | + { |
|
| 154 | 174 | // The section can't be empty |
| 155 | - if ($ipv6_part === '') { |
|
| 175 | + if ($ipv6_part === '') |
|
| 176 | + { |
|
| 156 | 177 | return false; |
| 157 | 178 | } |
| 158 | 179 | |
| 159 | 180 | // Nor can it be over four characters |
| 160 | - if (strlen($ipv6_part) > 4) { |
|
| 181 | + if (strlen($ipv6_part) > 4) |
|
| 182 | + { |
|
| 161 | 183 | return false; |
| 162 | 184 | } |
| 163 | 185 | |
| 164 | 186 | // Remove leading zeros (this is safe because of the above) |
| 165 | 187 | $ipv6_part = ltrim($ipv6_part, '0'); |
| 166 | - if ($ipv6_part === '') { |
|
| 188 | + if ($ipv6_part === '') |
|
| 189 | + { |
|
| 167 | 190 | $ipv6_part = '0'; |
| 168 | 191 | } |
| 169 | 192 | |
| 170 | 193 | // Check the value is valid |
| 171 | 194 | $value = hexdec($ipv6_part); |
| 172 | - if (dechex($value) !== strtolower($ipv6_part) || $value < 0 || $value > 0xFFFF) { |
|
| 195 | + if (dechex($value) !== strtolower($ipv6_part) || $value < 0 || $value > 0xFFFF) |
|
| 196 | + { |
|
| 173 | 197 | return false; |
| 174 | 198 | } |
| 175 | 199 | } |
| 176 | - if (count($ipv4) === 4) { |
|
| 177 | - foreach ($ipv4 as $ipv4_part) { |
|
| 200 | + if (count($ipv4) === 4) |
|
| 201 | + { |
|
| 202 | + foreach ($ipv4 as $ipv4_part) |
|
| 203 | + { |
|
| 178 | 204 | $value = (int) $ipv4_part; |
| 179 | - if ((string) $value !== $ipv4_part || $value < 0 || $value > 0xFF) { |
|
| 205 | + if ((string) $value !== $ipv4_part || $value < 0 || $value > 0xFF) |
|
| 206 | + { |
|
| 180 | 207 | return false; |
| 181 | 208 | } |
| 182 | 209 | } |
| 183 | 210 | } |
| 184 | 211 | return true; |
| 185 | 212 | } |
| 186 | - else { |
|
| 213 | + else |
|
| 214 | + { |
|
| 187 | 215 | return false; |
| 188 | 216 | } |
| 189 | 217 | } |
@@ -10,7 +10,8 @@ discard block |
||
| 10 | 10 | * |
| 11 | 11 | * @package Requests |
| 12 | 12 | */ |
| 13 | -class Requests_Response_Headers extends Requests_Utility_CaseInsensitiveDictionary { |
|
| 13 | +class Requests_Response_Headers extends Requests_Utility_CaseInsensitiveDictionary |
|
| 14 | +{ |
|
| 14 | 15 | /** |
| 15 | 16 | * Get the given header |
| 16 | 17 | * |
@@ -23,9 +24,11 @@ discard block |
||
| 23 | 24 | * @param string $key |
| 24 | 25 | * @return string Header value |
| 25 | 26 | */ |
| 26 | - public function offsetGet($key) { |
|
| 27 | + public function offsetGet($key) |
|
| 28 | + { |
|
| 27 | 29 | $key = strtolower($key); |
| 28 | - if (!isset($this->data[$key])) { |
|
| 30 | + if (!isset($this->data[$key])) |
|
| 31 | + { |
|
| 29 | 32 | return null; |
| 30 | 33 | } |
| 31 | 34 | |
@@ -40,14 +43,17 @@ discard block |
||
| 40 | 43 | * @param string $key Item name |
| 41 | 44 | * @param string $value Item value |
| 42 | 45 | */ |
| 43 | - public function offsetSet($key, $value) { |
|
| 44 | - if ($key === null) { |
|
| 46 | + public function offsetSet($key, $value) |
|
| 47 | + { |
|
| 48 | + if ($key === null) |
|
| 49 | + { |
|
| 45 | 50 | throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset'); |
| 46 | 51 | } |
| 47 | 52 | |
| 48 | 53 | $key = strtolower($key); |
| 49 | 54 | |
| 50 | - if (!isset($this->data[$key])) { |
|
| 55 | + if (!isset($this->data[$key])) |
|
| 56 | + { |
|
| 51 | 57 | $this->data[$key] = array(); |
| 52 | 58 | } |
| 53 | 59 | |
@@ -60,9 +66,11 @@ discard block |
||
| 60 | 66 | * @param string $key |
| 61 | 67 | * @return array Header values |
| 62 | 68 | */ |
| 63 | - public function getValues($key) { |
|
| 69 | + public function getValues($key) |
|
| 70 | + { |
|
| 64 | 71 | $key = strtolower($key); |
| 65 | - if (!isset($this->data[$key])) { |
|
| 72 | + if (!isset($this->data[$key])) |
|
| 73 | + { |
|
| 66 | 74 | return null; |
| 67 | 75 | } |
| 68 | 76 | |
@@ -78,8 +86,10 @@ discard block |
||
| 78 | 86 | * @param string|array $value Value to flatten |
| 79 | 87 | * @return string Flattened value |
| 80 | 88 | */ |
| 81 | - public function flatten($value) { |
|
| 82 | - if (is_array($value)) { |
|
| 89 | + public function flatten($value) |
|
| 90 | + { |
|
| 91 | + if (is_array($value)) |
|
| 92 | + { |
|
| 83 | 93 | $value = implode(',', $value); |
| 84 | 94 | } |
| 85 | 95 | |
@@ -92,7 +102,8 @@ discard block |
||
| 92 | 102 | * Converts the internal |
| 93 | 103 | * @return ArrayIterator |
| 94 | 104 | */ |
| 95 | - public function getIterator() { |
|
| 105 | + public function getIterator() |
|
| 106 | + { |
|
| 96 | 107 | return new Requests_Utility_FilteredIterator($this->data, array($this, 'flatten')); |
| 97 | 108 | } |
| 98 | 109 | } |
@@ -12,11 +12,13 @@ discard block |
||
| 12 | 12 | * Contains a response from Requests::request() |
| 13 | 13 | * @package Requests |
| 14 | 14 | */ |
| 15 | -class Requests_Response { |
|
| 15 | +class Requests_Response |
|
| 16 | +{ |
|
| 16 | 17 | /** |
| 17 | 18 | * Constructor |
| 18 | 19 | */ |
| 19 | - public function __construct() { |
|
| 20 | + public function __construct() |
|
| 21 | + { |
|
| 20 | 22 | $this->headers = new Requests_Response_Headers(); |
| 21 | 23 | $this->cookies = new Requests_Cookie_Jar(); |
| 22 | 24 | } |
@@ -95,7 +97,8 @@ discard block |
||
| 95 | 97 | * |
| 96 | 98 | * @return boolean True if redirect (3xx status), false if not. |
| 97 | 99 | */ |
| 98 | - public function is_redirect() { |
|
| 100 | + public function is_redirect() |
|
| 101 | + { |
|
| 99 | 102 | $code = $this->status_code; |
| 100 | 103 | return in_array($code, array(300, 301, 302, 303, 307)) || $code > 307 && $code < 400; |
| 101 | 104 | } |
@@ -107,13 +110,17 @@ discard block |
||
| 107 | 110 | * @throws Requests_Exception_HTTP On non-successful status code. Exception class corresponds to code (e.g. {@see Requests_Exception_HTTP_404}) |
| 108 | 111 | * @param boolean $allow_redirects Set to false to throw on a 3xx as well |
| 109 | 112 | */ |
| 110 | - public function throw_for_status($allow_redirects = true) { |
|
| 111 | - if ($this->is_redirect()) { |
|
| 112 | - if (!$allow_redirects) { |
|
| 113 | + public function throw_for_status($allow_redirects = true) |
|
| 114 | + { |
|
| 115 | + if ($this->is_redirect()) |
|
| 116 | + { |
|
| 117 | + if (!$allow_redirects) |
|
| 118 | + { |
|
| 113 | 119 | throw new Requests_Exception('Redirection not allowed', 'response.no_redirects', $this); |
| 114 | 120 | } |
| 115 | 121 | } |
| 116 | - elseif (!$this->success) { |
|
| 122 | + elseif (!$this->success) |
|
| 123 | + { |
|
| 117 | 124 | $exception = Requests_Exception_HTTP::get_class($this->status_code); |
| 118 | 125 | throw new $exception(null, $this); |
| 119 | 126 | } |
@@ -20,7 +20,8 @@ |
||
| 20 | 20 | * @subpackage Proxy |
| 21 | 21 | * @since 1.6 |
| 22 | 22 | */ |
| 23 | -interface Requests_Proxy { |
|
| 23 | +interface Requests_Proxy |
|
| 24 | +{ |
|
| 24 | 25 | /** |
| 25 | 26 | * Register hooks as needed |
| 26 | 27 | * |
@@ -10,7 +10,8 @@ discard block |
||
| 10 | 10 | * |
| 11 | 11 | * @package Requests |
| 12 | 12 | */ |
| 13 | -class Requests_Exception extends Exception { |
|
| 13 | +class Requests_Exception extends Exception |
|
| 14 | +{ |
|
| 14 | 15 | /** |
| 15 | 16 | * Type of exception |
| 16 | 17 | * |
@@ -33,7 +34,8 @@ discard block |
||
| 33 | 34 | * @param mixed $data Associated data |
| 34 | 35 | * @param integer $code Exception numerical code, if applicable |
| 35 | 36 | */ |
| 36 | - public function __construct($message, $type, $data = null, $code = 0) { |
|
| 37 | + public function __construct($message, $type, $data = null, $code = 0) |
|
| 38 | + { |
|
| 37 | 39 | parent::__construct($message, $code); |
| 38 | 40 | |
| 39 | 41 | $this->type = $type; |
@@ -46,7 +48,8 @@ discard block |
||
| 46 | 48 | * @codeCoverageIgnore |
| 47 | 49 | * @return string |
| 48 | 50 | */ |
| 49 | - public function getType() { |
|
| 51 | + public function getType() |
|
| 52 | + { |
|
| 50 | 53 | return $this->type; |
| 51 | 54 | } |
| 52 | 55 | |
@@ -56,7 +59,8 @@ discard block |
||
| 56 | 59 | * @codeCoverageIgnore |
| 57 | 60 | * @return mixed |
| 58 | 61 | */ |
| 59 | - public function getData() { |
|
| 62 | + public function getData() |
|
| 63 | + { |
|
| 60 | 64 | return $this->data; |
| 61 | 65 | } |
| 62 | 66 | } |
| 63 | 67 | \ No newline at end of file |
@@ -10,7 +10,8 @@ discard block |
||
| 10 | 10 | * @see https://tools.ietf.org/html/rfc3490 IDNA specification |
| 11 | 11 | * @see https://tools.ietf.org/html/rfc3492 Punycode/Bootstrap specification |
| 12 | 12 | */ |
| 13 | -class Requests_IDNAEncoder { |
|
| 13 | +class Requests_IDNAEncoder |
|
| 14 | +{ |
|
| 14 | 15 | /** |
| 15 | 16 | * ACE prefix used for IDNA |
| 16 | 17 | * |
@@ -40,9 +41,11 @@ discard block |
||
| 40 | 41 | * @param string $string Hostname |
| 41 | 42 | * @return string Punycode-encoded hostname |
| 42 | 43 | */ |
| 43 | - public static function encode($string) { |
|
| 44 | + public static function encode($string) |
|
| 45 | + { |
|
| 44 | 46 | $parts = explode('.', $string); |
| 45 | - foreach ($parts as &$part) { |
|
| 47 | + foreach ($parts as &$part) |
|
| 48 | + { |
|
| 46 | 49 | $part = self::to_ascii($part); |
| 47 | 50 | } |
| 48 | 51 | return implode('.', $parts); |
@@ -59,11 +62,14 @@ discard block |
||
| 59 | 62 | * @param string $string ASCII or UTF-8 string (max length 64 characters) |
| 60 | 63 | * @return string ASCII string |
| 61 | 64 | */ |
| 62 | - public static function to_ascii($string) { |
|
| 65 | + public static function to_ascii($string) |
|
| 66 | + { |
|
| 63 | 67 | // Step 1: Check if the string is already ASCII |
| 64 | - if (self::is_ascii($string)) { |
|
| 68 | + if (self::is_ascii($string)) |
|
| 69 | + { |
|
| 65 | 70 | // Skip to step 7 |
| 66 | - if (strlen($string) < 64) { |
|
| 71 | + if (strlen($string) < 64) |
|
| 72 | + { |
|
| 67 | 73 | return $string; |
| 68 | 74 | } |
| 69 | 75 | |
@@ -75,9 +81,11 @@ discard block |
||
| 75 | 81 | |
| 76 | 82 | // Step 3: UseSTD3ASCIIRules is false, continue |
| 77 | 83 | // Step 4: Check if it's ASCII now |
| 78 | - if (self::is_ascii($string)) { |
|
| 84 | + if (self::is_ascii($string)) |
|
| 85 | + { |
|
| 79 | 86 | // Skip to step 7 |
| 80 | - if (strlen($string) < 64) { |
|
| 87 | + if (strlen($string) < 64) |
|
| 88 | + { |
|
| 81 | 89 | return $string; |
| 82 | 90 | } |
| 83 | 91 | |
@@ -85,7 +93,8 @@ discard block |
||
| 85 | 93 | } |
| 86 | 94 | |
| 87 | 95 | // Step 5: Check ACE prefix |
| 88 | - if (strpos($string, self::ACE_PREFIX) === 0) { |
|
| 96 | + if (strpos($string, self::ACE_PREFIX) === 0) |
|
| 97 | + { |
|
| 89 | 98 | throw new Requests_Exception('Provided string begins with ACE prefix', 'idna.provided_is_prefixed', $string); |
| 90 | 99 | } |
| 91 | 100 | |
@@ -96,7 +105,8 @@ discard block |
||
| 96 | 105 | $string = self::ACE_PREFIX . $string; |
| 97 | 106 | |
| 98 | 107 | // Step 8: Check size |
| 99 | - if (strlen($string) < 64) { |
|
| 108 | + if (strlen($string) < 64) |
|
| 109 | + { |
|
| 100 | 110 | return $string; |
| 101 | 111 | } |
| 102 | 112 | |
@@ -111,7 +121,8 @@ discard block |
||
| 111 | 121 | * @param string $string |
| 112 | 122 | * @return bool Is the string ASCII-only? |
| 113 | 123 | */ |
| 114 | - protected static function is_ascii($string) { |
|
| 124 | + protected static function is_ascii($string) |
|
| 125 | + { |
|
| 115 | 126 | return (preg_match('/(?:[^\x00-\x7F])/', $string) !== 1); |
| 116 | 127 | } |
| 117 | 128 | |
@@ -122,7 +133,8 @@ discard block |
||
| 122 | 133 | * @param string $string |
| 123 | 134 | * @return string Prepared string |
| 124 | 135 | */ |
| 125 | - protected static function nameprep($string) { |
|
| 136 | + protected static function nameprep($string) |
|
| 137 | + { |
|
| 126 | 138 | return $string; |
| 127 | 139 | } |
| 128 | 140 | |
@@ -135,53 +147,64 @@ discard block |
||
| 135 | 147 | * @param string $input |
| 136 | 148 | * @return array Unicode code points |
| 137 | 149 | */ |
| 138 | - protected static function utf8_to_codepoints($input) { |
|
| 150 | + protected static function utf8_to_codepoints($input) |
|
| 151 | + { |
|
| 139 | 152 | $codepoints = array(); |
| 140 | 153 | |
| 141 | 154 | // Get number of bytes |
| 142 | 155 | $strlen = strlen($input); |
| 143 | 156 | |
| 144 | - for ($position = 0; $position < $strlen; $position++) { |
|
| 157 | + for ($position = 0; $position < $strlen; $position++) |
|
| 158 | + { |
|
| 145 | 159 | $value = ord($input[$position]); |
| 146 | 160 | |
| 147 | 161 | // One byte sequence: |
| 148 | - if ((~$value & 0x80) === 0x80) { |
|
| 162 | + if ((~$value & 0x80) === 0x80) |
|
| 163 | + { |
|
| 149 | 164 | $character = $value; |
| 150 | 165 | $length = 1; |
| 151 | 166 | $remaining = 0; |
| 152 | 167 | } |
| 153 | 168 | // Two byte sequence: |
| 154 | - elseif (($value & 0xE0) === 0xC0) { |
|
| 169 | + elseif (($value & 0xE0) === 0xC0) |
|
| 170 | + { |
|
| 155 | 171 | $character = ($value & 0x1F) << 6; |
| 156 | 172 | $length = 2; |
| 157 | 173 | $remaining = 1; |
| 158 | 174 | } |
| 159 | 175 | // Three byte sequence: |
| 160 | - elseif (($value & 0xF0) === 0xE0) { |
|
| 176 | + elseif (($value & 0xF0) === 0xE0) |
|
| 177 | + { |
|
| 161 | 178 | $character = ($value & 0x0F) << 12; |
| 162 | 179 | $length = 3; |
| 163 | 180 | $remaining = 2; |
| 164 | 181 | } |
| 165 | 182 | // Four byte sequence: |
| 166 | - elseif (($value & 0xF8) === 0xF0) { |
|
| 183 | + elseif (($value & 0xF8) === 0xF0) |
|
| 184 | + { |
|
| 167 | 185 | $character = ($value & 0x07) << 18; |
| 168 | 186 | $length = 4; |
| 169 | 187 | $remaining = 3; |
| 170 | 188 | } |
| 171 | 189 | // Invalid byte: |
| 172 | - else { |
|
| 190 | + else |
|
| 191 | + { |
|
| 173 | 192 | throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $value); |
| 174 | 193 | } |
| 175 | 194 | |
| 176 | - if ($remaining > 0) { |
|
| 177 | - if ($position + $length > $strlen) { |
|
| 195 | + if ($remaining > 0) |
|
| 196 | + { |
|
| 197 | + if ($position + $length > $strlen) |
|
| 198 | + { |
|
| 178 | 199 | throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character); |
| 179 | 200 | } |
| 180 | - for ($position++; $remaining > 0; $position++) { |
|
| 201 | + for ($position++; $remaining > 0; $position++) |
|
| 202 | + { |
|
| 181 | 203 | $value = ord($input[$position]); |
| 182 | 204 | |
| 183 | 205 | // If it is invalid, count the sequence as invalid and reprocess the current byte: |
| 184 | - if (($value & 0xC0) !== 0x80) { |
|
| 206 | + if (($value & 0xC0) !== 0x80) |
|
| 207 | + { |
|
| 185 | 208 | throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character); |
| 186 | 209 | } |
| 187 | 210 | |
@@ -225,7 +248,8 @@ discard block |
||
| 225 | 248 | * @param string $input UTF-8 encoded string to encode |
| 226 | 249 | * @return string Punycode-encoded string |
| 227 | 250 | */ |
| 228 | - public static function punycode_encode($input) { |
|
| 251 | + public static function punycode_encode($input) |
|
| 252 | + { |
|
| 229 | 253 | $output = ''; |
| 230 | 254 | # let n = initial_n |
| 231 | 255 | $n = self::BOOTSTRAP_INITIAL_N; |
@@ -239,8 +263,10 @@ discard block |
||
| 239 | 263 | $codepoints = self::utf8_to_codepoints($input); |
| 240 | 264 | $extended = array(); |
| 241 | 265 | |
| 242 | - foreach ($codepoints as $char) { |
|
| 243 | - if ($char < 128) { |
|
| 266 | + foreach ($codepoints as $char) |
|
| 267 | + { |
|
| 268 | + if ($char < 128) |
|
| 269 | + { |
|
| 244 | 270 | // Character is valid ASCII |
| 245 | 271 | // TODO: this should also check if it's valid for a URL |
| 246 | 272 | $output .= chr($char); |
@@ -249,11 +275,13 @@ discard block |
||
| 249 | 275 | // Check if the character is non-ASCII, but below initial n |
| 250 | 276 | // This never occurs for Punycode, so ignore in coverage |
| 251 | 277 | // @codeCoverageIgnoreStart |
| 252 | - elseif ($char < $n) { |
|
| 278 | + elseif ($char < $n) |
|
| 279 | + { |
|
| 253 | 280 | throw new Requests_Exception('Invalid character', 'idna.character_outside_domain', $char); |
| 254 | 281 | } |
| 255 | 282 | // @codeCoverageIgnoreEnd |
| 256 | - else { |
|
| 283 | + else |
|
| 284 | + { |
|
| 257 | 285 | $extended[$char] = true; |
| 258 | 286 | } |
| 259 | 287 | } |
@@ -261,12 +289,14 @@ discard block |
||
| 261 | 289 | sort($extended); |
| 262 | 290 | $b = $h; |
| 263 | 291 | # [copy them] followed by a delimiter if b > 0 |
| 264 | - if (strlen($output) > 0) { |
|
| 292 | + if (strlen($output) > 0) |
|
| 293 | + { |
|
| 265 | 294 | $output .= '-'; |
| 266 | 295 | } |
| 267 | 296 | # {if the input contains a non-basic code point < n then fail} |
| 268 | 297 | # while h < length(input) do begin |
| 269 | - while ($h < count($codepoints)) { |
|
| 298 | + while ($h < count($codepoints)) |
|
| 299 | + { |
|
| 270 | 300 | # let m = the minimum code point >= n in the input |
| 271 | 301 | $m = array_shift($extended); |
| 272 | 302 | //printf('next code point to insert is %s' . PHP_EOL, dechex($m)); |
@@ -275,31 +305,39 @@ discard block |
||
| 275 | 305 | # let n = m |
| 276 | 306 | $n = $m; |
| 277 | 307 | # for each code point c in the input (in order) do begin |
| 278 | - for ($num = 0; $num < count($codepoints); $num++) { |
|
| 308 | + for ($num = 0; $num < count($codepoints); $num++) |
|
| 309 | + { |
|
| 279 | 310 | $c = $codepoints[$num]; |
| 280 | 311 | # if c < n then increment delta, fail on overflow |
| 281 | - if ($c < $n) { |
|
| 312 | + if ($c < $n) |
|
| 313 | + { |
|
| 282 | 314 | $delta++; |
| 283 | 315 | } |
| 284 | 316 | # if c == n then begin |
| 285 | - elseif ($c === $n) { |
|
| 317 | + elseif ($c === $n) |
|
| 318 | + { |
|
| 286 | 319 | # let q = delta |
| 287 | 320 | $q = $delta; |
| 288 | 321 | # for k = base to infinity in steps of base do begin |
| 289 | - for ($k = self::BOOTSTRAP_BASE; ; $k += self::BOOTSTRAP_BASE) { |
|
| 322 | + for ($k = self::BOOTSTRAP_BASE; ; $k += self::BOOTSTRAP_BASE) |
|
| 323 | + { |
|
| 290 | 324 | # let t = tmin if k <= bias {+ tmin}, or |
| 291 | 325 | # tmax if k >= bias + tmax, or k - bias otherwise |
| 292 | - if ($k <= ($bias + self::BOOTSTRAP_TMIN)) { |
|
| 326 | + if ($k <= ($bias + self::BOOTSTRAP_TMIN)) |
|
| 327 | + { |
|
| 293 | 328 | $t = self::BOOTSTRAP_TMIN; |
| 294 | 329 | } |
| 295 | - elseif ($k >= ($bias + self::BOOTSTRAP_TMAX)) { |
|
| 330 | + elseif ($k >= ($bias + self::BOOTSTRAP_TMAX)) |
|
| 331 | + { |
|
| 296 | 332 | $t = self::BOOTSTRAP_TMAX; |
| 297 | 333 | } |
| 298 | - else { |
|
| 334 | + else |
|
| 335 | + { |
|
| 299 | 336 | $t = $k - $bias; |
| 300 | 337 | } |
| 301 | 338 | # if q < t then break |
| 302 | - if ($q < $t) { |
|
| 339 | + if ($q < $t) |
|
| 340 | + { |
|
| 303 | 341 | break; |
| 304 | 342 | } |
| 305 | 343 | # output the code point for digit t + ((q - t) mod (base - t)) |
@@ -339,10 +377,12 @@ discard block |
||
| 339 | 377 | * @param int $digit Digit in the range 0-35 |
| 340 | 378 | * @return string Single character corresponding to digit |
| 341 | 379 | */ |
| 342 | - protected static function digit_to_char($digit) { |
|
| 380 | + protected static function digit_to_char($digit) |
|
| 381 | + { |
|
| 343 | 382 | // @codeCoverageIgnoreStart |
| 344 | 383 | // As far as I know, this never happens, but still good to be sure. |
| 345 | - if ($digit < 0 || $digit > 35) { |
|
| 384 | + if ($digit < 0 || $digit > 35) |
|
| 385 | + { |
|
| 346 | 386 | throw new Requests_Exception(sprintf('Invalid digit %d', $digit), 'idna.invalid_digit', $digit); |
| 347 | 387 | } |
| 348 | 388 | // @codeCoverageIgnoreEnd |
@@ -359,14 +399,17 @@ discard block |
||
| 359 | 399 | * @param bool $firsttime |
| 360 | 400 | * @return int New bias |
| 361 | 401 | */ |
| 362 | - protected static function adapt($delta, $numpoints, $firsttime) { |
|
| 402 | + protected static function adapt($delta, $numpoints, $firsttime) |
|
| 403 | + { |
|
| 363 | 404 | # function adapt(delta,numpoints,firsttime): |
| 364 | 405 | # if firsttime then let delta = delta div damp |
| 365 | - if ($firsttime) { |
|
| 406 | + if ($firsttime) |
|
| 407 | + { |
|
| 366 | 408 | $delta = floor($delta / self::BOOTSTRAP_DAMP); |
| 367 | 409 | } |
| 368 | 410 | # else let delta = delta div 2 |
| 369 | - else { |
|
| 411 | + else |
|
| 412 | + { |
|
| 370 | 413 | $delta = floor($delta / 2); |
| 371 | 414 | } |
| 372 | 415 | # let delta = delta + (delta div numpoints) |
@@ -375,7 +418,8 @@ discard block |
||
| 375 | 418 | $k = 0; |
| 376 | 419 | # while delta > ((base - tmin) * tmax) div 2 do begin |
| 377 | 420 | $max = floor(((self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN) * self::BOOTSTRAP_TMAX) / 2); |
| 378 | - while ($delta > $max) { |
|
| 421 | + while ($delta > $max) |
|
| 422 | + { |
|
| 379 | 423 | # let delta = delta div (base - tmin) |
| 380 | 424 | $delta = floor($delta / (self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN)); |
| 381 | 425 | # let k = k + base |
@@ -63,7 +63,8 @@ discard block |
||
| 63 | 63 | * @property string $fragment Fragment, formatted for a URI (after '#') |
| 64 | 64 | * @property string $ifragment Fragment part of the IRI (after '#') |
| 65 | 65 | */ |
| 66 | -class Requests_IRI { |
|
| 66 | +class Requests_IRI |
|
| 67 | +{ |
|
| 67 | 68 | /** |
| 68 | 69 | * Scheme |
| 69 | 70 | * |
@@ -142,7 +143,8 @@ discard block |
||
| 142 | 143 | * |
| 143 | 144 | * @return string |
| 144 | 145 | */ |
| 145 | - public function __toString() { |
|
| 146 | + public function __toString() |
|
| 147 | + { |
|
| 146 | 148 | return $this->get_iri(); |
| 147 | 149 | } |
| 148 | 150 | |
@@ -152,8 +154,10 @@ discard block |
||
| 152 | 154 | * @param string $name Property name |
| 153 | 155 | * @param mixed $value Property value |
| 154 | 156 | */ |
| 155 | - public function __set($name, $value) { |
|
| 156 | - if (method_exists($this, 'set_' . $name)) { |
|
| 157 | + public function __set($name, $value) |
|
| 158 | + { |
|
| 159 | + if (method_exists($this, 'set_' . $name)) |
|
| 160 | + { |
|
| 157 | 161 | call_user_func(array($this, 'set_' . $name), $value); |
| 158 | 162 | } |
| 159 | 163 | elseif ( |
@@ -174,7 +178,8 @@ discard block |
||
| 174 | 178 | * @param string $name Property name |
| 175 | 179 | * @return mixed |
| 176 | 180 | */ |
| 177 | - public function __get($name) { |
|
| 181 | + public function __get($name) |
|
| 182 | + { |
|
| 178 | 183 | // isset() returns false for null, we don't want to do that |
| 179 | 184 | // Also why we use array_key_exists below instead of isset() |
| 180 | 185 | $props = get_object_vars($this); |
@@ -188,28 +193,34 @@ discard block |
||
| 188 | 193 | $method = 'get_' . $name; |
| 189 | 194 | $return = $this->$method(); |
| 190 | 195 | } |
| 191 | - elseif (array_key_exists($name, $props)) { |
|
| 196 | + elseif (array_key_exists($name, $props)) |
|
| 197 | + { |
|
| 192 | 198 | $return = $this->$name; |
| 193 | 199 | } |
| 194 | 200 | // host -> ihost |
| 195 | - elseif (($prop = 'i' . $name) && array_key_exists($prop, $props)) { |
|
| 201 | + elseif (($prop = 'i' . $name) && array_key_exists($prop, $props)) |
|
| 202 | + { |
|
| 196 | 203 | $name = $prop; |
| 197 | 204 | $return = $this->$prop; |
| 198 | 205 | } |
| 199 | 206 | // ischeme -> scheme |
| 200 | - elseif (($prop = substr($name, 1)) && array_key_exists($prop, $props)) { |
|
| 207 | + elseif (($prop = substr($name, 1)) && array_key_exists($prop, $props)) |
|
| 208 | + { |
|
| 201 | 209 | $name = $prop; |
| 202 | 210 | $return = $this->$prop; |
| 203 | 211 | } |
| 204 | - else { |
|
| 212 | + else |
|
| 213 | + { |
|
| 205 | 214 | trigger_error('Undefined property: ' . get_class($this) . '::' . $name, E_USER_NOTICE); |
| 206 | 215 | $return = null; |
| 207 | 216 | } |
| 208 | 217 | |
| 209 | - if ($return === null && isset($this->normalization[$this->scheme][$name])) { |
|
| 218 | + if ($return === null && isset($this->normalization[$this->scheme][$name])) |
|
| 219 | + { |
|
| 210 | 220 | return $this->normalization[$this->scheme][$name]; |
| 211 | 221 | } |
| 212 | - else { |
|
| 222 | + else |
|
| 223 | + { |
|
| 213 | 224 | return $return; |
| 214 | 225 | } |
| 215 | 226 | } |
@@ -220,7 +231,8 @@ discard block |
||
| 220 | 231 | * @param string $name Property name |
| 221 | 232 | * @return bool |
| 222 | 233 | */ |
| 223 | - public function __isset($name) { |
|
| 234 | + public function __isset($name) |
|
| 235 | + { |
|
| 224 | 236 | return (method_exists($this, 'get_' . $name) || isset($this->$name)); |
| 225 | 237 | } |
| 226 | 238 | |
@@ -229,8 +241,10 @@ discard block |
||
| 229 | 241 | * |
| 230 | 242 | * @param string $name Property name |
| 231 | 243 | */ |
| 232 | - public function __unset($name) { |
|
| 233 | - if (method_exists($this, 'set_' . $name)) { |
|
| 244 | + public function __unset($name) |
|
| 245 | + { |
|
| 246 | + if (method_exists($this, 'set_' . $name)) |
|
| 247 | + { |
|
| 234 | 248 | call_user_func(array($this, 'set_' . $name), ''); |
| 235 | 249 | } |
| 236 | 250 | } |
@@ -240,7 +254,8 @@ discard block |
||
| 240 | 254 | * |
| 241 | 255 | * @param string|null $iri |
| 242 | 256 | */ |
| 243 | - public function __construct($iri = null) { |
|
| 257 | + public function __construct($iri = null) |
|
| 258 | + { |
|
| 244 | 259 | $this->set_iri($iri); |
| 245 | 260 | } |
| 246 | 261 | |
@@ -253,64 +268,82 @@ discard block |
||
| 253 | 268 | * @param IRI|string $relative Relative IRI |
| 254 | 269 | * @return IRI|false |
| 255 | 270 | */ |
| 256 | - public static function absolutize($base, $relative) { |
|
| 257 | - if (!($relative instanceof Requests_IRI)) { |
|
| 271 | + public static function absolutize($base, $relative) |
|
| 272 | + { |
|
| 273 | + if (!($relative instanceof Requests_IRI)) |
|
| 274 | + { |
|
| 258 | 275 | $relative = new Requests_IRI($relative); |
| 259 | 276 | } |
| 260 | - if (!$relative->is_valid()) { |
|
| 277 | + if (!$relative->is_valid()) |
|
| 278 | + { |
|
| 261 | 279 | return false; |
| 262 | 280 | } |
| 263 | - elseif ($relative->scheme !== null) { |
|
| 281 | + elseif ($relative->scheme !== null) |
|
| 282 | + { |
|
| 264 | 283 | return clone $relative; |
| 265 | 284 | } |
| 266 | 285 | |
| 267 | - if (!($base instanceof Requests_IRI)) { |
|
| 286 | + if (!($base instanceof Requests_IRI)) |
|
| 287 | + { |
|
| 268 | 288 | $base = new Requests_IRI($base); |
| 269 | 289 | } |
| 270 | - if ($base->scheme === null || !$base->is_valid()) { |
|
| 290 | + if ($base->scheme === null || !$base->is_valid()) |
|
| 291 | + { |
|
| 271 | 292 | return false; |
| 272 | 293 | } |
| 273 | 294 | |
| 274 | - if ($relative->get_iri() !== '') { |
|
| 275 | - if ($relative->iuserinfo !== null || $relative->ihost !== null || $relative->port !== null) { |
|
| 295 | + if ($relative->get_iri() !== '') |
|
| 296 | + { |
|
| 297 | + if ($relative->iuserinfo !== null || $relative->ihost !== null || $relative->port !== null) |
|
| 298 | + { |
|
| 276 | 299 | $target = clone $relative; |
| 277 | 300 | $target->scheme = $base->scheme; |
| 278 | 301 | } |
| 279 | - else { |
|
| 302 | + else |
|
| 303 | + { |
|
| 280 | 304 | $target = new Requests_IRI; |
| 281 | 305 | $target->scheme = $base->scheme; |
| 282 | 306 | $target->iuserinfo = $base->iuserinfo; |
| 283 | 307 | $target->ihost = $base->ihost; |
| 284 | 308 | $target->port = $base->port; |
| 285 | - if ($relative->ipath !== '') { |
|
| 286 | - if ($relative->ipath[0] === '/') { |
|
| 309 | + if ($relative->ipath !== '') |
|
| 310 | + { |
|
| 311 | + if ($relative->ipath[0] === '/') |
|
| 312 | + { |
|
| 287 | 313 | $target->ipath = $relative->ipath; |
| 288 | 314 | } |
| 289 | - elseif (($base->iuserinfo !== null || $base->ihost !== null || $base->port !== null) && $base->ipath === '') { |
|
| 315 | + elseif (($base->iuserinfo !== null || $base->ihost !== null || $base->port !== null) && $base->ipath === '') |
|
| 316 | + { |
|
| 290 | 317 | $target->ipath = '/' . $relative->ipath; |
| 291 | 318 | } |
| 292 | - elseif (($last_segment = strrpos($base->ipath, '/')) !== false) { |
|
| 319 | + elseif (($last_segment = strrpos($base->ipath, '/')) !== false) |
|
| 320 | + { |
|
| 293 | 321 | $target->ipath = substr($base->ipath, 0, $last_segment + 1) . $relative->ipath; |
| 294 | 322 | } |
| 295 | - else { |
|
| 323 | + else |
|
| 324 | + { |
|
| 296 | 325 | $target->ipath = $relative->ipath; |
| 297 | 326 | } |
| 298 | 327 | $target->ipath = $target->remove_dot_segments($target->ipath); |
| 299 | 328 | $target->iquery = $relative->iquery; |
| 300 | 329 | } |
| 301 | - else { |
|
| 330 | + else |
|
| 331 | + { |
|
| 302 | 332 | $target->ipath = $base->ipath; |
| 303 | - if ($relative->iquery !== null) { |
|
| 333 | + if ($relative->iquery !== null) |
|
| 334 | + { |
|
| 304 | 335 | $target->iquery = $relative->iquery; |
| 305 | 336 | } |
| 306 | - elseif ($base->iquery !== null) { |
|
| 337 | + elseif ($base->iquery !== null) |
|
| 338 | + { |
|
| 307 | 339 | $target->iquery = $base->iquery; |
| 308 | 340 | } |
| 309 | 341 | } |
| 310 | 342 | $target->ifragment = $relative->ifragment; |
| 311 | 343 | } |
| 312 | 344 | } |
| 313 | - else { |
|
| 345 | + else |
|
| 346 | + { |
|
| 314 | 347 | $target = clone $base; |
| 315 | 348 | $target->ifragment = null; |
| 316 | 349 | } |
@@ -324,26 +357,33 @@ discard block |
||
| 324 | 357 | * @param string $iri |
| 325 | 358 | * @return array |
| 326 | 359 | */ |
| 327 | - protected function parse_iri($iri) { |
|
| 360 | + protected function parse_iri($iri) |
|
| 361 | + { |
|
| 328 | 362 | $iri = trim($iri, "\x20\x09\x0A\x0C\x0D"); |
| 329 | 363 | $has_match = preg_match('/^((?P<scheme>[^:\/?#]+):)?(\/\/(?P<authority>[^\/?#]*))?(?P<path>[^?#]*)(\?(?P<query>[^#]*))?(#(?P<fragment>.*))?$/', $iri, $match); |
| 330 | - if (!$has_match) { |
|
| 364 | + if (!$has_match) |
|
| 365 | + { |
|
| 331 | 366 | throw new Requests_Exception('Cannot parse supplied IRI', 'iri.cannot_parse', $iri); |
| 332 | 367 | } |
| 333 | 368 | |
| 334 | - if ($match[1] === '') { |
|
| 369 | + if ($match[1] === '') |
|
| 370 | + { |
|
| 335 | 371 | $match['scheme'] = null; |
| 336 | 372 | } |
| 337 | - if (!isset($match[3]) || $match[3] === '') { |
|
| 373 | + if (!isset($match[3]) || $match[3] === '') |
|
| 374 | + { |
|
| 338 | 375 | $match['authority'] = null; |
| 339 | 376 | } |
| 340 | - if (!isset($match[5])) { |
|
| 377 | + if (!isset($match[5])) |
|
| 378 | + { |
|
| 341 | 379 | $match['path'] = ''; |
| 342 | 380 | } |
| 343 | - if (!isset($match[6]) || $match[6] === '') { |
|
| 381 | + if (!isset($match[6]) || $match[6] === '') |
|
| 382 | + { |
|
| 344 | 383 | $match['query'] = null; |
| 345 | 384 | } |
| 346 | - if (!isset($match[8]) || $match[8] === '') { |
|
| 385 | + if (!isset($match[8]) || $match[8] === '') |
|
| 386 | + { |
|
| 347 | 387 | $match['fragment'] = null; |
| 348 | 388 | } |
| 349 | 389 | return $match; |
@@ -355,52 +395,63 @@ discard block |
||
| 355 | 395 | * @param string $input |
| 356 | 396 | * @return string |
| 357 | 397 | */ |
| 358 | - protected function remove_dot_segments($input) { |
|
| 398 | + protected function remove_dot_segments($input) |
|
| 399 | + { |
|
| 359 | 400 | $output = ''; |
| 360 | - while (strpos($input, './') !== false || strpos($input, '/.') !== false || $input === '.' || $input === '..') { |
|
| 401 | + while (strpos($input, './') !== false || strpos($input, '/.') !== false || $input === '.' || $input === '..') |
|
| 402 | + { |
|
| 361 | 403 | // A: If the input buffer begins with a prefix of "../" or "./", |
| 362 | 404 | // then remove that prefix from the input buffer; otherwise, |
| 363 | - if (strpos($input, '../') === 0) { |
|
| 405 | + if (strpos($input, '../') === 0) |
|
| 406 | + { |
|
| 364 | 407 | $input = substr($input, 3); |
| 365 | 408 | } |
| 366 | - elseif (strpos($input, './') === 0) { |
|
| 409 | + elseif (strpos($input, './') === 0) |
|
| 410 | + { |
|
| 367 | 411 | $input = substr($input, 2); |
| 368 | 412 | } |
| 369 | 413 | // B: if the input buffer begins with a prefix of "/./" or "/.", |
| 370 | 414 | // where "." is a complete path segment, then replace that prefix |
| 371 | 415 | // with "/" in the input buffer; otherwise, |
| 372 | - elseif (strpos($input, '/./') === 0) { |
|
| 416 | + elseif (strpos($input, '/./') === 0) |
|
| 417 | + { |
|
| 373 | 418 | $input = substr($input, 2); |
| 374 | 419 | } |
| 375 | - elseif ($input === '/.') { |
|
| 420 | + elseif ($input === '/.') |
|
| 421 | + { |
|
| 376 | 422 | $input = '/'; |
| 377 | 423 | } |
| 378 | 424 | // C: if the input buffer begins with a prefix of "/../" or "/..", |
| 379 | 425 | // where ".." is a complete path segment, then replace that prefix |
| 380 | 426 | // with "/" in the input buffer and remove the last segment and its |
| 381 | 427 | // preceding "/" (if any) from the output buffer; otherwise, |
| 382 | - elseif (strpos($input, '/../') === 0) { |
|
| 428 | + elseif (strpos($input, '/../') === 0) |
|
| 429 | + { |
|
| 383 | 430 | $input = substr($input, 3); |
| 384 | 431 | $output = substr_replace($output, '', strrpos($output, '/')); |
| 385 | 432 | } |
| 386 | - elseif ($input === '/..') { |
|
| 433 | + elseif ($input === '/..') |
|
| 434 | + { |
|
| 387 | 435 | $input = '/'; |
| 388 | 436 | $output = substr_replace($output, '', strrpos($output, '/')); |
| 389 | 437 | } |
| 390 | 438 | // D: if the input buffer consists only of "." or "..", then remove |
| 391 | 439 | // that from the input buffer; otherwise, |
| 392 | - elseif ($input === '.' || $input === '..') { |
|
| 440 | + elseif ($input === '.' || $input === '..') |
|
| 441 | + { |
|
| 393 | 442 | $input = ''; |
| 394 | 443 | } |
| 395 | 444 | // E: move the first path segment in the input buffer to the end of |
| 396 | 445 | // the output buffer, including the initial "/" character (if any) |
| 397 | 446 | // and any subsequent characters up to, but not including, the next |
| 398 | 447 | // "/" character or the end of the input buffer |
| 399 | - elseif (($pos = strpos($input, '/', 1)) !== false) { |
|
| 448 | + elseif (($pos = strpos($input, '/', 1)) !== false) |
|
| 449 | + { |
|
| 400 | 450 | $output .= substr($input, 0, $pos); |
| 401 | 451 | $input = substr_replace($input, '', 0, $pos); |
| 402 | 452 | } |
| 403 | - else { |
|
| 453 | + else |
|
| 454 | + { |
|
| 404 | 455 | $output .= $input; |
| 405 | 456 | $input = ''; |
| 406 | 457 | } |
@@ -417,7 +468,8 @@ discard block |
||
| 417 | 468 | * @param bool $iprivate Allow iprivate |
| 418 | 469 | * @return string |
| 419 | 470 | */ |
| 420 | - protected function replace_invalid_with_pct_encoding($string, $extra_chars, $iprivate = false) { |
|
| 471 | + protected function replace_invalid_with_pct_encoding($string, $extra_chars, $iprivate = false) |
|
| 472 | + { |
|
| 421 | 473 | // Normalize as many pct-encoded sections as possible |
| 422 | 474 | $string = preg_replace_callback('/(?:%[A-Fa-f0-9]{2})+/', array(&$this, 'remove_iunreserved_percent_encoded'), $string); |
| 423 | 475 | |
@@ -431,7 +483,8 @@ discard block |
||
| 431 | 483 | // Now replace any bytes that aren't allowed with their pct-encoded versions |
| 432 | 484 | $position = 0; |
| 433 | 485 | $strlen = strlen($string); |
| 434 | - while (($position += strspn($string, $extra_chars, $position)) < $strlen) { |
|
| 486 | + while (($position += strspn($string, $extra_chars, $position)) < $strlen) |
|
| 487 | + { |
|
| 435 | 488 | $value = ord($string[$position]); |
| 436 | 489 | |
| 437 | 490 | // Start position |
@@ -442,48 +495,58 @@ discard block |
||
| 442 | 495 | |
| 443 | 496 | // No one byte sequences are valid due to the while. |
| 444 | 497 | // Two byte sequence: |
| 445 | - if (($value & 0xE0) === 0xC0) { |
|
| 498 | + if (($value & 0xE0) === 0xC0) |
|
| 499 | + { |
|
| 446 | 500 | $character = ($value & 0x1F) << 6; |
| 447 | 501 | $length = 2; |
| 448 | 502 | $remaining = 1; |
| 449 | 503 | } |
| 450 | 504 | // Three byte sequence: |
| 451 | - elseif (($value & 0xF0) === 0xE0) { |
|
| 505 | + elseif (($value & 0xF0) === 0xE0) |
|
| 506 | + { |
|
| 452 | 507 | $character = ($value & 0x0F) << 12; |
| 453 | 508 | $length = 3; |
| 454 | 509 | $remaining = 2; |
| 455 | 510 | } |
| 456 | 511 | // Four byte sequence: |
| 457 | - elseif (($value & 0xF8) === 0xF0) { |
|
| 512 | + elseif (($value & 0xF8) === 0xF0) |
|
| 513 | + { |
|
| 458 | 514 | $character = ($value & 0x07) << 18; |
| 459 | 515 | $length = 4; |
| 460 | 516 | $remaining = 3; |
| 461 | 517 | } |
| 462 | 518 | // Invalid byte: |
| 463 | - else { |
|
| 519 | + else |
|
| 520 | + { |
|
| 464 | 521 | $valid = false; |
| 465 | 522 | $length = 1; |
| 466 | 523 | $remaining = 0; |
| 467 | 524 | } |
| 468 | 525 | |
| 469 | - if ($remaining) { |
|
| 470 | - if ($position + $length <= $strlen) { |
|
| 471 | - for ($position++; $remaining; $position++) { |
|
| 526 | + if ($remaining) |
|
| 527 | + { |
|
| 528 | + if ($position + $length <= $strlen) |
|
| 529 | + { |
|
| 530 | + for ($position++; $remaining; $position++) |
|
| 531 | + { |
|
| 472 | 532 | $value = ord($string[$position]); |
| 473 | 533 | |
| 474 | 534 | // Check that the byte is valid, then add it to the character: |
| 475 | - if (($value & 0xC0) === 0x80) { |
|
| 535 | + if (($value & 0xC0) === 0x80) |
|
| 536 | + { |
|
| 476 | 537 | $character |= ($value & 0x3F) << (--$remaining * 6); |
| 477 | 538 | } |
| 478 | 539 | // If it is invalid, count the sequence as invalid and reprocess the current byte: |
| 479 | - else { |
|
| 540 | + else |
|
| 541 | + { |
|
| 480 | 542 | $valid = false; |
| 481 | 543 | $position--; |
| 482 | 544 | break; |
| 483 | 545 | } |
| 484 | 546 | } |
| 485 | 547 | } |
| 486 | - else { |
|
| 548 | + else |
|
| 549 | + { |
|
| 487 | 550 | $position = $strlen - 1; |
| 488 | 551 | $valid = false; |
| 489 | 552 | } |
@@ -515,11 +578,13 @@ discard block |
||
| 515 | 578 | ) |
| 516 | 579 | ) { |
| 517 | 580 | // If we were a character, pretend we weren't, but rather an error. |
| 518 | - if ($valid) { |
|
| 581 | + if ($valid) |
|
| 582 | + { |
|
| 519 | 583 | $position--; |
| 520 | 584 | } |
| 521 | 585 | |
| 522 | - for ($j = $start; $j <= $position; $j++) { |
|
| 586 | + for ($j = $start; $j <= $position; $j++) |
|
| 587 | + { |
|
| 523 | 588 | $string = substr_replace($string, sprintf('%%%02X', ord($string[$j])), $j, 1); |
| 524 | 589 | $j += 2; |
| 525 | 590 | $position += 2; |
@@ -540,7 +605,8 @@ discard block |
||
| 540 | 605 | * @param array $match PCRE match |
| 541 | 606 | * @return string Replacement |
| 542 | 607 | */ |
| 543 | - protected function remove_iunreserved_percent_encoded($match) { |
|
| 608 | + protected function remove_iunreserved_percent_encoded($match) |
|
| 609 | + { |
|
| 544 | 610 | // As we just have valid percent encoded sequences we can just explode |
| 545 | 611 | // and ignore the first member of the returned array (an empty string). |
| 546 | 612 | $bytes = explode('%', $match[0]); |
@@ -552,11 +618,13 @@ discard block |
||
| 552 | 618 | $remaining = 0; |
| 553 | 619 | |
| 554 | 620 | // Loop over each and every byte, and set $value to its value |
| 555 | - for ($i = 1, $len = count($bytes); $i < $len; $i++) { |
|
| 621 | + for ($i = 1, $len = count($bytes); $i < $len; $i++) |
|
| 622 | + { |
|
| 556 | 623 | $value = hexdec($bytes[$i]); |
| 557 | 624 | |
| 558 | 625 | // If we're the first byte of sequence: |
| 559 | - if (!$remaining) { |
|
| 626 | + if (!$remaining) |
|
| 627 | + { |
|
| 560 | 628 | // Start position |
| 561 | 629 | $start = $i; |
| 562 | 630 | |
@@ -564,43 +632,51 @@ discard block |
||
| 564 | 632 | $valid = true; |
| 565 | 633 | |
| 566 | 634 | // One byte sequence: |
| 567 | - if ($value <= 0x7F) { |
|
| 635 | + if ($value <= 0x7F) |
|
| 636 | + { |
|
| 568 | 637 | $character = $value; |
| 569 | 638 | $length = 1; |
| 570 | 639 | } |
| 571 | 640 | // Two byte sequence: |
| 572 | - elseif (($value & 0xE0) === 0xC0) { |
|
| 641 | + elseif (($value & 0xE0) === 0xC0) |
|
| 642 | + { |
|
| 573 | 643 | $character = ($value & 0x1F) << 6; |
| 574 | 644 | $length = 2; |
| 575 | 645 | $remaining = 1; |
| 576 | 646 | } |
| 577 | 647 | // Three byte sequence: |
| 578 | - elseif (($value & 0xF0) === 0xE0) { |
|
| 648 | + elseif (($value & 0xF0) === 0xE0) |
|
| 649 | + { |
|
| 579 | 650 | $character = ($value & 0x0F) << 12; |
| 580 | 651 | $length = 3; |
| 581 | 652 | $remaining = 2; |
| 582 | 653 | } |
| 583 | 654 | // Four byte sequence: |
| 584 | - elseif (($value & 0xF8) === 0xF0) { |
|
| 655 | + elseif (($value & 0xF8) === 0xF0) |
|
| 656 | + { |
|
| 585 | 657 | $character = ($value & 0x07) << 18; |
| 586 | 658 | $length = 4; |
| 587 | 659 | $remaining = 3; |
| 588 | 660 | } |
| 589 | 661 | // Invalid byte: |
| 590 | - else { |
|
| 662 | + else |
|
| 663 | + { |
|
| 591 | 664 | $valid = false; |
| 592 | 665 | $remaining = 0; |
| 593 | 666 | } |
| 594 | 667 | } |
| 595 | 668 | // Continuation byte: |
| 596 | - else { |
|
| 669 | + else |
|
| 670 | + { |
|
| 597 | 671 | // Check that the byte is valid, then add it to the character: |
| 598 | - if (($value & 0xC0) === 0x80) { |
|
| 672 | + if (($value & 0xC0) === 0x80) |
|
| 673 | + { |
|
| 599 | 674 | $remaining--; |
| 600 | 675 | $character |= ($value & 0x3F) << ($remaining * 6); |
| 601 | 676 | } |
| 602 | 677 | // If it is invalid, count the sequence as invalid and reprocess the current byte as the start of a sequence: |
| 603 | - else { |
|
| 678 | + else |
|
| 679 | + { |
|
| 604 | 680 | $valid = false; |
| 605 | 681 | $remaining = 0; |
| 606 | 682 | $i--; |
@@ -608,7 +684,8 @@ discard block |
||
| 608 | 684 | } |
| 609 | 685 | |
| 610 | 686 | // If we've reached the end of the current byte sequence, append it to Unicode::$data |
| 611 | - if (!$remaining) { |
|
| 687 | + if (!$remaining) |
|
| 688 | + { |
|
| 612 | 689 | // Percent encode anything invalid or not in iunreserved |
| 613 | 690 | if ( |
| 614 | 691 | // Invalid sequences |
@@ -631,12 +708,15 @@ discard block |
||
| 631 | 708 | || $character > 0x7E && $character < 0xA0 |
| 632 | 709 | || $character > 0xD7FF && $character < 0xF900 |
| 633 | 710 | ) { |
| 634 | - for ($j = $start; $j <= $i; $j++) { |
|
| 711 | + for ($j = $start; $j <= $i; $j++) |
|
| 712 | + { |
|
| 635 | 713 | $string .= '%' . strtoupper($bytes[$j]); |
| 636 | 714 | } |
| 637 | 715 | } |
| 638 | - else { |
|
| 639 | - for ($j = $start; $j <= $i; $j++) { |
|
| 716 | + else |
|
| 717 | + { |
|
| 718 | + for ($j = $start; $j <= $i; $j++) |
|
| 719 | + { |
|
| 640 | 720 | $string .= chr(hexdec($bytes[$j])); |
| 641 | 721 | } |
| 642 | 722 | } |
@@ -645,8 +725,10 @@ discard block |
||
| 645 | 725 | |
| 646 | 726 | // If we have any bytes left over they are invalid (i.e., we are |
| 647 | 727 | // mid-way through a multi-byte sequence) |
| 648 | - if ($remaining) { |
|
| 649 | - for ($j = $start; $j < $len; $j++) { |
|
| 728 | + if ($remaining) |
|
| 729 | + { |
|
| 730 | + for ($j = $start; $j < $len; $j++) |
|
| 731 | + { |
|
| 650 | 732 | $string .= '%' . strtoupper($bytes[$j]); |
| 651 | 733 | } |
| 652 | 734 | } |
@@ -654,26 +736,34 @@ discard block |
||
| 654 | 736 | return $string; |
| 655 | 737 | } |
| 656 | 738 | |
| 657 | - protected function scheme_normalization() { |
|
| 658 | - if (isset($this->normalization[$this->scheme]['iuserinfo']) && $this->iuserinfo === $this->normalization[$this->scheme]['iuserinfo']) { |
|
| 739 | + protected function scheme_normalization() |
|
| 740 | + { |
|
| 741 | + if (isset($this->normalization[$this->scheme]['iuserinfo']) && $this->iuserinfo === $this->normalization[$this->scheme]['iuserinfo']) |
|
| 742 | + { |
|
| 659 | 743 | $this->iuserinfo = null; |
| 660 | 744 | } |
| 661 | - if (isset($this->normalization[$this->scheme]['ihost']) && $this->ihost === $this->normalization[$this->scheme]['ihost']) { |
|
| 745 | + if (isset($this->normalization[$this->scheme]['ihost']) && $this->ihost === $this->normalization[$this->scheme]['ihost']) |
|
| 746 | + { |
|
| 662 | 747 | $this->ihost = null; |
| 663 | 748 | } |
| 664 | - if (isset($this->normalization[$this->scheme]['port']) && $this->port === $this->normalization[$this->scheme]['port']) { |
|
| 749 | + if (isset($this->normalization[$this->scheme]['port']) && $this->port === $this->normalization[$this->scheme]['port']) |
|
| 750 | + { |
|
| 665 | 751 | $this->port = null; |
| 666 | 752 | } |
| 667 | - if (isset($this->normalization[$this->scheme]['ipath']) && $this->ipath === $this->normalization[$this->scheme]['ipath']) { |
|
| 753 | + if (isset($this->normalization[$this->scheme]['ipath']) && $this->ipath === $this->normalization[$this->scheme]['ipath']) |
|
| 754 | + { |
|
| 668 | 755 | $this->ipath = ''; |
| 669 | 756 | } |
| 670 | - if (isset($this->ihost) && empty($this->ipath)) { |
|
| 757 | + if (isset($this->ihost) && empty($this->ipath)) |
|
| 758 | + { |
|
| 671 | 759 | $this->ipath = '/'; |
| 672 | 760 | } |
| 673 | - if (isset($this->normalization[$this->scheme]['iquery']) && $this->iquery === $this->normalization[$this->scheme]['iquery']) { |
|
| 761 | + if (isset($this->normalization[$this->scheme]['iquery']) && $this->iquery === $this->normalization[$this->scheme]['iquery']) |
|
| 762 | + { |
|
| 674 | 763 | $this->iquery = null; |
| 675 | 764 | } |
| 676 | - if (isset($this->normalization[$this->scheme]['ifragment']) && $this->ifragment === $this->normalization[$this->scheme]['ifragment']) { |
|
| 765 | + if (isset($this->normalization[$this->scheme]['ifragment']) && $this->ifragment === $this->normalization[$this->scheme]['ifragment']) |
|
| 766 | + { |
|
| 677 | 767 | $this->ifragment = null; |
| 678 | 768 | } |
| 679 | 769 | } |
@@ -684,7 +774,8 @@ discard block |
||
| 684 | 774 | * |
| 685 | 775 | * @return bool |
| 686 | 776 | */ |
| 687 | - public function is_valid() { |
|
| 777 | + public function is_valid() |
|
| 778 | + { |
|
| 688 | 779 | $isauthority = $this->iuserinfo !== null || $this->ihost !== null || $this->port !== null; |
| 689 | 780 | if ($this->ipath !== '' && |
| 690 | 781 | ( |
@@ -710,16 +801,20 @@ discard block |
||
| 710 | 801 | * @param string $iri |
| 711 | 802 | * @return bool |
| 712 | 803 | */ |
| 713 | - protected function set_iri($iri) { |
|
| 804 | + protected function set_iri($iri) |
|
| 805 | + { |
|
| 714 | 806 | static $cache; |
| 715 | - if (!$cache) { |
|
| 807 | + if (!$cache) |
|
| 808 | + { |
|
| 716 | 809 | $cache = array(); |
| 717 | 810 | } |
| 718 | 811 | |
| 719 | - if ($iri === null) { |
|
| 812 | + if ($iri === null) |
|
| 813 | + { |
|
| 720 | 814 | return true; |
| 721 | 815 | } |
| 722 | - if (isset($cache[$iri])) { |
|
| 816 | + if (isset($cache[$iri])) |
|
| 817 | + { |
|
| 723 | 818 | list($this->scheme, |
| 724 | 819 | $this->iuserinfo, |
| 725 | 820 | $this->ihost, |
@@ -757,15 +852,19 @@ discard block |
||
| 757 | 852 | * @param string $scheme |
| 758 | 853 | * @return bool |
| 759 | 854 | */ |
| 760 | - protected function set_scheme($scheme) { |
|
| 761 | - if ($scheme === null) { |
|
| 855 | + protected function set_scheme($scheme) |
|
| 856 | + { |
|
| 857 | + if ($scheme === null) |
|
| 858 | + { |
|
| 762 | 859 | $this->scheme = null; |
| 763 | 860 | } |
| 764 | - elseif (!preg_match('/^[A-Za-z][0-9A-Za-z+\-.]*$/', $scheme)) { |
|
| 861 | + elseif (!preg_match('/^[A-Za-z][0-9A-Za-z+\-.]*$/', $scheme)) |
|
| 862 | + { |
|
| 765 | 863 | $this->scheme = null; |
| 766 | 864 | return false; |
| 767 | 865 | } |
| 768 | - else { |
|
| 866 | + else |
|
| 867 | + { |
|
| 769 | 868 | $this->scheme = strtolower($scheme); |
| 770 | 869 | } |
| 771 | 870 | return true; |
@@ -778,19 +877,23 @@ discard block |
||
| 778 | 877 | * @param string $authority |
| 779 | 878 | * @return bool |
| 780 | 879 | */ |
| 781 | - protected function set_authority($authority) { |
|
| 880 | + protected function set_authority($authority) |
|
| 881 | + { |
|
| 782 | 882 | static $cache; |
| 783 | - if (!$cache) { |
|
| 883 | + if (!$cache) |
|
| 884 | + { |
|
| 784 | 885 | $cache = array(); |
| 785 | 886 | } |
| 786 | 887 | |
| 787 | - if ($authority === null) { |
|
| 888 | + if ($authority === null) |
|
| 889 | + { |
|
| 788 | 890 | $this->iuserinfo = null; |
| 789 | 891 | $this->ihost = null; |
| 790 | 892 | $this->port = null; |
| 791 | 893 | return true; |
| 792 | 894 | } |
| 793 | - if (isset($cache[$authority])) { |
|
| 895 | + if (isset($cache[$authority])) |
|
| 896 | + { |
|
| 794 | 897 | list($this->iuserinfo, |
| 795 | 898 | $this->ihost, |
| 796 | 899 | $this->port, |
@@ -800,21 +903,26 @@ discard block |
||
| 800 | 903 | } |
| 801 | 904 | |
| 802 | 905 | $remaining = $authority; |
| 803 | - if (($iuserinfo_end = strrpos($remaining, '@')) !== false) { |
|
| 906 | + if (($iuserinfo_end = strrpos($remaining, '@')) !== false) |
|
| 907 | + { |
|
| 804 | 908 | $iuserinfo = substr($remaining, 0, $iuserinfo_end); |
| 805 | 909 | $remaining = substr($remaining, $iuserinfo_end + 1); |
| 806 | 910 | } |
| 807 | - else { |
|
| 911 | + else |
|
| 912 | + { |
|
| 808 | 913 | $iuserinfo = null; |
| 809 | 914 | } |
| 810 | - if (($port_start = strpos($remaining, ':', strpos($remaining, ']'))) !== false) { |
|
| 915 | + if (($port_start = strpos($remaining, ':', strpos($remaining, ']'))) !== false) |
|
| 916 | + { |
|
| 811 | 917 | $port = substr($remaining, $port_start + 1); |
| 812 | - if ($port === false || $port === '') { |
|
| 918 | + if ($port === false || $port === '') |
|
| 919 | + { |
|
| 813 | 920 | $port = null; |
| 814 | 921 | } |
| 815 | 922 | $remaining = substr($remaining, 0, $port_start); |
| 816 | 923 | } |
| 817 | - else { |
|
| 924 | + else |
|
| 925 | + { |
|
| 818 | 926 | $port = null; |
| 819 | 927 | } |
| 820 | 928 | |
@@ -836,11 +944,14 @@ discard block |
||
| 836 | 944 | * @param string $iuserinfo |
| 837 | 945 | * @return bool |
| 838 | 946 | */ |
| 839 | - protected function set_userinfo($iuserinfo) { |
|
| 840 | - if ($iuserinfo === null) { |
|
| 947 | + protected function set_userinfo($iuserinfo) |
|
| 948 | + { |
|
| 949 | + if ($iuserinfo === null) |
|
| 950 | + { |
|
| 841 | 951 | $this->iuserinfo = null; |
| 842 | 952 | } |
| 843 | - else { |
|
| 953 | + else |
|
| 954 | + { |
|
| 844 | 955 | $this->iuserinfo = $this->replace_invalid_with_pct_encoding($iuserinfo, '!$&\'()*+,;=:'); |
| 845 | 956 | $this->scheme_normalization(); |
| 846 | 957 | } |
@@ -855,21 +966,27 @@ discard block |
||
| 855 | 966 | * @param string $ihost |
| 856 | 967 | * @return bool |
| 857 | 968 | */ |
| 858 | - protected function set_host($ihost) { |
|
| 859 | - if ($ihost === null) { |
|
| 969 | + protected function set_host($ihost) |
|
| 970 | + { |
|
| 971 | + if ($ihost === null) |
|
| 972 | + { |
|
| 860 | 973 | $this->ihost = null; |
| 861 | 974 | return true; |
| 862 | 975 | } |
| 863 | - if (substr($ihost, 0, 1) === '[' && substr($ihost, -1) === ']') { |
|
| 864 | - if (Requests_IPv6::check_ipv6(substr($ihost, 1, -1))) { |
|
| 976 | + if (substr($ihost, 0, 1) === '[' && substr($ihost, -1) === ']') |
|
| 977 | + { |
|
| 978 | + if (Requests_IPv6::check_ipv6(substr($ihost, 1, -1))) |
|
| 979 | + { |
|
| 865 | 980 | $this->ihost = '[' . Requests_IPv6::compress(substr($ihost, 1, -1)) . ']'; |
| 866 | 981 | } |
| 867 | - else { |
|
| 982 | + else |
|
| 983 | + { |
|
| 868 | 984 | $this->ihost = null; |
| 869 | 985 | return false; |
| 870 | 986 | } |
| 871 | 987 | } |
| 872 | - else { |
|
| 988 | + else |
|
| 989 | + { |
|
| 873 | 990 | $ihost = $this->replace_invalid_with_pct_encoding($ihost, '!$&\'()*+,;='); |
| 874 | 991 | |
| 875 | 992 | // Lowercase, but ignore pct-encoded sections (as they should |
@@ -877,11 +994,14 @@ discard block |
||
| 877 | 994 | // as that can add unescaped characters. |
| 878 | 995 | $position = 0; |
| 879 | 996 | $strlen = strlen($ihost); |
| 880 | - while (($position += strcspn($ihost, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ%', $position)) < $strlen) { |
|
| 881 | - if ($ihost[$position] === '%') { |
|
| 997 | + while (($position += strcspn($ihost, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ%', $position)) < $strlen) |
|
| 998 | + { |
|
| 999 | + if ($ihost[$position] === '%') |
|
| 1000 | + { |
|
| 882 | 1001 | $position += 3; |
| 883 | 1002 | } |
| 884 | - else { |
|
| 1003 | + else |
|
| 1004 | + { |
|
| 885 | 1005 | $ihost[$position] = strtolower($ihost[$position]); |
| 886 | 1006 | $position++; |
| 887 | 1007 | } |
@@ -902,13 +1022,16 @@ discard block |
||
| 902 | 1022 | * @param string $port |
| 903 | 1023 | * @return bool |
| 904 | 1024 | */ |
| 905 | - protected function set_port($port) { |
|
| 906 | - if ($port === null) { |
|
| 1025 | + protected function set_port($port) |
|
| 1026 | + { |
|
| 1027 | + if ($port === null) |
|
| 1028 | + { |
|
| 907 | 1029 | $this->port = null; |
| 908 | 1030 | return true; |
| 909 | 1031 | } |
| 910 | 1032 | |
| 911 | - if (strspn($port, '0123456789') === strlen($port)) { |
|
| 1033 | + if (strspn($port, '0123456789') === strlen($port)) |
|
| 1034 | + { |
|
| 912 | 1035 | $this->port = (int) $port; |
| 913 | 1036 | $this->scheme_normalization(); |
| 914 | 1037 | return true; |
@@ -924,18 +1047,22 @@ discard block |
||
| 924 | 1047 | * @param string $ipath |
| 925 | 1048 | * @return bool |
| 926 | 1049 | */ |
| 927 | - protected function set_path($ipath) { |
|
| 1050 | + protected function set_path($ipath) |
|
| 1051 | + { |
|
| 928 | 1052 | static $cache; |
| 929 | - if (!$cache) { |
|
| 1053 | + if (!$cache) |
|
| 1054 | + { |
|
| 930 | 1055 | $cache = array(); |
| 931 | 1056 | } |
| 932 | 1057 | |
| 933 | 1058 | $ipath = (string) $ipath; |
| 934 | 1059 | |
| 935 | - if (isset($cache[$ipath])) { |
|
| 1060 | + if (isset($cache[$ipath])) |
|
| 1061 | + { |
|
| 936 | 1062 | $this->ipath = $cache[$ipath][(int) ($this->scheme !== null)]; |
| 937 | 1063 | } |
| 938 | - else { |
|
| 1064 | + else |
|
| 1065 | + { |
|
| 939 | 1066 | $valid = $this->replace_invalid_with_pct_encoding($ipath, '!$&\'()*+,;=@:/'); |
| 940 | 1067 | $removed = $this->remove_dot_segments($valid); |
| 941 | 1068 | |
@@ -952,11 +1079,14 @@ discard block |
||
| 952 | 1079 | * @param string $iquery |
| 953 | 1080 | * @return bool |
| 954 | 1081 | */ |
| 955 | - protected function set_query($iquery) { |
|
| 956 | - if ($iquery === null) { |
|
| 1082 | + protected function set_query($iquery) |
|
| 1083 | + { |
|
| 1084 | + if ($iquery === null) |
|
| 1085 | + { |
|
| 957 | 1086 | $this->iquery = null; |
| 958 | 1087 | } |
| 959 | - else { |
|
| 1088 | + else |
|
| 1089 | + { |
|
| 960 | 1090 | $this->iquery = $this->replace_invalid_with_pct_encoding($iquery, '!$&\'()*+,;=:@/?', true); |
| 961 | 1091 | $this->scheme_normalization(); |
| 962 | 1092 | } |
@@ -969,11 +1099,14 @@ discard block |
||
| 969 | 1099 | * @param string $ifragment |
| 970 | 1100 | * @return bool |
| 971 | 1101 | */ |
| 972 | - protected function set_fragment($ifragment) { |
|
| 973 | - if ($ifragment === null) { |
|
| 1102 | + protected function set_fragment($ifragment) |
|
| 1103 | + { |
|
| 1104 | + if ($ifragment === null) |
|
| 1105 | + { |
|
| 974 | 1106 | $this->ifragment = null; |
| 975 | 1107 | } |
| 976 | - else { |
|
| 1108 | + else |
|
| 1109 | + { |
|
| 977 | 1110 | $this->ifragment = $this->replace_invalid_with_pct_encoding($ifragment, '!$&\'()*+,;=:@/?'); |
| 978 | 1111 | $this->scheme_normalization(); |
| 979 | 1112 | } |
@@ -986,19 +1119,23 @@ discard block |
||
| 986 | 1119 | * @param string|bool IRI to convert (or false from {@see get_iri}) |
| 987 | 1120 | * @return string|false URI if IRI is valid, false otherwise. |
| 988 | 1121 | */ |
| 989 | - protected function to_uri($string) { |
|
| 990 | - if (!is_string($string)) { |
|
| 1122 | + protected function to_uri($string) |
|
| 1123 | + { |
|
| 1124 | + if (!is_string($string)) |
|
| 1125 | + { |
|
| 991 | 1126 | return false; |
| 992 | 1127 | } |
| 993 | 1128 | |
| 994 | 1129 | static $non_ascii; |
| 995 | - if (!$non_ascii) { |
|
| 1130 | + if (!$non_ascii) |
|
| 1131 | + { |
|
| 996 | 1132 | $non_ascii = implode('', range("\x80", "\xFF")); |
| 997 | 1133 | } |
| 998 | 1134 | |
| 999 | 1135 | $position = 0; |
| 1000 | 1136 | $strlen = strlen($string); |
| 1001 | - while (($position += strcspn($string, $non_ascii, $position)) < $strlen) { |
|
| 1137 | + while (($position += strcspn($string, $non_ascii, $position)) < $strlen) |
|
| 1138 | + { |
|
| 1002 | 1139 | $string = substr_replace($string, sprintf('%%%02X', ord($string[$position])), $position, 1); |
| 1003 | 1140 | $position += 3; |
| 1004 | 1141 | $strlen += 2; |
@@ -1012,23 +1149,29 @@ discard block |
||
| 1012 | 1149 | * |
| 1013 | 1150 | * @return string |
| 1014 | 1151 | */ |
| 1015 | - protected function get_iri() { |
|
| 1016 | - if (!$this->is_valid()) { |
|
| 1152 | + protected function get_iri() |
|
| 1153 | + { |
|
| 1154 | + if (!$this->is_valid()) |
|
| 1155 | + { |
|
| 1017 | 1156 | return false; |
| 1018 | 1157 | } |
| 1019 | 1158 | |
| 1020 | 1159 | $iri = ''; |
| 1021 | - if ($this->scheme !== null) { |
|
| 1160 | + if ($this->scheme !== null) |
|
| 1161 | + { |
|
| 1022 | 1162 | $iri .= $this->scheme . ':'; |
| 1023 | 1163 | } |
| 1024 | - if (($iauthority = $this->get_iauthority()) !== null) { |
|
| 1164 | + if (($iauthority = $this->get_iauthority()) !== null) |
|
| 1165 | + { |
|
| 1025 | 1166 | $iri .= '//' . $iauthority; |
| 1026 | 1167 | } |
| 1027 | 1168 | $iri .= $this->ipath; |
| 1028 | - if ($this->iquery !== null) { |
|
| 1169 | + if ($this->iquery !== null) |
|
| 1170 | + { |
|
| 1029 | 1171 | $iri .= '?' . $this->iquery; |
| 1030 | 1172 | } |
| 1031 | - if ($this->ifragment !== null) { |
|
| 1173 | + if ($this->ifragment !== null) |
|
| 1174 | + { |
|
| 1032 | 1175 | $iri .= '#' . $this->ifragment; |
| 1033 | 1176 | } |
| 1034 | 1177 | |
@@ -1040,7 +1183,8 @@ discard block |
||
| 1040 | 1183 | * |
| 1041 | 1184 | * @return string |
| 1042 | 1185 | */ |
| 1043 | - protected function get_uri() { |
|
| 1186 | + protected function get_uri() |
|
| 1187 | + { |
|
| 1044 | 1188 | return $this->to_uri($this->get_iri()); |
| 1045 | 1189 | } |
| 1046 | 1190 | |
@@ -1049,19 +1193,24 @@ discard block |
||
| 1049 | 1193 | * |
| 1050 | 1194 | * @return string |
| 1051 | 1195 | */ |
| 1052 | - protected function get_iauthority() { |
|
| 1053 | - if ($this->iuserinfo === null && $this->ihost === null && $this->port === null) { |
|
| 1196 | + protected function get_iauthority() |
|
| 1197 | + { |
|
| 1198 | + if ($this->iuserinfo === null && $this->ihost === null && $this->port === null) |
|
| 1199 | + { |
|
| 1054 | 1200 | return null; |
| 1055 | 1201 | } |
| 1056 | 1202 | |
| 1057 | 1203 | $iauthority = ''; |
| 1058 | - if ($this->iuserinfo !== null) { |
|
| 1204 | + if ($this->iuserinfo !== null) |
|
| 1205 | + { |
|
| 1059 | 1206 | $iauthority .= $this->iuserinfo . '@'; |
| 1060 | 1207 | } |
| 1061 | - if ($this->ihost !== null) { |
|
| 1208 | + if ($this->ihost !== null) |
|
| 1209 | + { |
|
| 1062 | 1210 | $iauthority .= $this->ihost; |
| 1063 | 1211 | } |
| 1064 | - if ($this->port !== null) { |
|
| 1212 | + if ($this->port !== null) |
|
| 1213 | + { |
|
| 1065 | 1214 | $iauthority .= ':' . $this->port; |
| 1066 | 1215 | } |
| 1067 | 1216 | return $iauthority; |
@@ -1072,12 +1221,15 @@ discard block |
||
| 1072 | 1221 | * |
| 1073 | 1222 | * @return string |
| 1074 | 1223 | */ |
| 1075 | - protected function get_authority() { |
|
| 1224 | + protected function get_authority() |
|
| 1225 | + { |
|
| 1076 | 1226 | $iauthority = $this->get_iauthority(); |
| 1077 | - if (is_string($iauthority)) { |
|
| 1227 | + if (is_string($iauthority)) |
|
| 1228 | + { |
|
| 1078 | 1229 | return $this->to_uri($iauthority); |
| 1079 | 1230 | } |
| 1080 | - else { |
|
| 1231 | + else |
|
| 1232 | + { |
|
| 1081 | 1233 | return $iauthority; |
| 1082 | 1234 | } |
| 1083 | 1235 | } |
@@ -12,7 +12,8 @@ |
||
| 12 | 12 | * @package Requests |
| 13 | 13 | * @subpackage Utilities |
| 14 | 14 | */ |
| 15 | -interface Requests_Hooker { |
|
| 15 | +interface Requests_Hooker |
|
| 16 | +{ |
|
| 16 | 17 | /** |
| 17 | 18 | * Register a callback for a hook |
| 18 | 19 | * |
@@ -15,7 +15,8 @@ discard block |
||
| 15 | 15 | * @package Requests |
| 16 | 16 | * @subpackage Authentication |
| 17 | 17 | */ |
| 18 | -class Requests_Auth_Basic implements Requests_Auth { |
|
| 18 | +class Requests_Auth_Basic implements Requests_Auth |
|
| 19 | +{ |
|
| 19 | 20 | /** |
| 20 | 21 | * Username |
| 21 | 22 | * |
@@ -36,9 +37,12 @@ discard block |
||
| 36 | 37 | * @throws Requests_Exception On incorrect number of arguments (`authbasicbadargs`) |
| 37 | 38 | * @param array|null $args Array of user and password. Must have exactly two elements |
| 38 | 39 | */ |
| 39 | - public function __construct($args = null) { |
|
| 40 | - if (is_array($args)) { |
|
| 41 | - if (count($args) !== 2) { |
|
| 40 | + public function __construct($args = null) |
|
| 41 | + { |
|
| 42 | + if (is_array($args)) |
|
| 43 | + { |
|
| 44 | + if (count($args) !== 2) |
|
| 45 | + { |
|
| 42 | 46 | throw new Requests_Exception('Invalid number of arguments', 'authbasicbadargs'); |
| 43 | 47 | } |
| 44 | 48 | |
@@ -53,7 +57,8 @@ discard block |
||
| 53 | 57 | * @see fsockopen_header |
| 54 | 58 | * @param Requests_Hooks $hooks Hook system |
| 55 | 59 | */ |
| 56 | - public function register(Requests_Hooks &$hooks) { |
|
| 60 | + public function register(Requests_Hooks &$hooks) |
|
| 61 | + { |
|
| 57 | 62 | $hooks->register('curl.before_send', array(&$this, 'curl_before_send')); |
| 58 | 63 | $hooks->register('fsockopen.after_headers', array(&$this, 'fsockopen_header')); |
| 59 | 64 | } |
@@ -63,7 +68,8 @@ discard block |
||
| 63 | 68 | * |
| 64 | 69 | * @param resource $handle cURL resource |
| 65 | 70 | */ |
| 66 | - public function curl_before_send(&$handle) { |
|
| 71 | + public function curl_before_send(&$handle) |
|
| 72 | + { |
|
| 67 | 73 | curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
| 68 | 74 | curl_setopt($handle, CURLOPT_USERPWD, $this->getAuthString()); |
| 69 | 75 | } |
@@ -73,7 +79,8 @@ discard block |
||
| 73 | 79 | * |
| 74 | 80 | * @param string $out HTTP header string |
| 75 | 81 | */ |
| 76 | - public function fsockopen_header(&$out) { |
|
| 82 | + public function fsockopen_header(&$out) |
|
| 83 | + { |
|
| 77 | 84 | $out .= sprintf("Authorization: Basic %s\r\n", base64_encode($this->getAuthString())); |
| 78 | 85 | } |
| 79 | 86 | |
@@ -82,7 +89,8 @@ discard block |
||
| 82 | 89 | * |
| 83 | 90 | * @return string |
| 84 | 91 | */ |
| 85 | - public function getAuthString() { |
|
| 92 | + public function getAuthString() |
|
| 93 | + { |
|
| 86 | 94 | return $this->user . ':' . $this->pass; |
| 87 | 95 | } |
| 88 | 96 | } |
| 89 | 97 | \ No newline at end of file |