summaryrefslogtreecommitdiff
path: root/includes/pages/user_atom.php
blob: 3c4b631d80718ad8939510a1336f28ab1d5e49df (plain)
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
<?php

use Engelsystem\Database\DB;

/**
 * Publically available page to feed the news to feedreaders
 */
function user_atom()
{
    global $user, $display_news;

    if (!isset($_REQUEST['key']) || !preg_match('/^[\da-f]{32}$/', $_REQUEST['key'])) {
        engelsystem_error('Missing key.');
    }
    $key = $_REQUEST['key'];

    $user = User_by_api_key($key);
    if ($user == null) {
        engelsystem_error('Key invalid.');
    }
    if (!in_array('atom', privileges_for_user($user['UID']))) {
        engelsystem_error('No privilege for atom.');
    }

    $news = DB::select('
        SELECT *
        FROM `News` 
        ' . (empty($_REQUEST['meetings']) ? '' : 'WHERE `Treffen` = 1 ') . '
        ORDER BY `ID`
        DESC LIMIT ' . (int)$display_news
    );

    $output = make_atom_entries_from_news($news);

    header('Content-Type: application/atom+xml; charset=utf-8');
    header('Content-Length: ' . strlen($output));
    raw_output($output);
}

/**
 * @param array[] $news_entries
 * @return string
 */
function make_atom_entries_from_news($news_entries)
{
    $html = '<?xml version="1.0" encoding="utf-8"?>
  <feed xmlns="http://www.w3.org/2005/Atom">
  <title>Engelsystem</title>
  <id>' . $_SERVER['HTTP_HOST']
        . htmlspecialchars(preg_replace(
            '#[&?]key=[a-f\d]{32}#',
            '',
            $_SERVER['REQUEST_URI']
        ))
        . '</id>
  <updated>' . date('Y-m-d\TH:i:sP', $news_entries[0]['Datum']) . '</updated>' . "\n";
    foreach ($news_entries as $news_entry) {
        $html .= make_atom_entry_from_news($news_entry);
    }
    $html .= '</feed>';
    return $html;
}

function make_atom_entry_from_news($news_entry)
{
    return '  <entry>
    <title>' . htmlspecialchars($news_entry['Betreff']) . '</title>
    <link href="' . page_link_to_absolute('news_comments&amp;nid=') . $news_entry['ID'] . '"/>
      <id>' . preg_replace('#^https?://#', '', page_link_to_absolute('news')) . '-' . $news_entry['ID'] . '</id>
      <updated>' . date('Y-m-d\TH:i:sP', $news_entry['Datum']) . '</updated>
    <summary type="html">' . htmlspecialchars($news_entry['Text']) . '</summary>
    </entry>' . "\n";
}