Files
hermes/functions/redirect.php
Joey Kimsey 4dbea74e5f Cleanup
Improved readme
removed contributing and code of conduct as they are basically just fluff when no one knows this repo exists
removed composer.lock because this repo doesn't install anything to lock
update copywrite to 2025
composer update to description
comments
2025-02-02 07:14:21 -05:00

65 lines
1.8 KiB
PHP

<?php
/**
* functions/redirect.php
*
* This class is used for header modification and page redirection.
*
* @version 1.1
* @author Joey Kimsey <Joey@thetempusproject.com>
* @link https://TheTempusProject.com/libraries/Hermes
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
*/
namespace TheTempusProject\Hermes\Functions;
class Redirect {
/**
* The main redirect function. This will automatically call the
* error controller if the value passed to it is numerical. It will
* automatically populate the url based on the config and add the
* $data string at the end
*
* @param string|int $data - The desired redirect location (string for location and integer for error page).
*/
public static function to( $data ) {
if ( ! HERMES_REDIRECTS_ENABLED ) {
return;
}
if ( is_numeric( $data ) ) {
header( 'Location: ' . Route::getAddress() . 'Errors/' . $data );
}
$url = Route::getAddress() . $data;
if ( filter_var( $url, FILTER_VALIDATE_URL ) != false ) {
header( 'Location: ' . $url );
}
}
public static function home() {
if ( ! HERMES_REDIRECTS_ENABLED ) {
return;
}
header( 'Location: ' . Route::getAddress() );
}
public static function external( $data ) {
if ( ! HERMES_REDIRECTS_ENABLED ) {
return;
}
$url = filter_var( $data, FILTER_SANITIZE_URL );
if ( filter_var( $url, FILTER_VALIDATE_URL ) != false ) {
header( 'Location: ' . $data );
}
}
/**
* Refreshes the current page.
*
* @return null
*/
public static function reload() {
if ( ! HERMES_REDIRECTS_ENABLED ) {
exit();
}
header( 'Refresh:0' );
}
}