InputService.php 3.88 KB
Newer Older
Olivier PEREZ's avatar
Olivier PEREZ committed
1
<?php
2
3
4
5
6
7
/**
 * This software is governed by the CeCILL-B license. If a copy of this license
 * is not distributed with this file, you can obtain one at
 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
 *
 * Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ
8
 * Authors of Framadate/OpenSondage: Framasoft (https://github.com/framasoft)
9
10
11
12
13
14
15
16
17
18
 *
 * =============================
 *
 * Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
 * ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
 *
 * Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
 * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
 */
Olivier PEREZ's avatar
Olivier PEREZ committed
19
namespace Framadate\Services;
Olivier Perez's avatar
Olivier Perez committed
20
use DateTime;
Olivier PEREZ's avatar
Olivier PEREZ committed
21
22
23
24
25
26
27
28
29
30

/**
 * This class helps to clean all inputs from the users or external services.
 */
class InputService {
    function __construct() {}

    /**
     * This method filter an array calling "filter_var" on each items.
     * Only items validated are added at their own indexes, the others are not returned.
31
32
33
34
     * @param array $arr The array to filter
     * @param int $type The type of filter to apply
     * @param array|null $options The associative array of options
     * @return array The filtered array
Olivier PEREZ's avatar
Olivier PEREZ committed
35
     */
36
    function filterArray(array $arr, $type, $options = null) {
Olivier PEREZ's avatar
Olivier PEREZ committed
37
38
39
40
41
42
43
44
45
46
47
48
        $newArr = [];

        foreach($arr as $id=>$item) {
            $item = filter_var($item, $type, $options);
            if ($item !== false) {
                $newArr[$id] = $item;
            }
        }

        return $newArr;
    }

49
50
51
52
    function filterAllowedValues($value, array $allowedValues) {
        return in_array($value, $allowedValues, true) ? $value : null;
    }

53
    public function filterTitle($title) {
54
        return $this->returnIfNotBlank($title);
55
56
    }

57
58
59
60
61
    public function filterId($id) {
        $filtered = filter_var($id, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => POLL_REGEX]]);
        return $filtered ? substr($filtered, 0, 64) : false;
    }

62
    public function filterName($name) {
Antonin's avatar
Antonin committed
63
        $filtered = trim($name);
64
        return $this->returnIfNotBlank($filtered);
65
66
67
68
69
70
71
72
    }

    public function filterMail($mail) {
        return filter_var($mail, FILTER_VALIDATE_EMAIL);
    }

    public function filterDescription($description) {
        $description = str_replace("\r\n", "\n", $description);
73
        return $description;
74
75
    }

76
77
78
    public function filterMD5($control) {
        return filter_var($control, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => MD5_REGEX]]);
    }
79

80
    public function filterInteger($int) {
81
82
83
84
85
86
        return filter_var($int, FILTER_VALIDATE_INT);
    }

    public function filterValueMax($int)
    {
        return $this->filterInteger($int) >= 1;
87
    }
88

89
90
91
92
93
94
95
96
97
    public function filterBoolean($boolean) {
        return !!filter_var($boolean, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => BOOLEAN_TRUE_REGEX]]);
    }

    public function filterEditable($editable) {
        return filter_var($editable, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => EDITABLE_CHOICE_REGEX]]);
    }

    public function filterComment($comment) {
Olivier Perez's avatar
Olivier Perez committed
98
        $comment = str_replace("\r\n", "\n", $comment);
99
        return $this->returnIfNotBlank($comment);
100
101
    }

Olivier Perez's avatar
Olivier Perez committed
102
103
104
105
106
    public function filterDate($date) {
        $dDate = DateTime::createFromFormat(__('Date', 'datetime_parseformat'), $date)->setTime(0, 0, 0);
        return $dDate->format('Y-m-d H:i:s');
    }

107
108
109
110
111
112
113
    /**
     * Return the value if it's not blank.
     *
     * @param string $filtered The value
     * @return string|null
     */
    private function returnIfNotBlank($filtered) {
114
115
116
117
118
        if ($filtered) {
            $withoutSpaces = str_replace(' ', '', $filtered);
            if (!empty($withoutSpaces)) {
                return $filtered;
            }
119
        }
120
121

        return null;
122
    }
Olivier Perez's avatar
Olivier Perez committed
123
}