forked from briancray/PHP-URL-Shortener
-
Notifications
You must be signed in to change notification settings - Fork 3
/
shorten.php
74 lines (57 loc) · 2.29 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
<?php
// First authored by Brian Cray
// Contact the author at http://briancray.com/
// Heavily modified by
// Fernando L. Canizo - http://flc.muriandre.com/
require_once('config.php');
require_once('Lib.php');
$url_to_shorten = get_magic_quotes_gpc() ? stripslashes(trim($_REQUEST['longurl'])) : trim($_REQUEST['longurl']);
if(empty($url_to_shorten)):
Lib::echoJson(false, "Got empty URL, nothing to shorten.");
endif;
if(false === ($hasHttp = preg_match('|^https?://|', $url_to_shorten))):
Lib::echoJson(false, "Regexp failed.");
endif;
if(0 === $hasHttp):
// no |https?://| string, let's add it
// TODO but we should check it exists, as we don't know if it's "http" or "https" or both
endif;
// check if the client IP is allowed to shorten
if($_SERVER['REMOTE_ADDR'] != LIMIT_TO_IP):
die('You are not allowed to shorten URLs with this service.');
endif;
// check if the URL is valid
if(CHECK_URL):
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_to_shorten);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
$response_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($response_status == '404'):
die('Not a valid URL');
endif;
endif;
// check if the URL has already been shortened
$safeUrl = $mysqli->real_escape_string($url_to_shorten);
$query = 'select urls_id from ' . DB_TABLE. ' where urls_long = "' . $safeUrl . '"';
if(false === ($myResult = $mysqli->query($query))):
die("Select query failed: (" . $mysqli->connect_errno . ') ' . $mysqli->connect_error); // TODO replace with proper JSON reply
endif;
$row = $myResult->fetch_assoc();
$myResult->free();
if(null !== $row):
// URL has already been shortened
$shortened_url = Lib::getShortenedURLFromID($row['urls_id']);
else:
// URL not in database, insert
$safeRemoteAddress = $mysqli->real_escape_string($_SERVER['REMOTE_ADDR']);
$query = 'insert into ' . DB_TABLE .
' (urls_long, urls_created_on, urls_creator) values ("' .
$safeUrl . '", "' . time() . '", "' . $safeRemoteAddress . '")';
if(false === $mysqli->query($query)):
die("Failed to insert new shortened url: (" . $mysqli->connect_errno . ') ' . $mysqli->connect_error); // TODO replace with proper JSON reply
endif;
$shortened_url = Lib::getShortenedURLFromID($mysqli->insert_id);
endif;
echo BASE_HREF . $shortened_url;