PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/rssaction.php

https://github.com/himmelex/NTW
PHP | 405 lines | 275 code | 53 blank | 77 comment | 31 complexity | a0c8b106cd606f4eeaf152f9f8a1d3e9 MD5 | raw file
  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('NEWTYPE') && !defined('DWORKS')) { 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. $this->showItem($n);
  154. }
  155. }
  156. $this->showCreators();
  157. $this->endRss();
  158. }
  159. function showChannel()
  160. {
  161. $channel = $this->getChannel();
  162. $image = $this->getImage();
  163. $this->elementStart('channel', array('rdf:about' => $channel['url']));
  164. $this->element('title', null, $channel['title']);
  165. $this->element('link', null, $channel['link']);
  166. $this->element('description', null, $channel['description']);
  167. $this->element('cc:licence', array('rdf:resource' => common_config('license','url')));
  168. if ($image) {
  169. $this->element('image', array('rdf:resource' => $image));
  170. }
  171. $this->elementStart('items');
  172. $this->elementStart('rdf:Seq');
  173. if (count($this->notices)) {
  174. foreach ($this->notices as $notice) {
  175. $this->element('rdf:li', array('rdf:resource' => $notice->uri));
  176. }
  177. }
  178. $this->elementEnd('rdf:Seq');
  179. $this->elementEnd('items');
  180. $this->elementEnd('channel');
  181. }
  182. function showImage()
  183. {
  184. $image = $this->getImage();
  185. if ($image) {
  186. $channel = $this->getChannel();
  187. $this->elementStart('image', array('rdf:about' => $image));
  188. $this->element('title', null, $channel['title']);
  189. $this->element('link', null, $channel['link']);
  190. $this->element('url', null, $image);
  191. $this->elementEnd('image');
  192. }
  193. }
  194. function showItem($notice)
  195. {
  196. $profile = Profile::staticGet($notice->profile_id);
  197. $nurl = common_local_url('shownotice', array('notice' => $notice->id));
  198. $creator_uri = common_profile_uri($profile);
  199. $this->elementStart('item', array('rdf:about' => $notice->uri,
  200. 'rdf:type' => 'http://rdfs.org/sioc/types#MicroblogPost'));
  201. $title = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
  202. $this->element('title', null, $title);
  203. $this->element('link', null, $nurl);
  204. $this->element('description', null, $profile->nickname."'s status on ".common_exact_date($notice->created));
  205. if ($notice->rendered) {
  206. $this->element('content:encoded', null, common_xml_safe_str($notice->rendered));
  207. }
  208. $this->element('dc:date', null, common_date_w3dtf($notice->created));
  209. $this->element('dc:creator', null, ($profile->fullname) ? $profile->fullname : $profile->nickname);
  210. $this->element('foaf:maker', array('rdf:resource' => $creator_uri));
  211. $this->element('sioc:has_creator', array('rdf:resource' => $creator_uri.'#acct'));
  212. $location = $notice->getLocation();
  213. if ($location && isset($location->lat) && isset($location->lon)) {
  214. $location_uri = $location->getRdfURL();
  215. $attrs = array('geo:lat' => $location->lat,
  216. 'geo:long' => $location->lon);
  217. if (strlen($location_uri)) {
  218. $attrs['rdf:resource'] = $location_uri;
  219. }
  220. $this->element('statusnet:origin', $attrs);
  221. }
  222. $this->element('statusnet:postIcon', array('rdf:resource' => $profile->avatarUrl()));
  223. $this->element('cc:licence', array('rdf:resource' => common_config('license', 'url')));
  224. if ($notice->reply_to) {
  225. $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
  226. $this->element('sioc:reply_of', array('rdf:resource' => $replyurl));
  227. }
  228. if (!empty($notice->conversation)) {
  229. $conversationurl = common_local_url('conversation',
  230. array('id' => $notice->conversation));
  231. $this->element('sioc:has_discussion', array('rdf:resource' => $conversationurl));
  232. }
  233. $attachments = $notice->attachments();
  234. if($attachments){
  235. foreach($attachments as $attachment){
  236. $enclosure=$attachment->getEnclosure();
  237. if ($enclosure) {
  238. $attribs = array('rdf:resource' => $enclosure->url);
  239. if ($enclosure->title) {
  240. $attribs['dc:title'] = $enclosure->title;
  241. }
  242. if ($enclosure->modified) {
  243. $attribs['dc:date'] = common_date_w3dtf($enclosure->modified);
  244. }
  245. if ($enclosure->size) {
  246. $attribs['enc:length'] = $enclosure->size;
  247. }
  248. if ($enclosure->mimetype) {
  249. $attribs['enc:type'] = $enclosure->mimetype;
  250. }
  251. $this->element('enc:enclosure', $attribs);
  252. }
  253. $this->element('sioc:links_to', array('rdf:resource'=>$attachment->url));
  254. }
  255. }
  256. $tag = new Notice_tag();
  257. $tag->notice_id = $notice->id;
  258. if ($tag->find()) {
  259. $entry['tags']=array();
  260. while ($tag->fetch()) {
  261. $tagpage = common_local_url('tag', array('tag' => $tag->tag));
  262. if ( in_array($tag, $this->tags_already_output) ) {
  263. $this->element('ctag:tagged', array('rdf:resource'=>$tagpage.'#concept'));
  264. continue;
  265. }
  266. $tagrss = common_local_url('tagrss', array('tag' => $tag->tag));
  267. $this->elementStart('ctag:tagged');
  268. $this->elementStart('ctag:Tag', array('rdf:about'=>$tagpage.'#concept', 'ctag:label'=>$tag->tag));
  269. $this->element('foaf:page', array('rdf:resource'=>$tagpage));
  270. $this->element('rdfs:seeAlso', array('rdf:resource'=>$tagrss));
  271. $this->elementEnd('ctag:Tag');
  272. $this->elementEnd('ctag:tagged');
  273. $this->tags_already_output[] = $tag->tag;
  274. }
  275. }
  276. $this->elementEnd('item');
  277. $this->creators[$creator_uri] = $profile;
  278. }
  279. function showCreators()
  280. {
  281. foreach ($this->creators as $uri => $profile) {
  282. $id = $profile->id;
  283. $nickname = $profile->nickname;
  284. $this->elementStart('foaf:Agent', array('rdf:about' => $uri));
  285. $this->element('foaf:nick', null, $nickname);
  286. if ($profile->fullname) {
  287. $this->element('foaf:name', null, $profile->fullname);
  288. }
  289. $this->element('foaf:holdsAccount', array('rdf:resource' => $uri.'#acct'));
  290. $avatar = $profile->avatarUrl();
  291. $this->element('foaf:depiction', array('rdf:resource' => $avatar));
  292. $this->elementEnd('foaf:Agent');
  293. }
  294. }
  295. function initRss()
  296. {
  297. $channel = $this->getChannel();
  298. header('Content-Type: application/rdf+xml');
  299. $this->startXml();
  300. $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
  301. 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
  302. 'xmlns:dc' =>
  303. 'http://purl.org/dc/elements/1.1/',
  304. 'xmlns:cc' =>
  305. 'http://creativecommons.org/ns#',
  306. 'xmlns:content' =>
  307. 'http://purl.org/rss/1.0/modules/content/',
  308. 'xmlns:ctag' =>
  309. 'http://commontag.org/ns#',
  310. 'xmlns:foaf' =>
  311. 'http://xmlns.com/foaf/0.1/',
  312. 'xmlns:enc' =>
  313. 'http://purl.oclc.org/net/rss_2.0/enc#',
  314. 'xmlns:sioc' =>
  315. 'http://rdfs.org/sioc/ns#',
  316. 'xmlns:sioct' =>
  317. 'http://rdfs.org/sioc/types#',
  318. 'xmlns:rdfs' =>
  319. 'http://www.w3.org/2000/01/rdf-schema#',
  320. 'xmlns:geo' =>
  321. 'http://www.w3.org/2003/01/geo/wgs84_pos#',
  322. 'xmlns:statusnet' =>
  323. 'http://status.net/ont/',
  324. 'xmlns' => 'http://purl.org/rss/1.0/'));
  325. $this->elementStart('sioc:Site', array('rdf:about' => common_root_url()));
  326. $this->element('sioc:name', null, common_config('site', 'name'));
  327. $this->elementStart('sioc:space_of');
  328. $this->element('sioc:Container', array('rdf:about' =>
  329. $channel['url']));
  330. $this->elementEnd('sioc:space_of');
  331. $this->elementEnd('sioc:Site');
  332. }
  333. function endRss()
  334. {
  335. $this->elementEnd('rdf:RDF');
  336. }
  337. /**
  338. * When was this page last modified?
  339. *
  340. */
  341. function lastModified()
  342. {
  343. if (empty($this->notices)) {
  344. return null;
  345. }
  346. if (count($this->notices) == 0) {
  347. return null;
  348. }
  349. // FIXME: doesn't handle modified profiles, avatars, deleted notices
  350. return strtotime($this->notices[0]->created);
  351. }
  352. }