diff options
Diffstat (limited to 'include/network.php')
-rw-r--r-- | include/network.php | 159 |
1 files changed, 76 insertions, 83 deletions
diff --git a/include/network.php b/include/network.php index b3a3d715c..83bb281a4 100644 --- a/include/network.php +++ b/include/network.php @@ -2,6 +2,7 @@ use Zotlabs\Lib\Activity; use Zotlabs\Lib\Config; +use Zotlabs\Lib\Mailer; use Zotlabs\Lib\Zotfinger; use Zotlabs\Lib\Libzot; use Zotlabs\Lib\Queue; @@ -72,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, @@ -96,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(); } @@ -105,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 { @@ -119,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 { @@ -127,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']); } @@ -135,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)) { @@ -204,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; } @@ -432,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)); @@ -612,7 +612,7 @@ function validate_email(string $addr): bool { $matches = array(); $result = preg_match( - '/^[A-Z0-9._%-]+@([A-Z0-9.-]+\.[A-Z0-9-]{2,})$/i', + '/^[A-Z0-9._%+-]+@([A-Z0-9.-]+\.[A-Z0-9-]{2,})$/i', punify($addr), $matches); @@ -1488,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) { @@ -1813,54 +1813,9 @@ function network_to_name($s) { */ function z_mail($params) { - if(! $params['fromEmail']) { - $params['fromEmail'] = Config::Get('system','from_email'); - if(! $params['fromEmail']) - $params['fromEmail'] = 'Administrator' . '@' . App::get_hostname(); - } - if(! $params['fromName']) { - $params['fromName'] = Config::Get('system','from_email_name'); - if(! $params['fromName']) - $params['fromName'] = Zotlabs\Lib\System::get_site_name(); - } - if(! $params['replyTo']) { - $params['replyTo'] = Config::Get('system','reply_address'); - if(! $params['replyTo']) - $params['replyTo'] = 'noreply' . '@' . App::get_hostname(); - } - - $params['sent'] = false; - $params['result'] = false; - - /** - * @hooks email_send - * * \e params @see z_mail() - */ - call_hooks('email_send', $params); - - if($params['sent']) { - logger('notification: z_mail returns ' . (($params['result']) ? 'success' : 'failure'), LOGGER_DEBUG); - return $params['result']; - } - - $fromName = email_header_encode(html_entity_decode($params['fromName'],ENT_QUOTES,'UTF-8'),'UTF-8'); - $messageSubject = email_header_encode(html_entity_decode($params['messageSubject'],ENT_QUOTES,'UTF-8'),'UTF-8'); - - $messageHeader = - $params['additionalMailHeader'] . - "From: $fromName <{$params['fromEmail']}>" . PHP_EOL . - "Reply-To: $fromName <{$params['replyTo']}>" . PHP_EOL . - "Content-Type: text/plain; charset=UTF-8"; - - // send the message - $res = mail( - $params['toEmail'], // send to address - $messageSubject, // subject - $params['textVersion'], - $messageHeader // message headers - ); - logger('notification: z_mail returns ' . (($res) ? 'success' : 'failure'), LOGGER_DEBUG); - return $res; + // Delegate the call to the Mailer class. + $mailer = new Mailer($params); + return $mailer->deliver(); } @@ -2145,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); } |