activityobject.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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('GNUSOCIAL')) { exit(1); }
  31. require_once(INSTALLDIR.'/lib/activitystreamjsondocument.php');
  32. /**
  33. * A noun-ish thing in the activity universe
  34. *
  35. * The activity streams spec talks about activity objects, while also having
  36. * a tag activity:object, which is in fact an activity object. Aaaaaah!
  37. *
  38. * This is just a thing in the activity universe. Can be the subject, object,
  39. * or indirect object (target!) of an activity verb. Rotten name, and I'm
  40. * propagating it. *sigh*
  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 ActivityObject
  50. {
  51. const ARTICLE = 'http://activitystrea.ms/schema/1.0/article';
  52. const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
  53. const NOTE = 'http://activitystrea.ms/schema/1.0/note';
  54. const STATUS = 'http://activitystrea.ms/schema/1.0/status';
  55. const FILE = 'http://activitystrea.ms/schema/1.0/file';
  56. const PHOTO = 'http://activitystrea.ms/schema/1.0/photo';
  57. const ALBUM = 'http://activitystrea.ms/schema/1.0/photo-album';
  58. const PLAYLIST = 'http://activitystrea.ms/schema/1.0/playlist';
  59. const VIDEO = 'http://activitystrea.ms/schema/1.0/video';
  60. const AUDIO = 'http://activitystrea.ms/schema/1.0/audio';
  61. const BOOKMARK = 'http://activitystrea.ms/schema/1.0/bookmark';
  62. const PERSON = 'http://activitystrea.ms/schema/1.0/person';
  63. const GROUP = 'http://activitystrea.ms/schema/1.0/group';
  64. const _LIST = 'http://activitystrea.ms/schema/1.0/list'; // LIST is reserved
  65. const PLACE = 'http://activitystrea.ms/schema/1.0/place';
  66. const COMMENT = 'http://activitystrea.ms/schema/1.0/comment';
  67. // ^^^^^^^^^^ tea!
  68. const ACTIVITY = 'http://activitystrea.ms/schema/1.0/activity';
  69. const SERVICE = 'http://activitystrea.ms/schema/1.0/service';
  70. const IMAGE = 'http://activitystrea.ms/schema/1.0/image';
  71. const COLLECTION = 'http://activitystrea.ms/schema/1.0/collection';
  72. const APPLICATION = 'http://activitystrea.ms/schema/1.0/application';
  73. // Atom elements we snarf
  74. const TITLE = 'title';
  75. const SUMMARY = 'summary';
  76. const ID = 'id';
  77. const SOURCE = 'source';
  78. const NAME = 'name';
  79. const URI = 'uri';
  80. const EMAIL = 'email';
  81. const POSTEROUS = 'http://posterous.com/help/rss/1.0';
  82. const AUTHOR = 'author';
  83. const USERIMAGE = 'userImage';
  84. const PROFILEURL = 'profileUrl';
  85. const NICKNAME = 'nickName';
  86. const DISPLAYNAME = 'displayName';
  87. public $element;
  88. public $type;
  89. public $id;
  90. public $title;
  91. public $summary;
  92. public $content;
  93. public $owner;
  94. public $link;
  95. public $source;
  96. public $avatarLinks = array();
  97. public $geopoint;
  98. public $poco;
  99. public $displayName;
  100. // @todo move this stuff to it's own PHOTO activity object
  101. const MEDIA_DESCRIPTION = 'description';
  102. public $thumbnail;
  103. public $largerImage;
  104. public $description;
  105. public $extra = array();
  106. public $stream;
  107. /**
  108. * Constructor
  109. *
  110. * This probably needs to be refactored
  111. * to generate a local class (ActivityPerson, ActivityFile, ...)
  112. * based on the object type.
  113. *
  114. * @param DOMElement $element DOM thing to turn into an Activity thing
  115. */
  116. function __construct($element = null)
  117. {
  118. if (empty($element)) {
  119. return;
  120. }
  121. $this->element = $element;
  122. $this->geopoint = $this->_childContent(
  123. $element,
  124. ActivityContext::POINT,
  125. ActivityContext::GEORSS
  126. );
  127. if ($element->tagName == 'author') {
  128. $this->_fromAuthor($element);
  129. } else if ($element->tagName == 'item') {
  130. $this->_fromRssItem($element);
  131. } else {
  132. $this->_fromAtomEntry($element);
  133. }
  134. // Some per-type attributes...
  135. if ($this->type == self::PERSON || $this->type == self::GROUP) {
  136. $this->displayName = $this->title;
  137. $photos = ActivityUtils::getLinks($element, 'photo');
  138. if (count($photos)) {
  139. foreach ($photos as $link) {
  140. $this->avatarLinks[] = new AvatarLink($link);
  141. }
  142. } else {
  143. $avatars = ActivityUtils::getLinks($element, 'avatar');
  144. foreach ($avatars as $link) {
  145. $this->avatarLinks[] = new AvatarLink($link);
  146. }
  147. }
  148. $this->poco = new PoCo($element);
  149. }
  150. if ($this->type == self::PHOTO) {
  151. $this->thumbnail = ActivityUtils::getLink($element, 'preview');
  152. $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
  153. $this->description = ActivityUtils::childContent(
  154. $element,
  155. ActivityObject::MEDIA_DESCRIPTION,
  156. Activity::MEDIA
  157. );
  158. }
  159. if ($this->type == self::_LIST) {
  160. $owner = ActivityUtils::child($this->element, Activity::AUTHOR, Activity::SPEC);
  161. $this->owner = new ActivityObject($owner);
  162. }
  163. }
  164. private function _fromAuthor($element)
  165. {
  166. $this->type = $this->_childContent($element,
  167. Activity::OBJECTTYPE,
  168. Activity::SPEC);
  169. if (empty($this->type)) {
  170. $this->type = self::PERSON; // XXX: is this fair?
  171. }
  172. // Start with <poco::displayName>
  173. $this->title = ActivityUtils::childContent($element, PoCo::DISPLAYNAME, PoCo::NS);
  174. // try falling back to <atom:title>
  175. if (empty($this->title)) {
  176. $title = ActivityUtils::childHtmlContent($element, self::TITLE);
  177. if (!empty($title)) {
  178. $this->title = common_strip_html($title);
  179. }
  180. }
  181. // fall back to <atom:name> as a last resort
  182. if (empty($this->title)) {
  183. $this->title = $this->_childContent($element, self::NAME);
  184. }
  185. // start with <atom:id>
  186. $this->id = $this->_childContent($element, self::ID);
  187. // fall back to <atom:uri>
  188. if (empty($this->id)) {
  189. $this->id = $this->_childContent($element, self::URI);
  190. }
  191. // fall further back to <atom:email>
  192. if (empty($this->id)) {
  193. $email = $this->_childContent($element, self::EMAIL);
  194. if (!empty($email)) {
  195. // XXX: acct: ?
  196. $this->id = 'mailto:'.$email;
  197. }
  198. }
  199. $this->link = ActivityUtils::getPermalink($element);
  200. // fall finally back to <link rel=alternate>
  201. if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
  202. $this->id = $this->link;
  203. }
  204. }
  205. private function _fromAtomEntry($element)
  206. {
  207. $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
  208. Activity::SPEC);
  209. if (empty($this->type)) {
  210. $this->type = ActivityObject::NOTE;
  211. }
  212. $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
  213. $this->content = ActivityUtils::getContent($element);
  214. // We don't like HTML in our titles, although it's technically allowed
  215. $this->title = common_strip_html(ActivityUtils::childHtmlContent($element, self::TITLE));
  216. $this->source = $this->_getSource($element);
  217. $this->link = ActivityUtils::getPermalink($element);
  218. $this->id = $this->_childContent($element, self::ID);
  219. if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
  220. $this->id = $this->link;
  221. }
  222. $els = $element->childNodes;
  223. $out = array();
  224. for ($i = 0; $i < $els->length; $i++) {
  225. $link = $els->item($i);
  226. if ($link->localName == ActivityUtils::LINK && $link->namespaceURI == ActivityUtils::ATOM) {
  227. $attrs = array();
  228. foreach ($link->attributes as $attrName=>$attrNode) {
  229. $attrs[$attrName] = $attrNode->nodeValue;
  230. }
  231. $this->extra[] = [$link->localName,
  232. $attrs,
  233. $link->nodeValue];
  234. }
  235. }
  236. }
  237. // @todo FIXME: rationalize with Activity::_fromRssItem()
  238. private function _fromRssItem($item)
  239. {
  240. if (empty($this->type)) {
  241. $this->type = ActivityObject::NOTE;
  242. }
  243. $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
  244. $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
  245. if (!empty($contentEl)) {
  246. $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
  247. } else {
  248. $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
  249. if (!empty($descriptionEl)) {
  250. $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
  251. }
  252. }
  253. $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
  254. $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
  255. if (!empty($guidEl)) {
  256. $this->id = $guidEl->textContent;
  257. if ($guidEl->hasAttribute('isPermaLink') && $guidEl->getAttribute('isPermaLink') != 'false') {
  258. // overwrites <link>
  259. $this->link = $this->id;
  260. }
  261. }
  262. }
  263. public static function fromRssAuthor($el)
  264. {
  265. $text = $el->textContent;
  266. if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
  267. $email = $match[1];
  268. $name = $match[2];
  269. } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
  270. $name = $match[1];
  271. $email = $match[2];
  272. } else if (preg_match('/.*@.*/', $text)) {
  273. $email = $text;
  274. $name = null;
  275. } else {
  276. $name = $text;
  277. $email = null;
  278. }
  279. // Not really enough info
  280. $obj = new ActivityObject();
  281. $obj->element = $el;
  282. $obj->type = ActivityObject::PERSON;
  283. $obj->title = $name;
  284. if (!empty($email)) {
  285. $obj->id = 'mailto:'.$email;
  286. }
  287. return $obj;
  288. }
  289. public static function fromDcCreator($el)
  290. {
  291. // Not really enough info
  292. $text = $el->textContent;
  293. $obj = new ActivityObject();
  294. $obj->element = $el;
  295. $obj->title = $text;
  296. $obj->type = ActivityObject::PERSON;
  297. return $obj;
  298. }
  299. public static function fromRssChannel($el)
  300. {
  301. $obj = new ActivityObject();
  302. $obj->element = $el;
  303. $obj->type = ActivityObject::PERSON; // @fixme guess better
  304. $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
  305. $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
  306. $obj->id = ActivityUtils::getLink($el, Activity::SELF);
  307. if (empty($obj->id)) {
  308. $obj->id = $obj->link;
  309. }
  310. $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
  311. if (!empty($desc)) {
  312. $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
  313. }
  314. $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
  315. if (!empty($imageEl)) {
  316. $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
  317. $al = new AvatarLink();
  318. $al->url = $url;
  319. $obj->avatarLinks[] = $al;
  320. }
  321. return $obj;
  322. }
  323. public static function fromPosterousAuthor($el)
  324. {
  325. $obj = new ActivityObject();
  326. $obj->type = ActivityObject::PERSON; // @fixme any others...?
  327. $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
  328. if (!empty($userImage)) {
  329. $al = new AvatarLink();
  330. $al->url = $userImage;
  331. $obj->avatarLinks[] = $al;
  332. }
  333. $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
  334. $obj->id = $obj->link;
  335. $obj->poco = new PoCo();
  336. $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
  337. $obj->poco->displayName = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
  338. $obj->title = $obj->poco->displayName;
  339. return $obj;
  340. }
  341. private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
  342. {
  343. return ActivityUtils::childContent($element, $tag, $namespace);
  344. }
  345. // Try to get a unique id for the source feed
  346. private function _getSource($element)
  347. {
  348. $sourceEl = ActivityUtils::child($element, 'source');
  349. if (empty($sourceEl)) {
  350. return null;
  351. } else {
  352. $href = ActivityUtils::getLink($sourceEl, 'self');
  353. if (!empty($href)) {
  354. return $href;
  355. } else {
  356. return ActivityUtils::childContent($sourceEl, 'id');
  357. }
  358. }
  359. }
  360. static function fromGroup(User_group $group)
  361. {
  362. $object = new ActivityObject();
  363. if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
  364. $object->type = ActivityObject::GROUP;
  365. $object->id = $group->getUri();
  366. $object->title = $group->getBestName();
  367. $object->link = $group->getUri();
  368. $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
  369. AVATAR_PROFILE_SIZE);
  370. $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
  371. AVATAR_STREAM_SIZE);
  372. $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
  373. AVATAR_MINI_SIZE);
  374. $object->poco = PoCo::fromGroup($group);
  375. Event::handle('EndActivityObjectFromGroup', array($group, &$object));
  376. }
  377. return $object;
  378. }
  379. static function fromPeopletag($ptag)
  380. {
  381. $object = new ActivityObject();
  382. if (Event::handle('StartActivityObjectFromPeopletag', array($ptag, &$object))) {
  383. $object->type = ActivityObject::_LIST;
  384. $object->id = $ptag->getUri();
  385. $object->title = $ptag->tag;
  386. $object->summary = $ptag->description;
  387. $object->link = $ptag->homeUrl();
  388. $object->owner = Profile::getKV('id', $ptag->tagger);
  389. $object->poco = PoCo::fromProfile($object->owner);
  390. Event::handle('EndActivityObjectFromPeopletag', array($ptag, &$object));
  391. }
  392. return $object;
  393. }
  394. static function fromFile(File $file)
  395. {
  396. $object = new ActivityObject();
  397. if (Event::handle('StartActivityObjectFromFile', array($file, &$object))) {
  398. $object->type = self::mimeTypeToObjectType($file->mimetype);
  399. $object->id = TagURI::mint(sprintf("file:%d", $file->id));
  400. $object->link = $file->getAttachmentUrl();
  401. if ($file->title) {
  402. $object->title = $file->title;
  403. }
  404. if ($file->date) {
  405. $object->date = $file->date;
  406. }
  407. try {
  408. $thumbnail = $file->getThumbnail();
  409. $object->thumbnail = $thumbnail;
  410. } catch (UseFileAsThumbnailException $e) {
  411. $object->thumbnail = null;
  412. } catch (UnsupportedMediaException $e) {
  413. $object->thumbnail = null;
  414. }
  415. switch (self::canonicalType($object->type)) {
  416. case 'image':
  417. $object->largerImage = $file->getUrl();
  418. break;
  419. case 'video':
  420. case 'audio':
  421. $object->stream = $file->getUrl();
  422. break;
  423. }
  424. Event::handle('EndActivityObjectFromFile', array($file, &$object));
  425. }
  426. return $object;
  427. }
  428. static function fromNoticeSource(Notice_source $source)
  429. {
  430. $object = new ActivityObject();
  431. $wellKnown = array('web', 'xmpp', 'mail', 'omb', 'system', 'api', 'ostatus',
  432. 'activity', 'feed', 'mirror', 'twitter', 'facebook');
  433. if (Event::handle('StartActivityObjectFromNoticeSource', array($source, &$object))) {
  434. $object->type = ActivityObject::APPLICATION;
  435. if (in_array($source->code, $wellKnown)) {
  436. // We use one ID for all well-known StatusNet sources
  437. $object->id = "tag:status.net,2009:notice-source:".$source->code;
  438. } else if ($source->url) {
  439. // They registered with an URL
  440. $object->id = $source->url;
  441. } else {
  442. // Locally-registered, no URL
  443. $object->id = TagURI::mint("notice-source:".$source->code);
  444. }
  445. if ($source->url) {
  446. $object->link = $source->url;
  447. }
  448. if ($source->name) {
  449. $object->title = $source->name;
  450. } else {
  451. $object->title = $source->code;
  452. }
  453. if ($source->created) {
  454. $object->date = $source->created;
  455. }
  456. $object->extra[] = array('status_net', array('source_code' => $source->code));
  457. Event::handle('EndActivityObjectFromNoticeSource', array($source, &$object));
  458. }
  459. return $object;
  460. }
  461. static function fromMessage(Message $message)
  462. {
  463. $object = new ActivityObject();
  464. if (Event::handle('StartActivityObjectFromMessage', array($message, &$object))) {
  465. $object->type = ActivityObject::NOTE;
  466. $object->id = ($message->uri) ? $message->uri : (($message->url) ? $message->url : TagURI::mint(sprintf("message:%d", $message->id)));
  467. $object->content = $message->rendered;
  468. $object->date = $message->created;
  469. if ($message->url) {
  470. $object->link = $message->url;
  471. } else {
  472. $object->link = common_local_url('showmessage', array('message' => $message->id));
  473. }
  474. $object->extra[] = array('status_net', array('message_id' => $message->id));
  475. Event::handle('EndActivityObjectFromMessage', array($message, &$object));
  476. }
  477. return $object;
  478. }
  479. function outputTo($xo, $tag='activity:object')
  480. {
  481. if (!empty($tag)) {
  482. $xo->elementStart($tag);
  483. }
  484. if (Event::handle('StartActivityObjectOutputAtom', array($this, $xo))) {
  485. $xo->element('activity:object-type', null, $this->type);
  486. // <author> uses URI
  487. if ($tag == 'author') {
  488. $xo->element(self::URI, null, $this->id);
  489. } else {
  490. $xo->element(self::ID, null, $this->id);
  491. }
  492. if (!empty($this->title)) {
  493. $name = common_xml_safe_str($this->title);
  494. if ($tag == 'author') {
  495. // XXX: Backward compatibility hack -- atom:name should contain
  496. // full name here, instead of nickname, i.e.: $name. Change
  497. // this in the next version.
  498. $xo->element(self::NAME, null, $this->poco->preferredUsername);
  499. } else {
  500. $xo->element(self::TITLE, null, $name);
  501. }
  502. }
  503. if (!empty($this->summary)) {
  504. $xo->element(
  505. self::SUMMARY,
  506. null,
  507. common_xml_safe_str($this->summary)
  508. );
  509. }
  510. if (!empty($this->content)) {
  511. // XXX: assuming HTML content here
  512. $xo->element(
  513. ActivityUtils::CONTENT,
  514. array('type' => 'html'),
  515. common_xml_safe_str($this->content)
  516. );
  517. }
  518. if (!empty($this->link)) {
  519. $xo->element(
  520. 'link',
  521. array(
  522. 'rel' => 'alternate',
  523. 'type' => 'text/html',
  524. 'href' => $this->link
  525. ),
  526. null
  527. );
  528. }
  529. if(!empty($this->owner)) {
  530. $owner = $this->owner->asActivityNoun(self::AUTHOR);
  531. $xo->raw($owner);
  532. }
  533. if ($this->type == ActivityObject::PERSON
  534. || $this->type == ActivityObject::GROUP) {
  535. foreach ($this->avatarLinks as $alink) {
  536. $xo->element('link',
  537. array(
  538. 'rel' => 'avatar',
  539. 'type' => $alink->type,
  540. 'media:width' => $alink->width,
  541. 'media:height' => $alink->height,
  542. 'href' => $alink->url,
  543. ),
  544. null);
  545. }
  546. }
  547. if (!empty($this->geopoint)) {
  548. $xo->element(
  549. 'georss:point',
  550. null,
  551. $this->geopoint
  552. );
  553. }
  554. if (!empty($this->poco)) {
  555. $this->poco->outputTo($xo);
  556. }
  557. // @fixme there's no way here to make a tree; elements can only contain plaintext
  558. // @fixme these may collide with JSON extensions
  559. foreach ($this->extra as $el) {
  560. list($extraTag, $attrs, $content) = array_pad($el, 3, null);
  561. $xo->element($extraTag, $attrs, $content);
  562. }
  563. Event::handle('EndActivityObjectOutputAtom', array($this, $xo));
  564. }
  565. if (!empty($tag)) {
  566. $xo->elementEnd($tag);
  567. }
  568. return;
  569. }
  570. function asString($tag='activity:object')
  571. {
  572. $xs = new XMLStringer(true);
  573. $this->outputTo($xs, $tag);
  574. return $xs->getString();
  575. }
  576. /*
  577. * Returns an array based on this Activity Object suitable for
  578. * encoding as JSON.
  579. *
  580. * @return array $object the activity object array
  581. */
  582. function asArray()
  583. {
  584. $object = array();
  585. if (Event::handle('StartActivityObjectOutputJson', array($this, &$object))) {
  586. // XXX: attachments are added by Activity
  587. // author (Add object for author? Could be useful for repeats.)
  588. // content (Add rendered version of the notice?)
  589. // downstreamDuplicates
  590. // id
  591. if ($this->id) {
  592. $object['id'] = $this->id;
  593. } else if ($this->link) {
  594. $object['id'] = $this->link;
  595. }
  596. if ($this->type == ActivityObject::PERSON
  597. || $this->type == ActivityObject::GROUP) {
  598. // displayName
  599. $object['displayName'] = $this->title;
  600. // XXX: Not sure what the best avatar is to use for the
  601. // author's "image". For now, I'm using the large size.
  602. $imgLink = null;
  603. $avatarMediaLinks = array();
  604. foreach ($this->avatarLinks as $a) {
  605. // Make a MediaLink for every other Avatar
  606. $avatar = new ActivityStreamsMediaLink(
  607. $a->url,
  608. $a->width,
  609. $a->height,
  610. $a->type,
  611. 'avatar'
  612. );
  613. // Find the big avatar to use as the "image"
  614. if ($a->height == AVATAR_PROFILE_SIZE) {
  615. $imgLink = $avatar;
  616. }
  617. $avatarMediaLinks[] = $avatar->asArray();
  618. }
  619. if (!array_key_exists('status_net', $object)) {
  620. $object['status_net'] = array();
  621. }
  622. $object['status_net']['avatarLinks'] = $avatarMediaLinks; // extension
  623. // image
  624. if (!empty($imgLink)) {
  625. $object['image'] = $imgLink->asArray();
  626. }
  627. }
  628. // objectType
  629. //
  630. // We can probably use the whole schema URL here but probably the
  631. // relative simple name is easier to parse
  632. $object['objectType'] = self::canonicalType($this->type);
  633. // summary
  634. $object['summary'] = $this->summary;
  635. // content, usually rendered HTML
  636. $object['content'] = $this->content;
  637. // published (probably don't need. Might be useful for repeats.)
  638. // updated (probably don't need this)
  639. // TODO: upstreamDuplicates
  640. if ($this->link) {
  641. $object['url'] = $this->link;
  642. }
  643. /* Extensions */
  644. // @fixme these may collide with XML extensions
  645. // @fixme multiple tags of same name will overwrite each other
  646. // @fixme text content from XML extensions will be lost
  647. foreach ($this->extra as $e) {
  648. list($objectName, $props, $txt) = array_pad($e, 3, null);
  649. if (!empty($objectName)) {
  650. $parts = explode(":", $objectName);
  651. if (count($parts) == 2 && $parts[0] == "statusnet") {
  652. if (!array_key_exists('status_net', $object)) {
  653. $object['status_net'] = array();
  654. }
  655. $object['status_net'][$parts[1]] = $props;
  656. } else {
  657. $object[$objectName] = $props;
  658. }
  659. }
  660. }
  661. if (!empty($this->geopoint)) {
  662. list($lat, $lon) = explode(' ', $this->geopoint);
  663. if (!empty($lat) && !empty($lon)) {
  664. $object['location'] = array(
  665. 'objectType' => 'place',
  666. 'position' => sprintf("%+02.5F%+03.5F/", $lat, $lon),
  667. 'lat' => $lat,
  668. 'lon' => $lon
  669. );
  670. $loc = Location::fromLatLon((float)$lat, (float)$lon);
  671. if ($loc) {
  672. $name = $loc->getName();
  673. if ($name) {
  674. $object['location']['displayName'] = $name;
  675. }
  676. $url = $loc->getURL();
  677. if ($url) {
  678. $object['location']['url'] = $url;
  679. }
  680. }
  681. }
  682. }
  683. if (!empty($this->poco)) {
  684. $object['portablecontacts_net'] = array_filter($this->poco->asArray());
  685. }
  686. if (!empty($this->thumbnail)) {
  687. if (is_string($this->thumbnail)) {
  688. $object['image'] = array('url' => $this->thumbnail);
  689. } else {
  690. $object['image'] = array('url' => $this->thumbnail->getUrl());
  691. if ($this->thumbnail->width) {
  692. $object['image']['width'] = $this->thumbnail->width;
  693. }
  694. if ($this->thumbnail->height) {
  695. $object['image']['height'] = $this->thumbnail->height;
  696. }
  697. }
  698. }
  699. switch (self::canonicalType($this->type)) {
  700. case 'image':
  701. if (!empty($this->largerImage)) {
  702. $object['fullImage'] = array('url' => $this->largerImage);
  703. }
  704. break;
  705. case 'audio':
  706. case 'video':
  707. if (!empty($this->stream)) {
  708. $object['stream'] = array('url' => $this->stream);
  709. }
  710. break;
  711. }
  712. Event::handle('EndActivityObjectOutputJson', array($this, &$object));
  713. }
  714. return array_filter($object);
  715. }
  716. public function getIdentifiers() {
  717. $ids = array();
  718. foreach(array('id', 'link', 'url') as $id) {
  719. if (isset($this->$id)) {
  720. $ids[] = $this->$id;
  721. }
  722. }
  723. return array_unique($ids);
  724. }
  725. static function canonicalType($type) {
  726. return ActivityUtils::resolveUri($type, true);
  727. }
  728. static function mimeTypeToObjectType($mimeType) {
  729. $ot = null;
  730. // Default
  731. if (empty($mimeType)) {
  732. return self::FILE;
  733. }
  734. $parts = explode('/', $mimeType);
  735. switch ($parts[0]) {
  736. case 'image':
  737. $ot = self::IMAGE;
  738. break;
  739. case 'audio':
  740. $ot = self::AUDIO;
  741. break;
  742. case 'video':
  743. $ot = self::VIDEO;
  744. break;
  745. default:
  746. $ot = self::FILE;
  747. }
  748. return $ot;
  749. }
  750. }