We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

rss10action.php 13.9 KB
Newer Older
Evan Prodromou's avatar
Evan Prodromou committed
1
<?php
2
/**
3
 * StatusNet, the distributed open-source microblogging tool
Evan Prodromou's avatar
Evan Prodromou committed
4
 *
5 6 7 8 9
 * Base class for RSS 1.0 feed actions
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
Evan Prodromou's avatar
Evan Prodromou committed
10 11 12 13 14 15 16 17 18 19 20
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 22
 *
 * @category  Mail
23
 * @package   StatusNet
24
 * @author    Evan Prodromou <evan@status.net>
25
 * @author    Earle Martin <earle@downlode.org>
26
 * @copyright 2008-9 StatusNet, Inc.
27
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28
 * @link      http://status.net/
Evan Prodromou's avatar
Evan Prodromou committed
29 30
 */

mattl's avatar
mattl committed
31
if (!defined('GNUSOCIAL')) { exit(1); }
Evan Prodromou's avatar
Evan Prodromou committed
32

33 34
define('DEFAULT_RSS_LIMIT', 48);

mattl's avatar
mattl committed
35
class Rss10Action extends ManagedAction
36
{
37
    // This will contain the details of each feed item's author and be used to generate SIOC data.
Evan Prodromou's avatar
Evan Prodromou committed
38

39
    var $creators = array();
40
    var $limit = DEFAULT_RSS_LIMIT;
41
    var $notices = null;
42
    var $tags_already_output = array();
Evan Prodromou's avatar
Evan Prodromou committed
43

mattl's avatar
mattl committed
44
    public function isReadOnly($args)
45
    {
46 47 48
        return true;
    }

mattl's avatar
mattl committed
49
    protected function doPreparation()
50
    {
mattl's avatar
mattl committed
51
        $this->limit = $this->int('limit');
52

53
        if (empty($this->limit)) {
Robin Millette's avatar
Robin Millette committed
54 55
            $this->limit = DEFAULT_RSS_LIMIT;
        }
56 57 58 59

        if (common_config('site', 'private')) {
            if (!isset($_SERVER['PHP_AUTH_USER'])) {

60
                // This header makes basic auth go
61
                header('WWW-Authenticate: Basic realm="GNU social RSS"');
62

63
                // If the user hits cancel -- bam!
64
                $this->show_basic_auth_error();
mattl's avatar
mattl committed
65
                // the above calls 'exit'
66 67 68 69 70
            } else {
                $nickname = $_SERVER['PHP_AUTH_USER'];
                $password = $_SERVER['PHP_AUTH_PW'];

                if (!common_check_user($nickname, $password)) {
71
                    // basic authentication failed
72 73 74
                    list($proxy, $ip) = common_client_ip();

                    common_log(LOG_WARNING, "Failed RSS auth attempt, nickname = $nickname, proxy = $proxy, ip = $ip.");
75
                    $this->show_basic_auth_error();
76
                    // the above calls 'exit'
77 78 79
                }
            }
        }
80 81 82 83 84 85 86 87 88

        $this->doStreamPreparation();

        $this->notices = $this->getNotices($this->limit);
    }

    protected function doStreamPreparation()
    {
        // for example if we need to set $this->target or something
89 90
    }

91 92 93 94 95 96 97 98 99 100
    function show_basic_auth_error()
    {
        header('HTTP/1.1 401 Unauthorized');
        header('Content-Type: application/xml; charset=utf-8');
        $this->startXML();
        $this->elementStart('hash');
        $this->element('error', null, 'Could not authenticate you.');
        $this->element('request', null, $_SERVER['REQUEST_URI']);
        $this->elementEnd('hash');
        $this->endXML();
mattl's avatar
mattl committed
101
        exit;
102 103
    }

104
    /**
105
     * Get the notices to output in this stream.
Evan Prodromou's avatar
Evan Prodromou committed
106
     *
107 108
     * @return array an array of Notice objects sorted in reverse chron
     */
Evan Prodromou's avatar
Evan Prodromou committed
109

110
    protected function getNotices()
111
    {
112 113 114
        return array();
    }

115 116
    /**
     * Get a description of the channel
Evan Prodromou's avatar
Evan Prodromou committed
117 118 119 120 121
     *
     * Returns an array with the following
     * @return array
     */

Robin Millette's avatar
Robin Millette committed
122
    function getChannel()
123
    {
124 125 126 127 128 129
        return array('url' => '',
                     'title' => '',
                     'link' => '',
                     'description' => '');
    }

Robin Millette's avatar
Robin Millette committed
130
    function getImage()
131
    {
Evan Prodromou's avatar
Evan Prodromou committed
132
        return null;
133 134
    }

mattl's avatar
mattl committed
135
    function showPage()
136
    {
Robin Millette's avatar
Robin Millette committed
137
        $this->initRss();
138
        $this->showChannel();
Robin Millette's avatar
Robin Millette committed
139
        $this->showImage();
140

141 142
        if (count($this->notices)) {
            foreach ($this->notices as $n) {
143 144 145 146 147 148 149
                try {
                    $this->showItem($n);
                } catch (Exception $e) {
                    // log exceptions and continue
                    common_log(LOG_ERR, $e->getMessage());
                    continue;
                }
150
            }
151 152
        }

Robin Millette's avatar
Robin Millette committed
153 154
        $this->showCreators();
        $this->endRss();
155 156
    }

157
    function showChannel()
158
    {
159

Robin Millette's avatar
Robin Millette committed
160 161
        $channel = $this->getChannel();
        $image = $this->getImage();
162

Robin Millette's avatar
Robin Millette committed
163 164 165 166 167
        $this->elementStart('channel', array('rdf:about' => $channel['url']));
        $this->element('title', null, $channel['title']);
        $this->element('link', null, $channel['link']);
        $this->element('description', null, $channel['description']);
        $this->element('cc:licence', array('rdf:resource' => common_config('license','url')));
168 169

        if ($image) {
Robin Millette's avatar
Robin Millette committed
170
            $this->element('image', array('rdf:resource' => $image));
171 172
        }

Robin Millette's avatar
Robin Millette committed
173 174
        $this->elementStart('items');
        $this->elementStart('rdf:Seq');
175

176 177 178 179
        if (count($this->notices)) {
            foreach ($this->notices as $notice) {
                $this->element('rdf:li', array('rdf:resource' => $notice->uri));
            }
180 181
        }

Robin Millette's avatar
Robin Millette committed
182 183
        $this->elementEnd('rdf:Seq');
        $this->elementEnd('items');
184

Robin Millette's avatar
Robin Millette committed
185
        $this->elementEnd('channel');
186 187
    }

Robin Millette's avatar
Robin Millette committed
188
    function showImage()
189
    {
Robin Millette's avatar
Robin Millette committed
190
        $image = $this->getImage();
191
        if ($image) {
Robin Millette's avatar
Robin Millette committed
192
            $channel = $this->getChannel();
193 194 195 196 197
            $this->elementStart('image', array('rdf:about' => $image));
            $this->element('title', null, $channel['title']);
            $this->element('link', null, $channel['link']);
            $this->element('url', null, $image);
            $this->elementEnd('image');
198 199 200
        }
    }

Robin Millette's avatar
Robin Millette committed
201
    function showItem($notice)
202
    {
203
        $profile = $notice->getProfile();
204 205
        $nurl = common_local_url('shownotice', array('notice' => $notice->id));
        $creator_uri = common_profile_uri($profile);
206 207
        $this->elementStart('item', array('rdf:about' => $notice->uri,
                            'rdf:type' => 'http://rdfs.org/sioc/types#MicroblogPost'));
208
        $title = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
Robin Millette's avatar
Robin Millette committed
209 210 211
        $this->element('title', null, $title);
        $this->element('link', null, $nurl);
        $this->element('description', null, $profile->nickname."'s status on ".common_exact_date($notice->created));
212 213
        if ($notice->getRendered()) {
            $this->element('content:encoded', null, common_xml_safe_str($notice->getRendered()));
214
        }
Robin Millette's avatar
Robin Millette committed
215 216
        $this->element('dc:date', null, common_date_w3dtf($notice->created));
        $this->element('dc:creator', null, ($profile->fullname) ? $profile->fullname : $profile->nickname);
217 218
        $this->element('foaf:maker', array('rdf:resource' => $creator_uri));
        $this->element('sioc:has_creator', array('rdf:resource' => $creator_uri.'#acct'));
219 220 221 222 223 224 225 226 227 228
        try {
            $location = Notice_location::locFromStored($notice);
            if (isset($location->lat) && isset($location->lon)) {
                $location_uri = $location->getRdfURL();
                $attrs = array('geo:lat' => $location->lat,
                               'geo:long' => $location->lon);
                if (strlen($location_uri)) {
                    $attrs['rdf:resource'] = $location_uri;
                }
                $this->element('statusnet:origin', $attrs);
Craig Andrews's avatar
Craig Andrews committed
229
            }
230 231
        } catch (ServerException $e) {
            // No result, so no location data
Craig Andrews's avatar
Craig Andrews committed
232
        }
233
        $this->element('statusnet:postIcon', array('rdf:resource' => $profile->avatarUrl()));
Robin Millette's avatar
Robin Millette committed
234
        $this->element('cc:licence', array('rdf:resource' => common_config('license', 'url')));
235
        if ($notice->reply_to) {
Sarven Capadisli's avatar
Sarven Capadisli committed
236
            $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
237
            $this->element('sioc:reply_of', array('rdf:resource' => $replyurl));
238
        }
239 240 241 242 243
        if (!empty($notice->conversation)) {
            $conversationurl = common_local_url('conversation',
                                         array('id' => $notice->conversation));
            $this->element('sioc:has_discussion', array('rdf:resource' => $conversationurl));
        }
244 245 246
        $attachments = $notice->attachments();
        if($attachments){
            foreach($attachments as $attachment){
247 248
                try {
                    $enclosure = $attachment->getEnclosure();
249
                    $attribs = array('rdf:resource' => $enclosure->url);
250 251
                    if ($enclosure->title) {
                        $attribs['dc:title'] = $enclosure->title;
252
                    }
253 254
                    if ($enclosure->modified) {
                        $attribs['dc:date'] = common_date_w3dtf($enclosure->modified);
255
                    }
256
                    if ($enclosure->size) {
257
                        $attribs['enc:length'] = $enclosure->size;
258
                    }
259
                    if ($enclosure->mimetype) {
260
                        $attribs['enc:type'] = $enclosure->mimetype;
261
                    }
262
                    $this->element('enc:enclosure', $attribs);
263 264
                } catch (ServerException $e) {
                    // There was not enough metadata available
265 266
                }
                $this->element('sioc:links_to', array('rdf:resource'=>$attachment->url));
267 268
            }
        }
269 270 271 272 273 274 275

        $tag = new Notice_tag();
        $tag->notice_id = $notice->id;
        if ($tag->find()) {
            $entry['tags']=array();
            while ($tag->fetch()) {
                $tagpage = common_local_url('tag', array('tag' => $tag->tag));
276 277 278 279 280 281

                if ( in_array($tag, $this->tags_already_output) ) {
                    $this->element('ctag:tagged', array('rdf:resource'=>$tagpage.'#concept'));
                    continue;
                }

282
                $tagrss  = common_local_url('tagrss', array('tag' => $tag->tag));
283
                $this->elementStart('ctag:tagged');
284
                $this->elementStart('ctag:Tag', array('rdf:about'=>$tagpage.'#concept', 'ctag:label'=>$tag->tag));
285 286 287 288
                $this->element('foaf:page', array('rdf:resource'=>$tagpage));
                $this->element('rdfs:seeAlso', array('rdf:resource'=>$tagrss));
                $this->elementEnd('ctag:Tag');
                $this->elementEnd('ctag:tagged');
289

290
                $this->tags_already_output[] = $tag->tag;
291 292
            }
        }
Robin Millette's avatar
Robin Millette committed
293
        $this->elementEnd('item');
294 295 296
        $this->creators[$creator_uri] = $profile;
    }

Robin Millette's avatar
Robin Millette committed
297
    function showCreators()
298
    {
299 300 301
        foreach ($this->creators as $uri => $profile) {
            $id = $profile->id;
            $nickname = $profile->nickname;
302
            $this->elementStart('foaf:Agent', array('rdf:about' => $uri));
Robin Millette's avatar
Robin Millette committed
303
            $this->element('foaf:nick', null, $nickname);
304
            if ($profile->fullname) {
Robin Millette's avatar
Robin Millette committed
305
                $this->element('foaf:name', null, $profile->fullname);
306
            }
307
            $this->element('foaf:holdsAccount', array('rdf:resource' => $uri.'#acct'));
308
            $avatar = $profile->avatarUrl();
309 310
            $this->element('foaf:depiction', array('rdf:resource' => $avatar));
            $this->elementEnd('foaf:Agent');
311 312 313
        }
    }

Robin Millette's avatar
Robin Millette committed
314
    function initRss()
315
    {
Robin Millette's avatar
Robin Millette committed
316
        $channel = $this->getChannel();
317 318
        header('Content-Type: application/rdf+xml');

Robin Millette's avatar
Robin Millette committed
319 320
        $this->startXml();
        $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
321 322 323 324
                                              'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
                                              'xmlns:dc' =>
                                              'http://purl.org/dc/elements/1.1/',
                                              'xmlns:cc' =>
325
                                              'http://creativecommons.org/ns#',
326 327
                                              'xmlns:content' =>
                                              'http://purl.org/rss/1.0/modules/content/',
328 329
                                              'xmlns:ctag' =>
                                              'http://commontag.org/ns#',
330 331
                                              'xmlns:foaf' =>
                                              'http://xmlns.com/foaf/0.1/',
332 333
                                              'xmlns:enc' =>
                                              'http://purl.oclc.org/net/rss_2.0/enc#',
334 335 336 337
                                              'xmlns:sioc' =>
                                              'http://rdfs.org/sioc/ns#',
                                              'xmlns:sioct' =>
                                              'http://rdfs.org/sioc/types#',
338 339
                                              'xmlns:rdfs' =>
                                              'http://www.w3.org/2000/01/rdf-schema#',
Craig Andrews's avatar
Craig Andrews committed
340 341
                                              'xmlns:geo' =>
                                              'http://www.w3.org/2003/01/geo/wgs84_pos#',
342
                                              'xmlns:statusnet' =>
343
                                              'http://status.net/ont/',
344
                                              'xmlns' => 'http://purl.org/rss/1.0/'));
Robin Millette's avatar
Robin Millette committed
345 346
        $this->elementStart('sioc:Site', array('rdf:about' => common_root_url()));
        $this->element('sioc:name', null, common_config('site', 'name'));
347
        $this->elementStart('sioc:space_of');
Robin Millette's avatar
Robin Millette committed
348
        $this->element('sioc:Container', array('rdf:about' =>
349
                                               $channel['url']));
350
        $this->elementEnd('sioc:space_of');
Robin Millette's avatar
Robin Millette committed
351
        $this->elementEnd('sioc:Site');
352 353
    }

Robin Millette's avatar
Robin Millette committed
354
    function endRss()
355
    {
Robin Millette's avatar
Robin Millette committed
356
        $this->elementEnd('rdf:RDF');
357
    }
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

    /**
     * When was this page last modified?
     *
     */

    function lastModified()
    {
        if (empty($this->notices)) {
            return null;
        }

        if (count($this->notices) == 0) {
            return null;
        }

374
        // FIXME: doesn't handle modified profiles, avatars, deleted notices
375 376 377

        return strtotime($this->notices[0]->created);
    }
Evan Prodromou's avatar
Evan Prodromou committed
378
}
Robin Millette's avatar
Robin Millette committed
379