-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.php
executable file
·208 lines (166 loc) · 4.23 KB
/
common.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
require_once("settings.php");
session_start();
// PHP_DOC
// INPUT: $s as string
// OUTPUT: $s as string
// DESCRIPTION: Prevents SQL injection attacks
function SQLE($s)
{
return mysql_real_escape_string($s);
}
// PHP_DOC
// INPUT: $s as string
// OUTPUT: $s as string
// DESCRIPTION: Prevents SQL injection attacks
function escapeString($s)
{
return str_replace("'", "\'", $s);
}
// PHP_DOC
// INPUT: $s as string
// OUTPUT: $s as string
// DESCRIPTION: Formats string for viewing
function unescapeString($s)
{
return str_replace("\'", "'", $s);
}
// PHP_DOC
// INPUT: $s as string
// OUTPUT: $s as string
// DESCRIPTION: **DEPRICATED**
function stripSlashesString($s){
//$slashes = array ("\", "/");
//return str_replace($slashes, "", $s);
}
// PHP_DOC
// INPUT: $s as string
// OUTPUT: $s as string
// DESCRIPTION: Filters non-hex characters from string, prevents injection attacks
function filterHexString($s){
$pattern = "~[^a-fA-F0-9]~";
$replacement = "";
return preg_replace($pattern, $replacement, $s);
}
// PHP_DOC
// INPUT: $s as string
// OUTPUT: $s as string
// DESCRIPTION: Filters non-alphanumeric characters from string, prevents injection attacks
function filterAlphaNumericString($s){
$pattern = "~[^a-zA-Z0-9.]~";
$replacement = "";
return preg_replace($pattern, $replacement, $s);
}
// PHP_DOC
// INPUT: $s as string
// OUTPUT: $s as string
// DESCRIPTION: Filters non-numerical characters, prevents injection attacks
function filterNumberString($s){
$pattern = "~[^0-9]~";
$replacement = "";
return preg_replace($pattern, $replacement, $s);
}
// PHP_DOC
// INPUT: $s as string
// OUTPUT: $s as string
// DESCRIPTION: Filters all characters except numbers and commas, prevents injection attacks
function filterNumberCSV($s) {
$pattern = "~[^0-9,]~";
$replacement = "";
return preg_replace($pattern, $replacement, $s);
}
// PHP_DOC
// INPUT: null
// OUTPUT: True on valid session, false otherwise
// DESCRIPTION:
function isUserLoggedIn()
{
return (isset($_SESSION) && isset($_SESSION['isValid']) && $_SESSION['isValid']);
}
// PHP_DOC
// INPUT: null
// OUTPUT: sess_username on user logged in, "@invalid_u5er!" on failure
// DESCRIPTION:
function getSessionUsername()
{
if(isUserLoggedIn()) {
return $_SESSION['sess_username'];
}
else {
return "@invalid_u5er!";
}
}
// PHP_DOC
// INPUT: null
// OUTPUT: $link as MySQL database link
// DESCRIPTION: Opens the Database Table using the SQL Database Credentials
function openDatabase()
{
//$db_user = "";
//$password = "";
$database = DB_SCHEMA;
$link = mysql_connect("127.0.0.1:3306", DB_USER, DB_PASSWORD);
if(!$link)
die("Unable to connect to database");
@mysql_select_db($database) or die( "Unable to select database");
return $link;
}
// PHP_DOC
// INPUT: null
// OUTPUT: null
// DESCRIPTION: Closes the Database
function closeDatabase()
{
mysql_close();
}
// PHP_DOC
// INPUT:
// OUTPUT:
// DESCRIPTION: TODO - ANALYIZE
function performSingleDbQueryGetRow($query)
{
return mysql_fetch_assoc(mysql_query($query)); // bug (?) wrong syntax
}
// PHP_DOC
// INPUT: $query as DB query
// OUTPUT: $count as integer
// DESCRIPTION: Performs Query and returns the #rows returned from the Query
function getQueryCount($query)
{
$results = mysql_query($query);
$count = null==$results ? 0 : mysql_num_rows($results);
return $count;
}
// PHP_DOC
// INPUT: $s as string
// OUTPUT: $s as string
// DESCRIPTION: Get Single Quoted & Escaped SQL string, prevents injection
function sqlQStr($s)
{
return "'" . SQLE($s) . "'";
}
// PHP_DOC
// INPUT:
// OUTPUT:
// DESCRIPTION: Damn single quoted strings for SQL gets your eyes tired!
function sqlArgs1($A1)
{
return "('" . SQLE($A1) . "')";
}
// PHP_DOC
// INPUT:
// OUTPUT:
// DESCRIPTION:
function sqlArgs2($A1, $A2)
{
return "('" . SQLE(A1) . "', '" . SQLE($A2) . "')";
}
// PHP_DOC
// INPUT:
// OUTPUT:
// DESCRIPTION:
function sqlArgs3($A1, $A2, $A3)
{
return "('" . SQLE($A1) . "', '" . SQLE($A2) . "', '" . SQLE($A3) . "')";
}
?>