87 lines
2.5 KiB
PHP
87 lines
2.5 KiB
PHP
<?php
|
|
/**
|
|
* ============================================
|
|
* File: Sanitizer.php
|
|
* Path: /app/Utils/
|
|
* Purpose: Utility class for sanitizing all user input securely
|
|
* Version: 1.1
|
|
* Author: Wizdom Networks (merged from HelpDesk+ and WizdomWeb)
|
|
* Usage: Called wherever input data needs cleaning before use or DB insertion
|
|
* ============================================
|
|
*/
|
|
|
|
namespace WizdomNetworks\WizeWeb\Utilities;
|
|
|
|
use WizdomNetworks\WizeWeb\Utilities\Logger;
|
|
use WizdomNetworks\WizeWeb\Utilities\ErrorHandler;
|
|
|
|
class Sanitizer
|
|
{
|
|
/**
|
|
* Basic string sanitization (removes HTML and encodes entities)
|
|
*/
|
|
public static function sanitizeString(string $value): string
|
|
{
|
|
try {
|
|
$sanitized = htmlspecialchars(strip_tags(trim($value)), ENT_QUOTES, 'UTF-8');
|
|
Logger::debug("Sanitized string: Original: $value | Sanitized: $sanitized");
|
|
return $sanitized;
|
|
} catch (\Throwable $e) {
|
|
ErrorHandler::exception($e);
|
|
return '';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Deep sanitize string using multiple layers (chained method)
|
|
*/
|
|
public static function sanitizeChained(string $value): string
|
|
{
|
|
return htmlspecialchars(strip_tags(trim(filter_var($value, FILTER_SANITIZE_STRING))), ENT_QUOTES, 'UTF-8');
|
|
}
|
|
|
|
/**
|
|
* Sanitize input with tag stripping and encoding
|
|
*/
|
|
public static function sanitizeInput(string $value): string
|
|
{
|
|
return htmlspecialchars(strip_tags(trim($value)), ENT_QUOTES, 'UTF-8');
|
|
}
|
|
|
|
/**
|
|
* Sanitize email
|
|
*/
|
|
public static function sanitizeEmail(string $value): string
|
|
{
|
|
$sanitized = filter_var(trim($value), FILTER_SANITIZE_EMAIL);
|
|
Logger::debug("Sanitized email: Original: $value | Sanitized: $sanitized");
|
|
return $sanitized;
|
|
}
|
|
|
|
/**
|
|
* Sanitize URL
|
|
*/
|
|
public static function sanitizeURL(string $value): string
|
|
{
|
|
$sanitized = filter_var(trim($value), FILTER_SANITIZE_URL);
|
|
Logger::debug("Sanitized URL: Original: $value | Sanitized: $sanitized");
|
|
return $sanitized;
|
|
}
|
|
|
|
/**
|
|
* Recursively sanitize array
|
|
*/
|
|
public static function sanitizeArray(array $data): array
|
|
{
|
|
$clean = [];
|
|
foreach ($data as $key => $value) {
|
|
if (is_array($value)) {
|
|
$clean[$key] = self::sanitizeArray($value);
|
|
} else {
|
|
$clean[$key] = self::sanitizeString((string) $value);
|
|
}
|
|
}
|
|
return $clean;
|
|
}
|
|
}
|