| 
									
										
										
										
											2016-05-20 12:42:21 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * FireflyValidator.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:58:22 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +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. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +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. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +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/>. | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-03-24 15:15:12 +01:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2015-02-08 01:15:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Validation; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-05 12:59:43 +02:00
										 |  |  | use Config; | 
					
						
							| 
									
										
										
										
											2025-01-03 09:15:52 +01:00
										 |  |  | use FireflyIII\Enums\AccountTypeEnum; | 
					
						
							| 
									
										
										
										
											2020-05-16 12:11:06 +02:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2015-03-30 20:08:27 +02:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							| 
									
										
										
										
											2016-02-11 06:40:16 +01:00
										 |  |  | use FireflyIII\Models\AccountMeta; | 
					
						
							| 
									
										
										
										
											2015-03-26 18:05:23 +01:00
										 |  |  | use FireflyIII\Models\AccountType; | 
					
						
							| 
									
										
										
										
											2024-12-01 18:16:48 +01:00
										 |  |  | use FireflyIII\Models\PiggyBank; | 
					
						
							| 
									
										
										
										
											2016-01-15 09:25:32 +01:00
										 |  |  | use FireflyIII\Models\TransactionType; | 
					
						
							| 
									
										
										
										
											2020-11-29 18:35:49 +01:00
										 |  |  | use FireflyIII\Models\Webhook; | 
					
						
							| 
									
										
										
										
											2018-09-15 13:43:57 +02:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2018-04-07 06:20:45 +02:00
										 |  |  | use FireflyIII\Repositories\Bill\BillRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2016-01-14 21:34:17 +01:00
										 |  |  | use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2020-11-20 06:22:43 +01:00
										 |  |  | use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2017-08-04 15:48:26 +02:00
										 |  |  | use FireflyIII\Services\Password\Verifier; | 
					
						
							| 
									
										
										
										
											2020-05-16 12:11:06 +02:00
										 |  |  | use FireflyIII\Support\ParseDateString; | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2025-02-23 12:47:04 +01:00
										 |  |  | use Illuminate\Support\Facades\DB; | 
					
						
							| 
									
										
										
										
											2025-01-26 06:30:38 +01:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2023-05-27 05:55:41 +02:00
										 |  |  | use Illuminate\Validation\Validator; | 
					
						
							| 
									
										
										
										
											2021-09-18 10:20:19 +02:00
										 |  |  | use PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException; | 
					
						
							|  |  |  | use PragmaRX\Google2FA\Exceptions\InvalidCharactersException; | 
					
						
							|  |  |  | use PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException; | 
					
						
							| 
									
										
										
										
											2025-05-24 17:15:46 +02:00
										 |  |  | use PragmaRX\Google2FALaravel\Facade; | 
					
						
							| 
									
										
										
										
											2025-10-05 12:57:58 +02:00
										 |  |  | use Psr\Container\ContainerExceptionInterface; | 
					
						
							|  |  |  | use Psr\Container\NotFoundExceptionInterface; | 
					
						
							|  |  |  | use Safe\Exceptions\JsonException; | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  | use ValueError; | 
					
						
							| 
									
										
										
										
											2025-10-05 13:03:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  | use function Safe\iconv; | 
					
						
							|  |  |  | use function Safe\json_encode; | 
					
						
							| 
									
										
										
										
											2025-10-05 12:59:43 +02:00
										 |  |  | use function Safe\preg_match; | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 07:35:10 +01:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Class FireflyValidator. | 
					
						
							| 
									
										
										
										
											2023-11-29 06:30:35 +01:00
										 |  |  |  * TODO all of these validations must become separate classes. | 
					
						
							| 
									
										
										
										
											2015-02-11 07:35:10 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-02-08 01:15:15 +01:00
										 |  |  | class FireflyValidator extends Validator | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-03 20:45:27 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param mixed $attribute | 
					
						
							|  |  |  |      * @param mixed $value | 
					
						
							| 
									
										
										
										
											2016-03-03 20:45:27 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-09-18 10:20:19 +02:00
										 |  |  |      * @throws IncompatibleWithGoogleAuthenticatorException | 
					
						
							|  |  |  |      * @throws InvalidCharactersException | 
					
						
							|  |  |  |      * @throws SecretKeyTooShortException | 
					
						
							| 
									
										
										
										
											2025-10-05 12:57:58 +02:00
										 |  |  |      * @throws ContainerExceptionInterface | 
					
						
							|  |  |  |      * @throws NotFoundExceptionInterface | 
					
						
							| 
									
										
										
										
											2025-10-05 13:03:51 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.UnusedFormalParameter") | 
					
						
							| 
									
										
										
										
											2016-03-03 20:45:27 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-02-11 15:27:28 +01:00
										 |  |  |     public function validate2faCode($attribute, $value): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-05-27 05:55:41 +02:00
										 |  |  |         if (!is_string($value) || 6 !== strlen($value)) { | 
					
						
							| 
									
										
										
										
											2016-03-03 20:45:27 +01:00
										 |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2016-03-14 20:53:56 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |         $user             = auth()->user(); | 
					
						
							| 
									
										
										
										
											2022-09-04 13:32:59 +02:00
										 |  |  |         if (null === $user) { | 
					
						
							| 
									
										
										
										
											2025-01-26 06:30:38 +01:00
										 |  |  |             Log::error('No user during validate2faCode'); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-04 13:32:59 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-29 12:10:03 +01:00
										 |  |  |         $secretPreference = app('preferences')->get('temp-mfa-secret'); | 
					
						
							| 
									
										
										
										
											2025-01-04 09:01:44 +01:00
										 |  |  |         $secret           = $secretPreference->data ?? ''; | 
					
						
							| 
									
										
										
										
											2023-11-29 06:30:35 +01:00
										 |  |  |         if (is_array($secret)) { | 
					
						
							| 
									
										
										
										
											2023-11-28 17:18:31 +01:00
										 |  |  |             $secret = ''; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |         $secret           = (string) $secret; | 
					
						
							| 
									
										
										
										
											2016-03-14 20:53:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-24 17:15:46 +02:00
										 |  |  |         return (bool) Facade::verifyKey($secret, $value); | 
					
						
							| 
									
										
										
										
											2016-03-03 20:45:27 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 22:53:38 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param mixed $attribute | 
					
						
							|  |  |  |      * @param mixed $value | 
					
						
							|  |  |  |      * @param mixed $parameters | 
					
						
							| 
									
										
										
										
											2023-12-10 06:51:59 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.UnusedFormalParameter") | 
					
						
							| 
									
										
										
										
											2016-01-14 21:34:17 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-02-11 15:27:28 +01:00
										 |  |  |     public function validateBelongsToUser($attribute, $value, $parameters): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-02-05 08:03:26 +01:00
										 |  |  |         $field = $parameters[1] ?? 'id'; | 
					
						
							| 
									
										
										
										
											2016-01-15 09:25:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |         if (0 === (int) $value) { | 
					
						
							| 
									
										
										
										
											2016-05-05 18:59:46 +02:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-02-23 12:47:04 +01:00
										 |  |  |         $count = DB::table($parameters[0])->where('user_id', auth()->user()->id)->where($field, $value)->count(); | 
					
						
							| 
									
										
										
										
											2016-01-15 09:25:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-29 19:01:47 +02:00
										 |  |  |         return 1 === $count; | 
					
						
							| 
									
										
										
										
											2016-01-20 15:23:36 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-28 18:55:56 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.UnusedFormalParameter") | 
					
						
							| 
									
										
										
										
											2016-11-28 18:55:56 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2025-05-04 17:41:26 +02:00
										 |  |  |     public function validateBic(mixed $attribute, mixed $value): bool | 
					
						
							| 
									
										
										
										
											2018-02-11 15:27:28 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-01-26 14:33:19 +01:00
										 |  |  |         if (!is_string($value) || strlen($value) < 8) { | 
					
						
							| 
									
										
										
										
											2025-01-26 06:30:38 +01:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-11-28 18:55:56 +01:00
										 |  |  |         $regex  = '/^[a-z]{6}[0-9a-z]{2}([0-9a-z]{3})?\z/i'; | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |         $result = preg_match($regex, $value); | 
					
						
							| 
									
										
										
										
											2025-09-07 07:51:01 +02:00
										 |  |  |         if (0 === $result) { | 
					
						
							| 
									
										
										
										
											2016-11-28 18:55:56 +01:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 15:16:11 +01:00
										 |  |  |     public function validateExistingMfaCode(mixed $attribute, mixed $value): bool | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (!is_string($value) || 6 !== strlen($value)) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $user   = auth()->user(); | 
					
						
							|  |  |  |         if (null === $user) { | 
					
						
							| 
									
										
										
										
											2025-01-26 06:30:38 +01:00
										 |  |  |             Log::error('No user during validate2faCode'); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $secret = (string) $user->mfa_secret; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-24 17:15:46 +02:00
										 |  |  |         return (bool) Facade::verifyKey($secret, $value); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 15:23:36 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.UnusedFormalParameter") | 
					
						
							|  |  |  |      * @SuppressWarnings("PHPMD.ExcessiveMethodLength") | 
					
						
							| 
									
										
										
										
											2016-01-20 15:23:36 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-12-30 09:28:03 +01:00
										 |  |  |     public function validateIban(mixed $attribute, mixed $value): bool | 
					
						
							| 
									
										
										
										
											2018-02-11 15:27:28 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-12-30 09:28:03 +01:00
										 |  |  |         if (!is_string($value) || strlen($value) < 6) { | 
					
						
							| 
									
										
										
										
											2016-01-20 15:23:36 +01:00
										 |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2016-01-14 21:34:17 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-01-12 20:32:09 +01:00
										 |  |  |         // strip spaces
 | 
					
						
							|  |  |  |         $search  = [ | 
					
						
							|  |  |  |             "\x20", // normal space
 | 
					
						
							|  |  |  |             "\u{0001}", // start of heading
 | 
					
						
							|  |  |  |             "\u{0002}", // start of text
 | 
					
						
							|  |  |  |             "\u{0003}", // end of text
 | 
					
						
							|  |  |  |             "\u{0004}", // end of transmission
 | 
					
						
							|  |  |  |             "\u{0005}", // enquiry
 | 
					
						
							|  |  |  |             "\u{0006}", // ACK
 | 
					
						
							|  |  |  |             "\u{0007}", // BEL
 | 
					
						
							|  |  |  |             "\u{0008}", // backspace
 | 
					
						
							|  |  |  |             "\u{000E}", // shift out
 | 
					
						
							|  |  |  |             "\u{000F}", // shift in
 | 
					
						
							|  |  |  |             "\u{0010}", // data link escape
 | 
					
						
							|  |  |  |             "\u{0011}", // DC1
 | 
					
						
							|  |  |  |             "\u{0012}", // DC2
 | 
					
						
							|  |  |  |             "\u{0013}", // DC3
 | 
					
						
							|  |  |  |             "\u{0014}", // DC4
 | 
					
						
							|  |  |  |             "\u{0015}", // NAK
 | 
					
						
							|  |  |  |             "\u{0016}", // SYN
 | 
					
						
							|  |  |  |             "\u{0017}", // ETB
 | 
					
						
							|  |  |  |             "\u{0018}", // CAN
 | 
					
						
							|  |  |  |             "\u{0019}", // EM
 | 
					
						
							|  |  |  |             "\u{001A}", // SUB
 | 
					
						
							|  |  |  |             "\u{001B}", // escape
 | 
					
						
							|  |  |  |             "\u{001C}", // file separator
 | 
					
						
							|  |  |  |             "\u{001D}", // group separator
 | 
					
						
							|  |  |  |             "\u{001E}", // record separator
 | 
					
						
							|  |  |  |             "\u{001F}", // unit separator
 | 
					
						
							|  |  |  |             "\u{007F}", // DEL
 | 
					
						
							|  |  |  |             "\u{00A0}", // non-breaking space
 | 
					
						
							|  |  |  |             "\u{1680}", // ogham space mark
 | 
					
						
							|  |  |  |             "\u{180E}", // mongolian vowel separator
 | 
					
						
							|  |  |  |             "\u{2000}", // en quad
 | 
					
						
							|  |  |  |             "\u{2001}", // em quad
 | 
					
						
							|  |  |  |             "\u{2002}", // en space
 | 
					
						
							|  |  |  |             "\u{2003}", // em space
 | 
					
						
							|  |  |  |             "\u{2004}", // three-per-em space
 | 
					
						
							|  |  |  |             "\u{2005}", // four-per-em space
 | 
					
						
							|  |  |  |             "\u{2006}", // six-per-em space
 | 
					
						
							|  |  |  |             "\u{2007}", // figure space
 | 
					
						
							|  |  |  |             "\u{2008}", // punctuation space
 | 
					
						
							|  |  |  |             "\u{2009}", // thin space
 | 
					
						
							|  |  |  |             "\u{200A}", // hair space
 | 
					
						
							|  |  |  |             "\u{200B}", // zero width space
 | 
					
						
							|  |  |  |             "\u{202F}", // narrow no-break space
 | 
					
						
							|  |  |  |             "\u{3000}", // ideographic space
 | 
					
						
							|  |  |  |             "\u{FEFF}", // zero width no -break space
 | 
					
						
							| 
									
										
										
										
											2024-05-18 06:42:09 +02:00
										 |  |  |             ' ', | 
					
						
							| 
									
										
										
										
											2021-08-12 15:13:59 +02:00
										 |  |  |             '-', | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |             '?', | 
					
						
							| 
									
										
										
										
											2018-01-12 20:32:09 +01:00
										 |  |  |         ]; | 
					
						
							|  |  |  |         $replace = ''; | 
					
						
							|  |  |  |         $value   = str_replace($search, $replace, $value); | 
					
						
							|  |  |  |         $value   = strtoupper($value); | 
					
						
							| 
									
										
										
										
											2016-01-20 15:23:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-11 14:13:19 +02:00
										 |  |  |         // replace characters outside of ASCI range.
 | 
					
						
							| 
									
										
										
										
											2025-09-07 11:04:11 +02:00
										 |  |  |         $value   = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value); | 
					
						
							| 
									
										
										
										
											2021-08-12 15:13:59 +02:00
										 |  |  |         $search  = [' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']; | 
					
						
							| 
									
										
										
										
											2023-12-21 06:06:23 +01:00
										 |  |  |         $replace = ['', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35']; | 
					
						
							| 
									
										
										
										
											2016-01-20 15:23:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // take
 | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |         $first   = substr($value, 0, 4); | 
					
						
							|  |  |  |         $last    = substr($value, 4); | 
					
						
							|  |  |  |         $iban    = $last.$first; | 
					
						
							|  |  |  |         $iban    = trim(str_replace($search, $replace, $iban)); | 
					
						
							| 
									
										
										
										
											2023-11-04 14:18:49 +01:00
										 |  |  |         if ('' === $iban) { | 
					
						
							| 
									
										
										
										
											2021-07-04 19:19:31 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-11 14:13:19 +02:00
										 |  |  |         try { | 
					
						
							|  |  |  |             $checksum = bcmod($iban, '97'); | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |         } catch (ValueError $e) { // @phpstan-ignore-line
 | 
					
						
							| 
									
										
										
										
											2021-08-11 14:17:27 +02:00
										 |  |  |             $message = sprintf('Could not validate IBAN check value "%s" (IBAN "%s")', $iban, $value); | 
					
						
							| 
									
										
										
										
											2025-01-26 06:30:38 +01:00
										 |  |  |             Log::error($message); | 
					
						
							|  |  |  |             Log::error($e->getTraceAsString()); | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-11 19:44:22 +02:00
										 |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2021-08-11 14:13:19 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-01-20 15:23:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |         return 1 === (int) $checksum; | 
					
						
							| 
									
										
										
										
											2016-01-14 21:34:17 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 17:25:28 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param mixed $attribute | 
					
						
							|  |  |  |      * @param mixed $value | 
					
						
							|  |  |  |      * @param mixed $parameters | 
					
						
							| 
									
										
										
										
											2023-12-10 06:51:59 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.UnusedFormalParameter") | 
					
						
							| 
									
										
										
										
											2017-01-04 17:25:28 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-08-04 17:30:47 +02:00
										 |  |  |     public function validateLess($attribute, $value, $parameters): bool | 
					
						
							| 
									
										
										
										
											2018-02-11 15:27:28 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-04-28 06:23:13 +02:00
										 |  |  |         /** @var mixed $compare */ | 
					
						
							|  |  |  |         $compare = $parameters[0] ?? '0'; | 
					
						
							| 
									
										
										
										
											2017-01-30 16:35:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |         return bccomp((string) $value, (string) $compare) < 0; | 
					
						
							| 
									
										
										
										
											2017-01-04 17:25:28 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-22 08:36:30 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param mixed $attribute | 
					
						
							|  |  |  |      * @param mixed $value | 
					
						
							|  |  |  |      * @param mixed $parameters | 
					
						
							| 
									
										
										
										
											2023-12-10 06:51:59 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.UnusedFormalParameter") | 
					
						
							| 
									
										
										
										
											2018-07-22 08:36:30 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-08-04 17:30:47 +02:00
										 |  |  |     public function validateMore($attribute, $value, $parameters): bool | 
					
						
							| 
									
										
										
										
											2018-07-22 08:36:30 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         /** @var mixed $compare */ | 
					
						
							|  |  |  |         $compare = $parameters[0] ?? '0'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |         return bccomp((string) $value, (string) $compare) > 0; | 
					
						
							| 
									
										
										
										
											2018-07-22 08:36:30 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-21 19:06:22 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param mixed $attribute | 
					
						
							|  |  |  |      * @param mixed $value | 
					
						
							|  |  |  |      * @param mixed $parameters | 
					
						
							| 
									
										
										
										
											2023-12-10 06:51:59 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.UnusedFormalParameter") | 
					
						
							| 
									
										
										
										
											2016-10-21 19:06:22 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-02-11 15:27:28 +01:00
										 |  |  |     public function validateMustExist($attribute, $value, $parameters): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-10-21 19:06:22 +02:00
										 |  |  |         $field = $parameters[1] ?? 'id'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |         if (0 === (int) $value) { | 
					
						
							| 
									
										
										
										
											2016-10-21 19:06:22 +02:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-02-23 12:47:04 +01:00
										 |  |  |         $count = DB::table($parameters[0])->where($field, $value)->count(); | 
					
						
							| 
									
										
										
										
											2016-10-21 19:06:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-29 19:01:47 +02:00
										 |  |  |         return 1 === $count; | 
					
						
							| 
									
										
										
										
											2016-10-21 19:06:22 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 20:25:30 +01:00
										 |  |  |     public function validateRuleActionValue(string $attribute, ?string $value = null): bool | 
					
						
							| 
									
										
										
										
											2016-01-14 21:34:17 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-08-05 15:34:20 +02:00
										 |  |  |         // first, get the index from this string:
 | 
					
						
							| 
									
										
										
										
											2023-12-10 06:45:59 +01:00
										 |  |  |         $value ??= ''; | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |         $parts      = explode('.', $attribute); | 
					
						
							|  |  |  |         $index      = (int) ($parts[1] ?? '0'); | 
					
						
							| 
									
										
										
										
											2018-08-05 15:34:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // get the name of the trigger from the data array:
 | 
					
						
							| 
									
										
										
										
											2018-12-16 13:55:19 +01:00
										 |  |  |         $actionType = $this->data['actions'][$index]['type'] ?? 'invalid'; | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-05 15:34:20 +02:00
										 |  |  |         // if it's "invalid" return false.
 | 
					
						
							|  |  |  |         if ('invalid' === $actionType) { | 
					
						
							|  |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-08-05 15:34:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 12:23:32 -05:00
										 |  |  |         // if value is an expression, assume valid
 | 
					
						
							| 
									
										
										
										
											2024-03-10 06:46:24 +01:00
										 |  |  |         if (true === config('firefly.feature_flags.expression_engine') && str_starts_with($value, '=') && strlen($value) > 1) { | 
					
						
							| 
									
										
										
										
											2024-03-07 12:23:32 -05:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-05 15:34:20 +02:00
										 |  |  |         // if it's set_budget, verify the budget name:
 | 
					
						
							|  |  |  |         if ('set_budget' === $actionType) { | 
					
						
							|  |  |  |             /** @var BudgetRepositoryInterface $repository */ | 
					
						
							|  |  |  |             $repository = app(BudgetRepositoryInterface::class); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-10 07:29:39 +02:00
										 |  |  |             return null !== $repository->findByName($value); | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-05 15:34:20 +02:00
										 |  |  |         // if it's link to bill, verify the name of the bill.
 | 
					
						
							|  |  |  |         if ('link_to_bill' === $actionType) { | 
					
						
							|  |  |  |             /** @var BillRepositoryInterface $repository */ | 
					
						
							|  |  |  |             $repository = app(BillRepositoryInterface::class); | 
					
						
							|  |  |  |             $bill       = $repository->findByName($value); | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-05 15:34:20 +02:00
										 |  |  |             return null !== $bill; | 
					
						
							| 
									
										
										
										
											2016-01-14 21:34:17 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-15 13:43:57 +02:00
										 |  |  |         // if it's convert_transfer, it must be a valid asset account name.
 | 
					
						
							|  |  |  |         if ('convert_transfer' === $actionType) { | 
					
						
							|  |  |  |             /** @var AccountRepositoryInterface $repository */ | 
					
						
							|  |  |  |             $repository = app(AccountRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2018-09-26 20:35:01 +02:00
										 |  |  |             $account    = $repository->findByName( | 
					
						
							|  |  |  |                 $value, | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |                 [ | 
					
						
							| 
									
										
										
										
											2025-01-03 09:15:52 +01:00
										 |  |  |                     AccountTypeEnum::DEFAULT->value, | 
					
						
							|  |  |  |                     AccountTypeEnum::ASSET->value, | 
					
						
							|  |  |  |                     AccountTypeEnum::LOAN->value, | 
					
						
							|  |  |  |                     AccountTypeEnum::DEBT->value, | 
					
						
							|  |  |  |                     AccountTypeEnum::MORTGAGE->value, | 
					
						
							|  |  |  |                     AccountTypeEnum::CREDITCARD->value, | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |                 ] | 
					
						
							| 
									
										
										
										
											2018-09-15 13:43:57 +02:00
										 |  |  |             ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return null !== $account; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-26 20:18:27 +02:00
										 |  |  |         if ('update_piggy' === $actionType) { | 
					
						
							| 
									
										
										
										
											2020-11-20 06:22:43 +01:00
										 |  |  |             /** @var PiggyBankRepositoryInterface $repository */ | 
					
						
							|  |  |  |             $repository = app(PiggyBankRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2020-11-29 18:35:49 +01:00
										 |  |  |             $piggy      = $repository->findByName($value); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-20 06:22:43 +01:00
										 |  |  |             return null !== $piggy; | 
					
						
							| 
									
										
										
										
											2020-08-26 20:18:27 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-05 15:34:20 +02:00
										 |  |  |         // return true for the rest.
 | 
					
						
							|  |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2016-01-14 21:34:17 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-12-09 20:54:11 +01:00
										 |  |  |      * $attribute has the format triggers.%d.value. | 
					
						
							| 
									
										
										
										
											2015-02-24 22:53:38 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-03-18 20:25:30 +01:00
										 |  |  |     public function validateRuleTriggerValue(string $attribute, ?string $value = null): bool | 
					
						
							| 
									
										
										
										
											2016-01-02 16:57:31 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-08-05 15:34:20 +02:00
										 |  |  |         // first, get the index from this string:
 | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |         $parts       = explode('.', $attribute); | 
					
						
							|  |  |  |         $index       = (int) ($parts[1] ?? '0'); | 
					
						
							| 
									
										
										
										
											2018-08-05 15:34:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // get the name of the trigger from the data array:
 | 
					
						
							| 
									
										
										
										
											2018-12-16 13:55:19 +01:00
										 |  |  |         $triggerType = $this->data['triggers'][$index]['type'] ?? 'invalid'; | 
					
						
							| 
									
										
										
										
											2018-08-05 15:34:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // invalid always returns false:
 | 
					
						
							|  |  |  |         if ('invalid' === $triggerType) { | 
					
						
							|  |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-05 15:34:20 +02:00
										 |  |  |         // these trigger types need a numerical check:
 | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |         $numerical   = ['amount_less', 'amount_more', 'amount_exactly']; | 
					
						
							| 
									
										
										
										
											2019-06-21 19:10:02 +02:00
										 |  |  |         if (in_array($triggerType, $numerical, true)) { | 
					
						
							| 
									
										
										
										
											2018-08-05 15:34:20 +02:00
										 |  |  |             return is_numeric($value); | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-08-05 15:34:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-18 18:57:29 +01:00
										 |  |  |         // these triggers need just the word "true":
 | 
					
						
							|  |  |  |         // TODO create a helper to automatically return these.
 | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |         $needTrue    = [ | 
					
						
							| 
									
										
										
										
											2024-01-20 07:04:19 +01:00
										 |  |  |             'reconciled', 'has_attachments', 'has_any_category', 'has_any_budget', 'has_any_bill', 'has_any_tag', 'any_notes', 'any_external_url', 'has_no_attachments', 'has_no_category', 'has_no_budget', 'has_no_bill', 'has_no_tag', 'no_notes', 'no_external_url', | 
					
						
							| 
									
										
										
										
											2024-01-18 18:57:29 +01:00
										 |  |  |             'source_is_cash', | 
					
						
							|  |  |  |             'destination_is_cash', | 
					
						
							|  |  |  |             'account_is_cash', | 
					
						
							|  |  |  |             'exists', | 
					
						
							|  |  |  |             'no_external_id', | 
					
						
							|  |  |  |             'any_external_id', | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         if (in_array($triggerType, $needTrue, true)) { | 
					
						
							|  |  |  |             return 'true' === $value; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-05 15:34:20 +02:00
										 |  |  |         // these trigger types need a simple strlen check:
 | 
					
						
							| 
									
										
										
										
											2024-01-18 18:57:29 +01:00
										 |  |  |         // TODO create a helper to automatically return these.
 | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |         $length      = [ | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |             'source_account_starts', | 
					
						
							|  |  |  |             'source_account_ends', | 
					
						
							|  |  |  |             'source_account_is', | 
					
						
							|  |  |  |             'source_account_contains', | 
					
						
							|  |  |  |             'destination_account_starts', | 
					
						
							|  |  |  |             'destination_account_ends', | 
					
						
							|  |  |  |             'destination_account_is', | 
					
						
							|  |  |  |             'destination_account_contains', | 
					
						
							|  |  |  |             'description_starts', | 
					
						
							|  |  |  |             'description_ends', | 
					
						
							|  |  |  |             'description_contains', | 
					
						
							|  |  |  |             'description_is', | 
					
						
							|  |  |  |             'category_is', | 
					
						
							|  |  |  |             'budget_is', | 
					
						
							|  |  |  |             'tag_is', | 
					
						
							|  |  |  |             'currency_is', | 
					
						
							|  |  |  |             'notes_contain', | 
					
						
							|  |  |  |             'notes_start', | 
					
						
							|  |  |  |             'notes_end', | 
					
						
							|  |  |  |             'notes_are', | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2019-06-21 19:10:02 +02:00
										 |  |  |         if (in_array($triggerType, $length, true)) { | 
					
						
							| 
									
										
										
										
											2018-08-05 15:34:20 +02:00
										 |  |  |             return '' !== $value; | 
					
						
							| 
									
										
										
										
											2018-06-30 16:46:51 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-03-30 20:08:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-04 16:50:16 +02:00
										 |  |  |         // check if it's an existing account.
 | 
					
						
							| 
									
										
										
										
											2024-01-18 18:57:29 +01:00
										 |  |  |         // TODO create a helper to automatically return these.
 | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |         if (in_array($triggerType, ['destination_account_id', 'source_account_id'], true)) { | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |             return is_numeric($value) && (int) $value > 0; | 
					
						
							| 
									
										
										
										
											2020-10-04 16:50:16 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-05 15:34:20 +02:00
										 |  |  |         // check transaction type.
 | 
					
						
							| 
									
										
										
										
											2024-01-18 18:57:29 +01:00
										 |  |  |         // TODO create a helper to automatically return these.
 | 
					
						
							| 
									
										
										
										
											2018-08-05 15:34:20 +02:00
										 |  |  |         if ('transaction_type' === $triggerType) { | 
					
						
							| 
									
										
										
										
											2025-05-04 13:50:20 +02:00
										 |  |  |             $count = TransactionType::where('type', ucfirst((string) $value))->count(); | 
					
						
							| 
									
										
										
										
											2018-08-05 15:34:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-26 20:35:01 +02:00
										 |  |  |             return 1 === $count; | 
					
						
							| 
									
										
										
										
											2015-07-07 01:07:19 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-01-29 07:35:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-18 18:57:29 +01:00
										 |  |  |         // if the type is date, then simply try to parse it and throw error when it's bad.
 | 
					
						
							|  |  |  |         // TODO create a helper to automatically return these.
 | 
					
						
							| 
									
										
										
										
											2020-08-26 20:18:27 +02:00
										 |  |  |         if (in_array($triggerType, ['date_is', 'created_on', 'updated_on', 'date_before', 'date_after'], true)) { | 
					
						
							| 
									
										
										
										
											2020-05-16 12:11:06 +02:00
										 |  |  |             /** @var ParseDateString $parser */ | 
					
						
							|  |  |  |             $parser = app(ParseDateString::class); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-16 12:11:06 +02:00
										 |  |  |             try { | 
					
						
							|  |  |  |                 $parser->parseDate($value); | 
					
						
							|  |  |  |             } catch (FireflyException $e) { | 
					
						
							| 
									
										
										
										
											2025-01-26 06:30:38 +01:00
										 |  |  |                 Log::error($e->getMessage()); | 
					
						
							| 
									
										
										
										
											2020-05-16 12:11:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-11-29 18:35:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-26 06:18:09 +01:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2015-07-03 12:51:14 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-08-04 15:46:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param mixed $attribute | 
					
						
							|  |  |  |      * @param mixed $value | 
					
						
							| 
									
										
										
										
											2023-12-10 06:51:59 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.UnusedFormalParameter") | 
					
						
							| 
									
										
										
										
											2017-08-04 15:46:52 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-02-11 15:27:28 +01:00
										 |  |  |     public function validateSecurePassword($attribute, $value): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-08-08 16:30:21 +02:00
										 |  |  |         $verify = false; | 
					
						
							| 
									
										
										
										
											2021-04-06 08:51:27 +02:00
										 |  |  |         if (array_key_exists('verify_password', $this->data)) { | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |             $verify = 1 === (int) $this->data['verify_password']; | 
					
						
							| 
									
										
										
										
											2017-08-08 16:30:21 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         if ($verify) { | 
					
						
							|  |  |  |             /** @var Verifier $service */ | 
					
						
							|  |  |  |             $service = app(Verifier::class); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $service->validPassword($value); | 
					
						
							| 
									
										
										
										
											2017-08-04 15:46:52 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-08-04 15:48:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-08 16:30:21 +02:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2017-08-04 15:46:52 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-03 12:51:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param mixed $attribute | 
					
						
							|  |  |  |      * @param mixed $value | 
					
						
							|  |  |  |      * @param mixed $parameters | 
					
						
							| 
									
										
										
										
											2015-03-26 18:05:23 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.UnusedFormalParameter") | 
					
						
							| 
									
										
										
										
											2015-03-26 18:05:23 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-02-11 15:27:28 +01:00
										 |  |  |     public function validateUniqueAccountForUser($attribute, $value, $parameters): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-01-26 14:33:19 +01:00
										 |  |  |         if (is_array($value)) { | 
					
						
							| 
									
										
										
										
											2025-01-26 06:30:38 +01:00
										 |  |  |             Log::debug('$value is an array, always return false', $value); | 
					
						
							| 
									
										
										
										
											2025-01-26 14:33:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-26 06:30:38 +01:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  |         // because a user does not have to be logged in (tests and what-not).
 | 
					
						
							| 
									
										
										
										
											2016-09-16 12:07:45 +02:00
										 |  |  |         if (!auth()->check()) { | 
					
						
							| 
									
										
										
										
											2025-01-26 06:30:38 +01:00
										 |  |  |             Log::debug('validateUniqueAccountForUser::anon'); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  |             return $this->validateAccountAnonymously(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-06 08:51:27 +02:00
										 |  |  |         if (array_key_exists('objectType', $this->data)) { | 
					
						
							| 
									
										
										
										
											2025-01-26 06:30:38 +01:00
										 |  |  |             Log::debug('validateUniqueAccountForUser::typeString'); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-11 17:31:32 +01:00
										 |  |  |             return $this->validateByAccountTypeString($value, $parameters, $this->data['objectType']); | 
					
						
							| 
									
										
										
										
											2018-02-13 18:24:06 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-26 06:30:38 +01:00
										 |  |  |         if (array_key_exists('type', $this->data) && !is_array($this->data['type'])) { | 
					
						
							|  |  |  |             Log::debug('validateUniqueAccountForUser::typeString'); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |             return $this->validateByAccountTypeString($value, $parameters, (string) $this->data['type']); | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-06 08:51:27 +02:00
										 |  |  |         if (array_key_exists('account_type_id', $this->data)) { | 
					
						
							| 
									
										
										
										
											2025-01-26 06:30:38 +01:00
										 |  |  |             Log::debug('validateUniqueAccountForUser::typeId'); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 12:18:20 +02:00
										 |  |  |             return $this->validateByAccountTypeId($value, $parameters); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-12-30 20:44:52 +01:00
										 |  |  |         $parameterId = $parameters[0] ?? null; | 
					
						
							|  |  |  |         if (null !== $parameterId) { | 
					
						
							| 
									
										
										
										
											2025-01-26 06:30:38 +01:00
										 |  |  |             Log::debug('validateUniqueAccountForUser::paramId'); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |             return $this->validateByParameterId((int) $parameterId, $value); | 
					
						
							| 
									
										
										
										
											2019-12-30 20:44:52 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-06 08:51:27 +02:00
										 |  |  |         if (array_key_exists('id', $this->data)) { | 
					
						
							| 
									
										
										
										
											2025-01-26 06:30:38 +01:00
										 |  |  |             Log::debug('validateUniqueAccountForUser::accountId'); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-29 07:22:51 +02:00
										 |  |  |             return $this->validateByAccountId($value); | 
					
						
							| 
									
										
										
										
											2015-06-28 21:13:08 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-10 20:14:00 +02:00
										 |  |  |         // without type, just try to validate the name.
 | 
					
						
							| 
									
										
										
										
											2025-01-26 06:30:38 +01:00
										 |  |  |         Log::debug('validateUniqueAccountForUser::accountName'); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         return $this->validateByAccountName($value); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |     private function validateAccountAnonymously(): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!array_key_exists('user_id', $this->data)) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var User $user */ | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |         $user   = User::find($this->data['user_id']); | 
					
						
							|  |  |  |         $type   = AccountType::find($this->data['account_type_id'])->first(); | 
					
						
							|  |  |  |         $value  = $this->data['name']; | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var null|Account $result */ | 
					
						
							|  |  |  |         $result = $user->accounts()->where('account_type_id', $type->id)->where('name', $value)->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return null === $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function validateByAccountTypeString(string $value, array $parameters, string $type): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var null|array $search */ | 
					
						
							| 
									
										
										
										
											2025-05-27 16:57:36 +02:00
										 |  |  |         $search         = Config::get('firefly.accountTypeByIdentifier.'.$type); | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (null === $search) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $accountTypes   = AccountType::whereIn('type', $search)->get(); | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |         $ignore         = (int) ($parameters[0] ?? 0.0); | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |         $accountTypeIds = $accountTypes->pluck('id')->toArray(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var null|Account $result */ | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |         $result         = auth()->user()->accounts()->whereIn('account_type_id', $accountTypeIds)->where('id', '!=', $ignore) | 
					
						
							|  |  |  |             ->where('name', $value) | 
					
						
							|  |  |  |             ->first() | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return null === $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param mixed $value | 
					
						
							|  |  |  |      * @param mixed $parameters | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function validateByAccountTypeId($value, $parameters): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $type   = AccountType::find($this->data['account_type_id'])->first(); | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |         $ignore = (int) ($parameters[0] ?? 0.0); | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var null|Account $result */ | 
					
						
							|  |  |  |         $result = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore) | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |             ->where('name', $value) | 
					
						
							|  |  |  |             ->first() | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return null === $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param mixed $value | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function validateByParameterId(int $accountId, $value): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Account $existingAccount */ | 
					
						
							|  |  |  |         $existingAccount = Account::find($accountId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |         $type            = $existingAccount->accountType; | 
					
						
							|  |  |  |         $ignore          = $existingAccount->id; | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |         $entry           = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore) | 
					
						
							|  |  |  |             ->where('name', $value) | 
					
						
							|  |  |  |             ->first() | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return null === $entry; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param mixed $value | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function validateByAccountId($value): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Account $existingAccount */ | 
					
						
							|  |  |  |         $existingAccount = Account::find($this->data['id']); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |         $type            = $existingAccount->accountType; | 
					
						
							|  |  |  |         $ignore          = $existingAccount->id; | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |         $entry           = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore) | 
					
						
							|  |  |  |             ->where('name', $value) | 
					
						
							|  |  |  |             ->first() | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return null === $entry; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function validateByAccountName(string $value): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return 0 === auth()->user()->accounts()->where('name', $value)->count(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param mixed $attribute | 
					
						
							|  |  |  |      * @param mixed $value | 
					
						
							|  |  |  |      * @param mixed $parameters | 
					
						
							| 
									
										
										
										
											2019-12-30 20:44:52 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2025-10-05 12:57:58 +02:00
										 |  |  |      * @throws JsonException | 
					
						
							| 
									
										
										
										
											2025-10-05 13:03:51 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.UnusedFormalParameter") | 
					
						
							| 
									
										
										
										
											2019-12-30 20:44:52 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     public function validateUniqueAccountNumberForUser($attribute, $value, $parameters): bool | 
					
						
							| 
									
										
										
										
											2019-12-30 20:44:52 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |         $accountId = (int) ($this->data['id'] ?? 0.0); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         if (0 === $accountId) { | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |             $accountId = (int) ($parameters[0] ?? 0.0); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-12-30 20:44:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |         $query     = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id') | 
					
						
							|  |  |  |             ->whereNull('accounts.deleted_at') | 
					
						
							|  |  |  |             ->where('accounts.user_id', auth()->user()->id) | 
					
						
							|  |  |  |             ->where('account_meta.name', 'account_number') | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |             ->where('account_meta.data', json_encode($value)) | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |         ; | 
					
						
							| 
									
										
										
										
											2019-12-30 20:44:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         if ($accountId > 0) { | 
					
						
							|  |  |  |             // exclude current account from check.
 | 
					
						
							|  |  |  |             $query->where('account_meta.account_id', '!=', $accountId); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |         $set       = $query->get(['account_meta.*']); | 
					
						
							|  |  |  |         $count     = $set->count(); | 
					
						
							| 
									
										
										
										
											2022-07-16 17:11:14 +02:00
										 |  |  |         if (0 === $count) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($count > 1) { | 
					
						
							|  |  |  |             // pretty much impossible but still.
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |         $type      = $this->data['objectType'] ?? 'unknown'; | 
					
						
							| 
									
										
										
										
											2022-07-16 17:11:14 +02:00
										 |  |  |         if ('expense' !== $type && 'revenue' !== $type) { | 
					
						
							| 
									
										
										
										
											2025-01-26 06:30:38 +01:00
										 |  |  |             Log::warning(sprintf('Account number "%s" is not unique and account type "%s" cannot share its account number.', $value, $type)); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 17:11:14 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-26 06:30:38 +01:00
										 |  |  |         Log::debug(sprintf('Account number "%s" is not unique but account type "%s" may share its account number.', $value, $type)); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 17:11:14 +02:00
										 |  |  |         // one other account with this account number.
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         /** @var AccountMeta $entry */ | 
					
						
							|  |  |  |         foreach ($set as $entry) { | 
					
						
							| 
									
										
										
										
											2022-07-16 17:11:14 +02:00
										 |  |  |             $otherAccount = $entry->account; | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |             $otherType    = (string) config(sprintf('firefly.shortNamesByFullName.%s', $otherAccount->accountType->type)); | 
					
						
							| 
									
										
										
										
											2022-07-16 17:11:14 +02:00
										 |  |  |             if (('expense' === $otherType || 'revenue' === $otherType) && $otherType !== $type) { | 
					
						
							| 
									
										
										
										
											2025-01-26 06:30:38 +01:00
										 |  |  |                 Log::debug(sprintf('The other account with this account number is a "%s" so return true.', $otherType)); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 17:11:14 +02:00
										 |  |  |                 return true; | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-01-26 06:30:38 +01:00
										 |  |  |             Log::debug(sprintf('The other account with this account number is a "%s" so return false.', $otherType)); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 17:11:14 +02:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2015-06-28 21:13:08 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-29 15:00:29 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.UnusedFormalParameter") | 
					
						
							| 
									
										
										
										
											2022-03-29 15:00:29 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-02-29 01:28:57 +01:00
										 |  |  |     public function validateUniqueCurrencyCode(?string $attribute, ?string $value): bool | 
					
						
							| 
									
										
										
										
											2022-03-29 15:00:29 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |         return $this->validateUniqueCurrency('code', (string) $attribute, (string) $value); | 
					
						
							| 
									
										
										
										
											2022-03-29 15:00:29 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.UnusedFormalParameter") | 
					
						
							| 
									
										
										
										
											2022-03-29 15:00:29 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     public function validateUniqueCurrency(string $field, string $attribute, string $value): bool | 
					
						
							| 
									
										
										
										
											2022-03-29 15:00:29 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-02-23 12:47:04 +01:00
										 |  |  |         return 0 === DB::table('transaction_currencies')->where($field, $value)->whereNull('deleted_at')->count(); | 
					
						
							| 
									
										
										
										
											2022-03-29 15:00:29 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-29 01:28:57 +01:00
										 |  |  |     public function validateUniqueCurrencyName(?string $attribute, ?string $value): bool | 
					
						
							| 
									
										
										
										
											2022-03-29 15:00:29 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |         return $this->validateUniqueCurrency('name', (string) $attribute, (string) $value); | 
					
						
							| 
									
										
										
										
											2022-03-29 15:00:29 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-29 01:28:57 +01:00
										 |  |  |     public function validateUniqueCurrencySymbol(?string $attribute, ?string $value): bool | 
					
						
							| 
									
										
										
										
											2022-03-29 15:00:29 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |         return $this->validateUniqueCurrency('symbol', (string) $attribute, (string) $value); | 
					
						
							| 
									
										
										
										
											2022-03-29 15:00:29 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 08:51:27 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param mixed $value | 
					
						
							|  |  |  |      * @param mixed $parameters | 
					
						
							|  |  |  |      * @param mixed $something | 
					
						
							| 
									
										
										
										
											2021-04-06 08:51:27 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.UnusedFormalParameter") | 
					
						
							| 
									
										
										
										
											2021-04-06 08:51:27 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-11-29 18:35:49 +01:00
										 |  |  |     public function validateUniqueExistingWebhook($value, $parameters, $something): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |         $existingId = (int) ($something[0] ?? 0); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         $trigger    = 0; | 
					
						
							|  |  |  |         $response   = 0; | 
					
						
							|  |  |  |         $delivery   = 0; | 
					
						
							| 
									
										
										
										
											2022-09-18 10:45:38 +02:00
										 |  |  |         $triggers   = Webhook::getTriggersForValidation(); | 
					
						
							|  |  |  |         $responses  = Webhook::getResponsesForValidation(); | 
					
						
							|  |  |  |         $deliveries = Webhook::getDeliveriesForValidation(); | 
					
						
							| 
									
										
										
										
											2020-11-29 18:35:49 +01:00
										 |  |  |         if (auth()->check()) { | 
					
						
							| 
									
										
										
										
											2021-03-08 09:56:40 +01:00
										 |  |  |             // get existing webhook value:
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |             if (0 !== $existingId) { | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |                 /** @var null|Webhook $webhook */ | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |                 $webhook  = auth()->user()->webhooks()->find($existingId); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |                 if (null === $webhook) { | 
					
						
							| 
									
										
										
										
											2021-03-08 09:56:40 +01:00
										 |  |  |                     return false; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 // set triggers etc.
 | 
					
						
							|  |  |  |                 $trigger  = $triggers[$webhook->trigger] ?? 0; | 
					
						
							|  |  |  |                 $response = $responses[$webhook->response] ?? 0; | 
					
						
							|  |  |  |                 $delivery = $deliveries[$webhook->delivery] ?? 0; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |             if (0 === $existingId) { | 
					
						
							| 
									
										
										
										
											2021-03-08 09:56:40 +01:00
										 |  |  |                 $trigger  = $triggers[$this->data['trigger']] ?? 0; | 
					
						
							|  |  |  |                 $response = $responses[$this->data['response']] ?? 0; | 
					
						
							|  |  |  |                 $delivery = $deliveries[$this->data['delivery']] ?? 0; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |             $url    = $this->data['url']; | 
					
						
							|  |  |  |             $userId = auth()->user()->id; | 
					
						
							| 
									
										
										
										
											2020-11-29 18:35:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return 0 === Webhook::whereUserId($userId) | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |                 ->where('trigger', $trigger) | 
					
						
							|  |  |  |                 ->where('response', $response) | 
					
						
							|  |  |  |                 ->where('delivery', $delivery) | 
					
						
							|  |  |  |                 ->where('id', '!=', $existingId) | 
					
						
							|  |  |  |                 ->where('url', $url)->count() | 
					
						
							|  |  |  |             ; | 
					
						
							| 
									
										
										
										
											2020-11-29 18:35:49 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-01-15 21:01:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-29 18:35:49 +01:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-04-06 08:51:27 +02:00
										 |  |  |      * Validate an object and its uniqueness. Checks for encryption / encrypted values as well. | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * parameter 0: the table | 
					
						
							|  |  |  |      * parameter 1: the field | 
					
						
							|  |  |  |      * parameter 2: an id to ignore (when editing) | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param mixed $attribute | 
					
						
							|  |  |  |      * @param mixed $value | 
					
						
							|  |  |  |      * @param mixed $parameters | 
					
						
							| 
									
										
										
										
											2020-11-29 18:35:49 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.UnusedFormalParameter") | 
					
						
							| 
									
										
										
										
											2020-11-29 18:35:49 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     public function validateUniqueObjectForUser($attribute, $value, $parameters): bool | 
					
						
							| 
									
										
										
										
											2020-11-29 18:35:49 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         [$table, $field] = $parameters; | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |         $exclude         = (int) ($parameters[2] ?? 0.0); | 
					
						
							| 
									
										
										
										
											2020-11-29 18:35:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         /* | 
					
						
							|  |  |  |          * If other data (in $this->getData()) contains | 
					
						
							|  |  |  |          * ID field, set that field to be the $exclude. | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |         $data            = $this->getData(); | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |         if (!array_key_exists(2, $parameters) && array_key_exists('id', $data) && (int) $data['id'] > 0) { | 
					
						
							|  |  |  |             $exclude = (int) $data['id']; | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         // get entries from table
 | 
					
						
							| 
									
										
										
										
											2025-02-23 12:47:04 +01:00
										 |  |  |         $result          = DB::table($table)->where('user_id', auth()->user()->id)->whereNull('deleted_at') | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |             ->where('id', '!=', $exclude) | 
					
						
							|  |  |  |             ->where($field, $value) | 
					
						
							|  |  |  |             ->first([$field]) | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2023-05-27 05:55:41 +02:00
										 |  |  |         if (null === $result) { | 
					
						
							|  |  |  |             return true; // not found, so true.
 | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-27 05:55:41 +02:00
										 |  |  |         // found, so not unique.
 | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2020-11-29 18:35:49 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 07:35:10 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param mixed $attribute | 
					
						
							|  |  |  |      * @param mixed $value | 
					
						
							|  |  |  |      * @param mixed $parameters | 
					
						
							| 
									
										
										
										
											2015-03-31 14:16:25 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.UnusedFormalParameter") | 
					
						
							| 
									
										
										
										
											2015-03-31 14:16:25 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     public function validateUniqueObjectGroup($attribute, $value, $parameters): bool | 
					
						
							| 
									
										
										
										
											2015-03-31 14:16:25 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         $exclude = $parameters[0] ?? null; | 
					
						
							| 
									
										
										
										
											2025-02-23 12:47:04 +01:00
										 |  |  |         $query   = DB::table('object_groups') | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |             ->whereNull('object_groups.deleted_at') | 
					
						
							|  |  |  |             ->where('object_groups.user_id', auth()->user()->id) | 
					
						
							|  |  |  |             ->where('object_groups.title', $value) | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         if (null !== $exclude) { | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |             $query->where('object_groups.id', '!=', (int) $exclude); | 
					
						
							| 
									
										
										
										
											2015-03-31 14:16:25 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         return 0 === $query->count(); | 
					
						
							| 
									
										
										
										
											2015-03-31 14:16:25 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-27 20:20:52 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param mixed $attribute | 
					
						
							|  |  |  |      * @param mixed $value | 
					
						
							|  |  |  |      * @param mixed $parameters | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.UnusedFormalParameter") | 
					
						
							| 
									
										
										
										
											2015-03-27 20:20:52 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     public function validateUniquePiggyBankForUser($attribute, $value, $parameters): bool | 
					
						
							| 
									
										
										
										
											2015-03-27 20:20:52 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         $exclude = $parameters[0] ?? null; | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |         $query   = PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id') | 
					
						
							| 
									
										
										
										
											2024-12-01 18:16:48 +01:00
										 |  |  |             ->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id') | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  |             ->where('accounts.user_id', auth()->user()->id) | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         if (null !== $exclude) { | 
					
						
							| 
									
										
										
										
											2024-10-08 07:21:23 +02:00
										 |  |  |             $query->where('piggy_banks.id', '!=', (int) $exclude); | 
					
						
							| 
									
										
										
										
											2018-12-03 07:18:05 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-08-11 14:13:19 +02:00
										 |  |  |         $query->where('piggy_banks.name', $value); | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-05 14:33:33 +01:00
										 |  |  |         return 0 === $query->count(); | 
					
						
							| 
									
										
										
										
											2015-03-27 20:20:52 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-10 20:14:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param mixed $value | 
					
						
							|  |  |  |      * @param mixed $parameters | 
					
						
							| 
									
										
										
										
											2020-11-29 18:35:49 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.UnusedFormalParameter") | 
					
						
							| 
									
										
										
										
											2019-06-10 20:14:00 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     public function validateUniqueWebhook($value, $parameters): bool | 
					
						
							| 
									
										
										
										
											2019-06-10 20:14:00 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         if (auth()->check()) { | 
					
						
							| 
									
										
										
										
											2022-09-18 10:45:38 +02:00
										 |  |  |             $triggers   = Webhook::getTriggersForValidation(); | 
					
						
							|  |  |  |             $responses  = Webhook::getResponsesForValidation(); | 
					
						
							|  |  |  |             $deliveries = Webhook::getDeliveriesForValidation(); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // integers
 | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |             $trigger    = $triggers[$this->data['trigger']] ?? 0; | 
					
						
							|  |  |  |             $response   = $responses[$this->data['response']] ?? 0; | 
					
						
							|  |  |  |             $delivery   = $deliveries[$this->data['delivery']] ?? 0; | 
					
						
							|  |  |  |             $url        = $this->data['url']; | 
					
						
							|  |  |  |             $userId     = auth()->user()->id; | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return 0 === Webhook::whereUserId($userId) | 
					
						
							| 
									
										
										
										
											2024-10-14 05:14:52 +02:00
										 |  |  |                 ->where('trigger', $trigger) | 
					
						
							|  |  |  |                 ->where('response', $response) | 
					
						
							|  |  |  |                 ->where('delivery', $delivery) | 
					
						
							| 
									
										
										
										
											2025-05-05 05:25:13 +02:00
										 |  |  |                 ->where('url', $url)->count() | 
					
						
							|  |  |  |             ; | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2019-06-10 20:14:00 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-02-08 01:15:15 +01:00
										 |  |  | } |