PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.0.1
Defender Security – Malware Scanner, Login Security & Firewall v3.0.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / framework / base / model.php

model.php in Defender Security – Malware Scanner, Login Security & Firewall 3.0.1, at framework/base/model.php

374 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Calotes\Base;
4
5 use Valitron\Validator;
6
7 abstract class Model extends Component {
8
9 /**
10 * Table name | option name | post type name
11 *
12 * @var string
13 */
14 protected $table;
15 /**
16 * Let validate to run on special scenario
17 *
18 * @string
19 */
20 protected $scenario;
21
22 /**
23 * Rules for validator, example
24 * If scenario is use, must be last position
25 * [
26 * [['name','slug','url' ],'required'],
27 * [['password','email' ],'required','scenario'=>'register'],
28 * [['confirm_password' ],'equals','password'],
29 * [['slug'],'length',10],
30 * ]
31 *
32 * @var array
33 * @deprecated
34 */
35 protected $rules = array();
36
37 /**
38 * Validation errors
39 *
40 * @var array
41 */
42 protected $errors = array();
43
44 /**
45 * Array of safe properties allow for mass assign, default is empty, mean accept all
46 *
47 * @var array
48 */
49 protected $safe = array();
50
51 /**
52 * Store the attribute that should not be export
53 *
54 * @var array
55 */
56 protected $exclude = array();
57
58 /**
59 * An array hold info to map class attributes to db field name
60 *
61 * @var array
62 */
63 protected $mapping = array();
64
65 /**
66 * Run the validation
67 *
68 * @return bool
69 * @throws \ReflectionException
70 */
71 public function validate() {
72 $this->before_validate();
73 if ( empty( $this->annotations ) ) {
74 $this->validate_oldway();
75 }
76 $validate = $this->get_validate_rules();
77 $validate->validate();
78 $this->errors = $validate->errors();
79 $this->after_validate();
80
81 return ! count( $this->errors );
82 }
83
84 /**
85 * Backward compatibility
86 *
87 * @deprecated
88 */
89 private function validate_oldway() {
90 if ( ! empty( $this->rules ) ) {
91 $v = new Validator( $this->export() );
92 foreach ( $this->rules as $item ) {
93 $scenario = null;
94 if ( isset( $item['scenario'] ) ) {
95 $scenario = $item['scenario'];
96 unset( $item['scenario'] );
97 }
98 if ( $scenario && $this->scenario !== $scenario ) {
99 continue;
100 }
101 $fields = $item[0];
102 $rule = $item[1];
103 unset( $item[0], $item[1] );
104 foreach ( $fields as $field ) {
105 $v->rule( $rule, $field, ...$item );
106 }
107 }
108 $v->validate();
109 $this->errors = $v->errors();
110 }
111 }
112
113 /**
114 * Override this if you want to trigger something before validation process
115 */
116 protected function before_validate() {
117 }
118
119 /**
120 * Override this if you want to trigger something after validation process
121 */
122 protected function after_validate() {
123 }
124
125 /**
126 * Export the class data
127 *
128 * @return array
129 */
130 public function export() {
131 if ( empty( $this->annotations ) ) {
132 return $this->export_oldway();
133 }
134 $return = array();
135 foreach ( array_keys( $this->annotations ) as $property ) {
136 if ( $this->has_property( $property ) ) {
137 $return[ $property ] = $this->$property;
138 }
139 }
140
141 return $return;
142 }
143
144 /**
145 * This is for backward compatibility
146 *
147 * @return array
148 */
149 private function export_oldway() {
150 try {
151 $reflection = new \ReflectionClass( $this );
152 $props = $reflection->getProperties( \ReflectionProperty::IS_PUBLIC );
153 $values = array();
154 foreach ( $props as $prop ) {
155 if ( 'annotations' === $prop->getName() ) {
156 continue;
157 }
158
159 if ( in_array( $prop->getName(), $this->exclude, true ) ) {
160 continue;
161 }
162
163 $value = $prop->getValue( $this );
164 if ( is_null( $value ) ) {
165 $value = '';
166 }
167 $values[ $prop->getName() ] = $value;
168 }
169
170 $this->exclude = array();
171
172 return $values;
173 } catch ( \Exception $e ) {
174 return array();
175 }
176 }
177
178 /**
179 * @return array
180 */
181 public function get_errors() {
182 return $this->errors;
183 }
184
185 /**
186 * Get error as formatted string
187 *
188 * @return string
189 */
190 public function get_formatted_errors() {
191 return implode( '<br/>', $this->errors );
192 }
193
194 /**
195 * Set properties that we should not return on export. Note that this will be wiped after an export done
196 *
197 * @param array $properties
198 */
199 public function set_excludes( array $properties ) {
200 $this->exclude = $properties;
201 }
202
203 /**
204 * Set properties allow for mass assign
205 *
206 * @param array $properties
207 */
208 public function set_safe( array $properties ) {
209 $this->safe = $properties;
210 }
211
212 /**
213 * @param array $data
214 *
215 * @throws \ReflectionException
216 */
217 public function import_old_way( $data ) {
218 foreach ( $data as $key => $val ) {
219 // check if we have a safe list
220 if ( ! empty( $this->safe ) && ! in_array( $key, $this->safe, true ) ) {
221 continue;
222 }
223
224 $allowed = array_keys( $this->export() );
225 if ( ! in_array( $key, $allowed ) ) {
226 continue;
227 }
228
229 if ( $this->has_property( $key ) ) {
230 $this->$key = $val;
231 }
232 }
233 }
234
235 /**
236 * @param $data
237 *
238 * @throws \ReflectionException
239 */
240 public function import( $data ) {
241 if ( empty( $this->annotations ) ) {
242 return $this->import_old_way( $data );
243 }
244
245 foreach ( array_keys( $this->annotations ) as $property ) {
246 if ( isset( $data[ $property ] ) && $this->has_property( $property ) ) {
247 $this->$property = $data[ $property ];
248 }
249 }
250
251 $this->sanitize();
252 }
253
254 /**
255 * This will return the key=>field for saving db, can be different with class attribute base on map
256 *
257 * @param array $data
258 *
259 * @return array
260 * @throws \ReflectionException
261 */
262 protected function prepare_data( $data = array() ) {
263 $scenario = 'import';
264 if ( ! count( $data ) ) {
265 $data = $this->export();
266 $scenario = 'export';
267 }
268 if ( empty( $this->mapping ) ) {
269 return $data;
270 }
271 foreach ( $this->mapping as $key => $val ) {
272 if ( 'export' === $scenario && isset( $data[ $key ] ) ) {
273 $data[ $val ] = $data[ $key ];
274 unset( $data[ $key ] );
275 } elseif ( 'import' === $scenario && isset( $data[ $val ] ) ) {
276 $data[ $key ] = $data[ $val ];
277 unset( $data[ $val ] );
278 }
279 }
280
281 return $data;
282 }
283
284 /**
285 * Run a filter for casting type and
286 *
287 * @return void
288 */
289 protected function sanitize() {
290 if ( empty( $this->annotations ) ) {
291 return;
292 }
293
294 foreach ( $this->annotations as $property => $meta ) {
295 if ( ! $this->has_property( $property ) ) {
296 // todo log it as this is not a good behavior
297 continue;
298 }
299 $type = $meta['type'];
300 if ( false === $type ) {
301 // without a type, wont allow it
302 $this->$property = null;
303 continue;
304 }
305
306 $value = $this->$property;
307 // cast it first
308 if ( 'boolean' === $type || 'bool' === $type ) {
309 $value = filter_var( $value, FILTER_VALIDATE_BOOLEAN );
310 } else {
311 settype( $value, $type );
312 }
313
314 if ( $meta['sanitize'] !== false ) {
315 $func = $meta['sanitize'];
316 if ( ! function_exists( $func ) ) {
317 // the formatting.php still need to be include
318 include_once ABSPATH . WPINC . '/formatting.php';
319 }
320 if ( is_array( $value ) ) {
321 $value = $this->sanitize_array( $value, $func );
322 } else {
323 $value = $func( $value );
324 }
325 }
326
327 $this->$property = $value;
328 }
329 }
330
331 /**
332 * @param $arr
333 * @param $sanitize
334 *
335 * @return mixed
336 */
337 protected function sanitize_array( $arr, $sanitize ) {
338 foreach ( $arr as &$value ) {
339 if ( is_array( $value ) ) {
340 $value = $this->sanitize_array( $value, $sanitize );
341 } else {
342 $value = $sanitize( $value );
343 }
344 }
345
346 return $arr;
347 }
348
349 /**
350 * Prepare the validation object
351 *
352 * @return Validator
353 * @throws \ReflectionException
354 */
355 protected function get_validate_rules() {
356 $v = new Validator( $this->export() );
357 foreach ( $this->annotations as $property => $meta ) {
358 if ( ! $this->has_property( $property ) ) {
359 continue;
360 }
361 if ( false === $meta['rule'] ) {
362 continue;
363 }
364
365 $rules = explode( '|', $meta['rule'] );
366 foreach ( $rules as $str ) {
367 $v->rule( $str, $property );
368 }
369 }
370
371 return $v;
372 }
373 }
374