1
|
|
|
<?php |
2
|
|
|
defined('ROOT_PATH') || exit('Access denied'); |
3
|
|
|
/** |
4
|
|
|
* TNH Framework |
5
|
|
|
* |
6
|
|
|
* A simple PHP framework using HMVC architecture |
7
|
|
|
* |
8
|
|
|
* This content is released under the GNU GPL License (GPL) |
9
|
|
|
* |
10
|
|
|
* Copyright (C) 2017 Tony NGUEREZA |
11
|
|
|
* |
12
|
|
|
* This program is free software; you can redistribute it and/or |
13
|
|
|
* modify it under the terms of the GNU General Public License |
14
|
|
|
* as published by the Free Software Foundation; either version 3 |
15
|
|
|
* of the License, or (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* This program is distributed in the hope that it will be useful, |
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20
|
|
|
* GNU General Public License for more details. |
21
|
|
|
* |
22
|
|
|
* You should have received a copy of the GNU General Public License |
23
|
|
|
* along with this program; if not, write to the Free Software |
24
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class FormValidation{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The form validation status |
32
|
|
|
* @var boolean |
33
|
|
|
*/ |
34
|
|
|
protected $_success = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The list of errors messages |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $_errorsMessages = array(); |
41
|
|
|
|
42
|
|
|
// Array of rule sets, fieldName => PIPE seperated ruleString |
43
|
|
|
protected $_rules = array(); |
44
|
|
|
|
45
|
|
|
// Array of errors, niceName => Error Message |
46
|
|
|
protected $_errors = array(); |
47
|
|
|
|
48
|
|
|
// Array of post Key => Nice name labels |
49
|
|
|
protected $_labels = array(); |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The errors delimiters |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $_allErrorsDelimiter = array('<div class="error">', '</div>'); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The each error delimiter |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
protected $_eachErrorDelimiter = array('<p class="error">', '</p>'); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Indicated if need force the validation to be failed |
65
|
|
|
* @var boolean |
66
|
|
|
*/ |
67
|
|
|
protected $_forceFail = false; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The list of the error messages overrides by the original |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
protected $_errorPhraseOverrides = array(); |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* The logger instance |
77
|
|
|
* @var Log |
78
|
|
|
*/ |
79
|
|
|
private $logger; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* The data to be validated, the default is to use $_POST |
83
|
|
|
* @var array |
84
|
|
|
*/ |
85
|
|
|
private $data = array(); |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Whether to check the CSRF. This attribute is just a way to allow custom change of the |
89
|
|
|
* CSRF global configuration |
90
|
|
|
* |
91
|
|
|
* @var boolean |
92
|
|
|
*/ |
93
|
|
|
public $enableCsrfCheck = false; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set all errors and rule sets empty, and sets success to false. |
97
|
|
|
* |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
public function __construct() { |
101
|
|
|
$this->logger =& class_loader('Log', 'classes'); |
102
|
|
|
$this->logger->setLogger('Library::FormValidation'); |
103
|
|
|
|
104
|
|
|
//Load form validation language message |
105
|
|
|
Loader::lang('form_validation'); |
106
|
|
|
$obj = & get_instance(); |
107
|
|
|
$this->_errorsMessages = array( |
108
|
|
|
'required' => $obj->lang->get('fv_required'), |
109
|
|
|
'min_length' => $obj->lang->get('fv_min_length'), |
110
|
|
|
'max_length' => $obj->lang->get('fv_max_length'), |
111
|
|
|
'exact_length' => $obj->lang->get('fv_exact_length'), |
112
|
|
|
'less_than' => $obj->lang->get('fv_less_than'), |
113
|
|
|
'greater_than' => $obj->lang->get('fv_greater_than'), |
114
|
|
|
'matches' => $obj->lang->get('fv_matches'), |
115
|
|
|
'valid_email' => $obj->lang->get('fv_valid_email'), |
116
|
|
|
'not_equal' => array( |
117
|
|
|
'post:key' => $obj->lang->get('fv_not_equal_post_key'), |
118
|
|
|
'string' => $obj->lang->get('fv_not_equal_string') |
119
|
|
|
), |
120
|
|
|
'depends' => $obj->lang->get('fv_depends'), |
121
|
|
|
'is_unique' => $obj->lang->get('fv_is_unique'), |
122
|
|
|
'is_unique_update' => $obj->lang->get('fv_is_unique_update'), |
123
|
|
|
'exists' => $obj->lang->get('fv_exists'), |
124
|
|
|
'regex' => $obj->lang->get('fv_regex'), |
125
|
|
|
'in_list' => $obj->lang->get('fv_in_list'), |
126
|
|
|
'numeric' => $obj->lang->get('fv_numeric'), |
127
|
|
|
'callback' => $obj->lang->get('fv_callback'), |
128
|
|
|
); |
129
|
|
|
$this->_resetValidation(); |
130
|
|
|
$this->setData($obj->request->post(null)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Reset the form validation instance |
135
|
|
|
*/ |
136
|
|
|
protected function _resetValidation() { |
137
|
|
|
$this->_rules = array(); |
138
|
|
|
$this->_labels = array(); |
139
|
|
|
$this->_errorPhraseOverrides = array(); |
140
|
|
|
$this->_errors = array(); |
141
|
|
|
$this->_success = false; |
142
|
|
|
$this->_forceFail = false; |
143
|
|
|
$this->data = array(); |
144
|
|
|
$this->enableCsrfCheck = false; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set the form validation data |
149
|
|
|
* @param array $data the values to be validated |
150
|
|
|
* |
151
|
|
|
* @return FormValidation Current instance of object. |
152
|
|
|
*/ |
153
|
|
|
public function setData(array $data){ |
154
|
|
|
$this->logger->debug('Setting the form validation data, the values are: ' . stringfy_vars($data)); |
155
|
|
|
$this->data = $data; |
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get the form validation data |
161
|
|
|
* @return array the form validation data to be validated |
162
|
|
|
*/ |
163
|
|
|
public function getData(){ |
164
|
|
|
return $this->data; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get the validation function name to validate a rule |
169
|
|
|
* |
170
|
|
|
* @return string the function name |
171
|
|
|
*/ |
172
|
|
|
protected function _toCallCase($funcName, $prefix='_validate') { |
173
|
|
|
$funcName = strtolower($funcName); |
174
|
|
|
$finalFuncName = $prefix; |
175
|
|
|
foreach (explode('_', $funcName) as $funcNamePart) { |
176
|
|
|
$finalFuncName .= strtoupper($funcNamePart[0]) . substr($funcNamePart, 1); |
177
|
|
|
} |
178
|
|
|
return $finalFuncName; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Returns the boolean of the data status success. It goes by the simple |
183
|
|
|
* |
184
|
|
|
* @return boolean Whether or not the data validation has succeeded |
185
|
|
|
*/ |
186
|
|
|
public function isSuccess() { |
187
|
|
|
return $this->_success; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Checks if the request method is POST or the Data to be validated is set |
192
|
|
|
* |
193
|
|
|
* @return boolean Whether or not the form has been submitted or the data is available for validation. |
194
|
|
|
*/ |
195
|
|
|
public function canDoValidation() { |
196
|
|
|
return get_instance()->request->method() === 'POST' || ! empty($this->data); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Runs _run once POST data has been submitted or data is set manually. |
201
|
|
|
* |
202
|
|
|
* @return boolean |
203
|
|
|
*/ |
204
|
|
|
public function run() { |
205
|
|
|
if ($this->canDoValidation()) { |
206
|
|
|
$this->logger->info('The data to validate are listed below: ' . stringfy_vars($this->getData())); |
207
|
|
|
$this->_run(); |
208
|
|
|
} |
209
|
|
|
return $this->isSuccess(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Validate the CSRF |
214
|
|
|
* @return void |
215
|
|
|
*/ |
216
|
|
|
protected function validateCSRF(){ |
217
|
|
|
if(get_instance()->request->method() == 'POST' || $this->enableCsrfCheck){ |
218
|
|
|
$this->logger->debug('Check if CSRF is enabled in configuration'); |
219
|
|
|
//first check for CSRF |
220
|
|
|
if ((get_config('csrf_enable', false) || $this->enableCsrfCheck) && ! Security::validateCSRF()){ |
221
|
|
|
show_error('Invalide data, Cross Site Request Forgery do his job, the data to validate is corrupted.'); |
222
|
|
|
} |
223
|
|
|
else{ |
224
|
|
|
$this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
/** |
229
|
|
|
* Takes and trims each data, if it has any rules, we parse the rule string and run |
230
|
|
|
* each rule against the data value. Sets _success to true if there are no errors |
231
|
|
|
* afterwards. |
232
|
|
|
*/ |
233
|
|
|
protected function _run() { |
234
|
|
|
//validate CSRF |
235
|
|
|
$this->validateCSRF(); |
236
|
|
|
///////////////////////////////////////////// |
237
|
|
|
$this->_forceFail = false; |
238
|
|
|
|
239
|
|
|
foreach ($this->getData() as $inputName => $inputVal) { |
240
|
|
|
if(is_array($this->data[$inputName])){ |
241
|
|
|
$this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
242
|
|
|
} |
243
|
|
|
else{ |
244
|
|
|
$this->data[$inputName] = trim($this->data[$inputName]); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
if (array_key_exists($inputName, $this->_rules)) { |
248
|
|
|
foreach ($this->_parseRuleString($this->_rules[$inputName]) as $eachRule) { |
249
|
|
|
$this->_validateRule($inputName, $this->data[$inputName], $eachRule); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
if (empty($this->_errors) && $this->_forceFail === false) { |
255
|
|
|
$this->_success = true; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Adds a rule to a form data validation field. |
261
|
|
|
* |
262
|
|
|
* @param string $inputField Name of the field or the data key to add a rule to |
263
|
|
|
* @param string $ruleSets PIPE seperated string of rules |
264
|
|
|
* |
265
|
|
|
* @return FormValidation Current instance of object. |
266
|
|
|
*/ |
267
|
|
|
public function setRule($inputField, $inputLabel, $ruleSets) { |
268
|
|
|
$this->_rules[$inputField] = $ruleSets; |
269
|
|
|
$this->_labels[$inputField] = $inputLabel; |
270
|
|
|
$this->logger->info('Set the field rule: name [' .$inputField. '], label [' .$inputLabel. '], rules [' .$ruleSets. ']'); |
271
|
|
|
return $this; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Takes an array of rules and uses setRule() to set them, accepts an array |
276
|
|
|
* of rule names rather than a pipe-delimited string as well. |
277
|
|
|
* @param array $ruleSets |
278
|
|
|
* |
279
|
|
|
* @return FormValidation Current instance of object. |
280
|
|
|
*/ |
281
|
|
|
public function setRules(array $ruleSets) { |
282
|
|
|
foreach ($ruleSets as $ruleSet) { |
283
|
|
|
$pipeDelimitedRules = null; |
284
|
|
|
if (is_array($ruleSet['rules'])) { |
285
|
|
|
$pipeDelimitedRules = implode('|', $ruleSet['rules']); |
286
|
|
|
} else { |
287
|
|
|
$pipeDelimitedRules = $ruleSet['rules']; |
288
|
|
|
} |
289
|
|
|
$this->setRule($ruleSet['name'], $ruleSet['label'], $pipeDelimitedRules); |
290
|
|
|
} |
291
|
|
|
return $this; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* This method creates the global errors delimiter, each argument occurs once, at the beginning, and |
296
|
|
|
* end of the errors block respectively. |
297
|
|
|
* |
298
|
|
|
* @param string $start Before block of errors gets displayed, HTML allowed. |
299
|
|
|
* @param string $end After the block of errors gets displayed, HTML allowed. |
300
|
|
|
* |
301
|
|
|
* @return FormValidation Current instance of object. |
302
|
|
|
*/ |
303
|
|
|
public function setErrorsDelimiter($start, $end) { |
304
|
|
|
$this->_allErrorsDelimiter[0] = $start; |
305
|
|
|
$this->_allErrorsDelimiter[1] = $end; |
306
|
|
|
return $this; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* This is the individual error delimiter, each argument occurs once before and after |
311
|
|
|
* each individual error listed. |
312
|
|
|
* |
313
|
|
|
* @param string $start Displayed before each error. |
314
|
|
|
* @param string $end Displayed after each error. |
315
|
|
|
* |
316
|
|
|
* @return FormValidation Current instance of object. |
317
|
|
|
*/ |
318
|
|
|
public function setErrorDelimiter($start, $end) { |
319
|
|
|
$this->_eachErrorDelimiter[0] = $start; |
320
|
|
|
$this->_eachErrorDelimiter[1] = $end; |
321
|
|
|
return $this; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Get the each errors delimiters |
326
|
|
|
* |
327
|
|
|
* @return array |
328
|
|
|
*/ |
329
|
|
|
public function getErrorDelimiter() { |
330
|
|
|
return $this->_eachErrorDelimiter; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Get the all errors delimiters |
335
|
|
|
* |
336
|
|
|
* @return array |
337
|
|
|
*/ |
338
|
|
|
public function getErrorsDelimiter() { |
339
|
|
|
return $this->_allErrorsDelimiter; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* This sets a custom error message that can override the default error phrase provided |
344
|
|
|
* by FormValidation, it can be used in the format of setMessage('rule', 'error phrase') |
345
|
|
|
* which will globally change the error phrase of that rule, or in the format of: |
346
|
|
|
* setMessage('rule', 'fieldname', 'error phrase') - which will only change the error phrase for |
347
|
|
|
* that rule, applied on that field. |
348
|
|
|
* |
349
|
|
|
* @return boolean True on success, false on failure. |
350
|
|
|
*/ |
351
|
|
|
public function setMessage() { |
352
|
|
|
$numArgs = func_num_args(); |
353
|
|
|
switch ($numArgs) { |
354
|
|
|
default: |
355
|
|
|
return false; |
356
|
|
|
// A global rule error message |
357
|
|
|
case 2: |
358
|
|
|
foreach ($this->post(null) as $key => $val) { |
359
|
|
|
$this->_errorPhraseOverrides[$key][func_get_arg(0)] = func_get_arg(1); |
360
|
|
|
} |
361
|
|
|
break; |
362
|
|
|
// Field specific rule error message |
363
|
|
|
case 3: |
364
|
|
|
$this->_errorPhraseOverrides[func_get_arg(1)][func_get_arg(0)] = func_get_arg(2); |
365
|
|
|
break; |
366
|
|
|
} |
367
|
|
|
return true; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Adds a custom error message in the errorSet array, that will |
372
|
|
|
* forcibly display it. |
373
|
|
|
* |
374
|
|
|
* @param string $inputName The form input name or data key |
375
|
|
|
* @param string $errorMessage Error to display |
376
|
|
|
* |
377
|
|
|
* @return formValidation Current instance of the object |
378
|
|
|
*/ |
379
|
|
|
public function setCustomError($inputName, $errorMessage) { |
380
|
|
|
$errorMessage = str_replace('%1', $this->_labels[$inputName], $errorMessage); |
381
|
|
|
$this->_errors[$inputName] = $errorMessage; |
382
|
|
|
return $this; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Allows for an accesor to any/all post values, if a value of null is passed as the key, it |
387
|
|
|
* will recursively find all keys/values of the $_POST array or data array. It also automatically trims |
388
|
|
|
* all values. |
389
|
|
|
* |
390
|
|
|
* @param string $key Key of $this->data to be found, pass null for all Key => Val pairs. |
391
|
|
|
* @param boolean $trim Defaults to true, trims all $this->data values. |
392
|
|
|
* @return string|array Array of post or data values if null is passed as key, string if only one key is desired. |
393
|
|
|
*/ |
394
|
|
|
public function post($key = null, $trim = true) { |
395
|
|
|
$returnValue = null; |
396
|
|
|
if (is_null($key)) { |
397
|
|
|
$returnValue = array(); |
398
|
|
|
foreach ($this->getData() as $key => $val) { |
399
|
|
|
$returnValue[$key] = $this->post($key, $trim); |
400
|
|
|
} |
401
|
|
|
} else { |
402
|
|
|
$returnValue = (array_key_exists($key, $this->getData())) ? (($trim) ? trim($this->data[$key]) : $this->data[$key]) : null; |
403
|
|
|
} |
404
|
|
|
return $returnValue; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Gets all errors from errorSet and displays them, can be echo out from the |
409
|
|
|
* function or just returned. |
410
|
|
|
* |
411
|
|
|
* @param boolean $limit number of error to display or return |
412
|
|
|
* @param boolean $echo Whether or not the values are to be returned or displayed |
413
|
|
|
* |
414
|
|
|
* @return string Errors formatted for output |
415
|
|
|
*/ |
416
|
|
|
public function displayErrors($limit = null, $echo = true) { |
417
|
|
|
list($errorsStart, $errorsEnd) = $this->_allErrorsDelimiter; |
418
|
|
|
list($errorStart, $errorEnd) = $this->_eachErrorDelimiter; |
419
|
|
|
$errorOutput = $errorsStart; |
420
|
|
|
$i = 0; |
421
|
|
|
if (!empty($this->_errors)) { |
422
|
|
|
foreach ($this->_errors as $fieldName => $error) { |
423
|
|
|
if ($i === $limit) { |
424
|
|
|
break; |
425
|
|
|
} |
426
|
|
|
$errorOutput .= $errorStart; |
427
|
|
|
$errorOutput .= $error; |
428
|
|
|
$errorOutput .= $errorEnd; |
429
|
|
|
$i++; |
430
|
|
|
} |
431
|
|
|
} |
432
|
|
|
$errorOutput .= $errorsEnd; |
433
|
|
|
echo ($echo) ? $errorOutput : ''; |
434
|
|
|
return (! $echo) ? $errorOutput : null; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* Returns raw array of errors in no format instead of displaying them |
439
|
|
|
* formatted. |
440
|
|
|
* |
441
|
|
|
* @return array |
442
|
|
|
*/ |
443
|
|
|
public function returnErrors() { |
444
|
|
|
return $this->_errors; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* Breaks up a PIPE seperated string of rules, and puts them into an array. |
449
|
|
|
* |
450
|
|
|
* @param string $ruleString String to be parsed. |
451
|
|
|
* |
452
|
|
|
* @return array Array of each value in original string. |
453
|
|
|
*/ |
454
|
|
|
protected function _parseRuleString($ruleString) { |
455
|
|
|
$ruleSets = array(); |
456
|
|
|
/* |
457
|
|
|
//////////////// hack for regex rule that can contain "|" |
458
|
|
|
*/ |
459
|
|
|
if(strpos($ruleString, 'regex') !== false){ |
460
|
|
|
$regexRule = array(); |
461
|
|
|
$rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
462
|
|
|
preg_match($rule, $ruleString, $regexRule); |
463
|
|
|
$ruleStringTemp = preg_replace($rule, '', $ruleString); |
464
|
|
|
if(!empty($regexRule[0])){ |
465
|
|
|
$ruleSets[] = $regexRule[0]; |
466
|
|
|
} |
467
|
|
|
$ruleStringRegex = explode('|', $ruleStringTemp); |
468
|
|
|
foreach ($ruleStringRegex as $rule) { |
469
|
|
|
$rule = trim($rule); |
470
|
|
|
if($rule){ |
471
|
|
|
$ruleSets[] = $rule; |
472
|
|
|
} |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
} |
476
|
|
|
/***********************************/ |
477
|
|
|
else{ |
478
|
|
|
if (strpos($ruleString, '|') !== FALSE) { |
479
|
|
|
$ruleSets = explode('|', $ruleString); |
480
|
|
|
} else { |
481
|
|
|
$ruleSets[] = $ruleString; |
482
|
|
|
} |
483
|
|
|
} |
484
|
|
|
return $ruleSets; |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
/** |
488
|
|
|
* Returns whether or not a field obtains the rule "required". |
489
|
|
|
* |
490
|
|
|
* @param string $fieldName Field to check if required. |
491
|
|
|
* |
492
|
|
|
* @return boolean Whether or not the field is required. |
493
|
|
|
*/ |
494
|
|
|
protected function _fieldIsRequired($fieldName) { |
495
|
|
|
$rules = $this->_parseRuleString($this->_rules[$fieldName]); |
496
|
|
|
return (in_array('required', $rules)); |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
/** |
500
|
|
|
* Takes a data input name, it's value, and the rule it's being validated against (ex: max_length[16]) |
501
|
|
|
* and adds an error to the errorSet if it fails validation of the rule. |
502
|
|
|
* |
503
|
|
|
* @param string $inputName Name or key of the validation data |
504
|
|
|
* @param string $inputVal Value of the validation data |
505
|
|
|
* @param string $ruleName Rule to be validated against, including args (exact_length[5]) |
506
|
|
|
* @return void |
507
|
|
|
*/ |
508
|
|
|
protected function _validateRule($inputName, $inputVal, $ruleName) { |
509
|
|
|
$this->logger->debug('Rule validation of field [' .$inputName. '], value [' .$inputVal. '], rule [' .$ruleName. ']'); |
510
|
|
|
// Array to store args |
511
|
|
|
$ruleArgs = array(); |
512
|
|
|
|
513
|
|
|
preg_match('/\[(.*)\]/', $ruleName, $ruleArgs); |
514
|
|
|
|
515
|
|
|
// Get the rule arguments, realRule is just the base rule name |
516
|
|
|
// Like min_length instead of min_length[3] |
517
|
|
|
$ruleName = preg_replace('/\[(.*)\]/', '', $ruleName); |
518
|
|
|
|
519
|
|
|
if (method_exists($this, $this->_toCallCase($ruleName))) { |
520
|
|
|
$methodToCall = $this->_toCallCase($ruleName); |
521
|
|
|
call_user_func(array($this, $methodToCall), $inputName, $ruleName, $ruleArgs); |
522
|
|
|
} |
523
|
|
|
return; |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
/** |
527
|
|
|
* Set error for the given field or key |
528
|
|
|
* |
529
|
|
|
* @param string $inputName the input or key name |
530
|
|
|
* @param string $ruleName the rule name |
531
|
|
|
* @param array|string $replacements |
532
|
|
|
*/ |
533
|
|
|
protected function _setError($inputName, $ruleName, $replacements = array()) { |
534
|
|
|
$rulePhraseKeyParts = explode(',', $ruleName); |
535
|
|
|
$rulePhrase = null; |
536
|
|
|
foreach ($rulePhraseKeyParts as $rulePhraseKeyPart) { |
537
|
|
|
if (array_key_exists($rulePhraseKeyPart, $this->_errorsMessages)) { |
538
|
|
|
$rulePhrase = $this->_errorsMessages[$rulePhraseKeyPart]; |
539
|
|
|
} else { |
540
|
|
|
$rulePhrase = $rulePhrase[$rulePhraseKeyPart]; |
541
|
|
|
} |
542
|
|
|
} |
543
|
|
|
// Any overrides? |
544
|
|
|
if (array_key_exists($inputName, $this->_errorPhraseOverrides) && array_key_exists($ruleName, $this->_errorPhraseOverrides[$inputName])) { |
545
|
|
|
$rulePhrase = $this->_errorPhraseOverrides[$inputName][$ruleName]; |
546
|
|
|
} |
547
|
|
|
// Type cast to array in case it's a string |
548
|
|
|
$replacements = (array) $replacements; |
549
|
|
|
$replacementCount = count($replacements); |
550
|
|
|
for ($i = 1; $i <= $replacementCount; $i++) { |
551
|
|
|
$key = $i - 1; |
552
|
|
|
$rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
553
|
|
|
} |
554
|
|
|
if (! array_key_exists($inputName, $this->_errors)) { |
555
|
|
|
$this->_errors[$inputName] = $rulePhrase; |
556
|
|
|
} |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
/** |
560
|
|
|
* Used to run a callback for the callback rule, as well as pass in a default |
561
|
|
|
* argument of the post value. For example the username field having a rule: |
562
|
|
|
* callback[userExists] will eval userExists(data[username]) - Note the use |
563
|
|
|
* of eval over call_user_func is in case the function is not user defined. |
564
|
|
|
* |
565
|
|
|
* @param type $inputArg |
566
|
|
|
* @param string $callbackFunc |
567
|
|
|
* |
568
|
|
|
* @return mixed |
569
|
|
|
*/ |
570
|
|
|
protected function _runCallback($inputArg, $callbackFunc) { |
571
|
|
|
return eval('return ' . $callbackFunc . '("' . $inputArg . '");'); |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
/** |
575
|
|
|
* Used for applying a rule only if the empty callback evaluates to true, |
576
|
|
|
* for example required[funcName] - This runs funcName without passing any |
577
|
|
|
* arguments. |
578
|
|
|
* |
579
|
|
|
* @param string $callbackFunc |
580
|
|
|
* |
581
|
|
|
* @return mixed |
582
|
|
|
*/ |
583
|
|
|
protected function _runEmptyCallback($callbackFunc) { |
584
|
|
|
return eval('return ' . $callbackFunc . '();'); |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
/** |
588
|
|
|
* Gets a specific label of a specific field input name. |
589
|
|
|
* |
590
|
|
|
* @param string $inputName |
591
|
|
|
* |
592
|
|
|
* @return string |
593
|
|
|
*/ |
594
|
|
|
protected function _getLabel($inputName) { |
595
|
|
|
return (array_key_exists($inputName, $this->_labels)) ? $this->_labels[$inputName] : $inputName; |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
/** |
599
|
|
|
* Peform validation for the rule "required" |
600
|
|
|
* @param string $inputName the form field or data key name used |
601
|
|
|
* @param string $ruleName the rule name for this validation ("required") |
602
|
|
|
* @param array $ruleArgs the rules argument |
603
|
|
|
*/ |
604
|
|
|
protected function _validateRequired($inputName, $ruleName, array $ruleArgs) { |
605
|
|
|
$inputVal = $this->post($inputName); |
606
|
|
|
if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
607
|
|
|
$callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
608
|
|
|
if ($inputVal == '' && $callbackReturn == true) { |
609
|
|
|
$this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
610
|
|
|
} |
611
|
|
|
} |
612
|
|
|
else if($inputVal == '') { |
613
|
|
|
$this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
614
|
|
|
} |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
/** |
618
|
|
|
* Perform validation for the honey pot so means for the validation to be failed |
619
|
|
|
* @param string $inputName the form field or data key name used |
620
|
|
|
* @param string $ruleName the rule name for this validation |
621
|
|
|
* @param array $ruleArgs the rules argument |
622
|
|
|
*/ |
623
|
|
|
protected function _validateHoneypot($inputName, $ruleName, array $ruleArgs) { |
624
|
|
|
if ($this->data[$inputName] != '') { |
625
|
|
|
$this->_forceFail = true; |
626
|
|
|
} |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
/** |
630
|
|
|
* Peform validation for the rule "callback" |
631
|
|
|
* @param string $inputName the form field or data key name used |
632
|
|
|
* @param string $ruleName the rule name for this validation ("callback") |
633
|
|
|
* @param array $ruleArgs the rules argument |
634
|
|
|
*/ |
635
|
|
|
protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
636
|
|
|
if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
637
|
|
|
$result = $this->_runCallback($this->data[$inputName], $ruleArgs[1]); |
638
|
|
|
if(! $result){ |
639
|
|
|
$this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
640
|
|
|
} |
641
|
|
|
} |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
/** |
645
|
|
|
* Peform validation for the rule "depends" |
646
|
|
|
* @param string $inputName the form field or data key name used |
647
|
|
|
* @param string $ruleName the rule name for this validation ("depends") |
648
|
|
|
* @param array $ruleArgs the rules argument |
649
|
|
|
*/ |
650
|
|
|
protected function _validateDepends($inputName, $ruleName, array $ruleArgs) { |
651
|
|
|
if (array_key_exists($ruleArgs[1], $this->_errors)) { |
652
|
|
|
$this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
653
|
|
|
} |
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
/** |
657
|
|
|
* Peform validation for the rule "not_equal" |
658
|
|
|
* @param string $inputName the form field or data key name used |
659
|
|
|
* @param string $ruleName the rule name for this validation ("not_equal") |
660
|
|
|
* @param array $ruleArgs the rules argument |
661
|
|
|
*/ |
662
|
|
|
protected function _validateNotEqual($inputName, $ruleName, array $ruleArgs) { |
663
|
|
|
$canNotEqual = explode(',', $ruleArgs[1]); |
664
|
|
|
foreach ($canNotEqual as $doNotEqual) { |
665
|
|
|
$inputVal = $this->post($inputName); |
666
|
|
|
if (preg_match('/post:(.*)/', $doNotEqual)) { |
667
|
|
|
if ($inputVal == $this->data[str_replace('post:', '', $doNotEqual)]) { |
668
|
|
|
$this->_setError($inputName, $ruleName . ',post:key', array($this->_getLabel($inputName), $this->_getLabel(str_replace('post:', '', $doNotEqual)))); |
669
|
|
|
continue; |
670
|
|
|
} |
671
|
|
|
} |
672
|
|
|
else{ |
673
|
|
|
if ($inputVal == $doNotEqual) { |
674
|
|
|
$this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
675
|
|
|
continue; |
676
|
|
|
} |
677
|
|
|
} |
678
|
|
|
} |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
/** |
682
|
|
|
* Peform validation for the rule "matches" |
683
|
|
|
* @param string $inputName the form field or data key name used |
684
|
|
|
* @param string $ruleName the rule name for this validation ("matches") |
685
|
|
|
* @param array $ruleArgs the rules argument |
686
|
|
|
*/ |
687
|
|
|
protected function _validateMatches($inputName, $ruleName, array $ruleArgs) { |
688
|
|
|
$inputVal = $this->post($inputName); |
689
|
|
|
if ($inputVal != $this->data[$ruleArgs[1]]) { |
690
|
|
|
$this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
691
|
|
|
} |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
/** |
695
|
|
|
* Peform validation for the rule "valid_email" |
696
|
|
|
* @param string $inputName the form field or data key name used |
697
|
|
|
* @param string $ruleName the rule name for this validation ("valid_email") |
698
|
|
|
* @param array $ruleArgs the rules argument |
699
|
|
|
*/ |
700
|
|
|
protected function _validateValidEmail($inputName, $ruleName, array $ruleArgs) { |
701
|
|
|
$inputVal = $this->post($inputName); |
702
|
|
|
if (! preg_match("/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i", $inputVal)) { |
703
|
|
|
if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
704
|
|
|
return; |
705
|
|
|
} |
706
|
|
|
$this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
707
|
|
|
} |
708
|
|
|
} |
709
|
|
|
|
710
|
|
|
/** |
711
|
|
|
* Peform validation for the rule "exact_length" |
712
|
|
|
* @param string $inputName the form field or data key name used |
713
|
|
|
* @param string $ruleName the rule name for this validation ("exact_length") |
714
|
|
|
* @param array $ruleArgs the rules argument |
715
|
|
|
*/ |
716
|
|
|
protected function _validateExactLength($inputName, $ruleName, array $ruleArgs) { |
717
|
|
|
$inputVal = $this->post($inputName); |
718
|
|
|
if (strlen($inputVal) != $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
719
|
|
|
if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
720
|
|
|
return; |
721
|
|
|
} |
722
|
|
|
$this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
723
|
|
|
} |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
/** |
727
|
|
|
* Peform validation for the rule "max_length" |
728
|
|
|
* @param string $inputName the form field or data key name used |
729
|
|
|
* @param string $ruleName the rule name for this validation ("max_length") |
730
|
|
|
* @param array $ruleArgs the rules argument |
731
|
|
|
*/ |
732
|
|
|
protected function _validateMaxLength($inputName, $ruleName, array $ruleArgs) { |
733
|
|
|
$inputVal = $this->post($inputName); |
734
|
|
|
if (strlen($inputVal) > $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
735
|
|
|
if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
736
|
|
|
return; |
737
|
|
|
} |
738
|
|
|
$this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
739
|
|
|
} |
740
|
|
|
} |
741
|
|
|
|
742
|
|
|
/** |
743
|
|
|
* Peform validation for the rule "min_length" |
744
|
|
|
* @param string $inputName the form field or data key name used |
745
|
|
|
* @param string $ruleName the rule name for this validation ("min_length") |
746
|
|
|
* @param array $ruleArgs the rules argument |
747
|
|
|
*/ |
748
|
|
|
protected function _validateMinLength($inputName, $ruleName, array $ruleArgs) { |
749
|
|
|
$inputVal = $this->post($inputName); |
750
|
|
|
if (strlen($inputVal) < $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
751
|
|
|
if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
752
|
|
|
return; |
753
|
|
|
} |
754
|
|
|
$this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
755
|
|
|
} |
756
|
|
|
} |
757
|
|
|
|
758
|
|
|
/** |
759
|
|
|
* Peform validation for the rule "less_than" |
760
|
|
|
* @param string $inputName the form field or data key name used |
761
|
|
|
* @param string $ruleName the rule name for this validation ("less_than") |
762
|
|
|
* @param array $ruleArgs the rules argument |
763
|
|
|
*/ |
764
|
|
|
protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
765
|
|
|
$inputVal = $this->post($inputName); |
766
|
|
|
if ($inputVal >= $ruleArgs[1]) { |
767
|
|
|
if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
768
|
|
|
return; |
769
|
|
|
} |
770
|
|
|
$this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
771
|
|
|
} |
772
|
|
|
} |
773
|
|
|
|
774
|
|
|
/** |
775
|
|
|
* Peform validation for the rule "greater_than" |
776
|
|
|
* @param string $inputName the form field or data key name used |
777
|
|
|
* @param string $ruleName the rule name for this validation ("greater_than") |
778
|
|
|
* @param array $ruleArgs the rules argument |
779
|
|
|
*/ |
780
|
|
|
protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
781
|
|
|
$inputVal = $this->post($inputName); |
782
|
|
|
if ($inputVal <= $ruleArgs[1]) { |
783
|
|
|
if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
784
|
|
|
return; |
785
|
|
|
} |
786
|
|
|
$this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
787
|
|
|
} |
788
|
|
|
} |
789
|
|
|
|
790
|
|
|
/** |
791
|
|
|
* Peform validation for the rule "numeric" |
792
|
|
|
* @param string $inputName the form field or data key name used |
793
|
|
|
* @param string $ruleName the rule name for this validation ("numeric") |
794
|
|
|
* @param array $ruleArgs the rules argument |
795
|
|
|
*/ |
796
|
|
|
protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
797
|
|
|
$inputVal = $this->post($inputName); |
798
|
|
|
if (! is_numeric($inputVal)) { |
799
|
|
|
if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
800
|
|
|
return; |
801
|
|
|
} |
802
|
|
|
$this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
803
|
|
|
} |
804
|
|
|
} |
805
|
|
|
|
806
|
|
|
/** |
807
|
|
|
* Peform validation for the rule "exists" |
808
|
|
|
* @param string $inputName the form field or data key name used |
809
|
|
|
* @param string $ruleName the rule name for this validation ("exists") |
810
|
|
|
* @param array $ruleArgs the rules argument |
811
|
|
|
*/ |
812
|
|
|
protected function _validateExists($inputName, $ruleName, array $ruleArgs) { |
813
|
|
|
$inputVal = $this->post($inputName); |
814
|
|
|
$obj = & get_instance(); |
815
|
|
|
if(! isset($obj->database)){ |
816
|
|
|
return; |
817
|
|
|
} |
818
|
|
|
list($table, $column) = explode('.', $ruleArgs[1]); |
819
|
|
|
$obj->database->from($table) |
820
|
|
|
->where($column, $inputVal) |
821
|
|
|
->get(); |
822
|
|
|
$nb = $obj->database->numRows(); |
823
|
|
|
if ($nb == 0) { |
824
|
|
|
if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
825
|
|
|
return; |
826
|
|
|
} |
827
|
|
|
$this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
828
|
|
|
} |
829
|
|
|
} |
830
|
|
|
|
831
|
|
|
/** |
832
|
|
|
* Peform validation for the rule "is_unique" |
833
|
|
|
* @param string $inputName the form field or data key name used |
834
|
|
|
* @param string $ruleName the rule name for this validation ("is_unique") |
835
|
|
|
* @param array $ruleArgs the rules argument |
836
|
|
|
*/ |
837
|
|
|
protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
838
|
|
|
$inputVal = $this->post($inputName); |
839
|
|
|
$obj = & get_instance(); |
840
|
|
|
if(! isset($obj->database)){ |
841
|
|
|
return; |
842
|
|
|
} |
843
|
|
|
list($table, $column) = explode('.', $ruleArgs[1]); |
844
|
|
|
$obj->database->from($table) |
845
|
|
|
->where($column, $inputVal) |
846
|
|
|
->get(); |
847
|
|
|
$nb = $obj->database->numRows(); |
848
|
|
|
if ($nb != 0) { |
849
|
|
|
if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
850
|
|
|
return; |
851
|
|
|
} |
852
|
|
|
$this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
853
|
|
|
} |
854
|
|
|
} |
855
|
|
|
|
856
|
|
|
/** |
857
|
|
|
* Peform validation for the rule "is_unique_update" |
858
|
|
|
* @param string $inputName the form field or data key name used |
859
|
|
|
* @param string $ruleName the rule name for this validation ("is_unique_update") |
860
|
|
|
* @param array $ruleArgs the rules argument |
861
|
|
|
*/ |
862
|
|
|
protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
863
|
|
|
$inputVal = $this->post($inputName); |
864
|
|
|
$obj = & get_instance(); |
865
|
|
|
if(! isset($obj->database)){ |
866
|
|
|
return; |
867
|
|
|
} |
868
|
|
|
$data = explode(',', $ruleArgs[1]); |
869
|
|
|
if(count($data) < 2){ |
870
|
|
|
return; |
871
|
|
|
} |
872
|
|
|
list($table, $column) = explode('.', $data[0]); |
873
|
|
|
list($field, $val) = explode('=', $data[1]); |
874
|
|
|
$obj->database->from($table) |
875
|
|
|
->where($column, $inputVal) |
876
|
|
|
->where($field, '!=', trim($val)) |
877
|
|
|
->get(); |
878
|
|
|
$nb = $obj->database->numRows(); |
879
|
|
|
if ($nb != 0) { |
880
|
|
|
if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
881
|
|
|
return; |
882
|
|
|
} |
883
|
|
|
$this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
884
|
|
|
} |
885
|
|
|
} |
886
|
|
|
|
887
|
|
|
/** |
888
|
|
|
* Peform validation for the rule "in_list" |
889
|
|
|
* @param string $inputName the form field or data key name used |
890
|
|
|
* @param string $ruleName the rule name for this validation ("in_list") |
891
|
|
|
* @param array $ruleArgs the rules argument |
892
|
|
|
*/ |
893
|
|
|
protected function _validateInList($inputName, $ruleName, array $ruleArgs) { |
894
|
|
|
$inputVal = $this->post($inputName); |
895
|
|
|
$list = explode(',', $ruleArgs[1]); |
896
|
|
|
$list = array_map('trim', $list); |
897
|
|
|
if (! in_array($inputVal, $list)) { |
898
|
|
|
if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
899
|
|
|
return; |
900
|
|
|
} |
901
|
|
|
$this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
902
|
|
|
} |
903
|
|
|
} |
904
|
|
|
|
905
|
|
|
/** |
906
|
|
|
* Peform validation for the rule "regex" |
907
|
|
|
* @param string $inputName the form field or data key name used |
908
|
|
|
* @param string $ruleName the rule name for this validation ("regex") |
909
|
|
|
* @param array $ruleArgs the rules argument |
910
|
|
|
*/ |
911
|
|
|
protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
912
|
|
|
$inputVal = $this->post($inputName); |
913
|
|
|
$regex = $ruleArgs[1]; |
914
|
|
|
if (! preg_match($regex, $inputVal)) { |
915
|
|
|
if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
916
|
|
|
return; |
917
|
|
|
} |
918
|
|
|
$this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
919
|
|
|
} |
920
|
|
|
} |
921
|
|
|
|
922
|
|
|
} |
923
|
|
|
|