This commit is contained in:
James Cole 2023-12-21 05:06:51 +01:00
parent 135b9fc010
commit 1f7ceb6df6
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
2 changed files with 87 additions and 86 deletions

View File

@ -82,7 +82,8 @@ class ParseDateString
// if regex for YYYY-MM-DD: // if regex for YYYY-MM-DD:
$pattern = '/^(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])$/'; $pattern = '/^(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])$/';
if (false !== preg_match($pattern, $date)) { $result = preg_match($pattern, $date);
if (false !== $result && 0 !== $result) {
return $this->parseDefaultDate($date); return $this->parseDefaultDate($date);
} }
@ -108,7 +109,7 @@ class ParseDateString
return new Carbon('1984-09-17'); return new Carbon('1984-09-17');
} }
// maybe a year, nothing else? // maybe a year, nothing else?
if (4 === strlen($date) && is_numeric($date) && (int)$date > 1000 && (int)$date <= 3000) { if (4 === strlen($date) && is_numeric($date) && (int) $date > 1000 && (int) $date <= 3000) {
return new Carbon(sprintf('%d-01-01', $date)); return new Carbon(sprintf('%d-01-01', $date));
} }
@ -223,15 +224,15 @@ class ParseDateString
// verify if correct // verify if correct
$pattern = '/[+-]\d+[wqmdy]/'; $pattern = '/[+-]\d+[wqmdy]/';
$res = preg_match($pattern, $part); $result = preg_match($pattern, $part);
if (0 === $res || false === $res) { if (0 === $result || false === $result) {
app('log')->error(sprintf('Part "%s" does not match regular expression. Will be skipped.', $part)); app('log')->error(sprintf('Part "%s" does not match regular expression. Will be skipped.', $part));
continue; continue;
} }
$direction = str_starts_with($part, '+') ? 1 : 0; $direction = str_starts_with($part, '+') ? 1 : 0;
$period = $part[strlen($part) - 1]; $period = $part[strlen($part) - 1];
$number = (int)substr($part, 1, -1); $number = (int) substr($part, 1, -1);
if (!array_key_exists($period, $functions[$direction])) { if (!array_key_exists($period, $functions[$direction])) {
app('log')->error(sprintf('No method for direction %d and period "%s".', $direction, $period)); app('log')->error(sprintf('No method for direction %d and period "%s".', $direction, $period));
@ -246,11 +247,19 @@ class ParseDateString
return $today; return $today;
} }
/**
* Returns true if this matches regex for xxxx-xx-DD:
*
* @param string $date
*
* @return bool
*/
protected function isDayRange(string $date): bool protected function isDayRange(string $date): bool
{ {
// if regex for xxxx-xx-DD:
$pattern = '/^xxxx-xx-(0[1-9]|[12]\d|3[01])$/'; $pattern = '/^xxxx-xx-(0[1-9]|[12]\d|3[01])$/';
if (false !== preg_match($pattern, $date)) { $result = preg_match($pattern, $date);
if (false !== $result && 0 !== $result) {
app('log')->debug(sprintf('"%s" is a day range.', $date)); app('log')->debug(sprintf('"%s" is a day range.', $date));
return true; return true;
@ -276,7 +285,8 @@ class ParseDateString
{ {
// if regex for xxxx-MM-xx: // if regex for xxxx-MM-xx:
$pattern = '/^xxxx-(0[1-9]|1[012])-xx$/'; $pattern = '/^xxxx-(0[1-9]|1[012])-xx$/';
if (false !== preg_match($pattern, $date)) { $result = preg_match($pattern, $date);
if (false !== $result && 0 !== $result) {
app('log')->debug(sprintf('"%s" is a month range.', $date)); app('log')->debug(sprintf('"%s" is a month range.', $date));
return true; return true;
@ -303,7 +313,8 @@ class ParseDateString
{ {
// if regex for YYYY-xx-xx: // if regex for YYYY-xx-xx:
$pattern = '/^(19|20)\d\d-xx-xx$/'; $pattern = '/^(19|20)\d\d-xx-xx$/';
if (false !== preg_match($pattern, $date)) { $result = preg_match($pattern, $date);
if (false !== $result && 0 !== $result) {
app('log')->debug(sprintf('"%s" is a year range.', $date)); app('log')->debug(sprintf('"%s" is a year range.', $date));
return true; return true;
@ -330,7 +341,8 @@ class ParseDateString
{ {
// if regex for xxxx-MM-DD: // if regex for xxxx-MM-DD:
$pattern = '/^xxxx-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])$/'; $pattern = '/^xxxx-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])$/';
if (false !== preg_match($pattern, $date)) { $result = preg_match($pattern, $date);
if (false !== $result && 0 !== $result) {
app('log')->debug(sprintf('"%s" is a month/day range.', $date)); app('log')->debug(sprintf('"%s" is a month/day range.', $date));
return true; return true;
@ -344,7 +356,8 @@ class ParseDateString
{ {
// if regex for YYYY-xx-DD: // if regex for YYYY-xx-DD:
$pattern = '/^(19|20)\d\d-xx-(0[1-9]|[12]\d|3[01])$/'; $pattern = '/^(19|20)\d\d-xx-(0[1-9]|[12]\d|3[01])$/';
if (false !== preg_match($pattern, $date)) { $result = preg_match($pattern, $date);
if (false !== $result && 0 !== $result) {
app('log')->debug(sprintf('"%s" is a day/year range.', $date)); app('log')->debug(sprintf('"%s" is a day/year range.', $date));
return true; return true;
@ -358,7 +371,8 @@ class ParseDateString
{ {
// if regex for YYYY-MM-xx: // if regex for YYYY-MM-xx:
$pattern = '/^(19|20)\d\d-(0[1-9]|1[012])-xx$/'; $pattern = '/^(19|20)\d\d-(0[1-9]|1[012])-xx$/';
if (false !== preg_match($pattern, $date)) { $result = preg_match($pattern, $date);
if (false !== $result && 0 !== $result) {
app('log')->debug(sprintf('"%s" is a month/year range.', $date)); app('log')->debug(sprintf('"%s" is a month/year range.', $date));
return true; return true;

View File

@ -74,7 +74,7 @@ class FireflyValidator extends Validator
$secret = ''; $secret = '';
} }
return (bool)\Google2FA::verifyKey((string)$secret, $value); return (bool) \Google2FA::verifyKey((string) $secret, $value);
} }
/** /**
@ -88,7 +88,7 @@ class FireflyValidator extends Validator
{ {
$field = $parameters[1] ?? 'id'; $field = $parameters[1] ?? 'id';
if (0 === (int)$value) { if (0 === (int) $value) {
return true; return true;
} }
$count = \DB::table($parameters[0])->where('user_id', auth()->user()->id)->where($field, $value)->count(); $count = \DB::table($parameters[0])->where('user_id', auth()->user()->id)->where($field, $value)->count();
@ -106,10 +106,7 @@ class FireflyValidator extends Validator
{ {
$regex = '/^[a-z]{6}[0-9a-z]{2}([0-9a-z]{3})?\z/i'; $regex = '/^[a-z]{6}[0-9a-z]{2}([0-9a-z]{3})?\z/i';
$result = preg_match($regex, $value); $result = preg_match($regex, $value);
if (false === $result) { if (false === $result || 0 === $result) {
return false;
}
if (0 === $result) {
return false; return false;
} }
@ -180,7 +177,7 @@ class FireflyValidator extends Validator
$value = strtoupper($value); $value = strtoupper($value);
// replace characters outside of ASCI range. // replace characters outside of ASCI range.
$value = (string)iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value); $value = (string) iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
$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']; $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'];
$replace = [ $replace = [
'', '',
@ -215,7 +212,7 @@ class FireflyValidator extends Validator
// take // take
$first = substr($value, 0, 4); $first = substr($value, 0, 4);
$last = substr($value, 4); $last = substr($value, 4);
$iban = $last.$first; $iban = $last . $first;
$iban = trim(str_replace($search, $replace, $iban)); $iban = trim(str_replace($search, $replace, $iban));
if ('' === $iban) { if ('' === $iban) {
return false; return false;
@ -231,7 +228,7 @@ class FireflyValidator extends Validator
return false; return false;
} }
return 1 === (int)$checksum; return 1 === (int) $checksum;
} }
/** /**
@ -246,7 +243,7 @@ class FireflyValidator extends Validator
/** @var mixed $compare */ /** @var mixed $compare */
$compare = $parameters[0] ?? '0'; $compare = $parameters[0] ?? '0';
return bccomp((string)$value, (string)$compare) < 0; return bccomp((string) $value, (string) $compare) < 0;
} }
/** /**
@ -261,7 +258,7 @@ class FireflyValidator extends Validator
/** @var mixed $compare */ /** @var mixed $compare */
$compare = $parameters[0] ?? '0'; $compare = $parameters[0] ?? '0';
return bccomp((string)$value, (string)$compare) > 0; return bccomp((string) $value, (string) $compare) > 0;
} }
/** /**
@ -275,7 +272,7 @@ class FireflyValidator extends Validator
{ {
$field = $parameters[1] ?? 'id'; $field = $parameters[1] ?? 'id';
if (0 === (int)$value) { if (0 === (int) $value) {
return true; return true;
} }
$count = \DB::table($parameters[0])->where($field, $value)->count(); $count = \DB::table($parameters[0])->where($field, $value)->count();
@ -288,7 +285,7 @@ class FireflyValidator extends Validator
// first, get the index from this string: // first, get the index from this string:
$value ??= ''; $value ??= '';
$parts = explode('.', $attribute); $parts = explode('.', $attribute);
$index = (int)($parts[1] ?? '0'); $index = (int) ($parts[1] ?? '0');
// get the name of the trigger from the data array: // get the name of the trigger from the data array:
$actionType = $this->data['actions'][$index]['type'] ?? 'invalid'; $actionType = $this->data['actions'][$index]['type'] ?? 'invalid';
@ -353,7 +350,7 @@ class FireflyValidator extends Validator
{ {
// first, get the index from this string: // first, get the index from this string:
$parts = explode('.', $attribute); $parts = explode('.', $attribute);
$index = (int)($parts[1] ?? '0'); $index = (int) ($parts[1] ?? '0');
// get the name of the trigger from the data array: // get the name of the trigger from the data array:
$triggerType = $this->data['triggers'][$index]['type'] ?? 'invalid'; $triggerType = $this->data['triggers'][$index]['type'] ?? 'invalid';
@ -398,7 +395,7 @@ class FireflyValidator extends Validator
// check if it's an existing account. // check if it's an existing account.
if (in_array($triggerType, ['destination_account_id', 'source_account_id'], true)) { if (in_array($triggerType, ['destination_account_id', 'source_account_id'], true)) {
return is_numeric($value) && (int)$value > 0; return is_numeric($value) && (int) $value > 0;
} }
// check transaction type. // check transaction type.
@ -435,7 +432,7 @@ class FireflyValidator extends Validator
{ {
$verify = false; $verify = false;
if (array_key_exists('verify_password', $this->data)) { if (array_key_exists('verify_password', $this->data)) {
$verify = 1 === (int)$this->data['verify_password']; $verify = 1 === (int) $this->data['verify_password'];
} }
if ($verify) { if ($verify) {
/** @var Verifier $service */ /** @var Verifier $service */
@ -470,7 +467,7 @@ class FireflyValidator extends Validator
if (array_key_exists('type', $this->data)) { if (array_key_exists('type', $this->data)) {
app('log')->debug('validateUniqueAccountForUser::typeString'); app('log')->debug('validateUniqueAccountForUser::typeString');
return $this->validateByAccountTypeString($value, $parameters, (string)$this->data['type']); return $this->validateByAccountTypeString($value, $parameters, (string) $this->data['type']);
} }
if (array_key_exists('account_type_id', $this->data)) { if (array_key_exists('account_type_id', $this->data)) {
app('log')->debug('validateUniqueAccountForUser::typeId'); app('log')->debug('validateUniqueAccountForUser::typeId');
@ -481,7 +478,7 @@ class FireflyValidator extends Validator
if (null !== $parameterId) { if (null !== $parameterId) {
app('log')->debug('validateUniqueAccountForUser::paramId'); app('log')->debug('validateUniqueAccountForUser::paramId');
return $this->validateByParameterId((int)$parameterId, $value); return $this->validateByParameterId((int) $parameterId, $value);
} }
if (array_key_exists('id', $this->data)) { if (array_key_exists('id', $this->data)) {
app('log')->debug('validateUniqueAccountForUser::accountId'); app('log')->debug('validateUniqueAccountForUser::accountId');
@ -504,17 +501,16 @@ class FireflyValidator extends Validator
*/ */
public function validateUniqueAccountNumberForUser($attribute, $value, $parameters): bool public function validateUniqueAccountNumberForUser($attribute, $value, $parameters): bool
{ {
$accountId = (int)($this->data['id'] ?? 0.0); $accountId = (int) ($this->data['id'] ?? 0.0);
if (0 === $accountId) { if (0 === $accountId) {
$accountId = (int)($parameters[0] ?? 0.0); $accountId = (int) ($parameters[0] ?? 0.0);
} }
$query = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id') $query = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id')
->whereNull('accounts.deleted_at') ->whereNull('accounts.deleted_at')
->where('accounts.user_id', auth()->user()->id) ->where('accounts.user_id', auth()->user()->id)
->where('account_meta.name', 'account_number') ->where('account_meta.name', 'account_number')
->where('account_meta.data', json_encode($value)) ->where('account_meta.data', json_encode($value));
;
if ($accountId > 0) { if ($accountId > 0) {
// exclude current account from check. // exclude current account from check.
@ -541,7 +537,7 @@ class FireflyValidator extends Validator
/** @var AccountMeta $entry */ /** @var AccountMeta $entry */
foreach ($set as $entry) { foreach ($set as $entry) {
$otherAccount = $entry->account; $otherAccount = $entry->account;
$otherType = (string)config(sprintf('firefly.shortNamesByFullName.%s', $otherAccount->accountType->type)); $otherType = (string) config(sprintf('firefly.shortNamesByFullName.%s', $otherAccount->accountType->type));
if (('expense' === $otherType || 'revenue' === $otherType) && $otherType !== $type) { if (('expense' === $otherType || 'revenue' === $otherType) && $otherType !== $type) {
app('log')->debug(sprintf('The other account with this account number is a "%s" so return true.', $otherType)); app('log')->debug(sprintf('The other account with this account number is a "%s" so return true.', $otherType));
@ -556,9 +552,9 @@ class FireflyValidator extends Validator
/** /**
* @SuppressWarnings(PHPMD.UnusedFormalParameter) * @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/ */
public function validateUniqueCurrencyCode(null|string $attribute, null|string $value): bool public function validateUniqueCurrencyCode(null | string $attribute, null | string $value): bool
{ {
return $this->validateUniqueCurrency('code', (string)$attribute, (string)$value); return $this->validateUniqueCurrency('code', (string) $attribute, (string) $value);
} }
/** /**
@ -569,14 +565,14 @@ class FireflyValidator extends Validator
return 0 === \DB::table('transaction_currencies')->where($field, $value)->whereNull('deleted_at')->count(); return 0 === \DB::table('transaction_currencies')->where($field, $value)->whereNull('deleted_at')->count();
} }
public function validateUniqueCurrencyName(null|string $attribute, null|string $value): bool public function validateUniqueCurrencyName(null | string $attribute, null | string $value): bool
{ {
return $this->validateUniqueCurrency('name', (string)$attribute, (string)$value); return $this->validateUniqueCurrency('name', (string) $attribute, (string) $value);
} }
public function validateUniqueCurrencySymbol(null|string $attribute, null|string $value): bool public function validateUniqueCurrencySymbol(null | string $attribute, null | string $value): bool
{ {
return $this->validateUniqueCurrency('symbol', (string)$attribute, (string)$value); return $this->validateUniqueCurrency('symbol', (string) $attribute, (string) $value);
} }
/** /**
@ -588,7 +584,7 @@ class FireflyValidator extends Validator
*/ */
public function validateUniqueExistingWebhook($value, $parameters, $something): bool public function validateUniqueExistingWebhook($value, $parameters, $something): bool
{ {
$existingId = (int)($something[0] ?? 0); $existingId = (int) ($something[0] ?? 0);
$trigger = 0; $trigger = 0;
$response = 0; $response = 0;
$delivery = 0; $delivery = 0;
@ -617,12 +613,11 @@ class FireflyValidator extends Validator
$userId = auth()->user()->id; $userId = auth()->user()->id;
return 0 === Webhook::whereUserId($userId) return 0 === Webhook::whereUserId($userId)
->where('trigger', $trigger) ->where('trigger', $trigger)
->where('response', $response) ->where('response', $response)
->where('delivery', $delivery) ->where('delivery', $delivery)
->where('id', '!=', $existingId) ->where('id', '!=', $existingId)
->where('url', $url)->count() ->where('url', $url)->count();
;
} }
return false; return false;
@ -644,22 +639,21 @@ class FireflyValidator extends Validator
public function validateUniqueObjectForUser($attribute, $value, $parameters): bool public function validateUniqueObjectForUser($attribute, $value, $parameters): bool
{ {
[$table, $field] = $parameters; [$table, $field] = $parameters;
$exclude = (int)($parameters[2] ?? 0.0); $exclude = (int) ($parameters[2] ?? 0.0);
/* /*
* If other data (in $this->getData()) contains * If other data (in $this->getData()) contains
* ID field, set that field to be the $exclude. * ID field, set that field to be the $exclude.
*/ */
$data = $this->getData(); $data = $this->getData();
if (!array_key_exists(2, $parameters) && array_key_exists('id', $data) && (int)$data['id'] > 0) { if (!array_key_exists(2, $parameters) && array_key_exists('id', $data) && (int) $data['id'] > 0) {
$exclude = (int)$data['id']; $exclude = (int) $data['id'];
} }
// get entries from table // get entries from table
$result = \DB::table($table)->where('user_id', auth()->user()->id)->whereNull('deleted_at') $result = \DB::table($table)->where('user_id', auth()->user()->id)->whereNull('deleted_at')
->where('id', '!=', $exclude) ->where('id', '!=', $exclude)
->where($field, $value) ->where($field, $value)
->first([$field]) ->first([$field]);
;
if (null === $result) { if (null === $result) {
return true; // not found, so true. return true; // not found, so true.
} }
@ -679,12 +673,11 @@ class FireflyValidator extends Validator
{ {
$exclude = $parameters[0] ?? null; $exclude = $parameters[0] ?? null;
$query = \DB::table('object_groups') $query = \DB::table('object_groups')
->whereNull('object_groups.deleted_at') ->whereNull('object_groups.deleted_at')
->where('object_groups.user_id', auth()->user()->id) ->where('object_groups.user_id', auth()->user()->id)
->where('object_groups.title', $value) ->where('object_groups.title', $value);
;
if (null !== $exclude) { if (null !== $exclude) {
$query->where('object_groups.id', '!=', (int)$exclude); $query->where('object_groups.id', '!=', (int) $exclude);
} }
return 0 === $query->count(); return 0 === $query->count();
@ -701,10 +694,9 @@ class FireflyValidator extends Validator
{ {
$exclude = $parameters[0] ?? null; $exclude = $parameters[0] ?? null;
$query = \DB::table('piggy_banks')->whereNull('piggy_banks.deleted_at') $query = \DB::table('piggy_banks')->whereNull('piggy_banks.deleted_at')
->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')->where('accounts.user_id', auth()->user()->id) ->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')->where('accounts.user_id', auth()->user()->id);
;
if (null !== $exclude) { if (null !== $exclude) {
$query->where('piggy_banks.id', '!=', (int)$exclude); $query->where('piggy_banks.id', '!=', (int) $exclude);
} }
$query->where('piggy_banks.name', $value); $query->where('piggy_banks.name', $value);
@ -732,11 +724,10 @@ class FireflyValidator extends Validator
$userId = auth()->user()->id; $userId = auth()->user()->id;
return 0 === Webhook::whereUserId($userId) return 0 === Webhook::whereUserId($userId)
->where('trigger', $trigger) ->where('trigger', $trigger)
->where('response', $response) ->where('response', $response)
->where('delivery', $delivery) ->where('delivery', $delivery)
->where('url', $url)->count() ->where('url', $url)->count();
;
} }
return false; return false;
@ -762,21 +753,20 @@ class FireflyValidator extends Validator
private function validateByAccountTypeString(string $value, array $parameters, string $type): bool private function validateByAccountTypeString(string $value, array $parameters, string $type): bool
{ {
/** @var null|array $search */ /** @var null|array $search */
$search = \Config::get('firefly.accountTypeByIdentifier.'.$type); $search = \Config::get('firefly.accountTypeByIdentifier.' . $type);
if (null === $search) { if (null === $search) {
return false; return false;
} }
$accountTypes = AccountType::whereIn('type', $search)->get(); $accountTypes = AccountType::whereIn('type', $search)->get();
$ignore = (int)($parameters[0] ?? 0.0); $ignore = (int) ($parameters[0] ?? 0.0);
$accountTypeIds = $accountTypes->pluck('id')->toArray(); $accountTypeIds = $accountTypes->pluck('id')->toArray();
/** @var null|Account $result */ /** @var null|Account $result */
$result = auth()->user()->accounts()->whereIn('account_type_id', $accountTypeIds)->where('id', '!=', $ignore) $result = auth()->user()->accounts()->whereIn('account_type_id', $accountTypeIds)->where('id', '!=', $ignore)
->where('name', $value) ->where('name', $value)
->first() ->first();
;
return null === $result; return null === $result;
} }
@ -788,13 +778,12 @@ class FireflyValidator extends Validator
private function validateByAccountTypeId($value, $parameters): bool private function validateByAccountTypeId($value, $parameters): bool
{ {
$type = AccountType::find($this->data['account_type_id'])->first(); $type = AccountType::find($this->data['account_type_id'])->first();
$ignore = (int)($parameters[0] ?? 0.0); $ignore = (int) ($parameters[0] ?? 0.0);
/** @var null|Account $result */ /** @var null|Account $result */
$result = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore) $result = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
->where('name', $value) ->where('name', $value)
->first() ->first();
;
return null === $result; return null === $result;
} }
@ -811,9 +800,8 @@ class FireflyValidator extends Validator
$ignore = $existingAccount->id; $ignore = $existingAccount->id;
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore) $entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
->where('name', $value) ->where('name', $value)
->first() ->first();
;
return null === $entry; return null === $entry;
} }
@ -830,9 +818,8 @@ class FireflyValidator extends Validator
$ignore = $existingAccount->id; $ignore = $existingAccount->id;
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore) $entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
->where('name', $value) ->where('name', $value)
->first() ->first();
;
return null === $entry; return null === $entry;
} }