]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/rssaction.php
15c238bf90aef1ca2686f7af229122cc300169d3
[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
43     /**
44      * Constructor
45      *
46      * Just wraps the Action constructor.
47      *
48      * @param string  $output URI to output to, default = stdout
49      * @param boolean $indent Whether to indent output, default true
50      *
51      * @see Action::__construct
52      */
53
54     function __construct($output='php://output', $indent=true)
55     {
56         parent::__construct($output, $indent);
57     }
58
59     /**
60      * Do we need to write to the database?
61      *
62      * @return boolean true
63      */
64
65     function isReadonly()
66     {
67         return true;
68     }
69
70     /**
71      * Read arguments and initialize members
72      *
73      * @param array $args Arguments from $_REQUEST
74      * @return boolean success
75      */
76
77     function prepare($args)
78     {
79         parent::prepare($args);
80         $this->limit = (int) $this->trimmed('limit');
81         if ($this->limit == 0) {
82             $this->limit = DEFAULT_RSS_LIMIT;
83         }
84         return true;
85     }
86
87     /**
88      * Handle a request
89      *
90      * @param array $args Arguments from $_REQUEST
91      *
92      * @return void
93      */
94
95     function handle($args)
96     {
97         // Parent handling, including cache check
98         parent::handle($args);
99
100         if (common_config('site', 'private')) {
101             if (!isset($_SERVER['PHP_AUTH_USER'])) {
102
103                 # This header makes basic auth go
104                 header('WWW-Authenticate: Basic realm="Laconica RSS"');
105
106                 # If the user hits cancel -- bam!
107                 $this->show_basic_auth_error();
108                 return;
109             } else {
110                 $nickname = $_SERVER['PHP_AUTH_USER'];
111                 $password = $_SERVER['PHP_AUTH_PW'];
112
113                 if (!common_check_user($nickname, $password)) {
114                     # basic authentication failed
115                     list($proxy, $ip) = common_client_ip();
116
117                     common_log(LOG_WARNING, "Failed RSS auth attempt, nickname = $nickname, proxy = $proxy, ip = $ip.");
118                     $this->show_basic_auth_error();
119                     return;
120                 }
121             }
122         }
123
124         // Get the list of notices
125         if (empty($this->tag)) {
126             $this->notices = $this->getNotices($this->limit);
127         } else {
128             $this->notices = $this->getTaggedNotices($this->tag, $this->limit);
129         }
130         $this->showRss();
131     }
132
133     function show_basic_auth_error()
134     {
135         header('HTTP/1.1 401 Unauthorized');
136         header('Content-Type: application/xml; charset=utf-8');
137         $this->startXML();
138         $this->elementStart('hash');
139         $this->element('error', null, 'Could not authenticate you.');
140         $this->element('request', null, $_SERVER['REQUEST_URI']);
141         $this->elementEnd('hash');
142         $this->endXML();
143     }
144
145     /**
146      * Get the notices to output in this stream
147      *
148      * @return array an array of Notice objects sorted in reverse chron
149      */
150
151     function getNotices()
152     {
153         return array();
154     }
155
156     /**
157      * Get a description of the channel
158      *
159      * Returns an array with the following
160      * @return array
161      */
162
163     function getChannel()
164     {
165         return array('url' => '',
166                      'title' => '',
167                      'link' => '',
168                      'description' => '');
169     }
170
171     function getImage()
172     {
173         return null;
174     }
175
176     function showRss()
177     {
178         $this->initRss();
179         $this->showChannel();
180         $this->showImage();
181
182         foreach ($this->notices as $n) {
183             $this->showItem($n);
184         }
185
186         $this->showCreators();
187         $this->endRss();
188     }
189
190     function showChannel()
191     {
192
193         $channel = $this->getChannel();
194         $image = $this->getImage();
195
196         $this->elementStart('channel', array('rdf:about' => $channel['url']));
197         $this->element('title', null, $channel['title']);
198         $this->element('link', null, $channel['link']);
199         $this->element('description', null, $channel['description']);
200         $this->element('cc:licence', array('rdf:resource' => common_config('license','url')));
201
202         if ($image) {
203             $this->element('image', array('rdf:resource' => $image));
204         }
205
206         $this->elementStart('items');
207         $this->elementStart('rdf:Seq');
208
209         foreach ($this->notices as $notice) {
210             $this->element('rdf:li', array('rdf:resource' => $notice->uri));
211         }
212
213         $this->elementEnd('rdf:Seq');
214         $this->elementEnd('items');
215
216         $this->elementEnd('channel');
217     }
218
219     function showImage()
220     {
221         $image = $this->getImage();
222         if ($image) {
223             $channel = $this->getChannel();
224             $this->elementStart('image', array('rdf:about' => $image));
225             $this->element('title', null, $channel['title']);
226             $this->element('link', null, $channel['link']);
227             $this->element('url', null, $image);
228             $this->elementEnd('image');
229         }
230     }
231
232     function showItem($notice)
233     {
234         $profile = Profile::staticGet($notice->profile_id);
235         $nurl = common_local_url('shownotice', array('notice' => $notice->id));
236         $creator_uri = common_profile_uri($profile);
237         $this->elementStart('item', array('rdf:about' => $notice->uri,
238                             'rdf:type' => 'http://rdfs.org/sioc/types#MicroblogPost'));
239         $title = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
240         $this->element('title', null, $title);
241         $this->element('link', null, $nurl);
242         $this->element('description', null, $profile->nickname."'s status on ".common_exact_date($notice->created));
243         if ($notice->rendered) {
244             $this->element('content:encoded', null, common_xml_safe_str($notice->rendered));
245         }
246         $this->element('dc:date', null, common_date_w3dtf($notice->created));
247         $this->element('dc:creator', null, ($profile->fullname) ? $profile->fullname : $profile->nickname);
248         $this->element('foaf:maker', array('rdf:resource' => $creator_uri));
249         $this->element('sioc:has_creator', array('rdf:resource' => $creator_uri.'#acct'));
250         $this->element('laconica:postIcon', array('rdf:resource' => $profile->avatarUrl()));
251         $this->element('cc:licence', array('rdf:resource' => common_config('license', 'url')));
252         if ($notice->reply_to) {
253             $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
254             $this->element('sioc:reply_of', array('rdf:resource' => $replyurl));
255         }
256         $attachments = $notice->attachments();
257         if($attachments){
258             foreach($attachments as $attachment){
259                 $this->element('enc:enclosure', array('rdf:resource'=>$attachment->url,'enc:type'=>$attachment->mimetype,'enc:length'=>$attachment->size), null);
260             }
261         }
262
263         $this->elementEnd('item');
264         $this->creators[$creator_uri] = $profile;
265     }
266
267     function showCreators()
268     {
269         foreach ($this->creators as $uri => $profile) {
270             $id = $profile->id;
271             $nickname = $profile->nickname;
272             $this->elementStart('foaf:Agent', array('rdf:about' => $uri));
273             $this->element('foaf:nick', null, $nickname);
274             if ($profile->fullname) {
275                 $this->element('foaf:name', null, $profile->fullname);
276             }
277             $this->element('foaf:holdsAccount', array('rdf:resource' => $uri.'#acct'));
278             $avatar = $profile->avatarUrl();
279             $this->element('foaf:depiction', array('rdf:resource' => $avatar));
280             $this->elementEnd('foaf:Agent');
281         }
282     }
283
284     function initRss()
285     {
286         $channel = $this->getChannel();
287         header('Content-Type: application/rdf+xml');
288
289         $this->startXml();
290         $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
291                                               'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
292                                               'xmlns:dc' =>
293                                               'http://purl.org/dc/elements/1.1/',
294                                               'xmlns:cc' =>
295                                               'http://creativecommons.org/ns#',
296                                               'xmlns:content' =>
297                                               'http://purl.org/rss/1.0/modules/content/',
298                                               'xmlns:enc' =>
299                                               'http://purl.oclc.org/net/rss_2.0/enc#',
300                                               'xmlns:foaf' =>
301                                               'http://xmlns.com/foaf/0.1/',
302                                               'xmlns:sioc' =>
303                                               'http://rdfs.org/sioc/ns#',
304                                               'xmlns:sioct' =>
305                                               'http://rdfs.org/sioc/types#',
306                                               'xmlns:laconica' =>
307                                               'http://laconi.ca/ont/',
308                                               'xmlns' => 'http://purl.org/rss/1.0/'));
309         $this->elementStart('sioc:Site', array('rdf:about' => common_root_url()));
310         $this->element('sioc:name', null, common_config('site', 'name'));
311         $this->elementStart('sioc:space_of');
312         $this->element('sioc:Container', array('rdf:about' =>
313                                                $channel['url']));
314         $this->elementEnd('sioc:space_of');
315         $this->elementEnd('sioc:Site');
316     }
317
318     function endRss()
319     {
320         $this->elementEnd('rdf:RDF');
321     }
322
323     /**
324      * When was this page last modified?
325      *
326      */
327
328     function lastModified()
329     {
330         if (empty($this->notices)) {
331             return null;
332         }
333
334         if (count($this->notices) == 0) {
335             return null;
336         }
337
338         // FIXME: doesn't handle modified profiles, avatars, deleted notices
339
340         return strtotime($this->notices[0]->created);
341     }
342 }
343