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

update admin: more checks; added ajax requests

parent 78f276c6
No related branches found
No related tags found
No related merge requests found
/**
* ======================================================================
*
* IML REDIRECT :: JAVASCRIPT FUNCTIONS
*
* ----------------------------------------------------------------------
* 2022-05-23 ah v0.1 initial version
* ======================================================================
*/
// ----------------------------------------------------------------------
// CONFIG
// ----------------------------------------------------------------------
let divOut="divoverlay";
// ----------------------------------------------------------------------
// FUNCTIONS
// ----------------------------------------------------------------------
/**
* show given html code in overlay
* @param {string} body html code to display
*/
async function show(body){
oDiv=document.getElementById(divOut);
oDiv.innerHTML='<div>'+body+'</div>';
oDiv.style.display='block';
}
/**
* show content of a url in the overlay
* @param {object} oLink link object; in a tag use this to reference itself
*/
async function showInOverlay(oLink){
show('<h2>Please wait ...</h2><p>Requesting data...</p>');
let response = await fetch(oLink.href);
let body = await response.text(); // read response body as text
if (response.ok) {
show(body);
} else {
show("HTTP-Error: " + response.status);
}
}
// ----------------------------------------------------------------------
// page init
// ----------------------------------------------------------------------
// init datatable
$(document).ready( function () {
$('.mydatatable').DataTable({
"lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
stateSave: true
});
} );
// ----------------------------------------------------------------------
\ No newline at end of file
...@@ -20,6 +20,24 @@ $oR=new redirectadmin(); ...@@ -20,6 +20,24 @@ $oR=new redirectadmin();
$sHtml=''; $sHtml='';
$sErrors=''; $sErrors='';
$aIco=[
'h1'=>'⏩',
'h2_head'=>'✔️',
'h2_err'=>'🔔',
'h2_config'=>'🛠️',
'h2_file'=>'📄',
'ip_ok'=>'🟢',
'ip_warn'=>'🟠',
'ip_err'=>'❗',
'type_config'=>'🔷',
'type_alias'=>'◻️',
'url'=>'🌐',
];
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// FUNCTIONS // FUNCTIONS
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
...@@ -36,9 +54,34 @@ if (!$oR->isEnabled()){ ...@@ -36,9 +54,34 @@ if (!$oR->isEnabled()){
$sHtml.='<div class="error">Admin is disabled.</div>'; $sHtml.='<div class="error">Admin is disabled.</div>';
} else { } else {
$sUrl=(isset($_GET['url']) && $_GET['url']) ? $sUrl=$_GET['url'] : '';
$sMyIp=gethostbyname($_SERVER['SERVER_NAME']);
// ---------- return content for ajax requests
// ---------- TEST URL
$sUrl=(isset($_GET['url']) && $_GET['url']) ? $_GET['url'] : '';
if ($sUrl){
$sResult=$oR->httpGet($sUrl.'?debugredirect=1',1);
echo '<h2>Response of a http HEAD request to '.$aIco['url'].' <a href="'.$sUrl.'">'.$sUrl.'</a></h2>'
. $oR->renderHttpResponseHeader($sResult, 1)
;
return true;
}
# ---------- return content for ajax requests
$sCfgfile=(isset($_GET['cfgfile']) && $_GET['cfgfile']) ? $_GET['cfgfile'] : '';
if($sCfgfile){
$sFilename=__DIR__.'/../../config/'.$sCfgfile;
echo '<h2>'.$aIco['h2_file'].' File: '.htmlentities($sCfgfile).'</h2>'
. (file_exists($sFilename)
? '<pre>'. file_get_contents($sFilename).'</pre>'
: '<div class="error">ERROR: config file was not found.</div>'
)
;
exit(0);
}
$sMyIp=gethostbyname($_SERVER['SERVER_NAME']);
if(!$sMyIp){ if(!$sMyIp){
$sErrors.='<li>Ip address of current host ['.$_SERVER['SERVER_NAME'].'] was not found.</li>'; $sErrors.='<li>Ip address of current host ['.$_SERVER['SERVER_NAME'].'] was not found.</li>';
} }
...@@ -50,7 +93,7 @@ if (!$oR->isEnabled()){ ...@@ -50,7 +93,7 @@ if (!$oR->isEnabled()){
// ---------- SHOW ERRORS // ---------- SHOW ERRORS
if(count($aHosts['_errors'])) { if(count($aHosts['_errors'])) {
$sErrors.= '<li>' . implode('</li></li>', $aHosts['_errors']).'</li>'; $sErrors.= '<li>' . implode('</li><li>', $aHosts['_errors']).'</li>';
} }
unset($aHosts['_errors']); unset($aHosts['_errors']);
...@@ -58,16 +101,17 @@ if (!$oR->isEnabled()){ ...@@ -58,16 +101,17 @@ if (!$oR->isEnabled()){
$sHtml.=' $sHtml.='
<h2>✔️ Http head tester</h2> <!--
<h2>'.$aIco['h2_head'].' Http head tester</h2>
<div class="content"> <div class="content">
<form> <form>
🌐 <input type="text" name="url" size="100" value="'.$sUrl.'" placeholder="Enter url or click a link in the table below."/> '.$aIco['url'].' <input type="text" name="url" size="100" value="'.$sUrl.'" placeholder="Enter url or click a link in the table below."/>
<button>Http HEAD</button> <button>Http HEAD</button>
</form> </form>
</div> </div>
<br> <br>
-->
<h2>🛠️ Domains and their redirects</h2> <h2>'.$aIco['h2_config'].' Domains and their redirects</h2>
<div class="content"> <div class="content">
<table class="mydatatable"><thead> <table class="mydatatable"><thead>
...@@ -83,20 +127,30 @@ if (!$oR->isEnabled()){ ...@@ -83,20 +127,30 @@ if (!$oR->isEnabled()){
</thead><tbody>'; </thead><tbody>';
foreach($aHosts as $sHost => $aCfg){ foreach($aHosts as $sHost => $aCfg){
$sTdFirst='<tr class="cfgtype-'.$aCfg['type'].'">' $sTdFirst='<tr class="cfgtype-'.$aCfg['type'].'">'
.'<td>🔷 <a href="?url=http://'.$sHost.'/" title="click to test http://'.$sHost.'/">'.$sHost.'</a></td>' .'<td>'
.'<span style="display: none">'.$sHost.'</span>'
.$aIco['type_'.$aCfg['type']]
.' <a href="?url=http://'.$sHost.'/" title="click to test http://'.$sHost.'/" onclick="showInOverlay(this); return false;">'.$sHost.'</a></td>'
.'<td>' .'<td>'
.($aCfg['ip'] .($aCfg['ip']
? ($aCfg['ip']===$sMyIp ? ($aCfg['ip']===$sMyIp
? '<span title="">🟢 '.$aCfg['ip']. '</span>' ? '<span title="">'.$aIco['ip_ok'].' '.$aCfg['ip']. '</span>'
: '<span title="Warning: this is not the ip address of the current host ('.$sMyIp.')">🟠 '.$aCfg['ip']. '</span>' : '<span title="Warning: this is not the ip address of the current host ('.$sMyIp.')">'.$aIco['ip_warn'].' '.$aCfg['ip']. '</span>'
)
: '<span class="error">'.$aIco['ip_err'].' ERROR: unknown host</span>')
.'</td>'
.'<td>'
.($aCfg['type']=="config"
? '<a href="?cfgfile=redirects_'.$sHost.'.json" onclick="showInOverlay(this); return false;" title="show config for host '.$sHost.'">'.$aCfg['type'].'</a> '
: $aCfg['type']
) )
: '<span class="error">❗ERROR: unknown host</span>')
. '</td>' . '</td>'
.'<td>'.$aCfg['type'].'</td>'
; ;
/*
if(!$aCfg['ip']){ if(!$aCfg['ip']){
$sErrors.='<li>Host was not found in DNS: '.$sHost.'</li>'; $sErrors.='<li>Host was not found in DNS: '.$sHost.'</li>';
} }
*/
if (isset($aCfg['redirects'])){ if (isset($aCfg['redirects'])){
$iCount=0; $iCount=0;
foreach(['direct', 'regex'] as $sType){ foreach(['direct', 'regex'] as $sType){
...@@ -106,10 +160,13 @@ if (!$oR->isEnabled()){ ...@@ -106,10 +160,13 @@ if (!$oR->isEnabled()){
$sHtml.=$sTdFirst $sHtml.=$sTdFirst
.'<td class="type-'.$sType.'">'.$sType.'</td>' .'<td class="type-'.$sType.'">'.$sType.'</td>'
.'<td class="type-'.$sType.'">' .'<td class="type-'.$sType.'">'
.($sType == 'direct' ? '<a href="?url=http://'.$sHost.'/'.$sFrom.'" title="click to test http://'.$sHost.'/'.$sFrom.'">'.$sFrom.'</a>' : $sFrom) .($sType == 'direct'
? '<a href="?url=http://'.$sHost.'/'.$sFrom.'" title="click to test http://'.$sHost.'/'.$sFrom.'" onclick="showInOverlay(this); return false;">/'.$sFrom.'</a>'
: $sFrom
)
.'</td>' .'</td>'
.'<td class="http-'.$aTo['code'].'">'.$aTo['code'].'</td>' .'<td class="http-'.$aTo['code'].'">'.$aTo['code'].'</td>'
.'<td>🌐 <a href="?url='.$aTo['target'].'" title="click to test '.$aTo['target'].'">'.$aTo['target'].'</a></td>' .'<td>'.$aIco['url'].' <a href="?url='.$aTo['target'].'" title="click to test '.$aTo['target'].'" onclick="showInOverlay(this); return false;">'.$aTo['target'].'</a></td>'
.'</tr>'; .'</tr>';
} }
} }
...@@ -129,18 +186,11 @@ if (!$oR->isEnabled()){ ...@@ -129,18 +186,11 @@ if (!$oR->isEnabled()){
*/ */
; ;
// ---------- TEST URL
if ($sUrl){
$sResult=$oR->httpGet($sUrl.'?debugredirect=1',1);
$sHtml.= '<div class="overlay" onclick="this.style.display=\'none\';"><div>'
. '<h2>🌐 <a href="'.$sUrl.'">'.$sUrl.'</a></h2> (click to open)<br><br>'
. '📄 Http response header:<br><br>'.$oR->renderHttpResponseHeader($sResult, 1)
. '</div></div>'
;
}
$sErrors = $sErrors $sErrors = $sErrors
? '<h2>🔔 Found errors</h2>' ? '<h2>'.$aIco['h2_err'].' Found errors</h2>'
.'<div class="content">' .'<div class="content">'
.'<ol class="error">' .'<ol class="error">'
.$sErrors .$sErrors
...@@ -163,17 +213,14 @@ if (!$oR->isEnabled()){ ...@@ -163,17 +213,14 @@ if (!$oR->isEnabled()){
<link rel="stylesheet" href="main.css"> <link rel="stylesheet" href="main.css">
</head> </head>
<body> <body>
<h1> <a href="?">Redirects :: admin</a></h1> <h1><?php echo $aIco['h1']?> <a href="?">Redirects :: admin</a></h1>
<?php echo $sErrors . $sHtml; ?> <?php echo $sErrors . $sHtml; ?>
<div id="divoverlay" class="overlay" onclick="this.style.display='none';"></div>
<script type="text/javascript" src="functions.js"></script>
<script> <script>
$(document).ready( function () {
$('.mydatatable').DataTable({
"lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
stateSave: true
});
} );
</script> </script>
</body> </body>
</html> </html>
...@@ -3,9 +3,11 @@ body{background: #f8f8f8; color: #234; font-family: arial; margin: 0;} ...@@ -3,9 +3,11 @@ body{background: #f8f8f8; color: #234; font-family: arial; margin: 0;}
h1{background:rgba(0,0,0,0.05); margin: 0 0 1em;; padding: 0.5em;} h1{background:rgba(0,0,0,0.05); margin: 0 0 1em;; padding: 0.5em;}
h1 a{color:#234; text-decoration: none;} h1 a{color:#234; text-decoration: none;}
h2{background:rgba(0,0,0,0.02); color:#458; margin: 1em 0 0.5em; border-top: 2px solid #fff; border-left: 5px solid #fff; border-top-left-radius: 0.5em; padding: 0.5em; } h2{background:rgba(0,0,0,0.02); color:#458; margin: 1em 0 0.5em; border-top: 2px solid #fff; border-left: 5px solid #fff; border-top-left-radius: 0.5em; padding: 0.5em; }
h2 a{color:#458; text-decoration: none; }
pre{background: rgba(0,0,0,0.02);padding: 0.3em 1em; border: 1px solid rgba(0,0,0,0.1); margin-bottom: 2em;} pre{background: rgba(0,0,0,0.02);padding: 0.3em 1em; border: 1px solid rgba(0,0,0,0.1); margin-bottom: 2em;}
tr:hover{background: #f4f0f8 !important;}
.content{margin: 0 1em;} .content{margin: 0 1em;}
.error{background: #fcc;} .error{background: #fcc;}
...@@ -24,5 +26,6 @@ pre{background: rgba(0,0,0,0.02);padding: 0.3em 1em; border: 1px solid rgba(0,0, ...@@ -24,5 +26,6 @@ pre{background: rgba(0,0,0,0.02);padding: 0.3em 1em; border: 1px solid rgba(0,0,
.location{background:#fec;} .location{background:#fec;}
.debug{color:#197;} .debug{color:#197;}
.overlay{position: fixed; margin: 0; width: 100%; height: 100%; top: 0; left: 0; background: rgba(0,0,0,0.3);overflow: scroll;} .overlay{position: fixed; margin: 0; width: 100%; height: 100%; top: 0; left: 0; background: rgba(0,0,0,0.3);overflow: scroll; display: none;}
.overlay>div{margin: 3% 10%; background: #fff; padding: 1em;box-shadow: 0 0 3em #000; } .overlay>div{margin: 3% 10%; background: #fff; padding: 1em;box-shadow: 0 0 3em #000; }
...@@ -15,6 +15,7 @@ require_once 'redirect.class.php'; ...@@ -15,6 +15,7 @@ require_once 'redirect.class.php';
* ---------------------------------------------------------------------- * ----------------------------------------------------------------------
* 2020-05-11 v1.4 ah rewrite as class * 2020-05-11 v1.4 ah rewrite as class
* 2022-02-03 v1.5 ah add method isEnabled * 2022-02-03 v1.5 ah add method isEnabled
* 2022-05-23 v1.6 ah add http head check+render output;
*/ */
/** /**
...@@ -119,14 +120,19 @@ class redirectadmin extends redirect { ...@@ -119,14 +120,19 @@ class redirectadmin extends redirect {
return file_exists($sFile2Enable); return file_exists($sFile2Enable);
} }
/**
* get ip address of a given hostname as ip (string) or false
* @return string|bool
*/
protected function _getIp($sHostname){ protected function _getIp($sHostname){
$sIp=gethostbyname($sHostname); $sIp=gethostbyname($sHostname);
return $sIp===$sHostname ? false : $sIp; return $sIp===$sHostname ? false : $sIp;
} }
/** /**
* get an array with all config entries in all json * get an array with all config entries in all json files
* including some error checking
* @return array
*/ */
public function getHosts(){ public function getHosts(){
$aReturn = array(); $aReturn = array();
...@@ -140,11 +146,14 @@ class redirectadmin extends redirect { ...@@ -140,11 +146,14 @@ class redirectadmin extends redirect {
'aliases'=>array(), 'aliases'=>array(),
'redirects'=>json_decode(file_get_contents($sFilename), 1), 'redirects'=>json_decode(file_get_contents($sFilename), 1),
); );
if (!$aReturn[$sMyHost]['ip']){
$aErrors[]=basename($sFilename).': The hostname was not found in DNS: '.$sMyHost;
}
} }
$aAliases=$this->_getAliases(); $aAliases=$this->_getAliases();
foreach($aAliases as $sAlias=>$sConfig){ foreach($aAliases as $sAlias=>$sConfig){
if(isset($aReturn[$sAlias])){ if(isset($aReturn[$sAlias])){
$aErrors[]="alias.json: Alias [$sAlias] was set already - ".print_r($aReturn[$sAlias], 1); $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.";
...@@ -155,6 +164,9 @@ class redirectadmin extends redirect { ...@@ -155,6 +164,9 @@ class redirectadmin extends redirect {
'target'=>$sConfig, 'target'=>$sConfig,
'ip'=> $this->_getIp($sAlias), 'ip'=> $this->_getIp($sAlias),
); );
if (!$aReturn[$sAlias]['ip']){
$aErrors[]='alias.json: The hostname was not found in DNS: '.$sAlias;
}
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment