rss10action.php 14 KB

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