X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=lib%2Factivityutils.php;h=8a2be350225140d6198128ad33d97a42a305c39b;hb=fb537fb7f455cde9c4ccd9f6e54e734467d4f4b3;hp=59f7cdcf3a987d9290609e163427f639bdd5678a;hpb=97debbab68ea6bc42ebd0d00e51500019bbcd1c0;p=quix0rs-gnu-social.git diff --git a/lib/activityutils.php b/lib/activityutils.php index 59f7cdcf3a..8a2be35022 100644 --- a/lib/activityutils.php +++ b/lib/activityutils.php @@ -145,6 +145,34 @@ class ActivityUtils } } + /** + * 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 * @@ -253,19 +281,20 @@ class ActivityUtils static function validateUri($uri) { // Check mailto: URIs first + $validate = new Validate(); if (preg_match('/^mailto:(.*)$/', $uri, $match)) { - return Validate::email($match[1], common_config('email', 'check_domain')); + return $validate->email($match[1], common_config('email', 'check_domain')); } - if (Validate::uri($uri)) { + if ($validate->uri($uri)) { return true; } // Possibly an upstream bug; tag: URIs aren't validated properly // unless you explicitly ask for them. All other schemes are accepted // for basic URI validation without asking. - if (Validate::uri($uri, array('allowed_scheme' => array('tag')))) { + if ($validate->uri($uri, array('allowed_scheme' => array('tag')))) { return true; } @@ -318,4 +347,107 @@ class ActivityUtils return null; } + + static function compareTypes($type, $objects) + { + $type = self::resolveUri($type); + foreach ((array)$objects as $object) { + if ($type === self::resolveUri($object)) { + return true; + } + } + return false; + } + + static function compareVerbs($type, $objects) + { + return self::compareTypes($type, $objects); + } + + 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) { + $obj_class = null; + // TODO: Extend this in plugins etc. and describe in EVENTS.txt + if (Event::handle('StartFindLocalActivityObject', array($uris, $type, &$obj_class))) { + switch (self::resolveUri($type)) { + case ActivityObject::PERSON: + // GROUP will also be here in due time... + $obj_class = 'Profile'; + break; + default: + $obj_class = 'Notice'; + } + } + $object = null; + $uris = array_unique($uris); + foreach ($uris as $uri) { + try { + // the exception thrown will cancel before reaching $object + $object = call_user_func("{$obj_class}::fromUri", $uri); + break; + } catch (UnknownUriException $e) { + common_debug('Could not find local activity object from uri: '.$e->object_uri); + } + } + if (!$object instanceof Managed_DataObject) { + throw new ServerException('Could not find any activityobject stored locally with given URIs: '.var_export($uris,true)); + } + Event::handle('EndFindLocalActivityObject', array($object->getUri(), $object->getObjectType(), $object)); + 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 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)); + } }