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,58 +1,55 @@
<?php
include('.config/include.php');
if (is_readable($_CONFIG['DATA_DIR'].$_CONFIG['ASSOC_NAME']))
include('config.php');
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);
}
else
$data = array();
// If we don't have exactly one $_GET arg, we print a default page
if (count($_GET) > 1 || count($_GET) < 1)
{
if (isset($_GET['add']) && $_GET['url'])
if (count($_GET) != 1) {
if (isset($_GET['add']) && !empty($_GET['url'])) {
$default_url = htmlspecialchars($_GET['url']);
else
}
else {
$default_url = "";
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Réduis moi!</title>
<link rel="stylesheet" media="screen" type="text/css" href="misc/design.css" />
<link rel="icon" href="favicon.ico" />
</head>
<body>
<h1>C'est trop long ...</h1>
<form method="post" action="process.php">
<p>
<label for="url">URL : </label><input type="text" size="50" name="url" id="url" value="<?php echo $default_url; ?>"/>
</p>
<p>
<label for="short">Raccourci (optionnel): </label><input type="short" size="50" name="short" id="short"/>
</p>
<p><input type="submit" value="... raccourcir !"/></p>
<p>Ajouter ce lien pour réduire les URL en un seul clic :
<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>
</p>
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Shorten me</title>
<link rel="stylesheet" media="screen" type="text/css" href="design.css" />
</head>
<body>
<h1>It's too long…</h1>
<form method="post" action="process.php">
<p>
<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"/>
</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>
</p>
</form>
</body>
</html>
<?php
}
// Else, we redirect the visitor to the right URL
else
{
else {
// We get the shortened url
$get = each($_GET);
$short = $get['key'];
$url = $_CONFIG['BASE_URL'];
foreach($data as $array)
{
if ($array['short'] == $short)
{
$url = BASE_URL;
foreach($data as $array) {
if ($array['short'] == $short) {
$url = $array['url'];
break;
}

View File

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