From 5171fded3f803fd6e97bf97cc15e74be524156d8 Mon Sep 17 00:00:00 2001 From: Phyks Date: Fri, 11 Oct 2013 14:00:00 +0100 Subject: [PATCH] End of refactor * Simplified storage * various bugfixes --- .gitignore | 2 ++ config.example.php | 4 ++-- index.php | 9 +++------ process.php | 41 +++++++++++++++++++++++++---------------- 4 files changed, 32 insertions(+), 24 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ece00d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +config.php +data diff --git a/config.example.php b/config.example.php index 71dbad9..7ea51cb 100644 --- a/config.example.php +++ b/config.example.php @@ -1,7 +1,7 @@ = MAX_SAVED_URLS) - { - // Delete the first element - array_shift($data); + +function sort_array(&$array, $key, $order=SORT_DESC) { + $sort_keys = array(); + + foreach ($array as $key2 => $entry) { + $sort_keys[$key2] = $entry[$key]; } - // Add that to the array - array_push($data, $that); - return $data; + + + return array_multisort($sort_keys, $order, $array); } if(empty($_POST['url'])) { @@ -18,7 +17,7 @@ if(empty($_POST['url'])) { } else { if (is_readable(DATA_FILE)) { - $data = unserialize(gzinflate(file_get_contents(DATA_DIR.ASSOC_NAME))); + $data = unserialize(gzinflate(file_get_contents(DATA_FILE))); } else { $data = array(); @@ -29,7 +28,7 @@ else { - Shorten me !<title> + <title>Shorten me ! @@ -44,17 +43,27 @@ else { if (isset($_POST['url']) && $_POST['url'] != "") { $url = htmlspecialchars($_POST['url']); $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 file_put_contents(DATA_FILE, gzdeflate(serialize($data))); + // Echoes the result - $new_url = $BASE_URL.'/?'.$short; + $new_url = BASE_URL.'/?'.$short; ?>

Your shorten URL:

-

Short link for:'.$url.''; ?>

+

Short link for: '.$url.''; ?>