-
Notifications
You must be signed in to change notification settings - Fork 1
/
shorten.php
49 lines (42 loc) · 1.14 KB
/
shorten.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
<?php
require('db_funcs.php');
require('shortening_funcs.php');
/* Base url - end with a forward slash */
$BASEURL = "http://examp.le/";
/* Regexp to prevent recursive shortening/cyclic redirections */
$REGEXP = "/^http:\/\/(www.)?examp\.le(\/)+(shorten\.php(\/)*\?d=)?[a-zA-Z0-9]+$/";
/* Database vars */
$DBURL = "";
$DBNAME = "";
$DBUSER = "";
$DBPASS = "";
//Do we encode?
if (isset($_GET["e"])) {
if (preg_match($REGEXP, $_GET["e"]))
$forwardingAddress = "http://www.google.com";
else {
connect($DBURL, $DBNAME, $DBUSER, $DBPASS);
$id = insertURL($_GET["e"]);
echo "$BASEURL" . encode($id, $alphabet);
disconnect();
}
}
//Or decode?
else if (isset($_GET["d"])) {
connect($DBURL, $DBNAME, $DBUSER, $DBPASS);
$id = decode($_GET["d"], $alphabet);
$forwardingAddress = retrieveURL($id);
if (!isset($forwardingAddress))
$forwardingAddress = "http://www.google.com";
disconnect();
}
//Else redirect somewhere
else {
$forwardingAddress = "http://www.google.com";
}
//Forward if no encoding
if (isset($forwardingAddress)) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: $forwardingAddress");
}
?>