2013-10-06 15:00:00 +02:00
|
|
|
<?php
|
2013-10-10 15:00:00 +02:00
|
|
|
require('config.php');
|
2013-10-11 15:00:00 +02:00
|
|
|
|
|
|
|
function sort_array(&$array, $key, $order=SORT_DESC) {
|
|
|
|
$sort_keys = array();
|
|
|
|
|
|
|
|
foreach ($array as $key2 => $entry) {
|
|
|
|
$sort_keys[$key2] = $entry[$key];
|
2013-10-09 15:00:00 +02:00
|
|
|
}
|
2013-10-11 15:00:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
return array_multisort($sort_keys, $order, $array);
|
2013-10-06 15:00:00 +02:00
|
|
|
}
|
|
|
|
|
2013-10-09 15:00:00 +02:00
|
|
|
if(empty($_POST['url'])) {
|
2013-10-10 15:00:00 +02:00
|
|
|
header('location : index.php');
|
2013-10-09 15:00:00 +02:00
|
|
|
}
|
|
|
|
else {
|
2013-10-10 15:00:00 +02:00
|
|
|
if (is_readable(DATA_FILE)) {
|
2013-10-11 15:00:00 +02:00
|
|
|
$data = unserialize(gzinflate(file_get_contents(DATA_FILE)));
|
2013-10-10 15:00:00 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$data = array();
|
2013-10-09 15:00:00 +02:00
|
|
|
}
|
2013-10-06 15:00:00 +02:00
|
|
|
}
|
|
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="fr">
|
2013-10-09 15:00:00 +02:00
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
2013-10-11 15:00:00 +02:00
|
|
|
<title>Shorten me !</title>
|
2013-10-12 15:00:00 +02:00
|
|
|
<style type="text/css">
|
|
|
|
body {
|
|
|
|
text-align: center;
|
|
|
|
background-color: #333;
|
|
|
|
color: #DDD;
|
|
|
|
}
|
|
|
|
|
|
|
|
a:link, a:visited, a:hover, a:active {
|
|
|
|
color: yellow;
|
|
|
|
}
|
|
|
|
</style>
|
2013-10-09 15:00:00 +02:00
|
|
|
</head>
|
|
|
|
<body>
|
2013-10-12 15:00:00 +02:00
|
|
|
<h1><a href="index.php">It was too long !</a></h1>
|
2013-10-06 15:00:00 +02:00
|
|
|
<?php
|
2013-10-09 15:00:00 +02:00
|
|
|
if (isset($_POST['short']) && $_POST['short'] != "") {
|
|
|
|
$short = htmlspecialchars($_POST['short']);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$short = dechex(crc32($_POST['url']));
|
|
|
|
}
|
|
|
|
if (isset($_POST['url']) && $_POST['url'] != "") {
|
|
|
|
$url = htmlspecialchars($_POST['url']);
|
|
|
|
$array = array("url"=>$url, "short"=>$short);
|
2013-10-11 15:00:00 +02:00
|
|
|
|
|
|
|
if (count($data) >= MAX_SAVED_URLS)
|
|
|
|
{
|
|
|
|
// Delete the first element
|
|
|
|
sort_array($data, 'timestamp');
|
|
|
|
array_shift($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store short link in the data array
|
|
|
|
$data[$short] = array('timestamp'=>time(), 'url'=>$url);
|
|
|
|
|
2013-10-09 15:00:00 +02:00
|
|
|
// Save it in the file
|
2013-10-10 15:00:00 +02:00
|
|
|
file_put_contents(DATA_FILE, gzdeflate(serialize($data)));
|
2013-10-11 15:00:00 +02:00
|
|
|
|
2013-10-09 15:00:00 +02:00
|
|
|
// Echoes the result
|
2013-10-11 15:00:00 +02:00
|
|
|
$new_url = BASE_URL.'/?'.$short;
|
2013-10-09 15:00:00 +02:00
|
|
|
?>
|
|
|
|
<p>Your shorten URL:<br/>
|
|
|
|
<strong><a href="<?php echo $new_url ?>"><?php echo $new_url; ?></a></strong>
|
|
|
|
</p>
|
2013-10-11 15:00:00 +02:00
|
|
|
<p>Short link for: <?php echo '<a href="'.$url.'">'.$url.'</a>'; ?></p>
|
2013-10-09 15:00:00 +02:00
|
|
|
<?php
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
echo "<p>Missing URL. <a href='index.php'>Back to index page</a>.</p>";
|
|
|
|
}
|
2013-10-06 15:00:00 +02:00
|
|
|
?>
|
2013-10-09 15:00:00 +02:00
|
|
|
</body>
|
2013-10-06 15:00:00 +02:00
|
|
|
</html>
|