1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\Utils; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Utilitário de variáveis globais ($_REQUEST, $_POST, $_GET, etc) |
7
|
|
|
* |
8
|
|
|
* Fornecendo uma camada de segurança maior do que manipulá-las diretamente. |
9
|
|
|
*/ |
10
|
|
|
abstract class Input |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Retorna variável $_POST |
14
|
|
|
* @param string $name |
15
|
|
|
* @param int $filter |
16
|
|
|
* @param mixed $default |
17
|
|
|
* @return mixed |
18
|
|
|
*/ |
19
|
|
|
public static function post($name, $filter = FILTER_SANITIZE_STRING, $default = null) |
20
|
|
|
{ |
21
|
|
|
$post = filter_input(INPUT_POST, $name, $filter); |
22
|
|
|
|
23
|
|
|
return $post ?? $default; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Retorna TRUE se a variável foi definida |
28
|
|
|
* @param string $name |
29
|
|
|
* @return boolean |
30
|
|
|
*/ |
31
|
|
|
public static function isset($name) |
32
|
|
|
{ |
33
|
|
|
return isset($_POST[$name]); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Retorna variável $_POST em modo array |
38
|
|
|
* @param string $name |
39
|
|
|
* @param int $filter |
40
|
|
|
* @return mixed[] |
41
|
|
|
*/ |
42
|
|
|
public static function postArray($name = null, $filter = FILTER_SANITIZE_STRING) |
43
|
|
|
{ |
44
|
|
|
return (array) filter_input(INPUT_POST, $name, $filter, FILTER_REQUIRE_ARRAY); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Retorna variável $_SERVER |
49
|
|
|
* @param string $name |
50
|
|
|
* @param int $filter |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
|
|
public static function server($name, $filter = FILTER_DEFAULT) |
54
|
|
|
{ |
55
|
|
|
$server = (key_exists($name, $_SERVER)) ? $_SERVER[$name] : ''; |
56
|
|
|
|
57
|
|
|
return filter_var($server, $filter); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Retorna variável $_GET |
62
|
|
|
* @param string $name |
63
|
|
|
* @param int $filter |
64
|
|
|
* @param mixed $default |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
|
|
public static function get($name, $filter = FILTER_SANITIZE_STRING, $default = null) |
68
|
|
|
{ |
69
|
|
|
$get = filter_input(INPUT_GET, $name, $filter); |
70
|
|
|
|
71
|
|
|
return $get ?? $default; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Retorna variável $_FILES |
76
|
|
|
* @param string $name |
77
|
|
|
* @return string[] |
78
|
|
|
*/ |
79
|
|
|
public static function file($name) |
80
|
|
|
{ |
81
|
|
|
return $_FILES[$name] ?? null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Retorna o protocolo atual |
86
|
|
|
* @return string 'http'|'https' |
87
|
|
|
*/ |
88
|
|
|
public static function protocol() |
89
|
|
|
{ |
90
|
|
|
$https = Input::server('HTTPS'); |
91
|
|
|
$port = Input::server('SERVER_PORT'); |
92
|
|
|
if (!empty($https) && ('off' !== $https || 443 == $port)) { |
93
|
|
|
return 'https'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return 'http'; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|