forked from mathiasbynens/php-url-shortener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshorten.php
102 lines (85 loc) · 2.75 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
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
<?php
require('config.php');
header('Content-Type: text/plain;charset=UTF-8');
$url = isset($_GET['url']) ? urldecode(trim($_GET['url'])) : '';
if (in_array($url, array('', 'about:blank', 'undefined', 'http://localhost/'))) {
die('Enter a URL.');
}
function nextLetter(&$str) {
$str = ('z' === $str ? 'a' : ++$str);
}
function getNextShortURL($s) {
$a = str_split($s);
$c = count($a);
if (preg_match('/^z*$/', $s)) { // string consists entirely of `z`
return str_repeat('a', $c + 1);
}
while ('z' === $a[--$c]) {
nextLetter($a[$c]);
}
nextLetter($a[$c]);
return implode($a);
}
switch(DB_FLAVOR) {
case "mysql":
$dsn = DB_FLAVOR . ":dbname=" . DB_DATABASE . ";host=" . DB_HOST;
break;
case "sqlite":
$dsn = DB_FLAVOR . ":" . DB_DATABASE;
break;
default:
exit("Unsupported database.");
break;
}
try {
$db = new PDO($dsn, DB_USER, DB_PASSWORD);
} catch (PDOException $e) {
exit("Database connection error: " . $e->getMessage(). "\n");
}
if (DB_FLAVOR == "sqlite") {
$row = $db->query("select name from sqlite_master where type = 'table' and name = 'redirect'")->fetch();
if ( ! $row) {
$create_table = "CREATE TABLE 'redirect' ("
. "'slug' varchar(14) NOT NULL, "
. "'url' varchar(620) NOT NULL, "
. "'date' datetime NOT NULL, "
. "'hits' bigint(20) NOT NULL default '0', "
. "PRIMARY KEY ('slug') "
. ");";
$first_entry = "INSERT INTO 'redirect' VALUES ('a', 'http://www.example.com', datetime('now', '-1 day'), 1);";
$db->query($create_table);
$db->query($first_entry);
}
}
if (DB_FLAVOR == "mysql") $db->query('SET NAMES "utf8"');
$lookup_stmt = $db->prepare("SELECT slug FROM redirect WHERE url = :url LIMIT 1");
$lookup_stmt->bindParam(':url', $url);
$lookup_stmt->execute();
$result = $lookup_stmt->fetch();
if ($result) { // If there’s already a short URL for this URL
exit(SHORT_URL . $result['slug']);
} else {
$result = $db->query("SELECT slug FROM redirect ORDER BY date DESC LIMIT 1")->fetch();
if ($result) {
$slug = getNextShortURL($result['slug']);
switch (DB_FLAVOR) {
case "mysql":
$insert_stmt = $db->prepare("INSERT INTO redirect(slug, url, date, hits) VALUES (:slug, :url, NOW(), 0)");
break;
case "sqlite":
$insert_stmt = $db->prepare("INSERT INTO redirect(slug, url, date, hits) VALUES (:slug, :url, datetime('now', 'localtime'), 0)");
break;
default:
break;
}
$insert_stmt->bindParam(':slug', $slug);
$insert_stmt->bindParam(':url', $url);
$r = $insert_stmt->execute();
if ($r) {
header('HTTP/1.1 201 Created');
echo SHORT_URL . $slug;
if (DB_FLAVOR == "mysql") $db->query('OPTIMIZE TABLE `redirect`');
}
}
}
?>