<?php
header('Content-Type: application/json');
$action = $_GET['action'] ?? '';
// Directory where templates are stored
$templatesDir = __DIR__ . '/templates';
switch ($action) {
case 'list':
// List all template files
if (!is_dir($templatesDir)) {
echo json_encode(['success' => false, 'error' => 'Templates directory not found']);
exit;
}
$files = scandir($templatesDir);
$templates = array_filter($files, function($file) use ($templatesDir) {
return is_file($templatesDir . '/' . $file)
&& !in_array($file, ['.', '..'])
&& preg_match('/\.(php|html|js|css|txt|md)$/i', $file);
});
echo json_encode([
'success' => true,
'templates' => array_values($templates)
]);
break;
case 'read':
// Read a specific template file
$file = $_GET['file'] ?? '';
// Security: prevent directory traversal
$file = basename($file);
$fullPath = $templatesDir . '/' . $file;
if (!file_exists($fullPath)) {
echo json_encode(['success' => false, 'error' => 'Template not found: ' . $file]);
exit;
}
$content = file_get_contents($fullPath);
echo json_encode([
'success' => true,
'content' => $content,
'file' => $file
]);
break;
default:
echo json_encode(['success' => false, 'error' => 'Invalid action']);
break;
}
?>