- refactored the validation classes a bit

- did some more work to get the installer running again
parent 5855b449
......@@ -10,7 +10,7 @@ use ACP3\Core;
class Admin extends Frontend
{
/**
* @var \ACP3\Core\Validate
* @var \ACP3\Core\Validator\Rules\Misc
*/
protected $validate;
/**
......@@ -31,7 +31,7 @@ class Admin extends Frontend
Core\View $view,
Core\Breadcrumb $breadcrumb,
Core\SEO $seo,
Core\Validate $validate,
Core\Validator\Rules\Misc $validate,
Core\Session $session,
Core\Router\Aliases $aliases)
{
......@@ -51,7 +51,7 @@ class Admin extends Frontend
}
/**
* @return \ACP3\Core\Validate
* @return \ACP3\Core\Validator\Rules\Misc
*/
public function getValidate()
{
......
......@@ -36,9 +36,9 @@ class Date
*/
protected $lang;
/**
* @var Validate
* @var \ACP3\Core\Validator\Rules\Date
*/
protected $validate;
protected $dateValidator;
/**
* @var \ACP3\Core\View
*/
......@@ -46,13 +46,23 @@ class Date
/**
* Falls man sich als User authentifiziert hat, eingestellte Zeitzone + Sommerzeiteinstellung holen
*
* @param Auth $auth
* @param Lang $lang
* @param Validator\Rules\Date $dateValidator
* @param View $view
*/
function __construct(Auth $auth, Lang $lang, Validate $validate, View $view)
function __construct(
Auth $auth,
Lang $lang,
\ACP3\Core\Validator\Rules\Date $dateValidator,
View $view
)
{
$info = $auth->getUserInfo();
$this->lang = $lang;
$this->validate = $validate;
$this->dateValidator = $dateValidator;
$this->view = $view;
if (!empty($info)) {
......@@ -100,7 +110,15 @@ class Date
* @param bool $inputFieldOnly
* @return string
*/
public function datepicker($name, $value = '', $format = 'Y-m-d H:i', array $params = array(), $range = 1, $withTime = true, $inputFieldOnly = false)
public function datepicker(
$name,
$value = '',
$format = 'Y-m-d H:i',
array $params = array(),
$range = 1,
$withTime = true,
$inputFieldOnly = false
)
{
$datepicker = array(
'range' => is_array($name) === true && $range === 1 ? 1 : 0,
......@@ -127,33 +145,33 @@ class Date
// Veröffentlichungszeitraum
if (is_array($name) === true && $range === 1) {
if (!empty($_POST[$name[0]]) && !empty($_POST[$name[1]])) {
$value_start = $_POST[$name[0]];
$value_end = $_POST[$name[1]];
$value_start_r = $this->format($_POST[$name[0]], 'r', false);
$value_end_r = $this->format($_POST[$name[1]], 'r', false);
} elseif (is_array($value) === true && $this->validate->date($value[0], $value[1]) === true) {
$value_start = $this->format($value[0], $format);
$value_end = $this->format($value[1], $format);
$value_start_r = $this->format($value[0], 'r');
$value_end_r = $this->format($value[1], 'r');
$valueStart = $_POST[$name[0]];
$valueEnd = $_POST[$name[1]];
$valueStartR = $this->format($_POST[$name[0]], 'r', false);
$valueEndR = $this->format($_POST[$name[1]], 'r', false);
} elseif (is_array($value) === true && $this->dateValidator->date($value[0], $value[1]) === true) {
$valueStart = $this->format($value[0], $format);
$valueEnd = $this->format($value[1], $format);
$valueStartR = $this->format($value[0], 'r');
$valueEndR = $this->format($value[1], 'r');
} else {
$value_start = $this->format('now', $format, false);
$value_end = $this->format('now', $format, false);
$value_start_r = $this->format('now', 'r', false);
$value_end_r = $this->format('now', 'r', false);
$valueStart = $this->format('now', $format, false);
$valueEnd = $this->format('now', $format, false);
$valueStartR = $this->format('now', 'r', false);
$valueEndR = $this->format('now', 'r', false);
}
$datepicker['name_start'] = $name[0];
$datepicker['name_end'] = $name[1];
$datepicker['value_start'] = $value_start;
$datepicker['value_start_r'] = $value_start_r;
$datepicker['value_end'] = $value_end;
$datepicker['value_end_r'] = $value_end_r;
$datepicker['value_start'] = $valueStart;
$datepicker['value_start_r'] = $valueStartR;
$datepicker['value_end'] = $valueEnd;
$datepicker['value_end_r'] = $valueEndR;
// Einfaches Inputfeld mit Datepicker
} else {
if (!empty($_POST[$name])) {
$value = $_POST[$name];
} elseif ($this->validate->date($value) === true) {
} elseif ($this->dateValidator->date($value) === true) {
$value = $this->format($value, $format);
} else {
$value = $this->format('now', $format, false);
......
......@@ -10,23 +10,27 @@ use ACP3\Core;
class Alerts
{
/**
* @var \ACP3\Core\Functions
* @var \ACP3\Core\Helpers\Output
*/
private $functions;
protected $outputHelper;
/**
* @var Core\View
*/
private $view;
protected $view;
/**
* @var Core\Request
*/
private $request;
protected $request;
public function __construct(Core\Request $request, Core\View $view, Core\Functions $functions)
public function __construct(
Core\Request $request,
Core\View $view,
Core\Helpers\Output $outputHelper
)
{
$this->request = $request;
$this->view = $view;
$this->functions = $functions;
$this->outputHelper = $outputHelper;
}
/**
......@@ -118,9 +122,9 @@ class Alerts
'content' => $content,
);
$this->functions->outputJson($return);
$this->outputHelper->outputJson($return);
}
return $content;
}
}
\ No newline at end of file
}
\ No newline at end of file
<?php
namespace ACP3\Core\Helpers;
use Symfony\Component\HttpFoundation\JsonResponse;
class Output
{
/**
* @param array $data
*/
public function outputJson(array $data)
{
new JsonResponse($data);
exit;
}
}
\ No newline at end of file
......@@ -20,15 +20,19 @@ class StringFormatter
*/
protected $router;
/**
* @var Core\Validate
* @var Core\Validator\Rules\Router\Aliases
*/
protected $validate;
protected $aliasesValidator;
public function __construct(Core\Modules $modules, Core\Router $router, Core\Validate $validate)
public function __construct(
Core\Modules $modules,
Core\Router $router,
Core\Validator\Rules\Router\Aliases $aliasValidator
)
{
$this->modules = $modules;
$this->router = $router;
$this->validate = $validate;
$this->aliasesValidator = $aliasValidator;
}
/**
......@@ -102,7 +106,7 @@ class StringFormatter
*/
public function rewriteInternalUriCallback($matches)
{
if ($this->validate->uriAliasExists($matches[6]) === true) {
if ($this->aliasesValidator->uriAliasExists($matches[6]) === true) {
return $matches[0];
} else {
$uriArray = explode('/', $matches[6]);
......
......@@ -30,7 +30,7 @@ class TableOfContents
*/
protected $router;
/**
* @var \ACP3\Core\Validate
* @var \ACP3\Core\Validator\Rules\Misc
*/
protected $validate;
/**
......@@ -44,7 +44,7 @@ class TableOfContents
Core\SEO $seo,
Core\Request $request,
Core\Router $router,
Core\Validate $validate,
Core\Validator\Rules\Misc $validate,
Core\View $view
)
{
......
......@@ -15,7 +15,7 @@ abstract class Admin extends Core\Modules\Controller\Frontend
*/
protected $session;
/**
* @var Core\Validate
* @var \ACP3\Core\Validator\Rules\Misc
*/
protected $validate;
/**
......@@ -57,7 +57,7 @@ abstract class Admin extends Core\Modules\Controller\Frontend
{
if (isset($_POST['entries']) && is_array($_POST['entries']) === true) {
$entries = $_POST['entries'];
} elseif ($this->validate->deleteEntries($this->request->entries) === true) {
} elseif ((bool)preg_match('/^((\d+)\|)*(\d+)$/', $this->request->entries) === true) {
$entries = $this->request->entries;
}
......
<?php
namespace ACP3\Core;
use ACP3\Core\Validator\Rules\Misc;
class Pagination
{
/**
* @var Auth
*/
protected $auth;
/**
* @var Breadcrumb
*/
protected $breadcrumb;
/**
* @var Lang
*/
......@@ -22,11 +20,19 @@ class Pagination
/**
* @var Request
*/
protected $uri;
protected $request;
/**
* @var Router
*/
protected $router;
/**
* @var View
*/
protected $view;
/**
* @var Misc
*/
protected $miscValidator;
/**
* @var int
*/
......@@ -66,22 +72,22 @@ class Pagination
function __construct(
Auth $auth,
Breadcrumb $breadcrumb,
Lang $lang,
SEO $seo,
Request $uri,
Request $request,
Router $router,
View $view,
$totalResults)
Misc $miscValidator)
{
$this->auth = $auth;
$this->breadcrumb = $breadcrumb;
$this->lang = $lang;
$this->seo = $seo;
$this->uri = $uri;
$this->request = $request;
$this->router = $router;
$this->view = $view;
$this->miscValidator = $miscValidator;
$this->resultsPerPage = $auth->entries;
$this->totalResults = $totalResults;
}
/**
......@@ -140,8 +146,8 @@ class Pagination
{
$output = '';
if ($this->totalResults > $this->resultsPerPage) {
$link = $this->uri->route(($this->uri->area === 'admin' ? 'acp/' : '') . $this->uri->getUriWithoutPages());
$this->currentPage = Validate::isNumber($this->uri->page) ? (int)$this->uri->page : 1;
$link = $this->router->route(($this->request->area === 'admin' ? 'acp/' : '') . $this->request->getUriWithoutPages());
$this->currentPage = $this->miscValidator->isNumber($this->request->page) ? (int)$this->request->page : 1;
$this->totalPages = (int)ceil($this->totalResults / $this->resultsPerPage);
$this->setMetaStatements($link);
......@@ -202,7 +208,7 @@ class Pagination
}
// Vorherige und nächste Seite für Suchmaschinen und Prefetching propagieren
if ($this->uri->area !== 'admin') {
if ($this->request->area !== 'admin') {
if ($this->currentPage - 1 > 0) {
// Seitenangabe in der Seitenbeschreibung ab Seite 2 angeben
$this->seo->setDescriptionPostfix(sprintf($this->lang->t('system', 'page_x'), $this->currentPage));
......@@ -211,7 +217,7 @@ class Pagination
if ($this->currentPage + 1 <= $this->totalPages) {
$this->seo->setNextPage($link . 'page_' . ($this->currentPage + 1) . '/');
}
if (isset($this->uri->page) && $this->uri->page === 1) {
if (isset($this->request->page) && $this->request->page === 1) {
$this->seo->setCanonicalUri($link);
}
}
......
This diff is collapsed.
......@@ -14,11 +14,14 @@ class AbstractValidator
*/
protected $lang;
/**
* @var \ACP3\Core\Validate
* @var \ACP3\Core\Validator\Rules\Misc
*/
protected $validate;
public function __construct(Core\Lang $lang, Core\Validate $validate)
public function __construct(
Core\Lang $lang,
Rules\Misc $validate
)
{
$this->lang = $lang;
$this->validate = $validate;
......
<?php
namespace ACP3\Core\Validator\Rules;
use ACP3\Core;
/**
* Class ACL
* @package ACP3\Core\Validator\Rules
*/
class ACL
{
/**
* @var Core\ACL
*/
protected $acl;
public function __construct(Core\ACL $acl)
{
$this->acl = $acl;
}
/**
* Überprüft, ob die übergebenen Privilegien existieren und
* plausible Werte enthalten
*
* @param array $privileges
* Array mit den IDs der zu überprüfenden Privilegien mit ihren Berechtigungen
* @return boolean
*/
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;
}
/**
* Überprüft, ob die selektierten Rollen existieren
*
* @param array $roles
* Die zu überprüfenden Rollen
* @return boolean
*/
public function aclRolesExist(array $roles)
{
$allRoles = $this->acl->getAllRoles();
$good = array();
foreach ($allRoles as $row) {
$good[] = $row['id'];
}
foreach ($roles as $row) {
if (in_array($row, $good) === false) {
return false;
}
}
return true;
}
}
\ No newline at end of file
<?php
namespace ACP3\Core\Validator\Rules;
use ACP3\Core;
/**
* Class Captcha
* @package ACP3\Core\Validator\Rules
*/
class Captcha
{
/**
* @var Core\Request
*/
protected $request;
/**
* @var Core\Router
*/
protected $router;
public function __construct(
Core\Request $request,
Core\Router $router
)
{
$this->request = $request;
$this->router = $router;
}
/**
* Überpürft, ob das eingegebene Captcha mit dem generierten übereinstimmt
*
* @param string $input
* @param string $path
* @return boolean
*/
public function captcha($input, $path = '')
{
$index = 'captcha_' . sha1($this->router->route(empty($path) === true ? $this->request->query : $path));
return preg_match('/^[a-zA-Z0-9]+$/', $input) && isset($_SESSION[$index]) && strtolower($input) === strtolower($_SESSION[$index]) ? true : false;
}
}
\ No newline at end of file
<?php
namespace ACP3\Core\Validator\Rules;
/**
* Class Date
* @package ACP3\Core\Validator\Rules
*/
class Date
{
/**
* Überprüft einen Geburtstag auf seine Gültigkeit
*
* @param string $var
* Das zu überprüfende Datum
* @return boolean
*/
public function birthday($var)
{
$regex = '/^(\d{4})-(\d{2})-(\d{2})$/';
$matches = array();
if (preg_match($regex, $var, $matches)) {
if (checkdate($matches[2], $matches[3], $matches[1])) {
return true;
}
}
return false;
}
/**
* Überprüft, ob alle Daten ein sinnvolles Datum ergeben
*
* @param string $start
* Startdatum
* @param string $end
* Enddatum
* @return boolean
*/
public function date($start, $end = null)
{
$matchesStart = $matchesEnd = array();
$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;
}
// Nur Startdatum überprüfen
} else {
if (checkdate($matchesStart[2], $matchesStart[3], $matchesStart[1])) {
return true;
}
}
}
return false;
}
/**
* Überprüft, ob eine gültige Zeitzone gewählt wurde
*
* @param string $var
* Die zu überprüfende Variable
* @return boolean
*/
public function timeZone($var)
{
$bool = true;
try {
new \DateTimeZone($var);
} catch (\Exception $e) {
$bool = false;
}
return $bool;
}
}
\ No newline at end of file
<?php
namespace ACP3\Core\Validator\Rules;
use ACP3\Core;
/**
* Class Mime
* @package ACP3\Core\Validator\Rules
*/
class Mime
{
/**
* @var \ACP3\Core\Validator\Rules\Misc
*/
protected $validate;
public function __construct(Core\Validator\Rules\Misc $validate)
{
$this->validate = $validate;
}
/**
* Überprüfen, ob es ein unterstütztes Bildformat ist
*
* @param string $file
* Zu überprüfendes Bild
* @param string $width
* @param string $height
* @param string $filesize
* @return boolean
*/
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;
}
/**
* Gibt in Abhängigkeit des Parameters $mimetype entweder
* den gefundenen MIMETYPE aus oder ob der gefundene MIMETYPE
* mit dem erwarteten übereinstimmt
*
* @param string $file
* Die zu überprüfende Datei
* @param string $mimetype
* Der zu vergleichende MIMETYPE
* @return mixed
*/
public function mimeType($file, $mimetype = '')
{
$return = '';
if (is_file($file) === true) {
if (function_exists('finfo_open') === true && $fp = finfo_open(FILEINFO_MIME)) {
$return = finfo_file($fp, $file);
finfo_close($fp);
} elseif (function_exists('mime_content_type') === true) {
$return = mime_content_type($file);
}
if (!empty($mimetype)) {
return $return == $mimetype ? true : false;
}
}
return $return;
}
}
\ No newline at end of file
<?php
namespace ACP3\Core\Validator\Rules;
use ACP3\Core\Request;
/**
* Class Misc
* @package ACP3\Core
*/
class Misc
{
/**
* @var Request
*/
protected $request;
public function __construct(Request $request)
{
$this->request = $request;