1<?php 2 3namespace dokuwiki\plugin\bez\mdl; 4 5use dokuwiki\plugin\bez\meta\ValidationException; 6 7class Validator { 8 private $rules=array(), $errors, $model; 9 public function __construct($model) { 10 $this->model = $model; 11 } 12 13 public function add_rule($field, $rule) { 14 $this->rules[$field] = $rule; 15 } 16 17 public function delete_rule($field) { 18 if (isset($this->rules[$field])) { 19 unset($this->rules[$field]); 20 } 21 } 22 23 public function set_rules($rules) { 24 $this->rules = array_merge($this->rules, $rules); 25 } 26 27 public function get_rules() { 28 return $this->rules; 29 } 30 31 public function get_errors() { 32 return $this->errors; 33 } 34 35 public function set_error($field, $code) { 36 $this->errors[$field] = $code; 37 } 38 39 protected function check_against_val_method($value, $method, $args) { 40 $validator = 'validate_'.$method; 41 if (!method_exists($this, $validator)) { 42 throw new \Exception("there is no validation function $validator"); 43 } 44 45 array_unshift($args, $value); 46 $result = call_user_func_array(array($this, $validator), $args); 47 return array($result, $method); 48 } 49 50 protected function validate_one($value, $method, $args, $null) { 51 if ($null === 'NOT NULL' && $value == '') { 52 return array(false, 'is_null'); 53 } else if ($null === 'NULL' && $value == '') { 54 return array(true, 'is_null'); 55 } 56 57 return $this->check_against_val_method($value, $method, $args); 58 } 59 60 public function validate_field($field, $value) { 61 if (!isset($this->rules[$field])) { 62 throw new \Exception('no validation rule for '.$field); 63 } 64 65 $args = $this->rules[$field][0]; 66 $null = $this->rules[$field][1]; 67 68 $method = array_shift($args); 69 list($result, $code) = $this->validate_one($value, $method, $args, $null); 70 if ($result === false) { 71 throw new ValidationException('-unknown', array($field => $code)); 72 } 73 74 /*by convention all values as passed as strings*/ 75 return (string) $value; 76 } 77 78 public function validate($data, $fields=null) { 79 $val_data = array(); 80 81 if (is_null($fields)) { 82 $fields = array_keys($this->rules); 83 } 84 85 foreach ($data as $key => $value) { 86 if (!in_array($key, $fields)) { 87 continue; 88 } 89 try { 90 $val_data[$key] = $this->validate_field($key, $value); 91 92 } catch (ValidationException $e) { 93 $this->errors[$key] = $e->get_errors()[$key]; 94 } 95 } 96 if (count($this->errors) > 0) { 97 return false; 98 } 99 100 return $val_data; 101 } 102 103 public function validate_array_of($array, $args) { 104 105 $method = array_shift($args); 106 107 foreach ($array as $value) { 108 $result = $this->check_against_val_method($value, $method, $args); 109 if ($result === false) { 110 return false; 111 } 112 } 113 return true; 114 } 115 116 public function validate_select($value, $options) { 117 if (in_array($value, $options)) { 118 return true; 119 } 120 return false; 121 } 122 123 public function validate_dw_user($user, $addtitional_values=array()) { 124 $wiki_users = $this->model->userFactory->get_all(); 125 if (array_key_exists($user, $wiki_users) || 126 in_array($user, $addtitional_values)) { 127 return true; 128 } 129 return false; 130 } 131 132 public function validate_unix_timestamp($stamp) { 133 if (is_numeric($stamp)) { 134 return true; 135 } 136 return false; 137 } 138 139 public function validate_numeric($value) { 140 if (is_numeric($value)) { 141 return true; 142 } 143 return false; 144 } 145 146 public function validate_length($value, $max_length) { 147 if (strlen($value) <= $max_length) { 148 return true; 149 } 150 return false; 151 } 152 153 public function validate_iso_date($date) { 154 if (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $date, $parts)) { 155 $year = $parts[1]; 156 $month = $parts[2]; 157 $day = $parts[3]; 158 if (mktime(0, 0, 0, $month, $day, $year)) { 159 return true; 160 } 161 return false; 162 } 163 return false; 164 } 165 166 public function validate_sqlite_datetime($date) { 167 $strtotime = strtotime($date); 168 if ($strtotime === false) { 169 return false; 170 } 171 172 $datetime = date('Y-m-d H:i:s', $strtotime); 173 if ($datetime !== $date) { 174 return false; 175 } 176 177 return true; 178 } 179 180 public function validate_time($time) { 181 if (preg_match('/^(\d{1,2}):(\d{1,2})$/', $time, $parts)) { 182 $hours = $parts[1]; 183 $minutes = $parts[2]; 184 185 if (mktime($hours, $minutes, 0)) { 186 return true; 187 } 188 return false; 189 } 190 return false; 191 } 192 193 public function validate_must_be_empty($value) { 194 if ($value == '') { 195 return true; 196 } 197 return false; 198 } 199} 200