InputValidator.class.php

Gesponsorde koppelingen

PHP script bestanden

  1. InputValidator.class.php
  2. ValidateForm.class.php
  3. CheckLib.class.php

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
<?php
/**
 * @author Mark Eilander
 * @copyright 2008
 * @package onderzoeksinstrument
 * @version 1
 */
 
/**
 * InputValidator Deze klasse bevat functionaliteit om invoer van de client te controleren.
 *
 * Deze klasse heeft tot taak het afschermen van de $_GET, $_POST, $_SESSION en $_COOKIE arrays voor
 * de andere klassen. De klasse is geimplementeerd als singleton.
 */

class InputValidator {
    // invoervariabelen typen
    const TYPE_STRING = 1;
    const TYPE_BOOLEAN = 2;
    const TYPE_INT = 3;
    const TYPE_FLOAT = 4;
    const TYPE_ARRAY = 5;
    const    TYPE_URL = 6;
    const TYPE_EMAIL = 7;
    const TYPE_ZIPCODE = 8;
    const TYPE_PLAINTEXT = 9;
    const TYPE_ALPHATEXT = 10;
    const TYPE_PHONE = 11;
    
    // scopes
    const SCOPE_GET = 1;
    const SCOPE_POST = 2;
    const SCOPE_SESSION = 4;
    const SCOPE_COOKIE = 8;
    const SCOPE_ANY = 15;
    
    // validatieresultaten
    const RESULT_NOT_VALIDATED = 0;
    const RESULT_OK = 1;
    const RESULT_CONVERTABLE = 2;
    const RESULT_EMPTY = 3;
    const RESULT_NON_EXISTENT = 4;
    const RESULT_INVALID = 5;
    
    /**
     * @var InputValidator $s_oInstantie Statische variabelen die object instantie bevat
     * @access private
     * @static
     */

    private static $s_oInstance = null;
    /**
     * @var array $m_aValidationList Lijst met variabelen die gevalideerd moeten worden (zie validateAll)
     * @access private
     */

    private $m_aValidationList;
    /**
     * @var array $p_aScopeOrder De volgorde waarin de scopes worden getest
     * @access private
     */

    private $m_aScopeOrder;
    /**
     * @var string $m_sManualOverwrite Waarde die alle andere validatie resultaten opheft
     * @access private
     */

    private $m_sManualOverwrite;
    
    /**
     * instantiate Deze method wordt gebruikt om het InputValidator object te instantieren
     * @return InputValidator Object
     */

    public static function instantiate() {
        if (self::$s_oInstance == null) {
            // er is nog geen instantie, creeer er een
            self::$s_oInstance = new InputValidator();
        }

        return self::$s_oInstance;
    }

    
    /**
     * constructor
     */

    private function __construct() {
        $this->m_sManualOverwrite = '';
        $this->m_aValidationList = array();
        $this->m_aScopeOrder = array(self::SCOPE_COOKIE, self::SCOPE_SESSION, self::SCOPE_POST, self::SCOPE_GET);
    }

    
    /**
     * validate Validateert een enkele variabele
     *
     * @param string $p_sVar De naam van de variabele die gevalideerd wordt
     * @param int    $p_nScope De scope waarin de variabele moet zitten
     * @param int $p_nType Het type dat de variabele moet hebben
     * @param boolean $p_bMandatory Moet de variabele verplicht aanwezig zijn of mag deze ook leeg zijn
     * @return int Resultaat van validatie (RESULT_OK, RESULT_EMPTY, RESULT_INVALID)
     * @throws ValidationException
     */

    public function validate($p_sVar, $p_nScope, $p_nType, $p_bMandatory) {
        $nResult = self::RESULT_NOT_VALIDATED;
        if ($this->isValidScope($p_nScope) && $this->isValidType($p_nType)) {
            $vResult = $this->getValue($p_sVar, $p_nScope);
            if ($vResult !== null) {
                // waarde gevonden
                $nResult = $this->checkType($vResult, $p_nType);
            }
else {
                // geen waarde gevonden
                $nResult = self::RESULT_NON_EXISTENT;
            }
        }
else {
            throw new ValidationException("Scope (".$p_nScope.") or Type (".$p_nType.") invalid");
        }

        
        // controleer het resultaat
        switch ($nResult) {
            case
self::RESULT_NOT_VALIDATED :
                $nResult = self::RESULT_INVALID;
            break;
            case
self::RESULT_NON_EXISTENT :
                if ($p_bMandatory) {
                    $nResult = self::RESULT_INVALID;
                }
else {
                    // hoeft niet aanwezig te zijn
                    $nResult = self::RESULT_OK;
                }

            break;
            default :

                if ($p_bMandatory) {
                    // als veld verplicht is mag deze niet leeg zijn
                    if (strlen(strVal($vResult)) == 0) {
                        $nResult = self::RESULT_INVALID;
                    }
                }
else {
                    
                    // als veld niet verplicht is mag deze ook leeg zijn
                    if (strlen(strVal($vResult)) == 0) {
                        $nResult = self::RESULT_OK;
                    }
                }
        }


        return $nResult;
    }

    
    /**
     * validateAll Valideert alle variabelen die zijn opgegeven met addValidation
     *
     * @return int Resultaat van validatie RESULT_OK als alle variabelen correct zijn, RESULT_INVALID als 1
     * of meer variabelen incorrect zijn
     */

    public function validateAll() {
        $nResult = self::RESULT_OK;
        foreach ($this->m_aValidationList as $sVarName => $aValidation) {
            $this->m_aValidationList[$sVarName]['result'] = $this->validate($sVarName, $aValidation['scope'], $aValidation['type'], $aValidation['mandatory']);
            if (($this->m_aValidationList[$sVarName]['result'] != self::RESULT_OK) && ($this->m_aValidationList[$sVarName]['result'] != self::RESULT_CONVERTABLE)){
                $nResult = self::RESULT_INVALID;
            }
elseif ($this->m_sManualOverwrite != '') {
                $nResult = $this->m_sManualOverwrite;
            }
        }

        return $nResult;
    }

    
    /**
     * ManualOverwrite Valideert alle variabelen die zijn opgegeven met addValidation
     *
     * @return int Resultaat van validatie RESULT_OK als alle variabelen correct zijn, RESULT_INVALID als 1
     * of meer variabelen incorrect zijn
     */

    public function ManualOverride($p_sValue) {
        $m_sManualOverwrite = $p_sValue;
    }

    
    /**
     * addValidation    Voegt een variabele toe aan de lijst met te valideren variabelen
     *
     * Als een variabele al bestaat in de lijst en zijn scope is gelijk, dan wordt het te valideren type
     * en scope overschreven.
     * Variabelenamen zijn hoofdlettergevoelig
     *
     * @access public
     * @param string $p_sVar    De naam van de variabele
     * @param int    $p_nScope De scope waarin de variabele moet zitten
     * @param int $p_nType Het type dat de variabele moet hebben
     * @param boolean $p_bMandatory Is de variabele verplicht
     * @return boolean true als de variabele correct is toegevoegd
     * @throws ValidationException
     */

    public function addValidation($p_sVar, $p_nScope, $p_nType, $p_bMandatory) {
        $bResult = false;
        if ($this->isValidScope($p_nScope) && $this->isValidType($p_nType)) {
            $this->m_aValidationList[$p_sVar] = array('scope' => $p_nScope, 'type' => $p_nType, 'mandatory' => $p_bMandatory, 'result' => self::RESULT_NOT_VALIDATED);
            $bResult = true;
        }
else {
            throw new ValidationException("Scope (".$p_nScope.") or Type (".$p_nType.") invalid");
        }

        return $bResult;
    }

    
    /**
     * removeValidation verwijdert een variabele uit de lijst met te valideren variabelen
     *
     * @access public
     * @param string $p_sVar De naam van de te verwijderen variabele
     * @return boolean true als variabele succesvol is verwijderd
     */

    public function removeValidation($p_sVar) {
        $bResult = false;
        if (isset($this->m_aValidationList[$p_sVar])) {
            unset($this->m_aValidationList[$p_sVar]);
            $bResult = true;
        }

        return $bResult;
    }

    
    /**
     * getResult Geeft alle validatieresulaten terug als array
     *
     * Geeft lijst terug met gevalideerde variabelen
     * formaat [variabele]['scope'],[variabele]['type'],[variabele]['mandatory'],[variabele]['result']  
     * @access public
     * @return array Lijst met validatieresulaten
     */

    public function getResult() {
        return $this->m_aValidationList;
    }

    
    /**
     * getValue Geeft de waarde van de gevraagde variabele terug
     *
     * @access public
     * @param string $p_sVar De naam van de variabele
     * @param int $p_nScope De scope waarin de variabele moet voorkomen
     * @return variant De waarde van de variabele, null als de variabele niet bestaat
     * @throws ValidationException
     */

    public function getValue($p_sVar, $p_nScope) {
        $vResult = null;
        if ($this->isValidScope($p_nScope)) {
            foreach($this->m_aScopeOrder as $nCurrScope) {
                // zoek in de volgorde van de scopes
                //echo ('getValue: '.$p_sVar.' : scope '.$nCurrScope.'<br/>');

                
                if ($vResult === null) {
                    // nog geen waarde gevonden
                    if (($p_nScope & $nCurrScope) != 0) {
                        switch ($nCurrScope) {
                            case
self::SCOPE_COOKIE :
                                if (isset($_COOKIE)) {
                                    if (isset($_COOKIE[$p_sVar])) {
                                        $vResult = $_COOKIE[$p_sVar];
                                    }
                                }

                            break;
                            case
self::SCOPE_SESSION :
                                if (isset($_SESSION)) {
                                    if (isset($_SESSION[$p_sVar])) {
                                        $vResult = $_SESSION[$p_sVar];
                                    }
                                }

                            break;
                            case
self::SCOPE_POST:
                                if (isset($_POST)) {
                                    if (isset($_POST[$p_sVar])) {
                                        $vResult = $_POST[$p_sVar];
                                    }
                                }

                            break;
                            case
self::SCOPE_GET :
                                if (isset($_GET)) {
                                    if (isset($_GET[$p_sVar])) {
                                        $vResult = $_GET[$p_sVar];
                                    }
                                }

                            break;
                            default :

                                // foute scope, null
                        }
                    }
                }
else {
                    // waarde is al gevonden, stop met zoeken
                    break;
                }
            }
        }
else {
            throw new ValidationException("Scope (".$p_nScope.") invalid");
        }

        //echo ('getValue: '.$p_sVar.' : '.$vResult.'<br/>');
        return $vResult;
    }

    
    /**
     * clearValidation leegt de lijst met te valideren variabelen
     * @access public
     * @return void
     */

    public function clearValidation() {
        $this->m_aValidationList = array();
    }

    
    /**
     * setScopeOrder Zet de volgorde waarin de verschillende scopes worden getest
     *
     * Deze methode zet de volgorde waarin de variabelen in hun scope worden getest.
     * Als een variabele in meerdere scopes bestaat, wordt alleen de eerste gevalideerd. Als niet alle
     * scopes worden opgegeven wordt de standaard volgorde aangehouden van de niet opgegeven scopes
     * @access public
     * @param int $p_nScope1 De eerste scope  
     * @param int $p_nScope2 De tweede scope  
     * @param int $p_nScope3 De derde scope  
     * @param int $p_nScope4 De vierde scope
     * @return void  
     */

    public function setScopeOrder($p_nScope1, $p_nScope2 = 0, $p_nScope3 = 0, $p_nScope4 = 0) {
        $nScopesLeft = self::SCOPE_ANY;
        $aNewOrder = array();
        $aScopes = array($p_nScope1, $p_nScope2, $p_nScope3, $p_nScope4);
        // behandel alle opgegeven scopes
        foreach($aScopes as $nScope) {
            if (($nScope == self::SCOPE_COOKIE) || ($nScope == self::SCOPE_SESSION) || ($nScope == self::SCOPE_POST) || ($nScope == self::SCOPE_GET)) {
                // geldige scope opgegeven
                if ((($p_nScope1 & $nScopesLeft) != 0)) {
                    // scope is nog niet gebruikt
                    $aNewOrder[] = $nScope;
                    // schakel gebruikte scope uit
                    $nScopesLeft &= ~$nScope;
                }
            }
        }

        // voeg niet behandelde scopes toe achteraan de volgordelijst
        if ($nScopesLeft != 0) {
            if (($nScopesLeft & self::SCOPE_COOKIE) != 0) {
                $aNewOrder[] = self::SCOPE_COOKIE;
            }

            if (($nScopesLeft & self::SCOPE_SESSION) != 0) {
                $aNewOrder[] = self::SCOPE_SESSION;
            }

            if (($nScopesLeft & self::SCOPE_POST) != 0) {
                $aNewOrder[] = self::SCOPE_POST;
            }

            if (($nScopesLeft & self::SCOPE_GET) != 0) {
                $aNewOrder[] = self::SCOPE_GET;
            }
        }

        $this->m_aScopeOrder = $aNewOrder;
    }

    
    /**
     * checkType controleert  de gegeven waarde tegen het gegeven type
     *
     * @access private
     * @param variant $p_vValue    de waarde
     * @param int $p_nType Het te controleren type
     * @return int het resultaat van de controle (RESULT_OK, RESULT_INVALID)
     */

    private function checkType($p_vValue, $p_nType) {
        $nResult = self::RESULT_INVALID;
        try {
            switch ($p_nType) {
                case
self::TYPE_STRING :
                    if (is_string($p_vValue)) {
                        $nResult = self::RESULT_OK;
                    }
else {
                        if (is_numeric($p_vValue)) {
                            $nResult = self::RESULT_CONVERTABLE;
                        }
                    }

                break;
                case
self::TYPE_BOOLEAN :
                    if (is_bool($p_vValue)) {
                        $nResult = self::RESULT_OK;
                    }
else {
                        // convertable als het 1 of 0 bevat
                        if (is_numeric($p_vValue) && ((intVal($p_vValue) == 1) || (intVal($p_vValue) == 0))) {
                            $nResult = self::RESULT_CONVERTABLE;
                        }
                    }

                break;
                case
self::TYPE_INT :
                    if (is_int($p_vValue)) {
                        $nResult = self::RESULT_OK;
                    }
else {
                        // convertable als het een getal bevat
                        if (is_numeric($p_vValue)) {
                            $nResult = self::RESULT_CONVERTABLE;
                        }
                    }

                break;
                case
self::TYPE_FLOAT :
                    $p_vValue = str_replace(",", ".", $p_vValue);
                    if (is_float($p_vValue)) {
                        $nResult = self::RESULT_OK;
                    }
else {
                        // convertable als het een getal bevat
                        if (is_numeric($p_vValue)) {
                            $nResult = self::RESULT_CONVERTABLE;
                        }
                    }

                break;
                case
self::TYPE_ARRAY :
                    if (is_array($p_vValue)) {
                        $nResult = self::RESULT_OK;
                    }

                break;
                case
self::TYPE_URL :
                    if (CheckLib::checkURL($p_vValue)) {
                        $nResult = self::RESULT_OK;
                    }

                break;
                case
self::TYPE_EMAIL :
                    if (CheckLib::checkEmail($p_vValue)) {
                        $nResult = self::RESULT_OK;
                    }

                break;
                case
self::TYPE_ZIPCODE :
                    if (CheckLib::checkDutchZipcode($p_vValue)) {
                        $nResult = self::RESULT_OK;
                    }

                break;
                case
self::TYPE_PLAINTEXT :
                    if (CheckLib::checkPlainText($p_vValue)) {
                        $nResult = self::RESULT_OK;
                    }

                break;
                case
self::TYPE_ALPHATEXT :
                    if (CheckLib::checkAlphaText($p_vValue)) {
                        $nResult = self::RESULT_OK;
                    }

                break;
                case
self::TYPE_PHONE :
                    if (CheckLib::checkPhoneNumber($p_vValue)) {
                        $nResult = self::RESULT_OK;
                    }

                break;
                default :
// resultaat invalid
            }
        }
catch (Exception $e) {
            // bij een fout wordt het resultaat invalid
            $nResult = self::RESULT_INVALID;
        }
    
        return $nResult;
    }

    
    /**
     * isValidScope checkt de gegeven scope
     *
     * @access private
     * @param int $p_nScope
     * @return boolean true als scope correct is
     */

    private function isValidScope($p_nScope) {
        if (($p_nScope & self::SCOPE_ANY) == $p_nScope) {
            return true;
        }

        return false;
    }

    
    /**
     * isValidType checkt het gegeven type
     *
     * @access private
     * @param int $p_nType
     * @return boolean true als type correct is
     */

    private function isValidType($p_nType) {
        switch ($p_nType) {
            case
  self::TYPE_STRING :
            case
  self::TYPE_BOOLEAN :
            case
  self::TYPE_INT :
            case
  self::TYPE_FLOAT :
            case
  self::TYPE_ARRAY :
            case
    self::TYPE_URL :
            case
  self::TYPE_EMAIL :
            case
  self::TYPE_ZIPCODE :
            case
  self::TYPE_PLAINTEXT :
            case
    self::TYPE_ALPHATEXT :
            case
  self::TYPE_PHONE :
                return true;
            break;
            default :
return false;
        }
    }

    
    /**
     * getPrintableResult geeft een string met het resultaat terug (voor debugging)
     */

    public static function getPrintableResult($p_nResult) {
        $sResult = 'Unknown result';
        switch ($p_nResult) {
            case
self::RESULT_NOT_VALIDATED :
                $sResult = 'NOT_VALIDATED';
            break;
            case
self::RESULT_OK :
                $sResult = 'OK';
            break;
            case
self::RESULT_CONVERTABLE :
                $sResult = 'CONVERTABLE';
            break;
            case
self::RESULT_EMPTY :
                $sResult = 'EMPTY';
            break;
            case
self::RESULT_NON_EXISTENT :
                $sResult = 'NON_EXISTENT';
            break;
            case
self::RESULT_INVALID :
                $sResult = 'INVALID';
            break;
        }
    
        return $sResult;
    }

    
    /**
     * debug toon alle input variabelen
     */

    public function debug() {
        $sHTML = '';
        if (isset($_GET)) {
            foreach($_GET as $var => $value) {
                $sHTML .= "GET : " . $var . " -> " . $value . "<br/>";
            }
        }

        if (isset($_POST)) {
            foreach($_POST as $var => $value) {
                $sHTML .= "POST : " . $var . " -> " . $value . "<br/>";
            }
        }

        if (isset($_COOKIE)) {
            foreach($_COOKIE as $var => $value) {
                $sHTML .= "COOKIE : " . $var . " -> " . $value . "<br/>";
            }
        }

        if (isset($_SESSION)) {
            foreach($_SESSION as $var => $value) {
                $sHTML .= "SESSION : " . $var . " -> " . $value . "<br/>";
            }
        }

        return $sHTML;
    }
    
}

?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.