* gzip encoding
* simplified configuration
* various fixes
This commit is contained in:
Phyks 2013-10-10 14:00:00 +01:00
parent ac6f01a524
commit de94f7312d
3 changed files with 30 additions and 28 deletions

View File

@ -1,5 +1,7 @@
<?php
define('DATA_DIR', );
define('ASSOC_NAME', );
define('BASE_URL', );
define('SAVED_URL', );
// The file to which we should store the data
define('DATA_FILE', 'data');
// The base URL with which you will access the script. Keep the trailing slash.
define('BASE_URL', 'http://your.domain.tld/');
// Max number of URLs to keep
define('MAX_SAVED_URLS', 100);

View File

@ -1,20 +1,26 @@
<?php
include('config.php');
if (is_readable(DATA_DIR.ASSOC_NAME))
require('config.php');
if (is_readable(DATA_FILE))
{
$rawData = file_get_contents(DATA_DIR.ASSOC_NAME);
$data = unserialize($rawData);
$data = unserialize(gzinflate(file_get_contents(DATA_FILE)));
}
else
$data = array();
// If we don't have exactly one $_GET arg, we print a default page
if (count($_GET) != 1) {
if (isset($_GET['add']) && !empty($_GET['url'])) {
if (!empty($_POST['url']) || (isset($_GET['add']) && !empty($_GET['url']))) {
$default_url = htmlspecialchars($_GET['url']);
}
else {
$default_url = "";
$default_url = '';
}
if (!empty($_POST['short'])) {
$default_short = htmlspecialchars($_GET['short']);
}
else {
$default_short = '';
}
?>
<!DOCTYPE html>
@ -31,11 +37,11 @@ if (count($_GET) != 1) {
<label for="url">URL: </label><input type="text" size="50" name="url" id="url" value="<?php echo $default_url; ?>"/>
</p>
<p>
<label for="short">Shortcut (optional): </label><input type="short" size="50" name="short" id="short"/>
<label for="short">Shortcut (optional): </label><input type="short" size="50" name="short" id="short" value="<?php echo $default_short;?>"/>
</p>
<p><input type="submit" value="Shorten !"/></p>
<p>Add this link to your bookmarks to shorten links in one click !
<a href="javascript:javascript:(function(){var%20url%20=%20location.href;var%20title%20=%20document.title%20||%20url;window.open('<?php echo BASE_URL; ?>/?add&url='%20+%20encodeURIComponent(url),'_blank','menubar=no,height=390,width=600,toolbar=no,scrollbars=no,status=no,dialog=1');})();">Réduis moi !</a>
<a href="javascript:javascript:(function(){var%20url%20=%20location.href;var%20title%20=%20document.title%20||%20url;window.open('<?php echo BASE_URL; ?>/?add&url='%20+%20encodeURIComponent(url),'_blank','menubar=no,height=390,width=600,toolbar=no,scrollbars=no,status=no,dialog=1');})();">Short link</a>
</p>
</form>
</body>
@ -45,6 +51,7 @@ if (count($_GET) != 1) {
// Else, we redirect the visitor to the right URL
else {
// We get the shortened url
// TODO
$get = each($_GET);
$short = $get['key'];
$url = BASE_URL;

View File

@ -1,10 +1,9 @@
<?php
include('config.php');
require('config.php');
// Add a new line to $data
function add($that) {
global $_CONFIG;
global $data;
if (count($data) >= SAVED_URL)
if (count($data) >= MAX_SAVED_URLS)
{
// Delete the first element
array_shift($data);
@ -15,20 +14,15 @@ function add($that) {
}
if(empty($_POST['url'])) {
header('location : message.php?m=1');
header('location : index.php');
}
else {
if (is_readable(DATA_DIR.ASSOC_NAME))
$rawData = file_get_contents(DATA_DIR.ASSOC_NAME);
else
{
touch(DATA_DIR.ASSOC_DIR);
$rawData = "";
if (is_readable(DATA_FILE)) {
$data = unserialize(gzinflate(file_get_contents(DATA_DIR.ASSOC_NAME)));
}
else {
$data = array();
}
if (empty($rawData))
$data = array();
else
$data = unserialize($rawData);
}
?>
<!DOCTYPE html>
@ -41,7 +35,6 @@ else {
<body>
<h1>It was too long !</h1>
<?php
if (isset($_POST['short']) && $_POST['short'] != "") {
$short = htmlspecialchars($_POST['short']);
}
@ -54,7 +47,7 @@ if (isset($_POST['url']) && $_POST['url'] != "") {
// Add the association at the end of $data array
$data = add($array);
// Save it in the file
file_put_contents(DATA_DIR.ASSOC_NAME, serialize($data));
file_put_contents(DATA_FILE, gzdeflate(serialize($data)));
// Echoes the result
$new_url = $BASE_URL.'/?'.$short;
?>