Add validator namespace

This commit is contained in:
Frederic Guillot 2016-08-23 22:03:45 -04:00
parent b05ad87028
commit dac1dc21e0
No known key found for this signature in database
GPG Key ID: 92D77191BA7FBC99
15 changed files with 149 additions and 127 deletions

1
.gitignore vendored
View File

@ -20,5 +20,6 @@ config.php
!app/models/* !app/models/*
!app/controllers/* !app/controllers/*
!app/templates/* !app/templates/*
!app/validators/*
rules/*.php rules/*.php
data/favicons/*.* data/favicons/*.*

View File

@ -1,6 +1,7 @@
<?php <?php
use PicoDb\Database; use PicoDb\Database;
use Miniflux\Validator;
// Display a form to add a new database // Display a form to add a new database
Router\get_action('new-db', function () { Router\get_action('new-db', function () {
@ -24,7 +25,7 @@ Router\post_action('new-db', function () {
if (ENABLE_MULTIPLE_DB) { if (ENABLE_MULTIPLE_DB) {
$values = Request\values(); $values = Request\values();
Helper\check_csrf_values($values); Helper\check_csrf_values($values);
list($valid, $errors) = Model\Database\validate($values); list($valid, $errors) = Validator\User\validate_creation($values);
if ($valid) { if ($valid) {
if (Model\Database\create(strtolower($values['name']).'.sqlite', $values['username'], $values['password'])) { if (Model\Database\create(strtolower($values['name']).'.sqlite', $values['username'], $values['password'])) {
@ -121,7 +122,7 @@ Router\get_action('config', function () {
Router\post_action('config', function () { Router\post_action('config', function () {
$values = Request\values() + array('nocontent' => 0, 'image_proxy' => 0, 'favicons' => 0, 'debug_mode' => 0, 'original_marks_read' => 0); $values = Request\values() + array('nocontent' => 0, 'image_proxy' => 0, 'favicons' => 0, 'debug_mode' => 0, 'original_marks_read' => 0);
Helper\check_csrf_values($values); Helper\check_csrf_values($values);
list($valid, $errors) = Model\Config\validate_modification($values); list($valid, $errors) = Validator\Config\validate_modification($values);
if ($valid) { if ($valid) {
if (Model\Config\save($values)) { if (Model\Config\save($values)) {

View File

@ -1,6 +1,7 @@
<?php <?php
use PicoFeed\Parser\MalformedXmlException; use PicoFeed\Parser\MalformedXmlException;
use Miniflux\Validator;
// Refresh all feeds, used when Javascript is disabled // Refresh all feeds, used when Javascript is disabled
Router\get_action('refresh-all', function () { Router\get_action('refresh-all', function () {
@ -40,7 +41,7 @@ Router\post_action('edit-feed', function () {
'create_group' => '' 'create_group' => ''
); );
list($valid, $errors) = Model\Feed\validate_modification($values); list($valid, $errors) = Validator\Feed\validate_modification($values);
if ($valid) { if ($valid) {
if (Model\Feed\update($values)) { if (Model\Feed\update($values)) {

View File

@ -1,5 +1,7 @@
<?php <?php
use Miniflux\Validator;
// Logout and destroy session // Logout and destroy session
Router\get_action('logout', function () { Router\get_action('logout', function () {
Model\User\logout(); Model\User\logout();
@ -26,7 +28,7 @@ Router\get_action('login', function () {
Router\post_action('login', function () { Router\post_action('login', function () {
$values = Request\values(); $values = Request\values();
Helper\check_csrf_values($values); Helper\check_csrf_values($values);
list($valid, $errors) = Model\User\validate_login($values); list($valid, $errors) = Validator\User\validate_login($values);
if ($valid) { if ($valid) {
Response\redirect('?action=unread'); Response\redirect('?action=unread');

View File

@ -5,8 +5,6 @@ namespace Model\Config;
use Helper; use Helper;
use Translator; use Translator;
use DirectoryIterator; use DirectoryIterator;
use SimpleValidator\Validator;
use SimpleValidator\Validators;
use PicoDb\Database; use PicoDb\Database;
use PicoFeed\Config\Config as ReaderConfig; use PicoFeed\Config\Config as ReaderConfig;
use PicoFeed\Logging\Logger; use PicoFeed\Logging\Logger;
@ -248,43 +246,6 @@ function get_all()
return $config; return $config;
} }
// Validation for edit action
function validate_modification(array $values)
{
$rules = array(
new Validators\Required('username', t('The user name is required')),
new Validators\MaxLength('username', t('The maximum length is 50 characters'), 50),
new Validators\Required('autoflush', t('Value required')),
new Validators\Required('autoflush_unread', t('Value required')),
new Validators\Required('items_per_page', t('Value required')),
new Validators\Integer('items_per_page', t('Must be an integer')),
new Validators\Required('theme', t('Value required')),
new Validators\Integer('frontend_updatecheck_interval', t('Must be an integer')),
new Validators\Integer('debug_mode', t('Must be an integer')),
new Validators\Integer('nocontent', t('Must be an integer')),
new Validators\Integer('favicons', t('Must be an integer')),
new Validators\Integer('original_marks_read', t('Must be an integer')),
);
if (ENABLE_AUTO_UPDATE) {
$rules[] = new Validators\Required('auto_update_url', t('Value required'));
}
if (! empty($values['password'])) {
$rules[] = new Validators\Required('password', t('The password is required'));
$rules[] = new Validators\MinLength('password', t('The minimum length is 6 characters'), 6);
$rules[] = new Validators\Required('confirmation', t('The confirmation is required'));
$rules[] = new Validators\Equals('password', 'confirmation', t('Passwords don\'t match'));
}
$v = new Validator($values, $rules);
return array(
$v->execute(),
$v->getErrors()
);
}
// Save config into the database and update the session // Save config into the database and update the session
function save(array $values) function save(array $values)
{ {

View File

@ -5,8 +5,6 @@ namespace Model\Database;
use Schema; use Schema;
use DirectoryIterator; use DirectoryIterator;
use Model\Config; use Model\Config;
use SimpleValidator\Validator;
use SimpleValidator\Validators;
// Create a new database for a new user // Create a new database for a new user
function create($filename, $username, $password) function create($filename, $username, $password)
@ -101,23 +99,3 @@ function get_list()
return $listing; return $listing;
} }
// Validate database form
function validate(array $values)
{
$v = new Validator($values, array(
new Validators\Required('name', t('The database name is required')),
new Validators\AlphaNumeric('name', t('The name must have only alpha-numeric characters')),
new Validators\Required('username', t('The user name is required')),
new Validators\MaxLength('username', t('The maximum length is 50 characters'), 50),
new Validators\Required('password', t('The password is required')),
new Validators\MinLength('password', t('The minimum length is 6 characters'), 6),
new Validators\Required('confirmation', t('The confirmation is required')),
new Validators\Equals('password', 'confirmation', t('Passwords don\'t match')),
));
return array(
$v->execute(),
$v->getErrors()
);
}

View File

@ -9,8 +9,6 @@ use Model\Item;
use Model\Group; use Model\Group;
use Model\Favicon; use Model\Favicon;
use Helper; use Helper;
use SimpleValidator\Validator;
use SimpleValidator\Validators;
use PicoDb\Database; use PicoDb\Database;
use PicoFeed\Reader\Reader; use PicoFeed\Reader\Reader;
use PicoFeed\PicoFeedException; use PicoFeed\PicoFeedException;
@ -351,22 +349,3 @@ function disable($feed_id)
{ {
return Database::getInstance('db')->table('feeds')->eq('id', $feed_id)->save((array('enabled' => 0))); return Database::getInstance('db')->table('feeds')->eq('id', $feed_id)->save((array('enabled' => 0)));
} }
// Validation for edit
function validate_modification(array $values)
{
$v = new Validator($values, array(
new Validators\Required('id', t('The feed id is required')),
new Validators\Required('title', t('The title is required')),
new Validators\Required('site_url', t('The site url is required')),
new Validators\Required('feed_url', t('The feed url is required')),
));
$result = $v->execute();
$errors = $v->getErrors();
return array(
$result,
$errors
);
}

View File

@ -4,7 +4,7 @@ namespace Model\Item;
use PicoDb\Database; use PicoDb\Database;
use PicoFeed\Logging\Logger; use PicoFeed\Logging\Logger;
use Model\Service; use Handler\Service;
use Model\Config; use Model\Config;
use Model\Group; use Model\Group;
use Handler; use Handler;

View File

@ -2,8 +2,6 @@
namespace Model\User; namespace Model\User;
use SimpleValidator\Validator;
use SimpleValidator\Validators;
use PicoDb\Database; use PicoDb\Database;
use Session; use Session;
use Request; use Request;
@ -39,40 +37,3 @@ function set_last_login()
->hashtable('settings') ->hashtable('settings')
->put(array('last_login' => time())); ->put(array('last_login' => time()));
} }
// Validate authentication
function validate_login(array $values)
{
$v = new Validator($values, array(
new Validators\Required('username', t('The user name is required')),
new Validators\MaxLength('username', t('The maximum length is 50 characters'), 50),
new Validators\Required('password', t('The password is required'))
));
$result = $v->execute();
$errors = $v->getErrors();
if ($result) {
$credentials = get_credentials();
if ($credentials && $credentials['username'] === $values['username'] && password_verify($values['password'], $credentials['password'])) {
set_last_login();
$_SESSION['loggedin'] = true;
$_SESSION['config'] = Config\get_all();
// Setup the remember me feature
if (! empty($values['remember_me'])) {
$cookie = RememberMe\create(DatabaseModel\select(), $values['username'], Request\get_ip_address(), Request\get_user_agent());
RememberMe\write_cookie($cookie['token'], $cookie['sequence'], $cookie['expiration']);
}
} else {
$result = false;
$errors['login'] = t('Bad username or password');
}
}
return array(
$result,
$errors
);
}

42
app/validators/config.php Normal file
View File

@ -0,0 +1,42 @@
<?php
namespace Miniflux\Validator\Config;
use SimpleValidator\Validator;
use SimpleValidator\Validators;
function validate_modification(array $values)
{
$rules = array(
new Validators\Required('username', t('The user name is required')),
new Validators\MaxLength('username', t('The maximum length is 50 characters'), 50),
new Validators\Required('autoflush', t('Value required')),
new Validators\Required('autoflush_unread', t('Value required')),
new Validators\Required('items_per_page', t('Value required')),
new Validators\Integer('items_per_page', t('Must be an integer')),
new Validators\Required('theme', t('Value required')),
new Validators\Integer('frontend_updatecheck_interval', t('Must be an integer')),
new Validators\Integer('debug_mode', t('Must be an integer')),
new Validators\Integer('nocontent', t('Must be an integer')),
new Validators\Integer('favicons', t('Must be an integer')),
new Validators\Integer('original_marks_read', t('Must be an integer')),
);
if (ENABLE_AUTO_UPDATE) {
$rules[] = new Validators\Required('auto_update_url', t('Value required'));
}
if (! empty($values['password'])) {
$rules[] = new Validators\Required('password', t('The password is required'));
$rules[] = new Validators\MinLength('password', t('The minimum length is 6 characters'), 6);
$rules[] = new Validators\Required('confirmation', t('The confirmation is required'));
$rules[] = new Validators\Equals('password', 'confirmation', t('Passwords don\'t match'));
}
$v = new Validator($values, $rules);
return array(
$v->execute(),
$v->getErrors()
);
}

21
app/validators/feed.php Normal file
View File

@ -0,0 +1,21 @@
<?php
namespace Miniflux\Validator\Feed;
use SimpleValidator\Validator;
use SimpleValidator\Validators;
function validate_modification(array $values)
{
$v = new Validator($values, array(
new Validators\Required('id', t('The feed id is required')),
new Validators\Required('title', t('The title is required')),
new Validators\Required('site_url', t('The site url is required')),
new Validators\Required('feed_url', t('The feed url is required')),
));
return array(
$v->execute(),
$v->getErrors(),
);
}

66
app/validators/user.php Normal file
View File

@ -0,0 +1,66 @@
<?php
namespace Miniflux\Validator\User;
use SimpleValidator\Validator;
use SimpleValidator\Validators;
use Model\Config;
use Model\User as UserModel;
use Model\Database as DatabaseModel;
use Model\RememberMe;
use Request;
function validate_creation(array $values)
{
$v = new Validator($values, array(
new Validators\Required('name', t('The database name is required')),
new Validators\AlphaNumeric('name', t('The name must have only alpha-numeric characters')),
new Validators\Required('username', t('The user name is required')),
new Validators\MaxLength('username', t('The maximum length is 50 characters'), 50),
new Validators\Required('password', t('The password is required')),
new Validators\MinLength('password', t('The minimum length is 6 characters'), 6),
new Validators\Required('confirmation', t('The confirmation is required')),
new Validators\Equals('password', 'confirmation', t('Passwords don\'t match')),
));
return array(
$v->execute(),
$v->getErrors()
);
}
function validate_login(array $values)
{
$v = new Validator($values, array(
new Validators\Required('username', t('The user name is required')),
new Validators\MaxLength('username', t('The maximum length is 50 characters'), 50),
new Validators\Required('password', t('The password is required'))
));
$result = $v->execute();
$errors = $v->getErrors();
if ($result) {
$credentials = UserModel\get_credentials();
if ($credentials && $credentials['username'] === $values['username'] && password_verify($values['password'], $credentials['password'])) {
UserModel\set_last_login();
$_SESSION['loggedin'] = true;
$_SESSION['config'] = Config\get_all();
// Setup the remember me feature
if (! empty($values['remember_me'])) {
$cookie = RememberMe\create(DatabaseModel\select(), $values['username'], Request\get_ip_address(), Request\get_user_agent());
RememberMe\write_cookie($cookie['token'], $cookie['sequence'], $cookie['expiration']);
}
} else {
$result = false;
$errors['login'] = t('Bad username or password');
}
}
return array(
$result,
$errors
);
}

View File

@ -54,7 +54,10 @@
"app/models/database.php", "app/models/database.php",
"app/models/remember_me.php", "app/models/remember_me.php",
"app/models/group.php", "app/models/group.php",
"app/models/favicon.php" "app/models/favicon.php",
"app/validators/config.php",
"app/validators/feed.php",
"app/validators/user.php"
], ],
"classmap": [ "classmap": [
"vendor/fguillot/json-rpc/src/", "vendor/fguillot/json-rpc/src/",

View File

@ -36,4 +36,7 @@ return array(
'ee585b658e324609d721bc6f959e85c8' => $baseDir . '/app/models/remember_me.php', 'ee585b658e324609d721bc6f959e85c8' => $baseDir . '/app/models/remember_me.php',
'fc49fda782025f9f73852265b1fa7760' => $baseDir . '/app/models/group.php', 'fc49fda782025f9f73852265b1fa7760' => $baseDir . '/app/models/group.php',
'785cebb801997d40232b8337459f1606' => $baseDir . '/app/models/favicon.php', '785cebb801997d40232b8337459f1606' => $baseDir . '/app/models/favicon.php',
'e348a7661429e81fa0e42efff1ebfe6e' => $baseDir . '/app/validators/config.php',
'679a0a7c75414c39298328823e0be180' => $baseDir . '/app/validators/feed.php',
'eea04c7f459daa801618b71f49a8c470' => $baseDir . '/app/validators/user.php',
); );

View File

@ -37,6 +37,9 @@ class ComposerStaticInitfd7e8d436e1dc450edc3153ac8bc31b4
'ee585b658e324609d721bc6f959e85c8' => __DIR__ . '/../..' . '/app/models/remember_me.php', 'ee585b658e324609d721bc6f959e85c8' => __DIR__ . '/../..' . '/app/models/remember_me.php',
'fc49fda782025f9f73852265b1fa7760' => __DIR__ . '/../..' . '/app/models/group.php', 'fc49fda782025f9f73852265b1fa7760' => __DIR__ . '/../..' . '/app/models/group.php',
'785cebb801997d40232b8337459f1606' => __DIR__ . '/../..' . '/app/models/favicon.php', '785cebb801997d40232b8337459f1606' => __DIR__ . '/../..' . '/app/models/favicon.php',
'e348a7661429e81fa0e42efff1ebfe6e' => __DIR__ . '/../..' . '/app/validators/config.php',
'679a0a7c75414c39298328823e0be180' => __DIR__ . '/../..' . '/app/validators/feed.php',
'eea04c7f459daa801618b71f49a8c470' => __DIR__ . '/../..' . '/app/validators/user.php',
); );
public static $prefixLengthsPsr4 = array ( public static $prefixLengthsPsr4 = array (