From 3f9a53718c2302d99f621cc1850faf6271881a0d Mon Sep 17 00:00:00 2001 From: Benjamin Takats Date: Wed, 7 Dec 2022 00:07:01 +0100 Subject: [PATCH] url_to_absolute --- app/Http/Livewire/Tables/BookCaseTable.php | 27 +++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/app/Http/Livewire/Tables/BookCaseTable.php b/app/Http/Livewire/Tables/BookCaseTable.php index de8d8b94..e9aa8534 100644 --- a/app/Http/Livewire/Tables/BookCaseTable.php +++ b/app/Http/Livewire/Tables/BookCaseTable.php @@ -45,11 +45,36 @@ class BookCaseTable extends DataTableComponent fn( $row, Column $column - ) => $row->homepage ? 'Link' : null + ) => $row->homepage ? 'Link' : null ) ->html(), BooleanColumn::make('Oranged-Pilled', 'deactivated') ->sortable(), ]; } + + private function url_to_absolute($url) + { + // Determine request protocol + $request_protocol = $request_protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http'); + // If dealing with a Protocol Relative URL + if (stripos($url, '//') === 0) { + return $url; + } + // If dealing with a Root-Relative URL + if (stripos($url, '/') === 0) { + return $request_protocol.'://'.$_SERVER['HTTP_HOST'].$url; + } + // If dealing with an Absolute URL, just return it as-is + if (stripos($url, 'http') === 0) { + return $url; + } + // If dealing with a relative URL, + // and attempt to handle double dot notation ".." + do { + $url = preg_replace('/[^\/]+\/\.\.\//', '', $url, 1, $count); + } while ($count); + // Return the absolute version of a Relative URL + return $request_protocol.'://'.$_SERVER['HTTP_HOST'].'/'.$url; + } }