diff options
author | 2024-02-09 22:27:35 +0100 | |
---|---|---|
committer | 2024-02-09 22:27:35 +0100 | |
commit | 7b2ac362648d755874171300d1f5b2d9aea0ff46 (patch) | |
tree | 2c07bb2df11b5acc8a53bf424c068c891ded408f /lib/php-urljoin | |
parent | 46ac77590e275bf3724e23f191cdfcd011bb72f9 (diff) | |
download | rss-bridge-7b2ac362648d755874171300d1f5b2d9aea0ff46.tar.gz rss-bridge-7b2ac362648d755874171300d1f5b2d9aea0ff46.tar.zst rss-bridge-7b2ac362648d755874171300d1f5b2d9aea0ff46.zip |
chore: move committed third-party deps to lib (#3973)
Diffstat (limited to 'lib/php-urljoin')
-rw-r--r-- | lib/php-urljoin/LICENSE | 21 | ||||
-rw-r--r-- | lib/php-urljoin/src/urljoin.php | 143 |
2 files changed, 164 insertions, 0 deletions
diff --git a/lib/php-urljoin/LICENSE b/lib/php-urljoin/LICENSE new file mode 100644 index 00000000..033fc3c7 --- /dev/null +++ b/lib/php-urljoin/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 j. shagam <fluffy@beesbuzz.biz> + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/lib/php-urljoin/src/urljoin.php b/lib/php-urljoin/src/urljoin.php new file mode 100644 index 00000000..026b767e --- /dev/null +++ b/lib/php-urljoin/src/urljoin.php @@ -0,0 +1,143 @@ +<?php + +/* + +A spiritual port of Python's urlparse.urljoin() function to PHP. Why this isn't in the standard library is anyone's guess. + +Author: fluffy, http://beesbuzz.biz/ +Latest version at: https://github.com/plaidfluff/php-urljoin + + */ + +function urljoin($base, $rel) { + if (!$base) { + return $rel; + } + + if (!$rel) { + return $base; + } + + $uses_relative = array('', 'ftp', 'http', 'gopher', 'nntp', 'imap', + 'wais', 'file', 'https', 'shttp', 'mms', + 'prospero', 'rtsp', 'rtspu', 'sftp', + 'svn', 'svn+ssh', 'ws', 'wss'); + + $pbase = parse_url($base); + $prel = parse_url($rel); + + if ($prel === false || preg_match('/^[a-z0-9\-.]*[^a-z0-9\-.:][a-z0-9\-.]*:/i', $rel)) { + /* + Either parse_url couldn't parse this, or the original URL + fragment had an invalid scheme character before the first :, + which can confuse parse_url + */ + $prel = array('path' => $rel); + } + + if (array_key_exists('path', $pbase) && $pbase['path'] === '/') { + unset($pbase['path']); + } + + if (isset($prel['scheme'])) { + if ( + $prel['scheme'] != ($pbase['scheme'] ?? null) + || in_array($prel['scheme'], $uses_relative) == false + ) { + return $rel; + } + } + + $merged = array_merge($pbase, $prel); + + // Handle relative paths: + // 'path/to/file.ext' + // './path/to/file.ext' + if (array_key_exists('path', $prel) && substr($prel['path'], 0, 1) != '/') { + + // Normalize: './path/to/file.ext' => 'path/to/file.ext' + if (substr($prel['path'], 0, 2) === './') { + $prel['path'] = substr($prel['path'], 2); + } + + if (array_key_exists('path', $pbase)) { + $dir = preg_replace('@/[^/]*$@', '', $pbase['path']); + $merged['path'] = $dir . '/' . $prel['path']; + } else { + $merged['path'] = '/' . $prel['path']; + } + + } + + if(array_key_exists('path', $merged)) { + // Get the path components, and remove the initial empty one + $pathParts = explode('/', $merged['path']); + array_shift($pathParts); + + $path = []; + $prevPart = ''; + foreach ($pathParts as $part) { + if ($part == '..' && count($path) > 0) { + // Cancel out the parent directory (if there's a parent to cancel) + $parent = array_pop($path); + // But if it was also a parent directory, leave it in + if ($parent == '..') { + array_push($path, $parent); + array_push($path, $part); + } + } else if ($prevPart != '' || ($part != '.' && $part != '')) { + // Don't include empty or current-directory components + if ($part == '.') { + $part = ''; + } + array_push($path, $part); + } + $prevPart = $part; + } + $merged['path'] = '/' . implode('/', $path); + } + + $ret = ''; + if (isset($merged['scheme'])) { + $ret .= $merged['scheme'] . ':'; + } + + if (isset($merged['scheme']) || isset($merged['host'])) { + $ret .= '//'; + } + + if (isset($prel['host'])) { + $hostSource = $prel; + } else { + $hostSource = $pbase; + } + + // username, password, and port are associated with the hostname, not merged + if (isset($hostSource['host'])) { + if (isset($hostSource['user'])) { + $ret .= $hostSource['user']; + if (isset($hostSource['pass'])) { + $ret .= ':' . $hostSource['pass']; + } + $ret .= '@'; + } + $ret .= $hostSource['host']; + if (isset($hostSource['port'])) { + $ret .= ':' . $hostSource['port']; + } + } + + if (isset($merged['path'])) { + $ret .= $merged['path']; + } + + if (isset($prel['query'])) { + $ret .= '?' . $prel['query']; + } + + if (isset($prel['fragment'])) { + $ret .= '#' . $prel['fragment']; + } + + return $ret; +} |