3 * @file src/Util/Network.php
5 namespace Friendica\Util;
7 use Friendica\Core\Addon;
8 use Friendica\Core\System;
9 use Friendica\Core\Config;
10 use Friendica\Network\Curl;
17 * @var Curl The latest Curl output
22 * Returns the latest Curl output
24 * @return Curl The latest Curl output
26 public static function getCurl()
28 if (empty(self::$curl)) {
29 self::$curl = new Curl();
38 * If binary flag is true, return binary results.
39 * Set the cookiejar argument to a string (e.g. "/tmp/friendica-cookies.txt")
40 * to preserve cookies from one request to the next.
43 * @param string $url URL to fetch
44 * @param boolean $binary default false
45 * TRUE if asked to return binary results (file download)
46 * @param integer $redirects The recursion counter for internal use - default 0
47 * @param integer $timeout Timeout in seconds, default system config value or 60 seconds
48 * @param string $accept_content supply Accept: header with 'accept_content' as the value
49 * @param string $cookiejar Path to cookie jar file
51 * @return string The fetched content
53 public static function fetchUrl($url, $binary = false, &$redirects = 0, $timeout = 0, $accept_content = null, $cookiejar = '')
55 $ret = self::fetchUrlFull($url, $binary, $redirects, $timeout, $accept_content, $cookiejar);
61 * Curl wrapper with array of return values.
63 * Inner workings and parameters are the same as @ref fetchUrl but returns an array with
64 * all the information collected during the fetch.
66 * @brief Curl wrapper with array of return values.
67 * @param string $url URL to fetch
68 * @param boolean $binary default false
69 * TRUE if asked to return binary results (file download)
70 * @param integer $redirects The recursion counter for internal use - default 0
71 * @param integer $timeout Timeout in seconds, default system config value or 60 seconds
72 * @param string $accept_content supply Accept: header with 'accept_content' as the value
73 * @param string $cookiejar Path to cookie jar file
75 * @return array With all relevant information, 'body' contains the actual fetched content.
77 public static function fetchUrlFull($url, $binary = false, &$redirects = 0, $timeout = 0, $accept_content = null, $cookiejar = '')
84 'accept_content'=>$accept_content,
85 'cookiejar'=>$cookiejar
91 * @brief fetches an URL.
93 * @param string $url URL to fetch
94 * @param boolean $binary default false
95 * TRUE if asked to return binary results (file download)
96 * @param int $redirects The recursion counter for internal use - default 0
97 * @param array $opts (optional parameters) assoziative array with:
98 * 'accept_content' => supply Accept: header with 'accept_content' as the value
99 * 'timeout' => int Timeout in seconds, default system config value or 60 seconds
100 * 'http_auth' => username:password
101 * 'novalidate' => do not validate SSL certs, default is to validate using our CA list
102 * 'nobody' => only return the header
103 * 'cookiejar' => path to cookie jar file
105 * @return array an assoziative array with:
106 * int 'return_code' => HTTP return code or 0 if timeout or failure
107 * boolean 'success' => boolean true (if HTTP 2xx result) or false
108 * string 'redirect_url' => in case of redirect, content was finally retrieved from this URL
109 * string 'header' => HTTP headers
110 * string 'body' => fetched content
112 public static function curl($url, $binary = false, &$redirects = 0, $opts = [])
114 $ret = ['return_code' => 0, 'success' => false, 'header' => '', 'info' => '', 'body' => ''];
116 $stamp1 = microtime(true);
120 $parts = parse_url($url);
121 $path_parts = explode('/', defaults($parts, 'path', ''));
122 foreach ($path_parts as $part) {
123 if (strlen($part) <> mb_strlen($part)) {
124 $parts2[] = rawurlencode($part);
129 $parts['path'] = implode('/', $parts2);
130 $url = self::unparseURL($parts);
132 if (self::isUrlBlocked($url)) {
133 logger('domain of ' . $url . ' is blocked', LOGGER_DATA);
137 $ch = @curl_init($url);
139 if (($redirects > 8) || (!$ch)) {
143 @curl_setopt($ch, CURLOPT_HEADER, true);
145 if (x($opts, "cookiejar")) {
146 curl_setopt($ch, CURLOPT_COOKIEJAR, $opts["cookiejar"]);
147 curl_setopt($ch, CURLOPT_COOKIEFILE, $opts["cookiejar"]);
150 // These settings aren't needed. We're following the location already.
151 // @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
152 // @curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
154 if (x($opts, 'accept_content')) {
158 ['Accept: ' . $opts['accept_content']]
162 @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
163 @curl_setopt($ch, CURLOPT_USERAGENT, $a->getUserAgent());
165 $range = intval(Config::get('system', 'curl_range_bytes', 0));
168 @curl_setopt($ch, CURLOPT_RANGE, '0-' . $range);
171 // Without this setting it seems as if some webservers send compressed content
172 // This seems to confuse curl so that it shows this uncompressed.
173 /// @todo We could possibly set this value to "gzip" or something similar
174 curl_setopt($ch, CURLOPT_ENCODING, '');
176 if (x($opts, 'headers')) {
177 @curl_setopt($ch, CURLOPT_HTTPHEADER, $opts['headers']);
180 if (x($opts, 'nobody')) {
181 @curl_setopt($ch, CURLOPT_NOBODY, $opts['nobody']);
184 if (x($opts, 'timeout')) {
185 @curl_setopt($ch, CURLOPT_TIMEOUT, $opts['timeout']);
187 $curl_time = Config::get('system', 'curl_timeout', 60);
188 @curl_setopt($ch, CURLOPT_TIMEOUT, intval($curl_time));
191 // by default we will allow self-signed certs
192 // but you can override this
194 $check_cert = Config::get('system', 'verifyssl');
195 @curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
198 @curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
201 $proxy = Config::get('system', 'proxy');
203 if (strlen($proxy)) {
204 @curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
205 @curl_setopt($ch, CURLOPT_PROXY, $proxy);
206 $proxyuser = @Config::get('system', 'proxyuser');
208 if (strlen($proxyuser)) {
209 @curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyuser);
213 if (Config::get('system', 'ipv4_resolve', false)) {
214 curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
218 @curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
221 // don't let curl abort the entire application
222 // if it throws any errors.
224 $s = @curl_exec($ch);
225 $curl_info = @curl_getinfo($ch);
227 // Special treatment for HTTP Code 416
228 // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/416
229 if (($curl_info['http_code'] == 416) && ($range > 0)) {
230 @curl_setopt($ch, CURLOPT_RANGE, '');
231 $s = @curl_exec($ch);
232 $curl_info = @curl_getinfo($ch);
235 if (curl_errno($ch) !== CURLE_OK) {
236 logger('error fetching ' . $url . ': ' . curl_error($ch), LOGGER_INFO);
239 $ret['errno'] = curl_errno($ch);
242 $ret['info'] = $curl_info;
244 $http_code = $curl_info['http_code'];
246 logger($url . ': ' . $http_code . " " . $s, LOGGER_DATA);
249 // Pull out multiple headers, e.g. proxy and continuation headers
250 // allow for HTTP/2.x without fixing code
252 while (preg_match('/^HTTP\/[1-2].+? [1-5][0-9][0-9]/', $base)) {
253 $chunk = substr($base, 0, strpos($base, "\r\n\r\n") + 4);
255 $base = substr($base, strlen($chunk));
258 self::$curl = new Curl($http_code, (isset($curl_info['content_type']) ? $curl_info['content_type'] : ''), $header);
260 if ($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) {
261 $new_location_info = @parse_url($curl_info['redirect_url']);
262 $old_location_info = @parse_url($curl_info['url']);
264 $newurl = $curl_info['redirect_url'];
266 if (empty($new_location_info['path']) && !empty($new_location_info['host'])) {
267 $newurl = $new_location_info['scheme'] . '://' . $new_location_info['host'] . $old_location_info['path'];
272 if (preg_match('/(Location:|URI:)(.*?)\n/i', $header, $matches)) {
273 $newurl = trim(array_pop($matches));
275 if (strpos($newurl, '/') === 0) {
276 $newurl = $old_location_info["scheme"]."://".$old_location_info["host"].$newurl;
278 $old_location_query = @parse_url($url, PHP_URL_QUERY);
280 if ($old_location_query != '') {
281 $newurl .= '?' . $old_location_query;
284 if (filter_var($newurl, FILTER_VALIDATE_URL)) {
287 return self::curl($newurl, $binary, $redirects, $opts);
291 self::$curl->setCode($http_code);
292 if (isset($curl_info['content_type'])) {
293 self::$curl->setContentType($curl_info['content_type']);
296 $rc = intval($http_code);
297 $ret['return_code'] = $rc;
298 $ret['success'] = (($rc >= 200 && $rc <= 299) ? true : false);
299 $ret['redirect_url'] = $url;
301 if (!$ret['success']) {
302 $ret['error'] = curl_error($ch);
303 $ret['debug'] = $curl_info;
304 logger('error: '.$url.': '.$ret['return_code'].' - '.$ret['error'], LOGGER_DEBUG);
305 logger('debug: '.print_r($curl_info, true), LOGGER_DATA);
308 $ret['body'] = substr($s, strlen($header));
309 $ret['header'] = $header;
311 if (x($opts, 'debug')) {
312 $ret['debug'] = $curl_info;
317 $a->saveTimestamp($stamp1, 'network');
323 * @brief Send POST request to $url
325 * @param string $url URL to post
326 * @param mixed $params array of POST variables
327 * @param string $headers HTTP headers
328 * @param integer $redirects Recursion counter for internal use - default = 0
329 * @param integer $timeout The timeout in seconds, default system config value or 60 seconds
331 * @return string The content
333 public static function post($url, $params, $headers = null, &$redirects = 0, $timeout = 0)
335 $stamp1 = microtime(true);
337 if (self::isUrlBlocked($url)) {
338 logger('post_url: domain of ' . $url . ' is blocked', LOGGER_DATA);
343 $ch = curl_init($url);
345 if (($redirects > 8) || (!$ch)) {
349 logger('post_url: start ' . $url, LOGGER_DATA);
351 curl_setopt($ch, CURLOPT_HEADER, true);
352 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
353 curl_setopt($ch, CURLOPT_POST, 1);
354 curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
355 curl_setopt($ch, CURLOPT_USERAGENT, $a->getUserAgent());
357 if (Config::get('system', 'ipv4_resolve', false)) {
358 curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
361 if (intval($timeout)) {
362 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
364 $curl_time = Config::get('system', 'curl_timeout', 60);
365 curl_setopt($ch, CURLOPT_TIMEOUT, intval($curl_time));
368 if (defined('LIGHTTPD')) {
369 if (!is_array($headers)) {
370 $headers = ['Expect:'];
372 if (!in_array('Expect:', $headers)) {
373 array_push($headers, 'Expect:');
379 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
382 $check_cert = Config::get('system', 'verifyssl');
383 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
386 @curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
389 $proxy = Config::get('system', 'proxy');
391 if (strlen($proxy)) {
392 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
393 curl_setopt($ch, CURLOPT_PROXY, $proxy);
394 $proxyuser = Config::get('system', 'proxyuser');
395 if (strlen($proxyuser)) {
396 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyuser);
400 self::getCurl()->setCode(0);
402 // don't let curl abort the entire application
403 // if it throws any errors.
405 $s = @curl_exec($ch);
408 $curl_info = curl_getinfo($ch);
409 $http_code = $curl_info['http_code'];
411 logger('post_url: result ' . $http_code . ' - ' . $url, LOGGER_DATA);
415 // Pull out multiple headers, e.g. proxy and continuation headers
416 // allow for HTTP/2.x without fixing code
418 while (preg_match('/^HTTP\/[1-2].+? [1-5][0-9][0-9]/', $base)) {
419 $chunk = substr($base, 0, strpos($base, "\r\n\r\n") + 4);
421 $base = substr($base, strlen($chunk));
424 if ($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) {
426 $new_location_info = @parse_url($curl_info['redirect_url']);
427 $old_location_info = @parse_url($curl_info['url']);
429 preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
430 $newurl = trim(array_pop($matches));
432 if (strpos($newurl, '/') === 0) {
433 $newurl = $old_location_info["scheme"] . "://" . $old_location_info["host"] . $newurl;
436 if (filter_var($newurl, FILTER_VALIDATE_URL)) {
438 logger('post_url: redirect ' . $url . ' to ' . $newurl);
439 return self::post($newurl, $params, $headers, $redirects, $timeout);
443 self::getCurl()->setCode($http_code);
445 $body = substr($s, strlen($header));
447 self::getCurl()->setHeaders($header);
451 $a->saveTimestamp($stamp1, 'network');
453 logger('post_url: end ' . $url, LOGGER_DATA);
459 * @brief Check URL to see if it's real
461 * Take a URL from the wild, prepend http:// if necessary
462 * and check DNS to see if it's real (or check if is a valid IP address)
464 * @param string $url The URL to be validated
465 * @return string|boolean The actual working URL, false else
467 public static function isUrlValid($url)
469 if (Config::get('system', 'disable_url_validation')) {
473 // no naked subdomains (allow localhost for tests)
474 if (strpos($url, '.') === false && strpos($url, '/localhost/') === false) {
478 if (substr($url, 0, 4) != 'http') {
479 $url = 'http://' . $url;
482 /// @TODO Really suppress function outcomes? Why not find them + debug them?
483 $h = @parse_url($url);
485 if ((is_array($h)) && (@dns_get_record($h['host'], DNS_A + DNS_CNAME) || filter_var($h['host'], FILTER_VALIDATE_IP) )) {
493 * @brief Checks that email is an actual resolvable internet address
495 * @param string $addr The email address
496 * @return boolean True if it's a valid email address, false if it's not
498 public static function isEmailDomainValid($addr)
500 if (Config::get('system', 'disable_email_validation')) {
504 if (! strpos($addr, '@')) {
508 $h = substr($addr, strpos($addr, '@') + 1);
510 // Concerning the @ see here: https://stackoverflow.com/questions/36280957/dns-get-record-a-temporary-server-error-occurred
511 if ($h && (@dns_get_record($h, DNS_A + DNS_MX) || filter_var($h, FILTER_VALIDATE_IP) )) {
514 if ($h && @dns_get_record($h, DNS_CNAME + DNS_MX)) {
521 * @brief Check if URL is allowed
523 * Check $url against our list of allowed sites,
524 * wildcards allowed. If allowed_sites is unset return true;
526 * @param string $url URL which get tested
527 * @return boolean True if url is allowed otherwise return false
529 public static function isUrlAllowed($url)
531 $h = @parse_url($url);
537 $str_allowed = Config::get('system', 'allowed_sites');
538 if (! $str_allowed) {
544 $host = strtolower($h['host']);
546 // always allow our own site
547 if ($host == strtolower($_SERVER['SERVER_NAME'])) {
551 $fnmatch = function_exists('fnmatch');
552 $allowed = explode(',', $str_allowed);
554 if (count($allowed)) {
555 foreach ($allowed as $a) {
556 $pat = strtolower(trim($a));
557 if (($fnmatch && fnmatch($pat, $host)) || ($pat == $host)) {
567 * Checks if the provided url domain is on the domain blocklist.
568 * Returns true if it is or malformed URL, false if not.
570 * @param string $url The url to check the domain from
574 public static function isUrlBlocked($url)
576 $host = @parse_url($url, PHP_URL_HOST);
581 $domain_blocklist = Config::get('system', 'blocklist', []);
582 if (!$domain_blocklist) {
586 foreach ($domain_blocklist as $domain_block) {
587 if (strcasecmp($domain_block['domain'], $host) === 0) {
596 * @brief Check if email address is allowed to register here.
598 * Compare against our list (wildcards allowed).
600 * @param string $email email address
601 * @return boolean False if not allowed, true if allowed
602 * or if allowed list is not configured
604 public static function isEmailDomainAllowed($email)
606 $domain = strtolower(substr($email, strpos($email, '@') + 1));
611 $str_allowed = Config::get('system', 'allowed_email', '');
612 if (!x($str_allowed)) {
616 $allowed = explode(',', $str_allowed);
618 return self::isDomainAllowed($domain, $allowed);
622 * Checks for the existence of a domain in a domain list
624 * @brief Checks for the existence of a domain in a domain list
625 * @param string $domain
626 * @param array $domain_list
629 public static function isDomainAllowed($domain, array $domain_list)
633 foreach ($domain_list as $item) {
634 $pat = strtolower(trim($item));
635 if (fnmatch($pat, $domain) || ($pat == $domain)) {
644 public static function lookupAvatarByEmail($email)
646 $avatar['size'] = 175;
647 $avatar['email'] = $email;
649 $avatar['success'] = false;
651 Addon::callHooks('avatar_lookup', $avatar);
653 if (! $avatar['success']) {
654 $avatar['url'] = System::baseUrl() . '/images/person-175.jpg';
657 logger('Avatar: ' . $avatar['email'] . ' ' . $avatar['url'], LOGGER_DEBUG);
658 return $avatar['url'];
662 * @brief Remove Google Analytics and other tracking platforms params from URL
664 * @param string $url Any user-submitted URL that may contain tracking params
665 * @return string The same URL stripped of tracking parameters
667 public static function stripTrackingQueryParams($url)
669 $urldata = parse_url($url);
670 if (!empty($urldata["query"])) {
671 $query = $urldata["query"];
672 parse_str($query, $querydata);
674 if (is_array($querydata)) {
675 foreach ($querydata as $param => $value) {
679 "utm_source", "utm_medium", "utm_term", "utm_content", "utm_campaign",
680 "wt_mc", "pk_campaign", "pk_kwd", "mc_cid", "mc_eid",
681 "fb_action_ids", "fb_action_types", "fb_ref",
683 "woo_campaign", "woo_source", "woo_medium", "woo_content", "woo_term"]
686 $pair = $param . "=" . urlencode($value);
687 $url = str_replace($pair, "", $url);
689 // Second try: if the url isn't encoded completely
690 $pair = $param . "=" . str_replace(" ", "+", $value);
691 $url = str_replace($pair, "", $url);
693 // Third try: Maybey the url isn't encoded at all
694 $pair = $param . "=" . $value;
695 $url = str_replace($pair, "", $url);
697 $url = str_replace(["?&", "&&"], ["?", ""], $url);
702 if (substr($url, -1, 1) == "?") {
703 $url = substr($url, 0, -1);
711 * @brief Returns the original URL of the provided URL
713 * This function strips tracking query params and follows redirections, either
714 * through HTTP code or meta refresh tags. Stops after 10 redirections.
716 * @todo Remove the $fetchbody parameter that generates an extraneous HEAD request
718 * @see ParseUrl::getSiteinfo
720 * @param string $url A user-submitted URL
721 * @param int $depth The current redirection recursion level (internal)
722 * @param bool $fetchbody Wether to fetch the body or not after the HEAD requests
723 * @return string A canonical URL
725 public static function finalUrl($url, $depth = 1, $fetchbody = false)
729 $url = self::stripTrackingQueryParams($url);
735 $url = trim($url, "'");
737 $stamp1 = microtime(true);
740 curl_setopt($ch, CURLOPT_URL, $url);
741 curl_setopt($ch, CURLOPT_HEADER, 1);
742 curl_setopt($ch, CURLOPT_NOBODY, 1);
743 curl_setopt($ch, CURLOPT_TIMEOUT, 10);
744 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
745 curl_setopt($ch, CURLOPT_USERAGENT, $a->getUserAgent());
748 $curl_info = @curl_getinfo($ch);
749 $http_code = $curl_info['http_code'];
752 $a->saveTimestamp($stamp1, "network");
754 if ($http_code == 0) {
758 if (in_array($http_code, ['301', '302'])) {
759 if (!empty($curl_info['redirect_url'])) {
760 return self::finalUrl($curl_info['redirect_url'], ++$depth, $fetchbody);
761 } elseif (!empty($curl_info['location'])) {
762 return self::finalUrl($curl_info['location'], ++$depth, $fetchbody);
766 // Check for redirects in the meta elements of the body if there are no redirects in the header.
768 return(self::finalUrl($url, ++$depth, true));
771 // if the file is too large then exit
772 if ($curl_info["download_content_length"] > 1000000) {
776 // if it isn't a HTML file then exit
777 if (!empty($curl_info["content_type"]) && !strstr(strtolower($curl_info["content_type"]), "html")) {
781 $stamp1 = microtime(true);
784 curl_setopt($ch, CURLOPT_URL, $url);
785 curl_setopt($ch, CURLOPT_HEADER, 0);
786 curl_setopt($ch, CURLOPT_NOBODY, 0);
787 curl_setopt($ch, CURLOPT_TIMEOUT, 10);
788 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
789 curl_setopt($ch, CURLOPT_USERAGENT, $a->getUserAgent());
791 $body = curl_exec($ch);
794 $a->saveTimestamp($stamp1, "network");
796 if (trim($body) == "") {
800 // Check for redirect in meta elements
801 $doc = new DOMDocument();
802 @$doc->loadHTML($body);
804 $xpath = new DomXPath($doc);
806 $list = $xpath->query("//meta[@content]");
807 foreach ($list as $node) {
809 if ($node->attributes->length) {
810 foreach ($node->attributes as $attribute) {
811 $attr[$attribute->name] = $attribute->value;
815 if (@$attr["http-equiv"] == 'refresh') {
816 $path = $attr["content"];
817 $pathinfo = explode(";", $path);
818 foreach ($pathinfo as $value) {
819 if (substr(strtolower($value), 0, 4) == "url=") {
820 return self::finalUrl(substr($value, 4), ++$depth);
830 * @brief Find the matching part between two url
832 * @param string $url1
833 * @param string $url2
834 * @return string The matching part
836 public static function getUrlMatch($url1, $url2)
838 if (($url1 == "") || ($url2 == "")) {
842 $url1 = normalise_link($url1);
843 $url2 = normalise_link($url2);
845 $parts1 = parse_url($url1);
846 $parts2 = parse_url($url2);
848 if (!isset($parts1["host"]) || !isset($parts2["host"])) {
852 if (empty($parts1["scheme"])) {
853 $parts1["scheme"] = '';
855 if (empty($parts2["scheme"])) {
856 $parts2["scheme"] = '';
859 if ($parts1["scheme"] != $parts2["scheme"]) {
863 if (empty($parts1["host"])) {
864 $parts1["host"] = '';
866 if (empty($parts2["host"])) {
867 $parts2["host"] = '';
870 if ($parts1["host"] != $parts2["host"]) {
874 if (empty($parts1["port"])) {
875 $parts1["port"] = '';
877 if (empty($parts2["port"])) {
878 $parts2["port"] = '';
881 if ($parts1["port"] != $parts2["port"]) {
885 $match = $parts1["scheme"]."://".$parts1["host"];
887 if ($parts1["port"]) {
888 $match .= ":".$parts1["port"];
891 if (empty($parts1["path"])) {
892 $parts1["path"] = '';
894 if (empty($parts2["path"])) {
895 $parts2["path"] = '';
898 $pathparts1 = explode("/", $parts1["path"]);
899 $pathparts2 = explode("/", $parts2["path"]);
904 $path1 = defaults($pathparts1, $i, '');
905 $path2 = defaults($pathparts2, $i, '');
907 if ($path1 == $path2) {
910 } while (($path1 == $path2) && ($i++ <= count($pathparts1)));
914 return normalise_link($match);
918 * @brief Glue url parts together
920 * @param array $parsed URL parts
922 * @return string The glued URL
924 public static function unparseURL($parsed)
926 $get = function ($key) use ($parsed) {
927 return isset($parsed[$key]) ? $parsed[$key] : null;
930 $pass = $get('pass');
931 $user = $get('user');
932 $userinfo = $pass !== null ? "$user:$pass" : $user;
933 $port = $get('port');
934 $scheme = $get('scheme');
935 $query = $get('query');
936 $fragment = $get('fragment');
937 $authority = ($userinfo !== null ? $userinfo."@" : '') .
939 ($port ? ":$port" : '');
941 return (strlen($scheme) ? $scheme.":" : '') .
942 (strlen($authority) ? "//".$authority : '') .
944 (strlen($query) ? "?".$query : '') .
945 (strlen($fragment) ? "#".$fragment : '');