Refactor
This commit is contained in:
parent
83ef0ea335
commit
ac6f01a524
4
README.md
Normal file
4
README.md
Normal file
@ -0,0 +1,4 @@
|
||||
shortURL
|
||||
========
|
||||
|
||||
Forked from http://git.cphyc.me/cgit.cgi/tinyURL
|
Before Width: | Height: | Size: 40 KiB After Width: | Height: | Size: 40 KiB |
5
config.example.php
Normal file
5
config.example.php
Normal file
@ -0,0 +1,5 @@
|
||||
<?php
|
||||
define('DATA_DIR', );
|
||||
define('ASSOC_NAME', );
|
||||
define('BASE_URL', );
|
||||
define('SAVED_URL', );
|
43
index.php
43
index.php
@ -1,41 +1,41 @@
|
||||
<?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" />
|
||||
<title>Shorten me</title>
|
||||
<link rel="stylesheet" media="screen" type="text/css" href="design.css" />
|
||||
</head>
|
||||
<body>
|
||||
<h1>C'est trop long ...</h1>
|
||||
<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; ?>"/>
|
||||
<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"/>
|
||||
<label for="short">Shortcut (optional): </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><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>
|
||||
@ -43,16 +43,13 @@ if (count($_GET) > 1 || count($_GET) < 1)
|
||||
<?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;
|
||||
}
|
||||
|
57
process.php
57
process.php
@ -1,11 +1,10 @@
|
||||
<?php
|
||||
include('.config/include.php');
|
||||
include('config.php');
|
||||
// Add a new line to $data
|
||||
function add($that)
|
||||
{
|
||||
function add($that) {
|
||||
global $_CONFIG;
|
||||
global $data;
|
||||
if (count($data) >= $_CONFIG['SAVED_URL'])
|
||||
if (count($data) >= SAVED_URL)
|
||||
{
|
||||
// Delete the first element
|
||||
array_shift($data);
|
||||
@ -15,59 +14,59 @@ function add($that)
|
||||
return $data;
|
||||
}
|
||||
|
||||
if(empty($_POST['url']))
|
||||
{
|
||||
if(empty($_POST['url'])) {
|
||||
header('location : message.php?m=1');
|
||||
}
|
||||
else
|
||||
{
|
||||
if (is_readable($_CONFIG['DATA_DIR'].$_CONFIG['ASSOC_NAME']))
|
||||
$rawData = file_get_contents($_CONFIG['DATA_DIR'].$_CONFIG['ASSOC_NAME']);
|
||||
else {
|
||||
if (is_readable(DATA_DIR.ASSOC_NAME))
|
||||
$rawData = file_get_contents(DATA_DIR.ASSOC_NAME);
|
||||
else
|
||||
{
|
||||
touch($_CONFIG['DATA_DIR'].$_CONFIG['ASSOC_DIR']);
|
||||
touch(DATA_DIR.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" />
|
||||
<title>Shorten me !<title>
|
||||
<link rel="stylesheet" media="screen" type="text/css" href="design.css" />
|
||||
</head>
|
||||
<body>
|
||||
<h1>C'était trop long !</h1>
|
||||
<h1>It was too long !</h1>
|
||||
<?php
|
||||
|
||||
if (isset($_POST['short']) && $_POST['short'] != "")
|
||||
if (isset($_POST['short']) && $_POST['short'] != "") {
|
||||
$short = htmlspecialchars($_POST['short']);
|
||||
else
|
||||
}
|
||||
else {
|
||||
$short = dechex(crc32($_POST['url']));
|
||||
if (isset($_POST['url']) && $_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));
|
||||
file_put_contents(DATA_DIR.ASSOC_NAME, serialize($data));
|
||||
// Echoes the result
|
||||
$new_url = $_CONFIG['BASE_URL'].'/?'.$short;
|
||||
$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>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>
|
||||
|
Loading…
Reference in New Issue
Block a user