This repository has been archived by the owner on Feb 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanscript.php
88 lines (71 loc) · 2.02 KB
/
cleanscript.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
<?php
/*
Curtis LayCraft
This file needs ../../access.php
It recieves three string varibles representing parts of a simple mysql query.
This file contains functions to sanitize the array. It returns an associative
array that is structured generally like this:
Array(
titles: Array([0]=>first_name [1]=>last_name
values: Array(
[0]=> Array([first_name]=>"Firsty" [last_name]=>"lasty" …)
[1]=> Array([first_name]=>"Firsty" [last_name]=>"lasty" …)
...
)
*/
// Kill the script if a semicolon, SQL comment, or column alias markers exist.
function charCheck($string){
preg_match("/(--)|\;|\"/",$string,$nullString);
if ($nullString != null){
die("…something went wrong. You cannot input
semicolons, SQL comments, or double quotes. <br/>Please try again.");
}else{
return $string;
}
}
// check if the "where" field is null. If it is, it is omitted.
function whereCheck($where){
if ($where==null){
return $where;
} else {
// append the where postfix
$where = "WHERE $where";
return $where;
}
}
function query($select,$from,$where){
// make $LinkID to the database
require '../../access.php';
// Clean all the values posted.
$select = strip_tags(charCheck($select));
$from = strip_tags(charCheck($from));
$where = strip_tags(whereCheck(charCheck($where)));
// SET A DEFAULT QUERY
if(empty($select)){
$select = '*';
}
if(empty($from)){
$from = 'employees';
}
// assemble the input info to SQL
$query = "SELECT $select FROM $from $where";
$result = mysqli_query($LinkID, $query);
//uncomment for debugging
//echo mysqli_error($LinkID);
//parse the result into an associative array
$things=mysqli_fetch_assoc($result);
foreach (array_keys($things) as $key){
$keys[]= $key;
}
//while there are rows of data write a new value to
while ($things=mysqli_fetch_assoc($result)) {
$values[] = $things;
}
//append the keys to the array
$list['titles'] = $keys;
//append the values onto the end of the associative array
$list['values'] = $values;
//return the result
return $list;
}
?>