diff --git a/public_html/admin.php b/public_html/admin.php index 3fa24c01a4bca25cedab7d9c726e2a00772792e6..7c70d3c3bc5ddca053f79053505c583a0c623072 100644 --- a/public_html/admin.php +++ b/public_html/admin.php @@ -13,107 +13,87 @@ * ---------------------------------------------------------------------- * admin: list all defined redirections * ---------------------------------------------------------------------- + * 2022-02-03 v0.1 <axel.hahn@iml.unibe.ch> initial version + * ---------------------------------------------------------------------- */ require_once 'classes/redirect.admin.class.php'; - - $oR=new redirectadmin(); - $sHtml=''; -// echo '<pre>'.print_r($oR->getHosts(), 1).'</pre>'; +// ---------------------------------------------------------------------- +// FUNCTIONS +// ---------------------------------------------------------------------- -/* +function getId($sDomain){ + return 'id_'.md5($sDomain); +} - [aum-biss.iml.unibe.ch] => Array - ( - [type] => alias - [target] => aum-biss.unibe.ch - [ip] => 130.92.30.62 - ) - ... - [chirosurf.ch] => Array - ( - [type] => config - [file] => /media/sf_htdocs/dev.links.iml.unibe.ch/public_html/classes/../../config/redirects_chirosurf.ch.json - [ip] => 130.92.30.62 - [aliases] => Array - ( - [0] => www.chirosurf.ch - ) +// ---------------------------------------------------------------------- +// MAIN +// ---------------------------------------------------------------------- - [redirects] => Array - ( - [direct] => Array - ( - ) +if (!$oR->isEnabled()){ + $sHtml.='<div class="error">Admin is disabled.</div>'; +} else { - [regex] => Array - ( - [.*] => Array - ( - [code] => 307 - [target] => https://chirosurf.elearning.aum.iml.unibe.ch - ) + // ---------- GET CONFIG DATA - ) + $aHosts=$oR->getHosts(); - ) + // ---------- SHOW ERRORS - ) -*/ -$aHosts=$oR->getHosts(); -if(count($aHosts['_errors'])) { - $sHtml.= '<h2>Found errors</h2>' - .'<ol class="error">' - .'<li>' . implode('</li></li>', $aHosts['_errors']).'</li>' - .'</ol>' - ; - unset($aHosts['_errors']); -} - -function getId($sDomain){ - return 'id_'.md5($sDomain); -} + if(count($aHosts['_errors'])) { + $sHtml.= '<h2>Found errors</h2>' + .'<ol class="error">' + .'<li>' . implode('</li></li>', $aHosts['_errors']).'</li>' + .'</ol>' + ; + unset($aHosts['_errors']); + } -$sHtml.='<h2>Domains and their redirects</h2>' -.'<table class="mydatatable"><thead> -<tr> -<th>Host</th> -<th>Setup</th> -<th>Type</th> -<th>From</th> -<th>Code</th> -<th>Target</th> -</tr> -</thead><tbody>'; -foreach($aHosts as $sHost => $aCfg){ - $sTdFirst='<tr class="cfgtype-'.$aCfg['type'].'"><td>'.$sHost.'</td><td>'.$aCfg['type'].'</td>'; - if (isset($aCfg['redirects'])){ - $iCount=0; - foreach(['direct', 'regex'] as $sType){ - if (count($aCfg['redirects'][$sType])){ - foreach($aCfg['redirects'][$sType] as $sFrom=>$aTo){ - $iCount++; - $sHtml.=$sTdFirst - .'<td class="type-'.$sType.'">'.$sType.'</td>' - .'<td class="type-'.$sType.'">'.$sFrom.'</td>' - .'<td class="http-'.$aTo['code'].'">'.$aTo['code'].'</td>' - .'<td><a href="'.$aTo['target'].'" target="_blank">'.$aTo['target'].'</a></td>' - .'</tr>'; + // ---------- LOOP OVER ALL ENTRIES + + $sHtml.='<h2>Domains and their redirects</h2>' + .'<table class="mydatatable"><thead> + <tr> + <th>Host</th> + <th>Setup</th> + <th>Type</th> + <th>From</th> + <th>Code</th> + <th>Target</th> + </tr> + </thead><tbody>'; + foreach($aHosts as $sHost => $aCfg){ + $sTdFirst='<tr class="cfgtype-'.$aCfg['type'].'"><td>'.$sHost.'</td><td>'.$aCfg['type'].'</td>'; + if (isset($aCfg['redirects'])){ + $iCount=0; + foreach(['direct', 'regex'] as $sType){ + if (count($aCfg['redirects'][$sType])){ + foreach($aCfg['redirects'][$sType] as $sFrom=>$aTo){ + $iCount++; + $sHtml.=$sTdFirst + .'<td class="type-'.$sType.'">'.$sType.'</td>' + .'<td class="type-'.$sType.'">'.$sFrom.'</td>' + .'<td class="http-'.$aTo['code'].'">'.$aTo['code'].'</td>' + .'<td><a href="'.$aTo['target'].'" target="_blank">'.$aTo['target'].'</a></td>' + .'</tr>'; + } } + } - + } else { + // type = alias + // $sHtml.='<tr>'.$sTdFirst.'<td></td><td></td><td></td><td>'.(isset($aCfg['target']) ? 'see config for <a href="#'.getId($aCfg['target']).'">'.$aCfg['target'].'</a>' : '').'</td></tr>'; + $sHtml.=$sTdFirst.'<td></td><td></td><td></td><td>'.(isset($aCfg['target']) ? 'see config for <em>'.$aCfg['target'].'</em>' : '').'</td></tr>'; } - } else { - // type = alias - // $sHtml.='<tr>'.$sTdFirst.'<td></td><td></td><td></td><td>'.(isset($aCfg['target']) ? 'see config for <a href="#'.getId($aCfg['target']).'">'.$aCfg['target'].'</a>' : '').'</td></tr>'; - $sHtml.=$sTdFirst.'<td></td><td></td><td></td><td>'.(isset($aCfg['target']) ? 'see config for <em>'.$aCfg['target'].'</em>' : '').'</td></tr>'; + } - + $sHtml.='</tbody></table>'; } -$sHtml.='</tbody></table>'; + +// ---------- OUTPUT ?><!doctype html> <html> @@ -125,7 +105,7 @@ $sHtml.='</tbody></table>'; <style> a{color:royalblue;} body{background: #f8f8f8; color: #234;} - ol.error{background: #fcc;} + .error{background: #fcc;} pre{background: rgba(0,0,0,0.05);padding: 0.3em; border: 1px solid rgba(0,0,0,0.1)} .cfgtype-alias{color:#89a; } .http-301::after{color:#a55; content: ' (Moved Permanently)'} diff --git a/public_html/classes/redirect.admin.class.php b/public_html/classes/redirect.admin.class.php index 0cf1335fbba3614ec455b64ce96956f78aebbcf3..2dc8acc8b5ea6ddd3957effaf2dac461f066ab96 100644 --- a/public_html/classes/redirect.admin.class.php +++ b/public_html/classes/redirect.admin.class.php @@ -15,6 +15,7 @@ require_once 'redirect.class.php'; * if the definition exists * ---------------------------------------------------------------------- * 2020-05-11 v1.4 ah rewrite as class + * 2022-02-03 v1.5 ah add method isEnabled */ /** @@ -24,6 +25,11 @@ require_once 'redirect.class.php'; */ class redirectadmin extends redirect { + public function isEnabled(){ + $sFile2Enable=__DIR__ . '/'.basename(__FILE__).'_enabled.txt'; + return file_exists($sFile2Enable); + } + public function getHosts(){ $aReturn = array(); $aErrors = array(); diff --git a/public_html/classes/redirect.admin.class.php_enabled.txt.dist b/public_html/classes/redirect.admin.class.php_enabled.txt.dist new file mode 100644 index 0000000000000000000000000000000000000000..3aa8f09773428624c7aa3ad2327e8dd94b19ad43 --- /dev/null +++ b/public_html/classes/redirect.admin.class.php_enabled.txt.dist @@ -0,0 +1,7 @@ +# redirect.admin.class.php_enabled.txt + +By default this file does not exist. +It means: the redirect admin is disabled. + +Rename the *dist file and remove the extension ".dist" +to enable the admin \ No newline at end of file