* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
* @link http://status.net/
*/
-
class ActivityUtils
{
const ATOM = 'http://www.w3.org/2005/Atom';
*
* @return string related link, if any
*/
-
static function getPermalink($element)
{
return self::getLink($element, 'alternate', 'text/html');
*
* @return string related link, if any
*/
-
static function getLink(DOMNode $element, $rel, $type=null)
{
$els = $element->childNodes;
foreach ($els as $link) {
-
if (!($link instanceof DOMElement)) {
continue;
}
if ($link->localName == self::LINK && $link->namespaceURI == self::ATOM) {
-
$linkRel = $link->getAttribute(self::REL);
$linkType = $link->getAttribute(self::TYPE);
{
$els = $element->childNodes;
$out = array();
-
- foreach ($els as $link) {
+
+ for ($i = 0; $i < $els->length; $i++) {
+ $link = $els->item($i);
if ($link->localName == self::LINK && $link->namespaceURI == self::ATOM) {
-
$linkRel = $link->getAttribute(self::REL);
$linkType = $link->getAttribute(self::TYPE);
*
* @return DOMElement found element or null
*/
-
static function child(DOMNode $element, $tag, $namespace=self::ATOM)
{
$els = $element->childNodes;
}
}
+ /**
+ * Gets all immediate child elements with the given tag
+ *
+ * @param DOMElement $element element to pick at
+ * @param string $tag tag to look for
+ * @param string $namespace Namespace to look under
+ *
+ * @return array found element or null
+ */
+
+ static function children(DOMNode $element, $tag, $namespace=self::ATOM)
+ {
+ $results = array();
+
+ $els = $element->childNodes;
+
+ if (!empty($els) && $els->length > 0) {
+ for ($i = 0; $i < $els->length; $i++) {
+ $el = $els->item($i);
+ if ($el->localName == $tag && $el->namespaceURI == $namespace) {
+ $results[] = $el;
+ }
+ }
+ }
+
+ return $results;
+ }
+
/**
* Grab the text content of a DOM element child of the current element
*
*
* @return string content of the child
*/
-
static function childContent(DOMNode $element, $tag, $namespace=self::ATOM)
{
$el = self::child($element, $tag, $namespace);
* @todo handle embedded XML mime types
* @todo handle base64-encoded non-XML and non-text mime types
*/
-
static function getContent($element)
{
return self::childHtmlContent($element, self::CONTENT, self::ATOM);
$src = $el->getAttribute(self::SRC);
if (!empty($src)) {
+ // TRANS: Client exception thrown when there is no source attribute.
throw new ClientException(_("Can't handle remote content yet."));
}
return trim($text);
} else if (in_array($type, array('text/xml', 'application/xml')) ||
preg_match('#(+|/)xml$#', $type)) {
+ // TRANS: Client exception thrown when there embedded XML content is found that cannot be processed yet.
throw new ClientException(_("Can't handle embedded XML content yet."));
} else if (strncasecmp($type, 'text/', 5)) {
return $el->textContent;
} else {
+ // TRANS: Client exception thrown when base64 encoded content is found that cannot be processed yet.
throw new ClientException(_("Can't handle embedded Base64 content yet."));
}
}
return false;
}
+
+ static function getFeedAuthor(DOMElement $feedEl)
+ {
+ // Try old and deprecated activity:subject
+
+ $subject = ActivityUtils::child($feedEl, Activity::SUBJECT, Activity::SPEC);
+
+ if (!empty($subject)) {
+ return new ActivityObject($subject);
+ }
+
+ // Try the feed author
+
+ $author = ActivityUtils::child($feedEl, Activity::AUTHOR, Activity::ATOM);
+
+ if (!empty($author)) {
+ return new ActivityObject($author);
+ }
+
+ // Sheesh. Not a very nice feed! Let's try fingerpoken in the
+ // entries.
+
+ $entries = $feedEl->getElementsByTagNameNS(Activity::ATOM, 'entry');
+
+ if (!empty($entries) && $entries->length > 0) {
+
+ $entry = $entries->item(0);
+
+ // Try the (deprecated) activity:actor
+
+ $actor = ActivityUtils::child($entry, Activity::ACTOR, Activity::SPEC);
+
+ if (!empty($actor)) {
+ return new ActivityObject($actor);
+ }
+
+ // Try the author
+
+ $author = ActivityUtils::child($entry, Activity::AUTHOR, Activity::ATOM);
+
+ if (!empty($author)) {
+ return new ActivityObject($author);
+ }
+ }
+
+ return null;
+ }
+
+ static function compareTypes($type, array $objects) // this does verbs too!
+ {
+ $type = self::resolveUri($type);
+ foreach ($objects as $object) {
+ if ($type === self::resolveUri($object)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ static function resolveUri($uri, $make_relative=false)
+ {
+ if (empty($uri)) {
+ throw new ServerException('No URI to resolve in ActivityUtils::resolveUri');
+ }
+
+ if (!$make_relative && parse_url($uri, PHP_URL_SCHEME) == '') { // relative -> absolute
+ $uri = Activity::SCHEMA . $uri;
+ } elseif ($make_relative) { // absolute -> relative
+ $uri = basename($uri); //preg_replace('/^http:\/\/activitystrea\.ms\/schema\/1\.0\//', '', $uri);
+ } // absolute schemas pass through unharmed
+
+ return $uri;
+ }
+
+ static function findLocalObject(array $uris, $type=ActivityObject::NOTE) {
+ $object = null;
+ // TODO: Extend this in plugins etc.
+ if (Event::handle('StartFindLocalActivityObject', array($uris, $type, &$object))) {
+ switch (self::resolveUri($type)) {
+ case ActivityObject::PERSON:
+ // GROUP will also be here in due time...
+ $object = new Profile();
+ break;
+ default:
+ $object = new Notice();
+ }
+ }
+ foreach (array_unique($uris) as $uri) {
+ try {
+ // the exception thrown will cancel before reaching $object
+ $object = call_user_func(array($object, 'fromUri'), $uri);
+ break;
+ } catch (Exception $e) {
+ common_debug('Could not find local activity object from uri: '.$uri);
+ }
+ }
+ if (!empty($object)) {
+ Event::handle('EndFindLocalActivityObject', array($object->getUri(), $type, $object));
+ } else {
+ throw new ServerException('Could not find any activityobject stored locally with given URI');
+ }
+ return $object;
+ }
+
+ // Check authorship by supplying a Profile as a default and letting plugins
+ // set it to something else if the activity's author is actually someone
+ // else (like with a group or peopletag feed as handled in OStatus).
+ //
+ // NOTE: Returned is not necessarily the supplied profile! For example,
+ // the "feed author" may be a group, but the "activity author" is a person!
+ static function checkAuthorship(Activity $activity, Profile $profile)
+ {
+ if (Event::handle('CheckActivityAuthorship', array($activity, &$profile))) {
+ // if (empty($activity->actor)), then we generated this Activity ourselves and can trust $profile
+
+ $actor_uri = $profile->getUri();
+
+ if (!in_array($actor_uri, array($activity->actor->id, $activity->actor->link))) {
+ // A mismatch between our locally stored URI and the supplied author?
+ // Probably not more than a blog feed or something (with multiple authors or so)
+ // but log it for future inspection.
+ common_log(LOG_WARNING, "Got an actor '{$activity->actor->title}' ({$activity->actor->id}) on single-user feed for " . $actor_uri);
+ } elseif (empty($activity->actor->id)) {
+ // Plain <author> without ActivityStreams actor info.
+ // We'll just ignore this info for now and save the update under the feed's identity.
+ }
+ }
+
+ if (!$profile instanceof Profile) {
+ throw new ServerException('Could not get an author Profile for activity');
+ }
+
+ return $profile;
+ }
+
+ static public function typeToTitle($type)
+ {
+ return ucfirst(self::resolveUri($type, true));
+ }
+
+ static public function verbToTitle($verb)
+ {
+ return ucfirst(self::resolveUri($verb, true));
+ }
}