<?php
header('Content-Type: application/rss+xml; charset=utf-8');
include __DIR__ . '/admin/config.php';
date_default_timezone_set('Asia/Kolkata');

$today = date('Y-m-d');

function slugify($text) {
    $text = str_replace('&', 'and', $text);
    $text = preg_replace('~[^\pL\d]+~u', '-', $text);
    $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
    $text = preg_replace('~[^-\w]+~', '', $text);
    $text = trim($text, '-');
    $text = preg_replace('~-+~', '-', $text);
    return strtolower($text);
}
function xmlsafe($s) { return htmlspecialchars($s ?? '', ENT_XML1, 'UTF-8'); }
function cdata($s) { return "<![CDATA[" . $s . "]]>"; }
function rfcdate($date) { return date('r', strtotime($date)); }

$baseUrl = ( (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http')
          . '://' . $_SERVER['HTTP_HOST'];

$channelLink = $baseUrl . "/blog";

$rows = $obj->fetch("SELECT id, title, url, short_description, created_at,summary_text 
                     FROM blog 
                     WHERE save_as_draft='No' 
                     AND (schedule_date IS NULL OR schedule_date <= '$today') 
                     ORDER BY created_at DESC LIMIT 100");

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" version="2.0">
<channel>
<atom:link href="<?= xmlsafe($baseUrl.'/rss.xml') ?>" rel="self" type="application/rss+xml"/>
<title>SoftwareBunch | RSS Feed</title>
<description>SoftwareBunch Official Blog RSS Feed</description>
<link><?= xmlsafe($channelLink) ?></link>
<category>Blog/Microblog/Website</category>
<language>en-us</language>
<lastBuildDate><?= rfcdate(date('Y-m-d H:i:s')) ?></lastBuildDate>
<pubDate><?= rfcdate(date('Y-m-d H:i:s')) ?></pubDate>
<generator>SoftwareBunch RSS Generator</generator>
<ttl>5</ttl>

<?php foreach($rows as $row): ?>
<item>
    <title><?= xmlsafe($row['title']) ?></title>
    <description><?= cdata($row['summary_text']) ?></description>
</item>
<?php endforeach; ?>

</channel>
</rss>