activity.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * An activity
  18. *
  19. * @category Feed
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @author Zach Copley <zach@status.net>
  23. * @copyright 2010 StatusNet, Inc.
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die();
  27. /**
  28. * An activity in the ActivityStrea.ms world
  29. *
  30. * An activity is kind of like a sentence: someone did something
  31. * to something else.
  32. *
  33. * 'someone' is the 'actor'; 'did something' is the verb;
  34. * 'something else' is the object.
  35. *
  36. * @category OStatus
  37. * @package GNUsocial
  38. * @author Evan Prodromou <evan@status.net>
  39. * @copyright 2010 StatusNet, Inc.
  40. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  41. */
  42. class Activity
  43. {
  44. const SPEC = 'http://activitystrea.ms/spec/1.0/';
  45. const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
  46. const MEDIA = 'http://purl.org/syndication/atommedia';
  47. const VERB = 'verb';
  48. const OBJECT = 'object';
  49. const ACTOR = 'actor';
  50. const SUBJECT = 'subject';
  51. const OBJECTTYPE = 'object-type';
  52. const CONTEXT = 'context';
  53. const TARGET = 'target';
  54. const ATOM = 'http://www.w3.org/2005/Atom';
  55. const AUTHOR = 'author';
  56. const PUBLISHED = 'published';
  57. const UPDATED = 'updated';
  58. const RSS = null; // no namespace!
  59. const PUBDATE = 'pubDate';
  60. const DESCRIPTION = 'description';
  61. const GUID = 'guid';
  62. const SELF = 'self';
  63. const IMAGE = 'image';
  64. const URL = 'url';
  65. const DC = 'http://purl.org/dc/elements/1.1/';
  66. const CREATOR = 'creator';
  67. const CONTENTNS = 'http://purl.org/rss/1.0/modules/content/';
  68. const ENCODED = 'encoded';
  69. public $actor; // an ActivityObject
  70. public $verb; // a string (the URL)
  71. public $objects = array(); // an array of ActivityObjects
  72. public $target; // an ActivityObject
  73. public $context; // an ActivityObject
  74. public $time; // Time of the activity
  75. public $link; // an ActivityObject
  76. public $entry; // the source entry
  77. public $feed; // the source feed
  78. public $summary; // summary of activity
  79. public $content; // HTML content of activity
  80. public $id; // ID of the activity
  81. public $title; // title of the activity
  82. public $categories = array(); // list of AtomCategory objects
  83. public $enclosures = array(); // list of enclosure URL references
  84. public $attachments = array(); // list of attachments
  85. public $extra = array(); // extra elements as array(tag, attrs, content)
  86. public $source; // ActivitySource object representing 'home feed'
  87. public $selfLink; // <link rel='self' type='application/atom+xml'>
  88. public $editLink; // <link rel='edit' type='application/atom+xml'>
  89. public $generator; // ActivityObject representing the generating application
  90. /**
  91. * Turns a regular old Atom <entry> into a magical activity
  92. *
  93. * @param DOMElement $entry Atom entry to poke at
  94. * @param DOMElement $feed Atom feed, for context
  95. */
  96. public function __construct($entry = null, $feed = null)
  97. {
  98. if (is_null($entry)) {
  99. return;
  100. }
  101. // Insist on a feed's root DOMElement; don't allow a DOMDocument
  102. if ($feed instanceof DOMDocument) {
  103. throw new ClientException(
  104. // TRANS: Client exception thrown when a feed instance is a DOMDocument.
  105. _('Expecting a root feed element but got a whole XML document.')
  106. );
  107. }
  108. $this->entry = $entry;
  109. $this->feed = $feed;
  110. if (
  111. $entry->namespaceURI === Activity::ATOM
  112. && $entry->localName === 'entry'
  113. ) {
  114. $this->_fromAtomEntry($entry, $feed);
  115. } elseif (
  116. $entry->namespaceURI === Activity::RSS
  117. && $entry->localName === 'item'
  118. ) {
  119. $this->_fromRssItem($entry, $feed);
  120. } elseif (
  121. $entry->namespaceURI === Activity::SPEC
  122. && $entry->localName === 'object'
  123. ) {
  124. $this->_fromAtomEntry($entry, $feed);
  125. } else {
  126. // Low level exception. No need for i18n.
  127. throw new Exception("Unknown DOM element: {$entry->namespaceURI} {$entry->localName}");
  128. }
  129. }
  130. public function _fromAtomEntry($entry, $feed)
  131. {
  132. $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
  133. if (!empty($pubEl)) {
  134. $this->time = strtotime($pubEl->textContent);
  135. } else {
  136. // XXX technically an error; being liberal. Good idea...?
  137. $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
  138. if (!empty($updateEl)) {
  139. $this->time = strtotime($updateEl->textContent);
  140. } else {
  141. $this->time = null;
  142. }
  143. }
  144. $this->link = ActivityUtils::getPermalink($entry);
  145. $verbEl = $this->_child($entry, self::VERB);
  146. if (!empty($verbEl)) {
  147. $this->verb = trim($verbEl->textContent);
  148. } else {
  149. $this->verb = ActivityVerb::POST;
  150. // XXX: do other implied stuff here
  151. }
  152. // get immediate object children
  153. $objectEls = ActivityUtils::children($entry, self::OBJECT, self::SPEC);
  154. if (count($objectEls) > 0) {
  155. foreach ($objectEls as $objectEl) {
  156. // Special case for embedded activities
  157. $objectType = ActivityUtils::childContent($objectEl, self::OBJECTTYPE, self::SPEC);
  158. if ((!empty($objectType) && $objectType == ActivityObject::ACTIVITY) || $this->verb == ActivityVerb::SHARE) {
  159. $this->objects[] = new Activity($objectEl);
  160. } else {
  161. $this->objects[] = new ActivityObject($objectEl);
  162. }
  163. }
  164. } else {
  165. // XXX: really?
  166. $this->objects[] = new ActivityObject($entry);
  167. }
  168. if (!empty($actorEl = $this->_child($entry, self::ACTOR))) {
  169. // Standalone <activity:actor> elements are a holdover from older
  170. // versions of ActivityStreams. Newer feeds should have this data
  171. // integrated straight into <atom:author>.
  172. $this->actor = new ActivityObject($actorEl);
  173. // Cliqset has bad actor IDs (just nickname of user). We
  174. // work around it by getting the author data and using its
  175. // id instead
  176. if (!preg_match('/^\w+:/', $this->actor->id)) {
  177. $authorEl = ActivityUtils::child($entry, 'author');
  178. if (!empty($authorEl)) {
  179. $authorObj = new ActivityObject($authorEl);
  180. $this->actor->id = $authorObj->id;
  181. }
  182. }
  183. } elseif (!empty($authorEl = $this->_child(
  184. $entry,
  185. self::AUTHOR,
  186. self::ATOM
  187. ))) {
  188. // An <atom:author> in the entry overrides any author info on
  189. // the surrounding feed.
  190. $this->actor = new ActivityObject($authorEl);
  191. } elseif (
  192. !empty($feed)
  193. && !empty($subjectEl = $this->_child($feed, self::SUBJECT))
  194. ) {
  195. // Feed subject is used for things like groups.
  196. // Should actually possibly not be interpreted as an actor...?
  197. $this->actor = new ActivityObject($subjectEl);
  198. } elseif (
  199. !empty($feed)
  200. && !empty($authorEl = $this->_child($feed, self::AUTHOR, self::ATOM))
  201. ) {
  202. // If there's no <atom:author> on the entry, it's safe to assume
  203. // the containing feed's authorship info applies.
  204. $this->actor = new ActivityObject($authorEl);
  205. }
  206. $contextEl = $this->_child($entry, self::CONTEXT);
  207. if (!empty($contextEl)) {
  208. $this->context = new ActivityContext($contextEl);
  209. } else {
  210. $this->context = new ActivityContext($entry);
  211. }
  212. $targetEl = $this->_child($entry, self::TARGET);
  213. if (!empty($targetEl)) {
  214. $this->target = new ActivityObject($targetEl);
  215. } elseif (ActivityUtils::compareVerbs($this->verb, array(ActivityVerb::FAVORITE))) {
  216. // StatusNet didn't send a 'target' for their Favorite atom entries
  217. $this->target = clone($this->objects[0]);
  218. }
  219. $this->summary = ActivityUtils::childContent($entry, 'summary');
  220. $this->id = ActivityUtils::childContent($entry, 'id');
  221. $this->content = ActivityUtils::getContent($entry);
  222. $catEls = $entry->getElementsByTagNameNS(self::ATOM, 'category');
  223. if ($catEls) {
  224. for ($i = 0; $i < $catEls->length; $i++) {
  225. $catEl = $catEls->item($i);
  226. $this->categories[] = new AtomCategory($catEl);
  227. }
  228. }
  229. foreach (ActivityUtils::getLinks($entry, 'enclosure') as $link) {
  230. $this->enclosures[] = $link->getAttribute('href');
  231. }
  232. // From APP. Might be useful.
  233. $this->selfLink = ActivityUtils::getSelfLink($entry);
  234. $this->editLink = ActivityUtils::getLink($entry, 'edit', 'application/atom+xml');
  235. }
  236. public function _fromRssItem($item, $channel)
  237. {
  238. $verbEl = $this->_child($item, self::VERB);
  239. if (!empty($verbEl)) {
  240. $this->verb = trim($verbEl->textContent);
  241. } else {
  242. $this->verb = ActivityVerb::POST;
  243. // XXX: do other implied stuff here
  244. }
  245. $pubDateEl = $this->_child($item, self::PUBDATE, self::RSS);
  246. if (!empty($pubDateEl)) {
  247. $this->time = strtotime($pubDateEl->textContent);
  248. }
  249. if (!empty($authorEl = $this->_child(
  250. $item,
  251. self::AUTHOR,
  252. self::RSS
  253. ))) {
  254. $this->actor = ActivityObject::fromRssAuthor($authorEl);
  255. } elseif (!empty($dcCreatorEl = $this->_child(
  256. $item,
  257. self::CREATOR,
  258. self::DC
  259. ))) {
  260. $this->actor = ActivityObject::fromDcCreator($dcCreatorEl);
  261. } elseif (!empty($posterousEl = $this->_child(
  262. $item,
  263. ActivityObject::AUTHOR,
  264. ActivityObject::POSTEROUS
  265. ))) {
  266. // Special case for Posterous.com
  267. $this->actor = ActivityObject::fromPosterousAuthor($posterousEl);
  268. } elseif (!empty($channel)) {
  269. $this->actor = ActivityObject::fromRssChannel($channel);
  270. } else {
  271. // No actor!
  272. }
  273. $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, self::RSS);
  274. $contentEl = ActivityUtils::child($item, self::ENCODED, self::CONTENTNS);
  275. if (!empty($contentEl)) {
  276. // <content:encoded> XML node's text content is HTML; no further processing needed.
  277. $this->content = $contentEl->textContent;
  278. } else {
  279. $descriptionEl = ActivityUtils::child($item, self::DESCRIPTION, self::RSS);
  280. if (!empty($descriptionEl)) {
  281. // Per spec, <description> must be plaintext.
  282. // In practice, often there's HTML... but these days good
  283. // feeds are using <content:encoded> which is explicitly
  284. // real HTML.
  285. // We'll treat this following spec, and do HTML escaping
  286. // to convert from plaintext to HTML.
  287. $this->content = htmlspecialchars($descriptionEl->textContent);
  288. }
  289. }
  290. $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, self::RSS);
  291. // @fixme enclosures
  292. // @fixme thumbnails... maybe
  293. $guidEl = ActivityUtils::child($item, self::GUID, self::RSS);
  294. if (!empty($guidEl)) {
  295. $this->id = $guidEl->textContent;
  296. if ($guidEl->hasAttribute('isPermaLink') && $guidEl->getAttribute('isPermaLink') != 'false') {
  297. // overwrites <link>
  298. $this->link = $this->id;
  299. }
  300. }
  301. $this->objects[] = new ActivityObject($item);
  302. $this->context = new ActivityContext($item);
  303. }
  304. /**
  305. * Returns an Atom <entry> based on this activity
  306. *
  307. * @return DOMElement Atom entry
  308. */
  309. public function toAtomEntry()
  310. {
  311. return null;
  312. }
  313. /**
  314. * Returns an array based on this activity suitable
  315. * for encoding as a JSON object
  316. *
  317. * @return array $activity
  318. */
  319. public function asArray()
  320. {
  321. $activity = array();
  322. // actor
  323. $activity['actor'] = $this->actor->asArray();
  324. // content
  325. $activity['content'] = $this->content;
  326. // generator
  327. if (!empty($this->generator)) {
  328. $activity['generator'] = $this->generator->asArray();
  329. }
  330. // icon <-- possibly a mini object representing verb?
  331. // id
  332. $activity['id'] = $this->id;
  333. // object
  334. if (count($this->objects) == 0) {
  335. common_log(LOG_ERR, "Can't save " . $this->id);
  336. } else {
  337. if (count($this->objects) > 1) {
  338. common_log(LOG_WARNING, "Ignoring " . (count($this->objects) - 1) . " extra objects in JSON output for activity " . $this->id);
  339. }
  340. $object = $this->objects[0];
  341. if ($object instanceof Activity) {
  342. // Sharing a post activity is more like sharing the original object
  343. if (ActivityVerb::canonical($this->verb) == ActivityVerb::canonical(ActivityVerb::SHARE) &&
  344. ActivityVerb::canonical($object->verb) == ActivityVerb::canonical(ActivityVerb::POST)) {
  345. // XXX: Here's one for the obfuscation record books
  346. $object = $object->objects[0];
  347. }
  348. }
  349. $activity['object'] = $object->asArray();
  350. if ($object instanceof Activity) {
  351. $activity['object']['objectType'] = 'activity';
  352. }
  353. foreach ($this->attachments as $attachment) {
  354. if (empty($activity['object']['attachments'])) {
  355. $activity['object']['attachments'] = array();
  356. }
  357. $activity['object']['attachments'][] = $attachment->asArray();
  358. }
  359. }
  360. // Context stuff.
  361. if (!empty($this->context)) {
  362. if (!empty($this->context->location)) {
  363. $loc = $this->context->location;
  364. $activity['location'] = array(
  365. 'objectType' => 'place',
  366. 'position' => sprintf("%+02.5F%+03.5F/", $loc->lat, $loc->lon),
  367. 'lat' => $loc->lat,
  368. 'lon' => $loc->lon
  369. );
  370. $name = $loc->getName();
  371. if ($name) {
  372. $activity['location']['displayName'] = $name;
  373. }
  374. $url = $loc->getURL();
  375. if ($url) {
  376. $activity['location']['url'] = $url;
  377. }
  378. }
  379. $activity['to'] = $this->context->getToArray();
  380. $ctxarr = $this->context->asArray();
  381. if (array_key_exists('inReplyTo', $ctxarr)) {
  382. $activity['object']['inReplyTo'] = $ctxarr['inReplyTo'];
  383. unset($ctxarr['inReplyTo']);
  384. }
  385. if (!array_key_exists('status_net', $activity)) {
  386. $activity['status_net'] = array();
  387. }
  388. foreach ($ctxarr as $key => $value) {
  389. $activity['status_net'][$key] = $value;
  390. }
  391. }
  392. // published
  393. $activity['published'] = self::iso8601Date($this->time);
  394. // provider
  395. $provider = array(
  396. 'objectType' => 'service',
  397. 'displayName' => common_config('site', 'name'),
  398. 'url' => common_root_url()
  399. );
  400. $activity['provider'] = $provider;
  401. // target
  402. if (!empty($this->target)) {
  403. $activity['target'] = $this->target->asArray();
  404. }
  405. // title
  406. $activity['title'] = $this->title;
  407. // updated <-- Optional. Should we use this to indicate the time we r
  408. // eceived a remote notice? Probably not.
  409. // verb
  410. $activity['verb'] = ActivityVerb::canonical($this->verb);
  411. // url
  412. if ($this->link) {
  413. $activity['url'] = $this->link;
  414. }
  415. /* Purely extensions hereafter */
  416. if ($activity['verb'] == 'post') {
  417. $tags = array();
  418. foreach ($this->categories as $cat) {
  419. if (mb_strlen($cat->term) > 0) {
  420. // Couldn't figure out which object type to use, so...
  421. $tags[] = array('objectType' => 'http://activityschema.org/object/hashtag',
  422. 'displayName' => $cat->term);
  423. }
  424. }
  425. if (count($tags) > 0) {
  426. $activity['object']['tags'] = $tags;
  427. }
  428. }
  429. // XXX: a bit of a hack... Since JSON isn't namespaced we probably
  430. // shouldn't be using 'statusnet:notice_info', but this will work
  431. // for the moment.
  432. foreach ($this->extra as $e) {
  433. list($objectName, $props, $txt) = $e;
  434. if (!empty($objectName)) {
  435. $parts = explode(":", $objectName);
  436. if (count($parts) == 2 && $parts[0] == "statusnet") {
  437. if (!array_key_exists('status_net', $activity)) {
  438. $activity['status_net'] = array();
  439. }
  440. $activity['status_net'][$parts[1]] = $props;
  441. } else {
  442. $activity[$objectName] = $props;
  443. }
  444. }
  445. }
  446. return array_filter($activity);
  447. }
  448. public function asString(
  449. $namespace = false,
  450. $author = true,
  451. $source = false
  452. ) {
  453. $xs = new XMLStringer(true);
  454. $this->outputTo($xs, $namespace, $author, $source);
  455. return $xs->getString();
  456. }
  457. public function outputTo(
  458. $xs,
  459. $namespace = false,
  460. $author = true,
  461. $source = false,
  462. $tag = 'entry'
  463. ) {
  464. if ($namespace) {
  465. $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
  466. 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0',
  467. 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
  468. 'xmlns:georss' => 'http://www.georss.org/georss',
  469. 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
  470. 'xmlns:poco' => 'http://portablecontacts.net/spec/1.0',
  471. 'xmlns:media' => 'http://purl.org/syndication/atommedia',
  472. 'xmlns:statusnet' => 'http://status.net/schema/api/1/');
  473. } else {
  474. $attrs = array();
  475. }
  476. $xs->elementStart($tag, $attrs);
  477. if ($tag != 'entry') {
  478. $xs->element('activity:object-type', null, ActivityObject::ACTIVITY);
  479. }
  480. if ($this->verb == ActivityVerb::POST && count($this->objects) == 1 && $tag == 'entry') {
  481. $obj = $this->objects[0];
  482. $obj->outputTo($xs, null);
  483. } else {
  484. $xs->element('id', null, $this->id);
  485. if ($this->title) {
  486. $xs->element('title', null, $this->title);
  487. } else {
  488. // Require element
  489. $xs->element('title', null, "");
  490. }
  491. $xs->element('content', array('type' => 'html'), $this->content);
  492. if (!empty($this->summary)) {
  493. $xs->element('summary', null, $this->summary);
  494. }
  495. if (!empty($this->link)) {
  496. $xs->element('link', array('rel' => 'alternate',
  497. 'type' => 'text/html',
  498. 'href' => $this->link));
  499. }
  500. }
  501. $xs->element('activity:verb', null, $this->verb);
  502. $published = self::iso8601Date($this->time);
  503. $xs->element('published', null, $published);
  504. $xs->element('updated', null, $published);
  505. if ($author) {
  506. $this->actor->outputTo($xs, 'author');
  507. }
  508. if ($this->verb != ActivityVerb::POST || count($this->objects) != 1 || $tag != 'entry') {
  509. foreach ($this->objects as $object) {
  510. if ($object instanceof Activity) {
  511. $object->outputTo($xs, false, true, true, 'activity:object');
  512. } else {
  513. $object->outputTo($xs, 'activity:object');
  514. }
  515. }
  516. }
  517. if (!empty($this->context)) {
  518. if (!empty($this->context->replyToID)) {
  519. if (!empty($this->context->replyToUrl)) {
  520. $xs->element('thr:in-reply-to', [
  521. 'ref' => $this->context->replyToID,
  522. 'href' => $this->context->replyToUrl,
  523. ]);
  524. } else {
  525. $xs->element('thr:in-reply-to', [
  526. 'ref' => $this->context->replyToID,
  527. ]);
  528. }
  529. }
  530. if (!empty($this->context->replyToUrl)) {
  531. $xs->element('link', [
  532. 'rel' => 'related',
  533. 'href' => $this->context->replyToUrl,
  534. ]);
  535. }
  536. if (!empty($this->context->conversation)) {
  537. $convattr = [];
  538. $conv = Conversation::getKV('uri', $this->context->conversation);
  539. if ($conv instanceof Conversation) {
  540. $convattr['href'] = $conv->getUrl();
  541. $convattr['local_id'] = $conv->getID();
  542. $convattr['ref'] = $conv->getUri();
  543. $xs->element('link', [
  544. 'rel' => 'ostatus:' . ActivityContext::CONVERSATION,
  545. 'href' => $convattr['href'],
  546. ]);
  547. } else {
  548. $convattr['ref'] = $this->context->conversation;
  549. }
  550. $xs->element(
  551. 'ostatus:' . ActivityContext::CONVERSATION,
  552. $convattr,
  553. $this->context->conversation
  554. );
  555. /* Since we use XMLWriter we just use the previously hardcoded prefix for ostatus,
  556. otherwise we should use something like this:
  557. $xs->elementNS(array(ActivityContext::OSTATUS => 'ostatus'), // namespace
  558. ActivityContext::CONVERSATION,
  559. null, // attributes
  560. $this->context->conversation); // content
  561. */
  562. }
  563. foreach ($this->context->attention as $attnURI=>$type) {
  564. $xs->element('link', [
  565. 'rel' => ActivityContext::MENTIONED,
  566. ActivityContext::OBJECTTYPE => $type, // @fixme undocumented
  567. 'href' => $attnURI,
  568. ]);
  569. }
  570. if (!empty($this->context->location)) {
  571. $loc = $this->context->location;
  572. $xs->element('georss:point', null, $loc->lat . ' ' . $loc->lon);
  573. }
  574. }
  575. if ($this->target) {
  576. $this->target->outputTo($xs, 'activity:target');
  577. }
  578. foreach ($this->categories as $cat) {
  579. $cat->outputTo($xs);
  580. }
  581. // can be either URLs or enclosure objects
  582. foreach ($this->enclosures as $enclosure) {
  583. if (is_string($enclosure)) {
  584. $xs->element('link', array('rel' => 'enclosure',
  585. 'href' => $enclosure));
  586. } else {
  587. $attributes = array('rel' => 'enclosure',
  588. 'href' => $enclosure->url,
  589. 'type' => $enclosure->mimetype,
  590. 'length' => $enclosure->size);
  591. if ($enclosure->title) {
  592. $attributes['title'] = $enclosure->title;
  593. }
  594. $xs->element('link', $attributes);
  595. }
  596. }
  597. // Info on the source feed
  598. if ($source && !empty($this->source)) {
  599. $xs->elementStart('source');
  600. $xs->element('id', null, $this->source->id);
  601. $xs->element('title', null, $this->source->title);
  602. if (array_key_exists('alternate', $this->source->links)) {
  603. $xs->element('link', array('rel' => 'alternate',
  604. 'type' => 'text/html',
  605. 'href' => $this->source->links['alternate']));
  606. }
  607. if (array_key_exists('self', $this->source->links)) {
  608. $xs->element('link', array('rel' => 'self',
  609. 'type' => 'application/atom+xml',
  610. 'href' => $this->source->links['self']));
  611. }
  612. if (array_key_exists('license', $this->source->links)) {
  613. $xs->element('link', array('rel' => 'license',
  614. 'href' => $this->source->links['license']));
  615. }
  616. if (!empty($this->source->icon)) {
  617. $xs->element('icon', null, $this->source->icon);
  618. }
  619. if (!empty($this->source->updated)) {
  620. $xs->element('updated', null, $this->source->updated);
  621. }
  622. $xs->elementEnd('source');
  623. }
  624. if (!empty($this->selfLink)) {
  625. $xs->element('link', array('rel' => 'self',
  626. 'type' => 'application/atom+xml',
  627. 'href' => $this->selfLink));
  628. }
  629. if (!empty($this->editLink)) {
  630. $xs->element('link', array('rel' => 'edit',
  631. 'type' => 'application/atom+xml',
  632. 'href' => $this->editLink));
  633. }
  634. // For throwing in extra elements; used for statusnet:notice_info
  635. foreach ($this->extra as $el) {
  636. list($tag, $attrs, $content) = $el;
  637. $xs->element($tag, $attrs, $content);
  638. }
  639. $xs->elementEnd($tag);
  640. return;
  641. }
  642. private function _child($element, $tag, $namespace=self::SPEC)
  643. {
  644. return ActivityUtils::child($element, $tag, $namespace);
  645. }
  646. /**
  647. * For consistency, we'll always output UTC rather than local time.
  648. * Note that clients *should* accept any timezone we give them as long
  649. * as it's properly formatted.
  650. *
  651. * @param int $tm Unix timestamp
  652. * @return string
  653. */
  654. public static function iso8601Date($tm)
  655. {
  656. $dateStr = date('d F Y H:i:s', $tm);
  657. $d = new DateTime($dateStr, new DateTimeZone('UTC'));
  658. return $d->format('c');
  659. }
  660. }