Sanatize.php 1.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/2/16
 * Time: 10:57 PM
 */

namespace qfq;

Carsten  Rose's avatar
Carsten Rose committed
11
use qfq;
12
13
use qfq\exceptions\CodeException;

14
require_once(__DIR__ . '/../../qfq/Constants.php');
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35


class Sanatize {

    private function __construct() {
        // Class should never be instantiated
    }

    /**
     * Sanatize: check value by sanatize class. Return verified value or empty string if failed.
     *
     * @param string $value - value to check
     * @param string $sanatizeClass - class used to check
     * @return string - if check failed: empty string, if check is ok: value
     * @throws CodeException
     */
    public static function sanatize($value, $sanatizeClass = SANATIZE_DEFAULT) {

        switch ($sanatizeClass) {
            case SANATIZE_DEFAULT:
            case SANATIZE_DIGIT:
36
37
38
39
40
41
            $newvalue = (string)$value;
            // remove '-'
            if (substr($newvalue, 0, 1) === "-") {
                $newvalue = substr($newvalue, 1);
            }
            if (!ctype_digit($newvalue)) {
42
                    $value = '';
43
            }
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
                break;

            case SANATIZE_ALNUMX:
                // replace ALNUMX valid characters, which are not part of ctype_alnum, by valid ctype_alnum characters, to fake ctype_alnum
                if (!ctype_alnum(str_replace(array('@', '-', '_', '.', ',', ';'), 'a', $value)))
                    $value = '';
                break;

            case SANATIZE_ALL:
                break;

            default:
                throw new CodeException("Sanatize class '$sanatizeClass' unknown. Used to sanatize GET/POST Variable.", ERROR_UNKNOW_SANATIZE_CLASS);
                break;
        }

        return ($value);
    }

}