/
Validation.php
241 lines (222 loc) · 8.11 KB
/
Validation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
namespace Concrete\Core\Form\Service;
use Concrete\Core\Support\Facade\Application;
use Concrete\Core\Validator\String\EmailValidator;
use Loader;
/**
* \@package Helpers
* @subpackage Validation
*
* @author Andrew Embler <andrew@concrete5.org>
* @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
* @license http://www.concrete5.org/license/ MIT License
*/
/**
* Helper functions to use with validating submitting forms.
*
* \@package Helpers
* @subpackage Validation
*
* @author Andrew Embler <andrew@concrete5.org>
* @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
* @license http://www.concrete5.org/license/ MIT License
*/
class Validation
{
const VALID_NOT_EMPTY = 1;
const VALID_EMAIL = 2;
const VALID_INTEGER = 3;
const VALID_INTEGER_REQUIRED = 4;
const VALID_UPLOADED_IMAGE = 10;
const VALID_UPLOADED_IMAGE_REQUIRED = 11;
const VALID_UPLOADED_FILE = 20;
const VALID_UPLOADED_FILE_REQUIRED = 25;
const VALID_TOKEN = 30;
const VALID_FIELD_INVALID = 99;
protected $fields = [];
protected $fieldsInvalid = [];
protected $data = [];
protected $files = [];
protected $error;
public function __construct()
{
$this->error = Loader::helper('validation/error');
}
/**
* Adds a test to a field to ensure that, if set, it is a valid uploaded image.
*
* @param string $field
* @param string $errorMsg
* @param bool $emptyIsOk Tells whether this can be submitted as empty (e.g. the validation tests only run if someone is actually submitted in the post.)
*/
public function addUploadedImage($field, $errorMsg = null, $emptyIsOk = true)
{
$const = ($emptyIsOk) ? self::VALID_UPLOADED_IMAGE : self::VALID_UPLOADED_IMAGE_REQUIRED;
$this->addRequired($field, $errorMsg, $const);
}
/**
* Adds a required field to the form helper object. This will then be typically used in conjunction with the test() method to see
* if the test is passed.
*
* @param string $field
* @param string $errorMsg
* @param string $validate test to validate against
*/
public function addRequired($field, $errorMsg = null, $validate = self::VALID_NOT_EMPTY)
{
$obj = new \stdClass();
$obj->message = ($errorMsg == null) ? t('Field "%s" is invalid', $field) : $errorMsg;
$obj->field = $field;
$obj->validate = $validate;
$this->fields[] = $obj;
}
/**
* Adds a test to a field to ensure that, if set, it is a valid uploaded file.
*
* @param string $field
* @param string $errorMsg
* @param bool $emptyIsOk Tells whether this can be submitted as empty (e.g. the validation tests only run if someone is actually submitted in the post.)
*/
public function addUploadedFile($field, $errorMsg = null, $emptyIsOk = true)
{
$const = ($emptyIsOk) ? self::VALID_UPLOADED_FILE : self::VALID_UPLOADED_FILE_REQUIRED;
$this->addRequired($field, $errorMsg, $const);
}
/**
* Adds a required field and tests that it is integer only.
*
* @param string $field
* @param string $errorMsg
* @param bool $emptyIsOk Tells whether this can be submitted as empty (e.g. the validation tests only run if someone is actually submitted in the post.)
*/
public function addInteger($field, $errorMsg = null, $emptyIsOk = true)
{
$const = ($emptyIsOk) ? self::VALID_INTEGER : self::VALID_INTEGER_REQUIRED;
$this->addRequired($field, $errorMsg, $const);
}
public function addRequiredToken($value, $errorMsg = null)
{
$obj = new \stdClass();
$vt = Loader::helper('validation/token');
$obj->message = ($errorMsg == null) ? $vt->getErrorMessage() : $errorMsg;
$obj->value = $value;
$obj->validate = self::VALID_TOKEN;
$this->fields[] = $obj;
}
/**
* Adds a required email address to the suite of tests to be run.
*
* @param string $field
* @param string $errorMsg
*/
public function addRequiredEmail($field, $errorMsg = null)
{
$this->addRequired($field, $errorMsg, self::VALID_EMAIL);
}
/**
* Sets the data files array.
*/
public function setFiles()
{
$this->files = $_FILES;
}
/**
* An associative array that we setup to validate against. Typical usage is $val->setData($_POST);.
*
* @param array $data
*/
public function setData($data)
{
$this->data = $data;
}
public function invalidate($message)
{
$f = new \stdClass();
$f->message = $message;
$this->fieldsInvalid[] = $f;
}
/**
* After the validation error helper has been setup, the test() method ensures that all fields that require validation
* pass. Returns true for success and false for failure.
*
* @return bool
*/
public function test()
{
$app = Application::getFacadeApplication();
$val = $app->make('helper/validation/strings');
$num = $app->make('helper/validation/numbers');
$fil = $app->make('helper/validation/file');
// loop through all the fields in the array, and run whatever validation
// the validate parameter says is required
foreach ($this->fields as $f) {
$validate = $f->validate;
$field = isset($f->field) ? $f->field : null;
$fieldValue = isset($this->data[$field]) ? $this->data[$field] : null;
switch ($validate) {
case self::VALID_NOT_EMPTY:
if (!$val->notempty($fieldValue)) {
$this->fieldsInvalid[] = $f;
}
break;
case self::VALID_TOKEN:
$vt = $app->make('helper/validation/token');
if (!$vt->validate($f->value)) {
$this->fieldsInvalid[] = $f;
}
break;
case self::VALID_INTEGER:
if ((!$num->integer($fieldValue)) && ($val->notempty($fieldValue))) {
$this->fieldsInvalid[] = $f;
}
break;
case self::VALID_INTEGER_REQUIRED:
if (!$num->integer($fieldValue)) {
$this->fieldsInvalid[] = $f;
}
break;
case self::VALID_UPLOADED_IMAGE:
if ((!$fil->image($this->files[$field]['tmp_name'])) && ($this->files[$field]['tmp_name'] != '')) {
$this->fieldsInvalid[] = $f;
}
break;
case self::VALID_UPLOADED_IMAGE_REQUIRED:
if (!$fil->image($this->files[$field]['tmp_name'])) {
$this->fieldsInvalid[] = $f;
}
break;
case self::VALID_UPLOADED_FILE:
if ((!$fil->file($this->files[$field]['tmp_name'])) && ($this->files[$field]['tmp_name'] != '')) {
$this->fieldsInvalid[] = $f;
}
break;
case self::VALID_UPLOADED_FILE_REQUIRED:
if (!$fil->file($this->files[$field]['tmp_name'])) {
$this->fieldsInvalid[] = $f;
}
break;
case self::VALID_EMAIL:
if (!$app->make(EmailValidator::class)->isValid($fieldValue)) {
$this->fieldsInvalid[] = $f;
}
break;
}
}
$this->setErrorsFromInvalidFields();
return count($this->fieldsInvalid) == 0;
}
protected function setErrorsFromInvalidFields()
{
foreach ($this->fieldsInvalid as $f) {
if (isset($f->field)) {
$this->error->add($f->message, $f->field);
} else {
$this->error->add($f->message);
}
}
}
public function getError()
{
return $this->error;
}
}