HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/spire.komma-mediadesign.nl/wwwroot/mvc/models/form_field.class.php
<?php
/**
 * m_form.php
 * Created by Komma Mediadesign.
 * Author: mike
 * Date: 3/27/13
 */
class Form_Field extends SiteTemplate
{
    /*
     * Name of the form this fields belongs to
     */
    private $_formName;

    /*
     * Field string id
     */
    private $_id;

    /*
     * Field string name
     */
    private $_label;

    /*
     * Field string name
     */
    private $_name;

    /*
     * Field string type
     */
    private $_type = 'text';

    /*
     * Field string type
     */
    private $_value;

    /*
     * Options for select / radio inputs
     */
    private $_options = array();

    /*
     * Error
     */
    private $_error;
    /*
     * Validations options
     * Current options:
     * - Required
     * - Numeric
     * - E-mail
     * - Price
     */
    private $_validation = array();

    /*
     * Field array type
     */
    private $_classes = array();

    public function __construct($label)
    {
        $this->_label = $label;
        $this->_name = $this->camelCase($label);
        $this->_id = $this->_name;
    }

    /*
     * SETS
     */
    public function setId($id)
    {
        if( ! empty($id))
        {
            $this->_id = $id;
        }
    }

    public function setValue($value)
    {
        if( ! empty($value))
        {
            $this->_value = $value;
        }
    }

    public function setType($type)
    {
        if( ! empty($type))
        {
            $this->_type = $type;
        }
    }

    public function addOption($name, $value)
    {
        $this->_options[] = array('name'=>$name,'value'=>$value);
    }

    public function defaultValue()
    {
        if(isset($_SESSION[$this->_formName][$this->_name]))
        {
            $this->_value = $_SESSION[$this->_formName][$this->_name];
        }
        else{
            $this->_value = $this->_label;
        }
    }

    public function addClass($className)
    {
        if( ! empty($className))
        {
            if(is_array($className))
            {
                foreach($className as $name)
                {
                    $this->_classes[] = $name;
                }
            }
            else
            {
                $this->_classes[] = $className;
            }
        }
    }

    public function addValidation($validation)
    {
        if( ! empty($validation))
        {
            if(is_array($validation))
            {
                foreach($validation as $val)
                {
                    $this->_validation[] = $val;
                }
            }
            else
            {
                $this->_validation[] = $validation;
            }
        }
    }

    public function setFormName($name)
    {
        if( ! empty($name))
        {
            $this->_formName = $name;
        }
    }

    public function get()
    {
        $data = array();

        $data['label'] = $this->_label;
        $data['name'] = $this->_name;
        $data['type'] = $this->_type;
        $data['id'] = $this->_id;
        $data['value'] = $this->_value;
        $data['classes'] = $this->_classes;
        $data['options'] = $this->_options;
        $data['validation'] = $this->_validation;

        return $data;
    }

    public function validate()
    {
        if(count($this->_validation > 0))
        {
            if(in_array('required_isset',$this->_validation))
            {
                if( ! isset($_POST[$this->_formName . '_' . $this->_name]))
                {
                    $this->_error = 'Please enter ' . $this->_label;
                    return false;
                }
            }

            $input = null;
            if(isset($_POST[$this->_formName . '_' . $this->_name])) $input = $_POST[$this->_formName . '_' . $this->_name];

            if(in_array('required',$this->_validation))
            {
                if(empty($input) || $input == $this->_label)
                {
                    $this->_error = 'Please enter ' . $this->_label;
                    return false;
                }
            }

            if(in_array('email',$this->_validation))
            {
                if(!$this->checkmail($input))
                {
                    $this->_error = 'Not a valid e-mail address';
                    return false;
                }
            }

            if(in_array('recaptcha',$this->_validation))
            {
                $resp = recaptcha_check_answer (RECAPTHA_PRIVATE,$_SERVER["REMOTE_ADDR"],$_POST["recaptcha_challenge_field"],$_POST["recaptcha_response_field"]);

                if ( ! $resp->is_valid) {
                    $this->_error = 'The reCAPTCHA was not entered correctly. Please try it again';
                    return false;
                }
            }
        }
        return true;
    }

    public function getError()
    {
        return $this->_error;
    }

    public function checkmail($email) {
        // First, we check that there's one @ symbol, and that the lengths are right
        if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$/", $email)) {
            // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
            return false;
        }
        // Split it into sections to make life easier
        $email_array = explode("@", $email);
        $local_array = explode(".", $email_array[0]);
        for ($i = 0; $i < sizeof($local_array); $i++) {
            if (!preg_match("/^(([A-Za-z0-9!#$%&'*+\/=?^_`{|}~-][A-Za-z0-9!#$%&'*+\/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/", $local_array[$i])) {
                return false;
            }
        }
        if (!preg_match("/^\[?[0-9\.]+\]?$/", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
            $domain_array = explode(".", $email_array[1]);
            if (sizeof($domain_array) < 2) {
                return false; // Not enough parts to domain
            }
            for ($i = 0; $i < sizeof($domain_array); $i++) {
                if (!preg_match("/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/", $domain_array[$i])) {
                    return false;
                }
            }
        }
        return true;
    }
}