From d8c9e4a12f9a36d05dd2b511424b6147c155bc5a Mon Sep 17 00:00:00 2001 From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch> Date: Mon, 20 Jan 2025 14:12:15 +0100 Subject: [PATCH] Update infoblock for found redirects; handle and show cookies --- public_html/admin/main.css | 7 +- public_html/classes/redirect.admin.class.php | 73 +++++++++++++++++--- public_html/classes/redirect.class.php | 2 +- 3 files changed, 69 insertions(+), 13 deletions(-) diff --git a/public_html/admin/main.css b/public_html/admin/main.css index 68f2c94..ecc416b 100644 --- a/public_html/admin/main.css +++ b/public_html/admin/main.css @@ -30,6 +30,9 @@ --txt-warning-bg: #fda; --txt-warning-color: #651; + --txt-redirect-bg: #e0e8f0; + --txt-redirect-color: default; + --txt-alias-color: #89a; --http-301-color: #a55; @@ -100,7 +103,7 @@ ol.error{padding-left: 2em;} .status{padding: 0.5em 1em; border-left: 5px solid; font-size: 125%; border-top-right-radius: 0.7em;} .status-ok{background: var(--txt-ok-bg); color: var(--txt-ok-color);} -.status-redirect{background: var(--txt-warning-bg); color: var(--txt-warning-color);} +.status-redirect{background: var(--txt-redirect-bg); color: var(--txt-redirect-color);} .status-error{background: var(--txt-error-bg); color: var(--txt-error-color);} .statuscode{background: var(--txt-statuscode-bg); font-size: 150%;} @@ -108,6 +111,8 @@ ol.error{padding-left: 2em;} .location::before{content:' 🌐 '; background-color: var(--txt-location-before-bg);} .debug{color:var(--txt-debug-color);} +.allJumps{display: flex; overflow: auto; padding-bottom: 1em; margin-bottom: 2em;;} +.allJumps div.redirectstatus{display: inline-block;margin-top: 1em;} .box{display: inline-block; border: 1px solid rgba(0,0,0,0.1); padding: 0.3em; margin: 0em; text-align: center; border-radius: 0.25em;} .overlay{position: fixed; margin: 0; width: 100%; height: 100%; top: 0; left: 0; background: var(--overlay-bg); overflow: scroll; display: none;} diff --git a/public_html/classes/redirect.admin.class.php b/public_html/classes/redirect.admin.class.php index 93517c9..01d3c1e 100644 --- a/public_html/classes/redirect.admin.class.php +++ b/public_html/classes/redirect.admin.class.php @@ -20,6 +20,7 @@ require_once 'redirect.class.php'; * 2023-08-28 v1.8 ah remove php warning if there is no config yet * 2024-10-04 v1.9 ah php8 only: typed variables * 2025-01-13 v1.10 ah fetch curl error + * 2025-01-20 v1.11 ah Update infoblock for found redirects; handle and show cookies */ /** @@ -30,6 +31,12 @@ require_once 'redirect.class.php'; class redirectadmin extends redirect { + /** + * Filename to collect cookies of a request + * @var string + */ + protected string $_sCookiefile=''; + /** * Get default curl options * @return array @@ -49,9 +56,6 @@ class redirectadmin extends redirect 'DNT: 1', ], - // TODO: this is unsafe .. better: let the user configure it - // CURLOPT_SSL_VERIFYHOST => true, - // CURLOPT_SSL_VERIFYPEER => false, CURLOPT_TIMEOUT => 5, ]; return $aReturn; @@ -66,11 +70,19 @@ class redirectadmin extends redirect public function httpGet(string $url, bool $bHeaderOnly = false): array { $aResult = []; - $ch = curl_init($url); foreach ($this->_getCurlOptions() as $sCurlOption => $sCurlValue) { curl_setopt($ch, $sCurlOption, $sCurlValue); } + + // handle cookies + $this->_sCookiefile = sys_get_temp_dir() . '/redirect_admin__found_cookies__' . md5($url) . '.txt'; + if(file_exists($this->_sCookiefile)){ + unlink($this->_sCookiefile); + } + curl_setopt($ch, CURLOPT_COOKIEFILE, $this->_sCookiefile); + curl_setopt($ch, CURLOPT_COOKIEJAR, $this->_sCookiefile); + if ($bHeaderOnly) { curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_NOBODY, 1); @@ -103,6 +115,31 @@ class redirectadmin extends redirect return $aResult; } + /** + * Get html code to show found cookies. + * The cookie file will be deleted too. + * + * @return string + */ + public function renderCookies(): string{ + $sReturn = ''; + $iCounter=0; + if(file_exists($this->_sCookiefile)) + { + $lines = explode(PHP_EOL, file_get_contents($this->_sCookiefile)); + foreach ($lines as $line) { + if (substr_count($line, "\t") == 6) { + $iCounter++; + $sReturn.= $line . '<br>'; + } + } + + unlink($this->_sCookiefile); + } + + return $sReturn ? "Found cookies: $iCounter<pre>$sReturn</pre>" : ''; + } + /** * Get html code for a response header of a request * @@ -117,6 +154,8 @@ class redirectadmin extends redirect $iJump = 0; $sBox = ''; $sReturn = ''; + $aHosts = []; + $aWebs = []; if ($aResponse['curlerrorcode']) { $sReturn .= '<br>' @@ -130,6 +169,7 @@ class redirectadmin extends redirect } $sUrl=$aResponse['url']; + foreach(explode("\r\n\r\n", $aResponse['response_header']."\r\n\r\n".$aResponse['response_body'] ) as $sBlock){ if(strlen($sBlock)){ $iJump++; @@ -160,19 +200,22 @@ class redirectadmin extends redirect $sBlock = preg_replace('/(location:.*)\\r/i', '<span class="location">$1</span>', $sBlock); - $sReturn.="<span class=\"status $sStatus\">$sUrl ($iStatus) </span><pre>$sBlock</pre>"; + $sReturn.="<span class=\"status $sStatus\">$sUrl ... $iStatus</span><pre>$sBlock</pre>"; // $sReturn .= '<strong>'.$iJump.') HTTP status: '.$iStatus.' - '.$sUrl.'</strong><pre>'.$sBlock.'</pre>'; $sWebhost=preg_replace('/_$/', '', parse_url($sUrl, PHP_URL_HOST)); + $sIp=$this->_getIp($sWebhost); + $aHosts[$sIp]=1; + $aWebs[$sWebhost]=1; $sBox.="<div class=\"box $sStatus\">" // .$sUrl.'<br>' .$sWebhost.'<br>' - . $this->_getIp($sWebhost) + . $sIp .'</div>'; if($sNextUrl){ - $sBox.= " --- $iStatus ---> "; + $sBox.= "<div class=\"redirectstatus\"><nobr> --- $iStatus ---> </nobr></div>"; $sUrl=$sNextUrl; } @@ -181,11 +224,19 @@ class redirectadmin extends redirect $iHops = $iJump-1; $sReturn = '<br>'.($iHops > 0 ? 'Found hops: <strong>' . $iHops . '</strong> ' - . ($iHops > 1 ? '<span class="warning"> ⚠️ Verify your redirect to skip unneeded hops.</span>' : '') . '<br><br>' - . '<div>'.$sBox.'</div><br><br><br>' + . ($iHops > 1 + ? '<span class="warning"> ⚠️ Verify your redirect to skip unneeded hops.</span><br>' + .'The configured redirect is not the final url - it continues redirecting from there.' + : '' + ) + . '<br>' + .sprintf('Required webs to be online to reach the final url: <strong>%s</strong>; required number of hosts: <strong>%s</strong>', count($aWebs), count($aHosts)) + . '<br><br>' + . '<div class="allJumps">'.$sBox.'</div>' : '' - ) - + ) + . $this->renderCookies() + . $sReturn; return $sReturn; diff --git a/public_html/classes/redirect.class.php b/public_html/classes/redirect.class.php index d97865d..aff4bc8 100644 --- a/public_html/classes/redirect.class.php +++ b/public_html/classes/redirect.class.php @@ -43,7 +43,7 @@ class redirect * About message * @var string */ - protected string $_version = '1.7'; + protected string $_version = '1.8'; /** * Flag: debug is enabled? -- GitLab