$error)); } function get_form_response($success, $data) { if (!is_array($data)) die('data must be array'); $status = array(); $status[$success ? 'FormResponse' : 'MusePHPFormResponse'] = array_merge(array('success' => $success), $data); return json_serialize($status); } function check_required_fields($form) { $errors = array(); foreach ($form['fields'] as $field => $properties) { if (!$properties['required']) continue; if (!array_key_exists($field, $_REQUEST) || ($_REQUEST[$field] !== "0" && empty($_REQUEST[$field]))) array_push($errors, array('field' => $field, 'message' => $properties['errors']['required'])); else if (!check_field_value_format($form, $field, $properties)) array_push($errors, array('field' => $field, 'message' => $properties['errors']['format'])); } if (!empty($errors)) die(get_form_error_response(array('fields' => $errors))); } function check_field_value_format($form, $field, $properties) { $value = get_form_field_value($field, $properties, $form['resources'], false); switch($properties['type']) { case 'checkbox': case 'string': case 'captcha': // no format to validate for those fields return true; case 'checkboxgroup': if (!array_key_exists('optionItems', $properties)) die(get_form_error_response(sprintf($form['resources']['invalid_form_config'], $properties['label']))); // If the value received is not an array, treat it as invalid format if (!isset($value)) return false; // Check each option to see if it is a valid value foreach($value as $checkboxValue) { if (!in_array($checkboxValue, $properties['optionItems'])) return false; } return true; case 'radiogroup': if (!array_key_exists('optionItems', $properties)) die(get_form_error_response(sprintf($form['resources']['invalid_form_config'], $properties['label']))); //check list of real radio values return in_array($value, $properties['optionItems']); case 'recaptcha': if (!array_key_exists('recaptcha', $form) || !array_key_exists('private_key', $form['recaptcha']) || empty($form['recaptcha']['private_key'])) die(get_form_error_response($form['resources']['invalid_reCAPTCHA_private_key'])); $resp = recaptcha_check_answer($form['recaptcha']['private_key'], $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]); return $resp->is_valid; case 'recaptcha2': if (!array_key_exists('recaptcha2', $form) || !array_key_exists('private_key', $form['recaptcha2']) || empty($form['recaptcha2']['private_key'])) die(get_form_error_response($form['resources']['invalid_reCAPTCHA2_private_key'])); $resp = recaptcha2_check_answer($form['recaptcha2']['private_key'], $_POST["g-recaptcha-response"], $_SERVER["REMOTE_ADDR"]); return $resp["success"]; case 'email': return 1 == preg_match('/^[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i', $value); case 'radio': // never validate the format of a single radio element; only the group gets validated default: die(get_form_error_response(sprintf($form['resources']['invalid_field_type'], $properties['type']))); } } /** * Returns an object with following properties: * "success": true|false, * "challenge_ts": timestamp, // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ) * "hostname": string, // the hostname of the site where the reCAPTCHA was solved * "error-codes": [...] // optional; possibe values: * missing-input-secret - The secret parameter is missing * invalid-input-secret - The secret parameter is invalid or malformed * missing-input-response - The response parameter is missing * invalid-input-response - The response parameter is invalid or malformed */ function recaptcha2_check_answer($secret, $response, $remoteIP) { $url = 'https://www.google.com/recaptcha/api/siteverify'; $data = array( 'secret' => $secret, 'response' => $response, 'remoteip' => $remoteIP ); $options = array( 'http' => array( 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($data) ) ); $context = stream_context_create($options); $contents = file_get_contents($url, false, $context); if ($contents === FALSE) { die(get_form_error_response($form['resources']['invalid_reCAPTCHA2_server_response'])); } $result = (array) json_decode($contents); return $result; } function email_form_submission($form) { if(!defined('PHP_EOL')) define('PHP_EOL', '\r\n'); $form_email = ((array_key_exists('Email', $_REQUEST) && !empty($_REQUEST['Email'])) ? cleanup_email($_REQUEST['Email']) : ''); $to = $form['email']['to']; $from = $form['email']['from']; $subject = $form['subject']; $message = get_email_body($subject, $form['heading'], $form['fields'], $form['resources']); $headers = get_email_headers($from, $form_email); $sent = @mail($to, $subject, $message, $headers); if(!$sent) die(get_form_error_response($form['resources']['failed_to_send_email'])); $success_data = array( 'redirect' => $form['success_redirect'] ); echo get_form_response(true, $success_data); } function get_email_headers($to_email, $form_email) { $headers = 'From: ' . $to_email . PHP_EOL; $headers = 'Reply-To' . $form_email . PHP_EOL; $headers .= 'X-Mailer: Adobe Muse CC 2018.1.1.386 with PHP' . PHP_EOL; $headers .= 'Content-type: text/html; charset=utf-8' . PHP_EOL; return $headers; } function get_email_body($subject, $heading, $fields, $resources) { $message = ''; $message .= ''; $message .= '
| ' . encode_for_form($field_wrapper['properties']['label']) . ': | ' . get_form_field_value($field_wrapper['field'], $field_wrapper['properties'], $resources, true) . ' |