<?php

class cand_process {

    public function __construct() {
        
    }

    public function save_setting() {
        $arrRemove = array('submit');
        foreach ($_POST as $key => $value) {
            if (in_array($key, $arrRemove))
                continue;
            update_option($key, $value);
        }
    }

    public function get_contact_email_field($author, $email) {
        $field = array();
        $formcode = stripslashes(get_option('cand_html_code'));
        $pattern = '/<input(.*?)>/s';
        preg_match_all($pattern, $formcode, $matchesArray);
        if (count($matchesArray[0]) > 0) {
            $fieldValue = "";
            for ($i = 0; $i < count($matchesArray[0]); $i++) {
                $matchesName = $this->get_contact_match_data($matchesArray[0][$i], 'name');
                $matchesValue = $this->get_contact_match_data($matchesArray[0][$i], 'value');
                if (count($matchesName[1][0]) > 0) {
                    $fieldname = str_replace(array("'", '"'), "", strtolower($matchesName[1][0]));
                    switch ($fieldname) {
                        case 'name':
                        case 'fields_fname':
                        case 'mmerge3':
                        case 'customfield3600':
                        case 'formvalue_fields[customfield9101]':
                        case 'fullname':
                            $fieldValue[] = $matchesName[1][0] . '=' . $author;
                            break;
                        case 'email':
                        case 'fields_email':
                        case 'formvalue_fields[emailaddress]':
                            $fieldValue[] = $matchesName[1][0] . '=' . $email;
                            break;
                        case 'redirect':
                        case 'errorredirect':
                        case 'bot_redirect':
                            break;
                        default :
                            $fieldValue[] = $matchesName[1][0] . '=' . $matchesValue[1][0];
                            break;
                    }
                }
            }
        }
        return $fieldValue;
    }

    public function get_contact_form_action_method() {
        $field = array();
        $formcode = stripslashes(get_option('cand_html_code'));
        $pattern = '/<form(.*?)>/s';
        preg_match_all($pattern, $formcode, $matchesArray);
        if (count($matchesArray[0]) > 0) {
            $actionarray = $this->get_contact_match_data($matchesArray[0][0], 'action');
            $arrayForm['action'] = $actionarray[1][0];
            $methodarray = $this->get_contact_match_data($matchesArray[0][0], 'method');
            $arrayForm['method'] = strtolower($methodarray[1][0]);
        }
        return $arrayForm;
    }

    public function get_contact_match_data($string, $value) {
        $pattern = '/' . $value . '="(.*?)"/s';
        $pattern2 = '/' . $value . '=(.*?) /s';
        $pattern3 = '/' . $value . '=(.*?)>/s';
        $pattern4 = "/$value='(.*?)'/s";
        preg_match_all($pattern, $string, $matcharray);
        if (is_array($matcharray) && $matcharray[1][0] == "") {
            preg_match_all($pattern2, $string, $matcharray);
        }
        if (is_array($matcharray) && $matcharray[1][0] == "") {
            preg_match_all($pattern3, $string, $matcharray);
        }
        if (is_array($matcharray) && $matcharray[1][0] == "") {
            preg_match_all($pattern4, $string, $matcharray);
        }
        return $matcharray;
    }

    public function submit_contact_post_form($action, $parameter) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $action);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $parameter);
        $result = curl_exec($ch);
        curl_close($ch);
    }

    public function submit_contact_get_form($action, $parameter) {
        $url = $action . '?' . $parameter;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $content = curl_exec($ch);
        curl_close($ch);
    }

}
?>