|
<?php |
|
|
|
require_once __DIR__.'/vendor/autoload.php'; |
|
|
|
/** |
|
* @see http://php.net/manual/fr/function.parse-url.php#106731 |
|
*/ |
|
function unparse_url($parsed_url, $with_fragment = false) |
|
{ |
|
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; |
|
$host = isset($parsed_url['host']) ? $parsed_url['host'] : ''; |
|
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; |
|
$user = isset($parsed_url['user']) ? $parsed_url['user'] : ''; |
|
$pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : ''; |
|
$pass = ($user || $pass) ? "$pass@" : ''; |
|
$path = isset($parsed_url['path']) ? $parsed_url['path'] : ''; |
|
$query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; |
|
$fragment = $with_fragment && isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; |
|
return "$scheme$user$pass$host$port$path$query$fragment"; |
|
} |
|
|
|
function getRedirection($url) |
|
{ |
|
$ch = curl_init(); |
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
curl_setopt($ch, CURLOPT_HEADER, true); |
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); |
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
curl_setopt($ch, CURLOPT_USERAGENT, 'Expand Short URL | Simon Leblanc'); |
|
|
|
$data = curl_exec($ch); |
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
$redirection = curl_getinfo($ch, CURLINFO_REDIRECT_URL); |
|
curl_close($ch); |
|
|
|
if ($http_code !== 301 && $http_code !== 302) { |
|
return false; |
|
} |
|
|
|
return $redirection; |
|
} |
|
|
|
function getFromRedis(Predis\Client $predis, $url) |
|
{ |
|
return $predis->get($url); |
|
} |
|
|
|
function saveInRedis(Predis\Client $predis, $url, $redirection) |
|
{ |
|
if (null === $redirection || false === $redirection) { |
|
return; |
|
} |
|
|
|
$predis->set($url, $redirection); |
|
} |
|
|
|
$url = null; |
|
$redirection = null; |
|
$error = null; |
|
|
|
if (isset($_GET['url']) === true && empty($_GET['url']) === false) { |
|
$datas = parse_url($_GET['url']); |
|
|
|
if (false === $datas) { |
|
$error = 'Bad URL !'; |
|
} else { |
|
if (isset($datas['scheme']) === false || empty($datas['scheme']) === true) { |
|
$datas['scheme'] = 'http'; |
|
} |
|
|
|
if ('http' !== $datas['scheme'] && 'https' !== $datas['scheme']) { |
|
$error = 'HTTP Url only !'; |
|
} else { |
|
$predis = new Predis\Client(null, ['prefix' => 'expand-url:']); |
|
|
|
$url = unparse_url($datas); |
|
if (null === ($redirection = getFromRedis($predis, $url))) { |
|
$redirection = getRedirection($url); |
|
saveInRedis($predis, $url, $redirection); |
|
} |
|
} |
|
} |
|
} |
|
?><!DOCTYPE html> |
|
<html lang="fr"> |
|
<head> |
|
<meta charset="utf-8" /> |
|
<title>Expand URL</title> |
|
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"> |
|
<style type="text/css"> |
|
label{display:none} |
|
.form-inline .form-control{width:100%} |
|
.form-group.col-md-11,.form-group.col-xs-10{padding-left:0} |
|
pre{display:inline-block;width:calc(100% - 50px);margin-right:8px} |
|
a{display:inline-block;float:right;margin-top:3px;} |
|
</style> |
|
</head> |
|
<body> |
|
<div class="container-fluid"> |
|
<div class="row"> |
|
<div class="col-md-12"> |
|
<h1>Expand<?php if (null !== $url) { echo ' : '.htmlentities($url, ENT_HTML5, 'UTF-8'); } ?></h1> |
|
</div> |
|
</div> |
|
<?php if (null !== $error) { ?> |
|
<div class="row"> |
|
<div class="col-md-12"> |
|
<div class="alert alert-danger"><?php echo htmlentities($error, ENT_HTML5, 'UTF-8') ?></div> |
|
</div> |
|
</div> |
|
<?php } ?> |
|
<div class="row"> |
|
<div class="col-md-12"> |
|
<?php if (null === $url) { ?> |
|
<form action="" method="get" role="form" class="form-inline"> |
|
<div class="form-group col-md-11 col-xs-10"> |
|
<label for="url">URL</label> |
|
<input type="text" name="url" id="url" class="form-control" autofocus placeholder="Enter url to expand"> |
|
</div> |
|
<button type="submit" class="btn btn-success"> |
|
<i class="glyphicon glyphicon-search"></i> |
|
</button> |
|
</form> |
|
<?php } elseif (false === $redirection) { ?> |
|
<pre>Aucune redirection disponible</pre> |
|
<?php } elseif (null !== $redirection) { ?> |
|
<pre><?php echo htmlentities($redirection, ENT_HTML5, 'UTF-8') ?></pre> |
|
<a href="<?php echo $_SERVER['PHP_SELF'].'?url='.urlencode($redirection) ?>" class="btn btn-success"> |
|
<i class="glyphicon glyphicon-search"></i></a> |
|
<?php } else { ?> |
|
<div class="alert alert-warning">Maybe, no result...</div> |
|
<?php } ?> |
|
</div> |
|
</div> |
|
</div> |
|
</body> |
|
</html> |