Add enclosure element to RSS feed
[pics.git] / rss.php
1 <?php
2
3 require_once __DIR__ . '/db.php';
4
5 function post_to_item(DOMDocument $dom, Array $post) {
6 $mime_type = $post['MIME_TYPE'];
7 $post_id = $post['POST_ID'];
8 $pic_id = $post['PIC_ID'];
9 $post_size = $post['SIZE'];
10 $post_body = $post['BODY'];
11 $timestamp = $post['TIMESTAMP'];
12 $item = $dom->createElement('item');
13 $description = $dom->createElement('description', $post_body);
14 $link = $dom->createElement('link', "https://pics.jacobcasper.com/#post-{$post_id}");
15 $guid = $dom->createElement('guid', "https://pics.jacobcasper.com/#post-{$post_id}");
16 $pub_date = $dom->createElement('pubDate', date(DATE_RSS, $timestamp));
17 $enclosure = $dom->createElement('enclosure');
18 $enclosure->setAttribute('url', "https://pics.jacobcasper.com/pic/{$pic_id}");
19 $enclosure->setAttribute('length', $post_size);
20 $enclosure->setAttribute('type', $mime_type);
21 $item->appendChild($description);
22 $item->appendChild($enclosure);
23 $item->appendChild($link);
24 $item->appendChild($guid);
25 $item->appendChild($pub_date);
26 return $item;
27 }
28
29 $rss_template = file_get_contents('rss.template.xml');
30 $rss_dom = new DOMDocument();
31 $rss_dom->formatOutput = true;
32 $rss_dom->loadXML($rss_template);
33 $channel_node = $rss_dom->firstChild->childNodes[1];
34 $db = get_db();
35 $posts = $db->query('SELECT
36 p.ID as POST_ID,
37 pi.id as PIC_ID,
38 p.TIMESTAMP,
39 pi.MIME_TYPE,
40 length(pi.PIC_B64) as SIZE,
41 c.BODY
42 FROM posts p
43 JOIN pics pi on p.id = pi.post_id
44 JOIN comments c on p.id = c.post_id
45 ORDER BY p.TIMESTAMP DESC');
46 while ($post = $posts->fetchArray()) {
47 $item = post_to_item($rss_dom, $post);
48 $channel_node->appendChild($item);
49 }
50
51 file_put_contents('rss.xml', $rss_dom->saveXML());