Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require 'settings.php';
- require 'vendor/autoload.php';
- function create_response($response) {
- @header('Content-Type: application/json');
- return json_encode($response);
- };
- // Фильтрация значений полей //
- function has_correct_value($name, $value, $filters) {
- foreach ($filters as $filter) {
- if (!$filter($value)) {
- return false;
- }
- };
- return true;
- };
- function send_message($array) {
- global $alias;
- global $settings;
- $message = "
- <p>" . $settings["mail"]["message"] . "</p>
- <br>
- <p><strong>Дата обращения: </strong>" . date('d.m.Y H:i:s') . "</p>
- ";
- foreach($array as $field) {
- $name = $field["name"];
- $value = $field["value"];
- if ($name !== "g-recaptcha-response" && $name !== "agreement") {
- $message .= "<p><strong>" . $alias[$name] . ":</strong> " . $value . "</p>";
- }
- };
- $message .= "<br><br><p>Это письмо сгенерировано автоматически. Пожалуйста, не отвечайте на него.</p>";
- $mail = new PHPMailer;
- $mail->isSMTP();
- $mail->Host = 'smtp.yandex.ru';
- $mail->SMTPAuth = true;
- $mail->Username = $settings["mail"]["auth"]["username"];
- $mail->Password = $settings["mail"]["auth"]["password"];
- $mail->SMTPSecure = "tsl";
- $mail->Port = 587;
- $mail->CharSet = "UTF-8";
- $mail->Subject = $settings["mail"]["subject"];
- $mail->SetFrom($settings["mail"]["sender"]["address"], $settings["mail"]["sender"]["alias"]);
- $mail->MsgHTML($message);
- $mail->AddAddress($settings["mail"]["receiver"]["address"], $settings["mail"]["receiver"]["alias"]);
- $mail->send();
- };
- // Функции — Фильтры //
- $is_filled_string = function($string) {
- return !(preg_match("/^\s+$/", $string) || strlen($string) === 0);
- };
- $is_phone = function($string) {
- return preg_match("/^\+?(\d.*){3,}$/", $string);
- };
- $is_email = function($string) {
- return preg_match("/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i", $string);
- };
- $is_correct_contact = function($string) {
- global $is_email;
- global $is_phone;
- return $is_email($string) || $is_phone($string);
- };
- $is_agree = function($value) {
- return $value === true;
- };
- $is_shorter_than = function($string) {
- return strlen($string) <= 180;
- };
- $is_correct_captcha = function($public_key) {
- global $settings;
- $secretKey = $settings["captcha"]["secret_key"];
- $ip = $_SERVER['REMOTE_ADDR'];
- $postdata = http_build_query(array(
- 'secret' => $secretKey,
- 'response' => $public_key,
- 'remoteip' => $ip,
- ));
- $opts = array(
- 'http' =>
- array(
- 'method' => 'POST',
- 'header' => 'Content-type: application/x-www-form-urlencoded',
- 'content' => $postdata
- )
- );
- $context = stream_context_create($opts);
- $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify", false, $context);
- $responseKeys = json_decode($response,true);
- return $responseKeys["success"];
- };
- // Настройки //
- $JSON_request = json_decode(file_get_contents('php://input'), true);
- $response = array();
- $filters = array(
- "name" => array(
- $is_filled_string,
- $is_shorter_than
- ),
- "contact" => array(
- $is_filled_string,
- $is_correct_contact
- ),
- "point" => array(
- $is_filled_string,
- $is_shorter_than
- ),
- "agreement" => array(
- $is_filled_string,
- $is_agree
- ),
- "g-recaptcha-response" => array(
- $is_filled_string,
- $is_correct_captcha
- )
- );
- $alias = array(
- "name" => "Имя",
- "contact" => "Email или номер телефона",
- "point" => "Текст вопроса"
- );
- foreach($JSON_request as $field) {
- $name = $field["name"];
- $value = $field["value"];
- if ( !has_correct_value($name, $value, $filters[$name]) ) {
- array_push($response, $name);
- }
- };
- if (empty($response)) {
- send_message($JSON_request);
- };
- echo create_response($response);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement