-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
233 lines (197 loc) · 8.39 KB
/
index.php
File metadata and controls
233 lines (197 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
require_once __DIR__ . '/config/env.php';
require_once __DIR__ . '/config/Logger.php';
require_once __DIR__ . '/config/Database.php';
putenv("APP_NAME=index");
class PageLoader {
private $logger;
private $db;
private $loadedStyles = [];
private $loadedScripts = [];
private $translations = [];
public function __construct() {
$this->logger = Logger::getInstance();
$this->logger->log("Request started: " . $_SERVER['REQUEST_URI']);
// Initialize database
if (!getenv('DB_PATH')) {
throw new Exception('DB_PATH environment variable is not set');
}
$this->db = Database::getInstance();
// Load translations
$this->loadTranslations();
}
private function loadTranslations() {
try {
$query = "SELECT l.code as lang, t.content_type || '.' || t.field_name as key, t.translation as value
FROM translations t
JOIN languages l ON t.language_id = l.id";
$result = $this->db->query($query);
foreach ($result as $row) {
$this->translations[$row['lang']][$row['key']] = $row['value'];
}
$this->logger->log("Translations loaded successfully");
} catch (Exception $e) {
$this->logger->logError("Error loading translations", $e);
}
}
private function processTranslations($content) {
return preg_replace_callback(
'/\[translate key="([^"]+)" lang="([^"]+)"\]/',
function($matches) {
$key = $matches[1];
$lang = $matches[2];
return $this->getTranslation($key, $lang);
},
$content
);
}
private function getTranslation($key, $lang) {
if (isset($this->translations[$lang][$key])) {
return $this->translations[$lang][$key];
}
$this->logger->log("Translation not found: {$key} ({$lang})", "WARNING");
return $key; // Return the key as fallback
}
public function loadSection($sectionName) {
$sectionPath = __DIR__ . '/sections/' . $sectionName;
// Check if section exists
if (!is_dir($sectionPath)) {
$this->logger->log("Section not found: {$sectionName}", "ERROR");
return false;
}
// Load section files
$this->loadSectionStyle($sectionName);
// Capture section HTML output to process translations
ob_start();
$this->loadSectionHTML($sectionName);
$content = ob_get_clean();
echo $this->processTranslations($content);
$this->registerSectionScript($sectionName);
// Load all other files except SQL
$files = glob($sectionPath . '/*');
foreach ($files as $file) {
if (is_file($file)) {
$extension = pathinfo($file, PATHINFO_EXTENSION);
if ($extension !== 'sql' &&
$extension !== 'css' && // Skip CSS as it's already handled
$extension !== 'js' && // Skip JS as it's already handled
$extension !== 'php') { // Skip PHP as it's already handled
include $file;
}
}
}
return true;
}
private function loadSectionStyle($sectionName) {
// Load base style from sections directory
$baseStylePath = "/sections/{$sectionName}/style.css";
$fullpath = __DIR__ . $baseStylePath;
if (!in_array($baseStylePath, $this->loadedStyles) && file_exists($fullpath)) {
$this->loadedStyles[] = $baseStylePath;
echo "<link rel='stylesheet' href='{$baseStylePath}?v=" . filemtime($fullpath) . "'>\n";
$this->logger->log("Loaded base style: {$baseStylePath} path: {$fullpath}");
}
// Check for override style in www directory
$wwwPath = __DIR__ . '/../www';
if (is_dir($wwwPath)) {
$sites = glob($wwwPath . '/*', GLOB_ONLYDIR);
foreach ($sites as $site) {
$overrideStylePath = $site . "/sections/{$sectionName}/style.css";
if (file_exists($overrideStylePath)) {
$relativePath = str_replace(__DIR__, '', $overrideStylePath);
if (!in_array($relativePath, $this->loadedStyles)) {
$this->loadedStyles[] = $relativePath;
echo "<link rel='stylesheet' href='{$relativePath}?v=" . filemtime($overrideStylePath) . "'>\n";
$this->logger->log("Loaded override style: {$relativePath} path: {$overrideStylePath}");
}
}
}
}
}
private function loadSectionHTML($sectionName) {
$htmlPath = __DIR__ . "/sections/{$sectionName}/html.php";
if (file_exists($htmlPath)) {
include $htmlPath;
$this->logger->log("Loaded HTML: {$htmlPath}");
}
}
private function registerSectionScript($sectionName) {
$scriptPath = "/sections/{$sectionName}/script.js";
if (!in_array($scriptPath, $this->loadedScripts) && file_exists(__DIR__ . $scriptPath)) {
$this->loadedScripts[] = $scriptPath;
echo "<script defer src='{$scriptPath}?v=" . filemtime(__DIR__ . $scriptPath) . "'></script>\n";
$this->logger->log("Registered script: {$scriptPath}");
}
}
public function run() {
try {
// Start session before any output
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
// Get current page from URL
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$page = trim($uri, '/');
if (empty($page)) {
$page = 'home';
}
$this->logger->log("Loading page: $page");
// Get page data
$pageData = $this->db->query(
"SELECT * FROM pages WHERE slug = :slug AND status = 'published'",
['slug' => $page]
);
if (empty($pageData)) {
$this->logger->log("Page not found: $page", "ERROR");
header("HTTP/1.0 404 Not Found");
include __DIR__ . '/404.php';
exit;
}
$pageId = $pageData[0]['id'];
$this->logger->log("Page found, ID: $pageId");
// Get page meta data
$metaData = $this->db->query(
"SELECT * FROM meta WHERE page_id = :page_id",
['page_id' => $pageId]
);
$this->logger->log("Meta data loaded");
// Get page sections
$sections = $this->db->query(
"SELECT * FROM sections WHERE page_id = :page_id ORDER BY sort_order ASC",
['page_id' => $pageId]
);
$this->logger->log("Loaded " . count($sections) . " sections");
// Start output buffering
ob_start();
// Include header
include __DIR__ . '/header.php';
// Render each section
foreach ($sections as $section) {
try {
$this->logger->log("Rendering section: " . $section['name']);
$this->loadSection($section['name']);
} catch (Exception $e) {
$this->logger->logError("Error rendering section: " . $section['name'], $e);
}
}
// Include footer
include __DIR__ . '/footer.php';
// Process translations in the final output
$content = ob_get_clean();
echo $this->processTranslations($content);
$this->logger->log("Page rendered successfully");
} catch (Exception $e) {
$this->logger->logError("Fatal error", $e);
// Clear any output
if (ob_get_level() > 0) {
ob_end_clean();
}
// Show error page
header("HTTP/1.0 500 Internal Server Error");
include __DIR__ . '/500.php';
}
}
}
// Initialize and run the page loader
$pageLoader = new PageLoader();
$pageLoader->run();