aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/http/HttpUtils.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-09-22 20:25:47 +0200
committerArthurHoaro <arthur@hoa.ro>2020-11-09 10:56:24 +0100
commit53054b2bf6a919fd4ff9b44b6ad1986f21f488b6 (patch)
tree39cad52645ce00fbf863ff8e482d10dfcbe7f26c /application/http/HttpUtils.php
parente09bb93e18a333eff8e6a4156f5b58ba9c7d25cd (diff)
downloadShaarli-53054b2bf6a919fd4ff9b44b6ad1986f21f488b6.tar.gz
Shaarli-53054b2bf6a919fd4ff9b44b6ad1986f21f488b6.tar.zst
Shaarli-53054b2bf6a919fd4ff9b44b6ad1986f21f488b6.zip
Apply PHP Code Beautifier on source code for linter automatic fixes
Diffstat (limited to 'application/http/HttpUtils.php')
-rw-r--r--application/http/HttpUtils.php73
1 files changed, 42 insertions, 31 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))