|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* _ _ ___ ____ ____ ____ |
|
5
|
|
|
* / \ | |__ _ _ ___ ___|_ _| _ \| _ \| __ ) |
|
6
|
|
|
* / _ \ | '_ \| | | / __|/ _ \| || |_) | | | | _ \ |
|
7
|
|
|
* / ___ \| |_) | |_| \__ \ __/| || __/| |_| | |_) | |
|
8
|
|
|
* /_/ \_\_.__/ \__,_|___/\___|___|_| |____/|____/ |
|
9
|
|
|
* |
|
10
|
|
|
* This file is part of Kristuff\AbsuseIPDB. |
|
11
|
|
|
* |
|
12
|
|
|
* (c) Kristuff <[email protected]> |
|
13
|
|
|
* |
|
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
15
|
|
|
* file that was distributed with this source code. |
|
16
|
|
|
* |
|
17
|
|
|
* @version 0.9.9 |
|
18
|
|
|
* @copyright 2020-2021 Kristuff |
|
19
|
|
|
*/ |
|
20
|
|
|
namespace Kristuff\AbuseIPDB; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class Utils |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
trait UtilsTrait |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* helper function to get formatted date |
|
30
|
|
|
* |
|
31
|
|
|
* @access private |
|
32
|
|
|
* @static |
|
33
|
|
|
* @param string $date The UTC date |
|
34
|
|
|
* |
|
35
|
|
|
* @return string Formated time |
|
36
|
|
|
*/ |
|
37
|
|
|
protected static function getDate($date) |
|
38
|
|
|
{ |
|
39
|
|
|
//2020-05-22T17:06:35+00:00 |
|
40
|
|
|
return \DateTime::createFromFormat('Y-m-d\TH:i:s+', $date)->format('Y-m-d H:i:s'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* helper function to get the color corresponding to given score: |
|
45
|
|
|
* 0 : green |
|
46
|
|
|
* 1-50 : yellow |
|
47
|
|
|
* > 50 : lightred |
|
48
|
|
|
* |
|
49
|
|
|
* @access protected |
|
50
|
|
|
* @static |
|
51
|
|
|
* @param mixed $score |
|
52
|
|
|
* |
|
53
|
|
|
* @return string |
|
54
|
|
|
* |
|
55
|
|
|
*/ |
|
56
|
|
|
protected static function getScoreColor($score) |
|
57
|
|
|
{ |
|
58
|
|
|
$score = intval($score); |
|
59
|
|
|
return $score > 50 ? 'lightred' : ($score > 0 ? 'yellow' : 'green') ; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Helper function to get the value of an argument |
|
65
|
|
|
* |
|
66
|
|
|
* @access protected |
|
67
|
|
|
* @static |
|
68
|
|
|
* @param array $arguments The list of arguments |
|
69
|
|
|
* @param string $shortArg The short argument name |
|
70
|
|
|
* @param string $longArg The long argument name |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
* |
|
74
|
|
|
*/ |
|
75
|
|
|
protected static function getArgumentValue(array $arguments, string $shortArg, string $longArg) |
|
76
|
|
|
{ |
|
77
|
|
|
return (array_key_exists($shortArg, $arguments) ? $arguments[$shortArg] : |
|
78
|
|
|
(array_key_exists($longArg, $arguments) ? $arguments[$longArg] : '')); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* helper function to check if a argument is given |
|
83
|
|
|
* |
|
84
|
|
|
* @access protected |
|
85
|
|
|
* @static |
|
86
|
|
|
* @param array $arguments The list of arguments |
|
87
|
|
|
* @param string $shortArg The short argument name |
|
88
|
|
|
* @param string $longArg The long argument name |
|
89
|
|
|
* |
|
90
|
|
|
* @return bool True if the short or long argument exist in the arguments array, otherwise false |
|
91
|
|
|
*/ |
|
92
|
|
|
protected static function inArguments(array $arguments, string $shortArg, string $longArg) |
|
93
|
|
|
{ |
|
94
|
|
|
return array_key_exists($shortArg, $arguments) || array_key_exists($longArg, $arguments); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Exit with code 0 or given exit code |
|
99
|
|
|
* |
|
100
|
|
|
* @access protected |
|
101
|
|
|
* @static |
|
102
|
|
|
* |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
|
|
protected static function safeExit(int $exitCode = 0) |
|
106
|
|
|
{ |
|
107
|
|
|
exit($exitCode); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
} |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.