diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-11-10 10:46:04 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-10 10:46:04 +0100 |
commit | 302662797cb5e8ac6579a99297ceae301f7927a6 (patch) | |
tree | 67f493a1cc6d3691742946f1d7e512bffe41e02f /application/http | |
parent | c94c32d1a3e86a479cb2582eadc668a5bb476fc6 (diff) | |
parent | 2f4df753041088d788d1923692a7d530167a6840 (diff) | |
download | Shaarli-302662797cb5e8ac6579a99297ceae301f7927a6.tar.gz Shaarli-302662797cb5e8ac6579a99297ceae301f7927a6.tar.zst Shaarli-302662797cb5e8ac6579a99297ceae301f7927a6.zip |
Merge pull request #1635 from ArthurHoaro/feature/phpcs
Diffstat (limited to 'application/http')
-rw-r--r-- | application/http/HttpUtils.php | 73 | ||||
-rw-r--r-- | application/http/Url.php | 10 | ||||
-rw-r--r-- | application/http/UrlUtils.php | 11 |
3 files changed, 53 insertions, 41 deletions
diff --git a/application/http/HttpUtils.php b/application/http/HttpUtils.php index ed1002b0..4bde1d5b 100644 --- a/application/http/HttpUtils.php +++ b/application/http/HttpUtils.php | |||
@@ -48,7 +48,7 @@ function get_http_response( | |||
48 | $cleanUrl = $urlObj->idnToAscii(); | 48 | $cleanUrl = $urlObj->idnToAscii(); |
49 | 49 | ||
50 | if (!filter_var($cleanUrl, FILTER_VALIDATE_URL) || !$urlObj->isHttp()) { | 50 | if (!filter_var($cleanUrl, FILTER_VALIDATE_URL) || !$urlObj->isHttp()) { |
51 | return array(array(0 => 'Invalid HTTP UrlUtils'), false); | 51 | return [[0 => 'Invalid HTTP UrlUtils'], false]; |
52 | } | 52 | } |
53 | 53 | ||
54 | $userAgent = | 54 | $userAgent = |
@@ -71,7 +71,7 @@ function get_http_response( | |||
71 | 71 | ||
72 | $ch = curl_init($cleanUrl); | 72 | $ch = curl_init($cleanUrl); |
73 | if ($ch === false) { | 73 | if ($ch === false) { |
74 | return array(array(0 => 'curl_init() error'), false); | 74 | return [[0 => 'curl_init() error'], false]; |
75 | } | 75 | } |
76 | 76 | ||
77 | // General cURL settings | 77 | // General cURL settings |
@@ -82,7 +82,7 @@ function get_http_response( | |||
82 | curl_setopt( | 82 | curl_setopt( |
83 | $ch, | 83 | $ch, |
84 | CURLOPT_HTTPHEADER, | 84 | CURLOPT_HTTPHEADER, |
85 | array('Accept-Language: ' . $acceptLanguage) | 85 | ['Accept-Language: ' . $acceptLanguage] |
86 | ); | 86 | ); |
87 | curl_setopt($ch, CURLOPT_MAXREDIRS, $maxRedirs); | 87 | curl_setopt($ch, CURLOPT_MAXREDIRS, $maxRedirs); |
88 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | 88 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
@@ -90,7 +90,7 @@ function get_http_response( | |||
90 | curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); | 90 | curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); |
91 | 91 | ||
92 | // Max download size management | 92 | // Max download size management |
93 | curl_setopt($ch, CURLOPT_BUFFERSIZE, 1024*16); | 93 | curl_setopt($ch, CURLOPT_BUFFERSIZE, 1024 * 16); |
94 | curl_setopt($ch, CURLOPT_NOPROGRESS, false); | 94 | curl_setopt($ch, CURLOPT_NOPROGRESS, false); |
95 | if (is_callable($curlHeaderFunction)) { | 95 | if (is_callable($curlHeaderFunction)) { |
96 | curl_setopt($ch, CURLOPT_HEADERFUNCTION, $curlHeaderFunction); | 96 | curl_setopt($ch, CURLOPT_HEADERFUNCTION, $curlHeaderFunction); |
@@ -122,9 +122,9 @@ function get_http_response( | |||
122 | * Removing this would require updating | 122 | * Removing this would require updating |
123 | * GetHttpUrlTest::testGetInvalidRemoteUrl() | 123 | * GetHttpUrlTest::testGetInvalidRemoteUrl() |
124 | */ | 124 | */ |
125 | return array(false, false); | 125 | return [false, false]; |
126 | } | 126 | } |
127 | return array(array(0 => 'curl_exec() error: ' . $errorStr), false); | 127 | return [[0 => 'curl_exec() error: ' . $errorStr], false]; |
128 | } | 128 | } |
129 | 129 | ||
130 | // Formatting output like the fallback method | 130 | // Formatting output like the fallback method |
@@ -135,7 +135,7 @@ function get_http_response( | |||
135 | $rawHeadersLastRedir = end($rawHeadersArrayRedirs); | 135 | $rawHeadersLastRedir = end($rawHeadersArrayRedirs); |
136 | 136 | ||
137 | $content = substr($response, $headSize); | 137 | $content = substr($response, $headSize); |
138 | $headers = array(); | 138 | $headers = []; |
139 | foreach (preg_split('~[\r\n]+~', $rawHeadersLastRedir) as $line) { | 139 | foreach (preg_split('~[\r\n]+~', $rawHeadersLastRedir) as $line) { |
140 | if (empty($line) || ctype_space($line)) { | 140 | if (empty($line) || ctype_space($line)) { |
141 | continue; | 141 | continue; |
@@ -146,7 +146,7 @@ function get_http_response( | |||
146 | $value = $splitLine[1]; | 146 | $value = $splitLine[1]; |
147 | if (array_key_exists($key, $headers)) { | 147 | if (array_key_exists($key, $headers)) { |
148 | if (!is_array($headers[$key])) { | 148 | if (!is_array($headers[$key])) { |
149 | $headers[$key] = array(0 => $headers[$key]); | 149 | $headers[$key] = [0 => $headers[$key]]; |
150 | } | 150 | } |
151 | $headers[$key][] = $value; | 151 | $headers[$key][] = $value; |
152 | } else { | 152 | } else { |
@@ -157,7 +157,7 @@ function get_http_response( | |||
157 | } | 157 | } |
158 | } | 158 | } |
159 | 159 | ||
160 | return array($headers, $content); | 160 | return [$headers, $content]; |
161 | } | 161 | } |
162 | 162 | ||
163 | /** | 163 | /** |
@@ -188,15 +188,15 @@ function get_http_response_fallback( | |||
188 | $acceptLanguage, | 188 | $acceptLanguage, |
189 | $maxRedr | 189 | $maxRedr |
190 | ) { | 190 | ) { |
191 | $options = array( | 191 | $options = [ |
192 | 'http' => array( | 192 | 'http' => [ |
193 | 'method' => 'GET', | 193 | 'method' => 'GET', |
194 | 'timeout' => $timeout, | 194 | 'timeout' => $timeout, |
195 | 'user_agent' => $userAgent, | 195 | 'user_agent' => $userAgent, |
196 | 'header' => "Accept: */*\r\n" | 196 | 'header' => "Accept: */*\r\n" |
197 | . 'Accept-Language: ' . $acceptLanguage | 197 | . 'Accept-Language: ' . $acceptLanguage |
198 | ) | 198 | ] |
199 | ); | 199 | ]; |
200 | 200 | ||
201 | stream_context_set_default($options); | 201 | stream_context_set_default($options); |
202 | list($headers, $finalUrl) = get_redirected_headers($cleanUrl, $maxRedr); | 202 | list($headers, $finalUrl) = get_redirected_headers($cleanUrl, $maxRedr); |
@@ -207,7 +207,7 @@ function get_http_response_fallback( | |||
207 | } | 207 | } |
208 | 208 | ||
209 | if (! $headers) { | 209 | if (! $headers) { |
210 | return array($headers, false); | 210 | return [$headers, false]; |
211 | } | 211 | } |
212 | 212 | ||
213 | try { | 213 | try { |
@@ -215,10 +215,10 @@ function get_http_response_fallback( | |||
215 | $context = stream_context_create($options); | 215 | $context = stream_context_create($options); |
216 | $content = file_get_contents($finalUrl, false, $context, -1, $maxBytes); | 216 | $content = file_get_contents($finalUrl, false, $context, -1, $maxBytes); |
217 | } catch (Exception $exc) { | 217 | } catch (Exception $exc) { |
218 | return array(array(0 => 'HTTP Error'), $exc->getMessage()); | 218 | return [[0 => 'HTTP Error'], $exc->getMessage()]; |
219 | } | 219 | } |
220 | 220 | ||
221 | return array($headers, $content); | 221 | return [$headers, $content]; |
222 | } | 222 | } |
223 | 223 | ||
224 | /** | 224 | /** |
@@ -237,10 +237,12 @@ function get_redirected_headers($url, $redirectionLimit = 3) | |||
237 | } | 237 | } |
238 | 238 | ||
239 | // Headers found, redirection found, and limit not reached. | 239 | // Headers found, redirection found, and limit not reached. |
240 | if ($redirectionLimit-- > 0 | 240 | if ( |
241 | $redirectionLimit-- > 0 | ||
241 | && !empty($headers) | 242 | && !empty($headers) |
242 | && (strpos($headers[0], '301') !== false || strpos($headers[0], '302') !== false) | 243 | && (strpos($headers[0], '301') !== false || strpos($headers[0], '302') !== false) |
243 | && !empty($headers['Location'])) { | 244 | && !empty($headers['Location']) |
245 | ) { | ||
244 | $redirection = is_array($headers['Location']) ? end($headers['Location']) : $headers['Location']; | 246 | $redirection = is_array($headers['Location']) ? end($headers['Location']) : $headers['Location']; |
245 | if ($redirection != $url) { | 247 | if ($redirection != $url) { |
246 | $redirection = getAbsoluteUrl($url, $redirection); | 248 | $redirection = getAbsoluteUrl($url, $redirection); |
@@ -248,7 +250,7 @@ function get_redirected_headers($url, $redirectionLimit = 3) | |||
248 | } | 250 | } |
249 | } | 251 | } |
250 | 252 | ||
251 | return array($headers, $url); | 253 | return [$headers, $url]; |
252 | } | 254 | } |
253 | 255 | ||
254 | /** | 256 | /** |
@@ -270,7 +272,7 @@ function getAbsoluteUrl($originalUrl, $newUrl) | |||
270 | } | 272 | } |
271 | 273 | ||
272 | $parts = parse_url($originalUrl); | 274 | $parts = parse_url($originalUrl); |
273 | $final = $parts['scheme'] .'://'. $parts['host']; | 275 | $final = $parts['scheme'] . '://' . $parts['host']; |
274 | $final .= (!empty($parts['port'])) ? $parts['port'] : ''; | 276 | $final .= (!empty($parts['port'])) ? $parts['port'] : ''; |
275 | $final .= '/'; | 277 | $final .= '/'; |
276 | if ($newUrl[0] != '/') { | 278 | if ($newUrl[0] != '/') { |
@@ -323,7 +325,8 @@ function server_url($server) | |||
323 | $scheme = 'https'; | 325 | $scheme = 'https'; |
324 | } | 326 | } |
325 | 327 | ||
326 | if (($scheme == 'http' && $port != '80') | 328 | if ( |
329 | ($scheme == 'http' && $port != '80') | ||
327 | || ($scheme == 'https' && $port != '443') | 330 | || ($scheme == 'https' && $port != '443') |
328 | ) { | 331 | ) { |
329 | $port = ':' . $port; | 332 | $port = ':' . $port; |
@@ -344,22 +347,26 @@ function server_url($server) | |||
344 | $host = $server['SERVER_NAME']; | 347 | $host = $server['SERVER_NAME']; |
345 | } | 348 | } |
346 | 349 | ||
347 | return $scheme.'://'.$host.$port; | 350 | return $scheme . '://' . $host . $port; |
348 | } | 351 | } |
349 | 352 | ||
350 | // SSL detection | 353 | // SSL detection |
351 | if ((! empty($server['HTTPS']) && strtolower($server['HTTPS']) == 'on') | 354 | if ( |
352 | || (isset($server['SERVER_PORT']) && $server['SERVER_PORT'] == '443')) { | 355 | (! empty($server['HTTPS']) && strtolower($server['HTTPS']) == 'on') |
356 | || (isset($server['SERVER_PORT']) && $server['SERVER_PORT'] == '443') | ||
357 | ) { | ||
353 | $scheme = 'https'; | 358 | $scheme = 'https'; |
354 | } | 359 | } |
355 | 360 | ||
356 | // Do not append standard port values | 361 | // Do not append standard port values |
357 | if (($scheme == 'http' && $server['SERVER_PORT'] != '80') | 362 | if ( |
358 | || ($scheme == 'https' && $server['SERVER_PORT'] != '443')) { | 363 | ($scheme == 'http' && $server['SERVER_PORT'] != '80') |
359 | $port = ':'.$server['SERVER_PORT']; | 364 | || ($scheme == 'https' && $server['SERVER_PORT'] != '443') |
365 | ) { | ||
366 | $port = ':' . $server['SERVER_PORT']; | ||
360 | } | 367 | } |
361 | 368 | ||
362 | return $scheme.'://'.$server['SERVER_NAME'].$port; | 369 | return $scheme . '://' . $server['SERVER_NAME'] . $port; |
363 | } | 370 | } |
364 | 371 | ||
365 | /** | 372 | /** |
@@ -567,7 +574,10 @@ function get_curl_download_callback( | |||
567 | * | 574 | * |
568 | * @return int|bool length of $data or false if we need to stop the download | 575 | * @return int|bool length of $data or false if we need to stop the download |
569 | */ | 576 | */ |
570 | return function ($ch, $data) use ( | 577 | return function ( |
578 | $ch, | ||
579 | $data | ||
580 | ) use ( | ||
571 | $retrieveDescription, | 581 | $retrieveDescription, |
572 | $tagsSeparator, | 582 | $tagsSeparator, |
573 | &$charset, | 583 | &$charset, |
@@ -601,7 +611,7 @@ function get_curl_download_callback( | |||
601 | $foundChunk = $currentChunk; | 611 | $foundChunk = $currentChunk; |
602 | // Keywords use the format tag1, tag2 multiple words, tag | 612 | // Keywords use the format tag1, tag2 multiple words, tag |
603 | // So we split the result with `,`, then if a tag contains the separator we replace it by `-`. | 613 | // So we split the result with `,`, then if a tag contains the separator we replace it by `-`. |
604 | $keywords = tags_array2str(array_map(function(string $keyword) use ($tagsSeparator): string { | 614 | $keywords = tags_array2str(array_map(function (string $keyword) use ($tagsSeparator): string { |
605 | return tags_array2str(tags_str2array($keyword, $tagsSeparator), '-'); | 615 | return tags_array2str(tags_str2array($keyword, $tagsSeparator), '-'); |
606 | }, tags_str2array($keywords, ',')), $tagsSeparator); | 616 | }, tags_str2array($keywords, ',')), $tagsSeparator); |
607 | } | 617 | } |
@@ -611,7 +621,8 @@ function get_curl_download_callback( | |||
611 | // If we already found either the title, description or keywords, | 621 | // If we already found either the title, description or keywords, |
612 | // it's highly unlikely that we'll found the other metas further than | 622 | // it's highly unlikely that we'll found the other metas further than |
613 | // in the same chunk of data or the next one. So we also stop the download after that. | 623 | // in the same chunk of data or the next one. So we also stop the download after that. |
614 | if ((!empty($responseCode) && !empty($contentType) && !empty($charset)) && $foundChunk !== null | 624 | if ( |
625 | (!empty($responseCode) && !empty($contentType) && !empty($charset)) && $foundChunk !== null | ||
615 | && (! $retrieveDescription | 626 | && (! $retrieveDescription |
616 | || $foundChunk < $currentChunk | 627 | || $foundChunk < $currentChunk |
617 | || (!empty($title) && !empty($description) && !empty($keywords)) | 628 | || (!empty($title) && !empty($description) && !empty($keywords)) |
diff --git a/application/http/Url.php b/application/http/Url.php index 90444a2f..fe87088f 100644 --- a/application/http/Url.php +++ b/application/http/Url.php | |||
@@ -17,7 +17,7 @@ namespace Shaarli\Http; | |||
17 | */ | 17 | */ |
18 | class Url | 18 | class Url |
19 | { | 19 | { |
20 | private static $annoyingQueryParams = array( | 20 | private static $annoyingQueryParams = [ |
21 | 21 | ||
22 | 'action_object_map=', | 22 | 'action_object_map=', |
23 | 'action_ref_map=', | 23 | 'action_ref_map=', |
@@ -37,15 +37,15 @@ class Url | |||
37 | 37 | ||
38 | // Other | 38 | // Other |
39 | 'campaign_' | 39 | 'campaign_' |
40 | ); | 40 | ]; |
41 | 41 | ||
42 | private static $annoyingFragments = array( | 42 | private static $annoyingFragments = [ |
43 | // ATInternet | 43 | // ATInternet |
44 | 'xtor=RSS-', | 44 | 'xtor=RSS-', |
45 | 45 | ||
46 | // Misc. | 46 | // Misc. |
47 | 'tk.rss_all' | 47 | 'tk.rss_all' |
48 | ); | 48 | ]; |
49 | 49 | ||
50 | /* | 50 | /* |
51 | * URL parts represented as an array | 51 | * URL parts represented as an array |
@@ -120,7 +120,7 @@ class Url | |||
120 | foreach (self::$annoyingQueryParams as $annoying) { | 120 | foreach (self::$annoyingQueryParams as $annoying) { |
121 | foreach ($queryParams as $param) { | 121 | foreach ($queryParams as $param) { |
122 | if (startsWith($param, $annoying)) { | 122 | if (startsWith($param, $annoying)) { |
123 | $queryParams = array_diff($queryParams, array($param)); | 123 | $queryParams = array_diff($queryParams, [$param]); |
124 | continue; | 124 | continue; |
125 | } | 125 | } |
126 | } | 126 | } |
diff --git a/application/http/UrlUtils.php b/application/http/UrlUtils.php index e8d1a283..de5b7db1 100644 --- a/application/http/UrlUtils.php +++ b/application/http/UrlUtils.php | |||
@@ -1,4 +1,5 @@ | |||
1 | <?php | 1 | <?php |
2 | |||
2 | /** | 3 | /** |
3 | * Converts an array-represented URL to a string | 4 | * Converts an array-represented URL to a string |
4 | * | 5 | * |
@@ -12,15 +13,15 @@ | |||
12 | */ | 13 | */ |
13 | function unparse_url($parsedUrl) | 14 | function unparse_url($parsedUrl) |
14 | { | 15 | { |
15 | $scheme = isset($parsedUrl['scheme']) ? $parsedUrl['scheme'].'://' : ''; | 16 | $scheme = isset($parsedUrl['scheme']) ? $parsedUrl['scheme'] . '://' : ''; |
16 | $host = isset($parsedUrl['host']) ? $parsedUrl['host'] : ''; | 17 | $host = isset($parsedUrl['host']) ? $parsedUrl['host'] : ''; |
17 | $port = isset($parsedUrl['port']) ? ':'.$parsedUrl['port'] : ''; | 18 | $port = isset($parsedUrl['port']) ? ':' . $parsedUrl['port'] : ''; |
18 | $user = isset($parsedUrl['user']) ? $parsedUrl['user'] : ''; | 19 | $user = isset($parsedUrl['user']) ? $parsedUrl['user'] : ''; |
19 | $pass = isset($parsedUrl['pass']) ? ':'.$parsedUrl['pass'] : ''; | 20 | $pass = isset($parsedUrl['pass']) ? ':' . $parsedUrl['pass'] : ''; |
20 | $pass = ($user || $pass) ? "$pass@" : ''; | 21 | $pass = ($user || $pass) ? "$pass@" : ''; |
21 | $path = isset($parsedUrl['path']) ? $parsedUrl['path'] : ''; | 22 | $path = isset($parsedUrl['path']) ? $parsedUrl['path'] : ''; |
22 | $query = isset($parsedUrl['query']) ? '?'.$parsedUrl['query'] : ''; | 23 | $query = isset($parsedUrl['query']) ? '?' . $parsedUrl['query'] : ''; |
23 | $fragment = isset($parsedUrl['fragment']) ? '#'.$parsedUrl['fragment'] : ''; | 24 | $fragment = isset($parsedUrl['fragment']) ? '#' . $parsedUrl['fragment'] : ''; |
24 | 25 | ||
25 | return "$scheme$user$pass$host$port$path$query$fragment"; | 26 | return "$scheme$user$pass$host$port$path$query$fragment"; |
26 | } | 27 | } |