~titan-phpdevshell/phpdevshell/main

« back to all changes in this revision

Viewing changes to plugins/CRUD/includes/crud.class.php

  • Committer: Jason Schoeman
  • Date: 2011-12-06 14:03:32 UTC
  • Revision ID: titan@phpdevshell.org-20111206140332-4ej6qy4b36d3q96s
Crud Added
ORM Added
Control Panel optimized

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * RedBeanPHP ORM plugin.
 
5
 * 
 
6
 * @author Jason Schoeman, maheshchari.com
 
7
 */
 
8
class crud extends PHPDS_dependant
 
9
{
 
10
        /**
 
11
         * Cleaned up $_GET.
 
12
         *
 
13
         * @var mixed
 
14
         */
 
15
        public $get;
 
16
        /**
 
17
         * Cleaned up $_POST.
 
18
         *
 
19
         * @var mixed
 
20
         */
 
21
        public $post;
 
22
        /**
 
23
         * Cleaned up $_REQUEST.
 
24
         *
 
25
         * @var mixed
 
26
         */
 
27
        public $request;
 
28
        /**
 
29
         * Should forms be protected agains possible injection?
 
30
         *
 
31
         * @var boolean
 
32
         */
 
33
        public $protect = true;
 
34
        /**
 
35
         * Where should data from form be looked for?
 
36
         *
 
37
         * @var string
 
38
         */
 
39
        public $from = 'post';
 
40
        /**
 
41
         * Register an orm service, this allows direct and easy saving to database.
 
42
         *
 
43
         * @var object
 
44
         */
 
45
        public $orm = null;
 
46
        /**
 
47
         * Register an orm service, this allows direct and easy saving to database.
 
48
         *
 
49
         * @var object
 
50
         */
 
51
        public $f;
 
52
        /**
 
53
         * Simply stores last field that was validated.
 
54
         *
 
55
         * @var string
 
56
         */
 
57
        public $lastField;
 
58
        /**
 
59
         * Store selections that did not have id's yet.
 
60
         *
 
61
         * @var string
 
62
         */
 
63
        public $selectWrite;
 
64
        /**
 
65
         * Contains arrays of errors.
 
66
         * 
 
67
         * @var array
 
68
         */
 
69
        public $errorExist = array();
 
70
                
 
71
        /**
 
72
         * This method does the actual security check, other security checks are done on a per call basis to this method in specific scripts.
 
73
         * Improved version reduces the cost of queries by 3, I also believe that this is a more secure method.
 
74
         *
 
75
         * @param boolean $validate_crypt_key Set if you would like the system to verify an encryption before accepting global $_POST variables. Use with method send_crypt_key_validation in your form.
 
76
         * @return string
 
77
         * @author Jason Schoeman
 
78
         */
 
79
        public function construct ($orm = null)
 
80
        {
 
81
                if (is_object($orm))
 
82
                        $this->orm = $orm;
 
83
                else
 
84
                        $this->orm = null;
 
85
                
 
86
                $this->f = new field();
 
87
                
 
88
                if (! empty($this->security->post))
 
89
                        $this->post = $this->security->post;
 
90
                if (! empty($this->security->get))
 
91
                        $this->get = $this->security->get;
 
92
                if (! empty($this->security->request))
 
93
                        $this->request = $this->security->request;
 
94
        }
 
95
        
 
96
        /**
 
97
         * After each form validation methods, use this to compile error fields if any.
 
98
         * @author Jason Schoeman
 
99
         */
 
100
        public function errorShow()
 
101
        {
 
102
                $t = $this->template;
 
103
                if (! empty($this->errorExist)) {
 
104
                        $t->addJsToHead($t->mod->errorField($this->errorExist));
 
105
                }       
 
106
        }       
 
107
        
 
108
        /**
 
109
         * Check if the data was submitted ok and make sure there are no errors.
 
110
         * @return boolean
 
111
         * @author Jason Schoeman
 
112
         */
 
113
        public function ok()
 
114
        {
 
115
                foreach($this->errorExist as $r)
 
116
                        if (! empty($r['type']))
 
117
                                return false;
 
118
                return true;
 
119
        }
 
120
        
 
121
        /**
 
122
         * Ater each validation, add this as the condition to report the error and its message.
 
123
         * @param string $error_message
 
124
         * @param string $field This should be the field name, else it will auto detect. 
 
125
         * @author Jason Schoeman
 
126
         */
 
127
        public function error($error_message, $field='')
 
128
        {
 
129
                if (empty($field))
 
130
                        $field = $this->lastField;
 
131
 
 
132
                $this->errorExist[$field] = array('type'=>'error', 'message'=>$error_message);
 
133
        }
 
134
        
 
135
        /**
 
136
         * Allows import of arrays and converts them to properties for easy access.
 
137
         * @param type $array
 
138
         * @author Jason Schoeman
 
139
         */
 
140
        public function importFields($array)
 
141
        {
 
142
                if (! empty($array) && is_array($array)) {
 
143
                        foreach ($array as $key => $val) {
 
144
                                $this->f->$key = $val;
 
145
                        }
 
146
                }
 
147
        }
 
148
        
 
149
        /**
 
150
         * Allows system to do general check on specified form receive type.
 
151
         * 
 
152
         * @param mixed $key
 
153
         * @param mixed $default
 
154
         * @return mixed
 
155
         * @author Jason Schoeman
 
156
         */
 
157
        public function field($key = null, $default = null)
 
158
        {
 
159
                switch ($this->from) {
 
160
                        case 'post':
 
161
                                $r = $this->POST($key, $default);
 
162
                        break;
 
163
                
 
164
                        case 'get':
 
165
                                $r = $this->GET($key, $default);
 
166
                        break;
 
167
                
 
168
                        case 'request':
 
169
                                $r = $this->REQUEST($key, $default);
 
170
                        break;
 
171
 
 
172
                        default:
 
173
                                $r = $this->POST($key, $default);
 
174
                        break;
 
175
                }
 
176
                
 
177
                if (is_string($r)) {
 
178
                        if (is_object($this->orm))
 
179
                                $this->orm->$key = $r;
 
180
                        else 
 
181
                                $this->f->$key = $r;
 
182
                        $this->lastField = (string) $key;
 
183
                } else {
 
184
                        $this->lastField = (string) $key . '[]';
 
185
                }
 
186
                
 
187
                $this->errorExist[$key] = array();
 
188
                
 
189
                return $r;
 
190
        }
 
191
        
 
192
        /**
 
193
         * Return a value from the REQUEST array
 
194
         *
 
195
         * @param string|null $key the name of the post variable to fetch; if null, the entire array is returned
 
196
         * @param mixed|array $default a default value to return when the post variable is not set; when returning the entire array, an array can be given here with default values
 
197
         *
 
198
         * @return scalar|array the content of the post variable or the whole array, possibly with default value(s)
 
199
         * @author Jason Schoeman
 
200
         */
 
201
        public function REQUEST($key = null, $default = null)
 
202
        {
 
203
                ($this->protect) ? $r = $this->request : $r = $_REQUEST;
 
204
                
 
205
                if (!empty($key)) {
 
206
                        return (isset($r[$key])) ? $r[$key] : $default;
 
207
                } else {
 
208
                        if (is_array($default)) return array_merge($default, $r);
 
209
                        else return $r;
 
210
                }
 
211
        }
 
212
        
 
213
        /**
 
214
         * Return a value from the POST array
 
215
         *
 
216
         * @param string|null $key the name of the post variable to fetch; if null, the entire array is returned
 
217
         * @param mixed|array $default a default value to return when the post variable is not set; when returning the entire array, an array can be given here with default values
 
218
         *
 
219
         * @return scalar|array the content of the post variable or the whole array, possibly with default value(s)
 
220
         * @author Jason Schoeman
 
221
         */
 
222
        public function POST($key = null, $default = null)
 
223
        {
 
224
                ($this->protect) ? $p = $this->post : $p = $_POST;
 
225
                
 
226
                if (!empty($key)) {
 
227
                        return (isset($p[$key])) ? $p[$key] : $default;
 
228
                } else {
 
229
                        if (is_array($default)) return array_merge($default, $p);
 
230
                        else return $p;
 
231
                }
 
232
        }
 
233
 
 
234
        /**
 
235
         * Return a value from the GET meta array
 
236
         *
 
237
         * @param string|null $key the name of the get variable to fetch; if null, the entire array is returned
 
238
         * @param mixed|array $default a default value to return when the get variable is not set; when returning the entire array, an array can be given here with default values
 
239
         *
 
240
         * @return scalar|array the content of the get variable or the whole array, possibly with default value(s)
 
241
         * @author Jason Schoeman
 
242
         */
 
243
        public function GET($key = null, $default = null)
 
244
        {
 
245
                ($this->protect) ? $g = $this->get : $g = $_GET;
 
246
                
 
247
                if (!empty($key)) {
 
248
                        return (isset($g[$key])) ? $g[$key] : $default;
 
249
                } else {
 
250
                        if (is_array($default)) return array_merge($default, $g);
 
251
                        else return $g;
 
252
                }
 
253
        }
 
254
        
 
255
        /**
 
256
         * Makes select fields easy to create and maintain.
 
257
         * @param type $options
 
258
         * @param type $selected
 
259
         * @return string
 
260
         * @author Jason Schoeman
 
261
         */
 
262
        public function select($options, $selected)
 
263
        {
 
264
                return $this->selectElements('', $options, $selected, 'select');
 
265
        }
 
266
        
 
267
        /**
 
268
         * Makes check boxes easy to create and maintain.
 
269
         * @param type $name
 
270
         * @param type $options
 
271
         * @param type $checked
 
272
         * @return string
 
273
         * @author Jason Schoeman
 
274
         */
 
275
        public function checkbox($name, $options, $checked)
 
276
        {
 
277
                return $this->selectElements($name, $options, $checked, 'checkbox');
 
278
        }
 
279
        
 
280
        /**
 
281
         * Makes radio buttons easy to create and maintain.
 
282
         * @param type $name
 
283
         * @param type $options
 
284
         * @param type $checked
 
285
         * @return string
 
286
         * @author Jason Schoeman
 
287
         */
 
288
        public function radio($name, $options, $checked)
 
289
        {
 
290
                return $this->selectElements($name, $options, $checked, 'radio');
 
291
        }
 
292
        
 
293
        /**
 
294
         * Maintainer for radio checkboxes and select fields.
 
295
         * @param type $name
 
296
         * @param type $options
 
297
         * @param type $checked
 
298
         * @param type $type
 
299
         * @return string
 
300
         */
 
301
        public function selectElements($name, $options, $checked, $type)
 
302
        {
 
303
                $m = $this->template->mod;
 
304
                $option = '';
 
305
                if (is_array($options)) {
 
306
                        foreach ($options as $value => $label) {
 
307
                                if (! empty($checked) && in_array($value, $checked))
 
308
                                        $select = true;
 
309
                                else
 
310
                                        $select = null;
 
311
                                
 
312
                                switch ($type) {
 
313
                                        case 'radio':
 
314
                                                $option .= $m->formRadio($name, $value, $label, $select);
 
315
                                        break;
 
316
                                        case 'checkbox':
 
317
                                                $option .= $m->formCheckbox($name, $value, $label, $select);
 
318
                                        break;
 
319
                                        case 'select':
 
320
                                                $option .= $m->formSelect($value, $label, $select);
 
321
                                        break;
 
322
                                }
 
323
                        }
 
324
                }
 
325
                
 
326
                if (empty($option)) {
 
327
                        return '';
 
328
                } else {
 
329
                        return $option;
 
330
                }
 
331
        }       
 
332
        
 
333
        /**
 
334
         * Allows you to easily maintain selected fields.
 
335
         * @param string $val
 
336
         * @param int $join_id
 
337
         * @param string $columns
 
338
         * @return array
 
339
         */
 
340
        public function multiSelected($val, $join_id=null, $columns = 'join_id,value')
 
341
        {
 
342
                if (is_object($this->orm)) {
 
343
                        // User ORM
 
344
                        return $this->multiSelectedORM($val, $join_id, $columns);
 
345
                } else {
 
346
                        // Use Model
 
347
                        return $this->multiSelectedModel($val, $join_id, $columns);
 
348
                }
 
349
        }
 
350
        
 
351
        /**
 
352
         * Simple check for multiple options.
 
353
         * 
 
354
         * @param   string expecting form field name 
 
355
         * @return  boolean
 
356
         */
 
357
        public function isMultipleOption($val)
 
358
        {                       
 
359
                $array = $this->field($val);
 
360
                if (! in_array($array, array(null, false, '', array()), true)) {
 
361
                        return true;
 
362
                } else {
 
363
                        return false;
 
364
                }
 
365
        }
 
366
        
 
367
        /**
 
368
         * Allows you to easily maintain selected fields.
 
369
         * @param string $val
 
370
         * @param int $join_id
 
371
         * @param string $columns
 
372
         * @return array
 
373
         */
 
374
        public function multiSelectedModel($val, $join_id=null, $columns = 'join_id,value')
 
375
        {
 
376
                if (empty($join_id))
 
377
                        if (! empty($this->f->id))
 
378
                                $join_id = $this->f->id;
 
379
 
 
380
                $previously_selected = $this->field($val);      
 
381
                list($join_id_col, $value_col) = explode(',', $columns);
 
382
                        
 
383
                if (! empty($join_id) && empty($previously_selected)) {
 
384
                        $previously_selected = $this->db->invokeQuery('CRUD_readMultipleOptions', $value_col, $val, $join_id_col, $join_id);
 
385
                        
 
386
                        if (! empty($previously_selected)) {
 
387
                                foreach ($previously_selected as $valprev) {
 
388
                                        $array[] = $valprev[$value_col];
 
389
                                }
 
390
                                if (! empty($array))
 
391
                                        return $array;
 
392
                                else
 
393
                                        return array();
 
394
                        }
 
395
                } else {
 
396
                        if (! empty($previously_selected)) {
 
397
                                
 
398
                                if (! empty($join_id)) {
 
399
                                        $this->db->invokeQuery('CRUD_writeMultipleOptions', $val, $join_id_col, $join_id, $value_col, $previously_selected);
 
400
                                }       
 
401
                                
 
402
                                foreach ($previously_selected as $valprev) {
 
403
                                        $array[] = $valprev;
 
404
                                }
 
405
                                if (! empty($array))
 
406
                                        return $array;
 
407
                                else
 
408
                                        return array();
 
409
                        }
 
410
                }
 
411
        }       
 
412
        
 
413
        /**
 
414
         * Allows you to easily maintain selected fields.
 
415
         * @param string $val
 
416
         * @param int $join_id
 
417
         * @param string $columns
 
418
         * @return array
 
419
         */
 
420
        public function multiSelectedORM($val, $join_id=null, $columns = 'join_id,value')
 
421
        {
 
422
                if (empty($join_id))
 
423
                        if (! empty($this->orm->id))
 
424
                                $join_id = $this->orm->id;
 
425
 
 
426
                $previously_selected = $this->field($val);
 
427
                list($join_id_col, $value_col) = explode(',', $columns);
 
428
                        
 
429
                if (! empty($join_id) && empty($previously_selected)) {
 
430
                        $previously_selected = R::find($val, " {$join_id_col} = {$join_id} ");
 
431
                        
 
432
                        if (! empty($previously_selected)) {
 
433
                                foreach ($previously_selected as $valprev) {
 
434
                                        $array[] = $valprev[$value_col];
 
435
                                }
 
436
                                if (! empty($array))
 
437
                                        return $array;
 
438
                                else
 
439
                                        return array();
 
440
                        }
 
441
                } else {
 
442
                        if (! empty($previously_selected)) {
 
443
                                
 
444
                                if (! empty($join_id)) {
 
445
                                        // Delete old selections.
 
446
                                        $replace = R::find($val, " {$join_id_col} = {$join_id} ");
 
447
 
 
448
                                        if (! empty($replace)) {
 
449
                                                foreach ($replace as $valprev) {
 
450
                                                        $bean = R::load("$val", $valprev['id']);
 
451
                                                        R::trash($bean);
 
452
                                                }
 
453
                                        }
 
454
 
 
455
                                        if (! empty($previously_selected)) {
 
456
                                                foreach ($previously_selected as $value) {
 
457
                                                        $multipleORM = R::dispense($val);
 
458
                                                        $multipleORM->$join_id_col = $join_id; 
 
459
                                                        $multipleORM->$value_col = $value;
 
460
                                                        R::store($multipleORM);
 
461
                                                }
 
462
                                        }
 
463
                                }
 
464
                                
 
465
                                foreach ($previously_selected as $valprev) {
 
466
                                        $array[] = $valprev;
 
467
                                }
 
468
                                if (! empty($array))
 
469
                                        return $array;
 
470
                                else
 
471
                                        return array();
 
472
                        }
 
473
                }
 
474
        }       
 
475
        
 
476
        /**
 
477
         * This could really be anything, it is a clean way to add more variable that is not required.
 
478
         * @param string expecting form field name
 
479
         * @return true
 
480
         */
 
481
        public function addField($val) {
 
482
                $val = $this->field($val);
 
483
                return true;
 
484
        }       
 
485
        
 
486
        /**
 
487
         * check if field empty string ,orject,array
 
488
         * @param   string expecting form field name 
 
489
         * @return  boolean
 
490
         */
 
491
        public function is($val) {
 
492
                $val = $this->field($val);
 
493
                return ! in_array($val, array(null, false, '', array()), true);
 
494
        }
 
495
 
 
496
        /**
 
497
         * check a number optional -,+,. values
 
498
         * @param   string expecting form field name 
 
499
         * @return  boolean
 
500
         */
 
501
        public function isNumeric($val) {
 
502
                $val = $this->field($val);
 
503
                return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $val);
 
504
        }
 
505
 
 
506
        /**
 
507
         * valid email     
 
508
         * @param   string expecting form field name 
 
509
         * @return  boolean
 
510
         */
 
511
        public function isEmail($val) {
 
512
                $val = $this->field($val);
 
513
                return (bool) (preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/i", $val));
 
514
        }
 
515
 
 
516
        /**
 
517
         * Valid URL or web address
 
518
         * @param   string expecting form field name 
 
519
         * @return  boolean
 
520
         */
 
521
        public function isUrl($val) {
 
522
                $val = $this->field($val);
 
523
                return (bool) preg_match("/^((((https?|ftps?|gopher|telnet|nntp):\/\/)|(mailto:|news:))(%[0-9A-Fa-f]{2}|[-()_.!~*';\/?:@&=+$,A-Za-z0-9])+)([).!';\/?:,][[:blank:]])?$/", $val);
 
524
        }
 
525
 
 
526
        /**
 
527
         * Valid IP address
 
528
         * @param   string expecting form field name 
 
529
         * @return  boolean
 
530
         */
 
531
        public function isIpAddress($val) {
 
532
                $val = $this->field($val);
 
533
                return (bool) preg_match("/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/", $val);
 
534
        }
 
535
 
 
536
        /**
 
537
         * Matches only alpha letters
 
538
         * @param   string expecting form field name 
 
539
         * @return  boolean
 
540
         */
 
541
        public function isAlpha($val) {
 
542
                $val = $this->field($val);
 
543
                return (bool) preg_match("/^([a-zA-Z])+$/i", $val);
 
544
        }
 
545
 
 
546
        /**
 
547
         * Matches alpha and numbers only
 
548
         * @param   string expecting form field name 
 
549
         * @return  boolean
 
550
         */
 
551
        public function isAlphaNumeric($val) {
 
552
                $val = $this->field($val);
 
553
                return (bool) preg_match("/^([a-zA-Z0-9])+$/i", $val);
 
554
        }
 
555
 
 
556
        /**
 
557
         * Matches alpha ,numbers,-,_ values
 
558
         * @param   string expecting form field name 
 
559
         * @return  boolean
 
560
         */
 
561
        public function isAlphaNumericDash($val) {
 
562
                $val = $this->field($val);
 
563
                return (bool) preg_match("/^([-a-zA-Z0-9_-])+$/i", $val);
 
564
        }
 
565
 
 
566
        /**
 
567
         * Matches alpha and dashes like -,_
 
568
         * @param   string expecting form field name 
 
569
         * @return  boolean
 
570
         */
 
571
        public function isAlphaDash($val) {
 
572
                $val = $this->field($val);
 
573
                return (bool) preg_match("/^([A-Za-z_-])+$/i", $val);
 
574
        }
 
575
 
 
576
        /**
 
577
         * Matches exactly number
 
578
         * @param   string expecting form field name 
 
579
         * @return  boolean
 
580
         */
 
581
        public function isInteger($val) {
 
582
                $val = $this->field($val);
 
583
                return is_int($val);
 
584
        }
 
585
 
 
586
        /**
 
587
         * Valid Credit Card
 
588
         * @param   string expecting form field name 
 
589
         * @return  boolean
 
590
         */
 
591
        public function isCreditCard($val) {
 
592
                $val = $this->field($val);
 
593
                return (bool) preg_match("/^((4\d{3})|(5[1-5]\d{2})|(6011)|(7\d{3}))-?\d{4}-?\d{4}-?\d{4}|3[4,7]\d{13}$/", $val);
 
594
        }
 
595
 
 
596
        /**
 
597
         * check given string length is between given range 
 
598
         * @param   string expecting form field name
 
599
         * @param       int min
 
600
         * @param       int max
 
601
         * @return  boolean
 
602
         */
 
603
        public function isRangeLength($val, $min = 0, $max = 0) {
 
604
                $val = $this->field($val);
 
605
                return (strlen($val) >= $min and strlen($val) <= $max);
 
606
        }
 
607
 
 
608
        /**
 
609
         * Check the string length has minimum length
 
610
         * @param   string expecting form field name 
 
611
         * @param       int min
 
612
         * @return  boolean
 
613
         */
 
614
        public function isMinLength($val, $min) {
 
615
                $val = $this->field($val);
 
616
                return (strlen($val) >= (int) $min);
 
617
        }
 
618
 
 
619
        /**
 
620
         * check string length exceeds maximum length     
 
621
         * @param   string expecting form field name 
 
622
         * @param       int max
 
623
         * @return  boolean
 
624
         */
 
625
        public function isMaxLength($val, $max) {
 
626
                $val = $this->field($val);
 
627
                return (strlen($val) <= (int) $max);
 
628
        }
 
629
 
 
630
        /**
 
631
         * check given number exceeds max values   
 
632
         * @param   string expecting form field name 
 
633
         * @param       int max
 
634
         * @return  boolean
 
635
         */
 
636
        public function isMaxValue($val, $max) {
 
637
                $number = $this->field($val);
 
638
                return ($number > $max);
 
639
        }
 
640
 
 
641
        /**
 
642
         * check given number below value   
 
643
         * @param   string expecting form field name 
 
644
         * @param       int min
 
645
         * @return  boolean
 
646
         */
 
647
        public function isMinValue($val, $min) {
 
648
                $number = $this->field($val);
 
649
                return ($number < $min);
 
650
        }
 
651
 
 
652
        /**
 
653
         * check given number between given values
 
654
         * @param   string expecting form field name 
 
655
         * @param       int min
 
656
         * @param       int max
 
657
         * @return  boolean
 
658
         */
 
659
        public function isRangeValue($val, $min, $max) {
 
660
                $number = $this->field($val);
 
661
                return ($number > $min and $number < $max);
 
662
        }
 
663
 
 
664
        /**
 
665
         * check for exactly length of string
 
666
         * @param   string expecting form field name 
 
667
         * @param       int expecting lenght of string
 
668
         * @return  boolean
 
669
         */
 
670
        public function isLength($val, $length) {
 
671
                $val = $this->field($val);
 
672
                return (strlen($val) == (int) $length);
 
673
        }
 
674
 
 
675
        /**
 
676
         * check decimal with . is optional and after decimal places up to 6th precision
 
677
         * @param   string expecting form field name
 
678
         * @return  boolean
 
679
         */
 
680
        public function isDecimal($val) {
 
681
                $val = $this->field($val);
 
682
                return (bool) pregMatch("/^\d+(\.\d{1,6})?$/'", $val);
 
683
        }
 
684
 
 
685
        /**
 
686
         * Valid hexadecimal color ,that may have #,
 
687
         * @param   string expecting form field name
 
688
         * @return  boolean
 
689
         */
 
690
        public function isHexColor($val) {
 
691
                $color = $this->field($val);
 
692
                return (bool) preg_match('/^#?+[0-9a-f]{3}(?:[0-9a-f]{3})?$/i', $color);
 
693
        }
 
694
 
 
695
        /**
 
696
         * Matches  againest given regular expression ,including delimeters
 
697
         * @param   string expecting form field name 
 
698
         * @param       string regular expression string to compare against
 
699
         * @return  boolean
 
700
         */
 
701
        public function isRegex($val, $expression) {
 
702
                $val = $this->field($val);
 
703
                return (bool) preg_match($expression, (string) $val);
 
704
        }
 
705
 
 
706
        /**
 
707
         * compares two any kind of values ,stictly
 
708
         * @param   string expecting form field name 
 
709
         * @param       mixed expecting string to compare too
 
710
         * @return  boolean
 
711
         */
 
712
        public function isMatches($val, $value) {
 
713
                $val = $this->field($val);
 
714
                return ($val === $value);
 
715
        }
 
716
 
 
717
        /**
 
718
         * check if field empty string ,orject,array
 
719
         * @param   string expecting form field name
 
720
         * @return  boolean
 
721
         */
 
722
        public function isEmpty($val) {
 
723
                $val = $this->field($val);
 
724
                return in_array($val, array(null, false, '', array()), true);
 
725
        }
 
726
 
 
727
        /**
 
728
         * Check if given string matches any format date
 
729
         * @param   string expecting form field name 
 
730
         * @return  boolean
 
731
         */
 
732
        public function isDate($val) {
 
733
                $val = $this->field($val);
 
734
                return (strtotime($val) !== false);
 
735
        }
 
736
 
 
737
        /**
 
738
         * check given string againest given array values
 
739
         * @param   string expecting form field name 
 
740
         * @param       array
 
741
         * @return  boolean
 
742
         */
 
743
        public function isEnum($val, $arr) {
 
744
                $val = $this->field($val);
 
745
                return in_array($val, $arr);
 
746
        }
 
747
 
 
748
        /**
 
749
         * Checks that a field matches a v2 md5 string
 
750
         * @param   string expecting form field name 
 
751
         * @return  boolean
 
752
         */
 
753
        public function isMd5($val) {
 
754
                $val = $this->field($val);
 
755
                return (bool) preg_match("/[0-9a-f]{32}/i", $val);
 
756
        }
 
757
 
 
758
        /**
 
759
         * Matches base64 enoding string
 
760
         * @param   string expecting form field name 
 
761
         * @return  boolean
 
762
         */
 
763
        public function isBase64($val) {
 
764
                $val = $this->field($val);
 
765
                return (bool) !preg_match('/[^a-zA-Z0-9\/\+=]/', $val);
 
766
        }
 
767
 
 
768
        /**
 
769
         * check if array has unique elements,it must have  minimum one element
 
770
         * @param   string expecting form field name 
 
771
         * @return  boolean
 
772
         */
 
773
        public function isUnique($val) {
 
774
                $arr = $this->field($val);
 
775
                $arr = (array) $arr;
 
776
                $count1 = count($arr);
 
777
                $count2 = count(array_unique($arr));
 
778
                return (count1 != 0 and (count1 == $count2));
 
779
        }
 
780
 
 
781
        /**
 
782
         * Check is rgb color value
 
783
         * @param   string expecting form field name 
 
784
         * @return  boolean
 
785
         */
 
786
        public function isRgb($val) {
 
787
                $val = $this->field($val);
 
788
                return (bool) preg_match("/^(rgb\(\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*\))|(rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\))$/", $val);
 
789
        }
 
790
 
 
791
        /**
 
792
         * is given field is boolean value or not
 
793
         * @param   string expecting form field name 
 
794
         * @return  boolean
 
795
         */
 
796
        public function isBoolean($val) {
 
797
                $val = $this->field($val);
 
798
                $booleans = array(1, 0, '1', '0', true, false, true, false);
 
799
                $literals = array('true', 'false', 'yes', 'no');
 
800
                foreach ($booleans as $bool) {
 
801
                        if ($val === $bool)
 
802
                                return true;
 
803
                }
 
804
 
 
805
                return in_array(strtolower($val), $literals);
 
806
        }
 
807
 
 
808
        /**
 
809
         * A token that don't have any white space
 
810
         * @param   string expecting form field name
 
811
         * @return  boolean
 
812
         */
 
813
        public function isToken($val) {
 
814
                $val = $this->field($val);
 
815
                return (bool) !preg_match('/\s/', $val);
 
816
        }
 
817
 
 
818
        /**
 
819
         * Checks that a field is exactly the right length.
 
820
         * @param   string expecting form field name
 
821
         * @link  http://php.net/checkdnsrr  not added to Windows until PHP 5.3.0
 
822
         * @return  boolean
 
823
         */
 
824
        public function isEmailDomain($val) {
 
825
                $email = $this->field($val);
 
826
                return (bool) checkdnsrr(preg_replace('/^[^@]++@/', '', $email), 'MX');
 
827
        }
 
828
 
 
829
        /**
 
830
         * Matches a phone number that length optional numbers 7,10,11
 
831
         * @param   string expecting form field name 
 
832
         * @param       int expecting number lenght
 
833
         * @return  boolean
 
834
         */
 
835
        public function isPhone($val, $lengths = null) {
 
836
                $number = $this->field($val);
 
837
                if (!is_array($lengths)) {
 
838
                        $lengths = array(7, 10, 11);
 
839
                }
 
840
                $number = preg_replace('/\D+/', '', $number);
 
841
                return in_array(strlen($number), $lengths);
 
842
        }
 
843
 
 
844
        /**
 
845
         * check given sting is UTF8 
 
846
         * @param   string expecting form field name 
 
847
         * @return  boolean
 
848
         */
 
849
        public function isUtf8($val) {
 
850
                $val = $this->field($val);
 
851
                return preg_match('%(?:
 
852
        [\xC2-\xDF][\x80-\xBF]        
 
853
        |\xE0[\xA0-\xBF][\x80-\xBF]               
 
854
        |[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}     
 
855
        |\xED[\x80-\x9F][\x80-\xBF]               
 
856
        |\xF0[\x90-\xBF][\x80-\xBF]{2}   
 
857
        |[\xF1-\xF3][\x80-\xBF]{3}                  
 
858
        |\xF4[\x80-\x8F][\x80-\xBF]{2}    
 
859
        )+%xs', $val);
 
860
        }
 
861
 
 
862
        /**
 
863
         * Given sting is lower cased
 
864
         * @param   string expecting form field name 
 
865
         * @return  boolean
 
866
         */
 
867
        public function isLower($val) {
 
868
                $val = $this->field($val);
 
869
                return (bool) preg_match("/^[a-z]+$/", $val);
 
870
        }
 
871
 
 
872
        /**
 
873
         * Given string is upper cased?
 
874
         * @param   string expecting form field name 
 
875
         * @return  boolean
 
876
         */
 
877
        public function isUpper($val) {
 
878
                $val = $this->field($val);
 
879
                return (bool) preg_match("/^[A-Z]+$/", $val);
 
880
        }
 
881
 
 
882
        /**
 
883
         * Checks that given value matches following country pin codes.     
 
884
         * at = austria
 
885
         * au = australia
 
886
         * ca = canada
 
887
         * de = german
 
888
         * ee = estonia
 
889
         * nl = netherlands
 
890
         * it = italy
 
891
         * pt = portugal
 
892
         * se = sweden
 
893
         * uk = united kingdom
 
894
         * us = united states
 
895
         * @param String expecting form field name   
 
896
         * @param String expecting country code
 
897
         * @return  boolean
 
898
         */
 
899
        public function isPincode($val, $country = 'us') {
 
900
                $val = $this->field($val);
 
901
                $patterns = array('at' => '^[0-9]{4,4}$', 'au' => '^[2-9][0-9]{2,3}$', 'ca' =>
 
902
                        '^[a-zA-Z].[0-9].[a-zA-Z].\s[0-9].[a-zA-Z].[0-9].', 'de' => '^[0-9]{5,5}$', 'ee' =>
 
903
                        '^[0-9]{5,5}$', 'nl' => '^[0-9]{4,4}\s[a-zA-Z]{2,2}$', 'it' => '^[0-9]{5,5}$',
 
904
                        'pt' => '^[0-9]{4,4}-[0-9]{3,3}$', 'se' => '^[0-9]{3,3}\s[0-9]{2,2}$', 'uk' =>
 
905
                        '^([A-Z]{1,2}[0-9]{1}[0-9A-Z]{0,1}) ?([0-9]{1}[A-Z]{1,2})$', 'us' =>
 
906
                        '^[0-9]{5,5}[\-]{0,1}[0-9]{4,4}$');
 
907
                if (!array_key_exists($country, $patterns))
 
908
                        return false;
 
909
                return (bool) preg_match("/" . $patterns[$country] . "/", $val);
 
910
        }
 
911
 
 
912
        /**
 
913
         * Check given url really exists?
 
914
         * @param   string expecting form field name 
 
915
         * @return  boolean
 
916
         */
 
917
        public function isUrlExists($val) {
 
918
                $link = $this->field($val);
 
919
                if (!$this->isUrl($link))
 
920
                        return false;
 
921
                return (bool) @fsockopen($link, 80, $errno, $errstr, 30);
 
922
        }
 
923
 
 
924
        /**
 
925
         * Check given sting has script tags
 
926
         * @param   string expecting form field name 
 
927
         * @return  boolean
 
928
         */
 
929
        public function isJsSafe($val) {
 
930
                $val = $this->field($val);
 
931
                return (bool) (!preg_match("/<script[^>]*>[\s\r\n]*(<\!--)?|(-->)?[\s\r\n]*<\/script>/", $val));
 
932
        }
 
933
 
 
934
        /**
 
935
         * given sting has html tags?
 
936
         * @param   string expecting form field name 
 
937
         * @return  boolean
 
938
         */
 
939
        public function isHtmlSafe($val) {
 
940
                $val = $this->field($val);
 
941
                return (bool) (!preg_match("/<(.*)>.*</$1>/", $val));
 
942
        }
 
943
 
 
944
        /**
 
945
         * check given sring has multilines 
 
946
         * @param   string expecting form field name
 
947
         * @return  boolean
 
948
         */
 
949
        public function isMultiLine($val) {
 
950
                $val = $this->field($val);
 
951
                return (bool) preg_match("/[\n\r\t]+/", $val);
 
952
        }
 
953
 
 
954
        /**
 
955
         * check given array key element exists?
 
956
         * @param   string expecting form field name 
 
957
         * @return  boolean
 
958
         */
 
959
        public function isExists($val, $arr) {
 
960
                $val = $this->field($val);
 
961
                return isset($arr[$val]);
 
962
        }
 
963
 
 
964
        /**
 
965
         * is given string is ascii format?
 
966
         * @param   string expecting form field name 
 
967
         * @return  boolean
 
968
         */
 
969
        public function isAscii($val) {
 
970
                $val = $this->field($val);
 
971
                return !preg_match('/[^\x00-\x7F]/i', $val);
 
972
        }
 
973
 
 
974
        /**
 
975
         * Checks given value again MAC address of the computer
 
976
         * @param   string expecting form field name 
 
977
         * @return  boolean
 
978
         */
 
979
        public function isMacAddress($val) {
 
980
                $val = $this->field($val);
 
981
                return (bool) preg_match('/^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/', $val);
 
982
        }
 
983
 
 
984
        /**
 
985
         * Checks given value matches us citizen social security number
 
986
         * @param   string expecting form field name    
 
987
         * @return  boolean
 
988
         */
 
989
        public function isUsssn($val) {
 
990
                $val = $this->field($val);
 
991
                return (bool) preg_match("/^\d{3}-\d{2}-\d{4}$/", $val);
 
992
        }
 
993
 
 
994
        /**
 
995
         * Checks given value matches date de
 
996
         * @param   string expecting form field name 
 
997
         * @return  boolean
 
998
         */
 
999
        public function isDateDE($val) {
 
1000
                $date = $this->field($val);
 
1001
                return (bool) preg_match("/^\d\d?\.\d\d?\.\d\d\d?\d?$/", $date);
 
1002
        }
 
1003
 
 
1004
        /**
 
1005
         * Checks given value matches us citizen social security number
 
1006
         * @param   string expecting form field name 
 
1007
         * @return  boolean
 
1008
         */
 
1009
        public function isDateISO($val) {
 
1010
                $date = $this->field($val);
 
1011
                return (bool) preg_match("/^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/", $date);
 
1012
        }
 
1013
 
 
1014
        /**
 
1015
         * Checks given value matches a time zone  
 
1016
         * +00:00 | -05:00 
 
1017
         * @param   string expecting form field name    
 
1018
         * @return  boolean
 
1019
         */
 
1020
        public function isTimezone($val) {
 
1021
                $val = $this->field($val);
 
1022
                return (bool) preg_match("/^[-+]((0[0-9]|1[0-3]):([03]0|45)|14:00)$/", $val);
 
1023
        }
 
1024
 
 
1025
        /**
 
1026
         * Time in 24 hours format with optional seconds
 
1027
         * 12:15 | 10:26:59 | 22:01:15 
 
1028
         * @param   string expecting form field name 
 
1029
         * @return  boolean
 
1030
         */
 
1031
        public function isTime24($val) {
 
1032
                $val = $this->field($val);
 
1033
                return (bool) preg_match("/^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/", $val);
 
1034
        }
 
1035
 
 
1036
        /**
 
1037
         * Time in 12 hours format with optional seconds
 
1038
         * 08:00AM | 10:00am | 7:00pm
 
1039
         * @param   string expecting form field name 
 
1040
         * @return  boolean
 
1041
         */
 
1042
        public function isTime12($val) {
 
1043
                $val = $this->field($val);
 
1044
                return (bool) preg_match("/^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9][aApP][mM]){1}$/", $val);
 
1045
        }
 
1046
 
 
1047
}
 
1048
 
 
1049
class field
 
1050
{
 
1051
        public function __get($name)
 
1052
        {
 
1053
                return $this->$name = '';
 
1054
        }
 
1055
        
 
1056
        public function __set($name, $value)
 
1057
        {
 
1058
                return $this->$name = $value;
 
1059
        }
 
1060
}
 
 
b'\\ No newline at end of file'