Bearsampp 2026.9.23
Loading...
Searching...
No Matches
class.util.input.php
Go to the documentation of this file.
1<?php
2/*
3 *
4 * * Copyright (c) 2022-2025 Bearsampp
5 * * License: GNU General Public License version 3 or later; see LICENSE.txt
6 * * Website: https://bearsampp.com
7 * * Github: https://github.com/Bearsampp
8 *
9 */
10
25{
34 public static function cleanArgv($name, $type = 'text')
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 }
50
59 public static function cleanGetVar($name, $type = 'text')
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 }
80
89 public static function cleanPostVar($name, $type = 'text')
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 }
114
123 public static function sanitizePID($pid)
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 }
143
152 public static function sanitizePort($port)
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 }
172
181 public static function sanitizeServiceName($serviceName)
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 }
204
213 public static function sanitizePath($path)
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 }
235
244 public static function sanitizeOutput($output)
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 }
254}
255
$port
static warning($data, $file=null)
static sanitizeServiceName($serviceName)
static cleanArgv($name, $type='text')
static sanitizePID($pid)
static cleanGetVar($name, $type='text')
static sanitizeOutput($output)
static sanitizePort($port)
static sanitizePath($path)
static cleanPostVar($name, $type='text')