60 lines
1.7 KiB
JavaScript
60 lines
1.7 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const buildDir = path.join(__dirname, 'dist');
|
|
|
|
// Basic HTML entity decode
|
|
function decodeHtmlEntities(str) {
|
|
return str
|
|
.replace(/&#(\d+);/g, (_, dec) => String.fromCharCode(dec))
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, "'")
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>');
|
|
}
|
|
|
|
// Decode both URI and HTML entities inside <?php ... ?>
|
|
function decodePhpBlocks(content) {
|
|
return content.replace(/<\?php([\s\S]*?)\?>/g, (match, code) => {
|
|
try {
|
|
let decoded = decodeURIComponent(code); // %XX to characters
|
|
decoded = decodeHtmlEntities(decoded); // ' to '
|
|
return `<?php${decoded}?>`;
|
|
} catch (e) {
|
|
console.warn('⚠️ Decode failed for block:', code.trim().slice(0, 50), '...');
|
|
return match;
|
|
}
|
|
});
|
|
}
|
|
|
|
function fixPhpTagsInFile(filePath) {
|
|
let content = fs.readFileSync(filePath, 'utf-8');
|
|
|
|
// Step 1: Replace escaped tags
|
|
content = content
|
|
.replace(/<\?php/g, '<?php')
|
|
.replace(/\?>/g, '?>');
|
|
|
|
// Step 2: Decode content inside <?php ... ?>
|
|
content = decodePhpBlocks(content);
|
|
|
|
fs.writeFileSync(filePath, content, 'utf-8');
|
|
console.log(`✅ Processed: ${filePath}`);
|
|
}
|
|
|
|
function walkDir(dir) {
|
|
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
for (const entry of entries) {
|
|
const fullPath = path.join(dir, entry.name);
|
|
if (entry.isDirectory()) {
|
|
walkDir(fullPath);
|
|
} else if (fullPath.endsWith('.html') || fullPath.endsWith('.php')) {
|
|
fixPhpTagsInFile(fullPath);
|
|
}
|
|
}
|
|
}
|
|
|
|
walkDir(buildDir);
|
|
console.log('🎉 All PHP tag fixes, URL decodes, and HTML entity decodes complete.');
|