-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerVariables.php
More file actions
142 lines (128 loc) · 3.35 KB
/
Copy pathServerVariables.php
File metadata and controls
142 lines (128 loc) · 3.35 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
142
<?php
namespace Cleantalk\Common\Variables;
use Cleantalk\Common\Cleaner\Sanitize;
use Cleantalk\Common\Cleaner\Validate;
use Cleantalk\Common\Templates\Singleton;
/**
* Class ServerVariables
* Safety handler for ${_SOMETHING}
*
* @usage \Cleantalk\Variables\{SOMETHING}::get( $name );
*
* @package Cleantalk\Variables
* @psalm-suppress PossiblyUnusedProperty
*/
class ServerVariables
{
use Singleton;
/**
* @var array Contains saved variables
*/
public $variables = array();
/**
* Gets variable from ${_SOMETHING}
*
* @param string $name Variable name
* @param null|string $validation_filter
* @psalm-param (null|"hash"|"int"|"float"|"word"|"isUrl") $validation_filter
* @param null|string $sanitize_filter
* @psalm-param (null|"xss"|"int"|"url"|"word"|"cleanEmail") $sanitize_filter
*
* @return string|array|false
*/
public static function get($name, $validation_filter = null, $sanitize_filter = null)
{
$variable = static::getInstance()->getVariable($name);
// Validate variable
if ( $validation_filter && ! Validate::validate($variable, $validation_filter) ) {
return false;
}
if ( $sanitize_filter ) {
$variable = Sanitize::sanitize($variable, $sanitize_filter);
}
return $variable;
}
/**
* BLUEPRINT
* Gets given ${_SOMETHING} variable and save it to memory
*
* @param $name
*
* @return mixed|string
*/
protected function getVariable($name)
{
}
/**
* Save variable to $this->variables[]
*
* @param string $name
* @param string $value
*/
protected function rememberVariable($name, $value)
{
static::$instance->variables[$name] = $value;
}
/**
* Checks if variable contains given string
*
* @param string $var Haystack to search in
* @param string $string Needle to search
*
* @return bool
*/
public static function hasString($var, $string)
{
return stripos(self::get($var), $string) !== false;
}
/**
* Checks if variable equal to $param
*
* @param string $var Variable to compare
* @param string $param Param to compare
*
* @return bool
* @psalm-suppress PossiblyUnusedMethod
*/
public static function equal($var, $param)
{
return self::get($var) === $param;
}
/**
* @param $value
* @param $nesting
*
* @return string|array
*/
public function getAndSanitize($value, $nesting = 0)
{
if ( is_array($value) ) {
foreach ( $value as $_key => & $val ) {
if ( is_array($val) ) {
if ( $nesting > 20 ) {
return $value;
}
$this->getAndSanitize($val, ++$nesting);
} else {
$val = $this->sanitizeDefault($val);
}
}
} else {
$value = $this->sanitizeDefault($value);
}
return $value;
}
/**
* Sanitize gathering data.
* No sanitizing by default.
* Override this method in the internal class!
*
* @param string $value
*
* @return string
*/
protected function sanitizeDefault($value)
{
return $value;
}
}