activity.php 27 KB

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