-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mechanism.php
123 lines (84 loc) · 4.46 KB
/
Mechanism.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
<?php
abstract class Mechanism {
private $valid = array();
private $invalid = array();
final public function handleRequest() {
//Check method exists
if (!isset($_GET["method"]) || !method_exists($this, $_GET["method"])) {
Lsucs_Auth::error(1);
}
$method = $_GET["method"];
//Validate inputs against running method
$inputs = $this->getMethodParameters($method);
if (!is_array($inputs)) $inputs = array();
$validInputs = array();
$invalidInputs = array();
$values = array();
foreach ($inputs as $name => $filters) {
if (!is_array($filters)) $filters = array($filters);
//Check if it exists
if (isset($_POST[$name])) {
$value = $_POST[$name];
} else {
$value = "";
}
//If notnull filter doesn't exist and input is null, accept input as valid
if (!in_array("notnull", $filters) && $value == "") {
$validInputs[] = $name;
$values[$name] = $value;
continue;
}
//Process filter validation
$invalid = array();
foreach ($filters as $filter) {
switch ($filter) {
case 'notnull':
if ($value == "") $invalid[] = 'notnull';
break;
case 'int':
if (!filter_var($value, FILTER_VALIDATE_INT)) $invalid[] = 'int';
break;
case 'bool':
if (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) === null) $invalid = 'bool';
break;
case 'email':
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) $invalid = 'email';
break;
case 'ip':
if (!filter_var($value, FILTER_VALIDATE_IP)) $invalid = 'ip';
break;
case 'url':
if (!filter_var($value, FILTER_VALIDATE_URL)) $invalid = 'url';
break;
}
}
//If invalid
if (count($invalid) > 0) $invalidInputs[$name] = $invalid;
else $validInputs[] = $name;
$values[$name] = $value;
}
//Run child page action
$this->valid = $validInputs;
$this->invalid = $invalidInputs;
call_user_func(array($this, $method), $values);
}
final public function isInvalid($parameter, $filter) {
if (isset($this->invalid[$parameter]) && in_array($filter, $this->invalid[$parameter])) return true;
return false;
}
final public function getMethodParameters($method) {
switch ($method) {
case 'validatecredentials': return array("username" => "notnull", "password" => "notnull"); break;
case 'getuserbyid': return array("userid" => array("int", "notnull")); break;
case 'getuserbyusername': return array("username" => "notnull"); break;
case 'getusersbyusername': return array("username" => "notnull"); break;
case 'checkfol': return array("userid" => array("int", "notnull")); break;
}
}
public abstract function validatecredentials($parameters);
public abstract function getuserbyid($parameters);
public abstract function getuserbyusername($parameters);
public abstract function getusersbyusername($parameters);
public abstract function checkfol($parameters);
}
?>