rssaction.php 14.3 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
 */

31
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
Evan Prodromou's avatar
Evan Prodromou committed
32

33 34
define('DEFAULT_RSS_LIMIT', 48);

35 36
class Rss10Action extends Action
{
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

44 45 46 47 48 49 50 51 52 53 54
    /**
     * Constructor
     *
     * Just wraps the Action constructor.
     *
     * @param string  $output URI to output to, default = stdout
     * @param boolean $indent Whether to indent output, default true
     *
     * @see Action::__construct
     */

55
    function __construct($output='php://output', $indent=null)
56 57 58
    {
        parent::__construct($output, $indent);
    }
59

60 61
    /**
     * Do we need to write to the database?
Evan Prodromou's avatar
Evan Prodromou committed
62
     *
63 64
     * @return boolean true
     */
Evan Prodromou's avatar
Evan Prodromou committed
65

66
    function isReadonly()
67
    {
68 69 70
        return true;
    }

71 72
    /**
     * Read arguments and initialize members
Evan Prodromou's avatar
Evan Prodromou committed
73
     *
74 75 76
     * @param array $args Arguments from $_REQUEST
     * @return boolean success
     */
Evan Prodromou's avatar
Evan Prodromou committed
77

78 79
    function prepare($args)
    {
Robin Millette's avatar
Robin Millette committed
80
        parent::prepare($args);
81

Robin Millette's avatar
Robin Millette committed
82
        $this->limit = (int) $this->trimmed('limit');
83

Robin Millette's avatar
Robin Millette committed
84 85 86
        if ($this->limit == 0) {
            $this->limit = DEFAULT_RSS_LIMIT;
        }
87 88 89 90

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

91
                // This header makes basic auth go
92
                header('WWW-Authenticate: Basic realm="StatusNet RSS"');
93

94
                // If the user hits cancel -- bam!
95 96 97 98 99 100 101
                $this->show_basic_auth_error();
                return;
            } else {
                $nickname = $_SERVER['PHP_AUTH_USER'];
                $password = $_SERVER['PHP_AUTH_PW'];

                if (!common_check_user($nickname, $password)) {
102
                    // basic authentication failed
103 104 105
                    list($proxy, $ip) = common_client_ip();

                    common_log(LOG_WARNING, "Failed RSS auth attempt, nickname = $nickname, proxy = $proxy, ip = $ip.");
106 107 108 109 110 111
                    $this->show_basic_auth_error();
                    return;
                }
            }
        }

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        return true;
    }

    /**
     * Handle a request
     *
     * @param array $args Arguments from $_REQUEST
     *
     * @return void
     */

    function handle($args)
    {
        // Parent handling, including cache check
        parent::handle($args);
127
        $this->showRss();
128 129
    }

130 131 132 133 134 135 136 137 138 139 140 141
    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();
    }

142
    /**
143
     * Get the notices to output in this stream.
Evan Prodromou's avatar
Evan Prodromou committed
144
     *
145 146
     * @return array an array of Notice objects sorted in reverse chron
     */
Evan Prodromou's avatar
Evan Prodromou committed
147

148
    function getNotices()
149
    {
150 151 152
        return array();
    }

153 154
    /**
     * Get a description of the channel
Evan Prodromou's avatar
Evan Prodromou committed
155 156 157 158 159
     *
     * Returns an array with the following
     * @return array
     */

Robin Millette's avatar
Robin Millette committed
160
    function getChannel()
161
    {
162 163 164 165 166 167
        return array('url' => '',
                     'title' => '',
                     'link' => '',
                     'description' => '');
    }

Robin Millette's avatar
Robin Millette committed
168
    function getImage()
169
    {
Evan Prodromou's avatar
Evan Prodromou committed
170
        return null;
171 172
    }

173
    function showRss()
174
    {
Robin Millette's avatar
Robin Millette committed
175
        $this->initRss();
176
        $this->showChannel();
Robin Millette's avatar
Robin Millette committed
177
        $this->showImage();
178

179 180
        if (count($this->notices)) {
            foreach ($this->notices as $n) {
181 182 183 184 185 186 187
                try {
                    $this->showItem($n);
                } catch (Exception $e) {
                    // log exceptions and continue
                    common_log(LOG_ERR, $e->getMessage());
                    continue;
                }
188
            }
189 190
        }

Robin Millette's avatar
Robin Millette committed
191 192
        $this->showCreators();
        $this->endRss();
193 194
    }

195
    function showChannel()
196
    {
197

Robin Millette's avatar
Robin Millette committed
198 199
        $channel = $this->getChannel();
        $image = $this->getImage();
200

Robin Millette's avatar
Robin Millette committed
201 202 203 204 205
        $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')));
206 207

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

Robin Millette's avatar
Robin Millette committed
211 212
        $this->elementStart('items');
        $this->elementStart('rdf:Seq');
213

214 215 216 217
        if (count($this->notices)) {
            foreach ($this->notices as $notice) {
                $this->element('rdf:li', array('rdf:resource' => $notice->uri));
            }
218 219
        }

Robin Millette's avatar
Robin Millette committed
220 221
        $this->elementEnd('rdf:Seq');
        $this->elementEnd('items');
222

Robin Millette's avatar
Robin Millette committed
223
        $this->elementEnd('channel');
224 225
    }

Robin Millette's avatar
Robin Millette committed
226
    function showImage()
227
    {
Robin Millette's avatar
Robin Millette committed
228
        $image = $this->getImage();
229
        if ($image) {
Robin Millette's avatar
Robin Millette committed
230
            $channel = $this->getChannel();
231 232 233 234 235
            $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');
236 237 238
        }
    }

Robin Millette's avatar
Robin Millette committed
239
    function showItem($notice)
240
    {
241
        $profile = $notice->getProfile();
242 243
        $nurl = common_local_url('shownotice', array('notice' => $notice->id));
        $creator_uri = common_profile_uri($profile);
244 245
        $this->elementStart('item', array('rdf:about' => $notice->uri,
                            'rdf:type' => 'http://rdfs.org/sioc/types#MicroblogPost'));
246
        $title = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
Robin Millette's avatar
Robin Millette committed
247 248 249
        $this->element('title', null, $title);
        $this->element('link', null, $nurl);
        $this->element('description', null, $profile->nickname."'s status on ".common_exact_date($notice->created));
250 251 252
        if ($notice->rendered) {
            $this->element('content:encoded', null, common_xml_safe_str($notice->rendered));
        }
Robin Millette's avatar
Robin Millette committed
253 254
        $this->element('dc:date', null, common_date_w3dtf($notice->created));
        $this->element('dc:creator', null, ($profile->fullname) ? $profile->fullname : $profile->nickname);
255 256
        $this->element('foaf:maker', array('rdf:resource' => $creator_uri));
        $this->element('sioc:has_creator', array('rdf:resource' => $creator_uri.'#acct'));
Craig Andrews's avatar
Craig Andrews committed
257 258 259 260 261 262 263 264 265 266
        $location = $notice->getLocation();
        if ($location && 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);
        }
267
        $this->element('statusnet:postIcon', array('rdf:resource' => $profile->avatarUrl()));
Robin Millette's avatar
Robin Millette committed
268
        $this->element('cc:licence', array('rdf:resource' => common_config('license', 'url')));
269
        if ($notice->reply_to) {
Sarven Capadisli's avatar
Sarven Capadisli committed
270
            $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
271
            $this->element('sioc:reply_of', array('rdf:resource' => $replyurl));
272
        }
273 274 275 276 277
        if (!empty($notice->conversation)) {
            $conversationurl = common_local_url('conversation',
                                         array('id' => $notice->conversation));
            $this->element('sioc:has_discussion', array('rdf:resource' => $conversationurl));
        }
278 279 280
        $attachments = $notice->attachments();
        if($attachments){
            foreach($attachments as $attachment){
281 282
                $enclosure=$attachment->getEnclosure();
                if ($enclosure) {
283
                    $attribs = array('rdf:resource' => $enclosure->url);
284 285
                    if ($enclosure->title) {
                        $attribs['dc:title'] = $enclosure->title;
286
                    }
287 288
                    if ($enclosure->modified) {
                        $attribs['dc:date'] = common_date_w3dtf($enclosure->modified);
289
                    }
290
                    if ($enclosure->size) {
291
                        $attribs['enc:length'] = $enclosure->size;
292
                    }
293
                    if ($enclosure->mimetype) {
294
                        $attribs['enc:type'] = $enclosure->mimetype;
295
                    }
296
                    $this->element('enc:enclosure', $attribs);
297 298
                }
                $this->element('sioc:links_to', array('rdf:resource'=>$attachment->url));
299 300
            }
        }
301 302 303 304 305 306 307

        $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));
308 309 310 311 312 313

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

314
                $tagrss  = common_local_url('tagrss', array('tag' => $tag->tag));
315
                $this->elementStart('ctag:tagged');
316
                $this->elementStart('ctag:Tag', array('rdf:about'=>$tagpage.'#concept', 'ctag:label'=>$tag->tag));
317 318 319 320
                $this->element('foaf:page', array('rdf:resource'=>$tagpage));
                $this->element('rdfs:seeAlso', array('rdf:resource'=>$tagrss));
                $this->elementEnd('ctag:Tag');
                $this->elementEnd('ctag:tagged');
321

322
                $this->tags_already_output[] = $tag->tag;
323 324
            }
        }
Robin Millette's avatar
Robin Millette committed
325
        $this->elementEnd('item');
326 327 328
        $this->creators[$creator_uri] = $profile;
    }

Robin Millette's avatar
Robin Millette committed
329
    function showCreators()
330
    {
331 332 333
        foreach ($this->creators as $uri => $profile) {
            $id = $profile->id;
            $nickname = $profile->nickname;
334
            $this->elementStart('foaf:Agent', array('rdf:about' => $uri));
Robin Millette's avatar
Robin Millette committed
335
            $this->element('foaf:nick', null, $nickname);
336
            if ($profile->fullname) {
Robin Millette's avatar
Robin Millette committed
337
                $this->element('foaf:name', null, $profile->fullname);
338
            }
339
            $this->element('foaf:holdsAccount', array('rdf:resource' => $uri.'#acct'));
340
            $avatar = $profile->avatarUrl();
341 342
            $this->element('foaf:depiction', array('rdf:resource' => $avatar));
            $this->elementEnd('foaf:Agent');
343 344 345
        }
    }

Robin Millette's avatar
Robin Millette committed
346
    function initRss()
347
    {
Robin Millette's avatar
Robin Millette committed
348
        $channel = $this->getChannel();
349 350
        header('Content-Type: application/rdf+xml');

Robin Millette's avatar
Robin Millette committed
351 352
        $this->startXml();
        $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
353 354 355 356
                                              'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
                                              'xmlns:dc' =>
                                              'http://purl.org/dc/elements/1.1/',
                                              'xmlns:cc' =>
357
                                              'http://creativecommons.org/ns#',
358 359
                                              'xmlns:content' =>
                                              'http://purl.org/rss/1.0/modules/content/',
360 361
                                              'xmlns:ctag' =>
                                              'http://commontag.org/ns#',
362 363
                                              'xmlns:foaf' =>
                                              'http://xmlns.com/foaf/0.1/',
364 365
                                              'xmlns:enc' =>
                                              'http://purl.oclc.org/net/rss_2.0/enc#',
366 367 368 369
                                              'xmlns:sioc' =>
                                              'http://rdfs.org/sioc/ns#',
                                              'xmlns:sioct' =>
                                              'http://rdfs.org/sioc/types#',
370 371
                                              'xmlns:rdfs' =>
                                              'http://www.w3.org/2000/01/rdf-schema#',
Craig Andrews's avatar
Craig Andrews committed
372 373
                                              'xmlns:geo' =>
                                              'http://www.w3.org/2003/01/geo/wgs84_pos#',
374
                                              'xmlns:statusnet' =>
375
                                              'http://status.net/ont/',
376
                                              'xmlns' => 'http://purl.org/rss/1.0/'));
Robin Millette's avatar
Robin Millette committed
377 378
        $this->elementStart('sioc:Site', array('rdf:about' => common_root_url()));
        $this->element('sioc:name', null, common_config('site', 'name'));
379
        $this->elementStart('sioc:space_of');
Robin Millette's avatar
Robin Millette committed
380
        $this->element('sioc:Container', array('rdf:about' =>
381
                                               $channel['url']));
382
        $this->elementEnd('sioc:space_of');
Robin Millette's avatar
Robin Millette committed
383
        $this->elementEnd('sioc:Site');
384 385
    }

Robin Millette's avatar
Robin Millette committed
386
    function endRss()
387
    {
Robin Millette's avatar
Robin Millette committed
388
        $this->elementEnd('rdf:RDF');
389
    }
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405

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

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

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

406
        // FIXME: doesn't handle modified profiles, avatars, deleted notices
407 408 409

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