-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.php
More file actions
141 lines (124 loc) · 3.23 KB
/
Copy pathServer.php
File metadata and controls
141 lines (124 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace Cleantalk\Common\Variables;
/**
* Class Server
* Wrapper to safely get $_SERVER variables
*
* @usage \CleantalkSP\Variables\Server::get( $name );
*
* @package \CleantalkSP\Variables
*/
class Server extends ServerVariables
{
protected static $instance;
/**
* Gets given $_SERVER variable and save it to memory
*
* @param string $name
*
* @return mixed|string
*/
protected function getVariable($name)
{
$name = strtoupper($name);
// Return from memory. From $this->server
if (isset(static::$instance->variables[$name])) {
return static::$instance->variables[$name];
}
if ( isset($_SERVER[$name]) ) {
$value = $this->getAndSanitize($_SERVER[$name]);
} else {
$value = '';
}
// Convert to upper case for REQUEST_METHOD
if ($name === 'REQUEST_METHOD') {
$value = strtoupper($value);
}
// Convert HTML chars for HTTP_USER_AGENT, SERVER_NAME
if (in_array($name, array('HTTP_USER_AGENT', 'SERVER_NAME'))) {
$value = htmlspecialchars($value);
}
// Remember for further calls
static::getInstance()->rememberVariable($name, $value);
return $value;
}
/**
* Checks if $_SERVER['REQUEST_URI'] contains string
*
* @param string $needle
*
* @return bool
* @psalm-suppress PossiblyUnusedMethod
*/
public static function inUri($needle)
{
return self::hasString('REQUEST_URI', $needle);
}
/**
* Is the host contains the string
*
* @param string $needle
*
* @return bool
* @psalm-suppress PossiblyUnusedMethod
*/
public static function inHost($needle)
{
return self::hasString('HTTP_HOST', $needle);
}
/**
* Getting domain name
*
* @return false|string
* @psalm-suppress PossiblyUnusedMethod
*/
public static function getDomain()
{
preg_match('@\S+\.(\S+)\/?$@', self::get('HTTP_HOST'), $matches);
return isset($matches[1]) ? $matches[1] : false;
}
/**
* Checks if $_SERVER['REQUEST_URI'] contains string
*
* @param string $needle needle
*
* @return bool
* @psalm-suppress PossiblyUnusedMethod
*/
public static function inReferer($needle)
{
return self::hasString('HTTP_REFERER', $needle);
}
/**
* Checks if the current request method is POST
*
* @return bool
* @psalm-suppress PossiblyUnusedMethod
*/
public static function isPost()
{
return self::get('REQUEST_METHOD') === 'POST';
}
/**
* Checks if the current request method is GET
*
* @return bool
* @psalm-suppress PossiblyUnusedMethod
*/
public static function isGet()
{
return self::get('REQUEST_METHOD') === 'GET';
}
/**
* Determines if SSL is used.
*
* @return bool True if SSL, otherwise false.
* @psalm-suppress PossiblyUnusedMethod
*/
public static function isSSL()
{
return self::get('HTTPS') === 'on' ||
self::get('HTTPS') === '1' ||
self::get('SERVER_PORT') == '443';
}
}