mirror of
https://framagit.org/hubzilla/core.git
synced 2026-06-21 00:52:33 -04:00
Merge branch 'dev' of https://framagit.org/hubzilla/core into dev
This commit is contained in:
@@ -2101,21 +2101,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, 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', '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);
|
||||
}
|
||||
|
||||
@@ -67,4 +67,56 @@ class NetworkTest extends Zotlabs\Tests\Unit\UnitTestCase {
|
||||
['address-tag@example.com', true],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the unparse_url function.
|
||||
*
|
||||
*/
|
||||
public function test_unparse_url_full()
|
||||
{
|
||||
$parsed_url = [
|
||||
'scheme' => 'https',
|
||||
'host' => 'www.example.com',
|
||||
'port' => '8080',
|
||||
'user' => 'username',
|
||||
'pass' => 'password',
|
||||
'path' => '/path',
|
||||
'query' => 'param=value',
|
||||
'fragment' => 'section'
|
||||
];
|
||||
|
||||
$expected = 'https://username:password@www.example.com:8080/path?param=value#section';
|
||||
$this->assertEquals($expected, unparse_url($parsed_url));
|
||||
}
|
||||
|
||||
public function test_unparse_url_partial()
|
||||
{
|
||||
$parsed_url = [
|
||||
'scheme' => 'http',
|
||||
'host' => 'example.com',
|
||||
'path' => '/index.php'
|
||||
];
|
||||
|
||||
$expected = 'http://example.com/index.php';
|
||||
$this->assertEquals($expected, unparse_url($parsed_url));
|
||||
}
|
||||
|
||||
public function test_unparse_url_custom()
|
||||
{
|
||||
$parsed_url = [
|
||||
'scheme' => 'https',
|
||||
'host' => 'www.example.com',
|
||||
'port' => '443',
|
||||
'path' => '/api'
|
||||
];
|
||||
|
||||
$parts = ['scheme', 'host'];
|
||||
$expected = 'https://www.example.com';
|
||||
$this->assertEquals($expected, unparse_url($parsed_url, $parts));
|
||||
}
|
||||
|
||||
public function test_unparse_url_empty()
|
||||
{
|
||||
$this->assertEquals('', unparse_url([]));
|
||||
}
|
||||
}
|
||||
|
||||
4
vendor/composer/installed.php
vendored
4
vendor/composer/installed.php
vendored
@@ -3,7 +3,7 @@
|
||||
'name' => 'zotlabs/hubzilla',
|
||||
'pretty_version' => 'dev-master',
|
||||
'version' => 'dev-master',
|
||||
'reference' => '65c8de34101cedc33fc8e6282ff11e8e57eeed1b',
|
||||
'reference' => 'cfcac590c3a5d183db3a496fa2e9be344b599705',
|
||||
'type' => 'application',
|
||||
'install_path' => __DIR__ . '/../../',
|
||||
'aliases' => array(),
|
||||
@@ -367,7 +367,7 @@
|
||||
'zotlabs/hubzilla' => array(
|
||||
'pretty_version' => 'dev-master',
|
||||
'version' => 'dev-master',
|
||||
'reference' => '65c8de34101cedc33fc8e6282ff11e8e57eeed1b',
|
||||
'reference' => 'cfcac590c3a5d183db3a496fa2e9be344b599705',
|
||||
'type' => 'application',
|
||||
'install_path' => __DIR__ . '/../../',
|
||||
'aliases' => array(),
|
||||
|
||||
Reference in New Issue
Block a user