Bearsampp 2026.9.23
Loading...
Searching...
No Matches
UtilInput Class Reference

Static Public Member Functions

static cleanArgv ($name, $type='text')
static cleanGetVar ($name, $type='text')
static cleanPostVar ($name, $type='text')
static sanitizeOutput ($output)
static sanitizePath ($path)
static sanitizePID ($pid)
static sanitizePort ($port)
static sanitizeServiceName ($serviceName)

Detailed Description

Input cleaning and sanitization utilities.

Provides safe access to command-line arguments, GET, and POST variables, plus sanitizers for PIDs, ports, service names, file paths, and HTML output.

Usage:

$action = UtilInput::cleanArgv(1);
$pid = UtilInput::sanitizePID($rawPid);
static cleanArgv($name, $type='text')
static sanitizePID($pid)
static cleanGetVar($name, $type='text')

Definition at line 24 of file class.util.input.php.

Member Function Documentation

◆ cleanArgv()

cleanArgv ( $name,
$type = 'text' )
static

Cleans and returns a specific command line argument based on the type specified.

Parameters
string$nameThe index of the argument in the $_SERVER['argv'] array.
string$typeThe type of the argument to return: 'text', 'numeric', 'boolean', or 'array'.
Returns
mixed Returns the cleaned argument based on the type or false if the argument is not set.

Definition at line 34 of file class.util.input.php.

35 {
36 if (isset($_SERVER['argv'])) {
37 if ($type == 'text') {
38 return (isset($_SERVER['argv'][$name]) && !empty($_SERVER['argv'][$name])) ? trim($_SERVER['argv'][$name]) : '';
39 } elseif ($type == 'numeric') {
40 return (isset($_SERVER['argv'][$name]) && is_numeric($_SERVER['argv'][$name])) ? intval($_SERVER['argv'][$name]) : '';
41 } elseif ($type == 'boolean') {
42 return (isset($_SERVER['argv'][$name])) ? true : false;
43 } elseif ($type == 'array') {
44 return (isset($_SERVER['argv'][$name]) && is_array($_SERVER['argv'][$name])) ? $_SERVER['argv'][$name] : array();
45 }
46 }
47
48 return false;
49 }

Referenced by Action\process().

Here is the caller graph for this function:

◆ cleanGetVar()

cleanGetVar ( $name,
$type = 'text' )
static

Cleans and returns a specific $_GET variable based on the type specified.

Parameters
string$nameThe name of the $_GET variable.
string$typeThe type of the variable to return: 'text', 'numeric', 'boolean', or 'array'.
Returns
mixed Returns the cleaned $_GET variable based on the type or false if the variable is not set.

Definition at line 59 of file class.util.input.php.

60 {
61 if (is_string($name)) {
62 if ($type == 'text') {
63 $value = (isset($_GET[$name]) && $_GET[$name] !== '') ? (string)$_GET[$name] : '';
64 $value = str_replace("\0", '', $value);
65 $value = preg_replace('/[\x00-\x1F\x7F]/u', '', $value);
66 $value = trim($value);
67
68 return filter_var($value, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
69 } elseif ($type == 'numeric') {
70 return (isset($_GET[$name]) && is_numeric($_GET[$name])) ? intval($_GET[$name]) : '';
71 } elseif ($type == 'boolean') {
72 return (isset($_GET[$name])) ? true : false;
73 } elseif ($type == 'array') {
74 return (isset($_GET[$name]) && is_array($_GET[$name])) ? $_GET[$name] : array();
75 }
76 }
77
78 return false;
79 }

Referenced by Homepage\__construct().

Here is the caller graph for this function:

◆ cleanPostVar()

cleanPostVar ( $name,
$type = 'text' )
static

Cleans and returns a specific $_POST variable based on the type specified.

Parameters
string$nameThe name of the $_POST variable.
string$typeThe type of the variable to return: 'text', 'number', 'float', 'boolean', 'array', or 'content'.
Returns
mixed Returns the cleaned $_POST variable based on the type or false if the variable is not set.

Definition at line 89 of file class.util.input.php.

90 {
91 if (is_string($name)) {
92 if ($type == 'text') {
93 $value = (isset($_POST[$name]) && $_POST[$name] !== '') ? (string)$_POST[$name] : '';
94 $value = str_replace("\0", '', $value);
95 $value = preg_replace('/[\x00-\x1F\x7F]/u', '', $value);
96 $value = trim($value);
97
98 return filter_var($value, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
99 } elseif ($type == 'number') {
100 return (isset($_POST[$name]) && is_numeric($_POST[$name])) ? intval($_POST[$name]) : '';
101 } elseif ($type == 'float') {
102 return (isset($_POST[$name]) && is_numeric($_POST[$name])) ? floatval($_POST[$name]) : '';
103 } elseif ($type == 'boolean') {
104 return (isset($_POST[$name])) ? true : false;
105 } elseif ($type == 'array') {
106 return (isset($_POST[$name]) && is_array($_POST[$name])) ? $_POST[$name] : array();
107 } elseif ($type == 'content') {
108 return (isset($_POST[$name]) && !empty($_POST[$name])) ? trim($_POST[$name]) : '';
109 }
110 }
111
112 return false;
113 }

◆ sanitizeOutput()

sanitizeOutput ( $output)
static

Sanitizes output for display to prevent XSS attacks. Escapes HTML special characters.

Parameters
string$outputThe output to sanitize.
Returns
string Returns the sanitized output safe for HTML display.

Definition at line 244 of file class.util.input.php.

245 {
246 if (!is_string($output)) {
247 return '';
248 }
249
250 $output = str_replace("\0", '', $output);
251
252 return htmlspecialchars($output, ENT_QUOTES | ENT_HTML5, 'UTF-8');
253 }

◆ sanitizePath()

sanitizePath ( $path)
static

Sanitizes a file path by removing null bytes and checking for path traversal attempts. This is a basic sanitization — paths should still be validated before use.

Parameters
string$pathThe path to sanitize.
Returns
string|false Returns the sanitized path, or false if dangerous patterns detected.

Definition at line 213 of file class.util.input.php.

214 {
215 if (!is_string($path) || empty($path)) {
216 return false;
217 }
218
219 $sanitized = str_replace("\0", '', $path);
220
221 // Check for path traversal attempts (but allow environment variables)
222 $pathWithoutEnvVars = preg_replace('/%[^%]+%/', '', $sanitized);
223 if (strpos($pathWithoutEnvVars, '..') !== false) {
224 Log::warning('Path traversal attempt detected: ' . $path);
225
226 return false;
227 }
228
229 // Remove dangerous characters — preserve : for drive letters and ; for PATH
230 // Also strip common cmd.exe metacharacters to reduce command-injection risk when paths are interpolated.
231 $sanitized = preg_replace('/[<>"|?*&^`\x00-\x1F]/', '', $sanitized);
232
233 return $sanitized;
234 }
static warning($data, $file=null)

References Log\warning().

Here is the call graph for this function:

◆ sanitizePID()

sanitizePID ( $pid)
static

Sanitizes a process ID (PID) by removing all non-numeric characters. This prevents command injection through PID parameters.

Parameters
mixed$pidThe PID to sanitize.
Returns
int|false Returns the sanitized PID as integer, or false if invalid.

Definition at line 123 of file class.util.input.php.

124 {
125 $sanitized = preg_replace('/[^0-9]/', '', (string)$pid);
126
127 if (empty($sanitized)) {
128 Log::warning('Invalid PID provided: ' . var_export($pid, true));
129
130 return false;
131 }
132
133 $pidInt = (int)$sanitized;
134
135 if ($pidInt <= 0 || $pidInt > 2147483647) {
136 Log::warning('PID out of valid range: ' . $pidInt);
137
138 return false;
139 }
140
141 return $pidInt;
142 }

References Log\warning().

Referenced by Batch\findExeByPid().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sanitizePort()

sanitizePort ( $port)
static

Sanitizes a port number by ensuring it's a valid integer in the correct range. This prevents command injection through port parameters.

Parameters
mixed$portThe port to sanitize.
Returns
int|false Returns the sanitized port as integer, or false if invalid.

Definition at line 152 of file class.util.input.php.

153 {
154 $portStr = trim((string)$port);
155
156 if ($portStr === '' || !preg_match('/^\d+$/', $portStr)) {
157 Log::warning('Invalid port provided: ' . var_export($port, true));
158
159 return false;
160 }
161
162 $portInt = (int)$portStr;
163
164 if ($portInt < 1 || $portInt > 65535) {
165 Log::warning('Port out of valid range: ' . $portInt);
166
167 return false;
168 }
169
170 return $portInt;
171 }
$port

References $port, and Log\warning().

Referenced by Batch\getProcessUsingPort().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sanitizeServiceName()

sanitizeServiceName ( $serviceName)
static

Sanitizes a service name by removing dangerous characters. Allows only alphanumeric characters, underscores, and hyphens.

Parameters
string$serviceNameThe service name to sanitize.
Returns
string|false Returns the sanitized service name, or false if invalid.

Definition at line 181 of file class.util.input.php.

182 {
183 if (!is_string($serviceName) || empty($serviceName)) {
184 Log::warning('Invalid service name: not a string or empty');
185
186 return false;
187 }
188
189 $sanitized = preg_replace('/[^a-zA-Z0-9_-]/', '', $serviceName);
190
191 if (empty($sanitized)) {
192 Log::warning('Service name became empty after sanitization: ' . $serviceName);
193
194 return false;
195 }
196
197 // Limit length to 256 characters (Windows service name limit)
198 if (strlen($sanitized) > 256) {
199 $sanitized = substr($sanitized, 0, 256);
200 }
201
202 return $sanitized;
203 }

References Log\warning().

Referenced by Batch\setServiceDescription(), Batch\setServiceDisplayName(), and Batch\setServiceStartType().

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file: