<commit_before><?php

namespace AkkaFeedback\Controller;

use AkkaFeedback\Controller\AppController;
use Cake\Event\Event;
use Cake\Core\Configure;

/**
 * Feedbacks Controller
 *
 * @property \AkkaFeedback\Model\Table\FeedbacksTable $Feedbacks
 */
class FeedbacksController extends AppController
{

    public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);
        $this->Auth->allow(['add']);
    }

    /**
     * Add method
     *
     * @return void Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $feedback = $this->Feedbacks->newEntity();

        $this->request->data['referrer'] = $this->referer();
        $this->request->data['uri'] = $this->request->env('REQUEST_URI');
        $this->request->data['ip'] = $this->request->clientIp();
        $this->request->data['browser'] = $this->request->env('HTTP_USER_AGENT');

        if ($this->request->is('post')) {

            $feedback = $this->Feedbacks->patchEntity($feedback, $this->request->data);

            if (isset($this->request->data['recaptcha'])) {
                $this->request->data['g-recaptcha-response'] = $this->request->data['recaptcha'];
            }

            if ($this->_validateReCaptcha()) {
                if ($this->Feedbacks->save($feedback)) {
                    $event = new Event('Model.Feedbacks.afterSave', $this, ['feedback' => $feedback]);
                    $this->eventManager()->dispatch($event);

                    $this->Flash->success(__('Your message has been submitted successfully.'));
                } else {
                    $this->Flash->error(__('There was a problem submitting your message. Please try again!.'));
                }
            } else {
                $feedback['error'] = "The reCaptcha submitted is invalid. Please try again!";
                $this->Flash->error('The reCaptcha submitted is invalid. Please try again!');
            }
        }
        $this->set(compact('feedback'));
        $this->set('_serialize', ['feedback']);
    }

    /**
     * Check the POST and validate reCaptcha
     *
     */
    protected function _validateReCaptcha()
    {
        if (Configure::read('Feedbacks.reCaptcha.enable')) {

            return $this->validateReCaptcha(
                    $this->request->data('g-recaptcha-response'), $this->request->clientIp()
            );
        }

        return false;
    }

    /**
     * Validates reCaptcha response
     *
     * @param string $recaptchaResponse response
     * @param string $clientIp client ip<commit_msg>Allowing TRUE to be returned if reCaptcha is not enabled.<commit_after><?php

namespace AkkaFeedback\Controller;

use AkkaFeedback\Controller\AppController;
use Cake\Event\Event;
use Cake\Core\Configure;

/**
 * Feedbacks Controller
 *
 * @property \AkkaFeedback\Model\Table\FeedbacksTable $Feedbacks
 */
class FeedbacksController extends AppController
{

    public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);
        $this->Auth->allow(['add']);
    }

    /**
     * Add method
     *
     * @return void Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $feedback = $this->Feedbacks->newEntity();

        $this->request->data['referrer'] = $this->referer();
        $this->request->data['uri'] = $this->request->env('REQUEST_URI');
        $this->request->data['ip'] = $this->request->clientIp();
        $this->request->data['browser'] = $this->request->env('HTTP_USER_AGENT');

        if ($this->request->is('post')) {

            $feedback = $this->Feedbacks->patchEntity($feedback, $this->request->data);

            if (isset($this->request->data['recaptcha'])) {
                $this->request->data['g-recaptcha-response'] = $this->request->data['recaptcha'];
            }

            if ($this->_validateReCaptcha()) {
                if ($this->Feedbacks->save($feedback)) {
                    $event = new Event('Model.Feedbacks.afterSave', $this, ['feedback' => $feedback]);
                    $this->eventManager()->dispatch($event);

                    $this->Flash->success(__('Your message has been submitted successfully.'));
                } else {
                    $this->Flash->error(__('There was a problem submitting your message. Please try again!.'));
                }
            } else {
                $feedback['error'] = "The reCaptcha submitted is invalid. Please try again!";
                $this->Flash->error('The reCaptcha submitted is invalid. Please try again!');
            }
        }
        $this->set(compact('feedback'));
        $this->set('_serialize', ['feedback']);
    }

    /**
     * Check the POST and validate reCaptcha
     *
     */
    protected function _validateReCaptcha()
    {
        if (Configure::read('Feedbacks.reCaptcha.enable')) {

            return $this->validateReCaptcha(
                    $this->request->data('g-recaptcha-response'), $this->request->clientIp()
            );
        }

        // Since reCaptcha is not enabled, true will be returned.
        return true;
    }

    /**
     * Validates reCaptcha response
     *
     * @param string $recaptchaResponse response
     * @param string $clientIp client ip
{'fraction_non_alphanumeric': 0.15531232307982637, 'fraction_numerical': 0.0, 'mean_word_length': 2.2797029702970297, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 103, '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': '2257233', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1416, 'n_words': 287}