diff options
Diffstat (limited to 'application/HttpUtils.php')
-rw-r--r-- | application/HttpUtils.php | 62 |
1 files changed, 47 insertions, 15 deletions
diff --git a/application/HttpUtils.php b/application/HttpUtils.php index 83a4c5e2..9c438160 100644 --- a/application/HttpUtils.php +++ b/application/HttpUtils.php | |||
@@ -1,13 +1,14 @@ | |||
1 | <?php | 1 | <?php |
2 | /** | 2 | /** |
3 | * GET an HTTP URL to retrieve its content | 3 | * GET an HTTP URL to retrieve its content |
4 | * Uses the cURL library or a fallback method | 4 | * Uses the cURL library or a fallback method |
5 | * | 5 | * |
6 | * @param string $url URL to get (http://...) | 6 | * @param string $url URL to get (http://...) |
7 | * @param int $timeout network timeout (in seconds) | 7 | * @param int $timeout network timeout (in seconds) |
8 | * @param int $maxBytes maximum downloaded bytes (default: 4 MiB) | 8 | * @param int $maxBytes maximum downloaded bytes (default: 4 MiB) |
9 | * @param callable|string $curlWriteFunction Optional callback called during the download (cURL CURLOPT_WRITEFUNCTION). | 9 | * @param callable|string $curlWriteFunction Optional callback called during the download (cURL CURLOPT_WRITEFUNCTION). |
10 | * Can be used to add download conditions on the headers (response code, content type, etc.). | 10 | * Can be used to add download conditions on the |
11 | * headers (response code, content type, etc.). | ||
11 | * | 12 | * |
12 | * @return array HTTP response headers, downloaded content | 13 | * @return array HTTP response headers, downloaded content |
13 | * | 14 | * |
@@ -64,29 +65,30 @@ function get_http_response($url, $timeout = 30, $maxBytes = 4194304, $curlWriteF | |||
64 | } | 65 | } |
65 | 66 | ||
66 | // General cURL settings | 67 | // General cURL settings |
67 | curl_setopt($ch, CURLOPT_AUTOREFERER, true); | 68 | curl_setopt($ch, CURLOPT_AUTOREFERER, true); |
68 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | 69 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
69 | curl_setopt($ch, CURLOPT_HEADER, true); | 70 | curl_setopt($ch, CURLOPT_HEADER, true); |
70 | curl_setopt( | 71 | curl_setopt( |
71 | $ch, | 72 | $ch, |
72 | CURLOPT_HTTPHEADER, | 73 | CURLOPT_HTTPHEADER, |
73 | array('Accept-Language: ' . $acceptLanguage) | 74 | array('Accept-Language: ' . $acceptLanguage) |
74 | ); | 75 | ); |
75 | curl_setopt($ch, CURLOPT_MAXREDIRS, $maxRedirs); | 76 | curl_setopt($ch, CURLOPT_MAXREDIRS, $maxRedirs); |
76 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | 77 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
77 | curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); | 78 | curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); |
78 | curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); | 79 | curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); |
79 | 80 | ||
80 | if (is_callable($curlWriteFunction)) { | 81 | if (is_callable($curlWriteFunction)) { |
81 | curl_setopt($ch, CURLOPT_WRITEFUNCTION, $curlWriteFunction); | 82 | curl_setopt($ch, CURLOPT_WRITEFUNCTION, $curlWriteFunction); |
82 | } | 83 | } |
83 | 84 | ||
84 | // Max download size management | 85 | // Max download size management |
85 | curl_setopt($ch, CURLOPT_BUFFERSIZE, 1024*16); | 86 | curl_setopt($ch, CURLOPT_BUFFERSIZE, 1024*16); |
86 | curl_setopt($ch, CURLOPT_NOPROGRESS, false); | 87 | curl_setopt($ch, CURLOPT_NOPROGRESS, false); |
87 | curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, | 88 | curl_setopt( |
88 | function($arg0, $arg1, $arg2, $arg3, $arg4 = 0) use ($maxBytes) | 89 | $ch, |
89 | { | 90 | CURLOPT_PROGRESSFUNCTION, |
91 | function ($arg0, $arg1, $arg2, $arg3, $arg4 = 0) use ($maxBytes) { | ||
90 | if (version_compare(phpversion(), '5.5', '<')) { | 92 | if (version_compare(phpversion(), '5.5', '<')) { |
91 | // PHP version lower than 5.5 | 93 | // PHP version lower than 5.5 |
92 | // Callback has 4 arguments | 94 | // Callback has 4 arguments |
@@ -232,7 +234,6 @@ function get_redirected_headers($url, $redirectionLimit = 3) | |||
232 | && !empty($headers) | 234 | && !empty($headers) |
233 | && (strpos($headers[0], '301') !== false || strpos($headers[0], '302') !== false) | 235 | && (strpos($headers[0], '301') !== false || strpos($headers[0], '302') !== false) |
234 | && !empty($headers['Location'])) { | 236 | && !empty($headers['Location'])) { |
235 | |||
236 | $redirection = is_array($headers['Location']) ? end($headers['Location']) : $headers['Location']; | 237 | $redirection = is_array($headers['Location']) ? end($headers['Location']) : $headers['Location']; |
237 | if ($redirection != $url) { | 238 | if ($redirection != $url) { |
238 | $redirection = getAbsoluteUrl($url, $redirection); | 239 | $redirection = getAbsoluteUrl($url, $redirection); |
@@ -415,6 +416,37 @@ function getIpAddressFromProxy($server, $trustedIps) | |||
415 | return array_pop($ips); | 416 | return array_pop($ips); |
416 | } | 417 | } |
417 | 418 | ||
419 | |||
420 | /** | ||
421 | * Return an identifier based on the advertised client IP address(es) | ||
422 | * | ||
423 | * This aims at preventing session hijacking from users behind the same proxy | ||
424 | * by relying on HTTP headers. | ||
425 | * | ||
426 | * See: | ||
427 | * - https://secure.php.net/manual/en/reserved.variables.server.php | ||
428 | * - https://stackoverflow.com/questions/3003145/how-to-get-the-client-ip-address-in-php | ||
429 | * - https://stackoverflow.com/questions/12233406/preventing-session-hijacking | ||
430 | * - https://stackoverflow.com/questions/21354859/trusting-x-forwarded-for-to-identify-a-visitor | ||
431 | * | ||
432 | * @param array $server The $_SERVER array | ||
433 | * | ||
434 | * @return string An identifier based on client IP address information | ||
435 | */ | ||
436 | function client_ip_id($server) | ||
437 | { | ||
438 | $ip = $server['REMOTE_ADDR']; | ||
439 | |||
440 | if (isset($server['HTTP_X_FORWARDED_FOR'])) { | ||
441 | $ip = $ip . '_' . $server['HTTP_X_FORWARDED_FOR']; | ||
442 | } | ||
443 | if (isset($server['HTTP_CLIENT_IP'])) { | ||
444 | $ip = $ip . '_' . $server['HTTP_CLIENT_IP']; | ||
445 | } | ||
446 | return $ip; | ||
447 | } | ||
448 | |||
449 | |||
418 | /** | 450 | /** |
419 | * Returns true if Shaarli's currently browsed in HTTPS. | 451 | * Returns true if Shaarli's currently browsed in HTTPS. |
420 | * Supports reverse proxies (if the headers are correctly set). | 452 | * Supports reverse proxies (if the headers are correctly set). |