PDF rausgenommen

This commit is contained in:
aschwarz
2023-01-23 11:03:31 +01:00
parent 82d562a322
commit a6523903eb
28078 changed files with 4247552 additions and 2 deletions

View File

@ -0,0 +1,48 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Validators;
abstract class BaseValidator
{
/**
* The method to validate a value. If the value has not an expected format, an instance of
* {@link Piwik\Validators\Exception} should be thrown.
*
* @param $value
* @throws Exception
*/
abstract public function validate($value);
protected function isValueBare($value)
{
// we allow this value. if it is supposed to be not empty, please use NotEmpty validator on top
return $value === false || $value === null || $value === '';
}
/**
* Lets you easily check a value against multiple validators.
*
* @param string $name The name/description of the field you want to validate the value for.
* The name will be prefixed in case there is any error.
* @param mixed $value The value which needs to be tested
* @param BaseValidator[] $validators
*/
public static function check($name, $value, $validators)
{
foreach ($validators as $validator) {
try {
$validator->validate($value);
} catch (\Exception $e) {
throw new Exception(strip_tags($name) . ': ' . $e->getMessage(), $e->getCode());
}
}
}
}

View File

@ -0,0 +1,58 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Validators;
use Piwik\Common;
use Piwik\Piwik;
class CharacterLength extends BaseValidator
{
/**
* @var null|int
*/
private $min;
/**
* @var null|int
*/
private $max;
/**
* @param null|int $min
* @param null|int $max
*/
public function __construct($min = null, $max = null)
{
if (isset($min)) {
$this->min = (int) $min;
}
if (isset($max)) {
$this->max = (int) $max;
}
}
public function validate($value)
{
if (!is_string($value) && !is_numeric($value)) {
return;
}
$lenValue = Common::mb_strlen($value);
if (isset($this->min) && $this->min > $lenValue) {
throw new Exception(Piwik::translate('General_ValidatorErrorCharacterTooShort', array($lenValue, $this->min)));
}
if (isset($this->max) && $this->max < $lenValue) {
throw new Exception(Piwik::translate('General_ValidatorErrorCharacterTooLong', array($lenValue, $this->max)));
}
}
}

View File

@ -0,0 +1,34 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Validators;
use Piwik\Date;
use Piwik\Piwik;
class DateTime extends BaseValidator
{
public function validate($value)
{
if ($this->isValueBare($value)) {
return;
}
if (!preg_match('/^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2}):(\d{2})Z?$/', $value)) {
throw new Exception(Piwik::translate('General_ValidatorErrorInvalidDateTimeFormat', array($value, 'YYYY-MM-DD HH:MM:SS')));
}
try {
Date::factory($value);
} catch (\Exception $e) {
throw new Exception($e->getMessage(), $e->getCode(), $e);
}
}
}

View File

@ -0,0 +1,26 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Validators;
use Piwik\Piwik;
class Email extends BaseValidator
{
public function validate($value)
{
if ($this->isValueBare($value)) {
return;
}
if (!Piwik::isValidEmailString($value)) {
throw new Exception(Piwik::translate('General_ValidatorErrorNotEmailLike', array($value)));
}
}
}

View File

@ -0,0 +1,15 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Validators;
class Exception extends \InvalidArgumentException
{
}

View File

@ -0,0 +1,22 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Validators;
use Piwik\Piwik;
use Piwik\Site;
use Piwik\UrlHelper;
class IdSite extends BaseValidator
{
public function validate($value)
{
new Site($value);
}
}

View File

@ -0,0 +1,33 @@
<?php
/**
* Matomo free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Validators;
use Piwik\Network\IPUtils;
use Piwik\Piwik;
class IpRanges extends BaseValidator
{
public function validate($value)
{
if (!empty($value)) {
if (!is_array($value)) {
throw new Exception('The IP ranges need to be an array');
}
$ips = array_map('trim', $value);
$ips = array_filter($ips, 'strlen');
foreach ($ips as $ip) {
if (IPUtils::getIPRangeBounds($ip) === null) {
throw new Exception(Piwik::translate('SitesManager_ExceptionInvalidIPFormat', array($ip, "1.2.3.4, 1.2.3.*, or 1.2.3.4/5")));
}
}
}
}
}

View File

@ -0,0 +1,22 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Validators;
use Piwik\Piwik;
class NotEmpty extends BaseValidator
{
public function validate($value)
{
if (empty($value)) {
throw new Exception(Piwik::translate('General_ValidatorErrorEmptyValue'));
}
}
}

View File

@ -0,0 +1,57 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Validators;
use Piwik\Piwik;
class NumberRange extends BaseValidator
{
const MAX_SMALL_INT_UNSIGNED = 65535;
const MAX_MEDIUM_INT_UNSIGNED = 16777215;
/**
* @var null|int
*/
private $min;
/**
* @var null|int
*/
private $max;
/**
* @param null|int $min
* @param null|int $max
*/
public function __construct($min = null, $max = null)
{
$this->min = $min;
$this->max = $max;
}
public function validate($value)
{
if ($this->isValueBare($value)) {
return;
}
if (!is_numeric($value)) {
throw new Exception(Piwik::translate('General_ValidatorErrorNotANumber'));
}
if (isset($this->min) && $this->min > $value) {
throw new Exception(Piwik::translate('General_ValidatorErrorNumberTooLow', array($value, $this->min)));
}
if (isset($this->max) && $this->max < $value) {
throw new Exception(Piwik::translate('General_ValidatorErrorNumberTooHigh', array($value, $this->max)));
}
}
}

View File

@ -0,0 +1,26 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Validators;
use Piwik\Piwik;
class Regex extends BaseValidator
{
public function validate($value)
{
if ($this->isValueBare($value)) {
return;
}
if (@preg_match($value, '') === false) {
throw new Exception(Piwik::translate('General_ValidatorErrorNoValidRegex', array($value)));
}
}
}

View File

@ -0,0 +1,23 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Validators;
use Piwik\Piwik;
use Piwik\UrlHelper;
class UrlLike extends BaseValidator
{
public function validate($value)
{
if (!UrlHelper::isLookLikeUrl($value)) {
throw new Exception(Piwik::translate('ValidatorErrorNotUrlLike', $value));
}
}
}

View File

@ -0,0 +1,36 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Validators;
use Piwik\Piwik;
class WhitelistedValue extends BaseValidator
{
private $whitelisted = array();
/**
* @param array $whitelistedValues
*/
public function __construct($whitelistedValues)
{
if (!is_array($whitelistedValues)) {
throw new Exception('The whitelisted values need to be an array');
}
$this->whitelisted = $whitelistedValues;
}
public function validate($value)
{
if (!in_array($value, $this->whitelisted, true)) {
throw new Exception(Piwik::translate('General_ValidatorErrorXNotWhitelisted', array($value, implode(', ', $this->whitelisted))));
}
}
}