-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
311 lines (251 loc) · 14.3 KB
/
index.php
File metadata and controls
311 lines (251 loc) · 14.3 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
$startTime = microtime(true); // Start timer for page render
// Define paths
$articleDirectory = __DIR__ . '/articles';
$templateDirectory = __DIR__ . '/templates';
$articleTemplateDirectory = $templateDirectory . '/article';
$styleDirectory = __DIR__ . '/styles';
$indexFile = $templateDirectory . '/index/1.txt';
$defaultTemplate = '1.txt';
$defaultStyle = '1.txt';
$membersDirectory = __DIR__ . '/members';
$aboutDirectory = $templateDirectory . '/about';
$emailsDirectory = $templateDirectory . '/email';
$queryParams = $_GET; // Get all the query parameters from the URL
$articleParam = isset($queryParams['article']) ? trim($queryParams['article']) : null;
$showHidden = isset($queryParams['showall']);
function space($n) { return str_repeat(' ', $n); }
function paramKey($key) { global $queryParams; return isset($queryParams[$key]); }
function paramVal($key, $default = null) { global $queryParams; return $queryParams[$key] ? $queryParams[$key] : $default; }
function loadFile($path) { return file_exists($path) ? file_get_contents($path) : null; }
function getArticlePaths($dir) {
global $showHidden;
$articles = [];
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile() && $fileinfo->getExtension() == 'txt' && ($showHidden || $fileinfo->getFilename()[0] != '.')) {
$articles[] = $fileinfo->getPathname();
}
}
// Sort the articles by date and file number (ascending order)
usort($articles, function($a, $b) {
$aDate = getArticleDateFromPath($a);
$bDate = getArticleDateFromPath($b);
// Compare dates first (ascending order)
if ($aDate != $bDate) {
return strcmp($aDate, $bDate); // Ascending order (oldest first)
}
// If dates are the same, compare the file number (ascending order)
$aNumber = getArticleNumberFromPath($a);
$bNumber = getArticleNumberFromPath($b);
return $aNumber - $bNumber; // Ascending order (smallest number first)
});
return $articles;
}
function getArticleDateFromPath($path) {
// Assuming the path is in the format /articles/YYYY/MM/DD/#.txt
// Example: /articles/2024/10/06/1.txt
preg_match('/\/articles\/(\d{4})\/(\d{2})\/(\d{2})\//', str_replace('\\', '/', $path), $matches);
if ($matches) {
return sprintf('%s-%s-%s', $matches[1], $matches[2], $matches[3]); // YYYY-MM-DD
}
return '';
}
function getArticleNumberFromPath($path) {
// Assuming the file is named like 1.txt, 2.txt, etc.
preg_match('/\/(\d+)\.txt$/', $path, $matches);
return isset($matches[1]) ? (int) $matches[1] : 0;
}
function getArticleMetadata($path) {
//$content = file_get_contents($path);
// More efficient than reading entire file
/*$handle = fopen($path, 'r');
$firstLines = '';
if ($handle) {
$lineCount = 0;
while (($line = fgets($handle)) !== false && $lineCount < 5) {
$firstLines .= $line;
$lineCount++;
}
fclose($handle);
} else {
echo "Error opening file ($path).";
}
$content = $firstLines;*/
$handle = fopen($path, 'r') or die("Error opening file ($path).");
$content = fgets($handle) or die("No content found in file ($path).");
preg_match('/TITLE=(.+)/', $content, $titleMatch) or die("No TITLE field found in file ($path). Expected to be first line.");
return [
'title' => isset($titleMatch[1]) ? trim($titleMatch[1]) : 'Untitled',
'date' => getArticleDateFromPath($path),
'path' => $path
];
}
function groupArticlesByMonth($articles) {
$grouped = [];
for ($i = count($articles) - 1; $i >= 0; $i--) {
$article = $articles[$i];
$metadata = getArticleMetadata($article);
$date = $metadata['date'];
$monthYear = date('F Y', strtotime($date)); // Convert date to "Month Year" format
if (!isset($grouped[$monthYear])) {
$grouped[$monthYear] = [];
}
$grouped[$monthYear][] = $metadata;
}
return $grouped;
}
function renderIndex($message = '') {
global $articleDirectory, $indexFile, $showHidden;
$articles = getArticlePaths($articleDirectory); // Get paths for all articles
$groupedArticles = groupArticlesByMonth($articles);
$templateContent = loadFile($indexFile);
if ($templateContent) {
$templateContent = preg_replace('/<!-- \(MESSAGE\) -->\s*/', $message ? "<div style='color: orange; text-align: center; font-family: verdana; font-size: 1em; padding: 5px;'>⚠ $message ⚠</div>\n\n" : '', $templateContent);
$html = ''; $indent = 8;
foreach ($groupedArticles as $monthYear => $articles) {
$html = $html . "\n\n" . space($indent) . "<h3>$monthYear</h3>\n";
$html = $html . space($indent) . "<ul>\n";
foreach ($articles as $article) {
$relativePath = str_replace([$articleDirectory, '.txt'], '', $article['path']);
$url = '/?article=' . str_replace('/', '-', trim($relativePath, '/\\'));
$url = str_replace('\\', '-', $url); // Needed for Windows localhost mode
$hidden = $showHidden && preg_match('/-\.\d+$/', $url);
$html .= space($indent + 4) . '<li' . ($hidden ? ' style="background-color: #eee;"' : '') . '><a href="' . $url . '">';
$html .= '<span class="publish-date">' . date('M d', strtotime($article['date'])) . '</span> | ';
$html .= htmlspecialchars($article['title']);
$html .= "</a></li>\n";
}
$html .= space($indent) . '</ul>';
}
echo preg_replace(space($indent) . '/\s*<!-- \(ARTICLE LINKS\) -->/', $html, $templateContent);
}
else {
echo "Error: Index file not found.";
}
}
function echoln($html) { echo $html . "\n"; }
function convertImages($html) { return paramKey('text') ? preg_replace('/\s+src\s*=\s*\"data:[^"]+"/', /*' src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR42mNgYGAAAAAEAAHI6uv5AAAAAElFTkSuQmCC"'*/' src="placeholder.png"', $html) : $html; }
//function getMemberPortrait($id) { global $membersDirectory; return convertImages(loadFile($membersDirectory . '/' . $id . '/portrait.txt')); }
function getMemberPortrait($id)
{
global $membersDirectory;
$info = getMemberInfo($id);
return convertImages(str_replace('<img src', '<img alt="' . $info['name'] . ', ' . $info['title'] . '" src', loadFile($membersDirectory . '/' . $id . '/portrait.txt')));
}
function removeBoldAndItalics($html) { return $html ? str_replace(['<strong>', '</strong>', '<em>', '</em>'], null, $html) : $html; }
function getMemberInfo($id)
{
global $membersDirectory;
$info = loadFile($membersDirectory . '/' . $id . '/info.txt') or die('Unable to read member info file');
$fields = [];
preg_match('/NAME=(.+)/', $info, $matches);
$fields['name'] = isset($matches[1]) ? trim($matches[1]) : 'Name not specified';
preg_match('/TITLE=(.+)/', $info, $matches);
$fields['title'] = isset($matches[1]) ? trim($matches[1]) : 'Title not specified';
$fields['role'] = null; $fields['bio'] = null;
$blocks = preg_split('/\[/', $info);
for ($i = 1; $i < count($blocks); $i++) if (substr($blocks[$i], 0, 5) == 'ROLE]') $fields['role'] = trim(substr($blocks[$i], 5)); elseif (substr($blocks[$i], 0, 4) == 'BIO]') $fields['bio'] = trim(substr($blocks[$i], 4));
if (!$fields['role']) $fields['role'] = $fields['bio'] ? $fields['bio'] : 'Role not specified';
if (!$fields['bio']) $fields['bio'] = $fields['role'] ? $fields['role'] : 'Bio not specified';
$fields['role'] = removeBoldAndItalics($fields['role']);
return $fields;
}
function renderArticle($articlePath) {
global $articleTemplateDirectory, $styleDirectory, $membersDirectory, $defaultTemplate, $defaultStyle;
$articleContent = loadFile($articlePath);
if (!$articleContent) {
return false; // Article not found
}
// Parse article fields (TEMPLATE, STYLE, DATETIME, TITLE, AUTHOR, CONTRIBUTORS, CONTENT)
$fields = [];
preg_match('/TEMPLATE=(.+)/', $articleContent, $matches);
$fields['template'] = isset($matches[1]) ? trim($matches[1]) . '.txt' : $defaultTemplate;
preg_match('/STYLE=(.+)/', $articleContent, $matches);
$fields['style'] = isset($matches[1]) ? trim($matches[1]) . '.txt' : $defaultStyle;
$dateFromPath = getArticleDateFromPath($articlePath);
$fields['datetime'] = "<time datetime=\"$dateFromPath\">Published on " . date('F j, Y', strtotime($dateFromPath)) . '</time>';
preg_match('/TITLE=(.+)/', $articleContent, $matches);
$fields['title'] = isset($matches[1]) ? trim($matches[1]) : 'Untitled Article';
preg_match('/AUTHOR=(.+)/', $articleContent, $matches);
$fields['author'] = isset($matches[1]) ? trim($matches[1]) : '';
preg_match('/CONTRIBUTORS=(.+)/', $articleContent, $matches);
$fields['contributors'] = isset($matches[1]) ? trim($matches[1]) : '';
// Extract content after [CONTENT]
$fields['content'] = trim(preg_split('/\[CONTENT\]\s*/', $articleContent)[1] ?? '');
// Load the template and style
$templateContent = loadFile($articleTemplateDirectory . '/' . $fields['template']);
$styleContent = trim(loadFile($styleDirectory . '/' . $fields['style']));
// Load the author bio
$authorId = $fields['author'];
//$authorPortrait = loadFile($membersDirectory . '/' . $authorId . '/portrait.txt');
$authorPortrait = getMemberPortrait($authorId);
$authorLinkedPortrait = $authorPortrait ? "<a class=\"proud-link\" href=\"/?about#m$authorId\">" . $authorPortrait . '</a>' : '';
$authorInfo = getMemberInfo($authorId);
//$authorBio = '<div class="author-bio">' . $authorInfo['bio'] . '</div>';
//$authorBio = '<div class="author-bio">' . '<div class="author-info-header-container" style="margin-top: 15px;"><h3 style="margin: 0; Xbackground-color: orange;">' . $authorInfo['name'] . '</h3><div class="gear-icon"><h3>⚙</h3><div class="tooltip"><h2><strong>Generative Models:</strong></h2><span class="contributors"><!-- (CONTRIBUTORS) --></span></div></div></div><h4 style="margin: 0; margin-top: 3px; color: #555;">' . $authorInfo['title'] . '</h4>' . $authorInfo['bio'] . '</div>';
$indent = 12;
$authorBio = '<div class="author-bio">' . "\n" . space($indent + 4) . '<div class="author-info-header-container">' . "\n" . space($indent + 8) . '<div></div>' . "\n" . space($indent + 8) . '<div class="gear-icon">' . "\n" . space($indent + 12) . '<h3>⚙</h3>' . "\n" . space($indent + 12) . '<div class="popup">' . "\n" . space($indent + 16) . '<h2><strong>Generative Models:</strong></h2>' . "\n" . space($indent + 16) . '<span class="contributors"><!-- (CONTRIBUTORS) --></span>' . "\n" . space($indent + 12) . '</div>' . "\n" . space($indent + 8) . '</div>' . "\n" . space($indent + 4) . '</div>' . "\n" . space($indent + 4) . '<h3 style="margin: 0; margin-top: -15px;">' . $authorInfo['name'] . '</h3>' . "\n" . space($indent + 4) . '<h4 style="margin: 0; margin-top: 3px; margin-bottom: 16px; color: #666;">' . $authorInfo['title'] . '</h4>' . "\n" . space($indent + 4) . $authorInfo['bio'] . "\n" . space($indent) . '</div>';
$authorInfoContent = trim($authorLinkedPortrait . "\n" . space($indent) . $authorBio);
// Substitute placeholders in the template
$output = str_replace(
[
'<!-- (TITLE) -->',
'<!-- (STYLE) -->',
'<!-- (DATETIME) -->',
'<!-- (CONTENT) -->',
'<!-- (AUTHOR) -->',
'<!-- (CONTRIBUTORS) -->',
],
[
htmlspecialchars($fields['title']),
$styleContent,
$fields['datetime'],
$fields['content'],
$authorInfoContent,
$fields['contributors'],
],
$templateContent
);
echo convertImages($output);
return true;
}
function checkForUnrecognizedParams($recognizedParams, $queryParams) {
foreach ($queryParams as $key => $value) {
if (!in_array($key, $recognizedParams)) {
renderIndex("Invalid parameter \"{$key}\"");
return true;
}
}
}
if (!checkForUnrecognizedParams(['about', 'article', 'showall', 'text', 'email', 'rnf'], $queryParams))
{
// Add "ErrorDocument 404 /index.php?rnf" to .htaccess file and restart Apache web server for this to work (Nginx uses "server { error_page 404 /index.php?rnf; }")
if (paramKey('rnf')) { renderIndex('The resource you requested does not exist'); }
elseif (paramKey('about')) { include($aboutDirectory . '/1.txt'); }
elseif (paramKey('email')) { echo file_get_contents($emailsDirectory . '/' . paramVal('email', 3) . '.html'); }
elseif ($articleParam)
{
if (preg_match('/^\d{4}-\d{2}-\d{2}-\.?\d+$/', $articleParam)) // Check if the article specifier has correct format
{
$articlePath = $articleDirectory . '/' . str_replace('-', '/', $articleParam) . '.txt';
if (!renderArticle($articlePath)) renderIndex("The article \"$articleParam\" does not exist");
}
else { renderIndex("Invalid article specifier \"$articleParam\" expected format is \"YYYY-MM-DD-#\""); }
}
else { renderIndex(); }
}
//$serverSoftware = $_SERVER['SERVER_SOFTWARE'];
//$serverSoftware = (stripos($serverSoftware, 'apache') !== false ? 'Apache' : (stripos($serverSoftware, 'nginx') !== false ? 'Nginx' : '{Unknown}'));
//$timestamp = date('Y-m-d @ H:i:s T') . date_default_timezone_get();
$timestamp = date('Y-m-d H:i:s');
$utcTimezone = new DateTimeZone(date_default_timezone_get());
$estTimezone = new DateTimeZone('America/Toronto');
$date = new DateTime($timestamp, $utcTimezone);
$date->setTimezone($estTimezone);
$timestamp = $date->format('Y-m-d @ H:i T');
$elapsed = number_format((microtime(true) - $startTime) * 1000, 1); // End timer for page render
//echo "\n\n<!-- Rendered in $elapsed ms by $serverSoftware Web Server & PHP " . phpversion() . " on $timestamp -->";
$serverSoftware = $_SERVER['SERVER_SOFTWARE'];
echo "\n\n<!-- Rendered in $elapsed ms on $timestamp | $serverSoftware -->";
?>