apitimelineuser.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Show a user's timeline
  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 API
  23. * @package StatusNet
  24. * @author Craig Andrews <candrews@integralblue.com>
  25. * @author Evan Prodromou <evan@status.net>
  26. * @author Jeffery To <jeffery.to@gmail.com>
  27. * @author mac65 <mac65@mac65.com>
  28. * @author Mike Cochrane <mikec@mikenz.geek.nz>
  29. * @author Robin Millette <robin@millette.info>
  30. * @author Zach Copley <zach@status.net>
  31. * @copyright 2009 StatusNet, Inc.
  32. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  33. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  34. * @link http://status.net/
  35. */
  36. if (!defined('GNUSOCIAL')) { exit(1); }
  37. /**
  38. * Returns the most recent notices (default 20) posted by the authenticating
  39. * user. Another user's timeline can be requested via the id parameter. This
  40. * is the API equivalent of the user profile web page.
  41. *
  42. * @category API
  43. * @package StatusNet
  44. * @author Craig Andrews <candrews@integralblue.com>
  45. * @author Evan Prodromou <evan@status.net>
  46. * @author Jeffery To <jeffery.to@gmail.com>
  47. * @author mac65 <mac65@mac65.com>
  48. * @author Mike Cochrane <mikec@mikenz.geek.nz>
  49. * @author Robin Millette <robin@millette.info>
  50. * @author Zach Copley <zach@status.net>
  51. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  52. * @link http://status.net/
  53. */
  54. class ApiTimelineUserAction extends ApiBareAuthAction
  55. {
  56. var $notices = null;
  57. var $next_id = null;
  58. /**
  59. * Take arguments for running
  60. *
  61. * @param array $args $_REQUEST args
  62. *
  63. * @return boolean success flag
  64. */
  65. protected function prepare(array $args=array())
  66. {
  67. parent::prepare($args);
  68. $this->target = $this->getTargetProfile($this->arg('id'));
  69. if (!($this->target instanceof Profile)) {
  70. // TRANS: Client error displayed requesting most recent notices for a non-existing user.
  71. $this->clientError(_('No such user.'), 404);
  72. }
  73. if (!$this->target->isLocal()) {
  74. $this->serverError(_('Remote user timelines are not available here yet.'), 501);
  75. }
  76. $this->notices = $this->getNotices();
  77. return true;
  78. }
  79. /**
  80. * Handle the request
  81. *
  82. * Just show the notices
  83. *
  84. * @return void
  85. */
  86. protected function handle()
  87. {
  88. parent::handle();
  89. if ($this->isPost()) {
  90. $this->handlePost();
  91. } else {
  92. $this->showTimeline();
  93. }
  94. }
  95. /**
  96. * Show the timeline of notices
  97. *
  98. * @return void
  99. */
  100. function showTimeline()
  101. {
  102. // We'll use the shared params from the Atom stub
  103. // for other feed types.
  104. $atom = new AtomUserNoticeFeed($this->target->getUser(), $this->scoped);
  105. $link = common_local_url(
  106. 'showstream',
  107. array('nickname' => $this->target->getNickname())
  108. );
  109. $self = $this->getSelfUri();
  110. // FriendFeed's SUP protocol
  111. // Also added RSS and Atom feeds
  112. $suplink = common_local_url('sup', null, null, $this->target->getID());
  113. header('X-SUP-ID: ' . $suplink);
  114. // paging links
  115. $nextUrl = !empty($this->next_id)
  116. ? common_local_url('ApiTimelineUser',
  117. array('format' => $this->format,
  118. 'id' => $this->target->getID()),
  119. array('max_id' => $this->next_id))
  120. : null;
  121. $prevExtra = array();
  122. if (!empty($this->notices)) {
  123. assert($this->notices[0] instanceof Notice);
  124. $prevExtra['since_id'] = $this->notices[0]->id;
  125. }
  126. $prevUrl = common_local_url('ApiTimelineUser',
  127. array('format' => $this->format,
  128. 'id' => $this->target->getID()),
  129. $prevExtra);
  130. $firstUrl = common_local_url('ApiTimelineUser',
  131. array('format' => $this->format,
  132. 'id' => $this->target->getID()));
  133. switch($this->format) {
  134. case 'xml':
  135. $this->showXmlTimeline($this->notices);
  136. break;
  137. case 'rss':
  138. $this->showRssTimeline(
  139. $this->notices,
  140. $atom->title,
  141. $link,
  142. $atom->subtitle,
  143. $suplink,
  144. $atom->logo,
  145. $self
  146. );
  147. break;
  148. case 'atom':
  149. header('Content-Type: application/atom+xml; charset=utf-8');
  150. $atom->setId($self);
  151. $atom->setSelfLink($self);
  152. // Add navigation links: next, prev, first
  153. // Note: we use IDs rather than pages for navigation; page boundaries
  154. // change too quickly!
  155. if (!empty($this->next_id)) {
  156. $atom->addLink($nextUrl,
  157. array('rel' => 'next',
  158. 'type' => 'application/atom+xml'));
  159. }
  160. if (($this->page > 1 || !empty($this->max_id)) && !empty($this->notices)) {
  161. $atom->addLink($prevUrl,
  162. array('rel' => 'prev',
  163. 'type' => 'application/atom+xml'));
  164. }
  165. if ($this->page > 1 || !empty($this->since_id) || !empty($this->max_id)) {
  166. $atom->addLink($firstUrl,
  167. array('rel' => 'first',
  168. 'type' => 'application/atom+xml'));
  169. }
  170. $atom->addEntryFromNotices($this->notices);
  171. $this->raw($atom->getString());
  172. break;
  173. case 'json':
  174. $this->showJsonTimeline($this->notices);
  175. break;
  176. case 'as':
  177. header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
  178. $doc = new ActivityStreamJSONDocument($this->scoped);
  179. $doc->setTitle($atom->title);
  180. $doc->addLink($link, 'alternate', 'text/html');
  181. $doc->addItemsFromNotices($this->notices);
  182. if (!empty($this->next_id)) {
  183. $doc->addLink($nextUrl,
  184. array('rel' => 'next',
  185. 'type' => ActivityStreamJSONDocument::CONTENT_TYPE));
  186. }
  187. if (($this->page > 1 || !empty($this->max_id)) && !empty($this->notices)) {
  188. $doc->addLink($prevUrl,
  189. array('rel' => 'prev',
  190. 'type' => ActivityStreamJSONDocument::CONTENT_TYPE));
  191. }
  192. if ($this->page > 1 || !empty($this->since_id) || !empty($this->max_id)) {
  193. $doc->addLink($firstUrl,
  194. array('rel' => 'first',
  195. 'type' => ActivityStreamJSONDocument::CONTENT_TYPE));
  196. }
  197. $this->raw($doc->asString());
  198. break;
  199. default:
  200. // TRANS: Client error displayed when coming across a non-supported API method.
  201. $this->clientError(_('API method not found.'), 404);
  202. }
  203. }
  204. /**
  205. * Get notices
  206. *
  207. * @return array notices
  208. */
  209. function getNotices()
  210. {
  211. $notices = array();
  212. $notice = $this->target->getNotices(($this->page-1) * $this->count,
  213. $this->count + 1,
  214. $this->since_id,
  215. $this->max_id,
  216. $this->scoped);
  217. while ($notice->fetch()) {
  218. if (count($notices) < $this->count) {
  219. $notices[] = clone($notice);
  220. } else {
  221. $this->next_id = $notice->id;
  222. break;
  223. }
  224. }
  225. return $notices;
  226. }
  227. /**
  228. * We expose AtomPub here, so non-GET/HEAD reqs must be read/write.
  229. *
  230. * @param array $args other arguments
  231. *
  232. * @return boolean true
  233. */
  234. function isReadOnly($args)
  235. {
  236. return ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD');
  237. }
  238. /**
  239. * When was this feed last modified?
  240. *
  241. * @return string datestamp of the latest notice in the stream
  242. */
  243. function lastModified()
  244. {
  245. if (!empty($this->notices) && (count($this->notices) > 0)) {
  246. return strtotime($this->notices[0]->created);
  247. }
  248. return null;
  249. }
  250. /**
  251. * An entity tag for this stream
  252. *
  253. * Returns an Etag based on the action name, language, user ID, and
  254. * timestamps of the first and last notice in the timeline
  255. *
  256. * @return string etag
  257. */
  258. function etag()
  259. {
  260. if (!empty($this->notices) && (count($this->notices) > 0)) {
  261. $last = count($this->notices) - 1;
  262. return '"' . implode(
  263. ':',
  264. array($this->arg('action'),
  265. common_user_cache_hash($this->scoped),
  266. common_language(),
  267. $this->target->getID(),
  268. strtotime($this->notices[0]->created),
  269. strtotime($this->notices[$last]->created))
  270. )
  271. . '"';
  272. }
  273. return null;
  274. }
  275. function handlePost()
  276. {
  277. if (!$this->scoped instanceof Profile ||
  278. !$this->target->sameAs($this->scoped)) {
  279. // TRANS: Client error displayed trying to add a notice to another user's timeline.
  280. $this->clientError(_('Only the user can add to their own timeline.'), 403);
  281. }
  282. // Only handle posts for Atom
  283. if ($this->format != 'atom') {
  284. // TRANS: Client error displayed when using another format than AtomPub.
  285. $this->clientError(_('Only accept AtomPub for Atom feeds.'));
  286. }
  287. $xml = trim(file_get_contents('php://input'));
  288. if (empty($xml)) {
  289. // TRANS: Client error displayed attempting to post an empty API notice.
  290. $this->clientError(_('Atom post must not be empty.'));
  291. }
  292. $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
  293. $dom = new DOMDocument();
  294. $ok = $dom->loadXML($xml);
  295. error_reporting($old);
  296. if (!$ok) {
  297. // TRANS: Client error displayed attempting to post an API that is not well-formed XML.
  298. $this->clientError(_('Atom post must be well-formed XML.'));
  299. }
  300. if ($dom->documentElement->namespaceURI != Activity::ATOM ||
  301. $dom->documentElement->localName != 'entry') {
  302. // TRANS: Client error displayed when not using an Atom entry.
  303. $this->clientError(_('Atom post must be an Atom entry.'));
  304. }
  305. $activity = new Activity($dom->documentElement);
  306. common_debug('AtomPub: Ignoring right now, but this POST was made to collection: '.$activity->id);
  307. // Reset activity data so we can handle it in the same functions as with OStatus
  308. // because we don't let clients set their own UUIDs... Not sure what AtomPub thinks
  309. // about that though.
  310. $activity->id = null;
  311. $activity->actor = null; // not used anyway, we use $this->target
  312. $activity->objects[0]->id = null;
  313. $stored = null;
  314. if (Event::handle('StartAtomPubNewActivity', array($activity, $this->target, &$stored))) {
  315. // TRANS: Client error displayed when not using the POST verb. Do not translate POST.
  316. throw new ClientException(_('Could not handle this Atom Activity.'));
  317. }
  318. if (!$stored instanceof Notice) {
  319. throw new ServerException('Server did not create a Notice object from handled AtomPub activity.');
  320. }
  321. Event::handle('EndAtomPubNewActivity', array($activity, $this->target, $stored));
  322. header('HTTP/1.1 201 Created');
  323. header("Location: " . common_local_url('ApiStatusesShow', array('id' => $stored->getID(),
  324. 'format' => 'atom')));
  325. $this->showSingleAtomStatus($stored);
  326. }
  327. }