1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ ___ ___ ___ ___ |
5
|
|
|
* __ _| |__ _ _ ___ ___|_ _| _ \ \| _ ) |
6
|
|
|
* / _` | '_ \ || (_-</ -_)| || _/ |) | _ \ |
7
|
|
|
* \__,_|_.__/\_,_/__/\___|___|_| |___/|___/ |
8
|
|
|
* |
9
|
|
|
* This file is part of Kristuff\AbuseIPDB. |
10
|
|
|
* |
11
|
|
|
* (c) Kristuff <[email protected]> |
12
|
|
|
* |
13
|
|
|
* For the full copyright and license information, please view the LICENSE |
14
|
|
|
* file that was distributed with this source code. |
15
|
|
|
* |
16
|
|
|
* @version 0.9.20 |
17
|
|
|
* @copyright 2020-2022 Kristuff |
18
|
|
|
*/ |
19
|
|
|
namespace Kristuff\AbuseIPDB; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Trait Utils |
23
|
|
|
*/ |
24
|
|
|
trait UtilsTrait |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* helper function to get formatted date |
28
|
|
|
* |
29
|
|
|
* @access private |
30
|
|
|
* @static |
31
|
|
|
* @param string $date The UTC date |
32
|
|
|
* |
33
|
|
|
* @return string Formated time |
34
|
|
|
*/ |
35
|
|
|
protected static function getDate($date): string |
36
|
|
|
{ |
37
|
|
|
//2020-05-22T17:06:35+00:00 |
38
|
|
|
return \DateTime::createFromFormat('Y-m-d\TH:i:s+', $date)->format('Y-m-d H:i:s'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* helper function to get the color corresponding to given score: |
43
|
|
|
* 0 : green |
44
|
|
|
* 1-50 : yellow |
45
|
|
|
* > 50 : lightred |
46
|
|
|
* |
47
|
|
|
* @access protected |
48
|
|
|
* @static |
49
|
|
|
* @param mixed $score |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
* |
53
|
|
|
*/ |
54
|
|
|
protected static function getScoreColor($score): string |
55
|
|
|
{ |
56
|
|
|
$score = intval($score); |
57
|
|
|
return $score > 50 ? 'lightred' : ($score > 0 ? 'yellow' : 'green') ; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Helper function to get the value of an argument |
63
|
|
|
* |
64
|
|
|
* @access protected |
65
|
|
|
* @static |
66
|
|
|
* @param array $arguments The list of arguments |
67
|
|
|
* @param string $shortArg The short argument name |
68
|
|
|
* @param string $longArg The long argument name |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
* |
72
|
|
|
*/ |
73
|
|
|
protected static function getArgumentValue(array $arguments, string $shortArg, string $longArg): string |
74
|
|
|
{ |
75
|
|
|
return (array_key_exists($shortArg, $arguments) ? $arguments[$shortArg] : |
76
|
|
|
(array_key_exists($longArg, $arguments) ? $arguments[$longArg] : '')); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* helper function to check if a given argument is given |
81
|
|
|
* |
82
|
|
|
* @access protected |
83
|
|
|
* @static |
84
|
|
|
* @param array $arguments The list of arguments |
85
|
|
|
* @param string $shortArg The short argument name |
86
|
|
|
* @param string $longArg The long argument name |
87
|
|
|
* |
88
|
|
|
* @return bool True if the short or long argument exist in the arguments array, otherwise false |
89
|
|
|
*/ |
90
|
|
|
protected static function inArguments(array $arguments, string $shortArg, string $longArg): bool |
91
|
|
|
{ |
92
|
|
|
return array_key_exists($shortArg, $arguments) || array_key_exists($longArg, $arguments); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Load and returns decoded Json from given file |
97
|
|
|
* |
98
|
|
|
* @access public |
99
|
|
|
* @static |
100
|
|
|
* @param string $filePath The file's full path |
101
|
|
|
* @param bool $throwError Throw error on true or silent process. Default is true |
102
|
|
|
* |
103
|
|
|
* @return object|null |
104
|
|
|
* @throws \Exception |
105
|
|
|
* @throws \LogicException |
106
|
|
|
*/ |
107
|
|
|
protected static function loadJsonFile(string $filePath, bool $throwError = true): ?object |
108
|
|
|
{ |
109
|
|
|
// check file exists |
110
|
|
|
if (!file_exists($filePath) || !is_file($filePath)){ |
111
|
|
|
if ($throwError) { |
112
|
|
|
throw new \Exception('Config file not found'); |
113
|
|
|
} |
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// get and parse content |
118
|
|
|
$content = utf8_encode(file_get_contents($filePath)); |
119
|
|
|
$json = json_decode($content); |
120
|
|
|
|
121
|
|
|
// check for errors |
122
|
|
|
if ($json == null && json_last_error() != JSON_ERROR_NONE && $throwError) { |
123
|
|
|
throw new \LogicException(sprintf("Failed to parse config file Error: '%s'", json_last_error_msg())); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $json; |
127
|
|
|
} |
128
|
|
|
} |