]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/activityutils.php
Show more links work with AJAX-retrieved HTML
[quix0rs-gnu-social.git] / lib / activityutils.php
index 401fd7fc283ff92b9b5d5a021ecbd6e74780fa74..c2c239f1d3cc68ef32872da97835f7d8ec58710c 100644 (file)
@@ -46,7 +46,6 @@ if (!defined('STATUSNET')) {
  * @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';
@@ -66,7 +65,6 @@ class ActivityUtils
      *
      * @return string related link, if any
      */
-
     static function getPermalink($element)
     {
         return self::getLink($element, 'alternate', 'text/html');
@@ -79,19 +77,16 @@ class ActivityUtils
      *
      * @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);
 
@@ -109,10 +104,10 @@ class ActivityUtils
     {
         $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);
 
@@ -135,7 +130,6 @@ class ActivityUtils
      *
      * @return DOMElement found element or null
      */
-
     static function child(DOMNode $element, $tag, $namespace=self::ATOM)
     {
         $els = $element->childNodes;
@@ -151,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
      *
@@ -160,7 +182,6 @@ class ActivityUtils
      *
      * @return string content of the child
      */
-
     static function childContent(DOMNode $element, $tag, $namespace=self::ATOM)
     {
         $el = self::child($element, $tag, $namespace);
@@ -194,7 +215,6 @@ class ActivityUtils
      * @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);
@@ -205,6 +225,7 @@ class ActivityUtils
         $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."));
         }
 
@@ -241,10 +262,12 @@ class ActivityUtils
             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."));
         }
     }
@@ -257,6 +280,12 @@ class ActivityUtils
      */
     static function validateUri($uri)
     {
+        // Check mailto: URIs first
+
+        if (preg_match('/^mailto:(.*)$/', $uri, $match)) {
+            return Validate::email($match[1], common_config('email', 'check_domain'));
+        }
+
         if (Validate::uri($uri)) {
             return true;
         }
@@ -270,4 +299,51 @@ class ActivityUtils
 
         return false;
     }
+
+    static function getFeedAuthor($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;
+    }
 }