NeuralMesh
  • Class
  • Tree

Classes

  • AbstractNetwork
  • Controller
  • layer
  • ManagedNetwork
  • Model
  • mysql
  • Navigation
  • network
  • NeuralMesh
  • neuron
  • nmesh
  • synapse
  • train
  • UnmanagedNetwork
  • users
  • validation
  1 <?php
  2 /**
  3  * Validation class
  4  * @author Louis Stowasser
  5  */
  6 class validation {
  7     
  8     /** Array of validation sets */
  9     private $set;
 10     /** Array of error messages */
 11     private $errors = array();
 12     /** Boolean of pass status */
 13     private $pass = true;
 14     
 15     /**
 16     * Constructor
 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     * Merges arrays together
 28     * @param array[n] Arrays to merge
 29     * @returns Merged array
 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     * Wrapper for the load function. Handles error messaging
 42     * @param name Name of the validation set
 43     * @param data Associative array collection
 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      * Loads a validation set and records errors to an array
 55      * @param name Name of the validation set
 56      * @param data Form data to process
 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; //skip if not required
 65                 $this->addError($rule, $index." does not exist");
 66                 continue; //skip if failed this level
 67             }
 68             if($rule['type'] == "file") { //if type is file, perform extended validation
 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'])) { //test by type
 73                 $this->addError($rule, $index." is not of type ".$rule['type']);
 74                 continue; //skip if failed this level
 75             }
 76             if(isset($rule['range']) && $rule['type'] == "int") { //test against range
 77                 $range = explode(",",$rule['range']); //create array from range attribute where 0=min and 1=max
 78                 if(intval($data[$index]) < intval($range[0]) || intval($data[$index]) > intval($range[1])) { //if value is out of bounds
 79                     $this->addError($rule, $index." is not between ".$range[0]." and ".$range[1]);
 80                     continue; //skip if failed this level
 81                 }
 82             }
 83             if(isset($rule['maxlen']) && strlen($data[$index]) > $rule['maxlen']) { //test against max length
 84                 $this->addError($rule, $index." is too long. Must be less than ".$rule['maxlen']);
 85                 continue; //skip if failed this level
 86             }
 87             if(isset($rule['minlen']) && strlen($data[$index]) < $rule['minlen']) { //test against min length
 88                 $this->addError($rule, $index." is too short. Must be greater than ".$rule['minlen']);
 89                 continue; //skip if failed this level
 90             }
 91             if(isset($rule['check'])) { //check by static function
 92                 $class = substr($rule['check'],0,strpos($rule['check'],".")); //store the specified class
 93                 $function = substr($rule['check'],strpos($rule['check'],".")+1,strlen($rule['check'])); //store the function
 94                 
 95                 eval("\$result = $class::$function(\$data[\$index]);"); //try run the assertion
 96                 if(!$result) {
 97                     $this->addError($rule, $index." is not valid");
 98                     continue; //skip if failed this level
 99                 }
100             }
101         }
102     }
103     
104     /**
105     * Add a new error message to the array
106     * @param rule The rules array for the item being validate
107     * @param msg If the message attribute does not exist, use this message
108     */
109     private function addError($rule,$msg="") {
110         $this->errors[] = (isset($rule['message'])) ? (string)$rule['message'] : $msg;
111     }
112     
113     /**
114     * Getter for the errors array
115     * @returns array of errors
116     */
117     public function getErrors() {
118         return $this->errors;
119     }
120     
121     /**
122     * Getter for the pass status
123     * @returns array of errors
124     */
125     public function passed() {
126         return $this->pass;
127     }
128     
129     /**
130     * Print the errors in an unordered list
131     * @returns The HTML error message
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     * Extended function to further validate files
147     * @param file File array or data in $_FILES
148     * @param rules Array of the files rules
149     * @returns Error message if there is an error
150     */
151     private function fileValidate($file,$rules) {
152         if(isset($rules['ext'])) { //validate against extension
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'])) { //validate against the media type
160             $type = substr($file['type'],0,strrpos($file['name'],"/"));
161             if(!strlen($type)) //unknown type, error
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']) { //check file size
169             return "The file provided must be less than ".$rules['maxsize']."kB";
170         }
171         if(isset($rules['minsize']) && ($file['size']/1024) < $rules['minsize']) { //check minimum file size
172             return "The file provided must be greater than ".$rules['minsize']."kB";
173         }
174         //file passed validation
175         return null;
176     }
177     
178     /**
179      * Performs a validation on a value against a type
180      * @param value The variable to validate
181      * @param type The type of validation to perform
182      * @return Boolean if passed or failed validation
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     * Check value contains only binary numbers
215     */
216     static public function is_binary($value) {
217         if(!strlen($value)) return false;
218         return !preg_match("/[^01]/",$value);
219     }
220     
221     /**
222      * Checks if value is a signed integer
223      * @param $value Value to check
224      * @return Boolean if passed or failed
225      */
226     static public function is_signed($value) {
227         return !preg_match("/[^0-9]/",$value);
228     }
229     
230     /**
231     * Check value contains only letters, underscore and hyphen
232     */
233     static public function is_alpha($value) {
234         return !preg_match("/[^a-zA-Z_-\s]/",$value);
235     }
236     
237     /**
238     * Check value contains only integers, decimal or hyphen
239     */
240     static public function is_num($value) {
241         return preg_match("/^-?[0-9]+\.?[0-9]*$/",$value);
242     }
243     
244     /**
245     * Check value contains only alphanumeric characters and hyphen, space, underscore
246     */
247     static public function is_alphanum($value) {
248         return !preg_match("/[^a-zA-Z_-\s0-9]/",$value);
249     }
250     
251     /**
252     * Check value contains only alphanumeric characters
253     */
254     static public function is_alphastrict($value) {
255         return !preg_match("/[^a-zA-Z-0-9]/",$value);
256     }
257 }
258 ?>
NeuralMesh API documentation generated by ApiGen