The official, canonical postActiv repository. http://www.postactiv.com

apitimelineuser.php 14KB

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