summaryrefslogblamecommitdiffstats
path: root/app/class/form.class.php
blob: 502e34885babfed75406f74aef83c2c7b04db569 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13












                                                                                          

                                    
 








                                    














                                           


































                                                                                         























































































                                                                                           


  
<?php

/*
 * Model web-forms and simplify the process of accepting, validating, and sanitizing input
 */
class Form
{
    /*
     * Constructor
     */
    function __construct()
    {
        $this->textFields = array();
        $this->numbFields = array();
        $this->enumFields = array();

        $this->errorlist  = array();
    }

    /*
     * Log an error
     */
    function logError($str)
    {
        $this->errorlist[] = $str;
    }

    /*
     * Add new text field to the form
     */
    function field_text($name, $req = true)
    {
        if ($req !== true)
            $req = false;

        $this->textFields[] = array(
            'name' => $name,
            'req'  => $req
        );
    }

    /*
     * Add new numeric field to the form
     */
    function field_numeric($name, $req = true, $integer = true, $min = null, $max = null)
    {
        if ($req !== true)
            $req = false;

        if ($integer !== true)
            $integer = false;

        $this->numbFields[] = array(
            'name' => $name,
            'req'  => $req,
            'int'  => $integer,
            'min'  => $min,
            'max'  => $max
        );
    }

    /*
     * Add new enumeration field to the form
     */
    function field_enum($name, $req = true, $values)
    {
        if ($req !== true)
            $req = false;

        $this->enumFields[] = array(
            'name' => $name,
            'req'  => $req,
            'vals' => $values
        );
    }

    /*
     * Populate the form with input data from web page
     */
    function populate($input)
    {
        /* detect duplicate names */
        $names = array();
        foreach ($this->textFields as $fld)
            $names[] = $fld['name'];
        foreach ($this->numbFields as $fld)
            $names[] = $fld['name'];
        foreach ($this->enumFields as $fld)
            $names[] = $fld['name'];

        if (count(array_unique($names)) != count($names))
        {
            $this->logError("Internal error: Duplicate field names defined in form");
            return false;
        }

        /* init text fields */
        foreach ($this->textFields as $fld)
        {
            if (isset($input[$fld['name']]))
                $this->$fld['name'] = htmlEntities($input[$fld['name']], ENT_QUOTES);

            else if ($fld['req'])
                $this->logError($fld['name'] . " is required");
        }

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

                if ($fld['int'] && (floor($input[$fld['name']]) != $input[$fld['name']]))
                {
                    $this->logError($fld['name'] . " must be an integer");
                    continue;
                }

                if (!is_null($fld['min']) && ($input[$fld['name']] < $fld['min']))
                {
                    $this->logError($fld['name'] . " must be no less than " . $fld['min']);
                    continue;
                }

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

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

            else if ($fld['req'])
                $this->logError($fld['name'] . " is required");
        }

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

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

            else if ($fld['req'])
                $this->logError($fld['name'] . " is required");
        }

        /* return */
        return count($this->errorlist) == 0;
    }
}

?>