Commit f0c1b820 authored by Tino Goratsch's avatar Tino Goratsch

- converted the Comments module to use the new validation rules

- minor bug fixes and improvements
parent 2ac736c6
<?php
namespace ACP3\Core\Validator\ValidationRules;
use ACP3\Core\Date;
/**
* Class FloodBarrierValidationRule
* @package ACP3\Core\Validator\ValidationRules
*/
class FloodBarrierValidationRule extends AbstractValidationRule
{
const NAME = 'flood_barrier';
/**
* @var \ACP3\Core\Date
*/
protected $date;
/**
* FloodBarrierValidationRule constructor.
*
* @param \ACP3\Core\Date $date
*/
public function __construct(Date $date)
{
$this->date = $date;
}
/**
* @inheritdoc
*/
public function isValid($data, $field = '', array $extra = [])
{
$floodTime = !empty($extra['last_date']) ? $this->date->timestamp($extra['last_date'], true) + 30 : 0;
$time = $this->date->timestamp('now', true);
return $floodTime <= $time;
}
}
\ No newline at end of file
......@@ -29,6 +29,6 @@ class MinLengthValidationRule extends AbstractValidationRule
*/
protected function checkMinLength($value, $length)
{
return mb_strlen($value) >= $length;
return mb_strlen(trim($value)) >= $length;
}
}
\ No newline at end of file
......@@ -18,6 +18,6 @@ class NotEmptyValidationRule extends AbstractValidationRule
return $this->isValid($data[$field], $field, $extra);
}
return !empty($data);
return !empty(trim($data));
}
}
\ No newline at end of file
......@@ -7,6 +7,12 @@ services:
tags:
- { name: core.validator.validation_rule }
core.validator.validation_rules.flood_barrier_validation_rule:
class: ACP3\Core\Validator\ValidationRules\FloodBarrierValidationRule
arguments: [@core.date]
tags:
- { name: core.validator.validation_rule }
core.validator.validation_rules.form_token_validation_rule:
class: ACP3\Core\Validator\ValidationRules\FormTokenValidationRule
arguments: [@core.request, @core.session]
......
......@@ -3,6 +3,7 @@ parameters:
captcha.helpers.fqdn: 'ACP3\Modules\ACP3\Captcha\Helpers'
captcha.installer.schema.fqdn: 'ACP3\Modules\ACP3\Captcha\Installer\Schema'
captcha.installer.migration.fqdn: 'ACP3\Modules\ACP3\Captcha\Installer\Migration'
captcha.validator.validation_rules.captcha_validation_rule.fqdn: 'ACP3\Modules\ACP3\Captcha\Validator\ValidationRules\CaptchaValidationRule'
services:
captcha.controller.frontend.index:
......@@ -18,3 +19,9 @@ services:
captcha.installer.migration:
class: %captcha.installer.migration.fqdn%
captcha.validator.validation_rules.captcha_validation_rule:
class: %captcha.validator.validation_rules.captcha_validation_rule.fqdn%
arguments: [@core.acl, @core.request, @core.router, @core.session, @core.user]
tags:
- { name: core.validator.validation_rule }
\ No newline at end of file
<?php
namespace ACP3\Modules\ACP3\Captcha\Validator\ValidationRules;
use ACP3\Core\ACL;
use ACP3\Core\Http\RequestInterface;
use ACP3\Core\Router;
use ACP3\Core\SessionHandler;
use ACP3\Core\User;
use ACP3\Core\Validator\ValidationRules\AbstractValidationRule;
/**
* Class CaptchaValidationRule
* @package ACP3\Modules\ACP3\Captcha\Validator\ValidationRules
*/
class CaptchaValidationRule extends AbstractValidationRule
{
const NAME = 'captcha';
/**
* @var \ACP3\Core\ACL
*/
protected $acl;
/**
* @var \ACP3\Core\Http\RequestInterface
*/
protected $request;
/**
* @var \ACP3\Core\Router
*/
protected $router;
/**
* @var \ACP3\Core\SessionHandler
*/
protected $sessionHandler;
/**
* @var \ACP3\Core\User
*/
protected $user;
/**
* CaptchaValidationRule constructor.
*
* @param \ACP3\Core\ACL $acl
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\Router $router
* @param \ACP3\Core\User $user
* @param \ACP3\Core\SessionHandler $sessionHandler
*/
public function __construct(
ACL $acl,
RequestInterface $request,
Router $router,
SessionHandler $sessionHandler,
User $user
)
{
$this->acl = $acl;
$this->request = $request;
$this->router = $router;
$this->sessionHandler = $sessionHandler;
$this->user = $user;
}
/**
* @inheritdoc
*/
public function isValid($data, $field = '', array $extra = [])
{
if (is_array($data) && array_key_exists($field, $data)) {
return $this->isValid($data[$field], $field, $extra);
}
if ($this->acl->hasPermission('frontend/captcha/index/image') === true &&
$this->user->isAuthenticated() === false
) {
return $this->checkCaptcha($data, isset($extra['path']) ? $extra['path'] : '');
}
return true;
}
protected function checkCaptcha($input, $path)
{
$index = 'captcha_' . sha1($this->router->route(empty($path) === true ? $this->request->getQuery() : $path));
return preg_match('/^[a-zA-Z0-9]+$/', $input) && strtolower($input) === strtolower($this->sessionHandler->get($index, ''));
}
}
\ No newline at end of file
......@@ -124,7 +124,7 @@ class Details extends Core\Modules\AdminController
->setTitlePostfix($comment['name']);
if ($this->request->getPost()->isEmpty() === false) {
return $this->_editPost($this->request->getPost()->all(), $comment, $id);
return $this->_editPost($this->request->getPost()->all(), $comment, $id, $comment['module_id']);
}
if ($this->emoticonsHelpers) {
......@@ -210,25 +210,29 @@ class Details extends Core\Modules\AdminController
* @param array $formData
* @param array $comment
* @param int $id
* @param int $moduleId
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function _editPost(array $formData, array $comment, $id)
protected function _editPost(array $formData, array $comment, $id, $moduleId)
{
return $this->actionHelper->handleEditPostAction(function () use ($formData, $comment, $id) {
$this->commentsValidator->validateEdit($formData);
$updateValues = [];
$updateValues['message'] = Core\Functions::strEncode($formData['message']);
if ((empty($comment['user_id']) || $this->get('core.validator.rules.misc')->isNumber($comment['user_id']) === false) && !empty($formData['name'])) {
$updateValues['name'] = Core\Functions::strEncode($formData['name']);
}
return $this->actionHelper->handleEditPostAction(
function () use ($formData, $comment, $id) {
$this->commentsValidator->validateEdit($formData);
$updateValues = [];
$updateValues['message'] = Core\Functions::strEncode($formData['message']);
if ((empty($comment['user_id']) || $this->get('core.validator.rules.misc')->isNumber($comment['user_id']) === false) && !empty($formData['name'])) {
$updateValues['name'] = Core\Functions::strEncode($formData['name']);
}
$bool = $this->commentRepository->update($updateValues, $id);
$bool = $this->commentRepository->update($updateValues, $id);
$this->formTokenHelper->unsetFormToken();
$this->formTokenHelper->unsetFormToken();
return $bool;
});
return $bool;
},
'acp/comments/details/index/id_' . $moduleId
);
}
}
......@@ -5,7 +5,7 @@
{$error_msg}
{/if}
<form action="{$REQUEST_URI}" method="post" accept-charset="UTF-8" class="form-horizontal" data-ajax-form="true" data-ajax-form-loading-text="{lang t="system|loading_please_wait"}">
{if isset($form.user_id) && $form.user_id == '0'}
{if !isset($form.user_id)}
<div class="form-group">
<label for="name" class="col-sm-2 control-label required">{lang t="system|name"}</label>
......
......@@ -10,7 +10,7 @@
<div class="navbar-header">
<strong class="navbar-brand">
{if !is_null($row.user_id)}
<a href="{uri args="users/view_profile/id_`$row.user_id`"}" title="{lang t="users|view_profile"}">{$row.name}</a>
<a href="{uri args="users/index/view_profile/id_`$row.user_id`"}" title="{lang t="users|view_profile"}">{$row.name}</a>
{else}
{$row.name}
{/if}
......
......@@ -5,6 +5,7 @@ parameters:
comments.helpers.fqdn: 'ACP3\Modules\ACP3\Comments\Helpers'
comments.model.commentrepository.fqdn: 'ACP3\Modules\ACP3\Comments\Model\CommentRepository'
comments.validator.fqdn: 'ACP3\Modules\ACP3\Comments\Validator'
comments.validator.validation_rules.user_name_validation_rule.fqdn: 'ACP3\Modules\ACP3\Comments\Validator\ValidationRules\UserNameValidationRule'
comments.installer.schema.fqdn: 'ACP3\Modules\ACP3\Comments\Installer\Schema'
comments.installer.migration.fqdn: 'ACP3\Modules\ACP3\Comments\Installer\Migration'
......@@ -36,7 +37,13 @@ services:
comments.validator:
class: %comments.validator.fqdn%
arguments: ['@core.lang', '@core.validator.rules.misc', '@core.validator.rules.captcha', '@core.acl', '@core.user', '@core.date', '@core.modules', '@comments.model.commentrepository']
arguments: ['@core.lang', @core.validator, '@core.validator.rules.misc', '@core.modules', '@comments.model.commentrepository']
comments.validator.validation_rules.user_name_validation_rule:
class: %comments.validator.validation_rules.user_name_validation_rule.fqdn%
arguments: [@core.validator.validation_rules.integer_validation_rule]
tags:
- { name: core.validator.validation_rule }
comments.installer.schema:
class: %comments.installer.schema.fqdn%
......
......@@ -2,6 +2,7 @@
namespace ACP3\Modules\ACP3\Comments;
use ACP3\Core;
use ACP3\Modules\ACP3\Captcha\Validator\ValidationRules\CaptchaValidationRule;
use ACP3\Modules\ACP3\Comments\Model\CommentRepository;
/**
......@@ -10,22 +11,6 @@ use ACP3\Modules\ACP3\Comments\Model\CommentRepository;
*/
class Validator extends Core\Validator\AbstractValidator
{
/**
* @var \ACP3\Core\Validator\Rules\Captcha
*/
protected $captchaValidator;
/**
* @var \ACP3\Core\ACL
*/
protected $acl;
/**
* @var \ACP3\Core\User
*/
protected $user;
/**
* @var \ACP3\Core\Date
*/
protected $date;
/**
* @var \ACP3\Core\Modules
*/
......@@ -34,72 +19,78 @@ class Validator extends Core\Validator\AbstractValidator
* @var \ACP3\Modules\ACP3\Comments\Model\CommentRepository
*/
protected $commentRepository;
/**
* @var \ACP3\Core\Validator\Validator
*/
protected $validator;
/**
* Validator constructor.
*
* @param \ACP3\Core\Lang $lang
* @param \ACP3\Core\Validator\Validator $validator
* @param \ACP3\Core\Validator\Rules\Misc $validate
* @param \ACP3\Core\Validator\Rules\Captcha $captchaValidator
* @param \ACP3\Core\ACL $acl
* @param \ACP3\Core\User $user
* @param \ACP3\Core\Date $date
* @param \ACP3\Core\Modules $modules
* @param \ACP3\Modules\ACP3\Comments\Model\CommentRepository $commentRepository
*/
public function __construct(
Core\Lang $lang,
Core\Validator\Validator $validator,
Core\Validator\Rules\Misc $validate,
Core\Validator\Rules\Captcha $captchaValidator,
Core\ACL $acl,
Core\User $user,
Core\Date $date,
Core\Modules $modules,
CommentRepository $commentRepository
)
{
parent::__construct($lang, $validate);
$this->captchaValidator = $captchaValidator;
$this->acl = $acl;
$this->user = $user;
$this->date = $date;
$this->validator = $validator;
$this->modules = $modules;
$this->commentRepository = $commentRepository;
}
/**
* @param array $formData
* @param $ip
* @param array $formData
* @param string $ip
*
* @throws Core\Exceptions\InvalidFormToken
* @throws Core\Exceptions\ValidationFailed
*/
public function validateCreate(array $formData, $ip)
{
$this->validateFormKey();
$this->validator
->addConstraint(Core\Validator\ValidationRules\FormTokenValidationRule::NAME)
->addConstraint(
Core\Validator\ValidationRules\FloodBarrierValidationRule::NAME,
[
'message' => $this->lang->t('system', 'flood_no_entry_possible'),
'extra' => [
'last_date' => $this->commentRepository->getLastDateFromIp($ip)
]
])
->addConstraint(
Core\Validator\ValidationRules\NotEmptyValidationRule::NAME,
[
'data' => $formData,
'field' => 'name',
'message' => $this->lang->t('system', 'name_to_short')
])
->addConstraint(
Core\Validator\ValidationRules\NotEmptyValidationRule::NAME,
[
'data' => $formData,
'field' => 'message',
'message' => $this->lang->t('system', 'message_to_short')
])
->addConstraint(
CaptchaValidationRule::NAME,
[
'data' => $formData,
'field' => 'captcha',
'message' => $this->lang->t('captcha', 'invalid_captcha_entered')
]
);
// Flood Sperre
$flood = $this->commentRepository->getLastDateFromIp($ip);
$floodTime = !empty($flood) ? $this->date->timestamp($flood, true) + 30 : 0;
$time = $this->date->timestamp('now', true);
$this->errors = [];
if ($floodTime > $time) {
$this->errors[] = sprintf($this->lang->t('system', 'flood_no_entry_possible'), $floodTime - $time);
}
if (empty($formData['name'])) {
$this->errors['name'] = $this->lang->t('system', 'name_to_short');
}
if (strlen($formData['message']) < 3) {
$this->errors['message'] = $this->lang->t('system', 'message_to_short');
}
if ($this->acl->hasPermission('frontend/captcha/index/image') === true &&
$this->user->isAuthenticated() === false &&
$this->captchaValidator->captcha($formData['captcha']) === false
) {
$this->errors['captcha'] = $this->lang->t('captcha', 'invalid_captcha_entered');
}
$this->_checkForFailedValidation();
$this->validator->validate();
}
/**
......@@ -110,17 +101,24 @@ class Validator extends Core\Validator\AbstractValidator
*/
public function validateEdit(array $formData)
{
$this->validateFormKey();
$this->errors = [];
if ((empty($comment['user_id']) || $this->validate->isNumber($comment['user_id']) === false) && empty($formData['name'])) {
$this->errors['name'] = $this->lang->t('system', 'name_to_short');
}
if (strlen($formData['message']) < 3) {
$this->errors['message'] = $this->lang->t('system', 'message_to_short');
}
$this->validator
->addConstraint(Core\Validator\ValidationRules\FormTokenValidationRule::NAME)
->addConstraint(
Validator\ValidationRules\UserNameValidationRule::NAME,
[
'data' => $formData,
'field' => ['name', 'user_id'],
'message' => $this->lang->t('system', 'name_to_short')
])
->addConstraint(
Core\Validator\ValidationRules\NotEmptyValidationRule::NAME,
[
'data' => $formData,
'field' => 'message',
'message' => $this->lang->t('system', 'message_to_short'),
]);
$this->_checkForFailedValidation();
$this->validator->validate();
}
/**
......@@ -133,14 +131,33 @@ class Validator extends Core\Validator\AbstractValidator
{
$this->validateFormKey();
$this->errors = [];
if (empty($formData['dateformat']) || ($formData['dateformat'] !== 'long' && $formData['dateformat'] !== 'short')) {
$this->errors['dateformat'] = $this->lang->t('system', 'select_date_format');
}
if ($this->modules->isActive('emoticons') === true && (!isset($formData['emoticons']) || ($formData['emoticons'] != 0 && $formData['emoticons'] != 1))) {
$this->errors['emoticons'] = $this->lang->t('comments', 'select_emoticons');
$this->validator
->addConstraint(Core\Validator\ValidationRules\FormTokenValidationRule::NAME)
->addConstraint(
Core\Validator\ValidationRules\InArrayValidationRule::NAME,
[
'data' => $formData,
'field' => 'dateformat',
'message' => $this->lang->t('system', 'select_date_format'),
'extra' => [
'haystack' => ['long', 'short']
]
]);
if ($this->modules->isActive('emoticons')) {
$this->validator
->addConstraint(
Core\Validator\ValidationRules\InArrayValidationRule::NAME,
[
'data' => $formData,
'field' => 'emoticons',
'message' => $this->lang->t('comments', 'select_emoticons'),
'extra' => [
'haystack' => [0, 1]
]
]);
}
$this->_checkForFailedValidation();
$this->validator->validate();
}
}
<?php
namespace ACP3\Modules\ACP3\Comments\Validator\ValidationRules;
use ACP3\Core\Validator\ValidationRules\AbstractValidationRule;
use ACP3\Core\Validator\ValidationRules\IntegerValidationRule;
/**
* Class UserNameValidationRule
* @package ACP3\Modules\ACP3\Comments\Validator\ValidationRules
*/
class UserNameValidationRule extends AbstractValidationRule
{
const NAME = 'comments_user_name';
/**
* @var \ACP3\Core\Validator\ValidationRules\IntegerValidationRule
*/
protected $integerValidationRule;
/**
* UserNameValidationRule constructor.
*
* @param \ACP3\Core\Validator\ValidationRules\IntegerValidationRule $integerValidationRule
*/
public function __construct(IntegerValidationRule $integerValidationRule)
{
$this->integerValidationRule = $integerValidationRule;
}
/**
* @inheritdoc
*/
public function isValid($data, $field = '', array $extra = [])
{
if (is_array($data) && is_array($field)) {
$userName = reset($field);
$userId = next($field);
return (!empty($data[$userId]) && $this->integerValidationRule->isValid($data[$userId])) || !empty($data[$userName]);
};
return false;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment