]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/rssaction.php
Merge commit 'jeff-themovie/private-rss' into 0.8.x
[quix0rs-gnu-social.git] / lib / rssaction.php
1 <?php
2 /**
3  * Laconica, 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   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @author    Earle Martin <earle@downlode.org>
26  * @copyright 2008-9 Control Yourself, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://laconi.ca/
29  */
30
31 if (!defined('LACONICA')) { 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="Laconica 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         if (empty($this->tag)) {
127             $this->notices = $this->getNotices($this->limit);
128         } else {
129             $this->notices = $this->getTaggedNotices($this->tag, $this->limit);
130         }
131         $this->showRss();
132     }
133
134     function show_basic_auth_error()
135     {
136         header('HTTP/1.1 401 Unauthorized');
137         header('Content-Type: application/xml; charset=utf-8');
138         $this->startXML();
139         $this->elementStart('hash');
140         $this->element('error', null, 'Could not authenticate you.');
141         $this->element('request', null, $_SERVER['REQUEST_URI']);
142         $this->elementEnd('hash');
143         $this->endXML();
144     }
145
146     /**
147      * Get the notices to output in this stream
148      *
149      * @return array an array of Notice objects sorted in reverse chron
150      */
151
152     function getNotices()
153     {
154         return array();
155     }
156
157     /**
158      * Get a description of the channel
159      *
160      * Returns an array with the following
161      * @return array
162      */
163
164     function getChannel()
165     {
166         return array('url' => '',
167                      'title' => '',
168                      'link' => '',
169                      'description' => '');
170     }
171
172     function getImage()
173     {
174         return null;
175     }
176
177     function showRss()
178     {
179         $this->initRss();
180         $this->showChannel();
181         $this->showImage();
182
183         foreach ($this->notices as $n) {
184             $this->showItem($n);
185         }
186
187         $this->showCreators();
188         $this->endRss();
189     }
190
191     function showChannel()
192     {
193
194         $channel = $this->getChannel();
195         $image = $this->getImage();
196
197         $this->elementStart('channel', array('rdf:about' => $channel['url']));
198         $this->element('title', null, $channel['title']);
199         $this->element('link', null, $channel['link']);
200         $this->element('description', null, $channel['description']);
201         $this->element('cc:licence', array('rdf:resource' => common_config('license','url')));
202
203         if ($image) {
204             $this->element('image', array('rdf:resource' => $image));
205         }
206
207         $this->elementStart('items');
208         $this->elementStart('rdf:Seq');
209
210         foreach ($this->notices as $notice) {
211             $this->element('rdf:li', array('rdf:resource' => $notice->uri));
212         }
213
214         $this->elementEnd('rdf:Seq');
215         $this->elementEnd('items');
216
217         $this->elementEnd('channel');
218     }
219
220     function showImage()
221     {
222         $image = $this->getImage();
223         if ($image) {
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');
230         }
231     }
232
233     function showItem($notice)
234     {
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));
246         }
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         $this->element('laconica:postIcon', array('rdf:resource' => $profile->avatarUrl()));
252         $this->element('cc:licence', array('rdf:resource' => common_config('license', 'url')));
253         if ($notice->reply_to) {
254             $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
255             $this->element('sioc:reply_of', array('rdf:resource' => $replyurl));
256         }
257         if (!empty($notice->conversation)) {
258             $conversationurl = common_local_url('conversation',
259                                          array('id' => $notice->conversation));
260             $this->element('sioc:has_discussion', array('rdf:resource' => $conversationurl));
261         }
262         $attachments = $notice->attachments();
263         if($attachments){
264             foreach($attachments as $attachment){
265                 if ($attachment->isEnclosure()) {
266                     // DO NOT move xmlns declaration to root element. Making it
267                     // the default namespace here improves compatibility with
268                     // real-world feed readers.
269                     $attribs = array(
270                         'rdf:resource' => $attachment->url,
271                         'url' => $attachment->url,
272                         'xmlns' => 'http://purl.oclc.org/net/rss_2.0/enc#'
273                         );
274                     if ($attachment->title) {
275                         $attribs['dc:title'] = $attachment->title;
276                     }
277                     if ($attachment->modified) {
278                         $attribs['dc:date'] = common_date_w3dtf($attachment->modified);
279                     }
280                     if ($attachment->size) {
281                         $attribs['length'] = $attachment->size;
282                     }
283                     if ($attachment->mimetype) {
284                         $attribs['type'] = $attachment->mimetype;
285                     }
286                     $this->element('enclosure', $attribs);
287                 }
288                 $this->element('sioc:links_to', array('rdf:resource'=>$attachment->url));
289             }
290         }
291
292         $tag = new Notice_tag();
293         $tag->notice_id = $notice->id;
294         if ($tag->find()) {
295             $entry['tags']=array();
296             while ($tag->fetch()) {
297                 $tagpage = common_local_url('tag', array('tag' => $tag->tag));
298
299                 if ( in_array($tag, $this->tags_already_output) ) {
300                     $this->element('ctag:tagged', array('rdf:resource'=>$tagpage.'#concept'));
301                     continue;
302                 }
303
304                 $tagrss  = common_local_url('tagrss', array('tag' => $tag->tag));
305                 $this->elementStart('ctag:tagged');
306                 $this->elementStart('ctag:Tag', array('rdf:about'=>$tagpage.'#concept', 'ctag:label'=>$tag->tag));
307                 $this->element('foaf:page', array('rdf:resource'=>$tagpage));
308                 $this->element('rdfs:seeAlso', array('rdf:resource'=>$tagrss));
309                 $this->elementEnd('ctag:Tag');
310                 $this->elementEnd('ctag:tagged');
311
312                 $this->tags_already_output[] = $tag->tag;
313             }
314         }
315         $this->elementEnd('item');
316         $this->creators[$creator_uri] = $profile;
317     }
318
319     function showCreators()
320     {
321         foreach ($this->creators as $uri => $profile) {
322             $id = $profile->id;
323             $nickname = $profile->nickname;
324             $this->elementStart('foaf:Agent', array('rdf:about' => $uri));
325             $this->element('foaf:nick', null, $nickname);
326             if ($profile->fullname) {
327                 $this->element('foaf:name', null, $profile->fullname);
328             }
329             $this->element('foaf:holdsAccount', array('rdf:resource' => $uri.'#acct'));
330             $avatar = $profile->avatarUrl();
331             $this->element('foaf:depiction', array('rdf:resource' => $avatar));
332             $this->elementEnd('foaf:Agent');
333         }
334     }
335
336     function initRss()
337     {
338         $channel = $this->getChannel();
339         header('Content-Type: application/rdf+xml');
340
341         $this->startXml();
342         $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
343                                               'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
344                                               'xmlns:dc' =>
345                                               'http://purl.org/dc/elements/1.1/',
346                                               'xmlns:cc' =>
347                                               'http://creativecommons.org/ns#',
348                                               'xmlns:content' =>
349                                               'http://purl.org/rss/1.0/modules/content/',
350                                               'xmlns:ctag' =>
351                                               'http://commontag.org/ns#',
352                                               'xmlns:foaf' =>
353                                               'http://xmlns.com/foaf/0.1/',
354                                               'xmlns:sioc' =>
355                                               'http://rdfs.org/sioc/ns#',
356                                               'xmlns:sioct' =>
357                                               'http://rdfs.org/sioc/types#',
358                                               'xmlns:rdfs' =>
359                                               'http://www.w3.org/2000/01/rdf-schema#',
360                                               'xmlns:laconica' =>
361                                               'http://laconi.ca/ont/',
362                                               'xmlns' => 'http://purl.org/rss/1.0/'));
363         $this->elementStart('sioc:Site', array('rdf:about' => common_root_url()));
364         $this->element('sioc:name', null, common_config('site', 'name'));
365         $this->elementStart('sioc:space_of');
366         $this->element('sioc:Container', array('rdf:about' =>
367                                                $channel['url']));
368         $this->elementEnd('sioc:space_of');
369         $this->elementEnd('sioc:Site');
370     }
371
372     function endRss()
373     {
374         $this->elementEnd('rdf:RDF');
375     }
376
377     /**
378      * When was this page last modified?
379      *
380      */
381
382     function lastModified()
383     {
384         if (empty($this->notices)) {
385             return null;
386         }
387
388         if (count($this->notices) == 0) {
389             return null;
390         }
391
392         // FIXME: doesn't handle modified profiles, avatars, deleted notices
393
394         return strtotime($this->notices[0]->created);
395     }
396 }
397