* 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 <?php
define('DATA_DIR', ); // The file to which we should store the data
define('ASSOC_NAME', ); define('DATA_FILE', 'data');
define('BASE_URL', ); // The base URL with which you will access the script. Keep the trailing slash.
define('SAVED_URL', ); 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 <?php
include('config.php'); require('config.php');
if (is_readable(DATA_DIR.ASSOC_NAME)) if (is_readable(DATA_FILE))
{ {
$rawData = file_get_contents(DATA_DIR.ASSOC_NAME); $data = unserialize(gzinflate(file_get_contents(DATA_FILE)));
$data = unserialize($rawData);
} }
else else
$data = array(); $data = array();
// If we don't have exactly one $_GET arg, we print a default page // If we don't have exactly one $_GET arg, we print a default page
if (count($_GET) != 1) { 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']); $default_url = htmlspecialchars($_GET['url']);
} }
else { else {
$default_url = ""; $default_url = '';
}
if (!empty($_POST['short'])) {
$default_short = htmlspecialchars($_GET['short']);
}
else {
$default_short = '';
} }
?> ?>
<!DOCTYPE html> <!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; ?>"/> <label for="url">URL: </label><input type="text" size="50" name="url" id="url" value="<?php echo $default_url; ?>"/>
</p> </p>
<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>
<p><input type="submit" value="Shorten !"/></p> <p><input type="submit" value="Shorten !"/></p>
<p>Add this link to your bookmarks to shorten links in one click ! <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> </p>
</form> </form>
</body> </body>
@ -45,6 +51,7 @@ 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;

View File

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