@@ -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 |
@@ -12,7 +12,8 @@ discard block |
||
| 12 | 12 | * @package Requests |
| 13 | 13 | * @subpackage Cookies |
| 14 | 14 | */ |
| 15 | -class Requests_Cookie { |
|
| 15 | +class Requests_Cookie |
|
| 16 | +{ |
|
| 16 | 17 | /** |
| 17 | 18 | * Cookie name. |
| 18 | 19 | * |
@@ -64,7 +65,8 @@ discard block |
||
| 64 | 65 | * @param string $value |
| 65 | 66 | * @param array|Requests_Utility_CaseInsensitiveDictionary $attributes Associative array of attribute data |
| 66 | 67 | */ |
| 67 | - public function __construct($name, $value, $attributes = array(), $flags = array(), $reference_time = null) { |
|
| 68 | + public function __construct($name, $value, $attributes = array(), $flags = array(), $reference_time = null) |
|
| 69 | + { |
|
| 68 | 70 | $this->name = $name; |
| 69 | 71 | $this->value = $value; |
| 70 | 72 | $this->attributes = $attributes; |
@@ -77,7 +79,8 @@ discard block |
||
| 77 | 79 | $this->flags = array_merge($default_flags, $flags); |
| 78 | 80 | |
| 79 | 81 | $this->reference_time = time(); |
| 80 | - if ($reference_time !== null) { |
|
| 82 | + if ($reference_time !== null) |
|
| 83 | + { |
|
| 81 | 84 | $this->reference_time = $reference_time; |
| 82 | 85 | } |
| 83 | 86 | |
@@ -92,17 +95,20 @@ discard block |
||
| 92 | 95 | * |
| 93 | 96 | * @return boolean True if expired, false if time is valid. |
| 94 | 97 | */ |
| 95 | - public function is_expired() { |
|
| 98 | + public function is_expired() |
|
| 99 | + { |
|
| 96 | 100 | // RFC6265, s. 4.1.2.2: |
| 97 | 101 | // If a cookie has both the Max-Age and the Expires attribute, the Max- |
| 98 | 102 | // Age attribute has precedence and controls the expiration date of the |
| 99 | 103 | // cookie. |
| 100 | - if (isset($this->attributes['max-age'])) { |
|
| 104 | + if (isset($this->attributes['max-age'])) |
|
| 105 | + { |
|
| 101 | 106 | $max_age = $this->attributes['max-age']; |
| 102 | 107 | return $max_age < $this->reference_time; |
| 103 | 108 | } |
| 104 | 109 | |
| 105 | - if (isset($this->attributes['expires'])) { |
|
| 110 | + if (isset($this->attributes['expires'])) |
|
| 111 | + { |
|
| 106 | 112 | $expires = $this->attributes['expires']; |
| 107 | 113 | return $expires < $this->reference_time; |
| 108 | 114 | } |
@@ -116,12 +122,15 @@ discard block |
||
| 116 | 122 | * @param Requests_IRI $uri URI to check |
| 117 | 123 | * @return boolean Whether the cookie is valid for the given URI |
| 118 | 124 | */ |
| 119 | - public function uri_matches(Requests_IRI $uri) { |
|
| 120 | - if (!$this->domain_matches($uri->host)) { |
|
| 125 | + public function uri_matches(Requests_IRI $uri) |
|
| 126 | + { |
|
| 127 | + if (!$this->domain_matches($uri->host)) |
|
| 128 | + { |
|
| 121 | 129 | return false; |
| 122 | 130 | } |
| 123 | 131 | |
| 124 | - if (!$this->path_matches($uri->path)) { |
|
| 132 | + if (!$this->path_matches($uri->path)) |
|
| 133 | + { |
|
| 125 | 134 | return false; |
| 126 | 135 | } |
| 127 | 136 | |
@@ -134,38 +143,45 @@ discard block |
||
| 134 | 143 | * @param string $string Domain to check |
| 135 | 144 | * @return boolean Whether the cookie is valid for the given domain |
| 136 | 145 | */ |
| 137 | - public function domain_matches($string) { |
|
| 138 | - if (!isset($this->attributes['domain'])) { |
|
| 146 | + public function domain_matches($string) |
|
| 147 | + { |
|
| 148 | + if (!isset($this->attributes['domain'])) |
|
| 149 | + { |
|
| 139 | 150 | // Cookies created manually; cookies created by Requests will set |
| 140 | 151 | // the domain to the requested domain |
| 141 | 152 | return true; |
| 142 | 153 | } |
| 143 | 154 | |
| 144 | 155 | $domain_string = $this->attributes['domain']; |
| 145 | - if ($domain_string === $string) { |
|
| 156 | + if ($domain_string === $string) |
|
| 157 | + { |
|
| 146 | 158 | // The domain string and the string are identical. |
| 147 | 159 | return true; |
| 148 | 160 | } |
| 149 | 161 | |
| 150 | 162 | // If the cookie is marked as host-only and we don't have an exact |
| 151 | 163 | // match, reject the cookie |
| 152 | - if ($this->flags['host-only'] === true) { |
|
| 164 | + if ($this->flags['host-only'] === true) |
|
| 165 | + { |
|
| 153 | 166 | return false; |
| 154 | 167 | } |
| 155 | 168 | |
| 156 | - if (strlen($string) <= strlen($domain_string)) { |
|
| 169 | + if (strlen($string) <= strlen($domain_string)) |
|
| 170 | + { |
|
| 157 | 171 | // For obvious reasons, the string cannot be a suffix if the domain |
| 158 | 172 | // is shorter than the domain string |
| 159 | 173 | return false; |
| 160 | 174 | } |
| 161 | 175 | |
| 162 | - if (substr($string, -1 * strlen($domain_string)) !== $domain_string) { |
|
| 176 | + if (substr($string, -1 * strlen($domain_string)) !== $domain_string) |
|
| 177 | + { |
|
| 163 | 178 | // The domain string should be a suffix of the string. |
| 164 | 179 | return false; |
| 165 | 180 | } |
| 166 | 181 | |
| 167 | 182 | $prefix = substr($string, 0, strlen($string) - strlen($domain_string)); |
| 168 | - if (substr($prefix, -1) !== '.') { |
|
| 183 | + if (substr($prefix, -1) !== '.') |
|
| 184 | + { |
|
| 169 | 185 | // The last character of the string that is not included in the |
| 170 | 186 | // domain string should be a %x2E (".") character. |
| 171 | 187 | return false; |
@@ -183,13 +199,16 @@ discard block |
||
| 183 | 199 | * @param string $request_path Path to check |
| 184 | 200 | * @return boolean Whether the cookie is valid for the given path |
| 185 | 201 | */ |
| 186 | - public function path_matches($request_path) { |
|
| 187 | - if (empty($request_path)) { |
|
| 202 | + public function path_matches($request_path) |
|
| 203 | + { |
|
| 204 | + if (empty($request_path)) |
|
| 205 | + { |
|
| 188 | 206 | // Normalize empty path to root |
| 189 | 207 | $request_path = '/'; |
| 190 | 208 | } |
| 191 | 209 | |
| 192 | - if (!isset($this->attributes['path'])) { |
|
| 210 | + if (!isset($this->attributes['path'])) |
|
| 211 | + { |
|
| 193 | 212 | // Cookies created manually; cookies created by Requests will set |
| 194 | 213 | // the path to the requested path |
| 195 | 214 | return true; |
@@ -197,19 +216,23 @@ discard block |
||
| 197 | 216 | |
| 198 | 217 | $cookie_path = $this->attributes['path']; |
| 199 | 218 | |
| 200 | - if ($cookie_path === $request_path) { |
|
| 219 | + if ($cookie_path === $request_path) |
|
| 220 | + { |
|
| 201 | 221 | // The cookie-path and the request-path are identical. |
| 202 | 222 | return true; |
| 203 | 223 | } |
| 204 | 224 | |
| 205 | - if (strlen($request_path) > strlen($cookie_path) && substr($request_path, 0, strlen($cookie_path)) === $cookie_path) { |
|
| 206 | - if (substr($cookie_path, -1) === '/') { |
|
| 225 | + if (strlen($request_path) > strlen($cookie_path) && substr($request_path, 0, strlen($cookie_path)) === $cookie_path) |
|
| 226 | + { |
|
| 227 | + if (substr($cookie_path, -1) === '/') |
|
| 228 | + { |
|
| 207 | 229 | // The cookie-path is a prefix of the request-path, and the last |
| 208 | 230 | // character of the cookie-path is %x2F ("/"). |
| 209 | 231 | return true; |
| 210 | 232 | } |
| 211 | 233 | |
| 212 | - if (substr($request_path, strlen($cookie_path), 1) === '/') { |
|
| 234 | + if (substr($request_path, strlen($cookie_path), 1) === '/') |
|
| 235 | + { |
|
| 213 | 236 | // The cookie-path is a prefix of the request-path, and the |
| 214 | 237 | // first character of the request-path that is not included in |
| 215 | 238 | // the cookie-path is a %x2F ("/") character. |
@@ -225,16 +248,20 @@ discard block |
||
| 225 | 248 | * |
| 226 | 249 | * @return boolean Whether the cookie was successfully normalized |
| 227 | 250 | */ |
| 228 | - public function normalize() { |
|
| 229 | - foreach ($this->attributes as $key => $value) { |
|
| 251 | + public function normalize() |
|
| 252 | + { |
|
| 253 | + foreach ($this->attributes as $key => $value) |
|
| 254 | + { |
|
| 230 | 255 | $orig_value = $value; |
| 231 | 256 | $value = $this->normalize_attribute($key, $value); |
| 232 | - if ($value === null) { |
|
| 257 | + if ($value === null) |
|
| 258 | + { |
|
| 233 | 259 | unset($this->attributes[$key]); |
| 234 | 260 | continue; |
| 235 | 261 | } |
| 236 | 262 | |
| 237 | - if ($value !== $orig_value) { |
|
| 263 | + if ($value !== $orig_value) |
|
| 264 | + { |
|
| 238 | 265 | $this->attributes[$key] = $value; |
| 239 | 266 | } |
| 240 | 267 | } |
@@ -251,16 +278,20 @@ discard block |
||
| 251 | 278 | * @param string|boolean $value Attribute value (string value, or true if empty/flag) |
| 252 | 279 | * @return mixed Value if available, or null if the attribute value is invalid (and should be skipped) |
| 253 | 280 | */ |
| 254 | - protected function normalize_attribute($name, $value) { |
|
| 255 | - switch (strtolower($name)) { |
|
| 281 | + protected function normalize_attribute($name, $value) |
|
| 282 | + { |
|
| 283 | + switch (strtolower($name)) |
|
| 284 | + { |
|
| 256 | 285 | case 'expires': |
| 257 | 286 | // Expiration parsing, as per RFC 6265 section 5.2.1 |
| 258 | - if (is_int($value)) { |
|
| 287 | + if (is_int($value)) |
|
| 288 | + { |
|
| 259 | 289 | return $value; |
| 260 | 290 | } |
| 261 | 291 | |
| 262 | 292 | $expiry_time = strtotime($value); |
| 263 | - if ($expiry_time === false) { |
|
| 293 | + if ($expiry_time === false) |
|
| 294 | + { |
|
| 264 | 295 | return null; |
| 265 | 296 | } |
| 266 | 297 | |
@@ -268,20 +299,24 @@ discard block |
||
| 268 | 299 | |
| 269 | 300 | case 'max-age': |
| 270 | 301 | // Expiration parsing, as per RFC 6265 section 5.2.2 |
| 271 | - if (is_int($value)) { |
|
| 302 | + if (is_int($value)) |
|
| 303 | + { |
|
| 272 | 304 | return $value; |
| 273 | 305 | } |
| 274 | 306 | |
| 275 | 307 | // Check that we have a valid age |
| 276 | - if (!preg_match('/^-?\d+$/', $value)) { |
|
| 308 | + if (!preg_match('/^-?\d+$/', $value)) |
|
| 309 | + { |
|
| 277 | 310 | return null; |
| 278 | 311 | } |
| 279 | 312 | |
| 280 | 313 | $delta_seconds = (int) $value; |
| 281 | - if ($delta_seconds <= 0) { |
|
| 314 | + if ($delta_seconds <= 0) |
|
| 315 | + { |
|
| 282 | 316 | $expiry_time = 0; |
| 283 | 317 | } |
| 284 | - else { |
|
| 318 | + else |
|
| 319 | + { |
|
| 285 | 320 | $expiry_time = $this->reference_time + $delta_seconds; |
| 286 | 321 | } |
| 287 | 322 | |
@@ -289,7 +324,8 @@ discard block |
||
| 289 | 324 | |
| 290 | 325 | case 'domain': |
| 291 | 326 | // Domain normalization, as per RFC 6265 section 5.2.3 |
| 292 | - if ($value[0] === '.') { |
|
| 327 | + if ($value[0] === '.') |
|
| 328 | + { |
|
| 293 | 329 | $value = substr($value, 1); |
| 294 | 330 | } |
| 295 | 331 | |
@@ -307,7 +343,8 @@ discard block |
||
| 307 | 343 | * |
| 308 | 344 | * @return string Cookie formatted for Cookie header |
| 309 | 345 | */ |
| 310 | - public function format_for_header() { |
|
| 346 | + public function format_for_header() |
|
| 347 | + { |
|
| 311 | 348 | return sprintf('%s=%s', $this->name, $this->value); |
| 312 | 349 | } |
| 313 | 350 | |
@@ -318,7 +355,8 @@ discard block |
||
| 318 | 355 | * @deprecated Use {@see Requests_Cookie::format_for_header} |
| 319 | 356 | * @return string |
| 320 | 357 | */ |
| 321 | - public function formatForHeader() { |
|
| 358 | + public function formatForHeader() |
|
| 359 | + { |
|
| 322 | 360 | return $this->format_for_header(); |
| 323 | 361 | } |
| 324 | 362 | |
@@ -330,16 +368,21 @@ discard block |
||
| 330 | 368 | * |
| 331 | 369 | * @return string Cookie formatted for Set-Cookie header |
| 332 | 370 | */ |
| 333 | - public function format_for_set_cookie() { |
|
| 371 | + public function format_for_set_cookie() |
|
| 372 | + { |
|
| 334 | 373 | $header_value = $this->format_for_header(); |
| 335 | - if (!empty($this->attributes)) { |
|
| 374 | + if (!empty($this->attributes)) |
|
| 375 | + { |
|
| 336 | 376 | $parts = array(); |
| 337 | - foreach ($this->attributes as $key => $value) { |
|
| 377 | + foreach ($this->attributes as $key => $value) |
|
| 378 | + { |
|
| 338 | 379 | // Ignore non-associative attributes |
| 339 | - if (is_numeric($key)) { |
|
| 380 | + if (is_numeric($key)) |
|
| 381 | + { |
|
| 340 | 382 | $parts[] = $value; |
| 341 | 383 | } |
| 342 | - else { |
|
| 384 | + else |
|
| 385 | + { |
|
| 343 | 386 | $parts[] = sprintf('%s=%s', $key, $value); |
| 344 | 387 | } |
| 345 | 388 | } |
@@ -356,7 +399,8 @@ discard block |
||
| 356 | 399 | * @deprecated Use {@see Requests_Cookie::format_for_set_cookie} |
| 357 | 400 | * @return string |
| 358 | 401 | */ |
| 359 | - public function formatForSetCookie() { |
|
| 402 | + public function formatForSetCookie() |
|
| 403 | + { |
|
| 360 | 404 | return $this->format_for_set_cookie(); |
| 361 | 405 | } |
| 362 | 406 | |
@@ -365,7 +409,8 @@ discard block |
||
| 365 | 409 | * |
| 366 | 410 | * Attributes and other data can be accessed via methods. |
| 367 | 411 | */ |
| 368 | - public function __toString() { |
|
| 412 | + public function __toString() |
|
| 413 | + { |
|
| 369 | 414 | return $this->value; |
| 370 | 415 | } |
| 371 | 416 | |
@@ -379,14 +424,17 @@ discard block |
||
| 379 | 424 | * @param string Cookie header value (from a Set-Cookie header) |
| 380 | 425 | * @return Requests_Cookie Parsed cookie object |
| 381 | 426 | */ |
| 382 | - public static function parse($string, $name = '', $reference_time = null) { |
|
| 427 | + public static function parse($string, $name = '', $reference_time = null) |
|
| 428 | + { |
|
| 383 | 429 | $parts = explode(';', $string); |
| 384 | 430 | $kvparts = array_shift($parts); |
| 385 | 431 | |
| 386 | - if (!empty($name)) { |
|
| 432 | + if (!empty($name)) |
|
| 433 | + { |
|
| 387 | 434 | $value = $string; |
| 388 | 435 | } |
| 389 | - elseif (strpos($kvparts, '=') === false) { |
|
| 436 | + elseif (strpos($kvparts, '=') === false) |
|
| 437 | + { |
|
| 390 | 438 | // Some sites might only have a value without the equals separator. |
| 391 | 439 | // Deviate from RFC 6265 and pretend it was actually a blank name |
| 392 | 440 | // (`=foo`) |
@@ -395,7 +443,8 @@ discard block |
||
| 395 | 443 | $name = ''; |
| 396 | 444 | $value = $kvparts; |
| 397 | 445 | } |
| 398 | - else { |
|
| 446 | + else |
|
| 447 | + { |
|
| 399 | 448 | list($name, $value) = explode('=', $kvparts, 2); |
| 400 | 449 | } |
| 401 | 450 | $name = trim($name); |
@@ -404,13 +453,17 @@ discard block |
||
| 404 | 453 | // Attribute key are handled case-insensitively |
| 405 | 454 | $attributes = new Requests_Utility_CaseInsensitiveDictionary(); |
| 406 | 455 | |
| 407 | - if (!empty($parts)) { |
|
| 408 | - foreach ($parts as $part) { |
|
| 409 | - if (strpos($part, '=') === false) { |
|
| 456 | + if (!empty($parts)) |
|
| 457 | + { |
|
| 458 | + foreach ($parts as $part) |
|
| 459 | + { |
|
| 460 | + if (strpos($part, '=') === false) |
|
| 461 | + { |
|
| 410 | 462 | $part_key = $part; |
| 411 | 463 | $part_value = true; |
| 412 | 464 | } |
| 413 | - else { |
|
| 465 | + else |
|
| 466 | + { |
|
| 414 | 467 | list($part_key, $part_value) = explode('=', $part, 2); |
| 415 | 468 | $part_value = trim($part_value); |
| 416 | 469 | } |
@@ -431,43 +484,52 @@ discard block |
||
| 431 | 484 | * @param int|null $time Reference time for expiration calculation |
| 432 | 485 | * @return array |
| 433 | 486 | */ |
| 434 | - public static function parse_from_headers(Requests_Response_Headers $headers, Requests_IRI $origin = null, $time = null) { |
|
| 487 | + public static function parse_from_headers(Requests_Response_Headers $headers, Requests_IRI $origin = null, $time = null) |
|
| 488 | + { |
|
| 435 | 489 | $cookie_headers = $headers->getValues('Set-Cookie'); |
| 436 | - if (empty($cookie_headers)) { |
|
| 490 | + if (empty($cookie_headers)) |
|
| 491 | + { |
|
| 437 | 492 | return array(); |
| 438 | 493 | } |
| 439 | 494 | |
| 440 | 495 | $cookies = array(); |
| 441 | - foreach ($cookie_headers as $header) { |
|
| 496 | + foreach ($cookie_headers as $header) |
|
| 497 | + { |
|
| 442 | 498 | $parsed = self::parse($header, '', $time); |
| 443 | 499 | |
| 444 | 500 | // Default domain/path attributes |
| 445 | - if (empty($parsed->attributes['domain']) && !empty($origin)) { |
|
| 501 | + if (empty($parsed->attributes['domain']) && !empty($origin)) |
|
| 502 | + { |
|
| 446 | 503 | $parsed->attributes['domain'] = $origin->host; |
| 447 | 504 | $parsed->flags['host-only'] = true; |
| 448 | 505 | } |
| 449 | - else { |
|
| 506 | + else |
|
| 507 | + { |
|
| 450 | 508 | $parsed->flags['host-only'] = false; |
| 451 | 509 | } |
| 452 | 510 | |
| 453 | 511 | $path_is_valid = (!empty($parsed->attributes['path']) && $parsed->attributes['path'][0] === '/'); |
| 454 | - if (!$path_is_valid && !empty($origin)) { |
|
| 512 | + if (!$path_is_valid && !empty($origin)) |
|
| 513 | + { |
|
| 455 | 514 | $path = $origin->path; |
| 456 | 515 | |
| 457 | 516 | // Default path normalization as per RFC 6265 section 5.1.4 |
| 458 | - if (substr($path, 0, 1) !== '/') { |
|
| 517 | + if (substr($path, 0, 1) !== '/') |
|
| 518 | + { |
|
| 459 | 519 | // If the uri-path is empty or if the first character of |
| 460 | 520 | // the uri-path is not a %x2F ("/") character, output |
| 461 | 521 | // %x2F ("/") and skip the remaining steps. |
| 462 | 522 | $path = '/'; |
| 463 | 523 | } |
| 464 | - elseif (substr_count($path, '/') === 1) { |
|
| 524 | + elseif (substr_count($path, '/') === 1) |
|
| 525 | + { |
|
| 465 | 526 | // If the uri-path contains no more than one %x2F ("/") |
| 466 | 527 | // character, output %x2F ("/") and skip the remaining |
| 467 | 528 | // step. |
| 468 | 529 | $path = '/'; |
| 469 | 530 | } |
| 470 | - else { |
|
| 531 | + else |
|
| 532 | + { |
|
| 471 | 533 | // Output the characters of the uri-path from the first |
| 472 | 534 | // character up to, but not including, the right-most |
| 473 | 535 | // %x2F ("/"). |
@@ -477,7 +539,8 @@ discard block |
||
| 477 | 539 | } |
| 478 | 540 | |
| 479 | 541 | // Reject invalid cookie domains |
| 480 | - if (!empty($origin) && !$parsed->domain_matches($origin->host)) { |
|
| 542 | + if (!empty($origin) && !$parsed->domain_matches($origin->host)) |
|
| 543 | + { |
|
| 481 | 544 | continue; |
| 482 | 545 | } |
| 483 | 546 | |
@@ -494,7 +557,8 @@ discard block |
||
| 494 | 557 | * @deprecated Use {@see Requests_Cookie::parse_from_headers} |
| 495 | 558 | * @return string |
| 496 | 559 | */ |
| 497 | - public static function parseFromHeaders(Requests_Response_Headers $headers) { |
|
| 560 | + public static function parseFromHeaders(Requests_Response_Headers $headers) |
|
| 561 | + { |
|
| 498 | 562 | return self::parse_from_headers($headers); |
| 499 | 563 | } |
| 500 | 564 | } |
@@ -18,7 +18,8 @@ discard block |
||
| 18 | 18 | * |
| 19 | 19 | * @package Requests |
| 20 | 20 | */ |
| 21 | -class Requests { |
|
| 21 | +class Requests |
|
| 22 | +{ |
|
| 22 | 23 | /** |
| 23 | 24 | * POST method |
| 24 | 25 | * |
@@ -121,7 +122,9 @@ discard block |
||
| 121 | 122 | * |
| 122 | 123 | * @codeCoverageIgnore |
| 123 | 124 | */ |
| 124 | - private function __construct() {} |
|
| 125 | + private function __construct() |
|
| 126 | + { |
|
| 127 | +} |
|
| 125 | 128 | |
| 126 | 129 | /** |
| 127 | 130 | * Autoloader for Requests |
@@ -135,14 +138,17 @@ discard block |
||
| 135 | 138 | * |
| 136 | 139 | * @param string $class Class name to load |
| 137 | 140 | */ |
| 138 | - public static function autoloader($class) { |
|
| 141 | + public static function autoloader($class) |
|
| 142 | + { |
|
| 139 | 143 | // Check that the class starts with "Requests" |
| 140 | - if (strpos($class, 'Requests') !== 0) { |
|
| 144 | + if (strpos($class, 'Requests') !== 0) |
|
| 145 | + { |
|
| 141 | 146 | return; |
| 142 | 147 | } |
| 143 | 148 | |
| 144 | 149 | $file = str_replace('_', '/', $class); |
| 145 | - if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) { |
|
| 150 | + if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) |
|
| 151 | + { |
|
| 146 | 152 | require_once(dirname(__FILE__) . '/' . $file . '.php'); |
| 147 | 153 | } |
| 148 | 154 | } |
@@ -152,7 +158,8 @@ discard block |
||
| 152 | 158 | * |
| 153 | 159 | * @codeCoverageIgnore |
| 154 | 160 | */ |
| 155 | - public static function register_autoloader() { |
|
| 161 | + public static function register_autoloader() |
|
| 162 | + { |
|
| 156 | 163 | spl_autoload_register(array('Requests', 'autoloader')); |
| 157 | 164 | } |
| 158 | 165 | |
@@ -161,8 +168,10 @@ discard block |
||
| 161 | 168 | * |
| 162 | 169 | * @param string $transport Transport class to add, must support the Requests_Transport interface |
| 163 | 170 | */ |
| 164 | - public static function add_transport($transport) { |
|
| 165 | - if (empty(self::$transports)) { |
|
| 171 | + public static function add_transport($transport) |
|
| 172 | + { |
|
| 173 | + if (empty(self::$transports)) |
|
| 174 | + { |
|
| 166 | 175 | self::$transports = array( |
| 167 | 176 | 'Requests_Transport_cURL', |
| 168 | 177 | 'Requests_Transport_fsockopen', |
@@ -178,7 +187,8 @@ discard block |
||
| 178 | 187 | * @throws Requests_Exception If no valid transport is found (`notransport`) |
| 179 | 188 | * @return Requests_Transport |
| 180 | 189 | */ |
| 181 | - protected static function get_transport($capabilities = array()) { |
|
| 190 | + protected static function get_transport($capabilities = array()) |
|
| 191 | + { |
|
| 182 | 192 | // Caching code, don't bother testing coverage |
| 183 | 193 | // @codeCoverageIgnoreStart |
| 184 | 194 | // array of capabilities as a string to be used as an array key |
@@ -186,12 +196,14 @@ discard block |
||
| 186 | 196 | $cap_string = serialize($capabilities); |
| 187 | 197 | |
| 188 | 198 | // Don't search for a transport if it's already been done for these $capabilities |
| 189 | - if (isset(self::$transport[$cap_string]) && self::$transport[$cap_string] !== null) { |
|
| 199 | + if (isset(self::$transport[$cap_string]) && self::$transport[$cap_string] !== null) |
|
| 200 | + { |
|
| 190 | 201 | return new self::$transport[$cap_string](); |
| 191 | 202 | } |
| 192 | 203 | // @codeCoverageIgnoreEnd |
| 193 | 204 | |
| 194 | - if (empty(self::$transports)) { |
|
| 205 | + if (empty(self::$transports)) |
|
| 206 | + { |
|
| 195 | 207 | self::$transports = array( |
| 196 | 208 | 'Requests_Transport_cURL', |
| 197 | 209 | 'Requests_Transport_fsockopen', |
@@ -199,18 +211,22 @@ discard block |
||
| 199 | 211 | } |
| 200 | 212 | |
| 201 | 213 | // Find us a working transport |
| 202 | - foreach (self::$transports as $class) { |
|
| 203 | - if (!class_exists($class)) { |
|
| 214 | + foreach (self::$transports as $class) |
|
| 215 | + { |
|
| 216 | + if (!class_exists($class)) |
|
| 217 | + { |
|
| 204 | 218 | continue; |
| 205 | 219 | } |
| 206 | 220 | |
| 207 | 221 | $result = call_user_func(array($class, 'test'), $capabilities); |
| 208 | - if ($result) { |
|
| 222 | + if ($result) |
|
| 223 | + { |
|
| 209 | 224 | self::$transport[$cap_string] = $class; |
| 210 | 225 | break; |
| 211 | 226 | } |
| 212 | 227 | } |
| 213 | - if (self::$transport[$cap_string] === null) { |
|
| 228 | + if (self::$transport[$cap_string] === null) |
|
| 229 | + { |
|
| 214 | 230 | throw new Requests_Exception('No working transports found', 'notransport', self::$transports); |
| 215 | 231 | } |
| 216 | 232 | |
@@ -227,28 +243,32 @@ discard block |
||
| 227 | 243 | /** |
| 228 | 244 | * Send a GET request |
| 229 | 245 | */ |
| 230 | - public static function get($url, $headers = array(), $options = array()) { |
|
| 246 | + public static function get($url, $headers = array(), $options = array()) |
|
| 247 | + { |
|
| 231 | 248 | return self::request($url, $headers, null, self::GET, $options); |
| 232 | 249 | } |
| 233 | 250 | |
| 234 | 251 | /** |
| 235 | 252 | * Send a HEAD request |
| 236 | 253 | */ |
| 237 | - public static function head($url, $headers = array(), $options = array()) { |
|
| 254 | + public static function head($url, $headers = array(), $options = array()) |
|
| 255 | + { |
|
| 238 | 256 | return self::request($url, $headers, null, self::HEAD, $options); |
| 239 | 257 | } |
| 240 | 258 | |
| 241 | 259 | /** |
| 242 | 260 | * Send a DELETE request |
| 243 | 261 | */ |
| 244 | - public static function delete($url, $headers = array(), $options = array()) { |
|
| 262 | + public static function delete($url, $headers = array(), $options = array()) |
|
| 263 | + { |
|
| 245 | 264 | return self::request($url, $headers, null, self::DELETE, $options); |
| 246 | 265 | } |
| 247 | 266 | |
| 248 | 267 | /** |
| 249 | 268 | * Send a TRACE request |
| 250 | 269 | */ |
| 251 | - public static function trace($url, $headers = array(), $options = array()) { |
|
| 270 | + public static function trace($url, $headers = array(), $options = array()) |
|
| 271 | + { |
|
| 252 | 272 | return self::request($url, $headers, null, self::TRACE, $options); |
| 253 | 273 | } |
| 254 | 274 | /**#@-*/ |
@@ -264,20 +284,23 @@ discard block |
||
| 264 | 284 | /** |
| 265 | 285 | * Send a POST request |
| 266 | 286 | */ |
| 267 | - public static function post($url, $headers = array(), $data = array(), $options = array()) { |
|
| 287 | + public static function post($url, $headers = array(), $data = array(), $options = array()) |
|
| 288 | + { |
|
| 268 | 289 | return self::request($url, $headers, $data, self::POST, $options); |
| 269 | 290 | } |
| 270 | 291 | /** |
| 271 | 292 | * Send a PUT request |
| 272 | 293 | */ |
| 273 | - public static function put($url, $headers = array(), $data = array(), $options = array()) { |
|
| 294 | + public static function put($url, $headers = array(), $data = array(), $options = array()) |
|
| 295 | + { |
|
| 274 | 296 | return self::request($url, $headers, $data, self::PUT, $options); |
| 275 | 297 | } |
| 276 | 298 | |
| 277 | 299 | /** |
| 278 | 300 | * Send an OPTIONS request |
| 279 | 301 | */ |
| 280 | - public static function options($url, $headers = array(), $data = array(), $options = array()) { |
|
| 302 | + public static function options($url, $headers = array(), $data = array(), $options = array()) |
|
| 303 | + { |
|
| 281 | 304 | return self::request($url, $headers, $data, self::OPTIONS, $options); |
| 282 | 305 | } |
| 283 | 306 | |
@@ -289,7 +312,8 @@ discard block |
||
| 289 | 312 | * |
| 290 | 313 | * @link https://tools.ietf.org/html/rfc5789 |
| 291 | 314 | */ |
| 292 | - public static function patch($url, $headers, $data = array(), $options = array()) { |
|
| 315 | + public static function patch($url, $headers, $data = array(), $options = array()) |
|
| 316 | + { |
|
| 293 | 317 | return self::request($url, $headers, $data, self::PATCH, $options); |
| 294 | 318 | } |
| 295 | 319 | /**#@-*/ |
@@ -354,8 +378,10 @@ discard block |
||
| 354 | 378 | * @param array $options Options for the request (see description for more information) |
| 355 | 379 | * @return Requests_Response |
| 356 | 380 | */ |
| 357 | - public static function request($url, $headers = array(), $data = array(), $type = self::GET, $options = array()) { |
|
| 358 | - if (empty($options['type'])) { |
|
| 381 | + public static function request($url, $headers = array(), $data = array(), $type = self::GET, $options = array()) |
|
| 382 | + { |
|
| 383 | + if (empty($options['type'])) |
|
| 384 | + { |
|
| 359 | 385 | $options['type'] = $type; |
| 360 | 386 | } |
| 361 | 387 | $options = array_merge(self::get_default_options(), $options); |
@@ -364,14 +390,17 @@ discard block |
||
| 364 | 390 | |
| 365 | 391 | $options['hooks']->dispatch('requests.before_request', array(&$url, &$headers, &$data, &$type, &$options)); |
| 366 | 392 | |
| 367 | - if (!empty($options['transport'])) { |
|
| 393 | + if (!empty($options['transport'])) |
|
| 394 | + { |
|
| 368 | 395 | $transport = $options['transport']; |
| 369 | 396 | |
| 370 | - if (is_string($options['transport'])) { |
|
| 397 | + if (is_string($options['transport'])) |
|
| 398 | + { |
|
| 371 | 399 | $transport = new $transport(); |
| 372 | 400 | } |
| 373 | 401 | } |
| 374 | - else { |
|
| 402 | + else |
|
| 403 | + { |
|
| 375 | 404 | $need_ssl = (0 === stripos($url, 'https://')); |
| 376 | 405 | $capabilities = array('ssl' => $need_ssl); |
| 377 | 406 | $transport = self::get_transport($capabilities); |
@@ -424,32 +453,42 @@ discard block |
||
| 424 | 453 | * @param array $options Global and default options (see {@see Requests::request}) |
| 425 | 454 | * @return array Responses (either Requests_Response or a Requests_Exception object) |
| 426 | 455 | */ |
| 427 | - public static function request_multiple($requests, $options = array()) { |
|
| 456 | + public static function request_multiple($requests, $options = array()) |
|
| 457 | + { |
|
| 428 | 458 | $options = array_merge(self::get_default_options(true), $options); |
| 429 | 459 | |
| 430 | - if (!empty($options['hooks'])) { |
|
| 460 | + if (!empty($options['hooks'])) |
|
| 461 | + { |
|
| 431 | 462 | $options['hooks']->register('transport.internal.parse_response', array('Requests', 'parse_multiple')); |
| 432 | - if (!empty($options['complete'])) { |
|
| 463 | + if (!empty($options['complete'])) |
|
| 464 | + { |
|
| 433 | 465 | $options['hooks']->register('multiple.request.complete', $options['complete']); |
| 434 | 466 | } |
| 435 | 467 | } |
| 436 | 468 | |
| 437 | - foreach ($requests as $id => &$request) { |
|
| 438 | - if (!isset($request['headers'])) { |
|
| 469 | + foreach ($requests as $id => &$request) |
|
| 470 | + { |
|
| 471 | + if (!isset($request['headers'])) |
|
| 472 | + { |
|
| 439 | 473 | $request['headers'] = array(); |
| 440 | 474 | } |
| 441 | - if (!isset($request['data'])) { |
|
| 475 | + if (!isset($request['data'])) |
|
| 476 | + { |
|
| 442 | 477 | $request['data'] = array(); |
| 443 | 478 | } |
| 444 | - if (!isset($request['type'])) { |
|
| 479 | + if (!isset($request['type'])) |
|
| 480 | + { |
|
| 445 | 481 | $request['type'] = self::GET; |
| 446 | 482 | } |
| 447 | - if (!isset($request['options'])) { |
|
| 483 | + if (!isset($request['options'])) |
|
| 484 | + { |
|
| 448 | 485 | $request['options'] = $options; |
| 449 | 486 | $request['options']['type'] = $request['type']; |
| 450 | 487 | } |
| 451 | - else { |
|
| 452 | - if (empty($request['options']['type'])) { |
|
| 488 | + else |
|
| 489 | + { |
|
| 490 | + if (empty($request['options']['type'])) |
|
| 491 | + { |
|
| 453 | 492 | $request['options']['type'] = $request['type']; |
| 454 | 493 | } |
| 455 | 494 | $request['options'] = array_merge($options, $request['options']); |
@@ -458,31 +497,38 @@ discard block |
||
| 458 | 497 | self::set_defaults($request['url'], $request['headers'], $request['data'], $request['type'], $request['options']); |
| 459 | 498 | |
| 460 | 499 | // Ensure we only hook in once |
| 461 | - if ($request['options']['hooks'] !== $options['hooks']) { |
|
| 500 | + if ($request['options']['hooks'] !== $options['hooks']) |
|
| 501 | + { |
|
| 462 | 502 | $request['options']['hooks']->register('transport.internal.parse_response', array('Requests', 'parse_multiple')); |
| 463 | - if (!empty($request['options']['complete'])) { |
|
| 503 | + if (!empty($request['options']['complete'])) |
|
| 504 | + { |
|
| 464 | 505 | $request['options']['hooks']->register('multiple.request.complete', $request['options']['complete']); |
| 465 | 506 | } |
| 466 | 507 | } |
| 467 | 508 | } |
| 468 | 509 | unset($request); |
| 469 | 510 | |
| 470 | - if (!empty($options['transport'])) { |
|
| 511 | + if (!empty($options['transport'])) |
|
| 512 | + { |
|
| 471 | 513 | $transport = $options['transport']; |
| 472 | 514 | |
| 473 | - if (is_string($options['transport'])) { |
|
| 515 | + if (is_string($options['transport'])) |
|
| 516 | + { |
|
| 474 | 517 | $transport = new $transport(); |
| 475 | 518 | } |
| 476 | 519 | } |
| 477 | - else { |
|
| 520 | + else |
|
| 521 | + { |
|
| 478 | 522 | $transport = self::get_transport(); |
| 479 | 523 | } |
| 480 | 524 | $responses = $transport->request_multiple($requests, $options); |
| 481 | 525 | |
| 482 | - foreach ($responses as $id => &$response) { |
|
| 526 | + foreach ($responses as $id => &$response) |
|
| 527 | + { |
|
| 483 | 528 | // If our hook got messed with somehow, ensure we end up with the |
| 484 | 529 | // correct response |
| 485 | - if (is_string($response)) { |
|
| 530 | + if (is_string($response)) |
|
| 531 | + { |
|
| 486 | 532 | $request = $requests[$id]; |
| 487 | 533 | self::parse_multiple($response, $request); |
| 488 | 534 | $request['options']['hooks']->dispatch('multiple.request.complete', array(&$response, $id)); |
@@ -499,7 +545,8 @@ discard block |
||
| 499 | 545 | * @param boolean $multirequest Is this a multirequest? |
| 500 | 546 | * @return array Default option values |
| 501 | 547 | */ |
| 502 | - protected static function get_default_options($multirequest = false) { |
|
| 548 | + protected static function get_default_options($multirequest = false) |
|
| 549 | + { |
|
| 503 | 550 | $defaults = array( |
| 504 | 551 | 'timeout' => 10, |
| 505 | 552 | 'connect_timeout' => 10, |
@@ -521,7 +568,8 @@ discard block |
||
| 521 | 568 | 'verify' => Requests::get_certificate_path(), |
| 522 | 569 | 'verifyname' => true, |
| 523 | 570 | ); |
| 524 | - if ($multirequest !== false) { |
|
| 571 | + if ($multirequest !== false) |
|
| 572 | + { |
|
| 525 | 573 | $defaults['complete'] = null; |
| 526 | 574 | } |
| 527 | 575 | return $defaults; |
@@ -532,8 +580,10 @@ discard block |
||
| 532 | 580 | * |
| 533 | 581 | * @return string Default certificate path. |
| 534 | 582 | */ |
| 535 | - public static function get_certificate_path() { |
|
| 536 | - if ( ! empty( Requests::$certificate_path ) ) { |
|
| 583 | + public static function get_certificate_path() |
|
| 584 | + { |
|
| 585 | + if ( ! empty( Requests::$certificate_path ) ) |
|
| 586 | + { |
|
| 537 | 587 | return Requests::$certificate_path; |
| 538 | 588 | } |
| 539 | 589 | |
@@ -545,7 +595,8 @@ discard block |
||
| 545 | 595 | * |
| 546 | 596 | * @param string $path Certificate path, pointing to a PEM file. |
| 547 | 597 | */ |
| 548 | - public static function set_certificate_path( $path ) { |
|
| 598 | + public static function set_certificate_path( $path ) |
|
| 599 | + { |
|
| 549 | 600 | Requests::$certificate_path = $path; |
| 550 | 601 | } |
| 551 | 602 | |
@@ -559,40 +610,51 @@ discard block |
||
| 559 | 610 | * @param array $options Options for the request |
| 560 | 611 | * @return array $options |
| 561 | 612 | */ |
| 562 | - protected static function set_defaults(&$url, &$headers, &$data, &$type, &$options) { |
|
| 563 | - if (!preg_match('/^http(s)?:\/\//i', $url, $matches)) { |
|
| 613 | + protected static function set_defaults(&$url, &$headers, &$data, &$type, &$options) |
|
| 614 | + { |
|
| 615 | + if (!preg_match('/^http(s)?:\/\//i', $url, $matches)) |
|
| 616 | + { |
|
| 564 | 617 | throw new Requests_Exception('Only HTTP(S) requests are handled.', 'nonhttp', $url); |
| 565 | 618 | } |
| 566 | 619 | |
| 567 | - if (empty($options['hooks'])) { |
|
| 620 | + if (empty($options['hooks'])) |
|
| 621 | + { |
|
| 568 | 622 | $options['hooks'] = new Requests_Hooks(); |
| 569 | 623 | } |
| 570 | 624 | |
| 571 | - if (is_array($options['auth'])) { |
|
| 625 | + if (is_array($options['auth'])) |
|
| 626 | + { |
|
| 572 | 627 | $options['auth'] = new Requests_Auth_Basic($options['auth']); |
| 573 | 628 | } |
| 574 | - if ($options['auth'] !== false) { |
|
| 629 | + if ($options['auth'] !== false) |
|
| 630 | + { |
|
| 575 | 631 | $options['auth']->register($options['hooks']); |
| 576 | 632 | } |
| 577 | 633 | |
| 578 | - if (is_string($options['proxy']) || is_array($options['proxy'])) { |
|
| 634 | + if (is_string($options['proxy']) || is_array($options['proxy'])) |
|
| 635 | + { |
|
| 579 | 636 | $options['proxy'] = new Requests_Proxy_HTTP($options['proxy']); |
| 580 | 637 | } |
| 581 | - if ($options['proxy'] !== false) { |
|
| 638 | + if ($options['proxy'] !== false) |
|
| 639 | + { |
|
| 582 | 640 | $options['proxy']->register($options['hooks']); |
| 583 | 641 | } |
| 584 | 642 | |
| 585 | - if (is_array($options['cookies'])) { |
|
| 643 | + if (is_array($options['cookies'])) |
|
| 644 | + { |
|
| 586 | 645 | $options['cookies'] = new Requests_Cookie_Jar($options['cookies']); |
| 587 | 646 | } |
| 588 | - elseif (empty($options['cookies'])) { |
|
| 647 | + elseif (empty($options['cookies'])) |
|
| 648 | + { |
|
| 589 | 649 | $options['cookies'] = new Requests_Cookie_Jar(); |
| 590 | 650 | } |
| 591 | - if ($options['cookies'] !== false) { |
|
| 651 | + if ($options['cookies'] !== false) |
|
| 652 | + { |
|
| 592 | 653 | $options['cookies']->register($options['hooks']); |
| 593 | 654 | } |
| 594 | 655 | |
| 595 | - if ($options['idn'] !== false) { |
|
| 656 | + if ($options['idn'] !== false) |
|
| 657 | + { |
|
| 596 | 658 | $iri = new Requests_IRI($url); |
| 597 | 659 | $iri->host = Requests_IDNAEncoder::encode($iri->ihost); |
| 598 | 660 | $url = $iri->uri; |
@@ -601,11 +663,14 @@ discard block |
||
| 601 | 663 | // Massage the type to ensure we support it. |
| 602 | 664 | $type = strtoupper($type); |
| 603 | 665 | |
| 604 | - if (!isset($options['data_format'])) { |
|
| 605 | - if (in_array($type, array(self::HEAD, self::GET, self::DELETE))) { |
|
| 666 | + if (!isset($options['data_format'])) |
|
| 667 | + { |
|
| 668 | + if (in_array($type, array(self::HEAD, self::GET, self::DELETE))) |
|
| 669 | + { |
|
| 606 | 670 | $options['data_format'] = 'query'; |
| 607 | 671 | } |
| 608 | - else { |
|
| 672 | + else |
|
| 673 | + { |
|
| 609 | 674 | $options['data_format'] = 'body'; |
| 610 | 675 | } |
| 611 | 676 | } |
@@ -625,17 +690,21 @@ discard block |
||
| 625 | 690 | * @param array $options Original $options array passed to {@link request()}, in case we need to follow redirects |
| 626 | 691 | * @return Requests_Response |
| 627 | 692 | */ |
| 628 | - protected static function parse_response($headers, $url, $req_headers, $req_data, $options) { |
|
| 693 | + protected static function parse_response($headers, $url, $req_headers, $req_data, $options) |
|
| 694 | + { |
|
| 629 | 695 | $return = new Requests_Response(); |
| 630 | - if (!$options['blocking']) { |
|
| 696 | + if (!$options['blocking']) |
|
| 697 | + { |
|
| 631 | 698 | return $return; |
| 632 | 699 | } |
| 633 | 700 | |
| 634 | 701 | $return->raw = $headers; |
| 635 | 702 | $return->url = $url; |
| 636 | 703 | |
| 637 | - if (!$options['filename']) { |
|
| 638 | - if (($pos = strpos($headers, "\r\n\r\n")) === false) { |
|
| 704 | + if (!$options['filename']) |
|
| 705 | + { |
|
| 706 | + if (($pos = strpos($headers, "\r\n\r\n")) === false) |
|
| 707 | + { |
|
| 639 | 708 | // Crap! |
| 640 | 709 | throw new Requests_Exception('Missing header/body separator', 'requests.no_crlf_separator'); |
| 641 | 710 | } |
@@ -643,7 +712,8 @@ discard block |
||
| 643 | 712 | $headers = substr($return->raw, 0, $pos); |
| 644 | 713 | $return->body = substr($return->raw, $pos + strlen("\n\r\n\r")); |
| 645 | 714 | } |
| 646 | - else { |
|
| 715 | + else |
|
| 716 | + { |
|
| 647 | 717 | $return->body = ''; |
| 648 | 718 | } |
| 649 | 719 | // Pretend CRLF = LF for compatibility (RFC 2616, section 19.3) |
@@ -652,44 +722,54 @@ discard block |
||
| 652 | 722 | $headers = preg_replace('/\n[ \t]/', ' ', $headers); |
| 653 | 723 | $headers = explode("\n", $headers); |
| 654 | 724 | preg_match('#^HTTP/(1\.\d)[ \t]+(\d+)#i', array_shift($headers), $matches); |
| 655 | - if (empty($matches)) { |
|
| 725 | + if (empty($matches)) |
|
| 726 | + { |
|
| 656 | 727 | throw new Requests_Exception('Response could not be parsed', 'noversion', $headers); |
| 657 | 728 | } |
| 658 | 729 | $return->protocol_version = (float) $matches[1]; |
| 659 | 730 | $return->status_code = (int) $matches[2]; |
| 660 | - if ($return->status_code >= 200 && $return->status_code < 300) { |
|
| 731 | + if ($return->status_code >= 200 && $return->status_code < 300) |
|
| 732 | + { |
|
| 661 | 733 | $return->success = true; |
| 662 | 734 | } |
| 663 | 735 | |
| 664 | - foreach ($headers as $header) { |
|
| 736 | + foreach ($headers as $header) |
|
| 737 | + { |
|
| 665 | 738 | list($key, $value) = explode(':', $header, 2); |
| 666 | 739 | $value = trim($value); |
| 667 | 740 | preg_replace('#(\s+)#i', ' ', $value); |
| 668 | 741 | $return->headers[$key] = $value; |
| 669 | 742 | } |
| 670 | - if (isset($return->headers['transfer-encoding'])) { |
|
| 743 | + if (isset($return->headers['transfer-encoding'])) |
|
| 744 | + { |
|
| 671 | 745 | $return->body = self::decode_chunked($return->body); |
| 672 | 746 | unset($return->headers['transfer-encoding']); |
| 673 | 747 | } |
| 674 | - if (isset($return->headers['content-encoding'])) { |
|
| 748 | + if (isset($return->headers['content-encoding'])) |
|
| 749 | + { |
|
| 675 | 750 | $return->body = self::decompress($return->body); |
| 676 | 751 | } |
| 677 | 752 | |
| 678 | 753 | //fsockopen and cURL compatibility |
| 679 | - if (isset($return->headers['connection'])) { |
|
| 754 | + if (isset($return->headers['connection'])) |
|
| 755 | + { |
|
| 680 | 756 | unset($return->headers['connection']); |
| 681 | 757 | } |
| 682 | 758 | |
| 683 | 759 | $options['hooks']->dispatch('requests.before_redirect_check', array(&$return, $req_headers, $req_data, $options)); |
| 684 | 760 | |
| 685 | - if ($return->is_redirect() && $options['follow_redirects'] === true) { |
|
| 686 | - if (isset($return->headers['location']) && $options['redirected'] < $options['redirects']) { |
|
| 687 | - if ($return->status_code === 303) { |
|
| 761 | + if ($return->is_redirect() && $options['follow_redirects'] === true) |
|
| 762 | + { |
|
| 763 | + if (isset($return->headers['location']) && $options['redirected'] < $options['redirects']) |
|
| 764 | + { |
|
| 765 | + if ($return->status_code === 303) |
|
| 766 | + { |
|
| 688 | 767 | $options['type'] = self::GET; |
| 689 | 768 | } |
| 690 | 769 | $options['redirected']++; |
| 691 | 770 | $location = $return->headers['location']; |
| 692 | - if (strpos($location, 'http://') !== 0 && strpos($location, 'https://') !== 0) { |
|
| 771 | + if (strpos($location, 'http://') !== 0 && strpos($location, 'https://') !== 0) |
|
| 772 | + { |
|
| 693 | 773 | // relative redirect, for compatibility make it absolute |
| 694 | 774 | $location = Requests_IRI::absolutize($url, $location); |
| 695 | 775 | $location = $location->uri; |
@@ -707,7 +787,8 @@ discard block |
||
| 707 | 787 | $redirected->history[] = $return; |
| 708 | 788 | return $redirected; |
| 709 | 789 | } |
| 710 | - elseif ($options['redirected'] >= $options['redirects']) { |
|
| 790 | + elseif ($options['redirected'] >= $options['redirects']) |
|
| 791 | + { |
|
| 711 | 792 | throw new Requests_Exception('Too many redirects', 'toomanyredirects', $return); |
| 712 | 793 | } |
| 713 | 794 | } |
@@ -728,15 +809,18 @@ discard block |
||
| 728 | 809 | * @param array $request Request data as passed into {@see Requests::request_multiple()} |
| 729 | 810 | * @return null `$response` is either set to a Requests_Response instance, or a Requests_Exception object |
| 730 | 811 | */ |
| 731 | - public static function parse_multiple(&$response, $request) { |
|
| 732 | - try { |
|
| 812 | + public static function parse_multiple(&$response, $request) |
|
| 813 | + { |
|
| 814 | + try |
|
| 815 | + { |
|
| 733 | 816 | $url = $request['url']; |
| 734 | 817 | $headers = $request['headers']; |
| 735 | 818 | $data = $request['data']; |
| 736 | 819 | $options = $request['options']; |
| 737 | 820 | $response = self::parse_response($response, $url, $headers, $data, $options); |
| 738 | 821 | } |
| 739 | - catch (Requests_Exception $e) { |
|
| 822 | + catch (Requests_Exception $e) |
|
| 823 | + { |
|
| 740 | 824 | $response = $e; |
| 741 | 825 | } |
| 742 | 826 | } |
@@ -748,8 +832,10 @@ discard block |
||
| 748 | 832 | * @param string $data Chunked body |
| 749 | 833 | * @return string Decoded body |
| 750 | 834 | */ |
| 751 | - protected static function decode_chunked($data) { |
|
| 752 | - if (!preg_match('/^([0-9a-f]+)(?:;(?:[\w-]*)(?:=(?:(?:[\w-]*)*|"(?:[^\r\n])*"))?)*\r\n/i', trim($data))) { |
|
| 835 | + protected static function decode_chunked($data) |
|
| 836 | + { |
|
| 837 | + if (!preg_match('/^([0-9a-f]+)(?:;(?:[\w-]*)(?:=(?:(?:[\w-]*)*|"(?:[^\r\n])*"))?)*\r\n/i', trim($data))) |
|
| 838 | + { |
|
| 753 | 839 | return $data; |
| 754 | 840 | } |
| 755 | 841 | |
@@ -758,15 +844,18 @@ discard block |
||
| 758 | 844 | $decoded = ''; |
| 759 | 845 | $encoded = $data; |
| 760 | 846 | |
| 761 | - while (true) { |
|
| 847 | + while (true) |
|
| 848 | + { |
|
| 762 | 849 | $is_chunked = (bool) preg_match('/^([0-9a-f]+)(?:;(?:[\w-]*)(?:=(?:(?:[\w-]*)*|"(?:[^\r\n])*"))?)*\r\n/i', $encoded, $matches); |
| 763 | - if (!$is_chunked) { |
|
| 850 | + if (!$is_chunked) |
|
| 851 | + { |
|
| 764 | 852 | // Looks like it's not chunked after all |
| 765 | 853 | return $data; |
| 766 | 854 | } |
| 767 | 855 | |
| 768 | 856 | $length = hexdec(trim($matches[1])); |
| 769 | - if ($length === 0) { |
|
| 857 | + if ($length === 0) |
|
| 858 | + { |
|
| 770 | 859 | // Ignore trailer headers |
| 771 | 860 | return $decoded; |
| 772 | 861 | } |
@@ -775,7 +864,8 @@ discard block |
||
| 775 | 864 | $decoded .= substr($encoded, $chunk_length, $length); |
| 776 | 865 | $encoded = substr($encoded, $chunk_length + $length + 2); |
| 777 | 866 | |
| 778 | - if (trim($encoded) === '0' || empty($encoded)) { |
|
| 867 | + if (trim($encoded) === '0' || empty($encoded)) |
|
| 868 | + { |
|
| 779 | 869 | return $decoded; |
| 780 | 870 | } |
| 781 | 871 | } |
@@ -791,9 +881,11 @@ discard block |
||
| 791 | 881 | * @param array $array Dictionary of header values |
| 792 | 882 | * @return array List of headers |
| 793 | 883 | */ |
| 794 | - public static function flatten($array) { |
|
| 884 | + public static function flatten($array) |
|
| 885 | + { |
|
| 795 | 886 | $return = array(); |
| 796 | - foreach ($array as $key => $value) { |
|
| 887 | + foreach ($array as $key => $value) |
|
| 888 | + { |
|
| 797 | 889 | $return[] = sprintf('%s: %s', $key, $value); |
| 798 | 890 | } |
| 799 | 891 | return $return; |
@@ -807,7 +899,8 @@ discard block |
||
| 807 | 899 | * @param array $array Dictionary of header values |
| 808 | 900 | * @return array List of headers |
| 809 | 901 | */ |
| 810 | - public static function flattern($array) { |
|
| 902 | + public static function flattern($array) |
|
| 903 | + { |
|
| 811 | 904 | return self::flatten($array); |
| 812 | 905 | } |
| 813 | 906 | |
@@ -820,22 +913,28 @@ discard block |
||
| 820 | 913 | * @param string $data Compressed data in one of the above formats |
| 821 | 914 | * @return string Decompressed string |
| 822 | 915 | */ |
| 823 | - public static function decompress($data) { |
|
| 824 | - if (substr($data, 0, 2) !== "\x1f\x8b" && substr($data, 0, 2) !== "\x78\x9c") { |
|
| 916 | + public static function decompress($data) |
|
| 917 | + { |
|
| 918 | + if (substr($data, 0, 2) !== "\x1f\x8b" && substr($data, 0, 2) !== "\x78\x9c") |
|
| 919 | + { |
|
| 825 | 920 | // Not actually compressed. Probably cURL ruining this for us. |
| 826 | 921 | return $data; |
| 827 | 922 | } |
| 828 | 923 | |
| 829 | - if (function_exists('gzdecode') && ($decoded = @gzdecode($data)) !== false) { |
|
| 924 | + if (function_exists('gzdecode') && ($decoded = @gzdecode($data)) !== false) |
|
| 925 | + { |
|
| 830 | 926 | return $decoded; |
| 831 | 927 | } |
| 832 | - elseif (function_exists('gzinflate') && ($decoded = @gzinflate($data)) !== false) { |
|
| 928 | + elseif (function_exists('gzinflate') && ($decoded = @gzinflate($data)) !== false) |
|
| 929 | + { |
|
| 833 | 930 | return $decoded; |
| 834 | 931 | } |
| 835 | - elseif (($decoded = self::compatible_gzinflate($data)) !== false) { |
|
| 932 | + elseif (($decoded = self::compatible_gzinflate($data)) !== false) |
|
| 933 | + { |
|
| 836 | 934 | return $decoded; |
| 837 | 935 | } |
| 838 | - elseif (function_exists('gzuncompress') && ($decoded = @gzuncompress($data)) !== false) { |
|
| 936 | + elseif (function_exists('gzuncompress') && ($decoded = @gzuncompress($data)) !== false) |
|
| 937 | + { |
|
| 839 | 938 | return $decoded; |
| 840 | 939 | } |
| 841 | 940 | |
@@ -862,29 +961,37 @@ discard block |
||
| 862 | 961 | * @param string $gzData String to decompress. |
| 863 | 962 | * @return string|bool False on failure. |
| 864 | 963 | */ |
| 865 | - public static function compatible_gzinflate($gzData) { |
|
| 964 | + public static function compatible_gzinflate($gzData) |
|
| 965 | + { |
|
| 866 | 966 | // Compressed data might contain a full zlib header, if so strip it for |
| 867 | 967 | // gzinflate() |
| 868 | - if (substr($gzData, 0, 3) == "\x1f\x8b\x08") { |
|
| 968 | + if (substr($gzData, 0, 3) == "\x1f\x8b\x08") |
|
| 969 | + { |
|
| 869 | 970 | $i = 10; |
| 870 | 971 | $flg = ord(substr($gzData, 3, 1)); |
| 871 | - if ($flg > 0) { |
|
| 872 | - if ($flg & 4) { |
|
| 972 | + if ($flg > 0) |
|
| 973 | + { |
|
| 974 | + if ($flg & 4) |
|
| 975 | + { |
|
| 873 | 976 | list($xlen) = unpack('v', substr($gzData, $i, 2)); |
| 874 | 977 | $i = $i + 2 + $xlen; |
| 875 | 978 | } |
| 876 | - if ($flg & 8) { |
|
| 979 | + if ($flg & 8) |
|
| 980 | + { |
|
| 877 | 981 | $i = strpos($gzData, "\0", $i) + 1; |
| 878 | 982 | } |
| 879 | - if ($flg & 16) { |
|
| 983 | + if ($flg & 16) |
|
| 984 | + { |
|
| 880 | 985 | $i = strpos($gzData, "\0", $i) + 1; |
| 881 | 986 | } |
| 882 | - if ($flg & 2) { |
|
| 987 | + if ($flg & 2) |
|
| 988 | + { |
|
| 883 | 989 | $i = $i + 2; |
| 884 | 990 | } |
| 885 | 991 | } |
| 886 | 992 | $decompressed = self::compatible_gzinflate(substr($gzData, $i)); |
| 887 | - if (false !== $decompressed) { |
|
| 993 | + if (false !== $decompressed) |
|
| 994 | + { |
|
| 888 | 995 | return $decompressed; |
| 889 | 996 | } |
| 890 | 997 | } |
@@ -905,17 +1012,21 @@ discard block |
||
| 905 | 1012 | // First 2 bytes should be divisible by 0x1F |
| 906 | 1013 | list(, $first_two_bytes) = unpack('n', $gzData); |
| 907 | 1014 | |
| 908 | - if (0x08 == $first_nibble && 0 == ($first_two_bytes % 0x1F)) { |
|
| 1015 | + if (0x08 == $first_nibble && 0 == ($first_two_bytes % 0x1F)) |
|
| 1016 | + { |
|
| 909 | 1017 | $huffman_encoded = true; |
| 910 | 1018 | } |
| 911 | 1019 | |
| 912 | - if ($huffman_encoded) { |
|
| 913 | - if (false !== ($decompressed = @gzinflate(substr($gzData, 2)))) { |
|
| 1020 | + if ($huffman_encoded) |
|
| 1021 | + { |
|
| 1022 | + if (false !== ($decompressed = @gzinflate(substr($gzData, 2)))) |
|
| 1023 | + { |
|
| 914 | 1024 | return $decompressed; |
| 915 | 1025 | } |
| 916 | 1026 | } |
| 917 | 1027 | |
| 918 | - if ("\x50\x4b\x03\x04" == substr($gzData, 0, 4)) { |
|
| 1028 | + if ("\x50\x4b\x03\x04" == substr($gzData, 0, 4)) |
|
| 1029 | + { |
|
| 919 | 1030 | // ZIP file format header |
| 920 | 1031 | // Offset 6: 2 bytes, General-purpose field |
| 921 | 1032 | // Offset 26: 2 bytes, filename length |
@@ -929,7 +1040,8 @@ discard block |
||
| 929 | 1040 | // between a compressed document, and a ZIP file |
| 930 | 1041 | $zip_compressed_on_the_fly = (0x08 == (0x08 & $general_purpose_flag)); |
| 931 | 1042 | |
| 932 | - if (!$zip_compressed_on_the_fly) { |
|
| 1043 | + if (!$zip_compressed_on_the_fly) |
|
| 1044 | + { |
|
| 933 | 1045 | // Don't attempt to decode a compressed zip file |
| 934 | 1046 | return $gzData; |
| 935 | 1047 | } |
@@ -937,29 +1049,34 @@ discard block |
||
| 937 | 1049 | // Determine the first byte of data, based on the above ZIP header |
| 938 | 1050 | // offsets: |
| 939 | 1051 | $first_file_start = array_sum(unpack('v2', substr($gzData, 26, 4))); |
| 940 | - if (false !== ($decompressed = @gzinflate(substr($gzData, 30 + $first_file_start)))) { |
|
| 1052 | + if (false !== ($decompressed = @gzinflate(substr($gzData, 30 + $first_file_start)))) |
|
| 1053 | + { |
|
| 941 | 1054 | return $decompressed; |
| 942 | 1055 | } |
| 943 | 1056 | return false; |
| 944 | 1057 | } |
| 945 | 1058 | |
| 946 | 1059 | // Finally fall back to straight gzinflate |
| 947 | - if (false !== ($decompressed = @gzinflate($gzData))) { |
|
| 1060 | + if (false !== ($decompressed = @gzinflate($gzData))) |
|
| 1061 | + { |
|
| 948 | 1062 | return $decompressed; |
| 949 | 1063 | } |
| 950 | 1064 | |
| 951 | 1065 | // Fallback for all above failing, not expected, but included for |
| 952 | 1066 | // debugging and preventing regressions and to track stats |
| 953 | - if (false !== ($decompressed = @gzinflate(substr($gzData, 2)))) { |
|
| 1067 | + if (false !== ($decompressed = @gzinflate(substr($gzData, 2)))) |
|
| 1068 | + { |
|
| 954 | 1069 | return $decompressed; |
| 955 | 1070 | } |
| 956 | 1071 | |
| 957 | 1072 | return false; |
| 958 | 1073 | } |
| 959 | 1074 | |
| 960 | - public static function match_domain($host, $reference) { |
|
| 1075 | + public static function match_domain($host, $reference) |
|
| 1076 | + { |
|
| 961 | 1077 | // Check for a direct match |
| 962 | - if ($host === $reference) { |
|
| 1078 | + if ($host === $reference) |
|
| 1079 | + { |
|
| 963 | 1080 | return true; |
| 964 | 1081 | } |
| 965 | 1082 | |
@@ -967,10 +1084,12 @@ discard block |
||
| 967 | 1084 | // Also validates that the host has 3 parts or more, as per Firefox's |
| 968 | 1085 | // ruleset. |
| 969 | 1086 | $parts = explode('.', $host); |
| 970 | - if (ip2long($host) === false && count($parts) >= 3) { |
|
| 1087 | + if (ip2long($host) === false && count($parts) >= 3) |
|
| 1088 | + { |
|
| 971 | 1089 | $parts[0] = '*'; |
| 972 | 1090 | $wildcard = implode('.', $parts); |
| 973 | - if ($wildcard === $reference) { |
|
| 1091 | + if ($wildcard === $reference) |
|
| 1092 | + { |
|
| 974 | 1093 | return true; |
| 975 | 1094 | } |
| 976 | 1095 | } |
@@ -1,7 +1,8 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | |
| 4 | -class GetPostDetails extends AbstractRequest { |
|
| 4 | +class GetPostDetails extends AbstractRequest |
|
| 5 | +{ |
|
| 5 | 6 | |
| 6 | 7 | function getApiEndPoint() |
| 7 | 8 | { |
@@ -1,6 +1,7 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -class SendJodel extends AbstractRequest { |
|
| 3 | +class SendJodel extends AbstractRequest |
|
| 4 | +{ |
|
| 4 | 5 | /** |
| 5 | 6 | * @var Location |
| 6 | 7 | */ |
@@ -1,7 +1,8 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | |
| 4 | -class UpdateLocation extends AbstractRequest { |
|
| 4 | +class UpdateLocation extends AbstractRequest |
|
| 5 | +{ |
|
| 5 | 6 | /** |
| 6 | 7 | * @var Location |
| 7 | 8 | */ |
@@ -1,6 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -class Location{ |
|
| 3 | +class Location |
|
| 4 | +{ |
|
| 4 | 5 | |
| 5 | 6 | public $cityName; |
| 6 | 7 | |
@@ -37,7 +38,8 @@ discard block |
||
| 37 | 38 | { |
| 38 | 39 | $this->lng = $lng; |
| 39 | 40 | } |
| 40 | - public function toArray(){ |
|
| 41 | + public function toArray() |
|
| 42 | + { |
|
| 41 | 43 | return array( |
| 42 | 44 | "city" => $this->getCityName(), |
| 43 | 45 | "country" => 'DE', |
@@ -45,7 +45,8 @@ |
||
| 45 | 45 | |
| 46 | 46 | if(isset($_GET['view'])) |
| 47 | 47 | { |
| 48 | - switch ($_GET['view']) { |
|
| 48 | + switch ($_GET['view']) |
|
| 49 | + { |
|
| 49 | 50 | case 'comment': |
| 50 | 51 | $view = 'comment'; |
| 51 | 52 | break; |