-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
142 lines (138 loc) · 5.28 KB
/
index.php
File metadata and controls
142 lines (138 loc) · 5.28 KB
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
<?php
class ShortLink {
/**
* Please enter the correct DB information.
* @var string
*/
const DB_IP = '127.0.0.1';
const DB_ID = 'hakase';
const DB_PW = 'hakase';
const DB_NAME = 'hakase';
/**
* mysqli Connect data.
* @var class
*/
private $mysqli;
/**
* Connect DB
*/
public function __construct() {
$this->mysqli = @new mysqli(self::DB_IP, self::DB_ID, self::DB_PW, self::DB_NAME);
if ($this->mysqli->connect_errno) die('DB Connect ERROR!!!');
}
/**
* Generate a random string, using a cryptographically secure
* pseudorandom number generator (random_int)
*
* For PHP 7, random_int is a PHP core function
* For PHP 5.x, depends on https://github.com/paragonie/random_compat
*
* @param int $length How many characters do we want?
* @param string $keyspace A string of all possible characters
* to select from
* @return string
*/
private function random_str($length, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
{
$pieces = [];
$max = mb_strlen($keyspace, '8bit') - 1;
for ($i = 0; $i < $length; ++$i) {
$pieces []= $keyspace[random_int(0, $max)];
}
return implode('', $pieces);
}
/**
* DB Select Function
* @param string $text Short URL data
* @param string $url URL data (optional)
* @return array Output DB data
*/
private function Select($text, $url = NULL) {
if (!is_null($text)) $text = htmlspecialchars($text, ENT_QUOTES);
if (!is_null($url)) $url = htmlspecialchars($url, ENT_QUOTES);
return $this->mysqli->query('select randstr, url from shortlink where url = \'' . $url . '\' or randstr = \'' . $text . '\';')->fetch_array(MYSQLI_ASSOC);
}
/**
* Create a short URL address.
* @param string $url URL data
* @param string $text Short URL data (optional)
* @return array/null When a valid value comes in,
* it returns the source address and the short URL.
* If it is not a valid value, it returns NULL.
*/
public function Make($url, $text = NULL) {
if (!is_NULL($text) &&
(strlen($text) > 16 || strlen($text < 3))) return 1;
$parse = parse_url($url);
switch($parse['scheme'])
{
case 'http':
case 'https':
break;
default:
return 2;
}
if (is_null($parse['host'])) return 2;
$select = $this->Select($text, $url);
$url = htmlspecialchars($url, ENT_QUOTES);
if ($text === NULL)
$text = $this->random_str(rand(3, 16));
if (!is_null($select['url']) &&
$url === $select['url']) {
return array('randstr' => $select['randstr'], 'url' => $select['url']);
}
if (!is_null($select['randstr']) &&
$select['randstr'] === $text &&
$select['url'] !== $url)
return NULL;
if (!is_null($select['randstr']))
return $this->Make($url);
$text = htmlspecialchars($text, ENT_QUOTES);
return ($this->mysqli->query('INSERT INTO shortlink(randstr, url) VALUES (\'' . $text . '\', \'' . $url . '\');')) ? array('randstr' => $text, 'url' => $url) : 0;
}
/**
* Returns the short URL as the source URL.
* @param string $text Short URL
* @return string/null Returns the source URL,
* if there is a source URL, or NULL if there is no source URL.
*/
public function LinkCheck($text) {
$data = $this->Select($text);
return (!is_null($data['url']) && !is_null($data['randstr'])) ? $data['url'] : NULL;
}
/**
* Move the source URL to the page.
* @param string Short URL
* @return null
*/
public function move_header($text) {
$data = $this->LinkCheck($text);
if (!is_null($data)) {
header($_SERVER['SERVER_PROTOCOL'] . ' 301 Moved Permanently');
header('Location: ' . htmlspecialchars_decode($data, ENT_QUOTES));
die('<!doctype html><html><head><title>Move</title></head><body><a href="' . $data . '">Move</a></body></html>');
}
return NULL;
}
}
$short = new ShortLink();
if ($_POST['url']) {
$data = $short->Make($_POST['url'], $_POST['str']);
if ($data === 1) die('Short');
else if ($data === 2) die('This is not a valid URL address.');
else if (!is_null($_POST['str']) &&
(strlen($_POST['str']) > 16 || strlen($_POST['str']) < 3)) die('Short URL must be between 3 and 16 characters long.');
else if ($data['randstr'] !== $_POST['str'] &&
!is_null($_POST['str']) &&
$data['url'] === $_POST['url']) die('Already registered - https://' . $_SERVER['HTTP_HOST'] . '/' . $data['randstr']);
else if (!is_null($data['url'])) die('https://' . $_SERVER['HTTP_HOST'] . '/' . $data['randstr']);
else die('Already registered short URL.');
}
if (strlen($_SERVER['QUERY_STRING']) >= 1)
$short->move_header($_SERVER['QUERY_STRING']);
?>
<form method="post">
<input type="text" name="url" id="url" value="https://hakase.io/">
<input type="submit">
</form>
Hakase's Short Link Service