This commit is contained in:
Phyks 2013-10-09 14:00:00 +01:00
parent 83ef0ea335
commit ac6f01a524
6 changed files with 104 additions and 99 deletions

4
README.md Normal file
View File

@ -0,0 +1,4 @@
shortURL
========
Forked from http://git.cphyc.me/cgit.cgi/tinyURL

View File

Before

Width:  |  Height:  |  Size: 40 KiB

After

Width:  |  Height:  |  Size: 40 KiB

5
config.example.php Normal file
View File

@ -0,0 +1,5 @@
<?php
define('DATA_DIR', );
define('ASSOC_NAME', );
define('BASE_URL', );
define('SAVED_URL', );

View File

@ -1,41 +1,41 @@
<?php <?php
include('.config/include.php'); include('config.php');
if (is_readable($_CONFIG['DATA_DIR'].$_CONFIG['ASSOC_NAME'])) if (is_readable(DATA_DIR.ASSOC_NAME))
{ {
$rawData = file_get_contents($_CONFIG['DATA_DIR'].$_CONFIG['ASSOC_NAME']); $rawData = file_get_contents(DATA_DIR.ASSOC_NAME);
$data = unserialize($rawData); $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 || count($_GET) < 1) if (count($_GET) != 1) {
{ if (isset($_GET['add']) && !empty($_GET['url'])) {
if (isset($_GET['add']) && $_GET['url'])
$default_url = htmlspecialchars($_GET['url']); $default_url = htmlspecialchars($_GET['url']);
else }
else {
$default_url = ""; $default_url = "";
}
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="fr"> <html lang="fr">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<title>Réduis moi!</title> <title>Shorten me</title>
<link rel="stylesheet" media="screen" type="text/css" href="misc/design.css" /> <link rel="stylesheet" media="screen" type="text/css" href="design.css" />
<link rel="icon" href="favicon.ico" />
</head> </head>
<body> <body>
<h1>C'est trop long ...</h1> <h1>It's too long…</h1>
<form method="post" action="process.php"> <form method="post" action="process.php">
<p> <p>
<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">Raccourci (optionnel): </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"/>
</p> </p>
<p><input type="submit" value="... raccourcir !"/></p> <p><input type="submit" value="Shorten !"/></p>
<p>Ajouter ce lien pour réduire les URL en un seul clic : <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 $_CONFIG['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');})();">Réduis moi !</a>
</p> </p>
</form> </form>
</body> </body>
@ -43,16 +43,13 @@ if (count($_GET) > 1 || count($_GET) < 1)
<?php <?php
} }
// 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
$get = each($_GET); $get = each($_GET);
$short = $get['key']; $short = $get['key'];
$url = $_CONFIG['BASE_URL']; $url = BASE_URL;
foreach($data as $array) foreach($data as $array) {
{ if ($array['short'] == $short) {
if ($array['short'] == $short)
{
$url = $array['url']; $url = $array['url'];
break; break;
} }

View File

@ -1,11 +1,10 @@
<?php <?php
include('.config/include.php'); include('config.php');
// Add a new line to $data // Add a new line to $data
function add($that) function add($that) {
{
global $_CONFIG; global $_CONFIG;
global $data; global $data;
if (count($data) >= $_CONFIG['SAVED_URL']) if (count($data) >= SAVED_URL)
{ {
// Delete the first element // Delete the first element
array_shift($data); array_shift($data);
@ -15,59 +14,59 @@ function add($that)
return $data; return $data;
} }
if(empty($_POST['url'])) if(empty($_POST['url'])) {
{
header('location : message.php?m=1'); header('location : message.php?m=1');
} }
else else {
{ if (is_readable(DATA_DIR.ASSOC_NAME))
if (is_readable($_CONFIG['DATA_DIR'].$_CONFIG['ASSOC_NAME'])) $rawData = file_get_contents(DATA_DIR.ASSOC_NAME);
$rawData = file_get_contents($_CONFIG['DATA_DIR'].$_CONFIG['ASSOC_NAME']);
else else
{ {
touch($_CONFIG['DATA_DIR'].$_CONFIG['ASSOC_DIR']); touch(DATA_DIR.ASSOC_DIR);
$rawData = ""; $rawData = "";
} }
if (empty($rawData)) if (empty($rawData))
$data = array(); $data = array();
else else
$data = unserialize($rawData); $data = unserialize($rawData);
} }
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="fr"> <html lang="fr">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<title>Réduit moi !</title> <title>Shorten me !<title>
<link rel="stylesheet" media="screen" type="text/css" href="misc/design.css" /> <link rel="stylesheet" media="screen" type="text/css" href="design.css" />
<link rel="icon" href="favicon.ico" />
</head> </head>
<body> <body>
<h1>C'était trop 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']);
else }
else {
$short = dechex(crc32($_POST['url'])); $short = dechex(crc32($_POST['url']));
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 // 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($_CONFIG['DATA_DIR'].$_CONFIG['ASSOC_NAME'], serialize($data)); file_put_contents(DATA_DIR.ASSOC_NAME, serialize($data));
// Echoes the result // Echoes the result
$new_url = $_CONFIG['BASE_URL'].'/?'.$short; $new_url = $BASE_URL.'/?'.$short;
?> ?>
<p>Votre raccourci :<br/> <p>Your shorten URL:<br/>
<b><a href="<?php echo $new_url ?>"><?php echo $new_url; ?></a></b></p><p>Réduction de : <?php echo $url; ?> <?php <strong><a href="<?php echo $new_url ?>"><?php echo $new_url; ?></a></strong>
}
else
{
echo "Url manquant. <a href='index.php'>Retour à l'acceuil</a>.";
}?>
</p> </p>
<p>Short link for:<?php echo '<a href="'.$url.'">'.$url.'</a>'; ?></p>
<?php
}
else {
echo "<p>Missing URL. <a href='index.php'>Back to index page</a>.</p>";
}
?>
</body> </body>
</html> </html>