Skip to content
Snippets Groups Projects
Commit faa3e562 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

php8 only: use typed variables

parent 496743dd
Branches
No related tags found
1 merge request!4Php 8.3 update
...@@ -18,6 +18,7 @@ require_once 'redirect.class.php'; ...@@ -18,6 +18,7 @@ require_once 'redirect.class.php';
* 2022-05-23 v1.6 ah add http head check+render output; * 2022-05-23 v1.6 ah add http head check+render output;
* 2022-05-31 v1.7 ah optical changes * 2022-05-31 v1.7 ah optical changes
* 2023-08-28 v1.8 ah remove php warning if there is no config yet * 2023-08-28 v1.8 ah remove php warning if there is no config yet
* 2024-10-03 v1.9 ah php8 only: typed variables
*/ */
/** /**
...@@ -25,39 +26,46 @@ require_once 'redirect.class.php'; ...@@ -25,39 +26,46 @@ require_once 'redirect.class.php';
* *
* @author axel * @author axel
*/ */
class redirectadmin extends redirect { class redirectadmin extends redirect
{
protected function _getCurlOptions(){ /**
$aReturn=array( * Get default curl options
* @return array
*/
protected function _getCurlOptions(): array
{
$aReturn = [
CURLOPT_HEADER => true, CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true, CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => strip_tags($this->sAbout), CURLOPT_USERAGENT => strip_tags($this->sAbout),
CURLOPT_VERBOSE => false, CURLOPT_VERBOSE => false,
CURLOPT_ENCODING => 'gzip, deflate', // to fetch encoding CURLOPT_ENCODING => 'gzip, deflate', // to fetch encoding
CURLOPT_HTTPHEADER => array( CURLOPT_HTTPHEADER => [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language: en', 'Accept-Language: en',
'DNT: 1', 'DNT: 1',
), ],
// TODO: this is unsafe .. better: let the user configure it // TODO: this is unsafe .. better: let the user configure it
CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 5, CURLOPT_TIMEOUT => 5,
); ];
return $aReturn; return $aReturn;
} }
/** /**
* make a single http(s) get request and return the response body * Make a single http(s) get request and return the response body
* @param string $url url to fetch * @param string $url url to fetch
* @param boolean $bHeaderOnly optional: true=make HEAD request; default: false (=GET) * @param boolean $bHeaderOnly optional: true=make HEAD request; default: false (=GET)
* @return string * @return string
*/ */
public function httpGet($url, $bHeaderOnly = false) { public function httpGet(string $url, bool $bHeaderOnly = false): bool|string
{
$ch = curl_init($url); $ch = curl_init($url);
foreach ($this->_getCurlOptions() as $sCurlOption=>$sCurlValue){ foreach ($this->_getCurlOptions() as $sCurlOption => $sCurlValue) {
curl_setopt($ch, $sCurlOption, $sCurlValue); curl_setopt($ch, $sCurlOption, $sCurlValue);
} }
if ($bHeaderOnly) { if ($bHeaderOnly) {
...@@ -69,113 +77,124 @@ class redirectadmin extends redirect { ...@@ -69,113 +77,124 @@ class redirectadmin extends redirect {
return ($res); return ($res);
} }
public function renderHttpResponseHeader($sHeader){ /**
$sReturn=$sHeader; * Get html code for a response header of a request
if(!$sReturn){ *
$sReturn='<pre><span class="status status-error">Request failed. </span><br>' * @param string $sHeader
.'No data... no response.<br>' * @return string
.'Maybe ... ' */
.'<ul>' public function renderHttpResponseHeader(string $sHeader): string
.'<li>the nostname does not exist ... </li>' {
.'<li>or there is a network problem ... or </li>' $sReturn = $sHeader;
.'<li>the webservice on the target system does not run.</li>' if (!$sReturn) {
.'</ul>' $sReturn = '<pre><span class="status status-error">Request failed. </span><br>'
.'</pre>' . 'No data... no response.<br>'
. 'Maybe ... '
. '<ul>'
. '<li>the nostname does not exist ... </li>'
. '<li>or there is a network problem ... or </li>'
. '<li>the webservice on the target system does not run.</li>'
. '</ul>'
. '</pre>'
; ;
} else { } else {
$sReturn=preg_replace('/(HTTP.*)\\r/', '</pre><pre><strong>$1</strong>', $sReturn); $sReturn = preg_replace('/(HTTP.*)\\r/', '</pre><pre><strong>$1</strong>', $sReturn);
$sReturn=preg_replace('/(HTTP.*200.*)/', '<span class="status status-ok">$1</span>', $sReturn); $sReturn = preg_replace('/(HTTP.*200.*)/', '<span class="status status-ok">$1</span>', $sReturn);
$sReturn=preg_replace('/(HTTP.*30.*)/', '<span class="status status-redirect">$1</span>', $sReturn); $sReturn = preg_replace('/(HTTP.*30.*)/', '<span class="status status-redirect">$1</span>', $sReturn);
$sReturn=preg_replace('/(HTTP.*40.*)/', '<span class="status status-error">$1</span>', $sReturn); $sReturn = preg_replace('/(HTTP.*40.*)/', '<span class="status status-error">$1</span>', $sReturn);
$sReturn=preg_replace('/(HTTP.*50.*)/', '<span class="status status-error">$1</span>', $sReturn); $sReturn = preg_replace('/(HTTP.*50.*)/', '<span class="status status-error">$1</span>', $sReturn);
$sReturn=preg_replace('/\ ([1-5][0-9][0-9])\ /', ' <span class="statuscode">$1</span> ', $sReturn); $sReturn = preg_replace('/\ ([1-5][0-9][0-9])\ /', ' <span class="statuscode">$1</span> ', $sReturn);
$sReturn=preg_replace('/(x-debug-.*)\\r/i', '<span class="debug">$1</span>', $sReturn); $sReturn = preg_replace('/(x-debug-.*)\\r/i', '<span class="debug">$1</span>', $sReturn);
$sReturn=preg_replace('/(location:.*)\\r/i', '<span class="location">$1</span>', $sReturn); $sReturn = preg_replace('/(location:.*)\\r/i', '<span class="location">$1</span>', $sReturn);
$sReturn.='</pre>'; $sReturn .= '</pre>';
preg_match_all('/(HTTP\/.*)/i', $sReturn, $aTmp); preg_match_all('/(HTTP\/.*)/i', $sReturn, $aTmp);
// $sReturn.=print_r($aTmp, 1); // $sReturn.=print_r($aTmp, 1);
$iHops=(count($aTmp[0])-1); $iHops = (count($aTmp[0]) - 1);
$sReturn=($iHops>0 $sReturn = ($iHops > 0
? 'Found hops: <strong>'.$iHops.'</strong>' ? 'Found hops: <strong>' . $iHops . '</strong>'
.($iHops>1 ? ' <span class="warning"> ⚠️ Verify your redirect to skip unneeded hops.</span>' : '' ).'<br><br>' . ($iHops > 1 ? ' <span class="warning"> ⚠️ Verify your redirect to skip unneeded hops.</span>' : '') . '<br><br>'
: '' : ''
).$sReturn ) . $sReturn
; ;
} }
return $sReturn; return $sReturn;
} }
/** /**
* check if admin is enabled * Check if admin is enabled
*
* @return bool * @return bool
*/ */
public function isEnabled(){ public function isEnabled(): bool
$sFile2Enable=__DIR__ . '/'.basename(__FILE__).'_enabled.txt'; {
$sFile2Enable = __DIR__ . '/' . basename(__FILE__) . '_enabled.txt';
return file_exists($sFile2Enable); return file_exists($sFile2Enable);
} }
/** /**
* get ip address of a given hostname as ip (string) or false * Get ip address of a given hostname as ip (string) or false
* @return string|bool * @return string|bool
*/ */
protected function _getIp($sHostname){ protected function _getIp(string $sHostname): string
$sIp=gethostbyname($sHostname); {
return $sIp===$sHostname ? false : $sIp; $sIp = gethostbyname($sHostname);
return $sIp === $sHostname ? '' : $sIp;
} }
/** /**
* get an array with all config entries in all json files * Get an array with all config entries in all json files
* including some error checking * including some error checking
*
* @return array * @return array
*/ */
public function getHosts(){ public function getHosts(): array
$aReturn = array(); {
$aErrors = array(); $aReturn = [];
foreach(glob($this->sConfigDir . '/redirects_*.json') as $sFilename){ $aErrors = [];
$sMyHost= str_replace(array('redirects_', '.json'), array('',''), basename($sFilename)); foreach (glob($this->sConfigDir . '/redirects_*.json') as $sFilename) {
$aReturn[$sMyHost]=array( $sMyHost = str_replace(['redirects_', '.json'], ['', ''], basename($sFilename));
'type'=>'config', $aReturn[$sMyHost] = [
'file'=>$sFilename, 'type' => 'config',
'ip'=> $this->_getIp($sMyHost), 'file' => $sFilename,
'aliases'=>array(), 'ip' => $this->_getIp($sMyHost),
'redirects'=>json_decode(file_get_contents($sFilename), 1), 'aliases' => [],
); 'redirects' => json_decode(file_get_contents($sFilename), 1),
if (!$aReturn[$sMyHost]['ip']){ ];
$aErrors[]=basename($sFilename).': The hostname was not found in DNS: '.$sMyHost; if (!$aReturn[$sMyHost]['ip']) {
$aErrors[] = basename($sFilename) . ': The hostname was not found in DNS: ' . $sMyHost;
} }
} }
$aAliases=$this->_getAliases(); $aAliases = $this->_getAliases();
if(is_array($aAliases) && count($aAliases)){ if (is_array($aAliases) && count($aAliases)) {
foreach($aAliases as $sAlias=>$sConfig){ foreach ($aAliases as $sAlias => $sConfig) {
if(isset($aReturn[$sAlias])){ if (isset($aReturn[$sAlias])) {
$aErrors[]="alias.json: A configuration for alias [$sAlias] is useless. There exists a file redirects_{$sAlias}.json (which has priority)."; $aErrors[] = "alias.json: A configuration for alias [$sAlias] is useless. There exists a file redirects_{$sAlias}.json (which has priority).";
} else { } else {
if(!isset($aReturn[$sConfig])){ if (!isset($aReturn[$sConfig])) {
$aErrors[]="alias.json: [$sAlias] points to a non existing host [$sConfig] - a file redirects_$sConfig.yml does not exist."; $aErrors[] = "alias.json: [$sAlias] points to a non existing host [$sConfig] - a file redirects_$sConfig.yml does not exist.";
} else { } else {
$aReturn[$sConfig]['aliases'][]=$sAlias; $aReturn[$sConfig]['aliases'][] = $sAlias;
$aReturn[$sAlias]=array( $aReturn[$sAlias] = [
'type'=>'alias', 'type' => 'alias',
'target'=>$sConfig, 'target' => $sConfig,
'ip'=> $this->_getIp($sAlias), 'ip' => $this->_getIp($sAlias),
); ];
if (!$aReturn[$sAlias]['ip']){ if (!$aReturn[$sAlias]['ip']) {
$aErrors[]='alias.json: The hostname was not found in DNS: '.$sAlias; $aErrors[] = 'alias.json: The hostname was not found in DNS: ' . $sAlias;
} }
} }
} }
} }
} }
$aReturn['_errors']=$aErrors; $aReturn['_errors'] = $aErrors;
ksort($aReturn); ksort($aReturn);
return $aReturn; return $aReturn;
} }
} }
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
* 2020-05-06 v1.3 ah added aliases for multiple domains with the same config * 2020-05-06 v1.3 ah added aliases for multiple domains with the same config
* 2020-05-06 v1.4 ah rewrite as class * 2020-05-06 v1.4 ah rewrite as class
* 2023-08-28 v1.5 ah fix loop over config with missing regex section. * 2023-08-28 v1.5 ah fix loop over config with missing regex section.
* 2024-10-03 v1.6 ah php8 only: typed variables
*/ */
/** /**
...@@ -31,86 +32,156 @@ ...@@ -31,86 +32,156 @@
* *
* @author axel * @author axel
*/ */
class redirect { class redirect
{
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// CONFIG // CONFIG
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
protected $bDebug = false; /**
protected $sConfigDir = __DIR__ . '/../../config'; * Flag: debug is enabled?
protected $sAbout = 'IML redirect <small>v1.4</small>'; * @var bool
*/
protected bool $bDebug = false;
/**
* configuration dir
* @var string
*/
protected string $sConfigDir = __DIR__ . '/../../config';
protected $sHostname = false; /**
protected $sRequest = false; * About message
* @var string
*/
protected string $sAbout = 'IML redirect <small>v1.4</small>';
protected $sCfgfile = false; /**
protected $aConfig = false; * Hostname
protected $aRedirect = false; * @var string
*/
public $urlRepo='https://git-repo.iml.unibe.ch/iml-open-source/redirect-handler'; protected string $sHostname = '';
public $urlDocs='https://os-docs.iml.unibe.ch/redirect-handler/';
/**
* Request to handle and to redirect to aconfigured target url
* @var string
*/
protected string $sRequest = '';
/**
* Config file
* @var
*/
protected string $sCfgfile = '';
/**
* Config data for the current hostname
* @var
*/
protected array $aConfig = [];
/**
* Redirect data for the current request
* @var array
*/
protected array $aRedirect = [];
/**
* URL to the repository of this project
* @var string
*/
public string $urlRepo = 'https://git-repo.iml.unibe.ch/iml-open-source/redirect-handler';
/**
* Url to the docs
* @var string
*/
public string $urlDocs = 'https://os-docs.iml.unibe.ch/redirect-handler/';
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// CONSTRUCTOR // CONSTRUCTOR
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
public function __constructor($sHostname=false, $sRequest=false) {
if($sHostname){ /**
* Constructor
* @param string $sHostname hostname
* @param string $sRequest request to proces
* @return void
*/
public function __constructor(string $sHostname = '', string $sRequest = '')
{
if ($sHostname) {
$this->setHost($sHostname); $this->setHost($sHostname);
} }
if($sRequest){ if ($sRequest) {
$this->setRequest($sRequest); $this->setRequest($sRequest);
} }
return true;
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// DEBUG // DEBUG
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
/** /**
* write a debug message into the header * Write a debug message into the http response header
*
* @global boolean $bDebug flag: show debug infos? * @global boolean $bDebug flag: show debug infos?
* @staticvar int $i counter * @staticvar int $i counter
*
* @param string $sDebugMessage message to show * @param string $sDebugMessage message to show
* @return boolean * @return boolean
*/ */
protected function _wd($sDebugMessage) { protected function _wd(string $sDebugMessage): bool
{
if (!$this->bDebug) { if (!$this->bDebug) {
return false; return false;
} }
static $i; static $i;
$i++; $i++;
header('X-DEBUG-' . $i . ': ' . $sDebugMessage); header("X-DEBUG-$i: $sDebugMessage");
return true; return true;
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// SET INTERNAL VARS // SET INTERNAL VARS
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
/** /**
* get a string with full path of a config file with aliases * Get a string with full path of a config file with aliases
* @return string * @return string
*/ */
protected function _generateAliasfile() { protected function _generateAliasfile(): string
{
return $this->sConfigDir . '/aliases.json'; return $this->sConfigDir . '/aliases.json';
} }
/** /**
* get a string with full path of a config file based on hostname * Get a string with full path of a config file based on hostname
* @param string $sHostname *
* @param string $sHostname hostname/ fqdn
* @return string * @return string
*/ */
protected function _generateCfgfile($sHostname) { protected function _generateCfgfile(string $sHostname): string
{
return $this->sConfigDir . '/redirects_' . $sHostname . '.json'; return $this->sConfigDir . '/redirects_' . $sHostname . '.json';
} }
protected function _getAliases(){ /**
$sAliasfile=$this->_generateAliasfile(); * Get Aliases from aliases.json
$this->_wd('check alias file '.$sAliasfile); * It returns false if no alias was found
if(!file_exists($sAliasfile)){ *
* @return bool|array
*/
protected function _getAliases(): bool|array
{
$sAliasfile = $this->_generateAliasfile();
$this->_wd('check alias file ' . $sAliasfile);
if (!file_exists($sAliasfile)) {
$this->_wd('alias do not exist'); $this->_wd('alias do not exist');
// $this->sendBody(500, '<h1>Internal Server Error</h1>Errorcode 01'); // $this->sendBody(500, '<h1>Internal Server Error</h1>Errorcode 01');
return false; return false;
...@@ -120,49 +191,51 @@ class redirect { ...@@ -120,49 +191,51 @@ class redirect {
} }
/** /**
* get an array with redirect config based on a given hostname * Get an array with redirect config based on a given hostname.
* @param string $sHostname hostname * @return bool
* @param boolean $bAbort flag: true = do not scan aliases (used for loop detection)
* @return array
*/ */
protected function _getConfig() { protected function _getConfig(): bool
$this->aConfig=false; {
$this->aRedirect=false; $this->aConfig = [];
$this->aRedirect = [];
$this->_getEffectiveConfigfile(); $this->_getEffectiveConfigfile();
if($this->sCfgfile){ if ($this->sCfgfile) {
$aConfig=json_decode(file_get_contents($this->sCfgfile), 1); $aConfig = json_decode(file_get_contents($this->sCfgfile), 1);
if (!is_array($aConfig) || !count($aConfig)) { if (!is_array($aConfig) || !count($aConfig)) {
$this->_wd('no config available'); $this->_wd('no config available');
// $this->sendBody(500, '<h1>Internal Server Error</h1>Errorcode 02'); // $this->sendBody(500, '<h1>Internal Server Error</h1>Errorcode 02');
} else {
$this->aConfig = $aConfig;
} }
$this->aConfig=$aConfig;
} }
return true; return true;
} }
/** /**
* get an array with redirect config based on a given hostname * Get filename of config file based on a given hostname
* or detection in the alias config * or detection in the alias config
*
* @param string $sHostname hostname * @param string $sHostname hostname
* @param boolean $bAbort flag: true = do not scan aliases (used for loop detection) * @param boolean $bAbort flag: true = do not scan aliases (used for loop detection)
* @return array * @return bool|string
*/ */
protected function _getEffectiveConfigfile($sHostname=false, $bAbort=false) { protected function _getEffectiveConfigfile(string $sHostname = '', bool $bAbort = false): bool|string
if(!$sHostname){ {
$sHostname=$this->sHostname; if (!$sHostname) {
$sHostname = $this->sHostname;
} }
$this->sCfgfile=false; $this->sCfgfile = false;
$sCfgfile = $this->_generateCfgfile($sHostname); $sCfgfile = $this->_generateCfgfile($sHostname);
$this->_wd('check config '.$sCfgfile); $this->_wd('check config ' . $sCfgfile);
if (!file_exists($sCfgfile)) { if (!file_exists($sCfgfile)) {
if($bAbort){ if ($bAbort) {
// $this->sendBody(500, '<h1>Internal Server Error</h1>Errorcode 01'); // $this->sendBody(500, '<h1>Internal Server Error</h1>Errorcode 01');
return false; return false;
} }
$aAliases = $this->_getAliases(); $aAliases = $this->_getAliases();
$this->_wd('checking aliases '.print_r($aAliases,1)); $this->_wd('checking aliases ' . print_r($aAliases, 1));
if (!isset($aAliases[$sHostname]) || !file_exists($this->_generateCfgfile($aAliases[$sHostname]))){ if (!isset($aAliases[$sHostname]) || !file_exists($this->_generateCfgfile($aAliases[$sHostname]))) {
$this->_wd('sorry no valid alias for '. $sHostname); $this->_wd('sorry no valid alias for ' . $sHostname);
// $this->sendBody(500, '<h1>Internal Server Error</h1>Errorcode 01'); // $this->sendBody(500, '<h1>Internal Server Error</h1>Errorcode 01');
return false; return false;
} }
...@@ -170,39 +243,42 @@ class redirect { ...@@ -170,39 +243,42 @@ class redirect {
return $this->_getEffectiveConfigfile($aAliases[$this->sHostname], 1); return $this->_getEffectiveConfigfile($aAliases[$this->sHostname], 1);
} }
$this->sCfgfile=$sCfgfile; $this->sCfgfile = $sCfgfile;
return $sCfgfile; return $sCfgfile;
} }
/** /**
* enable/ disable debug * Enable/ disable debug
* @param boolean $bEnable * @param boolean $bEnable
*/ */
public function setDebug($bEnable){ public function setDebug(bool $bEnable): bool
$this->bDebug=!!$bEnable; {
$this->bDebug = !!$bEnable;
return true; return true;
} }
/** /**
* set hostname; internally it detects the config too * Set hostname; internally it detects the config too
* @param type $sHostname * @param string $sHostname
* @return boolean * @return boolean
*/ */
public function setHost($sHostname){ public function setHost(string $sHostname): bool
$this->sHostname=$sHostname; {
$this->sHostname = $sHostname;
$this->_getConfig(); $this->_getConfig();
return true; return true;
} }
/** /**
* set the request * Set the request
* @param type $sRequest * @param string $sRequest
* @return boolean * @return boolean
*/ */
public function setRequest($sRequest){ public function setRequest(string $sRequest): bool
$this->sRequest=$sRequest; {
$this->aRedirect=false; $this->sRequest = $sRequest;
$this->aRedirect = false;
return true; return true;
} }
...@@ -211,26 +287,28 @@ class redirect { ...@@ -211,26 +287,28 @@ class redirect {
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
/** /**
* get an array with the matching redirect; it returns false if none was * Get an array with the matching redirect; it returns false if none was
* detected * detected
*
* @return array * @return array
*/ */
public function getRedirect(){ public function getRedirect(): array
if(is_array($this->aRedirect)){ {
if (is_array($this->aRedirect)) {
return $this->aRedirect; return $this->aRedirect;
} }
$aRedirect = false; $aRedirect = [];
// remark: // remark:
// $this->aConfig is set in setHost() with $this->_getConfig(); // $this->aConfig is set in setHost() with $this->_getConfig();
if (isset($this->aConfig['direct'][$this->sRequest])) { if (isset($this->aConfig['direct'][$this->sRequest])) {
$this->_wd("DIRECT MATCH"); $this->_wd("DIRECT MATCH");
$aRedirect = $this->aConfig['direct'][$this->sRequest]; $aRedirect = $this->aConfig['direct'][$this->sRequest];
} else { } else {
$this->_wd("no direct match ... scanning regex"); $this->_wd("no direct match ... scanning regex");
if(isset($this->aConfig['regex']) && is_array($this->aConfig['regex'])){ if (isset($this->aConfig['regex']) && is_array($this->aConfig['regex'])) {
foreach (array_keys($this->aConfig['regex']) as $sRegex) { foreach (array_keys($this->aConfig['regex']) as $sRegex) {
$this->_wd("check if regex [$sRegex] matches $this->sRequest"); $this->_wd("check if regex [$sRegex] matches $this->sRequest");
if (preg_match('#' . $sRegex . '#', $this->sRequest)) { if (preg_match('#' . $sRegex . '#', $this->sRequest)) {
...@@ -241,21 +319,32 @@ class redirect { ...@@ -241,21 +319,32 @@ class redirect {
} }
} }
} }
$this->aRedirect=$aRedirect; $this->aRedirect = $aRedirect;
return $aRedirect; return $aRedirect;
} }
public function getRedirectCode($iNone=false){ /**
$aRedirect=$this->getRedirect(); * Get 30x redirect code
* @param integer $iNone fallback value if no redirect status code was found
* @return integer
*/
public function getRedirectCode($iNone = 307): int
{
$aRedirect = $this->getRedirect();
return isset($aRedirect['code']) ? $aRedirect['code'] : $iNone; return isset($aRedirect['code']) ? $aRedirect['code'] : $iNone;
} }
public function getRedirectTarget(){
$aRedirect=$this->getRedirect(); /**
return isset($aRedirect['target']) ? $aRedirect['target'] : false; * Get Target url of redirect
* @return string
*/
public function getRedirectTarget(): string
{
$aRedirect = $this->getRedirect();
return $aRedirect['target'] ?? '';
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// FUNCTIONS - SEND DATA // FUNCTIONS - SEND DATA
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
...@@ -264,27 +353,29 @@ class redirect { ...@@ -264,27 +353,29 @@ class redirect {
* make the redirect if it was found ... or a 404 * make the redirect if it was found ... or a 404
* @return true * @return true
*/ */
public function makeRedirect(){ public function makeRedirect(): bool
$sTarget=$this->getRedirectTarget(); {
if(!$sTarget){ $sTarget = $this->getRedirectTarget();
if (!$sTarget) {
$this->_wd('send a not found'); $this->_wd('send a not found');
$this->sendBody(404, '<h1>404 Not found</h1>'); $this->sendBody(404, '<h1>404 Not found</h1>');
} else { } else {
$iCode=$this->getRedirectCode(); $iCode = $this->getRedirectCode();
$this->_wd("Redirect with http status [" . $iCode . "] to new Location: " . $sTarget); $this->_wd("Redirect with http status [$iCode] to new Location: $sTarget");
$this->sendHttpStatusheader($iCode); $this->sendHttpStatusheader($iCode);
header("Location: " . $sTarget); header("Location: " . $sTarget);
} }
return true; return true;
} }
/** /**
* send http status header * send http status header
* @param integer $iCode http code * @param integer $iCode http code
* @return boolean * @return boolean
*/ */
public function sendHttpStatusheader($iCode) { public function sendHttpStatusheader(int $iCode): bool
$aHeaders = array( {
$aHeaders = [
301 => 'Moved Permanently', 301 => 'Moved Permanently',
302 => 'Found', // (Moved Temporarily) 302 => 'Found', // (Moved Temporarily)
303 => 'See other', // redirect with GET 303 => 'See other', // redirect with GET
...@@ -293,7 +384,7 @@ class redirect { ...@@ -293,7 +384,7 @@ class redirect {
404 => 'Not found', 404 => 'Not found',
410 => 'Gone', 410 => 'Gone',
500 => 'Internal Server Error', 500 => 'Internal Server Error',
); ];
$iCode = (int) $iCode; $iCode = (int) $iCode;
if (!isset($aHeaders[$iCode])) { if (!isset($aHeaders[$iCode])) {
...@@ -308,28 +399,29 @@ class redirect { ...@@ -308,28 +399,29 @@ class redirect {
* @see sendHttpStatusheader() * @see sendHttpStatusheader()
* *
* @param integer $iCode http status code * @param integer $iCode http status code
* @param stringg $sBody message text as html code * @param string $sBody message text as html code
* @return void
*/ */
public function sendBody($iCode, $sBody) { public function sendBody(int $iCode, string $sBody): void
{
$this->sendHttpStatusheader($iCode); $this->sendHttpStatusheader($iCode);
die('<!doctype html><html><head>' die('<!doctype html><html><head>'
. '<title>Redirect</title>' . '<title>Redirect</title>'
. '<style>' . '<style>'
. 'body{background:#eee; background: linear-gradient(-10deg,#ccc,#eee,#ddd) fixed; color:#444; font-family: verdana,arial;}' . 'body{background:#eee; background: linear-gradient(-10deg,#ccc,#eee,#ddd) fixed; color:#444; font-family: verdana,arial;}'
. 'h1{color:#a44;font-size: 300%;border-bottom: 1px solid #fff;}' . 'h1{color:#a44;font-size: 300%;border-bottom: 1px solid #fff;}'
. 'h2{color:#ccc; color: rgba(0,0,0,0.1); font-size: 300%; position: absolute; right: 1em; bottom: 1em; text-align: right;}' . 'h2{color:#ccc; color: rgba(0,0,0,0.1); font-size: 300%; position: absolute; right: 1em; bottom: 1em; text-align: right;}'
. 'h2 small{font-size: 50%;}' . 'h2 small{font-size: 50%;}'
. 'footer{background:#ccc; bottom: 1em; color:#666; position: absolute; padding: 1em; right: 1em; }' . 'footer{background:#ccc; bottom: 1em; color:#666; position: absolute; padding: 1em; right: 1em; }'
. '</style>' . '</style>'
. '</head>' . '</head>'
. '<body>' . '<body>'
. $sBody . $sBody
. '<h2>' . $this->sAbout . '</h2>' . '<h2>' . $this->sAbout . '</h2>'
. '<footer>&copy; ' . date('Y') . ' '.$this->sHostname.'</footer>' . '<footer>&copy; ' . date('Y') . ' ' . $this->sHostname . '</footer>'
. '</body></html>' . '</body></html>'
); );
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment