Files
firefly-iii/app/Support/Http/Controllers/RequestInformation.php

233 lines
7.1 KiB
PHP
Raw Normal View History

<?php
/**
* RequestInformation.php
2020-02-16 13:56:52 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Http\Controllers;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Exceptions\ValidationException;
use FireflyIII\Helpers\Help\HelpInterface;
use FireflyIII\Http\Requests\TestRuleFormRequest;
use FireflyIII\Support\Binder\AccountList;
use FireflyIII\User;
use Hash;
use Illuminate\Contracts\Validation\Validator as ValidatorContract;
2018-08-09 19:44:36 +02:00
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Validator;
use InvalidArgumentException;
use Log;
2018-08-09 19:44:36 +02:00
use Route as RouteFacade;
2018-08-10 17:05:37 +02:00
/**
* Trait RequestInformation
*
*/
trait RequestInformation
{
/**
* Get the domain of FF system.
*
* @return string
*/
2021-04-06 17:00:16 +02:00
final protected function getDomain(): string // get request info
{
$url = url()->to('/');
$parts = parse_url($url);
return $parts['host'];
}
/**
* Get a list of triggers.
*
* @param TestRuleFormRequest $request
*
* @return array
*/
2021-04-06 17:00:16 +02:00
final protected function getValidTriggerList(TestRuleFormRequest $request): array // process input
{
$triggers = [];
2018-12-09 20:54:11 +01:00
$data = $request->get('triggers');
if (is_array($data)) {
2020-10-26 19:15:57 +01:00
foreach ($data as $triggerInfo) {
$triggers[] = [
2018-12-21 09:01:21 +01:00
'type' => $triggerInfo['type'] ?? '',
'value' => $triggerInfo['value'] ?? '',
2022-03-29 15:00:29 +02:00
'stop_processing' => 1 === (int) ($triggerInfo['stop_processing'] ?? '0'),
];
}
}
return $triggers;
}
/**
* Returns if user has seen demo.
*
* @return bool
* @throws FireflyException
2022-03-29 15:10:05 +02:00
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
2021-04-06 17:00:16 +02:00
final protected function hasSeenDemo(): bool // get request info + get preference
{
$page = $this->getPageName();
$specificPage = $this->getSpecificPageName();
// indicator if user has seen the help for this page ( + special page):
2019-06-22 05:51:32 +02:00
$key = sprintf('shown_demo_%s%s', $page, $specificPage);
// is there an intro for this route?
2019-06-22 05:51:32 +02:00
$intro = config(sprintf('intro.%s', $page)) ?? [];
$specialIntro = config(sprintf('intro.%s%s', $page, $specificPage)) ?? [];
// some routes have a "what" parameter, which indicates a special page:
$shownDemo = true;
// both must be array and either must be > 0
if (count($intro) > 0 || count($specialIntro) > 0) {
$shownDemo = app('preferences')->get($key, false)->data;
}
2019-05-16 19:37:32 +02:00
if (!is_bool($shownDemo)) {
$shownDemo = true;
2019-05-16 19:37:32 +02:00
}
return $shownDemo;
}
/**
* @return string
*/
final protected function getPageName(): string // get request info
{
return str_replace('.', '_', RouteFacade::currentRouteName());
}
/**
* Get the specific name of a page for intro.
*
* @return string
*/
final protected function getSpecificPageName(): string // get request info
{
return null === RouteFacade::current()->parameter('objectType') ? '' : '_' . RouteFacade::current()->parameter('objectType');
}
/**
* Check if date is outside session range.
*
* @param Carbon $date
*
* @return bool
2019-08-17 10:47:29 +02:00
*
*/
2021-04-06 17:00:16 +02:00
final protected function notInSessionRange(Carbon $date): bool // Validate a preference
{
/** @var Carbon $start */
$start = session('start', Carbon::now()->startOfMonth());
/** @var Carbon $end */
$end = session('end', Carbon::now()->endOfMonth());
$result = false;
if ($start->greaterThanOrEqualTo($date) && $end->greaterThanOrEqualTo($date)) {
$result = true;
}
// start and end in the past? use $end
if ($start->lessThanOrEqualTo($date) && $end->lessThanOrEqualTo($date)) {
$result = true;
}
return $result;
}
/**
2022-04-12 18:19:30 +02:00
* Parses attributes from URL
*
* @param array $attributes
*
* @return array
*/
2021-04-06 17:00:16 +02:00
final protected function parseAttributes(array $attributes): array // parse input + return result
{
$attributes['location'] = $attributes['location'] ?? '';
$attributes['accounts'] = AccountList::routeBinder($attributes['accounts'] ?? '', new Route('get', '', []));
try {
2019-08-14 19:51:46 +02:00
$attributes['startDate'] = Carbon::createFromFormat('Ymd', $attributes['startDate'])->startOfDay();
} catch (InvalidArgumentException $e) {
Log::debug(sprintf('Not important error message: %s', $e->getMessage()));
$date = Carbon::now()->startOfMonth();
$attributes['startDate'] = $date;
}
try {
2019-08-14 19:51:46 +02:00
$attributes['endDate'] = Carbon::createFromFormat('Ymd', $attributes['endDate'])->endOfDay();
} catch (InvalidArgumentException $e) {
Log::debug(sprintf('Not important error message: %s', $e->getMessage()));
$date = Carbon::now()->startOfMonth();
$attributes['endDate'] = $date;
}
return $attributes;
}
/**
* Validate users new password.
*
2021-03-21 09:15:40 +01:00
* @param User $user
* @param string $current
* @param string $new
*
* @return bool
*
* @throws ValidationException
*/
2021-04-06 17:00:16 +02:00
final protected function validatePassword(User $user, string $current, string $new): bool //get request info
{
if (!Hash::check($current, $user->password)) {
2022-03-29 15:00:29 +02:00
throw new ValidationException((string) trans('firefly.invalid_current_password'));
}
if ($current === $new) {
2022-03-29 15:00:29 +02:00
throw new ValidationException((string) trans('firefly.should_change'));
}
return true;
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
*
* @return ValidatorContract
2019-07-26 17:48:24 +02:00
* @codeCoverageIgnore
*/
2021-04-06 17:00:16 +02:00
final protected function validator(array $data): ValidatorContract
{
return Validator::make(
$data,
[
'email' => 'required|string|email|max:255|unique:users',
2020-04-11 06:42:40 +02:00
'password' => 'required|string|min:16|secure_password|confirmed',
]
);
}
2018-12-31 07:48:23 +01:00
}