]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/OStatus/actions/ostatussub.php
Empty resource would throw exception
[quix0rs-gnu-social.git] / plugins / OStatus / actions / ostatussub.php
index 493b519657de346ad64991d97390b9fe76ad300c..7531bb688644faa556684b799566702f317daf6d 100644 (file)
@@ -22,7 +22,7 @@
  * @maintainer Brion Vibber <brion@status.net>
  */
 
-if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
+if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
 
 /**
  * Key UI methods:
@@ -40,16 +40,44 @@ class OStatusSubAction extends Action
     protected $profile_uri; // provided acct: or URI of remote entity
     protected $oprofile; // Ostatus_profile of remote entity, if valid
 
+    protected function prepare(array $args=array())
+    {
+        parent::prepare($args);
+
+        if (!common_logged_in()) {
+            // XXX: selfURL() didn't work. :<
+            common_set_returnto($_SERVER['REQUEST_URI']);
+            if (Event::handle('RedirectToLogin', array($this, null))) {
+                common_redirect(common_local_url('login'), 303);
+            }
+            return false;
+        }
+
+        if ($this->pullRemoteProfile()) {
+            $this->validateRemoteProfile();
+        }
+        return true;
+    }
+
+    /**
+     * Handle the submission.
+     */
+    protected function handle()
+    {
+        parent::handle();
+        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
+            $this->handlePost();
+        } else {
+            $this->showForm();
+        }
+    }
+
     /**
      * Show the initial form, when we haven't yet been given a valid
      * remote profile.
      */
     function showInputForm()
     {
-        $user = common_current_user();
-
-        $profile = $user->getProfile();
-
         $this->elementStart('form', array('method' => 'post',
                                           'id' => 'form_ostatus_sub',
                                           'class' => 'form_settings',
@@ -66,7 +94,7 @@ class OStatusSubAction extends Action
                      _m('Subscribe to'),
                      $this->profile_uri,
                      // TRANS: Tooltip for field label "Subscribe to".
-                     _m('OStatus user\'s address, like nickname@example.com or http://example.net/nickname'));
+                     _m('OStatus user\'s address, like nickname@example.com or http://example.net/nickname.'));
         $this->elementEnd('li');
         $this->elementEnd('ul');
         // TRANS: Button text.
@@ -88,7 +116,7 @@ class OStatusSubAction extends Action
     {
         $ok = $this->preview();
         if (!$ok) {
-            // @fixme maybe provide a cancel button or link back?
+            // @todo FIXME maybe provide a cancel button or link back?
             return;
         }
 
@@ -104,8 +132,8 @@ class OStatusSubAction extends Action
         $this->hidden('token', common_session_token());
         $this->hidden('profile', $this->profile_uri);
         if ($this->oprofile->isGroup()) {
+            // TRANS: Button text.
             $this->submit('submit', _m('Join'), 'submit', null,
-                         // TRANS: Button text.
                          // TRANS: Tooltip for button "Join".
                          _m('BUTTON','Join this group'));
         } else {
@@ -127,20 +155,19 @@ class OStatusSubAction extends Action
      */
     function preview()
     {
-        $oprofile = $this->oprofile;
-        $profile = $oprofile->localProfile();
+        // Throws NoProfileException on localProfile when remote user's Profile not found
+        $profile = $this->oprofile->localProfile();
 
-        $cur = common_current_user();
-        if ($cur->isSubscribed($profile)) {
+        if ($this->scoped->isSubscribed($profile)) {
             $this->element('div', array('class' => 'error'),
-                           _m("You are already subscribed to this user."));
+                           // TRANS: Extra paragraph in remote profile view when already subscribed.
+                           _m('You are already subscribed to this user.'));
             $ok = false;
         } else {
             $ok = true;
         }
 
-        $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
-        $avatarUrl = $avatar ? $avatar->displayUrl() : false;
+        $avatarUrl = $profile->avatarUrl(AVATAR_PROFILE_SIZE);
 
         $this->showEntity($profile,
                           $profile->profileurl,
@@ -156,70 +183,42 @@ class OStatusSubAction extends Action
         $homepage = $entity->homepage;
         $location = $entity->location;
 
-        if (!$avatar) {
-            $avatar = Avatar::defaultImage(AVATAR_PROFILE_SIZE);
-        }
-
         $this->elementStart('div', 'entity_profile vcard');
-        $this->elementStart('dl', 'entity_depiction');
-        $this->element('dt', null, _m('Photo'));
-        $this->elementStart('dd');
         $this->element('img', array('src' => $avatar,
-                                    'class' => 'photo avatar',
+                                    'class' => 'photo avatar entity_depiction',
                                     'width' => AVATAR_PROFILE_SIZE,
                                     'height' => AVATAR_PROFILE_SIZE,
                                     'alt' => $nickname));
-        $this->elementEnd('dd');
-        $this->elementEnd('dl');
 
-        $this->elementStart('dl', 'entity_nickname');
-        $this->element('dt', null, _m('Nickname'));
-        $this->elementStart('dd');
-        $hasFN = ($fullname !== '') ? 'nickname' : 'fn nickname';
+        $hasFN = ($fullname !== '') ? 'nickname' : 'fn nickname entity_nickname';
         $this->elementStart('a', array('href' => $profile,
                                        'class' => 'url '.$hasFN));
-        $this->raw($nickname);
+        $this->text($nickname);
         $this->elementEnd('a');
-        $this->elementEnd('dd');
-        $this->elementEnd('dl');
 
         if (!is_null($fullname)) {
-            $this->elementStart('dl', 'entity_fn');
-            $this->elementStart('dd');
-            $this->elementStart('span', 'fn');
-            $this->raw($fullname);
-            $this->elementEnd('span');
-            $this->elementEnd('dd');
-            $this->elementEnd('dl');
+            $this->elementStart('div', 'fn entity_fn');
+            $this->text($fullname);
+            $this->elementEnd('div');
         }
+
         if (!is_null($location)) {
-            $this->elementStart('dl', 'entity_location');
-            $this->element('dt', null, _m('Location'));
-            $this->elementStart('dd', 'label');
-            $this->raw($location);
-            $this->elementEnd('dd');
-            $this->elementEnd('dl');
+            $this->elementStart('div', 'label entity_location');
+            $this->text($location);
+            $this->elementEnd('div');
         }
 
         if (!is_null($homepage)) {
-            $this->elementStart('dl', 'entity_url');
-            $this->element('dt', null, _m('URL'));
-            $this->elementStart('dd');
             $this->elementStart('a', array('href' => $homepage,
-                                                'class' => 'url'));
-            $this->raw($homepage);
+                                           'class' => 'url entity_url'));
+            $this->text($homepage);
             $this->elementEnd('a');
-            $this->elementEnd('dd');
-            $this->elementEnd('dl');
         }
 
         if (!is_null($note)) {
-            $this->elementStart('dl', 'entity_note');
-            $this->element('dt', null, _m('Note'));
-            $this->elementStart('dd', 'note');
-            $this->raw($note);
-            $this->elementEnd('dd');
-            $this->elementEnd('dl');
+            $this->elementStart('div', 'note entity_note');
+            $this->text($note);
+            $this->elementEnd('div');
         }
         $this->elementEnd('div');
     }
@@ -229,8 +228,7 @@ class OStatusSubAction extends Action
      */
     function success()
     {
-        $cur = common_current_user();
-        $url = common_local_url('subscriptions', array('nickname' => $cur->nickname));
+        $url = common_local_url('subscriptions', array('nickname' => $this->scoped->nickname));
         common_redirect($url, 303);
     }
 
@@ -243,34 +241,41 @@ class OStatusSubAction extends Action
      */
     function pullRemoteProfile()
     {
-        $this->profile_uri = $this->trimmed('profile');
+        $validate = new Validate();
+        try {
+            $this->profile_uri = Discovery::normalize($this->trimmed('profile'));
+        } catch (Exception $e) {
+            $this->profile_uri = null;
+        }
         try {
-            if (Validate::email($this->profile_uri)) {
+            if (Discovery::isAcct($this->profile_uri) && $validate->email(mb_substr($this->profile_uri, 5))) {
                 $this->oprofile = Ostatus_profile::ensureWebfinger($this->profile_uri);
-            } else if (Validate::uri($this->profile_uri)) {
+            } else if ($validate->uri($this->profile_uri)) {
                 $this->oprofile = Ostatus_profile::ensureProfileURL($this->profile_uri);
             } else {
-                // TRANS: Error text.
+                // TRANS: Error message in OStatus plugin. Do not translate the domain names example.com
+                // TRANS: and example.net, as these are official standard domain names for use in examples.
                 $this->error = _m("Sorry, we could not reach that address. Please make sure that the OStatus address is like nickname@example.com or http://example.net/nickname.");
                 common_debug('Invalid address format.', __FILE__);
                 return false;
             }
             return true;
         } catch (FeedSubBadURLException $e) {
-            // TRANS: Error text.
-            $this->error = _m("Sorry, we could not reach that address. Please make sure that the OStatus address is like nickname@example.com or http://example.net/nickname.");
+                // TRANS: Error message in OStatus plugin. Do not translate the domain names example.com
+                // TRANS: and example.net, as these are official standard domain names for use in examples.
+            $this->error = _m('Sorry, we could not reach that address. Please make sure that the OStatus address is like nickname@example.com or http://example.net/nickname.');
             common_debug('Invalid URL or could not reach server.', __FILE__);
         } catch (FeedSubBadResponseException $e) {
             // TRANS: Error text.
-            $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
+            $this->error = _m('Sorry, we could not reach that feed. Please try that OStatus address again later.');
             common_debug('Cannot read feed; server returned error.', __FILE__);
         } catch (FeedSubEmptyException $e) {
             // TRANS: Error text.
-            $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
+            $this->error = _m('Sorry, we could not reach that feed. Please try that OStatus address again later.');
             common_debug('Cannot read feed; server returned an empty page.', __FILE__);
         } catch (FeedSubBadHTMLException $e) {
             // TRANS: Error text.
-            $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
+            $this->error = _m('Sorry, we could not reach that feed. Please try that OStatus address again later.');
             common_debug('Bad HTML, could not find feed link.', __FILE__);
         } catch (FeedSubNoFeedException $e) {
             // TRANS: Error text.
@@ -280,9 +285,14 @@ class OStatusSubAction extends Action
             // TRANS: Error text.
             $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
             common_debug('Not a recognized feed type.', __FILE__);
+        } catch (FeedSubNoHubException $e) {
+            // TRANS: Error text.
+            $this->error = _m("Sorry, that feed is not Pubsubhubub enabled.");
+            common_debug('No hub found.', __FILE__);
         } catch (Exception $e) {
             // Any new ones we forgot about
-            // TRANS: Error text.
+                // TRANS: Error message in OStatus plugin. Do not translate the domain names example.com
+                // TRANS: and example.net, as these are official standard domain names for use in examples.
             $this->error = _m("Sorry, we could not reach that address. Please make sure that the OStatus address is like nickname@example.com or http://example.net/nickname.");
             common_debug(sprintf('Bad feed URL: %s %s', get_class($e), $e->getMessage()), __FILE__);
         }
@@ -292,10 +302,13 @@ class OStatusSubAction extends Action
 
     function validateRemoteProfile()
     {
+        // Send us to the respective subscription form for conf
         if ($this->oprofile->isGroup()) {
-            // Send us to the group subscription form for conf
             $target = common_local_url('ostatusgroup', array(), array('profile' => $this->profile_uri));
             common_redirect($target, 303);
+        } else if ($this->oprofile->isPeopletag()) {
+            $target = common_local_url('ostatuspeopletag', array(), array('profile' => $this->profile_uri));
+            common_redirect($target, 303);
         }
     }
 
@@ -308,12 +321,11 @@ class OStatusSubAction extends Action
     function saveFeed()
     {
         // And subscribe the current user to the local profile
-        $user = common_current_user();
         $local = $this->oprofile->localProfile();
-        if ($user->isSubscribed($local)) {
+        if ($this->scoped->isSubscribed($local)) {
             // TRANS: OStatus remote subscription dialog error.
             $this->showForm(_m('Already subscribed!'));
-        } elseif (Subscription::start($user, $local)) {
+        } elseif (Subscription::start($this->scoped, $local)) {
             $this->success();
         } else {
             // TRANS: OStatus remote subscription dialog error.
@@ -321,38 +333,6 @@ class OStatusSubAction extends Action
         }
     }
 
-    function prepare($args)
-    {
-        parent::prepare($args);
-
-        if (!common_logged_in()) {
-            // XXX: selfURL() didn't work. :<
-            common_set_returnto($_SERVER['REQUEST_URI']);
-            if (Event::handle('RedirectToLogin', array($this, null))) {
-                common_redirect(common_local_url('login'), 303);
-            }
-            return false;
-        }
-
-        if ($this->pullRemoteProfile()) {
-            $this->validateRemoteProfile();
-        }
-        return true;
-    }
-
-    /**
-     * Handle the submission.
-     */
-    function handle($args)
-    {
-        parent::handle($args);
-        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
-            $this->handlePost();
-        } else {
-            $this->showForm();
-        }
-    }
-
     /**
      * Handle posts to this form
      *
@@ -364,6 +344,7 @@ class OStatusSubAction extends Action
         // CSRF protection
         $token = $this->trimmed('token');
         if (!$token || $token != common_session_token()) {
+            // TRANS: Client error displayed when the session token does not match or is not given.
             $this->showForm(_m('There was a problem with your session token. '.
                               'Try again, please.'));
             return;
@@ -387,9 +368,7 @@ class OStatusSubAction extends Action
             $this->error = $err;
         }
         if ($this->boolean('ajax')) {
-            header('Content-Type: text/xml;charset=utf-8');
-            $this->xw->startDocument('1.0', 'UTF-8');
-            $this->elementStart('html');
+            $this->startHTML('text/xml;charset=utf-8');
             $this->elementStart('head');
             // TRANS: Form title.
             $this->element('title', null, _m('Subscribe to user'));
@@ -397,7 +376,7 @@ class OStatusSubAction extends Action
             $this->elementStart('body');
             $this->showContent();
             $this->elementEnd('body');
-            $this->elementEnd('html');
+            $this->endHTML();
         } else {
             $this->showPage();
         }
@@ -411,7 +390,7 @@ class OStatusSubAction extends Action
 
     function title()
     {
-        // TRANS: Page title for OStatus remote subscription form
+        // TRANS: Page title for OStatus remote subscription form.
         return _m('Confirm');
     }