| 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 | $now_rss = date(DATE_RSS); |
| 30 | |
| 31 | $rss_template = file_get_contents('rss.template.xml'); |
| 32 | $rss_dom = new DOMDocument(); |
| 33 | $rss_dom->formatOutput = true; |
| 34 | $rss_dom->loadXML($rss_template); |
| 35 | $channel_node = $rss_dom->firstChild->childNodes[1]; |
| 36 | $channel_node->appendChild($rss_dom->createElement('pubDate', $now_rss)); |
| 37 | $channel_node->appendChild($rss_dom->createElement('lastBuildDate', $now_rss)); |
| 38 | $channel_node->appendChild($rss_dom->createElement('generator', 'https://git.jacobcasper.com/?p=pics.git;a=blob;f=rss.php;h=0312255783027646c3dd7a86636776dec82db03c;hb=HEAD')); |
| 39 | $db = get_db(); |
| 40 | $posts = $db->query('SELECT |
| 41 | p.ID as POST_ID, |
| 42 | pi.id as PIC_ID, |
| 43 | p.TIMESTAMP, |
| 44 | pi.MIME_TYPE, |
| 45 | length(pi.PIC_B64) as SIZE, |
| 46 | c.BODY |
| 47 | FROM posts p |
| 48 | JOIN pics pi on p.id = pi.post_id |
| 49 | JOIN comments c on p.id = c.post_id |
| 50 | ORDER BY p.TIMESTAMP DESC'); |
| 51 | while ($post = $posts->fetchArray()) { |
| 52 | $item = post_to_item($rss_dom, $post); |
| 53 | $channel_node->appendChild($item); |
| 54 | } |
| 55 | |
| 56 | file_put_contents('rss.xml', $rss_dom->saveXML()); |