<?php
/**
* HTML Validator for Local Utility (Memory-Optimized)
*
* This script validates HTML files, focusing on common errors.
* Designed for low memory usage, suitable for local use.
*/
/**
* Function to validate an HTML file.
*
* @param string $filename The path to the HTML file.
* @return array An array of error messages. Empty array if no errors.
*/
function validateHTML(string $filename): array
{
$errors = [];
if (!file_exists($filename)) {
$errors[] = "File not found: " . $filename;
return $errors;
}
$html = file_get_contents($filename);
if ($html === false) {
$errors[] = "Error reading file: " . $filename;
return $errors;
}
// Basic HTML structure check
if (!preg_match('/^<!DOCTYPE html>/i', $html)) {
$errors[] = "Missing or incorrect doctype declaration.";
}
// Check for basic tag syntax/closing
if (strpos($html, '</html>') === false) {
$errors[] = "Missing closing </html> tag.";
}
// Check for common errors (can be expanded)
if (strpos($html, 'javascript:') !== false) {
$errors[] = "Inline JavaScript detected. Consider external files.";
}
if (strpos($html, 'eval(') !== false) {
$errors[] = "eval() detected. Security risk. Remove or reconsider.";
}
// More sophisticated validation (using regex - can be slow for large files)
if (preg_match('/<script[^>]*type="text\/javascript"[^>]*>/i', $html)) {
$errors[] = "Script tag with type='text/javascript' detected. Consider using a more modern approach.";
}
//Check for invalid attributes
if (preg_match('/(\s+href=["\']?)\s*#([\s\S]*?)\s*(["\'])/i', $html)) {
$errors[] = "Invalid anchor link usage detected.";
}
//check for potential XSS vulnerabilities
if (preg_match('/<img src=[\'"](.*?)[\'">/i', $html)) {
$errors[] = "Potential XSS vulnerability: Image source not properly sanitized.";
}
return $errors;
}
// Example usage:
if (isset($_GET['file'])) {
$filename = $_GET['file'];
$errors = validateHTML($filename);
if (!empty($errors)) {
echo "<h2>HTML Validation Errors for " . htmlspecialchars($filename) . ":</h2>";
echo "<ul>";
foreach ($errors as $error) {
echo "<li>" . htmlspecialchars($error) . "</li>";
}
echo "</ul>";
} else {
echo "<h2>HTML Validation for " . htmlspecialchars($filename) . " successful!</h2>";
}
} else {
echo "Usage: ?file=path/to/your/html/file.html";
}
?>
Add your comment