activityobject.php 29 KB

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