Cache
1 day ago
DBPermissions.php
1 day ago
DataEncoder.php
1 day ago
DatabaseOptions.php
1 day ago
Env.php
1 day ago
Escape.php
1 day ago
Glob.php
1 day ago
Hooks.php
1 day ago
Math.php
1 day ago
PluginInfo.php
1 day ago
Sanitize.php
1 day ago
ServerVars.php
1 day ago
SlashMode.php
1 day ago
Strings.php
1 day ago
Times.php
1 day ago
Urls.php
1 day ago
Version.php
1 day ago
WpDefaultDirectories.php
1 day ago
Sanitize.php
410 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Utils; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | class Sanitize |
| 15 | { |
| 16 | protected $config = []; |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | public function sanitizeString($value) |
| 34 | { |
| 35 | if (is_array($value) || is_object($value)) { |
| 36 | return ''; |
| 37 | } |
| 38 | |
| 39 | return trim(htmlspecialchars($value)); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | public function sanitizePassword($password) |
| 47 | { |
| 48 | if (!is_string($password)) { |
| 49 | return ''; |
| 50 | } |
| 51 | |
| 52 | return trim(stripslashes($password)); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | public function sanitizeCredentialContent($value) |
| 64 | { |
| 65 | if (!is_string($value) || $value === '') { |
| 66 | return ''; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | $value = str_replace("\0", '', $value); |
| 71 | |
| 72 | |
| 73 | $value = preg_replace('/[\x01-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', $value); |
| 74 | |
| 75 | |
| 76 | $value = str_replace(["\r\n", "\r"], "\n", $value); |
| 77 | |
| 78 | return trim($value); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | public function sanitizeRemotePath($path) |
| 89 | { |
| 90 | if (!is_string($path)) { |
| 91 | return ''; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | $path = str_replace('\\', '/', $path); |
| 96 | |
| 97 | $isAbsolute = strpos($path, '/') === 0; |
| 98 | |
| 99 | $parts = explode('/', $path); |
| 100 | $resolved = []; |
| 101 | |
| 102 | foreach ($parts as $part) { |
| 103 | if ($part === '' || $part === '.') { |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | if ($part === '..') { |
| 108 | if (!empty($resolved)) { |
| 109 | array_pop($resolved); |
| 110 | } |
| 111 | |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | $resolved[] = $part; |
| 116 | } |
| 117 | |
| 118 | $sanitized = implode('/', $resolved); |
| 119 | return $isAbsolute ? '/' . $sanitized : $sanitized; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | public function sanitizeInt($value, $useAbsValue = false) |
| 130 | { |
| 131 | $integer = filter_var($value, FILTER_VALIDATE_INT); |
| 132 | if ($useAbsValue) { |
| 133 | return absint($integer); |
| 134 | } |
| 135 | |
| 136 | return $integer; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | public function sanitizeBool($value) |
| 144 | { |
| 145 | |
| 146 | |
| 147 | return filter_var($value, defined('FILTER_VALIDATE_BOOL') ? FILTER_VALIDATE_BOOL : FILTER_VALIDATE_BOOLEAN); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | public function sanitizeEmail($value): string |
| 155 | { |
| 156 | return filter_var($value, FILTER_VALIDATE_EMAIL) ? $value : ''; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | public function sanitizePath($value) |
| 166 | { |
| 167 | if (is_array($value) || is_object($value)) { |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | $value = $this->sanitizeString($value); |
| 172 | |
| 173 | |
| 174 | $path = rtrim($value, '/\\'); |
| 175 | |
| 176 | |
| 177 | if (WPStaging::isWindowsOs()) { |
| 178 | return $path; |
| 179 | } |
| 180 | |
| 181 | |
| 182 | if (!WPStaging::isMacOs()) { |
| 183 | $path = preg_replace('/\s+/', '', $path); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | $replacements = [ |
| 188 | '//' => '/', |
| 189 | ]; |
| 190 | |
| 191 | return strtr($path, $replacements); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | public function htmlDecodeAndSanitize($text) |
| 201 | { |
| 202 | return sanitize_text_field(html_entity_decode($text)); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | public function sanitizeFileUpload($file) |
| 210 | { |
| 211 | if (!is_array($file)) { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | if (!isset($file['tmp_name'])) { |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | return $file; |
| 220 | } |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | public function sanitizeExcludeRules($htmlPost) |
| 227 | { |
| 228 | if (is_object($htmlPost)) { |
| 229 | return []; |
| 230 | } |
| 231 | |
| 232 | $decoded = wpstg_urldecode($htmlPost); |
| 233 | |
| 234 | if (!is_array($decoded)) { |
| 235 | $items = explode(',', $decoded); |
| 236 | } else { |
| 237 | $items = $decoded; |
| 238 | } |
| 239 | |
| 240 | $sanitized = []; |
| 241 | foreach ($items as $item) { |
| 242 | $sanitized[] = $this->sanitizeString($item); |
| 243 | } |
| 244 | |
| 245 | return $sanitized; |
| 246 | } |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | public function sanitizeArrayInt($items) |
| 253 | { |
| 254 | |
| 255 | if (!is_array($items) || empty($items)) { |
| 256 | return []; |
| 257 | } |
| 258 | |
| 259 | $sanitized = []; |
| 260 | foreach ($items as $item) { |
| 261 | $sanitized[] = $this->sanitizeInt($item); |
| 262 | } |
| 263 | |
| 264 | return $sanitized; |
| 265 | } |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | public function sanitizeArrayString($items) |
| 281 | { |
| 282 | if (!is_array($items) || empty($items)) { |
| 283 | return []; |
| 284 | } |
| 285 | |
| 286 | $sanitized = []; |
| 287 | foreach ($items as $key => $item) { |
| 288 | if (is_array($item)) { |
| 289 | $sanitized[$key] = $this->sanitizeArrayString($item); |
| 290 | } else { |
| 291 | $sanitized[$key] = $this->sanitizeString($item); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | return $sanitized; |
| 296 | } |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | public function sanitizeArray($items, $config = []) |
| 304 | { |
| 305 | |
| 306 | if (!is_array($items) || empty($items)) { |
| 307 | return []; |
| 308 | } |
| 309 | |
| 310 | $sanitized = []; |
| 311 | if (!is_array($config) || empty($config)) { |
| 312 | $config = $this->config; |
| 313 | } else { |
| 314 | $this->config = $config; |
| 315 | } |
| 316 | |
| 317 | foreach ($items as $key => $value) { |
| 318 | $sanitized[$key] = isset($config[$key]) ? |
| 319 | $this->sanitizeCall($value, $config[$key]) : |
| 320 | (is_array($value) ? $this->sanitizeArrayString($value) : $this->sanitizeString($value)); |
| 321 | } |
| 322 | |
| 323 | return $sanitized; |
| 324 | } |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | public function decodeBase64AndSanitize($text) |
| 331 | { |
| 332 | return $this->sanitizeString(base64_decode($text)); |
| 333 | } |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | protected function sanitizeCall($value, $method) |
| 341 | { |
| 342 | $methodName = 'sanitize' . ucfirst($method); |
| 343 | if (!method_exists($this, $methodName)) { |
| 344 | return $this->sanitizeString($value); |
| 345 | } |
| 346 | |
| 347 | return $this->{$methodName}($value); |
| 348 | } |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | public function sanitizeUrl($url, $protocols = null) |
| 358 | { |
| 359 | return esc_url($url, $protocols, 'db'); |
| 360 | } |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | public function sanitizeDomainForCli($domain) |
| 370 | { |
| 371 | if (!is_string($domain)) { |
| 372 | return ''; |
| 373 | } |
| 374 | |
| 375 | return preg_replace('/[^a-zA-Z0-9.\-]/', '', $domain); |
| 376 | } |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | public function sanitizeTablePrefixForCli($prefix) |
| 386 | { |
| 387 | if (!is_string($prefix)) { |
| 388 | return ''; |
| 389 | } |
| 390 | |
| 391 | return preg_replace('/[^a-zA-Z0-9_]/', '', $prefix); |
| 392 | } |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | public function sanitizeLicenseKeyForCli($licenseKey) |
| 402 | { |
| 403 | if (!is_string($licenseKey)) { |
| 404 | return ''; |
| 405 | } |
| 406 | |
| 407 | return preg_replace('/[^a-zA-Z0-9\-]/', '', $licenseKey); |
| 408 | } |
| 409 | } |
| 410 |