]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/OStatus/classes/Ostatus_profile.php
Merge branch 'apinamespace' into 0.9.x
[quix0rs-gnu-social.git] / plugins / OStatus / classes / Ostatus_profile.php
index cc4307b14f119c625e56ee606d2207c5a134c925..6a0fd1f3bd4de2d1ee6b8e5f048022377dd584f7 100644 (file)
@@ -188,9 +188,11 @@ class Ostatus_profile extends Memcached_DataObject
         } else if ($this->group_id && !$this->profile_id) {
             return true;
         } else if ($this->group_id && $this->profile_id) {
-            throw new ServerException("Invalid ostatus_profile state: both group and profile IDs set for $this->uri");
+            // @todo i18n FIXME: use sprintf and add i18n.
+            throw new ServerException("Invalid ostatus_profile state: both group and profile IDs set for $this->uri.");
         } else {
-            throw new ServerException("Invalid ostatus_profile state: both group and profile IDs empty for $this->uri");
+            // @todo i18n FIXME: use sprintf and add i18n.
+            throw new ServerException("Invalid ostatus_profile state: both group and profile IDs empty for $this->uri.");
         }
     }
 
@@ -370,7 +372,8 @@ class Ostatus_profile extends Memcached_DataObject
         } else if ($entry instanceof Notice) {
             return $preamble . $entry->asAtomEntry(true, true);
         } else {
-            throw new ServerException("Invalid type passed to Ostatus_profile::notify; must be XML string or Activity entry");
+            // @todo i18n FIXME: use sprintf and add i18n.
+            throw new ServerException("Invalid type passed to Ostatus_profile::notify; must be XML string or Activity entry.");
         }
     }
 
@@ -549,7 +552,8 @@ class Ostatus_profile extends Memcached_DataObject
             $sourceContent = $note->title;
         } else {
             // @fixme fetch from $sourceUrl?
-            throw new ClientException("No content for notice {$sourceUri}");
+            // @todo i18n FIXME: use sprintf and add i18n.
+            throw new ClientException("No content for notice {$sourceUri}.");
         }
 
         // Get (safe!) HTML and text versions of the content
@@ -587,7 +591,7 @@ class Ostatus_profile extends Memcached_DataObject
                             ' class="attachment more"' .
                             ' title="'. htmlspecialchars(_m('Show more')) . '">' .
                             '…' .
-                            '</a>';
+                            '</a>'; // @todo i18n FIXME: add translator hint/context.
             }
         }
 
@@ -700,14 +704,16 @@ class Ostatus_profile extends Memcached_DataObject
             }
 
             // Is the recipient a remote group?
-            $oprofile = Ostatus_profile::staticGet('uri', $recipient);
+            $oprofile = Ostatus_profile::ensureProfileURI($recipient);
+
             if ($oprofile) {
                 if ($oprofile->isGroup()) {
                     // Deliver to local members of this remote group.
                     // @fixme sender verification?
                     $groups[] = $oprofile->group_id;
                 } else {
-                    common_log(LOG_DEBUG, "Skipping reply to remote profile $recipient");
+                    // may be canonicalized or something
+                    $replies[] = $oprofile->uri;
                 }
                 continue;
             }
@@ -770,6 +776,7 @@ class Ostatus_profile extends Memcached_DataObject
         $response = $client->get($profile_url);
 
         if (!$response->isOk()) {
+            // @todo i18n FIXME: use sprintf and add i18n.
             throw new Exception("Could not reach profile page: " . $profile_url);
         }
 
@@ -827,6 +834,7 @@ class Ostatus_profile extends Memcached_DataObject
             return self::ensureFeedURL($feedurl, $hints);
         }
 
+        // @todo i18n FIXME: use sprintf and add i18n.
         throw new Exception("Could not find a feed URL for profile page " . $finalUrl);
     }
 
@@ -859,6 +867,7 @@ class Ostatus_profile extends Memcached_DataObject
         $user = User::staticGet('id', $profile->id);
 
         if (!empty($user)) {
+            // @todo i18n FIXME: use sprintf and add i18n.
             throw new OStatusShadowException($profile, "'$profile_url' is the profile for local user '{$user->nickname}'.");
         }
 
@@ -1023,7 +1032,7 @@ class Ostatus_profile extends Memcached_DataObject
             return;
         }
         if (!common_valid_http_url($url)) {
-            throw new ServerException(sprintf(_m("Invalid avatar URL %s"), $url));
+            throw new ServerException(sprintf(_m("Invalid avatar URL %s."), $url));
         }
 
         if ($this->isGroup()) {
@@ -1033,7 +1042,7 @@ class Ostatus_profile extends Memcached_DataObject
         }
         if (!$self) {
             throw new ServerException(sprintf(
-                _m("Tried to update avatar for unsaved remote profile %s"),
+                _m("Tried to update avatar for unsaved remote profile %s."),
                 $this->uri));
         }
 
@@ -1041,7 +1050,7 @@ class Ostatus_profile extends Memcached_DataObject
         // ripped from oauthstore.php (for old OMB client)
         $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
         if (!copy($url, $temp_filename)) {
-            throw new ServerException(sprintf(_m("Unable to fetch avatar from %s"), $url));
+            throw new ServerException(sprintf(_m("Unable to fetch avatar from %s."), $url));
         }
 
         if ($this->isGroup()) {
@@ -1224,7 +1233,7 @@ class Ostatus_profile extends Memcached_DataObject
         if ($object->link && common_valid_http_url($object->link)) {
             return $object->link;
         }
-        throw new ServerException("No author ID URI found");
+        throw new ServerException("No author ID URI found.");
     }
 
     /**
@@ -1254,10 +1263,12 @@ class Ostatus_profile extends Memcached_DataObject
 
         $user = User::staticGet('uri', $homeuri);
         if ($user) {
+            // @todo i18n FIXME: add i18n.
             throw new Exception("Local user can't be referenced as remote.");
         }
 
         if (OStatusPlugin::localGroupFromUrl($homeuri)) {
+            // @todo i18n FIXME: add i18n.
             throw new Exception("Local group can't be referenced as remote.");
         }
 
@@ -1309,7 +1320,8 @@ class Ostatus_profile extends Memcached_DataObject
 
             $oprofile->profile_id = $profile->insert();
             if (!$oprofile->profile_id) {
-                throw new ServerException("Can't save local profile");
+                // @todo i18n FIXME: add i18n.
+                throw new ServerException("Can't save local profile.");
             }
         } else {
             $group = new User_group();
@@ -1319,14 +1331,16 @@ class Ostatus_profile extends Memcached_DataObject
 
             $oprofile->group_id = $group->insert();
             if (!$oprofile->group_id) {
-                throw new ServerException("Can't save local profile");
+                // @todo i18n FIXME: add i18n.
+                throw new ServerException("Can't save local profile.");
             }
         }
 
         $ok = $oprofile->insert();
 
         if (!$ok) {
-            throw new ServerException("Can't save OStatus profile");
+            // @todo i18n FIXME: add i18n.
+            throw new ServerException("Can't save OStatus profile.");
         }
 
         $avatar = self::getActivityObjectAvatar($object, $hints);
@@ -1584,6 +1598,7 @@ class Ostatus_profile extends Memcached_DataObject
         if ($uri !== false) {
             if (is_null($uri)) {
                 // Negative cache entry
+                // @todo i18n FIXME: add i18n.
                 throw new Exception('Not a valid webfinger address.');
             }
             $oprofile = Ostatus_profile::staticGet('uri', $uri);
@@ -1611,6 +1626,7 @@ class Ostatus_profile extends Memcached_DataObject
             // Save negative cache entry so we don't waste time looking it up again.
             // @fixme distinguish temporary failures?
             self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), null);
+            // @todo i18n FIXME: add i18n.
             throw new Exception('Not a valid webfinger address.');
         }
 
@@ -1692,7 +1708,8 @@ class Ostatus_profile extends Memcached_DataObject
 
             if (!$profile_id) {
                 common_log_db_error($profile, 'INSERT', __FILE__);
-                throw new Exception("Couldn't save profile for '$addr'");
+                // @todo i18n FIXME: add i18n and use sprintf for parameter.
+                throw new Exception("Couldn't save profile for '$addr'.");
             }
 
             $oprofile = new Ostatus_profile();
@@ -1710,13 +1727,15 @@ class Ostatus_profile extends Memcached_DataObject
 
             if (!$result) {
                 common_log_db_error($oprofile, 'INSERT', __FILE__);
-                throw new Exception("Couldn't save ostatus_profile for '$addr'");
+                // @todo i18n FIXME: add i18n and use sprintf for parameter.
+                throw new Exception("Couldn't save ostatus_profile for '$addr'.");
             }
 
             self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
             return $oprofile;
         }
 
+        // @todo i18n FIXME: add i18n and use sprintf for parameter.
         throw new Exception("Couldn't find a valid profile for '$addr'");
     }
 
@@ -1764,6 +1783,37 @@ class Ostatus_profile extends Memcached_DataObject
 
         return $file;
     }
+
+    static function ensureProfileURI($uri)
+    {
+        $oprofile = null;
+
+        // First, try to query it
+
+        $oprofile = Ostatus_profile::staticGet('uri', $uri);
+
+        // If unfound, do discovery stuff
+
+        if (empty($oprofile)) {
+            if (preg_match("/^(\w+)\:(.*)/", $uri, $match)) {
+                $protocol = $match[1];
+                switch ($protocol) {
+                case 'http':
+                case 'https':
+                    $oprofile = Ostatus_profile::ensureProfileURL($uri);
+                    break;
+                case 'acct':
+                case 'mailto':
+                    $rest = $match[2];
+                    $oprofile = Ostatus_profile::ensureWebfinger($rest);
+                default:
+                    common_log("Unrecognized URI protocol for profile: $protocol ($uri)");
+                    break;
+                }
+            }
+        }
+        return $oprofile;
+    }
 }
 
 /**
@@ -1785,4 +1835,3 @@ class OStatusShadowException extends Exception
         parent::__construct($message);
     }
 }
-