]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/rssaction.php
change LACONICA to STATUSNET
[quix0rs-gnu-social.git] / lib / rssaction.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Base class for RSS 1.0 feed actions
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
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/
29  */
30
31 if (!defined('STATUSNET')) { exit(1); }
32
33 define('DEFAULT_RSS_LIMIT', 48);
34
35 class Rss10Action extends Action
36 {
37     # This will contain the details of each feed item's author and be used to generate SIOC data.
38
39     var $creators = array();
40     var $limit = DEFAULT_RSS_LIMIT;
41     var $notices = null;
42     var $tags_already_output = array();
43
44     /**
45      * Constructor
46      *
47      * Just wraps the Action constructor.
48      *
49      * @param string  $output URI to output to, default = stdout
50      * @param boolean $indent Whether to indent output, default true
51      *
52      * @see Action::__construct
53      */
54
55     function __construct($output='php://output', $indent=true)
56     {
57         parent::__construct($output, $indent);
58     }
59
60     /**
61      * Do we need to write to the database?
62      *
63      * @return boolean true
64      */
65
66     function isReadonly()
67     {
68         return true;
69     }
70
71     /**
72      * Read arguments and initialize members
73      *
74      * @param array $args Arguments from $_REQUEST
75      * @return boolean success
76      */
77
78     function prepare($args)
79     {
80         parent::prepare($args);
81         $this->limit = (int) $this->trimmed('limit');
82         if ($this->limit == 0) {
83             $this->limit = DEFAULT_RSS_LIMIT;
84         }
85         return true;
86     }
87
88     /**
89      * Handle a request
90      *
91      * @param array $args Arguments from $_REQUEST
92      *
93      * @return void
94      */
95
96     function handle($args)
97     {
98         // Parent handling, including cache check
99         parent::handle($args);
100
101         if (common_config('site', 'private')) {
102             if (!isset($_SERVER['PHP_AUTH_USER'])) {
103
104                 # This header makes basic auth go
105                 header('WWW-Authenticate: Basic realm="StatusNet RSS"');
106
107                 # If the user hits cancel -- bam!
108                 $this->show_basic_auth_error();
109                 return;
110             } else {
111                 $nickname = $_SERVER['PHP_AUTH_USER'];
112                 $password = $_SERVER['PHP_AUTH_PW'];
113
114                 if (!common_check_user($nickname, $password)) {
115                     # basic authentication failed
116                     list($proxy, $ip) = common_client_ip();
117
118                     common_log(LOG_WARNING, "Failed RSS auth attempt, nickname = $nickname, proxy = $proxy, ip = $ip.");
119                     $this->show_basic_auth_error();
120                     return;
121                 }
122             }
123         }
124
125         // Get the list of notices
126         $this->notices = $this->getNotices($this->limit);
127         $this->showRss();
128     }
129
130     function show_basic_auth_error()
131     {
132         header('HTTP/1.1 401 Unauthorized');
133         header('Content-Type: application/xml; charset=utf-8');
134         $this->startXML();
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');
139         $this->endXML();
140     }
141
142     /**
143      * Get the notices to output in this stream
144      *
145      * @return array an array of Notice objects sorted in reverse chron
146      */
147
148     function getNotices()
149     {
150         return array();
151     }
152
153     /**
154      * Get a description of the channel
155      *
156      * Returns an array with the following
157      * @return array
158      */
159
160     function getChannel()
161     {
162         return array('url' => '',
163                      'title' => '',
164                      'link' => '',
165                      'description' => '');
166     }
167
168     function getImage()
169     {
170         return null;
171     }
172
173     function showRss()
174     {
175         $this->initRss();
176         $this->showChannel();
177         $this->showImage();
178
179         foreach ($this->notices as $n) {
180             $this->showItem($n);
181         }
182
183         $this->showCreators();
184         $this->endRss();
185     }
186
187     function showChannel()
188     {
189
190         $channel = $this->getChannel();
191         $image = $this->getImage();
192
193         $this->elementStart('channel', array('rdf:about' => $channel['url']));
194         $this->element('title', null, $channel['title']);
195         $this->element('link', null, $channel['link']);
196         $this->element('description', null, $channel['description']);
197         $this->element('cc:licence', array('rdf:resource' => common_config('license','url')));
198
199         if ($image) {
200             $this->element('image', array('rdf:resource' => $image));
201         }
202
203         $this->elementStart('items');
204         $this->elementStart('rdf:Seq');
205
206         foreach ($this->notices as $notice) {
207             $this->element('rdf:li', array('rdf:resource' => $notice->uri));
208         }
209
210         $this->elementEnd('rdf:Seq');
211         $this->elementEnd('items');
212
213         $this->elementEnd('channel');
214     }
215
216     function showImage()
217     {
218         $image = $this->getImage();
219         if ($image) {
220             $channel = $this->getChannel();
221             $this->elementStart('image', array('rdf:about' => $image));
222             $this->element('title', null, $channel['title']);
223             $this->element('link', null, $channel['link']);
224             $this->element('url', null, $image);
225             $this->elementEnd('image');
226         }
227     }
228
229     function showItem($notice)
230     {
231         $profile = Profile::staticGet($notice->profile_id);
232         $nurl = common_local_url('shownotice', array('notice' => $notice->id));
233         $creator_uri = common_profile_uri($profile);
234         $this->elementStart('item', array('rdf:about' => $notice->uri,
235                             'rdf:type' => 'http://rdfs.org/sioc/types#MicroblogPost'));
236         $title = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
237         $this->element('title', null, $title);
238         $this->element('link', null, $nurl);
239         $this->element('description', null, $profile->nickname."'s status on ".common_exact_date($notice->created));
240         if ($notice->rendered) {
241             $this->element('content:encoded', null, common_xml_safe_str($notice->rendered));
242         }
243         $this->element('dc:date', null, common_date_w3dtf($notice->created));
244         $this->element('dc:creator', null, ($profile->fullname) ? $profile->fullname : $profile->nickname);
245         $this->element('foaf:maker', array('rdf:resource' => $creator_uri));
246         $this->element('sioc:has_creator', array('rdf:resource' => $creator_uri.'#acct'));
247         $this->element('laconica:postIcon', array('rdf:resource' => $profile->avatarUrl()));
248         $this->element('cc:licence', array('rdf:resource' => common_config('license', 'url')));
249         if ($notice->reply_to) {
250             $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
251             $this->element('sioc:reply_of', array('rdf:resource' => $replyurl));
252         }
253         if (!empty($notice->conversation)) {
254             $conversationurl = common_local_url('conversation',
255                                          array('id' => $notice->conversation));
256             $this->element('sioc:has_discussion', array('rdf:resource' => $conversationurl));
257         }
258         $attachments = $notice->attachments();
259         if($attachments){
260             foreach($attachments as $attachment){
261                 if ($attachment->isEnclosure()) {
262                     // DO NOT move xmlns declaration to root element. Making it
263                     // the default namespace here improves compatibility with
264                     // real-world feed readers.
265                     $attribs = array(
266                         'rdf:resource' => $attachment->url,
267                         'url' => $attachment->url,
268                         'xmlns' => 'http://purl.oclc.org/net/rss_2.0/enc#'
269                         );
270                     if ($attachment->title) {
271                         $attribs['dc:title'] = $attachment->title;
272                     }
273                     if ($attachment->modified) {
274                         $attribs['dc:date'] = common_date_w3dtf($attachment->modified);
275                     }
276                     if ($attachment->size) {
277                         $attribs['length'] = $attachment->size;
278                     }
279                     if ($attachment->mimetype) {
280                         $attribs['type'] = $attachment->mimetype;
281                     }
282                     $this->element('enclosure', $attribs);
283                 }
284                 $this->element('sioc:links_to', array('rdf:resource'=>$attachment->url));
285             }
286         }
287
288         $tag = new Notice_tag();
289         $tag->notice_id = $notice->id;
290         if ($tag->find()) {
291             $entry['tags']=array();
292             while ($tag->fetch()) {
293                 $tagpage = common_local_url('tag', array('tag' => $tag->tag));
294
295                 if ( in_array($tag, $this->tags_already_output) ) {
296                     $this->element('ctag:tagged', array('rdf:resource'=>$tagpage.'#concept'));
297                     continue;
298                 }
299
300                 $tagrss  = common_local_url('tagrss', array('tag' => $tag->tag));
301                 $this->elementStart('ctag:tagged');
302                 $this->elementStart('ctag:Tag', array('rdf:about'=>$tagpage.'#concept', 'ctag:label'=>$tag->tag));
303                 $this->element('foaf:page', array('rdf:resource'=>$tagpage));
304                 $this->element('rdfs:seeAlso', array('rdf:resource'=>$tagrss));
305                 $this->elementEnd('ctag:Tag');
306                 $this->elementEnd('ctag:tagged');
307
308                 $this->tags_already_output[] = $tag->tag;
309             }
310         }
311         $this->elementEnd('item');
312         $this->creators[$creator_uri] = $profile;
313     }
314
315     function showCreators()
316     {
317         foreach ($this->creators as $uri => $profile) {
318             $id = $profile->id;
319             $nickname = $profile->nickname;
320             $this->elementStart('foaf:Agent', array('rdf:about' => $uri));
321             $this->element('foaf:nick', null, $nickname);
322             if ($profile->fullname) {
323                 $this->element('foaf:name', null, $profile->fullname);
324             }
325             $this->element('foaf:holdsAccount', array('rdf:resource' => $uri.'#acct'));
326             $avatar = $profile->avatarUrl();
327             $this->element('foaf:depiction', array('rdf:resource' => $avatar));
328             $this->elementEnd('foaf:Agent');
329         }
330     }
331
332     function initRss()
333     {
334         $channel = $this->getChannel();
335         header('Content-Type: application/rdf+xml');
336
337         $this->startXml();
338         $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
339                                               'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
340                                               'xmlns:dc' =>
341                                               'http://purl.org/dc/elements/1.1/',
342                                               'xmlns:cc' =>
343                                               'http://creativecommons.org/ns#',
344                                               'xmlns:content' =>
345                                               'http://purl.org/rss/1.0/modules/content/',
346                                               'xmlns:ctag' =>
347                                               'http://commontag.org/ns#',
348                                               'xmlns:foaf' =>
349                                               'http://xmlns.com/foaf/0.1/',
350                                               'xmlns:sioc' =>
351                                               'http://rdfs.org/sioc/ns#',
352                                               'xmlns:sioct' =>
353                                               'http://rdfs.org/sioc/types#',
354                                               'xmlns:rdfs' =>
355                                               'http://www.w3.org/2000/01/rdf-schema#',
356                                               'xmlns:laconica' =>
357                                               'http://status.net/ont/',
358                                               'xmlns' => 'http://purl.org/rss/1.0/'));
359         $this->elementStart('sioc:Site', array('rdf:about' => common_root_url()));
360         $this->element('sioc:name', null, common_config('site', 'name'));
361         $this->elementStart('sioc:space_of');
362         $this->element('sioc:Container', array('rdf:about' =>
363                                                $channel['url']));
364         $this->elementEnd('sioc:space_of');
365         $this->elementEnd('sioc:Site');
366     }
367
368     function endRss()
369     {
370         $this->elementEnd('rdf:RDF');
371     }
372
373     /**
374      * When was this page last modified?
375      *
376      */
377
378     function lastModified()
379     {
380         if (empty($this->notices)) {
381             return null;
382         }
383
384         if (count($this->notices) == 0) {
385             return null;
386         }
387
388         // FIXME: doesn't handle modified profiles, avatars, deleted notices
389
390         return strtotime($this->notices[0]->created);
391     }
392 }
393