diff options
Diffstat (limited to 'include/network.php')
-rw-r--r-- | include/network.php | 105 |
1 files changed, 71 insertions, 34 deletions
diff --git a/include/network.php b/include/network.php index a8ccee15c..83bb281a4 100644 --- a/include/network.php +++ b/include/network.php @@ -73,21 +73,21 @@ function z_fetch_url($url, $binary = false, $redirects = 0, $opts = array()) { if($ciphers) @curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, $ciphers); - if(x($opts,'filep')) { + if(!empty($opts['filep'])) { @curl_setopt($ch, CURLOPT_FILE, $opts['filep']); @curl_setopt($ch, CURLOPT_HEADER, false); } - if(x($opts,'upload')) + if(!empty($opts['upload'])) @curl_setopt($ch, CURLOPT_UPLOAD, $opts['upload']); - if(x($opts,'infile')) + if(!empty($opts['infile'])) @curl_setopt($ch, CURLOPT_INFILE, $opts['infile']); - if(x($opts,'infilesize')) + if(!empty($opts['infilesize'])) @curl_setopt($ch, CURLOPT_INFILESIZE, $opts['infilesize']); - if(x($opts,'readfunc')) + if(!empty($opts['readfunc'])) @curl_setopt($ch, CURLOPT_READFUNCTION, $opts['readfunc']); // When using the session option and fetching from our own site, @@ -97,7 +97,7 @@ function z_fetch_url($url, $binary = false, $redirects = 0, $opts = array()) { $instance_headers = ((array_key_exists('headers',$opts) && is_array($opts['headers'])) ? $opts['headers'] : []); - if(x($opts,'session')) { + if(!empty($opts['session'])) { if(strpos($url,z_root()) === 0) { $instance_headers[] = 'Cookie: PHPSESSID=' . session_id(); } @@ -106,13 +106,13 @@ function z_fetch_url($url, $binary = false, $redirects = 0, $opts = array()) { @curl_setopt($ch, CURLOPT_HTTPHEADER, $instance_headers); - if(x($opts,'nobody')) + if(!empty($opts['nobody'])) @curl_setopt($ch, CURLOPT_NOBODY, $opts['nobody']); - if(x($opts,'custom')) + if(!empty($opts['custom'])) @curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $opts['custom']); - if(x($opts,'timeout') && intval($opts['timeout'])) { + if(!empty($opts['timeout'])) { @curl_setopt($ch, CURLOPT_TIMEOUT, intval($opts['timeout'])); } else { @@ -120,7 +120,7 @@ function z_fetch_url($url, $binary = false, $redirects = 0, $opts = array()) { @curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== 0) ? $curl_time : 60)); } - if(x($opts,'connecttimeout') && intval($opts['connecttimeout'])) { + if(!empty($opts['connecttimeout'])) { @curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, intval($opts['connecttimeout'])); } else { @@ -128,7 +128,7 @@ function z_fetch_url($url, $binary = false, $redirects = 0, $opts = array()) { @curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (($curl_contime !== 0) ? $curl_contime : 30)); } - if(x($opts,'http_auth')) { + if(!empty($opts['http_auth'])) { // "username" . ':' . "password" @curl_setopt($ch, CURLOPT_USERPWD, $opts['http_auth']); } @@ -136,16 +136,16 @@ function z_fetch_url($url, $binary = false, $redirects = 0, $opts = array()) { if(array_key_exists('http_version',$opts)) @curl_setopt($ch,CURLOPT_HTTP_VERSION,$opts['http_version']); - if(x($opts,'cookiejar')) + if(!empty($opts['cookiejar'])) @curl_setopt($ch, CURLOPT_COOKIEJAR, $opts['cookiejar']); - if(x($opts,'cookiefile')) + if(!empty($opts['cookiefile'])) @curl_setopt($ch, CURLOPT_COOKIEFILE, $opts['cookiefile']); - if(x($opts,'cookie')) + if(!empty($opts['cookie'])) @curl_setopt($ch, CURLOPT_COOKIE, $opts['cookie']); @curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, - ((x($opts,'novalidate') && intval($opts['novalidate'])) ? false : true)); + ((!empty($opts['novalidate'])) ? false : true)); $prx = @Config::Get('system','proxy'); if(strlen($prx)) { @@ -205,7 +205,7 @@ function z_fetch_url($url, $binary = false, $redirects = 0, $opts = array()) { $ret['header'] = $header; $ret['request_target'] = $opts['request_target']; - if(x($opts,'debug')) { + if(!empty($opts['debug'])) { $ret['debug'] = $curl_info; } @@ -433,7 +433,6 @@ function as_return_and_die($obj, $channel = []) { $headers['Content-Type'] = 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' ; $headers['Date'] = datetime_convert('UTC','UTC', 'now', 'D, d M Y H:i:s \\G\\M\\T'); $headers['Digest'] = HTTPSig::generate_digest_header($ret); - $headers['(request-target)'] = strtolower($_SERVER['REQUEST_METHOD']) . ' ' . $_SERVER['REQUEST_URI']; if ($channel) { $h = HTTPSig::create_sig($headers, $channel['channel_prvkey'], channel_url($channel)); @@ -1489,11 +1488,11 @@ function do_delivery($deliveries, $force = false) { $interval = Config::Get('queueworker', 'queue_interval', 500000); -// $deliveries_per_process = intval(Config::Get('system','delivery_batch_count')); + $deliveries_per_process = intval(Config::Get('system', 'delivery_batch_count')); - if($deliveries_per_process <= 0) + if($deliveries_per_process <= 0) { $deliveries_per_process = 1; - + } $deliver = []; foreach($deliveries as $d) { @@ -2101,21 +2100,59 @@ function get_request_string($url) { /** - * Builds a url from the result of `parse_url`. + * Reconstructs a URL from its parsed components. * - * @param array $parsed_url An associative array as produced by `parse_url`. + * This function takes a parsed URL as an associative array and reconstructs + * the URL based on the specified components (scheme, host, port, user, pass, path, query, fragment). + * You can specify which components should be included in the final URL by passing the optional + * `$parts` array. The function will return the complete URL string formed by combining + * only the parts that exist in both the parsed URL and the `$parts` array. * - * @return string The reassembled URL as a string. + * @param array $parsed_url The parsed URL components as an associative array. + * The array can include keys like 'scheme', 'host', 'port', 'user', 'pass', + * 'path', 'query', 'fragment'. + * + * @param array $parts An optional array that specifies which components of the URL + * should be included in the final string. Defaults to: + * ['scheme', 'host', 'port', 'user', 'pass', 'path', 'query', 'fragment']. + * If any of the components are not required, they can be omitted from the array. + * + * @return string The reconstructed URL as a string. */ -function unparse_url(array $parsed_url): string { - $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; - $host = isset($parsed_url['host']) ? $parsed_url['host'] : ''; - $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; - $user = isset($parsed_url['user']) ? $parsed_url['user'] : ''; - $pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : ''; - $pass = ($user || $pass) ? "$pass@" : ''; - $path = isset($parsed_url['path']) ? $parsed_url['path'] : ''; - $query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; - $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; - return $scheme . $user . $pass . $host . $port . $path . $query . $fragment; +function unparse_url(array $parsed_url, array $parts = ['scheme', 'host', 'port', 'user', 'pass', 'path', 'query', 'fragment']): string { + $url_parts = []; + + if (in_array('scheme', $parts) && array_key_exists('scheme', $parsed_url)) { + $url_parts[] = $parsed_url['scheme'] . '://'; + } + + if (in_array('user', $parts) && array_key_exists('user', $parsed_url)) { + $url_parts[] = $parsed_url['user']; + if (in_array('pass', $parts) && array_key_exists('pass', $parsed_url)) { + $url_parts[] = ':' . $parsed_url['pass']; + } + $url_parts[] = '@'; + } + + if (in_array('host', $parts) && array_key_exists('host', $parsed_url)) { + $url_parts[] = $parsed_url['host']; + } + + if (in_array('port', $parts) && array_key_exists('port', $parsed_url)) { + $url_parts[] = ':' . $parsed_url['port']; + } + + if (in_array('path', $parts) && array_key_exists('path', $parsed_url)) { + $url_parts[] = $parsed_url['path']; + } + + if (in_array('query', $parts) && array_key_exists('query', $parsed_url)) { + $url_parts[] = '?' . $parsed_url['query']; + } + + if (in_array('fragment', $parts) && array_key_exists('fragment', $parsed_url)) { + $url_parts[] = '#' . $parsed_url['fragment']; + } + + return implode('', $url_parts); } |