]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/rssaction.php
use none instead of unset
[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') && !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=null)
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
82         $this->limit = (int) $this->trimmed('limit');
83
84         if ($this->limit == 0) {
85             $this->limit = DEFAULT_RSS_LIMIT;
86         }
87
88         if (common_config('site', 'private')) {
89             if (!isset($_SERVER['PHP_AUTH_USER'])) {
90
91                 // This header makes basic auth go
92                 header('WWW-Authenticate: Basic realm="GNU social RSS"');
93
94                 // If the user hits cancel -- bam!
95                 $this->show_basic_auth_error();
96                 return;
97             } else {
98                 $nickname = $_SERVER['PHP_AUTH_USER'];
99                 $password = $_SERVER['PHP_AUTH_PW'];
100
101                 if (!common_check_user($nickname, $password)) {
102                     // basic authentication failed
103                     list($proxy, $ip) = common_client_ip();
104
105                     common_log(LOG_WARNING, "Failed RSS auth attempt, nickname = $nickname, proxy = $proxy, ip = $ip.");
106                     $this->show_basic_auth_error();
107                     return;
108                 }
109             }
110         }
111
112         return true;
113     }
114
115     /**
116      * Handle a request
117      *
118      * @param array $args Arguments from $_REQUEST
119      *
120      * @return void
121      */
122
123     function handle($args)
124     {
125         // Parent handling, including cache check
126         parent::handle($args);
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         if (count($this->notices)) {
180             foreach ($this->notices as $n) {
181                 try {
182                     $this->showItem($n);
183                 } catch (Exception $e) {
184                     // log exceptions and continue
185                     common_log(LOG_ERR, $e->getMessage());
186                     continue;
187                 }
188             }
189         }
190
191         $this->showCreators();
192         $this->endRss();
193     }
194
195     function showChannel()
196     {
197
198         $channel = $this->getChannel();
199         $image = $this->getImage();
200
201         $this->elementStart('channel', array('rdf:about' => $channel['url']));
202         $this->element('title', null, $channel['title']);
203         $this->element('link', null, $channel['link']);
204         $this->element('description', null, $channel['description']);
205         $this->element('cc:licence', array('rdf:resource' => common_config('license','url')));
206
207         if ($image) {
208             $this->element('image', array('rdf:resource' => $image));
209         }
210
211         $this->elementStart('items');
212         $this->elementStart('rdf:Seq');
213
214         if (count($this->notices)) {
215             foreach ($this->notices as $notice) {
216                 $this->element('rdf:li', array('rdf:resource' => $notice->uri));
217             }
218         }
219
220         $this->elementEnd('rdf:Seq');
221         $this->elementEnd('items');
222
223         $this->elementEnd('channel');
224     }
225
226     function showImage()
227     {
228         $image = $this->getImage();
229         if ($image) {
230             $channel = $this->getChannel();
231             $this->elementStart('image', array('rdf:about' => $image));
232             $this->element('title', null, $channel['title']);
233             $this->element('link', null, $channel['link']);
234             $this->element('url', null, $image);
235             $this->elementEnd('image');
236         }
237     }
238
239     function showItem($notice)
240     {
241         $profile = $notice->getProfile();
242         $nurl = common_local_url('shownotice', array('notice' => $notice->id));
243         $creator_uri = common_profile_uri($profile);
244         $this->elementStart('item', array('rdf:about' => $notice->uri,
245                             'rdf:type' => 'http://rdfs.org/sioc/types#MicroblogPost'));
246         $title = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
247         $this->element('title', null, $title);
248         $this->element('link', null, $nurl);
249         $this->element('description', null, $profile->nickname."'s status on ".common_exact_date($notice->created));
250         if ($notice->rendered) {
251             $this->element('content:encoded', null, common_xml_safe_str($notice->rendered));
252         }
253         $this->element('dc:date', null, common_date_w3dtf($notice->created));
254         $this->element('dc:creator', null, ($profile->fullname) ? $profile->fullname : $profile->nickname);
255         $this->element('foaf:maker', array('rdf:resource' => $creator_uri));
256         $this->element('sioc:has_creator', array('rdf:resource' => $creator_uri.'#acct'));
257         $location = $notice->getLocation();
258         if ($location && isset($location->lat) && isset($location->lon)) {
259             $location_uri = $location->getRdfURL();
260             $attrs = array('geo:lat' => $location->lat,
261                 'geo:long' => $location->lon);
262             if (strlen($location_uri)) {
263                 $attrs['rdf:resource'] = $location_uri;
264             }
265             $this->element('statusnet:origin', $attrs);
266         }
267         $this->element('statusnet:postIcon', array('rdf:resource' => $profile->avatarUrl()));
268         $this->element('cc:licence', array('rdf:resource' => common_config('license', 'url')));
269         if ($notice->reply_to) {
270             $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
271             $this->element('sioc:reply_of', array('rdf:resource' => $replyurl));
272         }
273         if (!empty($notice->conversation)) {
274             $conversationurl = common_local_url('conversation',
275                                          array('id' => $notice->conversation));
276             $this->element('sioc:has_discussion', array('rdf:resource' => $conversationurl));
277         }
278         $attachments = $notice->attachments();
279         if($attachments){
280             foreach($attachments as $attachment){
281                 try {
282                     $enclosure = $attachment->getEnclosure();
283                     $attribs = array('rdf:resource' => $enclosure->url);
284                     if ($enclosure->title) {
285                         $attribs['dc:title'] = $enclosure->title;
286                     }
287                     if ($enclosure->modified) {
288                         $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                 } catch (ServerException $e) {
298                     // There was not enough metadata available
299                 }
300                 $this->element('sioc:links_to', array('rdf:resource'=>$attachment->url));
301             }
302         }
303
304         $tag = new Notice_tag();
305         $tag->notice_id = $notice->id;
306         if ($tag->find()) {
307             $entry['tags']=array();
308             while ($tag->fetch()) {
309                 $tagpage = common_local_url('tag', array('tag' => $tag->tag));
310
311                 if ( in_array($tag, $this->tags_already_output) ) {
312                     $this->element('ctag:tagged', array('rdf:resource'=>$tagpage.'#concept'));
313                     continue;
314                 }
315
316                 $tagrss  = common_local_url('tagrss', array('tag' => $tag->tag));
317                 $this->elementStart('ctag:tagged');
318                 $this->elementStart('ctag:Tag', array('rdf:about'=>$tagpage.'#concept', 'ctag:label'=>$tag->tag));
319                 $this->element('foaf:page', array('rdf:resource'=>$tagpage));
320                 $this->element('rdfs:seeAlso', array('rdf:resource'=>$tagrss));
321                 $this->elementEnd('ctag:Tag');
322                 $this->elementEnd('ctag:tagged');
323
324                 $this->tags_already_output[] = $tag->tag;
325             }
326         }
327         $this->elementEnd('item');
328         $this->creators[$creator_uri] = $profile;
329     }
330
331     function showCreators()
332     {
333         foreach ($this->creators as $uri => $profile) {
334             $id = $profile->id;
335             $nickname = $profile->nickname;
336             $this->elementStart('foaf:Agent', array('rdf:about' => $uri));
337             $this->element('foaf:nick', null, $nickname);
338             if ($profile->fullname) {
339                 $this->element('foaf:name', null, $profile->fullname);
340             }
341             $this->element('foaf:holdsAccount', array('rdf:resource' => $uri.'#acct'));
342             $avatar = $profile->avatarUrl();
343             $this->element('foaf:depiction', array('rdf:resource' => $avatar));
344             $this->elementEnd('foaf:Agent');
345         }
346     }
347
348     function initRss()
349     {
350         $channel = $this->getChannel();
351         header('Content-Type: application/rdf+xml');
352
353         $this->startXml();
354         $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
355                                               'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
356                                               'xmlns:dc' =>
357                                               'http://purl.org/dc/elements/1.1/',
358                                               'xmlns:cc' =>
359                                               'http://creativecommons.org/ns#',
360                                               'xmlns:content' =>
361                                               'http://purl.org/rss/1.0/modules/content/',
362                                               'xmlns:ctag' =>
363                                               'http://commontag.org/ns#',
364                                               'xmlns:foaf' =>
365                                               'http://xmlns.com/foaf/0.1/',
366                                               'xmlns:enc' =>
367                                               'http://purl.oclc.org/net/rss_2.0/enc#',
368                                               'xmlns:sioc' =>
369                                               'http://rdfs.org/sioc/ns#',
370                                               'xmlns:sioct' =>
371                                               'http://rdfs.org/sioc/types#',
372                                               'xmlns:rdfs' =>
373                                               'http://www.w3.org/2000/01/rdf-schema#',
374                                               'xmlns:geo' =>
375                                               'http://www.w3.org/2003/01/geo/wgs84_pos#',
376                                               'xmlns:statusnet' =>
377                                               'http://status.net/ont/',
378                                               'xmlns' => 'http://purl.org/rss/1.0/'));
379         $this->elementStart('sioc:Site', array('rdf:about' => common_root_url()));
380         $this->element('sioc:name', null, common_config('site', 'name'));
381         $this->elementStart('sioc:space_of');
382         $this->element('sioc:Container', array('rdf:about' =>
383                                                $channel['url']));
384         $this->elementEnd('sioc:space_of');
385         $this->elementEnd('sioc:Site');
386     }
387
388     function endRss()
389     {
390         $this->elementEnd('rdf:RDF');
391     }
392
393     /**
394      * When was this page last modified?
395      *
396      */
397
398     function lastModified()
399     {
400         if (empty($this->notices)) {
401             return null;
402         }
403
404         if (count($this->notices) == 0) {
405             return null;
406         }
407
408         // FIXME: doesn't handle modified profiles, avatars, deleted notices
409
410         return strtotime($this->notices[0]->created);
411     }
412 }
413