Last active
November 8, 2018 21:52
-
-
Save liuxd/4f68cca155f26da75013bdbbc7b5c2c6 to your computer and use it in GitHub Desktop.
Web Ladder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace ladder; | |
class Ladder | |
{ | |
public function main() | |
{ | |
$sURL = $this->getURL(); | |
if (empty($sURL)) { | |
$this->showIndex(); | |
} else { | |
$sContent = $this->getPage($sURL); | |
$this->show($sContent, $sURL); | |
} | |
} | |
private function showIndex() | |
{ | |
echo <<<EOT | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>X-Ladder</title> | |
</head> | |
<body> | |
<style> | |
body { | |
background-color: black; | |
} | |
#url { | |
margin-top: 20%; | |
padding: 10px; | |
border: solid white 5px; | |
font-size:30px; | |
background-color: black; | |
color: white; | |
} | |
</style> | |
<center><input type="text" id="url" size=30></center> | |
<script> | |
(function(){ | |
var input = document.getElementById('url'); | |
input.focus(); | |
document.onkeydown = function(event){ | |
var e = event || window.event || arguments.callee.caller.arguments[0]; | |
if (e && e.keyCode == 13) { | |
var url = input.value; | |
if (url.substr(0, 4) !== 'http') { | |
url = 'http://' + url; | |
} | |
location.href = '?url=' + url; | |
} | |
}; | |
})(); | |
</script> | |
</body> | |
</html> | |
<!-- | |
EOT; | |
} | |
private function getURL() | |
{ | |
return isset($_GET['url']) ? $_GET['url'] : ''; | |
} | |
private function getPage($sURL) | |
{ | |
return !empty($sURL) ? file_get_contents($sURL) : ''; | |
} | |
private function show($sContent, $sURL) | |
{ | |
$tmp = explode('.', $sURL); | |
$ext = end($tmp); | |
$bSendHeader = $this->sendHeader($ext); | |
if (!$bSendHeader) { | |
$sContent = str_replace('charset=GBK', 'charset=utf8', $sContent); | |
$sContent = $this->processHTML($sContent, $sURL); | |
} | |
if (($pos = strpos($sURL, '?')) !== false) { | |
$url = substr($sURL, 0, $pos); | |
$tmp = explode('.', $url); | |
$ext = end($tmp); | |
$this->sendHeader($ext); | |
} | |
$sContent = gzencode($sContent, 9); | |
header("Content-Encoding: gzip"); | |
header("Vary: Accept-Encoding"); | |
header("Content-Length: ".strlen($sContent)); | |
echo $sContent; | |
echo "<!--"; | |
} | |
private function processHTML($sContent, $sURL) | |
{ | |
$aURLInfo = parse_url($sURL); | |
preg_match_all('/ href=([\'"]).+?(\1)/', $sContent, $matches_href); | |
foreach ($matches_href[0] as $match) { | |
$url_href = trim(ltrim($match, ' href='), '"|\''); | |
if ($url_href{0} === '#') { | |
continue; | |
} | |
$sNewURL = ''; | |
if ($url_href{0} === '/') { | |
$sNewURL = $aURLInfo['scheme'] . '://' . $aURLInfo['host'] . $url_href; | |
} else if (substr($url_href, 0, 4) === 'http') { | |
$sNewURL = $url_href; | |
} else { | |
$sNewURL = $aURLInfo['scheme'] . '://' . $aURLInfo['host'] . '/' . $url_href; | |
} | |
if (!empty($sNewURL)) { | |
$sPlacement = ' href="http://' . $_SERVER['HTTP_HOST'] . '?url=' . $sNewURL . '"'; | |
$sContent = str_replace($match, $sPlacement, $sContent); | |
} | |
} | |
preg_match_all('/ src=([\'"]).+?(\1)/', $sContent, $matches_src); | |
foreach ($matches_src[0] as $match) { | |
$url_src = trim(ltrim($match, ' src='), '"|\''); | |
if (substr($url_src, 0, 2) === '//') { | |
$sNewURL = $aURLInfo['scheme'] . ':' . $url_src; | |
} else if ($url_src{0} === '/') { | |
$sNewURL = $aURLInfo['scheme'] . '://' . $aURLInfo['host'] . $url_src; | |
} else if (substr($url_src, 0, 4) === 'http') { | |
$sNewURL = $url_src; | |
} else { | |
$sNewURL = $aURLInfo['scheme'] . '://' . $aURLInfo['host'] . '/' . $url_src; | |
} | |
$sPlacement = ' src="http://' . $_SERVER['HTTP_HOST'] . '?url=' . $sNewURL . '"'; | |
$sContent = str_replace($match, $sPlacement, $sContent); | |
} | |
preg_match_all('/ data-original=([\'"]).+?(\1)/', $sContent, $matches_original); | |
foreach ($matches_original[0] as $match) { | |
$url_original = trim(ltrim($match, ' data-orginal='), '"|\''); | |
$sPlacement = ' data-original="http://' . $_SERVER['HTTP_HOST'] . '?url=' . $url_original . '"'; | |
$sContent = str_replace($match, $sPlacement, $sContent); | |
} | |
return $sContent; | |
} | |
private function sendHeader($sExt) | |
{ | |
$aMineTypeList = [ | |
'js' => 'application/x-javascript', | |
'css' => 'text/css', | |
'jpg' => 'image/jpeg', | |
'png' => 'image/png', | |
'gif' => 'image/gif', | |
'svg' => 'text/xml' | |
]; | |
$bReturn = false; | |
if (isset($aMineTypeList[$sExt])) { | |
header('Content-Type: ' . $aMineTypeList[$sExt]); | |
$bReturn = true; | |
} | |
return $bReturn; | |
} | |
} | |
(new Ladder)->main(); | |
# end of this file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment