Example #1
0
<?php

/**
 * Licensed under The GPL-3.0 License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @since    2.0.0
 * @author   Christopher Castro <*****@*****.**>
 * @link     http://www.quickappscms.org
 * @license  http://opensource.org/licenses/gpl-3.0.html GPL-3.0 License
 */
use Captcha\CaptchaManager;
/**
 * Registers "Are You A Human" CAPTCHA adapter
 */
CaptchaManager::addAdapter('ayah', 'Captcha\\Adapter\\AyahAdapter');
/**
 * Registers "reCaptcha" CAPTCHA adapter
 */
CaptchaManager::addAdapter('reCaptcha', 'Captcha\\Adapter\\RecaptchaAdapter');
 /**
  * Renders CAPTCHA form element.
  *
  * @return string HTML
  */
 public function captcha()
 {
     if ($this->config('use_captcha')) {
         return CaptchaManager::adapter()->render($this->_View);
     }
     return '';
 }
 /**
  * Creates a validation object on the fly.
  *
  * @return \Cake\Validation\Validator
  */
 protected function _createValidator()
 {
     $config = $this->config();
     if ($config['validator'] instanceof Validator) {
         return $config['validator'];
     }
     $this->_controller->loadModel('Comment.Comments');
     if ($this->_controller->request->is('userLoggedIn')) {
         // logged user posting
         $validator = $this->_controller->Comments->validationDefault(new Validator());
         $validator->requirePresence('user_id')->notEmpty('user_id', __d('comment', 'Invalid user.'))->add('user_id', 'checkUserId', ['rule' => function ($value, $context) {
             if (!empty($value)) {
                 $valid = TableRegistry::get('User.Users')->find()->where(['Users.id' => $value, 'Users.status' => 1])->count() === 1;
                 return $valid;
             }
             return false;
         }, 'message' => __d('comment', 'Invalid user, please try again.'), 'provider' => 'table']);
     } elseif ($this->config('settings.allow_anonymous')) {
         // anonymous user posting
         $validator = $this->_controller->Comments->validator('anonymous');
     } else {
         // other case
         $validator = new Validator();
     }
     if ($this->config('settings.use_captcha')) {
         $validator->add('body', 'humanCheck', ['rule' => function ($value, $context) {
             return CaptchaManager::adapter()->validate($this->_controller->request);
         }, 'message' => __d('comment', 'We were not able to verify you as human. Please try again.'), 'provider' => 'table']);
     }
     return $validator;
 }