master
Jean-Christian Paul Denis 2023-05-01 00:52:41 +02:00
parent 39a933e5a1
commit 552de6fea8
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
2 changed files with 15 additions and 8 deletions

View File

@ -40,7 +40,7 @@ class Alias
return $this->aliases; return $this->aliases;
} }
// nullsafe PHP < 8.0 // nullsafe
if (is_null(dcCore::app()->blog)) { if (is_null(dcCore::app()->blog)) {
return []; return [];
} }
@ -51,7 +51,7 @@ class Alias
'alias_url', 'alias_url',
'alias_destination', 'alias_destination',
'alias_position', 'alias_position',
'alias_redirect' 'alias_redirect',
]) ])
->where('blog_id = ' . $sql->quote((string) dcCore::app()->blog->id)) ->where('blog_id = ' . $sql->quote((string) dcCore::app()->blog->id))
->order('alias_position ASC') ->order('alias_position ASC')
@ -100,11 +100,11 @@ class Alias
* @param string $url The URL * @param string $url The URL
* @param string $destination The destination * @param string $destination The destination
* @param int $position The position * @param int $position The position
* @param bool $position Do redirection * @param bool $redirect Do redirection
*/ */
public function createAlias(string $url, string $destination, int $position, bool $redirect): void public function createAlias(string $url, string $destination, int $position, bool $redirect): void
{ {
// nullsafe PHP < 8.0 // nullsafe
if (is_null(dcCore::app()->blog)) { if (is_null(dcCore::app()->blog)) {
return; return;
} }
@ -135,7 +135,7 @@ class Alias
*/ */
public function deleteAlias(string $url): void public function deleteAlias(string $url): void
{ {
// nullsafe PHP < 8.0 // nullsafe
if (is_null(dcCore::app()->blog)) { if (is_null(dcCore::app()->blog)) {
return; return;
} }
@ -152,7 +152,7 @@ class Alias
*/ */
public function deleteAliases(): void public function deleteAliases(): void
{ {
// nullsafe PHP < 8.0 // nullsafe
if (is_null(dcCore::app()->blog)) { if (is_null(dcCore::app()->blog)) {
return; return;
} }
@ -172,6 +172,6 @@ class Alias
*/ */
public static function removeBlogUrl(string $url): string public static function removeBlogUrl(string $url): string
{ {
return str_replace(dcCore::app()->blog->url, '', trim($url)); return is_null(dcCore::app()->blog) ? trim($url) : str_replace(dcCore::app()->blog->url, '', trim($url));
} }
} }

View File

@ -36,16 +36,19 @@ class Prepend extends dcNsProcess
dcCore::app()->addBehavior('urlHandlerGetArgsDocument', function (dcUrlHandlers $handler): void { dcCore::app()->addBehavior('urlHandlerGetArgsDocument', function (dcUrlHandlers $handler): void {
$found = $redir = false; $found = $redir = false;
$type = 'alias'; $type = '';
$part = $args = $_SERVER['URL_REQUEST_PART']; $part = $args = $_SERVER['URL_REQUEST_PART'];
// load all Aliases
foreach ((new Alias())->getAliases() as $v) { foreach ((new Alias())->getAliases() as $v) {
// multi alias using "/url/" to "destination"
if (@preg_match('#^/.*/$#', $v['alias_url']) && @preg_match($v['alias_url'], $args)) { if (@preg_match('#^/.*/$#', $v['alias_url']) && @preg_match($v['alias_url'], $args)) {
$part = preg_replace($v['alias_url'], $v['alias_destination'], $args); $part = preg_replace($v['alias_url'], $v['alias_destination'], $args);
$found = true; $found = true;
$redir = !empty($v['alias_redirect']); $redir = !empty($v['alias_redirect']);
break; break;
// single alias using "url" to "destination"
} elseif ($v['alias_url'] == $args) { } elseif ($v['alias_url'] == $args) {
$part = $v['alias_destination']; $part = $v['alias_destination'];
$found = true; $found = true;
@ -55,17 +58,21 @@ class Prepend extends dcNsProcess
} }
} }
// no URLs found
if (!$found) { if (!$found) {
return; return;
} }
// Use visible redirection
if ($redir) { if ($redir) {
Http::redirect(dcCore::app()->blog->url . $part); Http::redirect(dcCore::app()->blog->url . $part);
} }
// regain URL type
$_SERVER['URL_REQUEST_PART'] = $part; $_SERVER['URL_REQUEST_PART'] = $part;
dcCore::app()->url->getArgs($part, $type, $args); dcCore::app()->url->getArgs($part, $type, $args);
// call real handler
if (!$type) { if (!$type) {
dcCore::app()->url->callDefaultHandler($args); dcCore::app()->url->callDefaultHandler($args);
} else { } else {