Bearsampp 2026.5.5
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 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 }
79
88 public static function cleanPostVar($name, $type = 'text')
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 }
112
121 public static function sanitizePID($pid)
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 }
139
148 public static function sanitizePort($port)
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 }
166
175 public static function sanitizeServiceName($serviceName)
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 }
196
205 public static function sanitizePath($path)
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 }
226
235 public static function sanitizeOutput($output)
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 }
245}
$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')