form = $form; $this->id = $id; $this->type = $type; } /** * Set the field value or selected value. * * @param mixed $new_value the value, or an array of selected values ids */ public function setValue($new_value) { // simple value if (!is_array($this->value)) { $this->value = htmlentities($new_value, ENT_COMPAT, 'UTF-8', false); return; } // multiples-values (checkbox, radio, select) if (!is_array($new_value)) { $new_value = [$new_value]; } foreach ($new_value as $i) { $this->selected_values[intval($i)] = true; } } /** * Reset the selected values by finding ones who starts or end with ":". */ public function resetSelectedValues() { $this->selected_values = []; foreach ($this->value as $i => $val) { $value = preg_replace('`(^\s*:|:\s*$)`', '', $val, -1, $count); if ($count) { $this->value[$i] = $value; $this->selected_values[$i] = true; } } } /** * Check field value. * * @return bool */ public function validate() { // empty and required if (empty($this->value) && $this->required) { $this->error = 'field_required'; return false; } // value blacklisted or not in whitelist if ($reason = $this->isBlacklisted()) { $this->error = 'field_' . $reason; return false; } // not empty but not valid if (!empty($this->value) && !$this->isValid()) { $this->error = 'field_' . $this->type; return false; } return true; } /** * Check if field value is valid * Mean different things depending on field type. * * @return bool */ public function isValid() { switch ($this->type) { case 'email': return filter_var($this->value, FILTER_VALIDATE_EMAIL); case 'tel': $pattern = '`^\+?[-0-9(). ]{6,}$$`i'; return preg_match($pattern, $this->value); case 'url': return filter_var($this->value, FILTER_VALIDATE_URL); case 'message': return strlen($this->value) > $this->form->config('message_len'); case 'captcha': return $this->reCaptchaValidity($_POST['g-recaptcha-response']); case 'password': return $this->value == $this->required; default: return true; } } /** * Check if reCaptcha is valid. * * @param mixed $answer * * @return bool */ public function reCaptchaValidity($answer) { if (!$answer) { return false; } $params = [ 'secret' => $this->form->config('recaptcha_secret_key'), 'response' => $answer, ]; $url = 'https://www.google.com/recaptcha/api/siteverify?' . http_build_query($params); if (function_exists('curl_version')) { $curl = curl_init($url); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($curl); } else { $response = file_get_contents($url); } if (empty($response) || is_null($response)) { return false; } return json_decode($response)->success; } /** * Check if field value is blacklisted. * * Search for every comma-separated entry of every checklist * in value, and define if it should or should not be there. * * @return bool */ public function isBlacklisted() { $list = $this->form->config('checklist'); if (!$list) { return; } foreach ($list as $cl) { if ($cl->name != $this->type) { continue; } $content = array_filter(explode(',', $cl->content)); foreach ($content as $avoid) { $found = preg_match("`{$avoid}`", $this->value); $foundBlacklisted = $found && 'blacklist' == $cl->type; $notFoundWhitelisted = !$found && 'whitelist' == $cl->type; if ($foundBlacklisted || $notFoundWhitelisted) { return $cl->type; } } } return false; } /* * Return the html display of the field * * Manage field title, error message, and type-based display * @return string the
*/ public function html() { $id = 'p01-contact' . $this->form->getId() . '_field' . $this->id; $name = 'p01-contact_fields[' . $this->id . ']'; $type = $this->getGeneralType(); $orig = $type != $this->type ? $this->type : ''; $value = $this->value; $disabled = $this->locked ? ' disabled="disabled"' : ''; $required = $this->required ? ' required ' : ''; $placeholder = $this->placeholder ? ' placeholder="' . $this->placeholder . '"' : ''; $is_single_option = is_array($this->value) && 1 == count($this->value) ? 'inline' : ''; $html = "
"; switch ($type) { case 'textarea': $html .= '