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
|
|
|
if ('no' === $value) { |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
return (bool) $value; |
63
|
|
|
case 'integer': |
64
|
|
|
if (is_numeric($value) || is_string($value)) { |
65
|
|
|
return (int) $value; |
66
|
|
|
} |
67
|
|
|
// no break |
68
|
|
|
case 'object': |
69
|
|
|
return (object) (array) $value; |
70
|
|
|
case 'string': |
71
|
|
|
if (!is_array($value) && !is_object($value)) { |
72
|
|
|
return (string) $value; |
73
|
|
|
} |
74
|
|
|
// no break |
75
|
|
|
default: |
76
|
7 |
|
return $value; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $key |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
2 |
|
public function filterInput($key, array $request = []) |
85
|
|
|
{ |
86
|
2 |
|
if (isset($request[$key])) { |
87
|
1 |
|
return $request[$key]; |
88
|
|
|
} |
89
|
2 |
|
$variable = filter_input(INPUT_POST, $key); |
90
|
2 |
|
if (is_null($variable) && isset($_POST[$key])) { |
91
|
2 |
|
$variable = $_POST[$key]; |
92
|
|
|
} |
93
|
2 |
|
return $variable; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $key |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
2 |
|
public function filterInputArray($key) |
101
|
|
|
{ |
102
|
2 |
|
$variable = filter_input(INPUT_POST, $key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
103
|
2 |
|
if (empty($variable) && !empty($_POST[$key]) && is_array($_POST[$key])) { |
104
|
2 |
|
$variable = $_POST[$key]; |
105
|
|
|
} |
106
|
2 |
|
return (array) $variable; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
7 |
|
public function getIpAddress() |
113
|
|
|
{ |
114
|
7 |
|
$cloudflareIps = glsr(Cache::class)->getCloudflareIps(); |
115
|
7 |
|
$ipv6 = defined('AF_INET6') |
116
|
7 |
|
? $cloudflareIps['v6'] |
117
|
7 |
|
: []; |
118
|
7 |
|
$whitelist = apply_filters('site-reviews/whip/whitelist', [ |
119
|
7 |
|
Whip::CLOUDFLARE_HEADERS => [ |
120
|
7 |
|
Whip::IPV4 => $cloudflareIps['v4'], |
121
|
7 |
|
Whip::IPV6 => $ipv6, |
122
|
|
|
], |
123
|
7 |
|
Whip::CUSTOM_HEADERS => [ |
124
|
7 |
|
Whip::IPV4 => ['127.0.0.1'], |
125
|
7 |
|
Whip::IPV6 => ['::1'], |
126
|
|
|
], |
127
|
|
|
]); |
128
|
7 |
|
$methods = Whip::CUSTOM_HEADERS | Whip::CLOUDFLARE_HEADERS | Whip::REMOTE_ADDR; |
129
|
7 |
|
$methods = apply_filters('site-reviews/whip/methods', $methods); |
130
|
7 |
|
$whip = new Whip($methods, $whitelist); |
131
|
7 |
|
do_action_ref_array('site-reviews/whip', [$whip]); |
132
|
7 |
|
return (string) $whip->getValidIpAddress(); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|