<commit_before><?php
namespace Scat\Web;

use \Psr\Container\ContainerInterface;
use \Slim\Http\ServerRequest as Request;
use \Slim\Http\Response as Response;
use \Slim\Views\Twig as View;
use \Respect\Validation\Validator as v;

class Contact {
  private $view, $data, $config;

  public function __construct(
    View $view,
    \Scat\Service\Config $config,
    \Scat\Service\Email $email,
    \Scat\Service\Data $data
  ) {
    $this->view= $view;
    $this->config= $config;
    $this->email= $email;
    $this->data= $data;
  }

  function handleContact(Request $request, Response $response)
  {
    $email= trim($request->getParam('email'));
    if (!v::email()->validate($email)) {
      throw new \Exception("Sorry, you must provide a valid email address.");
    }

    $name= trim($request->getParam('name'));
    $ip= $request->getAttribute('ip_address');

    $subject= $request->getParam('subject') ?: 'Contact';
    $message= $request->getParam('comment');

    $key= $this->config->get('cleantalk.key');
    if ($key) {
      $req= new \Cleantalk\CleantalkRequest();
      $req->auth_key= $key;
      $req->agent= 'php-api';
      $req->sender_email= $email;
      $req->sender_ip= $ip;
      $req->sender_nickname= $name;
      $req->js_on= $request->getParam('scriptable');
      $req->message= $message;
      // Calculate how long they took to fill out the form
      $when= $request->getParam('when');
      $now= $request->getServerParam('REQUEST_TIME_FLOAT');
      $req->submit_time= (int)($now - $when);

      $ct= new \Cleantalk\Cleantalk();
      $ct->server_url= 'http://moderate.cleantalk.org/api2.0/';

      $res= $ct->isAllowMessage($req);
      if ($res->allow == 1) {
        error_log("Message allowed. Reason = " . $res->comment);
      } else {
        error_log("Message forbidden. Reason = " . $res->comment);
        throw new \Exception("Sorry, your message looks like spam.");
      }
    }

    if (preg_match('/(erype|bitcoin|cryptocurrency|sexy?.*girl|seowriters|telegra\.ph|goo\\.gl|go\\.obermatsa\\.com|0j35)/i', $message)) {
      throw new \Exception("Sorry, your message looks like spam.");
    }

    $body= $this->view->fetch('email/contact.html', [
      'TIME' => $request->getServerParam('REQUEST_TIME_FLOAT'),
      'name' => $name,
      'email' => $email,
      'subject' => $subject,
      'message' => $message,
      'ip' => $ip,
    ]);

    /* We are sending mail to ourselves, but we set the Reply-To */
    $to= $this->email->default_from_address();

    $this->email->send($to, $subject, $body, NULL, [ 'replyTo' => $email, 'no_bcc' => true ]);

    return $response->withRedirect('/contact/thanks');
  }
}<commit_msg>Check name for spam<commit_after><?php
namespace Scat\Web;

use \Psr\Container\ContainerInterface;
use \Slim\Http\ServerRequest as Request;
use \Slim\Http\Response as Response;
use \Slim\Views\Twig as View;
use \Respect\Validation\Validator as v;

class Contact {
  private $view, $data, $config;

  public function __construct(
    View $view,
    \Scat\Service\Config $config,
    \Scat\Service\Email $email,
    \Scat\Service\Data $data
  ) {
    $this->view= $view;
    $this->config= $config;
    $this->email= $email;
    $this->data= $data;
  }

  function handleContact(Request $request, Response $response)
  {
    $email= trim($request->getParam('email'));
    if (!v::email()->validate($email)) {
      throw new \Exception("Sorry, you must provide a valid email address.");
    }

    $name= trim($request->getParam('name'));
    $ip= $request->getAttribute('ip_address');

    $subject= $request->getParam('subject') ?: 'Contact';
    $message= $request->getParam('comment');

    $key= $this->config->get('cleantalk.key');
    if ($key) {
      $req= new \Cleantalk\CleantalkRequest();
      $req->auth_key= $key;
      $req->agent= 'php-api';
      $req->sender_email= $email;
      $req->sender_ip= $ip;
      $req->sender_nickname= $name;
      $req->js_on= $request->getParam('scriptable');
      $req->message= $message;
      // Calculate how long they took to fill out the form
      $when= $request->getParam('when');
      $now= $request->getServerParam('REQUEST_TIME_FLOAT');
      $req->submit_time= (int)($now - $when);

      $ct= new \Cleantalk\Cleantalk();
      $ct->server_url= 'http://moderate.cleantalk.org/api2.0/';

      $res= $ct->isAllowMessage($req);
      if ($res->allow == 1) {
        error_log("Message allowed. Reason = " . $res->comment);
      } else {
        error_log("Message forbidden. Reason = " . $res->comment);
        throw new \Exception("Sorry, your message looks like spam.");
      }
    }

    if (preg_match('/(BlahGaky)/i', $name)) {
      throw new \Exception("Sorry, your message looks like spam.");
    }


    if (preg_match('/(erype|bitcoin|cryptocurrency|sexy?.*girl|seowriters|telegra\.ph|goo\\.gl|go\\.obermatsa\\.com|0j35)/i', $message)) {
      throw new \Exception("Sorry, your message looks like spam.");
    }

    $body= $this->view->fetch('email/contact.html', [
      'TIME' => $request->getServerParam('REQUEST_TIME_FLOAT'),
      'name' => $name,
      'email' => $email,
      'subject' => $subject,
      'message' => $message,
      'ip' => $ip,
    ]);

    /* We are sending mail to ourselves, but we set the Reply-To */
    $to= $this->email->default_from_address();

    $this->email->send($to, $subject, $body, NULL, [ 'replyTo' => $email, 'no_bcc' => true ]);

    return $response->withRedirect('/contact/thanks');
  }
}
{'fraction_non_alphanumeric': 0.18704512372634644, 'fraction_numerical': 0.002183406113537118, 'mean_word_length': 3.129977460555973, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2065727', 'n_tokens_mistral': 1889, 'n_tokens_neox': 1829, 'n_words': 388}