.
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | General Information:
2 | --------------------
3 |
4 | This is a very simple URL shortener and redirector/dereferrer script previously used on my website. Due to regular abuse and blacklisting of my domain in several anti-malware solutions, this service is no longer available. Think twice before you add this script to a publicly available website.
5 |
6 | Installation Instructions:
7 | --------------------------
8 |
9 | Create a mysql database, user and add the table for storing link information
10 | ```
11 | mysql
12 | CREATE USER 'links'@'%' IDENTIFIED BY 'MY_PASSWORD';
13 | CREATE DATABASE links;
14 | use links;
15 | CREATE TABLE `link` (
16 | `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
17 | `url` varchar(2000) COLLATE latin1_bin NOT NULL,
18 | PRIMARY KEY (`id`)
19 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin;
20 | FLUSH PRIVILEGES;
21 | quit
22 | ```
23 |
24 | Upload the script to your web server and modify the constants at the top to match your setup.
25 |
26 | That's it, enjoy your new URL shortener :)
27 |
--------------------------------------------------------------------------------
/url.php:
--------------------------------------------------------------------------------
1 | PDO::ERRMODE_WARNING, PDO::ATTR_PERSISTENT=>true]);
18 | }catch(PDOException $e){
19 | }
20 | if(empty($_GET['id'])){
21 | $style = '.red{color:red}';
22 | send_headers([$style]);
23 | echo '';
24 | echo 'URL-Shortener/Redirector';
25 | echo '';
26 | echo '';
27 | echo '';
28 | echo '';
29 | echo 'URL-Shortener/Redirector
';
30 | echo 'Shorten a URL or strip referrers by redirecting via '.CANONICAL_URL.'?r=LINK
';
31 | if(!isset($db)){
32 | echo 'ERROR: No database connection!
';
33 | echo '';
34 | exit;
35 | }
36 | echo '
';
43 | echo '
';
52 | if(!empty($_REQUEST['info'])){
53 | $stmt=$db->prepare("SELECT url FROM link WHERE id=?;");
54 | $stmt->execute([$_REQUEST['info']]);
55 | if($url=$stmt->fetch(PDO::FETCH_ASSOC)){
56 | $url=$url['url'];
57 | echo 'Short link is: ".CANONICAL_URL."?id=$_REQUEST[info]
";
58 | echo "Redirects to: $url
";
59 | }else{
60 | echo 'Sorry, this redirect doesn\'t exist.
';
61 | }
62 | }elseif($_SERVER['REQUEST_METHOD']==='POST' && !empty($_POST['addr'])){
63 | if(!(
64 | // 1. all explicit schemes with whatever xxx://yyyyyyy
65 | preg_match('~^(\w*://[^\s<>]+)$~i', $_POST['addr'])
66 | // 2. valid URLs without scheme:
67 | || preg_match('~^((?:[^\s<>]*:[^\s<>]*@)?[a-z0-9\-]+(?:\.[a-z0-9\-]+)+(?::\d*)?/[^\s<>]*)(?![^<>]*>)$~i', $_POST['addr'])
68 | || preg_match('~^((?:[^\s<>]*:[^\s<>]*@)?[a-z0-9\-]+(?:\.[a-z0-9\-]+)+:\d+)(?![^<>]*>)$~i', $_POST['addr'])
69 | || preg_match('~^([^\s<>]*:[^\s<>]*@[a-z0-9\-]+(?:\.[a-z0-9\-]+)+(?::\d+)?)(?![^<>]*>)$~i', $_POST['addr'])
70 | // 3. likely servers without any hints but not filenames like *.rar zip exe etc.
71 | || preg_match('~^((?:[a-z0-9\-]+\.)*[a-z2-7]{16}\.onion)(?![^<>]*>)$~i', $_POST['addr'])// *.onion
72 | )
73 | ){
74 | echo 'ERROR: Invalid address given.
';
75 | }else{
76 | $id=$db->query("SELECT COUNT(*) FROM link;")->fetch(PDO::FETCH_NUM);
77 | $id=$id[0]+1;
78 | $db->prepare("INSERT INTO link (id, url) VALUES (?, ?);")->execute([$id, $_POST['addr']]);
79 | echo 'Your link is: ".CANONICAL_URL."?id=$id
";
80 | }
81 | }
82 | echo '