-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
executable file
·148 lines (132 loc) · 4.64 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
<?php
require_once 'config.php';
require_once 'mnemonic.php';
$url_pattern = '(([\w]+:)?\/\/)(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?';
$tag_pattern = '[a-zA-Z0-9_.-]+';
//preg compatible
$tag_url_preg = "/^$tag_pattern\|$url_pattern/";
function dbConnect()
{
global $MYSQL_HOST, $MYSQL_USERNAME, $MYSQL_PASSWORD, $MYSQL_DATABASE;
$db = mysql_connect($MYSQL_HOST, $MYSQL_USERNAME, $MYSQL_PASSWORD) or die("Error. Coudn't Connect to Database" . mysql_error());
mysql_select_db($MYSQL_DATABASE, $db) or die("Error. Coudn't Select the Database: " . mysql_error());
return $db;
}
function get_tag_url($uri)
{
global $tag_url_preg;
if (!preg_match($tag_url_preg, $uri))
return false;
else
return explode('|', $uri, 2);
}
function valid_tag($uri)
{
global $tag_pattern;
global $max_tag_length;
$tag_preg = "/^$tag_pattern$/";
if (preg_match($tag_preg, $uri) && (strlen($uri) <= $max_tag_length))
return true;
else
return false;
}
function valid_url($uri)
{
global $url_pattern;
$url_preg = "/^$url_pattern/";
if (!preg_match($url_preg, $uri))
return false;
else
return true;
}
function get_url_from_tag($tag)
{
global $MYSQL_TABLE_REDIRECTIONS;
$table = $MYSQL_TABLE_REDIRECTIONS;
$query = "SELECT url FROM " . $table . " WHERE tag='" . mysql_real_escape_string($tag) . "';";
$result = mysql_query($query);
if ($result) {
//should return array with only one row
$url = mysql_fetch_assoc($result);
//print_r($url);
$no_of_url = count($url);
if ($no_of_url == 0) {
echo("This tag $tag doesn't have any URL associated with it");
return false;
} elseif ($no_of_url != 1) {
die("This tag $tag didn't returned unique query. Please contact the website administrator");
return false;
} else {
//increment the value of hits columns
$query = "UPDATE $table SET hits = hits + 1 WHERE tag='$tag';";
mysql_query($query) or die(mysql_error());
//return the URL
return $url['url'];
}
}
else {
die("An error occured in retriving the URL for the tag $tag");
return false;
}
}
function register_url($url)
{
global $tag_length;
global $MYSQL_TABLE_REDIRECTIONS;
global $logged_in;
$newtag = mnemonic($tag_length, 0);
//echo $newtag;
$query = "SELECT url FROM " . $MYSQL_TABLE_REDIRECTIONS . " WHERE tag='" . mysql_real_escape_string($newtag) . "';";
//echo $query;
//exit();
while (!mysql_query($query)) {
//keep trying till a random tag which is not already in DB is found
//TODO: A do-while loop would have been nicer
$newtag = mnemonic($tag_length, 0);
$query = "SELECT url FROM " . $MYSQL_TABLE_REDIRECTIONS . " WHERE tag='" . mysql_real_escape_string($newtag) . "';";
}
//now we have our tag, we have our URL, we have to get other information
$tagger = "";
if ($logged_in)
$tagger = $_SESSION['OPENID_IDENTITY'];
else
$tagger = 'guest';
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date_now = $mysqldate = date('Y-m-d H:i:s');
$query = "INSERT INTO " . $MYSQL_TABLE_REDIRECTIONS . " value('$newtag','$url','$tagger','$ipaddress','$date_now',0);";
if (mysql_query($query))
return $newtag;
else {
die("Cannot insert into Database for URL: $url and tag:$newtag");
return false;
}
}
function register_url_tag($url, $tag)
{
global $MYSQL_TABLE_REDIRECTIONS;
//echo $newtag;
$query = "SELECT url FROM " . $MYSQL_TABLE_REDIRECTIONS . " WHERE tag='" . mysql_real_escape_string($tag) . "';";
if (mysql_num_rows(mysql_query($query))) {
//echo "tag existed";
return register_url($url);
}
else {
global $MYSQL_TABLE_REDIRECTIONS;
global $logged_in;
$tagger = "";
if ($logged_in)
$tagger = $_SESSION['OPENID_IDENTITY'];
else
$tagger = 'guest';
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date_now = $mysqldate = date('Y-m-d H:i:s');
$query = "INSERT INTO " . $MYSQL_TABLE_REDIRECTIONS . " value('$tag','$url','$tagger','$ipaddress','$date_now',0);";
if (mysql_query($query))
return $tag;
else {
die("Cannot insert into Database for URL: $url and tag:$tag");
return false;
}
}
}
?>