Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
URL expand
{
"require": {
"predis/predis": "~1"
}
}
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "8b51fc183ca7b77379ac47fcf29dfa95",
"packages": [
{
"name": "predis/predis",
"version": "v1.0.1",
"source": {
"type": "git",
"url": "https://github.com/nrk/predis.git",
"reference": "7a170b3d8123c556597b4fbdb88631f99de180c2"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/nrk/predis/zipball/7a170b3d8123c556597b4fbdb88631f99de180c2",
"reference": "7a170b3d8123c556597b4fbdb88631f99de180c2",
"shasum": ""
},
"require": {
"php": ">=5.3.2"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"suggest": {
"ext-curl": "Allows access to Webdis when paired with phpiredis",
"ext-phpiredis": "Allows faster serialization and deserialization of the Redis protocol"
},
"type": "library",
"autoload": {
"psr-4": {
"Predis\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Daniele Alessandri",
"email": "suppakilla@gmail.com",
"homepage": "http://clorophilla.net"
}
],
"description": "Flexible and feature-complete PHP client library for Redis",
"homepage": "http://github.com/nrk/predis",
"keywords": [
"nosql",
"predis",
"redis"
],
"time": "2015-01-02 12:51:34"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": []
}
<?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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment