]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/rss10action.php
Rss10Action migrated to ManagedAction
[quix0rs-gnu-social.git] / lib / rss10action.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('GNUSOCIAL')) { exit(1); }
32
33 define('DEFAULT_RSS_LIMIT', 48);
34
35 class Rss10Action extends ManagedAction
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     public function isReadOnly($args)
45     {
46         return true;
47     }
48
49     protected function doPreparation()
50     {
51         $this->limit = $this->int('limit');
52
53         if ($this->limit == 0) {
54             $this->limit = DEFAULT_RSS_LIMIT;
55         }
56
57         if (common_config('site', 'private')) {
58             if (!isset($_SERVER['PHP_AUTH_USER'])) {
59
60                 // This header makes basic auth go
61                 header('WWW-Authenticate: Basic realm="GNU social RSS"');
62
63                 // If the user hits cancel -- bam!
64                 $this->show_basic_auth_error();
65                 // the above calls 'exit'
66             } else {
67                 $nickname = $_SERVER['PHP_AUTH_USER'];
68                 $password = $_SERVER['PHP_AUTH_PW'];
69
70                 if (!common_check_user($nickname, $password)) {
71                     // basic authentication failed
72                     list($proxy, $ip) = common_client_ip();
73
74                     common_log(LOG_WARNING, "Failed RSS auth attempt, nickname = $nickname, proxy = $proxy, ip = $ip.");
75                     $this->show_basic_auth_error();
76                     return;
77                 }
78             }
79         }
80     }
81
82     function show_basic_auth_error()
83     {
84         header('HTTP/1.1 401 Unauthorized');
85         header('Content-Type: application/xml; charset=utf-8');
86         $this->startXML();
87         $this->elementStart('hash');
88         $this->element('error', null, 'Could not authenticate you.');
89         $this->element('request', null, $_SERVER['REQUEST_URI']);
90         $this->elementEnd('hash');
91         $this->endXML();
92         exit;
93     }
94
95     /**
96      * Get the notices to output in this stream.
97      *
98      * @return array an array of Notice objects sorted in reverse chron
99      */
100
101     function getNotices()
102     {
103         return array();
104     }
105
106     /**
107      * Get a description of the channel
108      *
109      * Returns an array with the following
110      * @return array
111      */
112
113     function getChannel()
114     {
115         return array('url' => '',
116                      'title' => '',
117                      'link' => '',
118                      'description' => '');
119     }
120
121     function getImage()
122     {
123         return null;
124     }
125
126     function showPage()
127     {
128         $this->initRss();
129         $this->showChannel();
130         $this->showImage();
131
132         if (count($this->notices)) {
133             foreach ($this->notices as $n) {
134                 try {
135                     $this->showItem($n);
136                 } catch (Exception $e) {
137                     // log exceptions and continue
138                     common_log(LOG_ERR, $e->getMessage());
139                     continue;
140                 }
141             }
142         }
143
144         $this->showCreators();
145         $this->endRss();
146     }
147
148     function showChannel()
149     {
150
151         $channel = $this->getChannel();
152         $image = $this->getImage();
153
154         $this->elementStart('channel', array('rdf:about' => $channel['url']));
155         $this->element('title', null, $channel['title']);
156         $this->element('link', null, $channel['link']);
157         $this->element('description', null, $channel['description']);
158         $this->element('cc:licence', array('rdf:resource' => common_config('license','url')));
159
160         if ($image) {
161             $this->element('image', array('rdf:resource' => $image));
162         }
163
164         $this->elementStart('items');
165         $this->elementStart('rdf:Seq');
166
167         if (count($this->notices)) {
168             foreach ($this->notices as $notice) {
169                 $this->element('rdf:li', array('rdf:resource' => $notice->uri));
170             }
171         }
172
173         $this->elementEnd('rdf:Seq');
174         $this->elementEnd('items');
175
176         $this->elementEnd('channel');
177     }
178
179     function showImage()
180     {
181         $image = $this->getImage();
182         if ($image) {
183             $channel = $this->getChannel();
184             $this->elementStart('image', array('rdf:about' => $image));
185             $this->element('title', null, $channel['title']);
186             $this->element('link', null, $channel['link']);
187             $this->element('url', null, $image);
188             $this->elementEnd('image');
189         }
190     }
191
192     function showItem($notice)
193     {
194         $profile = $notice->getProfile();
195         $nurl = common_local_url('shownotice', array('notice' => $notice->id));
196         $creator_uri = common_profile_uri($profile);
197         $this->elementStart('item', array('rdf:about' => $notice->uri,
198                             'rdf:type' => 'http://rdfs.org/sioc/types#MicroblogPost'));
199         $title = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
200         $this->element('title', null, $title);
201         $this->element('link', null, $nurl);
202         $this->element('description', null, $profile->nickname."'s status on ".common_exact_date($notice->created));
203         if ($notice->rendered) {
204             $this->element('content:encoded', null, common_xml_safe_str($notice->rendered));
205         }
206         $this->element('dc:date', null, common_date_w3dtf($notice->created));
207         $this->element('dc:creator', null, ($profile->fullname) ? $profile->fullname : $profile->nickname);
208         $this->element('foaf:maker', array('rdf:resource' => $creator_uri));
209         $this->element('sioc:has_creator', array('rdf:resource' => $creator_uri.'#acct'));
210         $location = $notice->getLocation();
211         if ($location && isset($location->lat) && isset($location->lon)) {
212             $location_uri = $location->getRdfURL();
213             $attrs = array('geo:lat' => $location->lat,
214                 'geo:long' => $location->lon);
215             if (strlen($location_uri)) {
216                 $attrs['rdf:resource'] = $location_uri;
217             }
218             $this->element('statusnet:origin', $attrs);
219         }
220         $this->element('statusnet:postIcon', array('rdf:resource' => $profile->avatarUrl()));
221         $this->element('cc:licence', array('rdf:resource' => common_config('license', 'url')));
222         if ($notice->reply_to) {
223             $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
224             $this->element('sioc:reply_of', array('rdf:resource' => $replyurl));
225         }
226         if (!empty($notice->conversation)) {
227             $conversationurl = common_local_url('conversation',
228                                          array('id' => $notice->conversation));
229             $this->element('sioc:has_discussion', array('rdf:resource' => $conversationurl));
230         }
231         $attachments = $notice->attachments();
232         if($attachments){
233             foreach($attachments as $attachment){
234                 try {
235                     $enclosure = $attachment->getEnclosure();
236                     $attribs = array('rdf:resource' => $enclosure->url);
237                     if ($enclosure->title) {
238                         $attribs['dc:title'] = $enclosure->title;
239                     }
240                     if ($enclosure->modified) {
241                         $attribs['dc:date'] = common_date_w3dtf($enclosure->modified);
242                     }
243                     if ($enclosure->size) {
244                         $attribs['enc:length'] = $enclosure->size;
245                     }
246                     if ($enclosure->mimetype) {
247                         $attribs['enc:type'] = $enclosure->mimetype;
248                     }
249                     $this->element('enc:enclosure', $attribs);
250                 } catch (ServerException $e) {
251                     // There was not enough metadata available
252                 }
253                 $this->element('sioc:links_to', array('rdf:resource'=>$attachment->url));
254             }
255         }
256
257         $tag = new Notice_tag();
258         $tag->notice_id = $notice->id;
259         if ($tag->find()) {
260             $entry['tags']=array();
261             while ($tag->fetch()) {
262                 $tagpage = common_local_url('tag', array('tag' => $tag->tag));
263
264                 if ( in_array($tag, $this->tags_already_output) ) {
265                     $this->element('ctag:tagged', array('rdf:resource'=>$tagpage.'#concept'));
266                     continue;
267                 }
268
269                 $tagrss  = common_local_url('tagrss', array('tag' => $tag->tag));
270                 $this->elementStart('ctag:tagged');
271                 $this->elementStart('ctag:Tag', array('rdf:about'=>$tagpage.'#concept', 'ctag:label'=>$tag->tag));
272                 $this->element('foaf:page', array('rdf:resource'=>$tagpage));
273                 $this->element('rdfs:seeAlso', array('rdf:resource'=>$tagrss));
274                 $this->elementEnd('ctag:Tag');
275                 $this->elementEnd('ctag:tagged');
276
277                 $this->tags_already_output[] = $tag->tag;
278             }
279         }
280         $this->elementEnd('item');
281         $this->creators[$creator_uri] = $profile;
282     }
283
284     function showCreators()
285     {
286         foreach ($this->creators as $uri => $profile) {
287             $id = $profile->id;
288             $nickname = $profile->nickname;
289             $this->elementStart('foaf:Agent', array('rdf:about' => $uri));
290             $this->element('foaf:nick', null, $nickname);
291             if ($profile->fullname) {
292                 $this->element('foaf:name', null, $profile->fullname);
293             }
294             $this->element('foaf:holdsAccount', array('rdf:resource' => $uri.'#acct'));
295             $avatar = $profile->avatarUrl();
296             $this->element('foaf:depiction', array('rdf:resource' => $avatar));
297             $this->elementEnd('foaf:Agent');
298         }
299     }
300
301     function initRss()
302     {
303         $channel = $this->getChannel();
304         header('Content-Type: application/rdf+xml');
305
306         $this->startXml();
307         $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
308                                               'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
309                                               'xmlns:dc' =>
310                                               'http://purl.org/dc/elements/1.1/',
311                                               'xmlns:cc' =>
312                                               'http://creativecommons.org/ns#',
313                                               'xmlns:content' =>
314                                               'http://purl.org/rss/1.0/modules/content/',
315                                               'xmlns:ctag' =>
316                                               'http://commontag.org/ns#',
317                                               'xmlns:foaf' =>
318                                               'http://xmlns.com/foaf/0.1/',
319                                               'xmlns:enc' =>
320                                               'http://purl.oclc.org/net/rss_2.0/enc#',
321                                               'xmlns:sioc' =>
322                                               'http://rdfs.org/sioc/ns#',
323                                               'xmlns:sioct' =>
324                                               'http://rdfs.org/sioc/types#',
325                                               'xmlns:rdfs' =>
326                                               'http://www.w3.org/2000/01/rdf-schema#',
327                                               'xmlns:geo' =>
328                                               'http://www.w3.org/2003/01/geo/wgs84_pos#',
329                                               'xmlns:statusnet' =>
330                                               'http://status.net/ont/',
331                                               'xmlns' => 'http://purl.org/rss/1.0/'));
332         $this->elementStart('sioc:Site', array('rdf:about' => common_root_url()));
333         $this->element('sioc:name', null, common_config('site', 'name'));
334         $this->elementStart('sioc:space_of');
335         $this->element('sioc:Container', array('rdf:about' =>
336                                                $channel['url']));
337         $this->elementEnd('sioc:space_of');
338         $this->elementEnd('sioc:Site');
339     }
340
341     function endRss()
342     {
343         $this->elementEnd('rdf:RDF');
344     }
345
346     /**
347      * When was this page last modified?
348      *
349      */
350
351     function lastModified()
352     {
353         if (empty($this->notices)) {
354             return null;
355         }
356
357         if (count($this->notices) == 0) {
358             return null;
359         }
360
361         // FIXME: doesn't handle modified profiles, avatars, deleted notices
362
363         return strtotime($this->notices[0]->created);
364     }
365 }
366