End of refactor
* Simplified storage * various bugfixes
This commit is contained in:
parent
de94f7312d
commit
5171fded3f
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
config.php
|
||||||
|
data
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
// The file to which we should store the data
|
// The file to which we should store the data
|
||||||
define('DATA_FILE', 'data');
|
define('DATA_FILE', 'data');
|
||||||
// The base URL with which you will access the script. Keep the trailing slash.
|
// The base URL with which you will access the script. No trailing slash.
|
||||||
define('BASE_URL', 'http://your.domain.tld/');
|
define('BASE_URL', 'http://localhost/tinyURL');
|
||||||
// Max number of URLs to keep
|
// Max number of URLs to keep
|
||||||
define('MAX_SAVED_URLS', 100);
|
define('MAX_SAVED_URLS', 100);
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
require('config.php');
|
require('config.php');
|
||||||
|
|
||||||
if (is_readable(DATA_FILE))
|
if (is_readable(DATA_FILE))
|
||||||
{
|
{
|
||||||
$data = unserialize(gzinflate(file_get_contents(DATA_FILE)));
|
$data = unserialize(gzinflate(file_get_contents(DATA_FILE)));
|
||||||
@ -51,15 +52,11 @@ if (count($_GET) != 1) {
|
|||||||
// Else, we redirect the visitor to the right URL
|
// Else, we redirect the visitor to the right URL
|
||||||
else {
|
else {
|
||||||
// We get the shortened url
|
// We get the shortened url
|
||||||
// TODO
|
|
||||||
$get = each($_GET);
|
$get = each($_GET);
|
||||||
$short = $get['key'];
|
$short = $get['key'];
|
||||||
$url = BASE_URL;
|
$url = BASE_URL;
|
||||||
foreach($data as $array) {
|
if (array_key_exists($short, $data)) {
|
||||||
if ($array['short'] == $short) {
|
$url = $data[$short]['url'];
|
||||||
$url = $array['url'];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// $url is now index.php if no element was found, the right url if found
|
// $url is now index.php if no element was found, the right url if found
|
||||||
header('location:'.$url);
|
header('location:'.$url);
|
||||||
|
39
process.php
39
process.php
@ -1,16 +1,15 @@
|
|||||||
<?php
|
<?php
|
||||||
require('config.php');
|
require('config.php');
|
||||||
// Add a new line to $data
|
|
||||||
function add($that) {
|
function sort_array(&$array, $key, $order=SORT_DESC) {
|
||||||
global $data;
|
$sort_keys = array();
|
||||||
if (count($data) >= MAX_SAVED_URLS)
|
|
||||||
{
|
foreach ($array as $key2 => $entry) {
|
||||||
// Delete the first element
|
$sort_keys[$key2] = $entry[$key];
|
||||||
array_shift($data);
|
|
||||||
}
|
}
|
||||||
// Add that to the array
|
|
||||||
array_push($data, $that);
|
|
||||||
return $data;
|
return array_multisort($sort_keys, $order, $array);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(empty($_POST['url'])) {
|
if(empty($_POST['url'])) {
|
||||||
@ -18,7 +17,7 @@ if(empty($_POST['url'])) {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (is_readable(DATA_FILE)) {
|
if (is_readable(DATA_FILE)) {
|
||||||
$data = unserialize(gzinflate(file_get_contents(DATA_DIR.ASSOC_NAME)));
|
$data = unserialize(gzinflate(file_get_contents(DATA_FILE)));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$data = array();
|
$data = array();
|
||||||
@ -29,7 +28,7 @@ else {
|
|||||||
<html lang="fr">
|
<html lang="fr">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<title>Shorten me !<title>
|
<title>Shorten me !</title>
|
||||||
<link rel="stylesheet" media="screen" type="text/css" href="design.css" />
|
<link rel="stylesheet" media="screen" type="text/css" href="design.css" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@ -44,12 +43,22 @@ else {
|
|||||||
if (isset($_POST['url']) && $_POST['url'] != "") {
|
if (isset($_POST['url']) && $_POST['url'] != "") {
|
||||||
$url = htmlspecialchars($_POST['url']);
|
$url = htmlspecialchars($_POST['url']);
|
||||||
$array = array("url"=>$url, "short"=>$short);
|
$array = array("url"=>$url, "short"=>$short);
|
||||||
// Add the association at the end of $data array
|
|
||||||
$data = add($array);
|
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);
|
||||||
|
|
||||||
// Save it in the file
|
// Save it in the file
|
||||||
file_put_contents(DATA_FILE, gzdeflate(serialize($data)));
|
file_put_contents(DATA_FILE, gzdeflate(serialize($data)));
|
||||||
|
|
||||||
// Echoes the result
|
// Echoes the result
|
||||||
$new_url = $BASE_URL.'/?'.$short;
|
$new_url = BASE_URL.'/?'.$short;
|
||||||
?>
|
?>
|
||||||
<p>Your shorten URL:<br/>
|
<p>Your shorten URL:<br/>
|
||||||
<strong><a href="<?php echo $new_url ?>"><?php echo $new_url; ?></a></strong>
|
<strong><a href="<?php echo $new_url ?>"><?php echo $new_url; ?></a></strong>
|
||||||
|
Loading…
Reference in New Issue
Block a user