1 <?php
2 3 4 5
6 class validation {
7
8
9 private $set;
10
11 private $errors = array();
12
13 private $pass = true;
14
15 16 17
18 public function validation() {
19 $vxml = simplexml_load_file(Controller::$root."data/validation.xml");
20
21 foreach($vxml as $set) {
22 $this->set[(string)$set['name']] = $set;
23 }
24 }
25
26 27 28 29 30
31 static public function add() {
32 $args = func_get_args();
33 $data = array();
34 foreach($args as $arg) {
35 $data = array_merge($data, (array)$arg);
36 }
37 return $data;
38 }
39
40 41 42 43 44
45 public function run($name,$data=array()) {
46 $this->load($name,$data);
47 if(count($this->errors)) {
48 throw new Exception(implode("<br>",$this->errors));
49 return false;
50 } else return true;
51 }
52
53 54 55 56 57
58 public function load($name="default",$data=array()) {
59 $set = $this->set[$name] or die("<b>Error:</b> validation set ".$name." not found");
60
61 foreach($set as $index=>$rule) {
62 if($index == "@attributes") continue;
63 if(!isset($data[$index])) {
64 if(isset($rule['required']) && $rule['required'] == "false") continue;
65 $this->addError($rule, $index." does not exist");
66 continue;
67 }
68 if($rule['type'] == "file") {
69 $result = $this->fileValidate($data[$index],$rule);
70 if(!is_null($result)) $this->addError($rule, $result);
71 }
72 if(!$this->validate($data[$index],$rule['type'])) {
73 $this->addError($rule, $index." is not of type ".$rule['type']);
74 continue;
75 }
76 if(isset($rule['range']) && $rule['type'] == "int") {
77 $range = explode(",",$rule['range']);
78 if(intval($data[$index]) < intval($range[0]) || intval($data[$index]) > intval($range[1])) {
79 $this->addError($rule, $index." is not between ".$range[0]." and ".$range[1]);
80 continue;
81 }
82 }
83 if(isset($rule['maxlen']) && strlen($data[$index]) > $rule['maxlen']) {
84 $this->addError($rule, $index." is too long. Must be less than ".$rule['maxlen']);
85 continue;
86 }
87 if(isset($rule['minlen']) && strlen($data[$index]) < $rule['minlen']) {
88 $this->addError($rule, $index." is too short. Must be greater than ".$rule['minlen']);
89 continue;
90 }
91 if(isset($rule['check'])) {
92 $class = substr($rule['check'],0,strpos($rule['check'],"."));
93 $function = substr($rule['check'],strpos($rule['check'],".")+1,strlen($rule['check']));
94
95 eval("\$result = $class::$function(\$data[\$index]);");
96 if(!$result) {
97 $this->addError($rule, $index." is not valid");
98 continue;
99 }
100 }
101 }
102 }
103
104 105 106 107 108
109 private function addError($rule,$msg="") {
110 $this->errors[] = (isset($rule['message'])) ? (string)$rule['message'] : $msg;
111 }
112
113 114 115 116
117 public function getErrors() {
118 return $this->errors;
119 }
120
121 122 123 124
125 public function passed() {
126 return $this->pass;
127 }
128
129 130 131 132
133 public function displayErrors() {
134 if(count($this->errors)) {
135 $output = "<ul>";
136 foreach($this->errors as $error) {
137 $output .= "<li>".$error."</li>";
138 }
139 $output .= "</ul>";
140 return $output;
141 }
142 return "";
143 }
144
145 146 147 148 149 150
151 private function fileValidate($file,$rules) {
152 if(isset($rules['ext'])) {
153 $ext = substr($file['name'],strrpos($file['name'],".")+1,strlen($file['name']));
154 $valid = explode(",",$rules['ext']);
155 if(!in_array($ext,$valid)) {
156 return "The file ".$file['name']." is an invalid extension. Must have the following extension: ".$rules['ext'];
157 }
158 }
159 if(isset($rules['media'])) {
160 $type = substr($file['type'],0,strrpos($file['name'],"/"));
161 if(!strlen($type))
162 return "The file ".$file['name']." is an unknown type. Must be of the following types: ".$rules['media'];
163
164 $valid = explode(",",$rules['ext']);
165 if(!in_array($type,$valid))
166 return "The file ".$file['name']." is an invalid type. Must be in ".$rules['media'];
167 }
168 if(isset($rules['maxsize']) && ($file['size']/1024) > $rules['maxsize']) {
169 return "The file provided must be less than ".$rules['maxsize']."kB";
170 }
171 if(isset($rules['minsize']) && ($file['size']/1024) < $rules['minsize']) {
172 return "The file provided must be greater than ".$rules['minsize']."kB";
173 }
174
175 return null;
176 }
177
178 179 180 181 182 183
184 private function validate($value,$type) {
185 switch($type) {
186 case "varchar":
187 return self::is_alphanum($value);
188 break;
189 case "int":
190 return self::is_num($value);
191 break;
192 case "binary":
193 return self::is_binary($value);
194 break;
195 case "alpha":
196 return self::is_alpha($value);
197 break;
198 case "signed":
199 return self::is_signed($value);
200 break;
201 case "file":
202 return true;
203 break;
204 case "alpha":
205 return self::is_alpha($value);
206 break;
207 default:
208 return false;
209 break;
210 }
211 }
212
213 214 215
216 static public function is_binary($value) {
217 if(!strlen($value)) return false;
218 return !preg_match("/[^01]/",$value);
219 }
220
221 222 223 224 225
226 static public function is_signed($value) {
227 return !preg_match("/[^0-9]/",$value);
228 }
229
230 231 232
233 static public function is_alpha($value) {
234 return !preg_match("/[^a-zA-Z_-\s]/",$value);
235 }
236
237 238 239
240 static public function is_num($value) {
241 return preg_match("/^-?[0-9]+\.?[0-9]*$/",$value);
242 }
243
244 245 246
247 static public function is_alphanum($value) {
248 return !preg_match("/[^a-zA-Z_-\s0-9]/",$value);
249 }
250
251 252 253
254 static public function is_alphastrict($value) {
255 return !preg_match("/[^a-zA-Z-0-9]/",$value);
256 }
257 }
258 ?>