Bearsampp 2026.5.5
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().

◆ 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 return filter_var($value, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
68 } elseif ($type == 'numeric') {
69 return (isset($_GET[$name]) && is_numeric($_GET[$name])) ? intval($_GET[$name]) : '';
70 } elseif ($type == 'boolean') {
71 return (isset($_GET[$name])) ? true : false;
72 } elseif ($type == 'array') {
73 return (isset($_GET[$name]) && is_array($_GET[$name])) ? $_GET[$name] : array();
74 }
75 }
76
77 return false;
78 }

Referenced by Homepage\__construct().

◆ 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 88 of file class.util.input.php.

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

◆ 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 235 of file class.util.input.php.

236 {
237 if (!is_string($output)) {
238 return '';
239 }
240
241 $output = str_replace("\0", '', $output);
242
243 return htmlspecialchars($output, ENT_QUOTES | ENT_HTML5, 'UTF-8');
244 }

◆ 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 205 of file class.util.input.php.

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

References Log\warning().

◆ 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 121 of file class.util.input.php.

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

References Log\warning().

Referenced by Batch\findExeByPid().

◆ 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 148 of file class.util.input.php.

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

References $port, and Log\warning().

Referenced by Batch\getProcessUsingPort().

◆ 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 175 of file class.util.input.php.

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

References Log\warning().

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


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