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.10 |
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 given 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
|
|
|
* Load and returns decoded Json from given file |
99
|
|
|
* |
100
|
|
|
* @access public |
101
|
|
|
* @static |
102
|
|
|
* @param string $filePath The file's full path |
103
|
|
|
* @param bool $throwError Throw error on true or silent process. Default is true |
104
|
|
|
* |
105
|
|
|
* @return object|null |
106
|
|
|
* @throws \Exception |
107
|
|
|
* @throws \LogicException |
108
|
|
|
*/ |
109
|
|
|
protected static function loadJsonFile(string $filePath, bool $throwError = true) |
110
|
|
|
{ |
111
|
|
|
// check file exists |
112
|
|
|
if (!file_exists($filePath) || !is_file($filePath)){ |
113
|
|
|
if ($throwError) { |
114
|
|
|
throw new \Exception('Config file not found'); |
115
|
|
|
} |
116
|
|
|
return null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// get and parse content |
120
|
|
|
$content = utf8_encode(file_get_contents($filePath)); |
121
|
|
|
$json = json_decode($content); |
122
|
|
|
|
123
|
|
|
// check for errors |
124
|
|
|
if ($json == null && json_last_error() != JSON_ERROR_NONE && $throwError) { |
125
|
|
|
throw new \LogicException(sprintf("Failed to parse config file Error: '%s'", json_last_error_msg())); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $json; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
} |