-
Notifications
You must be signed in to change notification settings - Fork 1
/
shorturl.php
executable file
·194 lines (156 loc) · 4.72 KB
/
shorturl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/** Simple Url Shortener
* Copyright (C) 2010 Hyacinthe Cartiaux <hyacinthe.cartiaux@free.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
error_reporting(E_ALL);
/* Class used to convert base 10 id to base 66 code used in URI */
require_once("Base10Convert.php");
/* Config */
$TITLE = "Générateur d'adresses courtes";
$SITE = "http://b4n.fr"; // Full path to the script
$PAGE_CONTENT = "";
$SOURCE = "http://www.0xf.fr/shorturl.tar.bz2";
$BLK_SOURCE = "https://github.com/blankoworld/Simple-URL-Shortener";
$FILE = basename($_SERVER['PHP_SELF']);
$DBFILE = "index.db"; /* basename($FILE, ".php") . ".db";*/
/* Database object */
if (!file_exists($DBFILE))
{
/* Create the DB*/
$dbHandle = new SQLiteDatabase($DBFILE, 0666, &$error);
createdb($dbHandle);
}
if (!isset($dbHandle))
$dbHandle = new SQLiteDatabase($DBFILE);
/* Short Functions */
/* Create table shorturl in database, called if the database does not exist */
function createdb($dbHandle)
{
$sql = "CREATE TABLE shorturl (
id INTEGER AUTOINCREMENT,
url VARCHAR NOT NULL,
Primary Key(id)
);";
return $dbHandle->queryExec($sql);
}
/* Get the URL from DB with the code */
function geturl($dbHandle, $code)
{
$code = sqlite_escape_string($code);
try {
$convert = new Base10Convert();
$id = $convert->decode($code);
}
catch (Exception $e)
{
return FALSE;
}
$sql = "SELECT url
FROM shorturl
WHERE id='". $id ."';";
$result = $dbHandle->query($sql);
if ($result->numRows() == 1)
return $result->fetchSingle();
else
return FALSE;
}
/* Insert the URL in DB and return its code or FALSE */
function shortenurl($dbHandle, $url)
{
// Check if the url is already in the database
$sql = "SELECT COUNT(*)
FROM shorturl
WHERE url='". $url ."';";
$result = $dbHandle->query($sql);
$count = $result->fetchSingle();
// if there is no record for the url, we insert it
if ($count == 0)
{
/* Very basic URL validation */
if(!preg_match("!^[a-z0-9]*://.*!i", $url))
return FALSE;
$url = sqlite_escape_string($url);
$sql = "INSERT INTO shorturl (url)
VALUES ('" . $url . "');";
$dbHandle->queryExec($sql);
}
// and we get the id
$sql = "SELECT id
FROM shorturl
WHERE url='". $url ."';";
$result = $dbHandle->query($sql);
$id = $result->fetchSingle();
try {
$convert = new Base10Convert();
return $convert->encode((int) $id);
}
catch (Exception $e)
{
return FALSE;
}
}
$PAGE_CONTENT = "";
if (isset($_GET['p']))
{
if ($url = geturl($dbHandle, $_GET['p']))
{
header("Location: ". $url);
exit();
}
else
{
$PAGE_CONTENT .= "<h2 class=\"error\">Cette adresse n'existe pas !</h2>";
}
}
else if (isset($_POST['url']))
{
if ($code = shortenurl($dbHandle, $_POST['url']))
{
$PAGE_CONTENT .= '<h2 class="success">'. $SITE . '/' . $code .'</h2>';
}
else
{
$PAGE_CONTENT .= "<h2 class=\"error\">Erreur durant la procédure de raccourcissement de l'URL soumise !</h2>";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $TITLE ?></title>
<link href="style.css" rel="stylesheet" title="default" type="text/css" />
</head>
<body>
<div id="container">
<h1>Générateur d'adresses courtes</h1>
<?php echo $PAGE_CONTENT ?>
<form action="#" method="post">
<fieldset>
<label>Entrez une adresse : </label>
<input type="text" name="url" value="http://" />
<input type="submit" value="La rendre plus courte !" />
</fieldset>
</form>
<h4>
Propulsé par <a href="<?php echo $SOURCE ?>">Simple Url Shortener</a> <br />
Minimalist PHP script using SQLite, GNU AGPL V3<br />
© 2010 Hyacinthe Cartiaux <hyacinthe.cartiaux (a) free.fr><br />
Modifié sur <a href="<?php echo $BLK_SOURCE ?>">Github</a>.
Thème par <a href="http://m.b4n.fr/">Blankoworld</a>
</h4>
</div>
</body>
</html>