99 lines
2.9 KiB
PHP
99 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace WizdomNetworks\WizeWeb\Utilities;
|
|
|
|
use WizdomNetworks\WizeWeb\Utilities\Logger;
|
|
use WizdomNetworks\WizeWeb\Utilities\ErrorHandler;
|
|
|
|
/**
|
|
* Sanitizer Utility
|
|
*
|
|
* Provides secure, traceable input sanitation with modern and recursive handling.
|
|
*/
|
|
class Sanitizer
|
|
{
|
|
/**
|
|
* Sanitizes a string using modern techniques.
|
|
*/
|
|
public static function sanitizeString(string $value): string
|
|
{
|
|
return self::sanitizeInput($value); // alias to avoid deprecated filters
|
|
}
|
|
|
|
/**
|
|
* Performs chained sanitation: trim, strip_tags, htmlspecialchars.
|
|
*/
|
|
public static function sanitizeInput(string $value): string
|
|
{
|
|
try {
|
|
$sanitized = htmlspecialchars(strip_tags(trim($value)));
|
|
Logger::info("Sanitized input: Original: $value | Sanitized: $sanitized");
|
|
return $sanitized;
|
|
} catch (\Throwable $e) {
|
|
Logger::error("Failed to sanitize input: $value");
|
|
ErrorHandler::exception($e);
|
|
return '';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Alias to sanitizeInput() for semantic clarity.
|
|
*/
|
|
public static function sanitizeChained(string $value): string
|
|
{
|
|
return self::sanitizeInput($value);
|
|
}
|
|
|
|
/**
|
|
* Sanitizes an email address.
|
|
*/
|
|
public static function sanitizeEmail(string $value): string
|
|
{
|
|
try {
|
|
$sanitized = filter_var($value, FILTER_SANITIZE_EMAIL);
|
|
Logger::info("Sanitized email: Original: $value | Sanitized: $sanitized");
|
|
return $sanitized;
|
|
} catch (\Throwable $e) {
|
|
Logger::error("Failed to sanitize email: $value");
|
|
ErrorHandler::exception($e);
|
|
return '';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sanitizes a URL.
|
|
*/
|
|
public static function sanitizeURL(string $value): string
|
|
{
|
|
try {
|
|
$sanitized = filter_var($value, FILTER_SANITIZE_URL);
|
|
Logger::info("Sanitized URL: Original: $value | Sanitized: $sanitized");
|
|
return $sanitized;
|
|
} catch (\Throwable $e) {
|
|
Logger::error("Failed to sanitize URL: $value");
|
|
ErrorHandler::exception($e);
|
|
return '';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Recursively sanitizes a nested array using sanitizeInput.
|
|
*/
|
|
public static function sanitizeArray(array $values): array
|
|
{
|
|
try {
|
|
$sanitizedArray = array_map(function ($item) {
|
|
return is_array($item)
|
|
? self::sanitizeArray($item)
|
|
: self::sanitizeInput((string)$item);
|
|
}, $values);
|
|
Logger::info("Sanitized array: Original: " . json_encode($values) . " | Sanitized: " . json_encode($sanitizedArray));
|
|
return $sanitizedArray;
|
|
} catch (\Throwable $e) {
|
|
Logger::error("Failed to sanitize array: " . json_encode($values));
|
|
ErrorHandler::exception($e);
|
|
return [];
|
|
}
|
|
}
|
|
}
|