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