-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
150 lines (130 loc) · 4.17 KB
/
functions.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
<?php
/**
** File: functions.php
** Author: CBMcARthur via Upwork
** Date: 01/31/2016
**
** This file contains functions for processing user input, DB interaction, etc.
** It's really just to keep index.php cleaner and everything more readable. (I hope)
**/
require_once("config.php");
/**
** Function: validate_full_url()
** Return: string containing validation error
** -OR- FALSE if valid URL provided.
**
** This function should be called to validate the user's input for
** a full URL (that will eventually be shortened).
**/
function validate_full_url() {
// Form for shortening URL submitted. Validate input.
if (empty(trim($_POST['full_url']))):
// No input provided. Create error message and display the form again.
$error['full_url'] = "URL field is required.";
return $error;
endif;
// If the user did not include display an error in the form.
if (stripos($_POST['full_url'], "http://") === FALSE):
$error['full_url'] = "URLs must include 'http://' at the beginnning.";
return $error;
endif;
$headers = get_headers($_POST['full_url'], 1);
if ($headers === FALSE):
// Invalid URL or failure acessing the URL. Create error and display form again.
$error['full_url'] = "Invalid URL was provided.";
return $error;
endif;
// $response array will include redirects and lots of extra info.
// This will look nasty, if there is time I can clean this up.
$rheaders = array_reverse($headers, TRUE);
foreach ($rheaders as $key => $value):
// Find the first key that is numeric
if (is_numeric($key)):
$response = substr($value, 9, 3);
if (strcmp($response, "200") === FALSE):
$error['full_url'] = "Invalid URL was provided.";
return $error;
endif;
endif;
endforeach;
// URL seems to be valid.
return FALSE;
}
/**
** Function: shorten_url
** Input: (string) valid URL
** Output: string with shortened URL
**
** This function performs a quick and dirty shortening of the provided URL.
** The domain name comes from config.php. The shortening is a substring
** of a SHA1 hash. Duplicates are possible so there's a check for that.
**/
function shorten_url($url, $short = NULL) {
global $short_domain, $short_length, $dsn, $db_username, $db_password;
$autogenerate = FALSE;
if ($short == NULL):
$autogenerate = TRUE;
$hash = SHA1($url);
$short = $short_domain.substr($hash, ($short_length *-1));
endif;
// Create connection to the DB via PDO
$db = new PDO($dsn, $db_username, $db_password);
// Get count of URLs with this shortened form.
$query = "SELECT * FROM `urls` WHERE `short_url` LIKE ?";
$stmt = $db->prepare($query);
$stmt->execute(array($short));
$rows = $stmt->fetchAll();
if (count($rows) > 0):
// Make sure that the long_url matches the provided URL.
if (strcasecmp($url, $rows[0]['long_url']) === 0):
return $short;
else:
// Collision found. Need to modify short url if auto generated
if ($autogenerate == TRUE):
echo "<pre>";
echo "Collision found!\n";
echo "Long URL: $url\n";
echo "Short URL: $short\n";
var_dump($rows);
exit();
else:
$error['error_short_url'] = "That short form already exists. Try another one.";
return $error;
endif;
endif;
else:
// Insert this shortened URL into the DB
$query = "INSERT INTO `urls` (`long_url`, `short_url`) VALUES (?, ?)";
$stmt = $db->prepare($query);
$result = $stmt->execute(array($url, $short));
return $short;
endif;
}
/**
** Function: get_long_url
** Input: (string) short_url to reteive long version of
** Return: (string) valid URL
** -OR- (array) error(s)
**/
function get_long_url($short_url) {
global $dsn, $db_username, $db_password;
// Make sure a short form was provided
if (strlen(trim($short_url)) == 0):
$error['error_short_url'] = "Short URL is required.";
return $error;
endif;
// Create connection to the DB via PDO
$db = new PDO($dsn, $db_username, $db_password);
// Get count of URLs with this shortened form.
$query = "SELECT * FROM `urls` WHERE `short_url` LIKE ?";
$stmt = $db->prepare($query);
$stmt->execute(array($short_url));
$rows = $stmt->fetchAll();
if (count($rows) > 0):
return $rows[0]['long_url'];
else:
echo "<pre>";
echo "Rows: ";
var_dump($rows);
endif;
}