Last active
October 10, 2019 13:41
-
-
Save milouse/11104731f2184c4cf84376e9b5fecd85 to your computer and use it in GitHub Desktop.
Small script intended to expand short URL passed as a GET parameter.
This file contains 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 | |
/** | |
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
* Version 2, December 2004 | |
* | |
* Copyright (C) 2004 Sam Hocevar <[email protected]> | |
* | |
* Everyone is permitted to copy and distribute verbatim or modified | |
* copies of this license document, and changing it is allowed as long | |
* as the name is changed. | |
* | |
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
* | |
* 0. You just DO WHAT THE FUCK YOU WANT TO. | |
*/ | |
$tinydomains = array( | |
array('a.sgdf.fr', 'French scouts and guides association'), | |
array('bddy.me', ''), | |
array('bit.ly', ''), | |
array('buff.ly', ''), | |
array('dlvr.it', ''), | |
array('ebx.sh', ''), | |
array('fb.me', 'Facebook'), | |
array('frama.link', 'Framasoft'), | |
array('frphn.co', 'Fairphone'), | |
array('goo.gl', 'Google'), | |
array('h2fr.fr', 'Hauts de France, a France administrative region'), | |
array('huit.re', 'Framasoft'), | |
array('is.gd', ''), | |
array('kck.st', 'Kickstarter'), | |
array('lemde.fr', 'Le Monde, a french newspaper'), | |
array('lnkd.in', 'Linkedin / Microsoft'), | |
array('mzl.la', 'Mozilla'), | |
array('ow.ly', ''), | |
array('po.st', ''), | |
array('t.co', 'Twitter'), | |
array('tinyurl.com', ''), | |
array('u.afp.com', 'AFP, the french press agency. Redir. to fr. newspapers'), | |
array('ur1.ca', 'Old identi.ca shortening services'), | |
array('vdn.lv', 'La Voix du Nord, a french newspaper') | |
); | |
if (isset($_GET['json'])) { | |
header('Content-Type: application/json'); | |
if ($_GET['json'] == 'info') { | |
$json_d = array_map(function ($i) { | |
return '{"domain":"' . $i[0] . '","info":"' . $i[1] . '"}'; | |
}, $tinydomains); | |
echo '[' . join(', ', $json_d) . ']'; | |
} else { | |
$json_d = array_map(function ($i) { return $i[0]; }, $tinydomains); | |
echo '["' . join('", "', $json_d) . '"]'; | |
} | |
exit; | |
} else if (isset($_GET['url'])) { | |
$tinyurl = $_GET['url']; | |
$pipedtd = join('|', array_map( | |
function ($i) { return $i[0]; }, $tinydomains)); | |
$tinyregexp = "/^https?:\/\/(?:$pipedtd)\/[a-zA-Z0-9_]+$/"; | |
if (preg_match($tinyregexp, $tinyurl) !== 1) { | |
echo $tinyurl; | |
exit; | |
} | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $tinyurl); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($ch, CURLOPT_FORBID_REUSE, true); | |
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); | |
curl_setopt($ch, CURLOPT_HEADER, true); | |
curl_setopt($ch, CURLOPT_NOBODY, true); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_MAXREDIRS, 3); | |
$output = curl_exec($ch); | |
curl_close($ch); | |
if (preg_match_all('/^location: (https?:\/\/.*)/im', $output, $matches) !== false) { | |
$tinyurl = array_pop($matches[1]); | |
} | |
echo $tinyurl; | |
exit; | |
} | |
?> | |
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<title>Unshorten tiny URLs</title> | |
<h1>Unshorten tiny URLs</h1> | |
<form action="unshorten.php" method="get"> | |
<fieldset> | |
<label for="tiny_url_field"> | |
Short URL: | |
</label> | |
<br> | |
<input name="url" type="text" | |
id="tiny_url_field" style="width:300px;" | |
placeholder="https://t.co/QmQGE3SgEJ"> | |
<input type="submit" value="Expand"> | |
</fieldset> | |
</form> | |
<p style="font-size: smaller;color: gray;"> | |
<?php | |
$dl = array_map(function ($i) { | |
if ($i[1] == '') return $i[0]; | |
return $i[0] . ' (' . $i[1] . ')'; | |
}, $tinydomains); | |
?> | |
(Supported services: <?php echo join(', ', $dl) ?>) | |
</p> | |
<h2>API</h2> | |
<dl> | |
<dt><code>/unshorten.php?json</code></dt> | |
<dd>gives you the list of supported domains as an array.</dd> | |
<dt><code>/unshorten.php?json=info</code></dt> | |
<dd>gives you the list of supported domains, as an array of objects, | |
with the domain in the <code>domain</code> property and | |
complementary information in the <code>info</code> property.</dd> | |
</dl> | |
<footer> | |
<p style="font-size: smaller;"> | |
Author : <a href="https://etienne.depar.is">Étienne Deparis</a> – | |
License <a href="http://www.wtfpl.net/txt/copying/">WTFPL</a> – | |
<a href="https://gist.github.com/milouse/11104731f2184c4cf84376e9b5fecd85">source</a> | |
</p> | |
</footer> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment