summaryrefslogtreecommitdiffstats
path: root/app/class/form.class.php
blob: 8f9d936caa36b39f0166de4e6077673d8cd6ebff (plain) (blame)
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
<?php

/*
 * SCROTT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * For more information, please refer to UNLICENSE
 */

/*
 * Model web-forms and simplify the process of accepting, validating, and
 * sanitizing user input.
 */
class form
{
    /*
     * These arrays track the meta-data of all fields defined on this
     * form.  This mainly includes their type, range (if numeric), and
     * whether or not the field is required.
     */
    private $textFields = array();
    private $numbFields = array();
    private $enumFields = array();

    /*
     * Add new text field to this form
     */
    public function text(string $name, bool $required = true,
        string $deflt = "") : void
    {
        $this->textFields[] = array(
            'name' => $name,
            'required' => $required,
            'default' => $deflt,
        );
    }

    /*
     * Add new numeric field to this form
     */
    public function numeric(string $name, ?int $min = NULL, ?int $max = NULL,
        int $deflt = 0, bool $isInt = true, bool $required = true) : void
    {
        $this->numbFields[] = array(
            'name' => $name,
            'min' => $min,
            'max' => $max,
            'default' => $deflt,
            'isInt' => $isInt,
            'required' => $required,
        );
    }

    /*
     * Add new enumeration field to this form
     */
    public function enum(string $name, array $values, $deflt = NULL,
        bool $required = true) : void
    {
        $this->enumFields[] = array(
            'name' => $name,
            'values' => $values,
            'default' => $deflt,
            'required' => $required,
        );
    }

    /*
     * Add new boolean field to this form
     */
    public function flag(string $name) : void
    {
        $this->enum($name, array("1", "0"), "0", false);
    }

    /*
     * Populate this form with input data from web page.  If an error
     * is encountered, or a required field is missing, false is returned.
     */
    public function populate(array $input) : bool
    {
        /* detect duplicate names */
        $names = array();

        foreach ($this->textFields as $field)
            $names[] = $field['name'];

        foreach ($this->numbFields as $field)
            $names[] = $field['name'];

        foreach ($this->enumFields as $field)
            $names[] = $field['name'];

        if (count(array_unique($names)) != count($names))
            throw new Exception("Internal error: Duplicate field names defined in form");

        /* init text fields */
        foreach ($this->textFields as $field)
        {
            if (isset($input[$field['name']]) && $input[$field['name']] != "")
                $this->{$field['name']} = htmlentities($input[$field['name']], ENT_QUOTES);

            else if ($field['required'])
                logError(ERROR, $field['name'] . " is required");

            else
                $this->{$field['name']} = $field['default'];
        }

        /* init numeric fields */
        foreach ($this->numbFields as $field)
        {
            if (isset($input[$field['name']]) && $input[$field['name']] != "")
            {
                if (!is_numeric($input[$field['name']]))
                {
                    logError(ERROR, $field['name'] . " must be numeric");
                    continue;
                }

                if ($field['isInt'] && (floor($input[$field['name']]) != $input[$field['name']]))
                {
                    logError(ERROR, $field['name'] . " must be an integer");
                    continue;
                }

                if (!is_null($field['min']) && ($input[$field['name']] < $field['min']))
                {
                    logError(ERROR, $field['name'] . " must be no less than " . $field['min']);
                    continue;
                }

                if (!is_null($field['max']) && ($input[$field['name']] > $field['max']))
                {
                    logError(ERROR, $field['name'] . " must be no more than " . $field['max']);
                    continue;
                }

                $this->{$field['name']} = $input[$field['name']];
            }

            else if ($field['required'])
                logError(ERROR, $field['name'] . " is required");

            else
                $this->{$field['name']} = $field['default'];
        }

        /* init enum fields */
        foreach ($this->enumFields as $field)
        {
            if (isset($input[$field['name']]) && $input[$field['name']] != "")
            {
                if (array_search($input[$field['name']], $field['values']) === false)
                {
                    logError(ERROR, $field['name'] . " is not an appropriate value");
                    continue;
                }

                $this->{$field['name']} = $input[$field['name']];
            }

            else if ($field['required'])
                logError(ERROR, $field['name'] . " is required");

            else
                $this->{$field['name']} = $field['default'];
        }

        return !isError(ERROR);
    }
}

?>