123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460 |
- <?php
- if (!defined('STATUSNET')) {
- exit(1);
- }
- class ActivityUtils
- {
- const ATOM = 'http://www.w3.org/2005/Atom';
- const LINK = 'link';
- const REL = 'rel';
- const TYPE = 'type';
- const HREF = 'href';
- const CONTENT = 'content';
- const SRC = 'src';
-
- static function getPermalink(DOMNode $element)
- {
- return self::getLink($element, 'alternate', 'text/html');
- }
- static function getSelfLink(DOMNode $element)
- {
- return self::getLink($element, 'self', 'application/atom+xml');
- }
-
- 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);
-
- if ($linkRel == $rel &&
- (is_null($type) || common_bare_mime($linkType) == common_bare_mime($type))) {
- return $link->getAttribute(self::HREF);
- }
- }
- }
- return null;
- }
- static function getLinks(DOMNode $element, $rel, $type=null)
- {
- $els = $element->childNodes;
- $out = array();
-
- 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);
- if ($linkRel == $rel &&
- (is_null($type) || $linkType == $type)) {
- $out[] = $link;
- }
- }
- }
- return $out;
- }
-
- static function child(DOMNode $element, $tag, $namespace=self::ATOM)
- {
- $els = $element->childNodes;
- if (empty($els) || $els->length == 0) {
- return null;
- } else {
- for ($i = 0; $i < $els->length; $i++) {
- $el = $els->item($i);
- if ($el->localName == $tag && $el->namespaceURI == $namespace) {
- return $el;
- }
- }
- }
- }
-
- 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;
- }
-
- static function childContent(DOMNode $element, $tag, $namespace=self::ATOM)
- {
- $el = self::child($element, $tag, $namespace);
- if (empty($el)) {
- return null;
- } else {
- return $el->textContent;
- }
- }
- static function childHtmlContent(DOMNode $element, $tag, $namespace=self::ATOM)
- {
- $el = self::child($element, $tag, $namespace);
- if (empty($el)) {
- return null;
- } else {
- return self::textConstruct($el);
- }
- }
-
- static function getContent($element)
- {
- return self::childHtmlContent($element, self::CONTENT, self::ATOM);
- }
- static function textConstruct($el)
- {
- $src = $el->getAttribute(self::SRC);
- if (!empty($src)) {
-
- throw new ClientException(_("Can't handle remote content yet."));
- }
- $type = $el->getAttribute(self::TYPE);
-
- if (empty($type) || $type == 'text') {
-
-
- return htmlspecialchars($el->textContent);
- } else if ($type == 'html') {
-
-
- $text = $el->textContent;
- return $text;
- } else if ($type == 'xhtml') {
-
-
-
-
- $divEl = ActivityUtils::child($el, 'div', 'http://www.w3.org/1999/xhtml');
- if (empty($divEl)) {
- return null;
- }
- $doc = $divEl->ownerDocument;
- $text = '';
- $children = $divEl->childNodes;
- for ($i = 0; $i < $children->length; $i++) {
- $child = $children->item($i);
- $text .= $doc->saveXML($child);
- }
- return trim($text);
- } else if (in_array($type, array('text/xml', 'application/xml')) ||
- preg_match('#(+|/)xml$#', $type)) {
-
- throw new ClientException(_("Can't handle embedded XML content yet."));
- } else if (strncasecmp($type, 'text/', 5)) {
- return $el->textContent;
- } else {
-
- throw new ClientException(_("Can't handle embedded Base64 content yet."));
- }
- }
-
- static function validateUri($uri)
- {
-
- $validate = new Validate();
- if (preg_match('/^mailto:(.*)$/', $uri, $match)) {
- return $validate->email($match[1], common_config('email', 'check_domain'));
- }
- if ($validate->uri($uri)) {
- return true;
- }
-
-
-
- if ($validate->uri($uri, array('allowed_schemes' => array('tag')))) {
- return true;
- }
- return false;
- }
- static function getFeedAuthor(DOMElement $feedEl)
- {
-
- $subject = ActivityUtils::child($feedEl, Activity::SUBJECT, Activity::SPEC);
- if (!empty($subject)) {
- return new ActivityObject($subject);
- }
-
- $author = ActivityUtils::child($feedEl, Activity::AUTHOR, Activity::ATOM);
- if (!empty($author)) {
- return new ActivityObject($author);
- }
-
-
- $entries = $feedEl->getElementsByTagNameNS(Activity::ATOM, 'entry');
- if (!empty($entries) && $entries->length > 0) {
- $entry = $entries->item(0);
-
- $actor = ActivityUtils::child($entry, Activity::ACTOR, Activity::SPEC);
- if (!empty($actor)) {
- return new ActivityObject($actor);
- }
-
- $author = ActivityUtils::child($entry, Activity::AUTHOR, Activity::ATOM);
- if (!empty($author)) {
- return new ActivityObject($author);
- }
- }
- return null;
- }
- static function compareTypes($type, $objects)
- {
- $type = self::resolveUri($type, false);
- 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) == '') {
- $uri = Activity::SCHEMA . $uri;
- } elseif ($make_relative) {
- $uri = basename($uri);
- }
- return $uri;
- }
- static function findLocalObject(array $uris, $type=ActivityObject::NOTE) {
- $obj_class = null;
-
- if (Event::handle('StartFindLocalActivityObject', array($uris, $type, &$obj_class))) {
- switch (self::resolveUri($type)) {
- case ActivityObject::PERSON:
-
- $obj_class = 'Profile';
- break;
- default:
- $obj_class = 'Notice';
- }
- }
- $object = null;
- $uris = array_unique($uris);
- foreach ($uris as $uri) {
- try {
-
- $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;
- }
-
-
-
-
-
-
- static function checkAuthorship(Activity $activity, Profile $profile)
- {
- if (Event::handle('CheckActivityAuthorship', array($activity, &$profile))) {
-
- $actor_uri = $profile->getUri();
- if (!in_array($actor_uri, array($activity->actor->id, $activity->actor->link))) {
-
-
-
- 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)) {
-
-
- }
- }
- 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));
- }
- }
|