Advertisement
Guest User

Untitled

a guest
Jul 6th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.16 KB | None | 0 0
  1. <?php
  2.  
  3. require 'settings.php';
  4. require 'vendor/autoload.php';
  5.  
  6. function create_response($response) {
  7.     @header('Content-Type: application/json');
  8.     return json_encode($response);
  9. };
  10.  
  11. // Фильтрация значений полей //
  12.  
  13. function has_correct_value($name, $value, $filters) {
  14.  
  15.     foreach ($filters as $filter) {
  16.  
  17.         if (!$filter($value)) {
  18.             return false;
  19.         }
  20.  
  21.     };
  22.  
  23.     return true;
  24. };
  25.  
  26. function send_message($array) {
  27.     global $alias;
  28.     global $settings;
  29.  
  30.     $message = "
  31.         <p>" . $settings["mail"]["message"] . "</p>
  32.        <br>
  33.        <p><strong>Дата обращения: </strong>" . date('d.m.Y H:i:s') .  "</p>
  34.     ";
  35.     foreach($array as $field) {
  36.         $name = $field["name"];
  37.         $value = $field["value"];
  38.  
  39.         if ($name !== "g-recaptcha-response" && $name !== "agreement") {
  40.             $message .= "<p><strong>" . $alias[$name] . ":</strong> " . $value . "</p>";
  41.         }
  42.     };
  43.     $message .= "<br><br><p>Это письмо сгенерировано автоматически. Пожалуйста, не отвечайте на него.</p>";
  44.  
  45.     $mail = new PHPMailer;
  46.  
  47.     $mail->isSMTP();
  48.     $mail->Host = 'smtp.yandex.ru';
  49.     $mail->SMTPAuth = true;
  50.     $mail->Username = $settings["mail"]["auth"]["username"];
  51.     $mail->Password = $settings["mail"]["auth"]["password"];
  52.     $mail->SMTPSecure = "tsl";
  53.     $mail->Port = 587;
  54.     $mail->CharSet = "UTF-8";
  55.  
  56.     $mail->Subject = $settings["mail"]["subject"];
  57.     $mail->SetFrom($settings["mail"]["sender"]["address"], $settings["mail"]["sender"]["alias"]);
  58.     $mail->MsgHTML($message);
  59.     $mail->AddAddress($settings["mail"]["receiver"]["address"], $settings["mail"]["receiver"]["alias"]);
  60.  
  61.     $mail->send();
  62. };
  63.  
  64. // Функции — Фильтры //
  65.  
  66. $is_filled_string = function($string) {
  67.     return !(preg_match("/^\s+$/", $string) || strlen($string) === 0);
  68. };
  69.  
  70. $is_phone = function($string) {
  71.     return preg_match("/^\+?(\d.*){3,}$/", $string);
  72. };
  73.  
  74. $is_email = function($string) {
  75.     return preg_match("/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i", $string);
  76. };
  77.  
  78. $is_correct_contact = function($string) {
  79.     global $is_email;
  80.     global $is_phone;
  81.  
  82.     return $is_email($string) || $is_phone($string);
  83. };
  84.  
  85. $is_agree = function($value) {
  86.     return $value === true;
  87. };
  88.  
  89. $is_shorter_than = function($string) {
  90.     return strlen($string) <= 180;
  91. };
  92.  
  93. $is_correct_captcha = function($public_key) {
  94.     global $settings;
  95.  
  96.     $secretKey = $settings["captcha"]["secret_key"];
  97.     $ip = $_SERVER['REMOTE_ADDR'];
  98.  
  99.     $postdata = http_build_query(array(
  100.             'secret' => $secretKey,
  101.             'response' => $public_key,
  102.             'remoteip' => $ip,
  103.         ));
  104.  
  105.     $opts = array(
  106.         'http' =>
  107.         array(
  108.             'method'  => 'POST',
  109.             'header'  => 'Content-type: application/x-www-form-urlencoded',
  110.             'content' => $postdata
  111.         )
  112.     );
  113.  
  114.     $context  = stream_context_create($opts);
  115.     $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify", false, $context);
  116.     $responseKeys = json_decode($response,true);
  117.  
  118.     return $responseKeys["success"];
  119. };
  120.  
  121. // Настройки //
  122.  
  123. $JSON_request = json_decode(file_get_contents('php://input'), true);
  124. $response = array();
  125. $filters = array(
  126.     "name" => array(
  127.         $is_filled_string,
  128.         $is_shorter_than
  129.     ),
  130.     "contact" => array(
  131.         $is_filled_string,
  132.         $is_correct_contact
  133.     ),
  134.     "point" => array(
  135.         $is_filled_string,
  136.         $is_shorter_than
  137.     ),
  138.     "agreement" => array(
  139.         $is_filled_string,
  140.         $is_agree
  141.     ),
  142.     "g-recaptcha-response" => array(
  143.         $is_filled_string,
  144.         $is_correct_captcha
  145.     )
  146. );
  147. $alias = array(
  148.     "name" => "Имя",
  149.     "contact" => "Email или номер телефона",
  150.     "point" => "Текст вопроса"
  151. );
  152.  
  153. foreach($JSON_request as $field) {
  154.     $name = $field["name"];
  155.     $value = $field["value"];
  156.  
  157.     if ( !has_correct_value($name, $value, $filters[$name]) ) {
  158.         array_push($response, $name);
  159.     }
  160. };
  161.  
  162. if (empty($response)) {
  163.     send_message($JSON_request);
  164. };
  165.  
  166. echo create_response($response);
  167.  
  168. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement