/
index.php
155 lines (132 loc) · 4.07 KB
/
index.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
<?php
define('ROOT_DIR', realpath(__DIR__));
define('SHORT_URL_LENGTH', 8); // 52 ^ 8 = 53459728531456 combinations. [a-zA-Z]
require_once('config.php');
require_once('db.php');
app();
function app() {
$uri_arr = explode('?', $_SERVER['REQUEST_URI']);
$uri = $uri_arr[0];
if ($uri == '/') {
ob_start();
require(ROOT_DIR .'/view/main.php');
$content = ob_get_clean();
require(ROOT_DIR . '/layout.php');
return;
} else if (strpos($_SERVER['REQUEST_URI'], '/ajax_make_short_url') === 0) {
ajax_make_short_url();
return;
}
$is_short_url = is_known_short_url($_SERVER['REQUEST_URI'], $redirect_url);
if (!$is_short_url) {
require_once(ROOT_DIR . '/404.php');
return;
}
//defalut behavior
make_redirect($redirect_url);
return;
}
/**
* checks if $url is known short URL, stored in a database.
* sets $redirect_url, if found.
*
* @return bool
*/
function is_known_short_url($url, &$redirect_url) {
global $pdo;
$short_url = ltrim($url, '/');
$stmt = $pdo->prepare('SELECT long_url FROM urls WHERE short_url=?');
$stmt->execute(array($short_url));
$long_url = $stmt->fetchColumn();
if ($long_url) {
$redirect_url = $long_url;
return true;
}
return false;
}
function make_redirect($redirect_url) {
header("Location: ". $redirect_url);
die;
}
function ajax_make_short_url() {
$response = array();
$response['error'] = array();
if (!isset($_REQUEST['long_url'])) {
$response['error'][] = 'long_url not defined';
echo json_encode($response); die;
}
$short_url = make_short_url($_REQUEST['long_url'], $response['error']);
if (!$short_url) {
$response['error'][] = 'Sorry, we were unable to make short URL.';
echo json_encode($response); die;
}
$response['short_url'] = $short_url;
echo json_encode($response); die;
}
/**
* Creates unique short URL and stores it in database
*
* @return short URL; Or NULL on error;
*/
function make_short_url($long_url, &$error) {
global $pdo;
$url = $long_url;
preg_match('%^[a-z]+://%', $url, $matches);
if (!isset($matches[0])) {
$url = 'http://' . $url;
}
if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
$error[] = 'not a valid URL.';
return null;
}
//check if such long URL already in database
$stmt = $pdo->prepare('SELECT short_url FROM urls WHERE long_url=?');
$stmt->execute(array($url));
$short_url = $stmt->fetchColumn();
if (!$short_url) { //create record
$short_url = generate_short_url();
try {
$stmt = $pdo->prepare('INSERT INTO urls(long_url, short_url, created) VALUES (?, ?, ?)');
$params = array($url, $short_url, date('Y-m-d H:i:s'));
$stmt->execute($params);
} catch (Exception $e) {
$error[] = $e->getMessage();
return null;
}
}
$result = 'http://' . rtrim($_SERVER['HTTP_HOST'], '/') . '/' . $short_url;
return $result;
}
/**
* generates unique short url. (Not URL but URI actually)
*
* @return string;
*/
function generate_short_url() {
global $pdo;
//we can deviate this set. So no ASCII ranges, please.
$chars_set = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$set_length = strlen($chars_set);
$result_url = '';
for ($i=0; $i < SHORT_URL_LENGTH; $i++) {
$rand_pos = mt_rand(0, $set_length - 1);
$char = substr($chars_set, $rand_pos, 1);
$result_url .= $char;
}
//now, need to check, if URL not preserved in db earlier. Though, the probability is low...
$rows_found_num = $pdo->query("SELECT COUNT(id) FROM urls WHERE short_url='{$result_url}'")->fetchColumn();
if ($rows_found_num) { //very unlikely
$result_url = generate_short_url();
}
return $result_url;
}
function pr($val) {
echo '<pre>';
print_r($val);
echo '</pre>';
}
function vd($val) {
echo '<pre>';
var_dump($val);
echo '</pre>';
}