3 * StatusNet, the distributed open-source microblogging tool
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.
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.
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/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @copyright 2010 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
27 * @link http://status.net/
30 if (!defined('STATUSNET')) {
35 * Utilities for turning DOMish things into Activityish things
37 * Some common functions that I didn't have the bandwidth to try to factor
38 * into some kind of reasonable superclass, so just dumped here. Might
39 * be useful to have an ActivityObject parent class or something.
43 * @author Evan Prodromou <evan@status.net>
44 * @copyright 2010 StatusNet, Inc.
45 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
46 * @link http://status.net/
51 const ATOM = 'http://www.w3.org/2005/Atom';
59 * Get the permalink for an Activity object
61 * @param DOMElement $element A DOM element
63 * @return string related link, if any
66 static function getPermalink($element)
68 return self::getLink($element, 'alternate', 'text/html');
72 * Get the permalink for an Activity object
74 * @param DOMElement $element A DOM element
76 * @return string related link, if any
79 static function getLink($element, $rel, $type=null)
81 $links = $element->getElementsByTagnameNS(self::ATOM, self::LINK);
83 foreach ($links as $link) {
85 $linkRel = $link->getAttribute(self::REL);
86 $linkType = $link->getAttribute(self::TYPE);
88 if ($linkRel == $rel &&
89 (is_null($type) || $linkType == $type)) {
90 return $link->getAttribute(self::HREF);
98 * Gets the first child element with the given tag
100 * @param DOMElement $element element to pick at
101 * @param string $tag tag to look for
102 * @param string $namespace Namespace to look under
104 * @return DOMElement found element or null
107 static function child($element, $tag, $namespace=self::ATOM)
109 $els = $element->childNodes;
110 if (empty($els) || $els->length == 0) {
113 for ($i = 0; $i < $els->length; $i++) {
114 $el = $els->item($i);
115 if ($el->localName == $tag && $el->namespaceURI == $namespace) {
123 * Grab the text content of a DOM element child of the current element
125 * @param DOMElement $element Element whose children we examine
126 * @param string $tag Tag to look up
127 * @param string $namespace Namespace to use, defaults to Atom
129 * @return string content of the child
132 static function childContent($element, $tag, $namespace=self::ATOM)
134 $el = self::child($element, $tag, $namespace);
139 return $el->textContent;
145 * A noun-ish thing in the activity universe
147 * The activity streams spec talks about activity objects, while also having
148 * a tag activity:object, which is in fact an activity object. Aaaaaah!
150 * This is just a thing in the activity universe. Can be the subject, object,
151 * or indirect object (target!) of an activity verb. Rotten name, and I'm
152 * propagating it. *sigh*
156 * @author Evan Prodromou <evan@status.net>
157 * @copyright 2010 StatusNet, Inc.
158 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
159 * @link http://status.net/
164 const ARTICLE = 'http://activitystrea.ms/schema/1.0/article';
165 const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
166 const NOTE = 'http://activitystrea.ms/schema/1.0/note';
167 const STATUS = 'http://activitystrea.ms/schema/1.0/status';
168 const FILE = 'http://activitystrea.ms/schema/1.0/file';
169 const PHOTO = 'http://activitystrea.ms/schema/1.0/photo';
170 const ALBUM = 'http://activitystrea.ms/schema/1.0/photo-album';
171 const PLAYLIST = 'http://activitystrea.ms/schema/1.0/playlist';
172 const VIDEO = 'http://activitystrea.ms/schema/1.0/video';
173 const AUDIO = 'http://activitystrea.ms/schema/1.0/audio';
174 const BOOKMARK = 'http://activitystrea.ms/schema/1.0/bookmark';
175 const PERSON = 'http://activitystrea.ms/schema/1.0/person';
176 const GROUP = 'http://activitystrea.ms/schema/1.0/group';
177 const PLACE = 'http://activitystrea.ms/schema/1.0/place';
178 const COMMENT = 'http://activitystrea.ms/schema/1.0/comment';
181 // Atom elements we snarf
183 const TITLE = 'title';
184 const SUMMARY = 'summary';
185 const CONTENT = 'content';
189 const SOURCE = 'source';
193 const EMAIL = 'email';
207 * This probably needs to be refactored
208 * to generate a local class (ActivityPerson, ActivityFile, ...)
209 * based on the object type.
211 * @param DOMElement $element DOM thing to turn into an Activity thing
214 function __construct($element = null)
216 if (empty($element)) {
220 $this->element = $element;
222 if ($element->tagName == 'author') {
224 $this->type = self::PERSON; // XXX: is this fair?
225 $this->title = $this->_childContent($element, self::NAME);
226 $this->id = $this->_childContent($element, self::URI);
228 if (empty($this->id)) {
229 $email = $this->_childContent($element, self::EMAIL);
230 if (!empty($email)) {
232 $this->id = 'mailto:'.$email;
238 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
241 if (empty($this->type)) {
242 $this->type = ActivityObject::NOTE;
245 $this->id = $this->_childContent($element, self::ID);
246 $this->title = $this->_childContent($element, self::TITLE);
247 $this->summary = $this->_childContent($element, self::SUMMARY);
249 $this->source = $this->_getSource($element);
251 $this->content = $this->_getContent($element);
253 $this->link = ActivityUtils::getPermalink($element);
255 // XXX: grab PoCo stuff
258 // Some per-type attributes...
259 if ($this->type == self::PERSON || $this->type == self::GROUP) {
260 $this->displayName = $this->title;
262 // @fixme we may have multiple avatars with different resolutions specified
263 $this->avatar = ActivityUtils::getLink($element, 'avatar');
267 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
269 return ActivityUtils::childContent($element, $tag, $namespace);
272 // Try to get a unique id for the source feed
274 private function _getSource($element)
276 $sourceEl = ActivityUtils::child($element, 'source');
278 if (empty($sourceEl)) {
281 $href = ActivityUtils::getLink($sourceEl, 'self');
285 return ActivityUtils::childContent($sourceEl, 'id');
291 * Get the content of an atom:entry-like object
293 * @param DOMElement $element The element to examine.
295 * @return string unencoded HTML content of the element, like "This -< is <b>HTML</b>."
297 * @todo handle remote content
298 * @todo handle embedded XML mime types
299 * @todo handle base64-encoded non-XML and non-text mime types
302 private function _getContent($element)
304 $contentEl = ActivityUtils::child($element, self::CONTENT);
306 if (!empty($contentEl)) {
308 $src = $contentEl->getAttribute(self::SRC);
311 throw new ClientException(_("Can't handle remote content yet."));
314 $type = $contentEl->getAttribute(self::TYPE);
316 // slavishly following http://atompub.org/rfc4287.html#rfc.section.4.1.3.3
318 if ($type == 'text') {
319 return $contentEl->textContent;
320 } else if ($type == 'html') {
321 $text = $contentEl->textContent;
322 return htmlspecialchars_decode($text, ENT_QUOTES);
323 } else if ($type == 'xhtml') {
324 $divEl = ActivityUtils::child($contentEl, 'div');
328 $doc = $divEl->ownerDocument;
330 $children = $divEl->childNodes;
332 for ($i = 0; $i < $children->length; $i++) {
333 $child = $children->item($i);
334 $text .= $doc->saveXML($child);
337 } else if (in_array(array('text/xml', 'application/xml'), $type) ||
338 preg_match('#(+|/)xml$#', $type)) {
339 throw new ClientException(_("Can't handle embedded XML content yet."));
340 } else if (strncasecmp($type, 'text/', 5)) {
341 return $contentEl->textContent;
343 throw new ClientException(_("Can't handle embedded Base64 content yet."));
348 static function fromNotice($notice)
350 $object = new ActivityObject();
352 $object->type = ActivityObject::NOTE;
354 $object->id = $notice->uri;
355 $object->title = $notice->content;
356 $object->content = $notice->rendered;
357 $object->link = $notice->bestUrl();
362 function asString($tag='activity:object')
364 $xs = new XMLStringer(true);
366 $xs->elementStart($tag);
368 $xs->element('activity:object-type', null, $this->type);
370 $xs->element(self::ID, null, $this->id);
372 if (!empty($this->title)) {
373 $xs->element(self::TITLE, null, $this->title);
376 if (!empty($this->summary)) {
377 $xs->element(self::SUMMARY, null, $this->summary);
380 if (!empty($this->content)) {
381 // XXX: assuming HTML content here
382 $xs->element(self::CONTENT, array('type' => 'html'), $this->content);
385 if (!empty($this->link)) {
386 $xs->element('link', array('rel' => 'alternate', 'type' => 'text/html'),
390 $xs->elementEnd($tag);
392 return $xs->getString();
397 * Utility class to hold a bunch of constant defining default verb types
401 * @author Evan Prodromou <evan@status.net>
402 * @copyright 2010 StatusNet, Inc.
403 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
404 * @link http://status.net/
409 const POST = 'http://activitystrea.ms/schema/1.0/post';
410 const SHARE = 'http://activitystrea.ms/schema/1.0/share';
411 const SAVE = 'http://activitystrea.ms/schema/1.0/save';
412 const FAVORITE = 'http://activitystrea.ms/schema/1.0/favorite';
413 const PLAY = 'http://activitystrea.ms/schema/1.0/play';
414 const FOLLOW = 'http://activitystrea.ms/schema/1.0/follow';
415 const FRIEND = 'http://activitystrea.ms/schema/1.0/make-friend';
416 const JOIN = 'http://activitystrea.ms/schema/1.0/join';
417 const TAG = 'http://activitystrea.ms/schema/1.0/tag';
419 // Custom OStatus verbs for the flipside until they're standardized
420 const DELETE = 'http://ostatus.org/schema/1.0/unfollow';
421 const UNFAVORITE = 'http://ostatus.org/schema/1.0/unfavorite';
422 const UNFOLLOW = 'http://ostatus.org/schema/1.0/unfollow';
423 const LEAVE = 'http://ostatus.org/schema/1.0/leave';
426 class ActivityContext
431 public $attention = array();
432 public $conversation;
434 const THR = 'http://purl.org/syndication/thread/1.0';
435 const GEORSS = 'http://www.georss.org/georss';
436 const OSTATUS = 'http://ostatus.org/schema/1.0';
438 const INREPLYTO = 'in-reply-to';
442 const POINT = 'point';
444 const ATTENTION = 'ostatus:attention';
445 const CONVERSATION = 'ostatus:conversation';
447 function __construct($element)
449 $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
451 if (!empty($replyToEl)) {
452 $this->replyToID = $replyToEl->getAttribute(self::REF);
453 $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
456 $this->location = $this->getLocation($element);
458 $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
460 // Multiple attention links allowed
462 $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
464 for ($i = 0; $i < $links->length; $i++) {
466 $link = $links->item($i);
468 $linkRel = $link->getAttribute(ActivityUtils::REL);
470 if ($linkRel == self::ATTENTION) {
471 $this->attention[] = $link->getAttribute(self::HREF);
477 * Parse location given as a GeoRSS-simple point, if provided.
478 * http://www.georss.org/simple
480 * @param feed item $entry
481 * @return mixed Location or false
483 function getLocation($dom)
485 $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
487 for ($i = 0; $i < $points->length; $i++) {
488 $point = $points->item($i)->textContent;
489 $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
490 $point = preg_replace('/\s+/', ' ', $point);
491 $point = trim($point);
492 $coords = explode(' ', $point);
493 if (count($coords) == 2) {
494 list($lat, $lon) = $coords;
495 if (is_numeric($lat) && is_numeric($lon)) {
496 common_log(LOG_INFO, "Looking up location for $lat $lon from georss");
497 return Location::fromLatLon($lat, $lon);
500 common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
508 * An activity in the ActivityStrea.ms world
510 * An activity is kind of like a sentence: someone did something
513 * 'someone' is the 'actor'; 'did something' is the verb;
514 * 'something else' is the object.
518 * @author Evan Prodromou <evan@status.net>
519 * @copyright 2010 StatusNet, Inc.
520 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
521 * @link http://status.net/
526 const SPEC = 'http://activitystrea.ms/spec/1.0/';
527 const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
530 const OBJECT = 'object';
531 const ACTOR = 'actor';
532 const SUBJECT = 'subject';
533 const OBJECTTYPE = 'object-type';
534 const CONTEXT = 'context';
535 const TARGET = 'target';
537 const ATOM = 'http://www.w3.org/2005/Atom';
539 const AUTHOR = 'author';
540 const PUBLISHED = 'published';
541 const UPDATED = 'updated';
543 public $actor; // an ActivityObject
544 public $verb; // a string (the URL)
545 public $object; // an ActivityObject
546 public $target; // an ActivityObject
547 public $context; // an ActivityObject
548 public $time; // Time of the activity
549 public $link; // an ActivityObject
550 public $entry; // the source entry
551 public $feed; // the source feed
554 * Turns a regular old Atom <entry> into a magical activity
556 * @param DOMElement $entry Atom entry to poke at
557 * @param DOMElement $feed Atom feed, for context
560 function __construct($entry, $feed = null)
562 $this->entry = $entry;
565 $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
567 if (!empty($pubEl)) {
568 $this->time = strtotime($pubEl->textContent);
570 // XXX technically an error; being liberal. Good idea...?
571 $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
572 if (!empty($updateEl)) {
573 $this->time = strtotime($updateEl->textContent);
579 $this->link = ActivityUtils::getPermalink($entry);
581 $verbEl = $this->_child($entry, self::VERB);
583 if (!empty($verbEl)) {
584 $this->verb = trim($verbEl->textContent);
586 $this->verb = ActivityVerb::POST;
587 // XXX: do other implied stuff here
590 $objectEl = $this->_child($entry, self::OBJECT);
592 if (!empty($objectEl)) {
593 $this->object = new ActivityObject($objectEl);
595 $this->object = new ActivityObject($entry);
598 $actorEl = $this->_child($entry, self::ACTOR);
600 if (!empty($actorEl)) {
602 $this->actor = new ActivityObject($actorEl);
604 } else if (!empty($feed) &&
605 $subjectEl = $this->_child($feed, self::SUBJECT)) {
607 $this->actor = new ActivityObject($subjectEl);
609 } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
611 $this->actor = new ActivityObject($authorEl);
613 } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
616 $this->actor = new ActivityObject($authorEl);
619 $contextEl = $this->_child($entry, self::CONTEXT);
621 if (!empty($contextEl)) {
622 $this->context = new ActivityContext($contextEl);
624 $this->context = new ActivityContext($entry);
627 $targetEl = $this->_child($entry, self::TARGET);
629 if (!empty($targetEl)) {
630 $this->target = new ActivityObject($targetEl);
635 * Returns an Atom <entry> based on this activity
637 * @return DOMElement Atom entry
640 function toAtomEntry()
645 private function _child($element, $tag, $namespace=self::SPEC)
647 return ActivityUtils::child($element, $tag, $namespace);