activityobject.php 31 KB

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