1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database\Cache; |
6
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
7
|
|
|
use GeminiLabs\Vectorface\Whip\Whip; |
8
|
|
|
|
9
|
|
|
class Helper |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @param string $name |
13
|
|
|
* @param string $path |
14
|
|
|
* @return string |
15
|
|
|
*/ |
16
|
10 |
|
public static function buildClassName($name, $path = '') |
17
|
|
|
{ |
18
|
10 |
|
$className = Str::camelCase($name); |
19
|
10 |
|
$path = ltrim(str_replace(__NAMESPACE__, '', $path), '\\'); |
20
|
10 |
|
return !empty($path) |
21
|
2 |
|
? __NAMESPACE__.'\\'.$path.'\\'.$className |
22
|
10 |
|
: $className; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $name |
27
|
|
|
* @param string $prefix |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
9 |
|
public static function buildMethodName($name, $prefix = '') |
31
|
|
|
{ |
32
|
9 |
|
return lcfirst($prefix.static::buildClassName($name)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $name |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
1 |
|
public static function buildPropertyName($name) |
40
|
|
|
{ |
41
|
1 |
|
return static::buildMethodName($name); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $cast |
46
|
|
|
* @param mixed $value |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
7 |
|
public static function castTo($cast = '', $value) |
50
|
|
|
{ |
51
|
7 |
|
$method = static::buildMethodName($cast, 'castTo'); |
52
|
7 |
|
return !empty($cast) && method_exists(__CLASS__, $method) |
53
|
|
|
? static::$method($value) |
54
|
7 |
|
: $value; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param mixed $value |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public static function castToArray($value) |
62
|
|
|
{ |
63
|
|
|
return (array) $value; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param mixed $value |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
1 |
|
public static function castToBool($value) |
71
|
|
|
{ |
72
|
1 |
|
return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param mixed $value |
77
|
|
|
* @return float |
78
|
|
|
*/ |
79
|
|
|
public static function castToFloat($value) |
80
|
|
|
{ |
81
|
|
|
return (float) filter_var($value, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param mixed $value |
86
|
|
|
* @return int |
87
|
|
|
*/ |
88
|
|
|
public static function castToInt($value) |
89
|
|
|
{ |
90
|
|
|
return (int) filter_var($value, FILTER_VALIDATE_INT); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param mixed $value |
95
|
|
|
* @return object |
96
|
|
|
*/ |
97
|
|
|
public static function castToObject($value) |
98
|
|
|
{ |
99
|
|
|
return (object) (array) $value; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param mixed $value |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public static function castToString($value) |
107
|
|
|
{ |
108
|
|
|
if (is_object($value) && in_array('__toString', get_class_methods($value))) { |
109
|
|
|
return (string) $value->__toString(); |
110
|
|
|
} |
111
|
|
|
if (is_array($value) || is_object($value)) { |
112
|
|
|
return serialize($value); |
113
|
|
|
} |
114
|
|
|
return (string) $value; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $key |
119
|
|
|
* @return mixed |
120
|
|
|
*/ |
121
|
2 |
|
public static function filterInput($key, array $request = []) |
122
|
|
|
{ |
123
|
2 |
|
if (isset($request[$key])) { |
124
|
1 |
|
return $request[$key]; |
125
|
|
|
} |
126
|
2 |
|
$variable = filter_input(INPUT_POST, $key); |
127
|
2 |
|
if (is_null($variable) && isset($_POST[$key])) { |
128
|
2 |
|
$variable = $_POST[$key]; |
129
|
|
|
} |
130
|
2 |
|
return $variable; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $key |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
2 |
|
public static function filterInputArray($key) |
138
|
|
|
{ |
139
|
2 |
|
$variable = filter_input(INPUT_POST, $key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
140
|
2 |
|
if (empty($variable) && !empty($_POST[$key]) && is_array($_POST[$key])) { |
141
|
2 |
|
$variable = $_POST[$key]; |
142
|
|
|
} |
143
|
2 |
|
return (array) $variable; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
1 |
|
public static function getIpAddress() |
150
|
|
|
{ |
151
|
1 |
|
$whitelist = []; |
152
|
1 |
|
$isUsingCloudflare = !empty(filter_input(INPUT_SERVER, 'CF-Connecting-IP')); |
153
|
1 |
|
if (apply_filters('site-reviews/whip/whitelist/cloudflare', $isUsingCloudflare)) { |
154
|
|
|
$cloudflareIps = glsr(Cache::class)->getCloudflareIps(); |
155
|
|
|
$whitelist[Whip::CLOUDFLARE_HEADERS] = [Whip::IPV4 => $cloudflareIps['v4']]; |
156
|
|
|
if (defined('AF_INET6')) { |
157
|
|
|
$whitelist[Whip::CLOUDFLARE_HEADERS][Whip::IPV6] = $cloudflareIps['v6']; |
158
|
|
|
} |
159
|
|
|
} |
160
|
1 |
|
$whitelist = apply_filters('site-reviews/whip/whitelist', $whitelist); |
161
|
1 |
|
$methods = apply_filters('site-reviews/whip/methods', Whip::ALL_METHODS); |
162
|
1 |
|
$whip = new Whip($methods, $whitelist); |
163
|
1 |
|
do_action_ref_array('site-reviews/whip', [$whip]); |
164
|
1 |
|
if (false !== ($clientAddress = $whip->getValidIpAddress())) { |
165
|
1 |
|
return (string) $clientAddress; |
166
|
|
|
} |
167
|
|
|
glsr_log()->error('Unable to detect IP address.'); |
168
|
|
|
return 'unknown'; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param mixed $value |
173
|
|
|
* @param string|int $min |
174
|
|
|
* @param string|int $max |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
public static function inRange($value, $min, $max) |
178
|
|
|
{ |
179
|
|
|
$inRange = filter_var($value, FILTER_VALIDATE_INT, ['options' => [ |
180
|
|
|
'min_range' => intval($min), |
181
|
|
|
'max_range' => intval($max), |
182
|
|
|
]]); |
183
|
|
|
return false !== $inRange; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|