rss10action.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Base class for RSS 1.0 feed actions
  18. *
  19. * @category Mail
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @author Earle Martin <earle@downlode.org>
  23. * @copyright 2008, 2009 StatusNet, Inc.
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die();
  27. define('DEFAULT_RSS_LIMIT', 48);
  28. class Rss10Action extends ManagedAction
  29. {
  30. // This will contain the details of each feed item's author and be used to generate SIOC data.
  31. public $creators = [];
  32. public $limit = DEFAULT_RSS_LIMIT;
  33. public $notices = null;
  34. public $tags_already_output = [];
  35. public function isReadOnly($args)
  36. {
  37. return true;
  38. }
  39. protected function doPreparation()
  40. {
  41. $this->limit = $this->int('limit');
  42. if (empty($this->limit)) {
  43. $this->limit = DEFAULT_RSS_LIMIT;
  44. }
  45. if (common_config('site', 'private')) {
  46. if (!isset($_SERVER['PHP_AUTH_USER'])) {
  47. // This header makes basic auth go
  48. header('WWW-Authenticate: Basic realm="GNU social RSS"');
  49. // If the user hits cancel -- bam!
  50. $this->show_basic_auth_error();
  51. // the above calls 'exit'
  52. } else {
  53. $nickname = $_SERVER['PHP_AUTH_USER'];
  54. $password = $_SERVER['PHP_AUTH_PW'];
  55. if (!common_check_user($nickname, $password)) {
  56. // basic authentication failed
  57. list($proxy, $ip) = common_client_ip();
  58. common_log(LOG_WARNING, "Failed RSS auth attempt, nickname = $nickname, proxy = $proxy, ip = $ip.");
  59. $this->show_basic_auth_error();
  60. // the above calls 'exit'
  61. }
  62. }
  63. }
  64. $this->doStreamPreparation();
  65. $this->notices = $this->getNotices($this->limit);
  66. }
  67. protected function doStreamPreparation()
  68. {
  69. // for example if we need to set $this->target or something
  70. }
  71. public function show_basic_auth_error()
  72. {
  73. http_response_code(401);
  74. header('Content-Type: application/xml; charset=utf-8');
  75. $this->startXML();
  76. $this->elementStart('hash');
  77. $this->element('error', null, 'Could not authenticate you.');
  78. $this->element('request', null, $_SERVER['REQUEST_URI']);
  79. $this->elementEnd('hash');
  80. $this->endXML();
  81. exit;
  82. }
  83. /**
  84. * Get the notices to output in this stream.
  85. *
  86. * @return array an array of Notice objects sorted in reverse chron
  87. */
  88. protected function getNotices()
  89. {
  90. return array();
  91. }
  92. /**
  93. * Get a description of the channel
  94. *
  95. * Returns an array with the following
  96. * @return array
  97. */
  98. public function getChannel()
  99. {
  100. return [
  101. 'url' => '',
  102. 'title' => '',
  103. 'link' => '',
  104. 'description' => '',
  105. ];
  106. }
  107. public function getImage()
  108. {
  109. return null;
  110. }
  111. public function showPage()
  112. {
  113. $this->initRss();
  114. $this->showChannel();
  115. $this->showImage();
  116. if (count($this->notices)) {
  117. foreach ($this->notices as $n) {
  118. try {
  119. $this->showItem($n);
  120. } catch (Exception $e) {
  121. // log exceptions and continue
  122. common_log(LOG_ERR, $e->getMessage());
  123. continue;
  124. }
  125. }
  126. }
  127. $this->showCreators();
  128. $this->endRss();
  129. }
  130. public function showChannel()
  131. {
  132. $channel = $this->getChannel();
  133. $image = $this->getImage();
  134. $this->elementStart('channel', array('rdf:about' => $channel['url']));
  135. $this->element('title', null, $channel['title']);
  136. $this->element('link', null, $channel['link']);
  137. $this->element('description', null, $channel['description']);
  138. $this->element('cc:licence', [
  139. 'rdf:resource' => common_config('license', 'url'),
  140. ]);
  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. public 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. public 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(
  207. 'conversation',
  208. ['id' => $notice->conversation]
  209. );
  210. $this->element('sioc:has_discussion', [
  211. 'rdf:resource' => $conversationurl,
  212. ]);
  213. }
  214. $attachments = $notice->attachments();
  215. if ($attachments) {
  216. foreach ($attachments as $attachment) {
  217. try {
  218. $enclosure = $attachment->getEnclosure();
  219. $attribs = array('rdf:resource' => $enclosure->url);
  220. if ($enclosure->title) {
  221. $attribs['dc:title'] = $enclosure->title;
  222. }
  223. if ($enclosure->modified) {
  224. $attribs['dc:date'] = common_date_w3dtf($enclosure->modified);
  225. }
  226. if ($enclosure->size) {
  227. $attribs['enc:length'] = $enclosure->size;
  228. }
  229. if ($enclosure->mimetype) {
  230. $attribs['enc:type'] = $enclosure->mimetype;
  231. }
  232. $this->element('enc:enclosure', $attribs);
  233. } catch (ServerException $e) {
  234. // There was not enough metadata available
  235. }
  236. $this->element('sioc:links_to', array('rdf:resource'=>$attachment->url));
  237. }
  238. }
  239. $tag = new Notice_tag();
  240. $tag->notice_id = $notice->id;
  241. if ($tag->find()) {
  242. $entry['tags']=array();
  243. while ($tag->fetch()) {
  244. $tagpage = common_local_url('tag', array('tag' => $tag->tag));
  245. if (in_array($tag, $this->tags_already_output)) {
  246. $this->element('ctag:tagged', array('rdf:resource'=>$tagpage.'#concept'));
  247. continue;
  248. }
  249. $tagrss = common_local_url('tagrss', array('tag' => $tag->tag));
  250. $this->elementStart('ctag:tagged');
  251. $this->elementStart('ctag:Tag', array('rdf:about'=>$tagpage.'#concept', 'ctag:label'=>$tag->tag));
  252. $this->element('foaf:page', array('rdf:resource'=>$tagpage));
  253. $this->element('rdfs:seeAlso', array('rdf:resource'=>$tagrss));
  254. $this->elementEnd('ctag:Tag');
  255. $this->elementEnd('ctag:tagged');
  256. $this->tags_already_output[] = $tag->tag;
  257. }
  258. }
  259. $this->elementEnd('item');
  260. $this->creators[$creator_uri] = $profile;
  261. }
  262. public function showCreators()
  263. {
  264. foreach ($this->creators as $uri => $profile) {
  265. $id = $profile->id;
  266. $nickname = $profile->nickname;
  267. $this->elementStart('foaf:Agent', array('rdf:about' => $uri));
  268. $this->element('foaf:nick', null, $nickname);
  269. if ($profile->fullname) {
  270. $this->element('foaf:name', null, $profile->fullname);
  271. }
  272. $this->element('foaf:holdsAccount', array('rdf:resource' => $uri.'#acct'));
  273. $avatar = $profile->avatarUrl();
  274. $this->element('foaf:depiction', array('rdf:resource' => $avatar));
  275. $this->elementEnd('foaf:Agent');
  276. }
  277. }
  278. public function initRss()
  279. {
  280. $channel = $this->getChannel();
  281. header('Content-Type: application/rdf+xml');
  282. $this->startXml();
  283. $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
  284. 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
  285. 'xmlns:dc' =>
  286. 'http://purl.org/dc/elements/1.1/',
  287. 'xmlns:cc' =>
  288. 'http://creativecommons.org/ns#',
  289. 'xmlns:content' =>
  290. 'http://purl.org/rss/1.0/modules/content/',
  291. 'xmlns:ctag' =>
  292. 'http://commontag.org/ns#',
  293. 'xmlns:foaf' =>
  294. 'http://xmlns.com/foaf/0.1/',
  295. 'xmlns:enc' =>
  296. 'http://purl.oclc.org/net/rss_2.0/enc#',
  297. 'xmlns:sioc' =>
  298. 'http://rdfs.org/sioc/ns#',
  299. 'xmlns:sioct' =>
  300. 'http://rdfs.org/sioc/types#',
  301. 'xmlns:rdfs' =>
  302. 'http://www.w3.org/2000/01/rdf-schema#',
  303. 'xmlns:geo' =>
  304. 'http://www.w3.org/2003/01/geo/wgs84_pos#',
  305. 'xmlns:statusnet' =>
  306. 'http://status.net/ont/',
  307. 'xmlns' => 'http://purl.org/rss/1.0/'));
  308. $this->elementStart('sioc:Site', array('rdf:about' => common_root_url()));
  309. $this->element('sioc:name', null, common_config('site', 'name'));
  310. $this->elementStart('sioc:space_of');
  311. $this->element('sioc:Container', array('rdf:about' =>
  312. $channel['url']));
  313. $this->elementEnd('sioc:space_of');
  314. $this->elementEnd('sioc:Site');
  315. }
  316. public function endRss()
  317. {
  318. $this->elementEnd('rdf:RDF');
  319. }
  320. /**
  321. * When was this page last modified?
  322. *
  323. */
  324. public function lastModified()
  325. {
  326. if (empty($this->notices)) {
  327. return null;
  328. }
  329. if (count($this->notices) == 0) {
  330. return null;
  331. }
  332. // FIXME: doesn't handle modified profiles, avatars, deleted notices
  333. return strtotime($this->notices[0]->created);
  334. }
  335. }