atom.php 6.17 KB
Newer Older
1
2
3
4
5
6
<?php
/**
 *
 * @package    mahara
 * @subpackage artefact-blog
 * @author     Catalyst IT Ltd
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
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
 *
 */

define('INTERNAL', 1);
define('PUBLIC', 1);

require(dirname(dirname(dirname(__FILE__))) . '/init.php');

define('POSTCOUNT', 20); // number of posts to include in the feed

function atom_date($date) {
    $date = str_replace(' ', 'T', $date);
    $date .= date('P');
    return $date;
}

function generate_feed($feed, $posts) {
    $smarty = smarty();
    $smarty->assign('feed', $feed);
    $smarty->assign('posts', $posts);

    header("Content-Type: application/atom+xml");
    $smarty->display('artefact:blog:atom.xml.tpl');
}

function error_feed() {
    return array(
        'title' => get_string('accessdenied', 'error'),
        'link' => '',
        'selflink' => '',
        'id' => '',
        'description' => '',
        'ownername' => '',
        'updated' => '',
        'logo' => '',
    );
}

function error_post($message) {
    return array(
        0 => array(
            'title' => get_string('accessdenied', 'error'),
            'link' => '',
            'id' => '',
            'description' => $message,
            'mtime' => '',
        ));
}

$artefactid = param_integer('artefact');
$viewid     = param_integer('view');

require_once(get_config('docroot') . 'artefact/lib.php');
$artefact = artefact_instance_from_id($artefactid);

if (!can_view_view($viewid)) {
    generate_feed(error_feed(), error_post(''));
}
elseif (!artefact_in_view($artefactid, $viewid)) {
    generate_feed(error_feed(), error_post(get_string('artefactnotinview', 'error', $artefactid, $viewid)));
}
elseif (!$artefact->in_view_list()) {
    generate_feed(error_feed(), error_post(get_string('artefactonlyviewableinview', 'error')));
}
elseif ($artefact->get('artefacttype') != 'blog') {
    generate_feed(error_feed(), error_post(get_string('feedsnotavailable', 'artefact.blog')));
}
else {
    $owner = get_records_sql_array("
        SELECT a.mtime, u.id, u.firstname, u.lastname, u.profileicon
        FROM {usr} u, {artefact} a
        WHERE a.id = ?
        AND a.owner = u.id
        LIMIT 1;",
        array($artefactid));

    if ($owner[0]->profileicon) {
        $image = get_config('wwwroot') . 'thumb.php?type=profileiconbyid&maxsize=100&id='
            . $owner[0]->profileicon;
    }
    else {
        // use the Mahara logo
        $image = get_config('wwwroot') . 'theme/raw/static/images/site-logo.png';
    }

    // if the owner has a personal website set, use it as the author URI
    $personal_site = get_field('artefact', 'title', 'artefacttype', 'personalwebsite', 'owner', $owner[0]->id);

    $author = array(
        'name' => implode(' ', array($owner[0]->firstname, $owner[0]->lastname)),
99
        'uri' => sanitize_url($personal_site),
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    );
    $link = get_config('wwwroot') . 'view/artefact.php?artefact=' .
        $artefactid . '&view=' . $viewid;
    $selflink = get_config('wwwroot') . 'artefact/blog/atom.php?artefact=' .
        $artefactid . '&view=' . $viewid;

    $postids = get_records_sql_array("
        SELECT a.id, a.title, a.description, a.mtime
        FROM {artefact} a, {artefact_blog_blogpost} bp
        WHERE a.id = bp.blogpost
        AND a.parent = ?
        AND bp.published = 1
        ORDER BY a.ctime DESC
        LIMIT ?;",
        array($artefactid, POSTCOUNT));

    if ($postids) {
        $updated = $postids[0]->mtime;
    }
    else {
        $updated = $owner[0]->mtime;
    }

    $generator = array(
124
125
        'uri' => 'https://mahara.org',
        'version' => get_config('series'),
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
        'text' => 'Mahara',
    );

    $rights = get_string('feedrights', 'artefact.blog', substr($updated, 0, 4) . ' ' . $author['name']);

    // is there a Creative Commons block in this view?
    // if so, set the feed rights accordingly
    $ccblock = get_records_sql_array("
        SELECT b.id
        FROM {block_instance} b
        WHERE b.view = ?
        AND b.blocktype = 'creativecommons'
        LIMIT 1;", array($viewid));

    if ($ccblock) {
        require_once(get_config('docroot') . 'blocktype/lib.php');
        $ccblock_instance = new BlockInstance($ccblock[0]->id);
        $configdata = $ccblock_instance->get('configdata');
        $licensetype = $configdata['license'];
        $licenseurl = "http://creativecommons.org/licenses/$licensetype/3.0/";
        $licensename = get_string($licensetype, 'blocktype.creativecommons');
        $rights .= ' ' . get_string('licensestatement', 'blocktype.creativecommons', $licenseurl, $licensename);
    }

    $feed = array(
        'title' => $artefact->get('title'),
        'link' => $link,
        'selflink' => $selflink,
        'id' => implode(',', array(get_config('wwwroot'), $artefactid, $viewid)),
        'description' => $artefact->get('description'),
        'author' => $author,
        'updated' => atom_date($updated),
        'logo' => $image,
        'icon' => get_config('wwwroot') . 'favicon.ico',
        'generator' => $generator,
        'rights' => $rights,
    );

    $posts = array();
    if ($postids) {
        foreach ($postids as $postid) {
            $post = artefact_instance_from_id($postid->id);
            $attachments = $post->get_attachments();
            $attachlinks = array();
            foreach ($attachments as $attachment) {
                $attachlinks[] = array(
                    'link' => get_config('wwwroot') . 'artefact/file/download.php?file=' .
                        $attachment->id . '&view=' . $viewid,
                    'title' => $attachment->title,
                );
            }
            $posts[] = array(
                'title' => $post->get('title'),
                'link' => get_config('wwwroot') . 'view/artefact.php?artefact=' .
                    $post->get('id') . '&view=' . $viewid,
                'id' => implode(',', array(get_config('wwwroot'), $post->get('id'), $viewid)),
                'description' => $post->get('description'),
                'mtime' => atom_date($postid->mtime),
                'attachments' => $attachlinks,
            );
        }
    }

    generate_feed($feed, $posts);
}