szurubooru/src/Validator.php

117 lines
3.1 KiB
PHP
Raw Normal View History

<?php
namespace Szurubooru;
class Validator
{
private $config;
public function __construct(\Szurubooru\Config $config)
{
$this->config = $config;
}
public function validate(\Szurubooru\IValidatable $validatable)
{
$validatable->validate($this);
}
2014-09-09 12:34:57 +02:00
public function validateNumber($subject)
{
if (!preg_match('/^-?[0-9]+$/', $subject))
throw new \DomainException($subject . ' does not look like a number.');
}
public function validateNonEmpty($subject, $subjectName = 'Object')
{
if (!$subject)
throw new \DomainException($subjectName . ' cannot be empty.');
}
public function validateLength($subject, $minLength, $maxLength, $subjectName = 'Object')
{
$this->validateMinLength($subject, $minLength, $subjectName);
$this->validateMaxLength($subject, $maxLength, $subjectName);
}
public function validateMinLength($subject, $minLength, $subjectName = 'Object')
{
if (strlen($subject) < $minLength)
throw new \DomainException($subjectName . ' must have at least ' . $minLength . ' character(s).');
}
public function validateMaxLength($subject, $maxLength, $subjectName = 'Object')
{
if (strlen($subject) > $maxLength)
throw new \DomainException($subjectName . ' must have at most ' . $maxLength . ' character(s).');
}
2014-09-09 12:34:57 +02:00
public function validateUserName($userName)
{
$minUserNameLength = intval($this->config->users->minUserNameLength);
$maxUserNameLength = intval($this->config->users->maxUserNameLength);
$this->validateNonEmpty($userName, 'User name');
$this->validateLength($userName, $minUserNameLength, $maxUserNameLength, 'User name');
2014-09-02 23:07:31 +02:00
if (preg_match('/[^a-zA-Z0-9_-]/', $userName))
{
throw new \DomainException('User name may contain only characters, numbers, underscore (_) and dash (-).');
}
}
public function validateEmail($email)
{
if (!$email)
return;
if (!preg_match('/^[^@]+@[^@]+\.\w+$/', $email))
throw new \DomainException('Specified e-mail appears to be invalid.');
}
public function validatePassword($password)
{
$minPasswordLength = intval($this->config->security->minPasswordLength);
$this->validateNonEmpty($password, 'Password');
$this->validateMinLength($password, $minPasswordLength, 'Password');
if (preg_match('/[^\x20-\x7f]/', $password))
{
throw new \DomainException(
'Password may contain only characters from ASCII range to avoid potential problems with encoding.');
}
}
2014-09-15 11:38:24 +02:00
public function validatePostTags($tags)
{
if (empty($tags))
throw new \DomainException('Tags cannot be empty.');
$illegalCharacters = str_split("\r\n\t " . chr(160));
foreach ($tags as $tag)
{
if (empty($tag))
{
throw new \DomainException('Tags cannot be empty.');
}
foreach ($illegalCharacters as $char)
{
if (strpos($tag, $char) !== false)
{
throw new \DomainException(
'Tags cannot contain any of following characters: ' . implode(', ', $illegalCharacters));
}
}
}
}
2014-09-25 19:11:41 +02:00
public function validatePostSource($source)
{
$this->validateMaxLength($source, 200, 'Source');
}
public function validateToken($token)
{
$this->validateNonEmpty($token, 'Token');
}
}