-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
147 lines (125 loc) · 4.78 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
<?php
/*~ Greco395 Simple Url Shorter
.---------------------------------------------------------------------------.
| Software: Simple Url Shorter |
| Version: 0.9.1 |
| Site: https://greco395.com/ |
| ------------------------------------------------------------------------- |
| Author: Greco Domenico (project admininistrator & Author) |
| Founder: Greco395.com |
| ------------------------------------------------------------------------- |
| This program is distributed in the hope that it will be useful - WITHOUT |
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| FITNESS FOR A PARTICULAR PURPOSE. |
'---------------------------------------------------------------------------'
*/
//////// CHANGE THE FOLLOWING VARIABLES /////////
// Website URL ( add the "/" at the end )
$website_name = "http://example.com/";
// Database configuration
$dbHost = "db_host";
$dbUsername = "db_user";
$dbPassword = "db_pass";
$dbName = "db_name";
////////////////////////////////////////////////
// Create database connection
try{
$db = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUsername, $dbPassword);
}catch(PDOException $e){
echo "Connection failed: " . $e->getMessage();
}
class URL_SHORTER{
public $domain;
protected static $chars = "abcdfghjkmnpqrstvwxyz|ABCDFGHJKLMNPQRSTVWXYZ|0123456789";
protected static $table = "short_urls";
protected static $checkUrlExists = false;
public function __construct(){
global $website_name;
$this->domain = $website_name;
}
public function validateUrl($url){
if (strpos($url, $this->domain) !== false) {
return false;
}
return filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED);
}
public function checkUrl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
$response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return (!empty($response) && $response != 404);
}
function urlExistsInDB($url){
global $db;
$query = "SELECT short_code FROM ".self::$table." WHERE long_url = :long_url LIMIT 1";
$stmt = $db->prepare($query);
$params = array(
"long_url" => $url
);
$stmt->execute($params);
$result = $stmt->fetch();
return (empty($result)) ? false : $result["short_code"];
}
public function generateRandomString($length = 6){
$sets = explode('|', self::$chars);
$all = '';
$randString = '';
foreach($sets as $set){
$randString .= $set[array_rand(str_split($set))];
$all .= $set;
}
$all = str_split($all);
for($i = 0; $i < $length - count($sets); $i++){
$randString .= $all[array_rand($all)];
}
$randString = str_shuffle($randString);
return strtoupper($randString);
}
public function insertUrlInDB($url, $code){
global $db;
$query = "INSERT INTO `short_urls` (`id`, `long_url`, `short_code`, `hits`, `created`) VALUES (null, '".$url."', '".$code."', 0, ".time().");";
$stmnt = $db->exec($query);
return $db->lastInsertId();
}
public function infoID($id){
global $db;
$stmt = $db->prepare("SELECT * FROM short_urls WHERE id=?");
$stmt->execute([$id]);
return $stmt->fetch();
}
public function shortize($long_url){
if(isset($long_url)){
$isset_urlDB = self::urlExistsInDB($long_url);
if($isset_urlDB != true){
if(self::checkUrlExists == true){
if($this->checkUrl($long_url) != true){
return "INVALID WEBSITE";
}
}
if($this->validateUrl($long_url)){
return $this->domain.self::infoID(self::insertUrlInDB($long_url, self::generateRandomString(4)))['short_code'];
}else{
return "INVALID URL";
}
}else{
return $this->domain.$isset_urlDB;
}
}
}
}
$shorty = new URL_SHORTER;
// REDIRECT SHORT URL TO LONG URL
if(isset($_GET) && !isset($_GET['new'])){
foreach($_GET as $rq){
if(strlen($rq) < 5 && strlen($rq) > 3){
header("Location: ".$shorty->infoCODE($rq)['long_url']."");
}
}
}
// EXAMPLE
// echo $shorty->shortize("https://greco395.com");
//