-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss.php
More file actions
122 lines (109 loc) · 5.33 KB
/
rss.php
File metadata and controls
122 lines (109 loc) · 5.33 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
<?php
declare(strict_types=1);
/**
* FlatFile Blog - Enhanced RSS Feed
* Generates RSS 2.0 feed with comprehensive metadata
*/
// Check if blog is installed
if (!file_exists('config.php')) {
header('Location: install.php');
exit;
}
// Define constant to allow access
define('ALLOW_DIRECT_ACCESS', true);
try {
require_once 'functions.php';
} catch (Exception $e) {
error_log('Error loading functions.php: ' . $e->getMessage());
die('System error. Please try again later.');
}
// Load settings and interface mode
$settings = load_settings();
$interface_mode = defined('INTERFACE_MODE') ? (string) constant('INTERFACE_MODE') : ($settings['interface_mode'] ?? 'classic');
// Get latest published posts
$posts = get_posts(1, 20, 'published'); // Get first 20 published posts
if ($interface_mode === 'custom') {
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: public, max-age=300');
$index_file = CONTENT_DIR . 'index.json';
$last_modified_ts = file_exists($index_file) ? filemtime($index_file) : time();
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $last_modified_ts) . ' GMT');
$json_posts = [];
foreach ($posts as $post) {
$json_posts[] = [
'slug' => $post['slug'] ?? '',
'title' => $post['title'] ?? '',
'excerpt' => $post['excerpt'] ?? '',
'content_html' => $post['content_html'] ?? $post['content'] ?? '',
'content_markdown' => $post['content_markdown'] ?? '',
'content_type' => $post['content_type'] ?? 'html',
'author' => $post['author'] ?? 'admin',
'status' => $post['status'] ?? 'published',
'date' => $post['date'] ?? '',
'updated' => $post['updated'] ?? '',
'tags' => $post['tags'] ?? [],
'categories' => $post['categories'] ?? [],
'meta' => $post['meta'] ?? [],
'url' => BASE_URL . urlencode($post['slug'] ?? '')
];
}
$payload = [
'mode' => 'custom',
'site' => [
'title' => SITE_TITLE,
'base_url' => BASE_URL,
'description' => $settings['site_description'] ?? ''
],
'generated_at' => date('c'),
'count' => count($json_posts),
'posts' => $json_posts
];
echo json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
exit;
}
// Set RSS headers with caching
header('Content-Type: application/rss+xml; charset=utf-8');
header('Cache-Control: public, max-age=3600'); // 1 hour cache
$index_file = CONTENT_DIR . 'index.json';
$last_modified_ts = file_exists($index_file) ? filemtime($index_file) : time();
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $last_modified_ts) . ' GMT');
// Generate RSS XML
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title><?php echo htmlspecialchars(SITE_TITLE); ?></title>
<link><?php echo BASE_URL; ?></link>
<description>A fast, lightweight flat-file blog system built with PHP and Bootstrap</description>
<language>en-us</language>
<lastBuildDate><?php echo date('r'); ?></lastBuildDate>
<generator>FlatFile Blog v1.0</generator>
<managingEditor><?php echo htmlspecialchars($posts[0]['author'] ?? 'admin'); ?>@<?php echo parse_url(BASE_URL, PHP_URL_HOST); ?></managingEditor>
<webMaster><?php echo htmlspecialchars($posts[0]['author'] ?? 'admin'); ?>@<?php echo parse_url(BASE_URL, PHP_URL_HOST); ?></webMaster>
<ttl>60</ttl>
<atom:link href="<?php echo BASE_URL; ?>rss" rel="self" type="application/rss+xml" />
<?php if (!empty($posts)): ?>
<?php foreach ($posts as $post): ?>
<item>
<title><?php echo htmlspecialchars($post['title']); ?></title>
<link><?php echo BASE_URL; ?><?php echo urlencode($post['slug']); ?></link>
<description>
<![CDATA[<?php echo htmlspecialchars($post['excerpt'] ?: substr(strip_tags($post['content'] ?? ''), 0, 200) . '...'); ?>]]>
</description>
<pubDate><?php echo date('r', strtotime($post['date'])); ?></pubDate>
<guid isPermaLink="true"><?php echo BASE_URL; ?><?php echo urlencode($post['slug']); ?></guid>
<author><?php echo htmlspecialchars($post['author']); ?>@<?php echo parse_url(BASE_URL, PHP_URL_HOST); ?></author>
<dc:creator><?php echo htmlspecialchars($post['author']); ?></dc:creator>
<?php if (!empty($post['tags'])): ?>
<?php foreach ($post['tags'] as $tag): ?>
<category><?php echo htmlspecialchars($tag); ?></category>
<?php endforeach; ?>
<?php endif; ?>
<?php if (!empty($post['meta']['image'])): ?>
<enclosure url="<?php echo htmlspecialchars($post['meta']['image']); ?>" type="image/jpeg" />
<?php endif; ?>
</item>
<?php endforeach; ?>
<?php endif; ?>
</channel>
</rss>