1
|
|
|
<?php |
2
|
|
|
namespace BretRZaun\StatusPage\Check; |
3
|
|
|
|
4
|
|
|
use BretRZaun\StatusPage\Result; |
5
|
|
|
|
6
|
|
|
class PhpIniCheck extends AbstractCheck |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
const TypeBoolean = 'boolean'; |
10
|
|
|
const TypeMemory = 'memory'; |
11
|
|
|
const TypeNumber = 'number'; |
12
|
|
|
const TypeRegex = 'regex'; |
13
|
|
|
const TypeString = 'string'; |
14
|
|
|
|
15
|
|
|
protected $varName; |
16
|
|
|
protected $varType; |
17
|
|
|
protected $varValue; |
18
|
|
|
protected $maxValue; |
19
|
|
|
protected $iniValue; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* PhpIniCheck constructor. |
23
|
|
|
* |
24
|
|
|
* @param string $label |
25
|
|
|
* @param string $varName <b>Name</b> of ini value - used for ini_get(...) |
26
|
|
|
* @param string $varType <b>Type</b> of ini value - see class constant |
27
|
|
|
* @param mixed $varValue <b>Value</b> which is expected |
28
|
|
|
* @param null $maxValue <b>maximum</b> value is optional used for numbers |
29
|
|
|
* |
30
|
|
|
* @return PhpIniCheck |
|
|
|
|
31
|
|
|
*/ |
32
|
|
|
public function __construct(string $label, string $varName, string $varType, $varValue, $maxValue=null) |
33
|
|
|
{ |
34
|
|
|
parent::__construct($label); |
35
|
|
|
$this->varName = $varName; |
36
|
|
|
$this->varType = $varType; |
37
|
|
|
$this->varValue = $varValue; |
38
|
|
|
$this->maxValue = $maxValue; |
39
|
|
|
$this->iniValue = ini_get($varName); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $size |
44
|
|
|
* @return int|bool(false) |
|
|
|
|
45
|
|
|
*/ |
46
|
|
|
protected function stringToMegabyte(string $size) |
47
|
|
|
{ |
48
|
|
|
$value = preg_replace('~[^0-9]*~','', $size); |
49
|
|
|
switch (substr(strtolower($size), -1)) { |
50
|
|
|
case 'm': |
51
|
|
|
return (int)$value; |
52
|
|
|
case 'k': |
53
|
|
|
return round((int)$value / 1024); |
54
|
|
|
case 'g': |
55
|
|
|
return ((int)$value * 1024); |
56
|
|
|
default: |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return Result |
64
|
|
|
*/ |
65
|
|
|
public function checkStatus(): Result |
66
|
|
|
{ |
67
|
|
|
switch ($this->varType) { |
68
|
|
|
case self::TypeBoolean: |
69
|
|
|
return $this->checkBoolean(); |
70
|
|
|
case self::TypeMemory: |
71
|
|
|
return $this->checkMemory(); |
72
|
|
|
case self::TypeNumber: |
73
|
|
|
return $this->checkNumber(); |
74
|
|
|
case self::TypeRegex: |
75
|
|
|
return $this->checkRegex(); |
76
|
|
|
case self::TypeString: |
77
|
|
|
return $this->checkString(); |
78
|
|
|
default: |
79
|
|
|
$result = new Result($this->label); |
80
|
|
|
$result->setSuccess(false); |
81
|
|
|
$result->setError("Invalid Type: " . $this->varType); |
82
|
|
|
return $result; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return Result |
88
|
|
|
*/ |
89
|
|
|
protected function checkBoolean() |
90
|
|
|
{ |
91
|
|
|
$result = new Result($this->label); |
92
|
|
|
// some boolval advance |
93
|
|
|
switch (strtolower($this->iniValue)) { |
94
|
|
|
case 'on': |
95
|
|
|
$this->iniValue = true; |
96
|
|
|
break; |
97
|
|
|
case 'off': |
98
|
|
|
$this->iniValue = false; |
99
|
|
|
break; |
100
|
|
|
case 'yes': |
101
|
|
|
$this->iniValue = true; |
102
|
|
|
break; |
103
|
|
|
case 'no': |
104
|
|
|
$this->iniValue = false; |
105
|
|
|
break; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$result->setSuccess( boolval($this->iniValue) === boolval($this->varValue) ); |
109
|
|
|
$result->setError("php.ini value of '".$this->varName."' is set to '".strval(boolval($this->iniValue))."' instead of expected '".strval(boolval($this->varValue))."'"); |
110
|
|
|
return $result; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return Result |
115
|
|
|
*/ |
116
|
|
|
protected function checkMemory() |
117
|
|
|
{ |
118
|
|
|
$result = new Result($this->label); |
119
|
|
|
if ($this->iniValue != -1) { |
120
|
|
|
$value = $this->stringToMegabyte($this->iniValue); |
|
|
|
|
121
|
|
|
$result->setSuccess( $value >= $this->varValue ); |
122
|
|
|
$result->setError("php.ini value of '".$this->varName."' is set to '".strval($this->iniValue)."', minimum expected is '".strval($this->varValue)."'"); |
123
|
|
|
} |
124
|
|
|
return $result; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return Result |
129
|
|
|
*/ |
130
|
|
|
protected function checkNumber() |
131
|
|
|
{ |
132
|
|
|
$result = new Result($this->label); |
133
|
|
View Code Duplication |
if (!is_null($this->varValue)) { |
|
|
|
|
134
|
|
|
if ($this->iniValue < $this->varValue) { |
135
|
|
|
$result->setSuccess(false); |
136
|
|
|
$result->setError("php.ini value of '".$this->varName."' is set to '".strval($this->iniValue)."', minimum expected is '".strval($this->varValue)."'"); |
137
|
|
|
return $result; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
View Code Duplication |
if (!is_null($this->maxValue)) { |
|
|
|
|
141
|
|
|
if ($this->iniValue > $this->maxValue) { |
142
|
|
|
$result->setSuccess(false); |
143
|
|
|
$result->setError("php.ini value of '".$this->varName."' is set to '".strval($this->iniValue)."', maximum expected is '".strval($this->maxValue)."'"); |
144
|
|
|
return $result; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
$result->setError("php.ini value of '".$this->varName."' is set to '".strval($this->iniValue)); |
148
|
|
|
return $result; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return Result |
153
|
|
|
*/ |
154
|
|
|
protected function checkRegex() |
155
|
|
|
{ |
156
|
|
|
$result = new Result($this->label); |
157
|
|
|
$result->setSuccess(preg_match('~'.$this->varValue.'~', $this->iniValue)); |
158
|
|
|
$result->setError("php.ini value of '".$this->varName."' is set to '".strval($this->iniValue)."', expected is '".strval($this->varValue)."'"); |
159
|
|
|
return $result; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return Result |
164
|
|
|
*/ |
165
|
|
|
protected function checkString() |
166
|
|
|
{ |
167
|
|
|
$result = new Result($this->label); |
168
|
|
|
$result->setSuccess( strval($this->iniValue) == strval($this->varValue) ); |
169
|
|
|
$result->setError("php.ini value of '".$this->varName."' is set to '".strval($this->iniValue)."', expected is '".strval($this->varValue)."'"); |
170
|
|
|
return $result; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.