rssaction.php 14 KB

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