url_to_absolute

This commit is contained in:
Benjamin Takats
2022-12-07 00:07:01 +01:00
parent a99ee24ad2
commit 3f9a53718c

View File

@@ -45,11 +45,36 @@ class BookCaseTable extends DataTableComponent
fn( fn(
$row, $row,
Column $column Column $column
) => $row->homepage ? '<a target="_blank" class="underline text-amber-500" href="'.$row->homepage.'">Link</a>' : null ) => $row->homepage ? '<a target="_blank" class="underline text-amber-500" href="'.$this->url_to_absolute($row->homepage).'">Link</a>' : null
) )
->html(), ->html(),
BooleanColumn::make('Oranged-Pilled', 'deactivated') BooleanColumn::make('Oranged-Pilled', 'deactivated')
->sortable(), ->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;
}
} }