apitimelineuser.php 13 KB

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