3 * StatusNet, the distributed open-source microblogging tool
5 * Base class for RSS 1.0 feed actions
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
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/
31 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
33 define('DEFAULT_RSS_LIMIT', 48);
35 class Rss10Action extends Action
37 # This will contain the details of each feed item's author and be used to generate SIOC data.
39 var $creators = array();
40 var $limit = DEFAULT_RSS_LIMIT;
42 var $tags_already_output = array();
47 * Just wraps the Action constructor.
49 * @param string $output URI to output to, default = stdout
50 * @param boolean $indent Whether to indent output, default true
52 * @see Action::__construct
55 function __construct($output='php://output', $indent=null)
57 parent::__construct($output, $indent);
61 * Do we need to write to the database?
63 * @return boolean true
72 * Read arguments and initialize members
74 * @param array $args Arguments from $_REQUEST
75 * @return boolean success
78 function prepare($args)
80 parent::prepare($args);
82 $this->limit = (int) $this->trimmed('limit');
84 if ($this->limit == 0) {
85 $this->limit = DEFAULT_RSS_LIMIT;
88 if (common_config('site', 'private')) {
89 if (!isset($_SERVER['PHP_AUTH_USER'])) {
91 # This header makes basic auth go
92 header('WWW-Authenticate: Basic realm="StatusNet RSS"');
94 # If the user hits cancel -- bam!
95 $this->show_basic_auth_error();
98 $nickname = $_SERVER['PHP_AUTH_USER'];
99 $password = $_SERVER['PHP_AUTH_PW'];
101 if (!common_check_user($nickname, $password)) {
102 # basic authentication failed
103 list($proxy, $ip) = common_client_ip();
105 common_log(LOG_WARNING, "Failed RSS auth attempt, nickname = $nickname, proxy = $proxy, ip = $ip.");
106 $this->show_basic_auth_error();
118 * @param array $args Arguments from $_REQUEST
123 function handle($args)
125 // Parent handling, including cache check
126 parent::handle($args);
130 function show_basic_auth_error()
132 header('HTTP/1.1 401 Unauthorized');
133 header('Content-Type: application/xml; charset=utf-8');
135 $this->elementStart('hash');
136 $this->element('error', null, 'Could not authenticate you.');
137 $this->element('request', null, $_SERVER['REQUEST_URI']);
138 $this->elementEnd('hash');
143 * Get the notices to output in this stream.
145 * @return array an array of Notice objects sorted in reverse chron
148 function getNotices()
154 * Get a description of the channel
156 * Returns an array with the following
160 function getChannel()
162 return array('url' => '',
165 'description' => '');
176 $this->showChannel();
179 if (count($this->notices)) {
180 foreach ($this->notices as $n) {
185 $this->showCreators();
189 function showChannel()
192 $channel = $this->getChannel();
193 $image = $this->getImage();
195 $this->elementStart('channel', array('rdf:about' => $channel['url']));
196 $this->element('title', null, $channel['title']);
197 $this->element('link', null, $channel['link']);
198 $this->element('description', null, $channel['description']);
199 $this->element('cc:licence', array('rdf:resource' => common_config('license','url')));
202 $this->element('image', array('rdf:resource' => $image));
205 $this->elementStart('items');
206 $this->elementStart('rdf:Seq');
208 if (count($this->notices)) {
209 foreach ($this->notices as $notice) {
210 $this->element('rdf:li', array('rdf:resource' => $notice->uri));
214 $this->elementEnd('rdf:Seq');
215 $this->elementEnd('items');
217 $this->elementEnd('channel');
222 $image = $this->getImage();
224 $channel = $this->getChannel();
225 $this->elementStart('image', array('rdf:about' => $image));
226 $this->element('title', null, $channel['title']);
227 $this->element('link', null, $channel['link']);
228 $this->element('url', null, $image);
229 $this->elementEnd('image');
233 function showItem($notice)
235 $profile = Profile::staticGet($notice->profile_id);
236 $nurl = common_local_url('shownotice', array('notice' => $notice->id));
237 $creator_uri = common_profile_uri($profile);
238 $this->elementStart('item', array('rdf:about' => $notice->uri,
239 'rdf:type' => 'http://rdfs.org/sioc/types#MicroblogPost'));
240 $title = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
241 $this->element('title', null, $title);
242 $this->element('link', null, $nurl);
243 $this->element('description', null, $profile->nickname."'s status on ".common_exact_date($notice->created));
244 if ($notice->rendered) {
245 $this->element('content:encoded', null, common_xml_safe_str($notice->rendered));
247 $this->element('dc:date', null, common_date_w3dtf($notice->created));
248 $this->element('dc:creator', null, ($profile->fullname) ? $profile->fullname : $profile->nickname);
249 $this->element('foaf:maker', array('rdf:resource' => $creator_uri));
250 $this->element('sioc:has_creator', array('rdf:resource' => $creator_uri.'#acct'));
251 $location = $notice->getLocation();
252 if ($location && isset($location->lat) && isset($location->lon)) {
253 $location_uri = $location->getRdfURL();
254 $attrs = array('geo:lat' => $location->lat,
255 'geo:long' => $location->lon);
256 if (strlen($location_uri)) {
257 $attrs['rdf:resource'] = $location_uri;
259 $this->element('statusnet:origin', $attrs);
261 $this->element('statusnet:postIcon', array('rdf:resource' => $profile->avatarUrl()));
262 $this->element('cc:licence', array('rdf:resource' => common_config('license', 'url')));
263 if ($notice->reply_to) {
264 $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
265 $this->element('sioc:reply_of', array('rdf:resource' => $replyurl));
267 if (!empty($notice->conversation)) {
268 $conversationurl = common_local_url('conversation',
269 array('id' => $notice->conversation));
270 $this->element('sioc:has_discussion', array('rdf:resource' => $conversationurl));
272 $attachments = $notice->attachments();
274 foreach($attachments as $attachment){
275 $enclosure=$attachment->getEnclosure();
277 $attribs = array('rdf:resource' => $enclosure->url);
278 if ($enclosure->title) {
279 $attribs['dc:title'] = $enclosure->title;
281 if ($enclosure->modified) {
282 $attribs['dc:date'] = common_date_w3dtf($enclosure->modified);
284 if ($enclosure->size) {
285 $attribs['enc:length'] = $enclosure->size;
287 if ($enclosure->mimetype) {
288 $attribs['enc:type'] = $enclosure->mimetype;
290 $this->element('enc:enclosure', $attribs);
292 $this->element('sioc:links_to', array('rdf:resource'=>$attachment->url));
296 $tag = new Notice_tag();
297 $tag->notice_id = $notice->id;
299 $entry['tags']=array();
300 while ($tag->fetch()) {
301 $tagpage = common_local_url('tag', array('tag' => $tag->tag));
303 if ( in_array($tag, $this->tags_already_output) ) {
304 $this->element('ctag:tagged', array('rdf:resource'=>$tagpage.'#concept'));
308 $tagrss = common_local_url('tagrss', array('tag' => $tag->tag));
309 $this->elementStart('ctag:tagged');
310 $this->elementStart('ctag:Tag', array('rdf:about'=>$tagpage.'#concept', 'ctag:label'=>$tag->tag));
311 $this->element('foaf:page', array('rdf:resource'=>$tagpage));
312 $this->element('rdfs:seeAlso', array('rdf:resource'=>$tagrss));
313 $this->elementEnd('ctag:Tag');
314 $this->elementEnd('ctag:tagged');
316 $this->tags_already_output[] = $tag->tag;
319 $this->elementEnd('item');
320 $this->creators[$creator_uri] = $profile;
323 function showCreators()
325 foreach ($this->creators as $uri => $profile) {
327 $nickname = $profile->nickname;
328 $this->elementStart('foaf:Agent', array('rdf:about' => $uri));
329 $this->element('foaf:nick', null, $nickname);
330 if ($profile->fullname) {
331 $this->element('foaf:name', null, $profile->fullname);
333 $this->element('foaf:holdsAccount', array('rdf:resource' => $uri.'#acct'));
334 $avatar = $profile->avatarUrl();
335 $this->element('foaf:depiction', array('rdf:resource' => $avatar));
336 $this->elementEnd('foaf:Agent');
342 $channel = $this->getChannel();
343 header('Content-Type: application/rdf+xml');
346 $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
347 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
349 'http://purl.org/dc/elements/1.1/',
351 'http://creativecommons.org/ns#',
353 'http://purl.org/rss/1.0/modules/content/',
355 'http://commontag.org/ns#',
357 'http://xmlns.com/foaf/0.1/',
359 'http://purl.oclc.org/net/rss_2.0/enc#',
361 'http://rdfs.org/sioc/ns#',
363 'http://rdfs.org/sioc/types#',
365 'http://www.w3.org/2000/01/rdf-schema#',
367 'http://www.w3.org/2003/01/geo/wgs84_pos#',
369 'http://status.net/ont/',
370 'xmlns' => 'http://purl.org/rss/1.0/'));
371 $this->elementStart('sioc:Site', array('rdf:about' => common_root_url()));
372 $this->element('sioc:name', null, common_config('site', 'name'));
373 $this->elementStart('sioc:space_of');
374 $this->element('sioc:Container', array('rdf:about' =>
376 $this->elementEnd('sioc:space_of');
377 $this->elementEnd('sioc:Site');
382 $this->elementEnd('rdf:RDF');
386 * When was this page last modified?
390 function lastModified()
392 if (empty($this->notices)) {
396 if (count($this->notices) == 0) {
400 // FIXME: doesn't handle modified profiles, avatars, deleted notices
402 return strtotime($this->notices[0]->created);