1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database\Cache; |
6
|
|
|
use GeminiLabs\SiteReviews\HelperTraits\Arr; |
7
|
|
|
use GeminiLabs\SiteReviews\HelperTraits\Str; |
8
|
|
|
use GeminiLabs\Vectorface\Whip\Whip; |
9
|
|
|
|
10
|
|
|
class Helper |
11
|
|
|
{ |
12
|
|
|
use Arr; |
13
|
|
|
use Str; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param string $name |
17
|
|
|
* @param string $path |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
4 |
|
public function buildClassName($name, $path = '') |
21
|
|
|
{ |
22
|
4 |
|
$className = $this->camelCase($name); |
23
|
4 |
|
$path = ltrim(str_replace(__NAMESPACE__, '', $path), '\\'); |
24
|
4 |
|
return !empty($path) |
25
|
2 |
|
? __NAMESPACE__.'\\'.$path.'\\'.$className |
26
|
4 |
|
: $className; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $name |
31
|
|
|
* @param string $prefix |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
2 |
|
public function buildMethodName($name, $prefix = '') |
35
|
|
|
{ |
36
|
2 |
|
return lcfirst($prefix.$this->buildClassName($name)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $name |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
1 |
|
public function buildPropertyName($name) |
44
|
|
|
{ |
45
|
1 |
|
return lcfirst($this->buildClassName($name)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $cast |
50
|
|
|
* @param mixed $value |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
7 |
|
public function castTo($cast = '', $value) |
54
|
|
|
{ |
55
|
7 |
|
switch ($cast) { |
56
|
|
|
case 'array': |
57
|
|
|
return (array) $value; |
58
|
|
|
case 'boolean': |
59
|
|
|
return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
60
|
|
|
case 'float': |
61
|
|
|
return (float) filter_var($value, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND); |
62
|
|
|
case 'integer': |
63
|
|
|
return (int) filter_var($value, FILTER_VALIDATE_INT); |
64
|
|
|
case 'object': |
65
|
|
|
return (object) (array) $value; |
66
|
|
|
case 'string': |
67
|
|
|
if (is_object($value) && in_array('__toString', get_class_methods($value))) { |
68
|
|
|
return (string) $value->__toString(); |
69
|
|
|
} |
70
|
|
|
if (is_array($value) || is_object($value)) { |
71
|
|
|
return serialize($value); |
72
|
|
|
} |
73
|
|
|
return (string) $value; |
74
|
|
|
default: |
75
|
7 |
|
return $value; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $key |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
2 |
|
public function filterInput($key, array $request = []) |
84
|
|
|
{ |
85
|
2 |
|
if (isset($request[$key])) { |
86
|
1 |
|
return $request[$key]; |
87
|
|
|
} |
88
|
2 |
|
$variable = filter_input(INPUT_POST, $key); |
89
|
2 |
|
if (is_null($variable) && isset($_POST[$key])) { |
90
|
2 |
|
$variable = $_POST[$key]; |
91
|
|
|
} |
92
|
2 |
|
return $variable; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $key |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
2 |
|
public function filterInputArray($key) |
100
|
|
|
{ |
101
|
2 |
|
$variable = filter_input(INPUT_POST, $key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
102
|
2 |
|
if (empty($variable) && !empty($_POST[$key]) && is_array($_POST[$key])) { |
103
|
2 |
|
$variable = $_POST[$key]; |
104
|
|
|
} |
105
|
2 |
|
return (array) $variable; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
1 |
|
public function getIpAddress() |
112
|
|
|
{ |
113
|
1 |
|
$cloudflareIps = glsr(Cache::class)->getCloudflareIps(); |
114
|
1 |
|
$ipv6 = defined('AF_INET6') |
115
|
1 |
|
? $cloudflareIps['v6'] |
116
|
1 |
|
: []; |
117
|
1 |
|
$whitelist = apply_filters('site-reviews/whip/whitelist', [ |
118
|
1 |
|
Whip::CLOUDFLARE_HEADERS => [ |
119
|
1 |
|
Whip::IPV4 => $cloudflareIps['v4'], |
120
|
1 |
|
Whip::IPV6 => $ipv6, |
121
|
|
|
], |
122
|
1 |
|
Whip::CUSTOM_HEADERS => [ |
123
|
1 |
|
Whip::IPV4 => ['127.0.0.1'], |
124
|
1 |
|
Whip::IPV6 => ['::1'], |
125
|
|
|
], |
126
|
|
|
]); |
127
|
1 |
|
$methods = Whip::CUSTOM_HEADERS | Whip::CLOUDFLARE_HEADERS | Whip::REMOTE_ADDR; |
128
|
1 |
|
$methods = apply_filters('site-reviews/whip/methods', $methods); |
129
|
1 |
|
$whip = new Whip($methods, $whitelist); |
130
|
1 |
|
do_action_ref_array('site-reviews/whip', [$whip]); |
131
|
1 |
|
return (string) $whip->getValidIpAddress(); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|