Commit 6cd0ad89 authored by Tino Goratsch's avatar Tino Goratsch

mapped most of the old validation rule logic to the new validation rules

parent c89cb7a0
......@@ -2,6 +2,8 @@
namespace ACP3\Core\Validator\Rules;
use ACP3\Core;
use ACP3\Modules\ACP3\Permissions\Validator\ValidationRules\PrivilegesExistValidationRule;
use ACP3\Modules\ACP3\Permissions\Validator\ValidationRules\RolesExistValidationRule;
/**
* Class ACL
......@@ -12,16 +14,27 @@ use ACP3\Core;
class ACL
{
/**
* @var \ACP3\Core\ACL
* @var \ACP3\Modules\ACP3\Permissions\Validator\ValidationRules\PrivilegesExistValidationRule
*/
protected $acl;
protected $privilegesExistValidationRule;
/**
* @var \ACP3\Modules\ACP3\Permissions\Validator\ValidationRules\RolesExistValidationRule
*/
protected $rolesExistValidationRule;
/**
* @param \ACP3\Core\ACL $acl
* ACL constructor.
*
* @param \ACP3\Modules\ACP3\Permissions\Validator\ValidationRules\PrivilegesExistValidationRule $privilegesExistValidationRule
* @param \ACP3\Modules\ACP3\Permissions\Validator\ValidationRules\RolesExistValidationRule $rolesExistValidationRule
*/
public function __construct(Core\ACL $acl)
public function __construct(
PrivilegesExistValidationRule $privilegesExistValidationRule,
RolesExistValidationRule $rolesExistValidationRule
)
{
$this->acl = $acl;
$this->privilegesExistValidationRule = $privilegesExistValidationRule;
$this->rolesExistValidationRule = $rolesExistValidationRule;
}
/**
......@@ -37,21 +50,7 @@ class ACL
*/
public function aclPrivilegesExist(array $privileges)
{
$allPrivileges = $this->acl->getAllPrivileges();
$c_allPrivileges = count($allPrivileges);
$valid = false;
for ($i = 0; $i < $c_allPrivileges; ++$i) {
$valid = false;
foreach ($privileges as $module) {
foreach ($module as $privilegeId => $value) {
if ($privilegeId == $allPrivileges[$i]['id'] && $value >= 0 && $value <= 2) {
$valid = true;
}
}
}
}
return $valid;
return $this->privilegesExistValidationRule->isValid($privileges);
}
/**
......@@ -64,17 +63,6 @@ class ACL
*/
public function aclRolesExist(array $roles)
{
$allRoles = $this->acl->getAllRoles();
$good = [];
foreach ($allRoles as $row) {
$good[] = $row['id'];
}
foreach ($roles as $row) {
if (in_array($row, $good) === false) {
return false;
}
}
return true;
return $this->rolesExistValidationRule->isValid($roles);
}
}
......@@ -2,6 +2,7 @@
namespace ACP3\Core\Validator\Rules;
use ACP3\Core;
use ACP3\Modules\ACP3\Captcha\Validator\ValidationRules\CaptchaValidationRule;
/**
* Class Captcha
......@@ -12,37 +13,21 @@ use ACP3\Core;
class Captcha
{
/**
* @var \ACP3\Core\Http\RequestInterface
* @var \ACP3\Modules\ACP3\Captcha\Validator\ValidationRules\CaptchaValidationRule
*/
protected $request;
/**
* @var \ACP3\Core\Router
*/
protected $router;
/**
* @var \ACP3\Core\SessionHandler
*/
protected $sessionHandler;
protected $captchaValidationRule;
/**
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\Router $router
* @param \ACP3\Core\SessionHandler $sessionHandler
* Captcha constructor.
*
* @param \ACP3\Modules\ACP3\Captcha\Validator\ValidationRules\CaptchaValidationRule $captchaValidationRule
*/
public function __construct(
Core\Http\RequestInterface $request,
Core\Router $router,
Core\SessionHandler $sessionHandler
)
public function __construct(CaptchaValidationRule $captchaValidationRule)
{
$this->request = $request;
$this->router = $router;
$this->sessionHandler = $sessionHandler;
$this->captchaValidationRule = $captchaValidationRule;
}
/**
* Überpürft, ob das eingegebene Captcha mit dem generierten übereinstimmt
*
* @param string $input
* @param string $path
*
......@@ -52,8 +37,6 @@ class Captcha
*/
public function captcha($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, '')) ? true : false;
return $this->captchaValidationRule->isValid($input, '', ['path' => $path]);
}
}
<?php
namespace ACP3\Core\Validator\Rules;
use ACP3\Core\Validator\ValidationRules\DateValidationRule;
use ACP3\Core\Validator\ValidationRules\TimeZoneExistsValidationRule;
use ACP3\Modules\ACP3\Users\Validator\ValidationRules\BirthdayValidationRule;
/**
* Class Date
* @package ACP3\Core\Validator\Rules
......@@ -9,6 +13,36 @@ namespace ACP3\Core\Validator\Rules;
*/
class Date
{
/**
* @var \ACP3\Core\Validator\ValidationRules\DateValidationRule
*/
protected $dateValidationRule;
/**
* @var \ACP3\Core\Validator\ValidationRules\TimeZoneExistsValidationRule
*/
protected $timeZoneExistsValidationRule;
/**
* @var \ACP3\Modules\ACP3\Users\Validator\ValidationRules\BirthdayValidationRule
*/
protected $birthdayValidationRule;
/**
* Date constructor.
*
* @param \ACP3\Core\Validator\ValidationRules\DateValidationRule $dateValidationRule
* @param \ACP3\Core\Validator\ValidationRules\TimeZoneExistsValidationRule $timeZoneExistsValidationRule
* @param \ACP3\Modules\ACP3\Users\Validator\ValidationRules\BirthdayValidationRule $birthdayValidationRule
*/
public function __construct(
DateValidationRule $dateValidationRule,
TimeZoneExistsValidationRule $timeZoneExistsValidationRule,
BirthdayValidationRule $birthdayValidationRule)
{
$this->dateValidationRule = $dateValidationRule;
$this->timeZoneExistsValidationRule = $timeZoneExistsValidationRule;
$this->birthdayValidationRule = $birthdayValidationRule;
}
/**
* Überprüft einen Geburtstag auf seine Gültigkeit
*
......@@ -21,14 +55,7 @@ class Date
*/
public function birthday($var)
{
$regex = '/^(\d{4})-(\d{2})-(\d{2})$/';
$matches = [];
if (preg_match($regex, $var, $matches)) {
if (checkdate($matches[2], $matches[3], $matches[1])) {
return true;
}
}
return false;
return $this->birthdayValidationRule->isValid($var);
}
/**
......@@ -45,24 +72,11 @@ class Date
*/
public function date($start, $end = null)
{
$matchesStart = $matchesEnd = [];
$regex = '/^(\d{4})-(\d{2})-(\d{2})( ([01][0-9]|2[0-3])(:([0-5][0-9])){1,2}){0,1}$/';
if (preg_match($regex, $start, $matchesStart)) {
// Wenn ein Enddatum festgelegt wurde, dieses ebenfalls mit überprüfen
if ($end != null && preg_match($regex, $end, $matchesEnd)) {
if (checkdate($matchesStart[2], $matchesStart[3], $matchesStart[1]) &&
checkdate($matchesEnd[2], $matchesEnd[3], $matchesEnd[1]) &&
strtotime($start) <= strtotime($end)
) {
return true;
}
} else { // Nur Startdatum überprüfen
if (checkdate($matchesStart[2], $matchesStart[3], $matchesStart[1])) {
return true;
}
}
}
return false;
$data = [
'start' => $start,
'end' => $end
];
return $this->dateValidationRule->isValid($data, ['start', 'end']);
}
......@@ -76,12 +90,6 @@ class Date
*/
public function timeZone($var)
{
$bool = true;
try {
new \DateTimeZone($var);
} catch (\Exception $e) {
$bool = false;
}
return $bool;
return $this->timeZoneExistsValidationRule->isValid($var);
}
}
......@@ -12,16 +12,18 @@ use ACP3\Core;
class Mime
{
/**
* @var \ACP3\Core\Validator\Rules\Misc
* @var \ACP3\Core\Validator\ValidationRules\PictureValidationRule
*/
protected $validate;
protected $pictureValidationRule;
/**
* @param \ACP3\Core\Validator\Rules\Misc $validate
* Mime constructor.
*
* @param \ACP3\Core\Validator\ValidationRules\PictureValidationRule $pictureValidationRule
*/
public function __construct(Core\Validator\Rules\Misc $validate)
public function __construct(Core\Validator\ValidationRules\PictureValidationRule $pictureValidationRule)
{
$this->validate = $validate;
$this->pictureValidationRule = $pictureValidationRule;
}
/**
......@@ -39,22 +41,11 @@ class Mime
*/
public function isPicture($file, $width = '', $height = '', $filesize = '')
{
$info = getimagesize($file);
$isPicture = $info[2] >= 1 && $info[2] <= 3 ? true : false;
if ($isPicture === true) {
$bool = true;
// Optionale Parameter
if ($this->validate->isNumber($width) && $info[0] > $width ||
$this->validate->isNumber($height) && $info[1] > $height ||
filesize($file) === 0 || $this->validate->isNumber($filesize) && filesize($file) > $filesize
) {
$bool = false;
}
return $bool;
}
return false;
return $this->pictureValidationRule->isValid($file, [], [
'width' => $width,
'height' => $height,
'filesize' => $filesize
]);
}
/**
......@@ -64,14 +55,14 @@ class Mime
*
* @param string $file
* Die zu überprüfende Datei
* @param string $mimetype
* @param string $mimeType
* Der zu vergleichende MIMETYPE
*
* @return mixed
*
* @deprecated
*/
public function mimeType($file, $mimetype = '')
public function mimeType($file, $mimeType = '')
{
$return = '';
......@@ -83,8 +74,8 @@ class Mime
$return = mime_content_type($file);
}
if (!empty($mimetype)) {
return $return == $mimetype ? true : false;
if (!empty($mimeType)) {
return $return == $mimeType ? true : false;
}
}
......
......@@ -2,8 +2,9 @@
namespace ACP3\Core\Validator\Rules;
use ACP3\Core\Http\RequestInterface;
use ACP3\Core\SessionHandler;
use ACP3\Core\Validator\ValidationRules\EmailValidationRule;
use ACP3\Core\Validator\ValidationRules\FormTokenValidationRule;
use ACP3\Core\Validator\ValidationRules\IntegerValidationRule;
/**
* Class Misc
......@@ -14,25 +15,34 @@ use ACP3\Core\SessionHandler;
class Misc
{
/**
* @var \ACP3\Core\Http\RequestInterface
* @var \ACP3\Core\Validator\ValidationRules\EmailValidationRule
*/
protected $request;
protected $emailValidationRule;
/**
* @var \ACP3\Core\SessionHandler
* @var \ACP3\Core\Validator\ValidationRules\FormTokenValidationRule
*/
protected $sessionHandler;
protected $formTokenValidationRule;
/**
* @var \ACP3\Core\Validator\ValidationRules\IntegerValidationRule
*/
protected $integerValidationRule;
/**
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\SessionHandler $sessionHandler
* Misc constructor.
*
* @param \ACP3\Core\Validator\ValidationRules\EmailValidationRule $emailValidationRule
* @param \ACP3\Core\Validator\ValidationRules\FormTokenValidationRule $formTokenValidationRule
* @param \ACP3\Core\Validator\ValidationRules\IntegerValidationRule $integerValidationRule
*/
public function __construct(
RequestInterface $request,
SessionHandler $sessionHandler
EmailValidationRule $emailValidationRule,
FormTokenValidationRule $formTokenValidationRule,
IntegerValidationRule $integerValidationRule
)
{
$this->request = $request;
$this->sessionHandler = $sessionHandler;
$this->emailValidationRule = $emailValidationRule;
$this->formTokenValidationRule = $formTokenValidationRule;
$this->integerValidationRule = $integerValidationRule;
}
/**
......@@ -50,12 +60,7 @@ class Misc
*/
public function email($var)
{
if (function_exists('filter_var')) {
return (bool)filter_var($var, FILTER_VALIDATE_EMAIL);
} else {
$pattern = '/^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/';
return (bool)preg_match($pattern, $var);
}
return $this->emailValidationRule->isValid($var);
}
/**
......@@ -67,11 +72,7 @@ class Misc
*/
public function formToken()
{
$tokenName = SessionHandler::XSRF_TOKEN_NAME;
$urlQueryString = $this->request->getQuery();
$sessionToken = $this->sessionHandler->get($tokenName);
return (isset($sessionToken[$urlQueryString]) && $this->request->getPost()->get($tokenName, '') === $sessionToken[$urlQueryString]);
return $this->formTokenValidationRule->isValid('');
}
/**
......@@ -99,6 +100,6 @@ class Misc
*/
public function isNumber($var)
{
return (bool)preg_match('/^(\d+)$/', $var);
return $this->integerValidationRule->isValid($var);
}
}
<?php
namespace ACP3\Core\Validator\Rules;
use ACP3\Core\Validator\ValidationRules\InternalUriValidationRule;
use ACP3\Core\Validator\ValidationRules\UriSafeValidationRule;
/**
* Class Router
......@@ -9,6 +11,30 @@ namespace ACP3\Core\Validator\Rules;
*/
class Router
{
/**
* @var \ACP3\Core\Validator\ValidationRules\UriSafeValidationRule
*/
protected $uriSafeValidationRule;
/**
* @var \ACP3\Core\Validator\ValidationRules\InternalUriValidationRule
*/
protected $internalUriValidationRule;
/**
* Router constructor.
*
* @param \ACP3\Core\Validator\ValidationRules\UriSafeValidationRule $uriSafeValidationRule
* @param \ACP3\Core\Validator\ValidationRules\InternalUriValidationRule $internalUriValidationRule
*/
public function __construct(
UriSafeValidationRule $uriSafeValidationRule,
InternalUriValidationRule $internalUriValidationRule
)
{
$this->uriSafeValidationRule = $uriSafeValidationRule;
$this->internalUriValidationRule = $internalUriValidationRule;
}
/**
* Überprüft, ob der eingegebene URI-Alias sicher ist, d.h. es dürfen nur
* die Kleinbuchstaben von a-z, Zahlen, der Bindestrich und das Slash eingegeben werden
......@@ -21,7 +47,7 @@ class Router
*/
public function isUriSafe($var)
{
return (bool)preg_match('/^([a-z]{1}[a-z\d\-]*(\/[a-z\d\-]+)*)$/', $var);
return $this->uriSafeValidationRule->isValid($var);
}
/**
......@@ -35,6 +61,6 @@ class Router
*/
public function isInternalURI($var)
{
return (bool)preg_match('/^([a-z\d_\-]+\/){3,}$/', $var);
return $this->internalUriValidationRule->isValid($var);
}
}
......@@ -22,9 +22,7 @@ class Aliases
*
* @param \ACP3\Modules\ACP3\Seo\Validator\ValidationRules\UriAliasValidationRule $uriAliasValidationRule
*/
public function __construct(
Seo\Validator\ValidationRules\UriAliasValidationRule $uriAliasValidationRule
)
public function __construct(Seo\Validator\ValidationRules\UriAliasValidationRule $uriAliasValidationRule)
{
$this->uriAliasValidationRule = $uriAliasValidationRule;
}
......
......@@ -97,26 +97,27 @@ services:
core.validator.rules.acl:
class: ACP3\Core\Validator\Rules\ACL
arguments: ['@core.acl']
arguments: [@permissions.validator.validation_rules.privileges_exist_validation_rule, @permissions.validator.validation_rules.roles_exist_validation_rule]
core.validator.rules.captcha:
class: ACP3\Core\Validator\Rules\Captcha
arguments: ['@core.request', '@core.router', @core.session]
arguments: [@captcha.validator.validation_rules.captcha_validation_rule]
core.validator.rules.date:
class: ACP3\Core\Validator\Rules\Date
arguments: ['@core.validator.rules.misc']
arguments: [@core.validator.validation_rules.date_validation_rule, @core.validator.validation_rules.time_zone_exists_validation_rule, @users.validation.validation_rules.birthday_validation_rule]
core.validator.rules.mime:
class: ACP3\Core\Validator\Rules\Mime
arguments: ['@core.validator.rules.misc']
arguments: [@core.validator.validation_rules.picture_validation_rule]
core.validator.rules.misc:
class: ACP3\Core\Validator\Rules\Misc
arguments: ['@core.request', @core.session]
arguments: [@core.validator.validation_rules.email_validation_rule, @core.validator.validation_rules.form_token_validation_rule, @core.validator.validation_rules.integer_validation_rule]
core.validator.rules.router:
class: ACP3\Core\Validator\Rules\Router
arguments: [@core.validator.validation_rules.uri_safe_validation_rule, @core.validator.validation_rules.internal_uri_validation_rule]
core.validator.rules.router.aliases:
class: ACP3\Core\Validator\Rules\Router\Aliases
......
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