2018-08-09 17:46:14 +02:00
|
|
|
<?php
|
2024-11-25 04:18:55 +01:00
|
|
|
|
2018-08-09 17:46:14 +02:00
|
|
|
/**
|
|
|
|
* RequestInformation.php
|
2020-02-16 13:56:52 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-08-09 17:46:14 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-08-09 17:46:14 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* 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.
|
2018-08-09 17:46:14 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-08-09 17:46:14 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-08-09 17:46:14 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* 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/>.
|
2018-08-09 17:46:14 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Support\Http\Controllers;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Exceptions\ValidationException;
|
2022-12-28 06:49:40 +01:00
|
|
|
use FireflyIII\Http\Requests\RuleFormRequest;
|
2018-08-09 17:46:14 +02:00
|
|
|
use FireflyIII\Http\Requests\TestRuleFormRequest;
|
|
|
|
use FireflyIII\Support\Binder\AccountList;
|
|
|
|
use FireflyIII\User;
|
|
|
|
use Illuminate\Contracts\Validation\Validator as ValidatorContract;
|
2018-08-09 19:44:36 +02:00
|
|
|
use Illuminate\Routing\Route;
|
2018-08-09 17:46:14 +02:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2018-08-09 19:44:36 +02:00
|
|
|
use Route as RouteFacade;
|
2018-08-10 17:05:37 +02:00
|
|
|
|
2018-08-09 17:46:14 +02:00
|
|
|
/**
|
|
|
|
* Trait RequestInformation
|
|
|
|
*/
|
|
|
|
trait RequestInformation
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Get the domain of FF system.
|
|
|
|
*/
|
2021-04-06 17:00:16 +02:00
|
|
|
final protected function getDomain(): string // get request info
|
2018-08-09 17:46:14 +02:00
|
|
|
{
|
|
|
|
$url = url()->to('/');
|
|
|
|
$parts = parse_url($url);
|
|
|
|
|
2023-11-28 05:05:42 +01:00
|
|
|
return $parts['host'] ?? '';
|
2018-08-09 17:46:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of triggers.
|
|
|
|
*/
|
2021-04-06 17:00:16 +02:00
|
|
|
final protected function getValidTriggerList(TestRuleFormRequest $request): array // process input
|
2018-08-09 17:46:14 +02:00
|
|
|
{
|
|
|
|
$triggers = [];
|
2018-12-09 20:54:11 +01:00
|
|
|
$data = $request->get('triggers');
|
2019-06-22 13:09:25 +02:00
|
|
|
if (is_array($data)) {
|
2020-10-26 19:15:57 +01:00
|
|
|
foreach ($data as $triggerInfo) {
|
2022-12-28 06:49:40 +01:00
|
|
|
$current = [
|
2018-12-21 09:01:21 +01:00
|
|
|
'type' => $triggerInfo['type'] ?? '',
|
2018-08-09 17:46:14 +02:00
|
|
|
'value' => $triggerInfo['value'] ?? '',
|
2023-04-02 19:42:06 +02:00
|
|
|
'prohibited' => $triggerInfo['prohibited'] ?? false,
|
2024-12-22 08:43:12 +01:00
|
|
|
'stop_processing' => 1 === (int) ($triggerInfo['stop_processing'] ?? '0'),
|
2018-08-09 17:46:14 +02:00
|
|
|
];
|
2022-12-28 06:49:40 +01:00
|
|
|
$current = RuleFormRequest::replaceAmountTrigger($current);
|
|
|
|
$triggers[] = $current;
|
2018-08-09 17:46:14 +02:00
|
|
|
}
|
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2018-08-09 17:46:14 +02:00
|
|
|
return $triggers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns if user has seen demo.
|
|
|
|
*/
|
2021-04-06 17:00:16 +02:00
|
|
|
final protected function hasSeenDemo(): bool // get request info + get preference
|
2018-08-09 17:46:14 +02:00
|
|
|
{
|
|
|
|
$page = $this->getPageName();
|
|
|
|
$specificPage = $this->getSpecificPageName();
|
|
|
|
// indicator if user has seen the help for this page ( + special page):
|
2024-12-22 20:37:54 +01:00
|
|
|
$key = sprintf('shown_demo_%s%s', $page, $specificPage);
|
2018-08-09 17:46:14 +02:00
|
|
|
// 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)) ?? [];
|
2018-08-09 17:46:14 +02:00
|
|
|
// some routes have a "what" parameter, which indicates a special page:
|
|
|
|
|
2024-12-22 20:37:54 +01:00
|
|
|
$shownDemo = true;
|
2018-08-09 17:46:14 +02:00
|
|
|
// both must be array and either must be > 0
|
2019-05-30 12:31:19 +02:00
|
|
|
if (count($intro) > 0 || count($specialIntro) > 0) {
|
2018-08-09 17:46:14 +02:00
|
|
|
$shownDemo = app('preferences')->get($key, false)->data;
|
|
|
|
}
|
2019-05-16 19:37:32 +02:00
|
|
|
if (!is_bool($shownDemo)) {
|
2021-09-18 10:26:12 +02:00
|
|
|
$shownDemo = true;
|
2019-05-16 19:37:32 +02:00
|
|
|
}
|
2018-08-09 17:46:14 +02:00
|
|
|
|
|
|
|
return $shownDemo;
|
|
|
|
}
|
|
|
|
|
2023-06-21 12:34:58 +02:00
|
|
|
final protected function getPageName(): string // get request info
|
|
|
|
{
|
|
|
|
return str_replace('.', '_', RouteFacade::currentRouteName());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the specific name of a page for intro.
|
|
|
|
*/
|
|
|
|
final protected function getSpecificPageName(): string // get request info
|
|
|
|
{
|
2023-12-20 19:35:52 +01:00
|
|
|
/** @var null|string $param */
|
2023-11-28 05:05:42 +01:00
|
|
|
$param = RouteFacade::current()->parameter('objectType');
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-11-28 05:05:42 +01:00
|
|
|
return null === $param ? '' : sprintf('_%s', $param);
|
2023-06-21 12:34:58 +02:00
|
|
|
}
|
|
|
|
|
2018-08-09 17:46:14 +02:00
|
|
|
/**
|
|
|
|
* Check if date is outside session range.
|
|
|
|
*/
|
2021-04-06 17:00:16 +02:00
|
|
|
final protected function notInSessionRange(Carbon $date): bool // Validate a preference
|
2018-08-09 17:46:14 +02:00
|
|
|
{
|
|
|
|
/** @var Carbon $start */
|
2024-12-22 20:37:54 +01:00
|
|
|
$start = session('start', today(config('app.timezone'))->startOfMonth());
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2018-08-09 17:46:14 +02:00
|
|
|
/** @var Carbon $end */
|
2023-02-11 07:36:45 +01:00
|
|
|
$end = session('end', today(config('app.timezone'))->endOfMonth());
|
2018-08-09 17:46:14 +02:00
|
|
|
$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
|
2018-08-09 17:46:14 +02:00
|
|
|
*/
|
2021-04-06 17:00:16 +02:00
|
|
|
final protected function parseAttributes(array $attributes): array // parse input + return result
|
2018-08-09 17:46:14 +02:00
|
|
|
{
|
2023-12-10 06:45:59 +01:00
|
|
|
$attributes['location'] ??= '';
|
2024-12-22 20:37:54 +01:00
|
|
|
$attributes['accounts'] = AccountList::routeBinder($attributes['accounts'] ?? '', new Route('get', '', []));
|
|
|
|
$date = Carbon::createFromFormat('Ymd', $attributes['startDate']);
|
2024-04-02 15:40:33 +02:00
|
|
|
if (null === $date) {
|
2023-11-28 05:05:42 +01:00
|
|
|
$date = today(config('app.timezone'));
|
2018-08-09 17:46:14 +02:00
|
|
|
}
|
2023-11-28 05:05:42 +01:00
|
|
|
$date->startOfMonth();
|
|
|
|
$attributes['startDate'] = $date;
|
2018-08-09 17:46:14 +02:00
|
|
|
|
2024-12-22 20:37:54 +01:00
|
|
|
$date2 = Carbon::createFromFormat('Ymd', $attributes['endDate']);
|
2024-04-02 15:40:33 +02:00
|
|
|
if (null === $date2) {
|
2023-11-28 05:05:42 +01:00
|
|
|
$date2 = today(config('app.timezone'));
|
2018-08-09 17:46:14 +02:00
|
|
|
}
|
2023-11-28 05:05:42 +01:00
|
|
|
$date2->endOfDay();
|
2024-12-22 20:37:54 +01:00
|
|
|
$attributes['endDate'] = $date2;
|
2023-11-28 05:05:42 +01:00
|
|
|
|
2018-08-09 17:46:14 +02:00
|
|
|
return $attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate users new password.
|
|
|
|
*
|
|
|
|
* @throws ValidationException
|
|
|
|
*/
|
2023-12-20 19:35:52 +01:00
|
|
|
final protected function validatePassword(User $user, string $current, string $new): bool // get request info
|
2018-08-09 17:46:14 +02:00
|
|
|
{
|
2024-12-22 20:37:54 +01:00
|
|
|
if (!\Hash::check($current, $user->password)) {
|
2024-12-22 08:43:12 +01:00
|
|
|
throw new ValidationException((string) trans('firefly.invalid_current_password'));
|
2018-08-09 17:46:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($current === $new) {
|
2024-12-22 08:43:12 +01:00
|
|
|
throw new ValidationException((string) trans('firefly.should_change'));
|
2018-08-09 17:46:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a validator for an incoming registration request.
|
|
|
|
*/
|
2021-04-06 17:00:16 +02:00
|
|
|
final protected function validator(array $data): ValidatorContract
|
2018-08-09 17:46:14 +02:00
|
|
|
{
|
|
|
|
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-08-09 17:46:14 +02:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2018-12-31 07:48:23 +01:00
|
|
|
}
|