-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss.php
More file actions
139 lines (128 loc) · 5.85 KB
/
Copy pathrss.php
File metadata and controls
139 lines (128 loc) · 5.85 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
<?php
/**
* RSS 2.0 feed for the Cloud Technology Computing blog.
*
* Supports optional ?s=<term> filter that narrows to posts whose title, excerpt,
* content, or category contains the term (same pattern as blog.php).
*
* @see https://validator.w3.org/feed/ (validate after deploy)
*/
require_once __DIR__ . '/includes/db.php';
$siteUrl = 'https://www.cloudtechnologycomputing.com';
$feedUrl = $siteUrl . '/rss.php';
$search = trim((string) ($_GET['s'] ?? ''));
header('Content-Type: application/rss+xml; charset=utf-8');
header('Cache-Control: public, max-age=900'); // 15 minutes
$whereSql = "WHERE p.status = 'published'";
$params = [];
if ($search !== '') {
$whereSql .= " AND (
p.title LIKE :search
OR p.excerpt LIKE :search
OR p.content_html LIKE :search
OR c.name LIKE :search
)";
$params[':search'] = '%' . $search . '%';
}
$sql = "
SELECT
p.id, p.slug, p.title, p.excerpt, p.content_html, p.author_name,
p.post_date, p.updated_at, p.featured_image, p.featured_image_alt,
p.og_image,
c.name AS category_name, c.slug AS category_slug
FROM blog_posts p
LEFT JOIN blog_categories c ON p.category_id = c.id
{$whereSql}
ORDER BY p.post_date DESC, p.id DESC
LIMIT 20
";
$posts = [];
try {
$stmt = $pdo->prepare($sql);
foreach ($params as $k => $v) {
$stmt->bindValue($k, $v);
}
$stmt->execute();
$posts = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Throwable $e) {
// Database unavailable: emit a valid empty feed rather than 500.
error_log('rss.php DB error: ' . $e->getMessage());
$posts = [];
}
$feedTitle = $search !== ''
? "Cloud Technology Computing Blog — search: {$search}"
: 'Cloud Technology Computing Blog';
$feedDescription = $search !== ''
? "Blog posts matching \"{$search}\" from Cloud Technology Computing."
: 'Cloud computing, AI automation, web development, SEO, and small business IT insights from Cloud Technology Computing.';
// Build site logo absolute URL
$siteLogo = $siteUrl . '/assets/img/sm-logo.svg';
// Last build date from latest post, or now
$lastBuild = !empty($posts) ? strtotime($posts[0]['post_date']) : time();
$pubDate = gmdate('D, d M Y H:i:s', $lastBuild) . ' GMT';
echo '<?xml version="1.0" encoding="UTF-8"?>', "\n";
?>
<rss version="2.0"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
<channel>
<title><?= htmlspecialchars($feedTitle, ENT_XML1, 'UTF-8') ?></title>
<link><?= htmlspecialchars($siteUrl, ENT_XML1, 'UTF-8') ?></link>
<description><?= htmlspecialchars($feedDescription, ENT_XML1, 'UTF-8') ?></description>
<language>en-us</language>
<copyright>© <?= date('Y') ?> Cloud Technology Computing. All rights reserved.</copyright>
<managingEditor>Jgil20@me.com (Jhon Arzu-Gil)</managingEditor>
<webMaster>Jgil20@me.com (Jhon Arzu-Gil)</webMaster>
<lastBuildDate><?= $pubDate ?></lastBuildDate>
<pubDate><?= $pubDate ?></pubDate>
<ttl>60</ttl>
<image>
<url><?= htmlspecialchars($siteLogo, ENT_XML1, 'UTF-8') ?></url>
<title><?= htmlspecialchars($feedTitle, ENT_XML1, 'UTF-8') ?></title>
<link><?= htmlspecialchars($siteUrl, ENT_XML1, 'UTF-8') ?></link>
<width>144</width>
<height>144</height>
</image>
<atom:link href="<?= htmlspecialchars($feedUrl . ($search !== '' ? '?s=' . rawurlencode($search) : ''), ENT_XML1, 'UTF-8') ?>" rel="self" type="application/rss+xml" />
<?php foreach ($posts as $post):
$postUrl = $siteUrl . '/blog/' . rawurlencode($post['slug']);
$postImg = $post['og_image'] ?: $post['featured_image'];
$postImgAbs = '';
if ($postImg) {
if (str_starts_with($postImg, 'http://') || str_starts_with($postImg, 'https://')) {
$postImgAbs = $postImg;
} else {
$postImgAbs = $siteUrl . '/' . ltrim($postImg, '/');
}
}
$pub = strtotime($post['post_date']);
$upd = strtotime($post['updated_at'] ?? '');
$pubR = $pub ? gmdate('D, d M Y H:i:s', $pub) . ' GMT' : '';
$updR = $upd ? gmdate('D, d M Y H:i:s', $upd) . ' GMT' : '';
?>
<item>
<title><?= htmlspecialchars($post['title'], ENT_XML1, 'UTF-8') ?></title>
<link><?= htmlspecialchars($postUrl, ENT_XML1, 'UTF-8') ?></link>
<guid isPermaLink="true"><![CDATA[<?= htmlspecialchars($postUrl, ENT_XML1, 'UTF-8') ?>]]></guid>
<description><![CDATA[<?= $post['excerpt'] ?? '' ?>]]></description>
<content:encoded><![CDATA[<?= $post['content_html'] ?? '' ?>]]></content:encoded>
<dc:creator><![CDATA[<?= htmlspecialchars($post['author_name'] ?? 'Jhon Arzu-Gil', ENT_XML1, 'UTF-8') ?>]]></dc:creator>
<?php if (!empty($post['category_name'])): ?>
<category><![CDATA[<?= htmlspecialchars($post['category_name'], ENT_XML1, 'UTF-8') ?>]]></category>
<?php endif; ?>
<?php if ($pubR): ?><pubDate><?= $pubR ?></pubDate><?php endif; ?>
<?php if ($updR): ?><atom:updated><?= gmdate('Y-m-d\TH:i:s\Z', $upd) ?></atom:updated><?php endif; ?>
<?php if ($postImgAbs): ?>
<media:content url="<?= htmlspecialchars($postImgAbs, ENT_XML1, 'UTF-8') ?>" medium="image" <?= !empty($post['featured_image_alt']) ? '' : '' ?>type="<?= preg_match('/\.(avif|webp)$/i', $postImgAbs) ? (preg_match('/\.avif$/i', $postImgAbs) ? 'image/avif' : 'image/webp') : 'image/jpeg' ?>">
<?php if (!empty($post['featured_image_alt'])): ?>
<media:title type="plain"><![CDATA[<?= htmlspecialchars($post['featured_image_alt'], ENT_XML1, 'UTF-8') ?>]]></media:title>
<?php endif; ?>
</media:content>
<?php endif; ?>
</item>
<?php endforeach; ?>
</channel>
</rss>