]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - actions/userauthorization.php
Merge branch '0.7.x' of git@gitorious.org:laconica/dev into 0.7.x
[quix0rs-gnu-social.git] / actions / userauthorization.php
index 05efbc16c70ec7ef7d1f125044dde4415be3b58c..ed17ceec977723ecd20899d380a645f183eeea20 100644 (file)
@@ -24,6 +24,8 @@ define('TIMESTAMP_THRESHOLD', 300);
 
 class UserauthorizationAction extends Action
 {
+    var $error;
+    var $req;
 
     function handle($args)
     {
@@ -33,46 +35,63 @@ class UserauthorizationAction extends Action
             # CSRF protection
             $token = $this->trimmed('token');
             if (!$token || $token != common_session_token()) {
-                $req = $this->get_stored_request();
-                $this->show_form(_('There was a problem with your session token. Try again, please.'), $req);
+                $req = $this->getStoredRequest();
+                $this->showForm($req, _('There was a problem with your session token. '.
+                                        'Try again, please.'));
                 return;
             }
             # We've shown the form, now post user's choice
-            $this->send_authorization();
+            $this->sendAuthorization();
         } else {
             if (!common_logged_in()) {
                 # Go log in, and then come back
-                common_debug('saving URL for returnto', __FILE__);
                 common_set_returnto($_SERVER['REQUEST_URI']);
 
-                common_debug('redirecting to login', __FILE__);
                 common_redirect(common_local_url('login'));
                 return;
             }
             try {
                 # this must be a new request
-                common_debug('getting new request', __FILE__);
-                $req = $this->get_new_request();
+                $req = $this->getNewRequest();
                 if (!$req) {
-                    $this->client_error(_('No request found!'));
+                    $this->clientError(_('No request found!'));
                 }
-                common_debug('validating request', __FILE__);
                 # XXX: only validate new requests, since nonce is one-time use
-                $this->validate_request($req);
-                common_debug('showing form', __FILE__);
-                $this->store_request($req);
-                $this->show_form($req);
+                $this->validateRequest($req);
+                $this->storeRequest($req);
+                $this->showForm($req);
             } catch (OAuthException $e) {
-                $this->clear_request();
-                $this->client_error($e->getMessage());
+                $this->clearRequest();
+                $this->clientError($e->getMessage());
                 return;
             }
 
         }
     }
 
-    function show_form($req)
+    function showForm($req, $error=null)
     {
+        $this->req = $req;
+        $this->error = $error;
+        $this->showPage();
+    }
+
+    function title()
+    {
+        return _('Authorize subscription');
+    }
+
+    function showPageNotice()
+    {
+        $this->element('p', null, _('Please check these details to make sure '.
+                                    'that you want to subscribe to this user\'s notices. '.
+                                    'If you didn\'t just ask to subscribe to someone\'s notices, '.
+                                    'click "Reject".'));
+    }
+
+    function showContent()
+    {
+        $req = $this->req;
 
         $nickname = $req->get_parameter('omb_listenee_nickname');
         $profile = $req->get_parameter('omb_listenee_profile');
@@ -83,75 +102,69 @@ class UserauthorizationAction extends Action
         $location = $req->get_parameter('omb_listenee_location');
         $avatar = $req->get_parameter('omb_listenee_avatar');
 
-        common_show_header(_('Authorize subscription'));
-        common_element('p', null, _('Please check these details to make sure '.
-                                     'that you want to subscribe to this user\'s notices. '.
-                                     'If you didn\'t just ask to subscribe to someone\'s notices, '.
-                                     'click "Cancel".'));
-        common_element_start('div', 'profile');
+        $this->elementStart('div', 'profile');
         if ($avatar) {
-            common_element('img', array('src' => $avatar,
+            $this->element('img', array('src' => $avatar,
                                         'class' => 'avatar profile',
                                         'width' => AVATAR_PROFILE_SIZE,
                                         'height' => AVATAR_PROFILE_SIZE,
                                         'alt' => $nickname));
         }
-        common_element('a', array('href' => $profile,
+        $this->element('a', array('href' => $profile,
                                   'class' => 'external profile nickname'),
                        $nickname);
         if ($fullname) {
-            common_element_start('div', 'fullname');
+            $this->elementStart('div', 'fullname');
             if ($homepage) {
-                common_element('a', array('href' => $homepage),
+                $this->element('a', array('href' => $homepage),
                                $fullname);
             } else {
-                common_text($fullname);
+                $this->text($fullname);
             }
-            common_element_end('div');
+            $this->elementEnd('div');
         }
         if ($location) {
-            common_element('div', 'location', $location);
+            $this->element('div', 'location', $location);
         }
         if ($bio) {
-            common_element('div', 'bio', $bio);
+            $this->element('div', 'bio', $bio);
         }
-        common_element_start('div', 'license');
-        common_element('a', array('href' => $license,
+        $this->elementStart('div', 'license');
+        $this->element('a', array('href' => $license,
                                   'class' => 'license'),
                        $license);
-        common_element_end('div');
-        common_element_end('div');
-        common_element_start('form', array('method' => 'post',
-                                           'id' => 'userauthorization',
-                                           'name' => 'userauthorization',
-                                           'action' => common_local_url('userauthorization')));
-        common_hidden('token', common_session_token());
-        common_submit('accept', _('Accept'));
-        common_submit('reject', _('Reject'));
-        common_element_end('form');
-        common_show_footer();
+        $this->elementEnd('div');
+        $this->elementEnd('div');
+        $this->elementStart('form', array('method' => 'post',
+                                          'id' => 'userauthorization',
+                                          'name' => 'userauthorization',
+                                          'action' => common_local_url('userauthorization')));
+        $this->hidden('token', common_session_token());
+        $this->submit('accept', _('Accept'));
+        $this->submit('reject', _('Reject'));
+        $this->elementEnd('form');
     }
 
-    function send_authorization()
+    function sendAuthorization()
     {
-        $req = $this->get_stored_request();
+        $req = $this->getStoredRequest();
 
         if (!$req) {
-            common_user_error(_('No authorization request!'));
+            $this->clientError(_('No authorization request!'));
             return;
         }
 
         $callback = $req->get_parameter('oauth_callback');
 
         if ($this->arg('accept')) {
-            if (!$this->authorize_token($req)) {
-                $this->client_error(_('Error authorizing token'));
+            if (!$this->authorizeToken($req)) {
+                $this->clientError(_('Error authorizing token'));
             }
-            if (!$this->save_remote_profile($req)) {
-                $this->client_error(_('Error saving remote profile'));
+            if (!$this->saveRemoteProfile($req)) {
+                $this->clientError(_('Error saving remote profile'));
             }
             if (!$callback) {
-                $this->show_accept_message($req->get_parameter('oauth_token'));
+                $this->showAcceptMessage($req->get_parameter('oauth_token'));
             } else {
                 $params = array();
                 $params['oauth_token'] = $req->get_parameter('oauth_token');
@@ -160,7 +173,7 @@ class UserauthorizationAction extends Action
                 $profile = $user->getProfile();
                 if (!$profile) {
                     common_log_db_error($user, 'SELECT', __FILE__);
-                    $this->server_error(_('User without matching profile'));
+                    $this->serverError(_('User without matching profile'));
                     return;
                 }
                 $params['omb_listener_nickname'] = $user->nickname;
@@ -193,7 +206,7 @@ class UserauthorizationAction extends Action
             }
         } else {
             if (!$callback) {
-                $this->show_reject_message();
+                $this->showRejectMessage();
             } else {
                 # XXX: not 100% sure how to signal failure... just redirect without token?
                 common_redirect($callback, 303);
@@ -201,24 +214,19 @@ class UserauthorizationAction extends Action
         }
     }
 
-    function authorize_token(&$req)
+    function authorizeToken(&$req)
     {
         $consumer_key = $req->get_parameter('oauth_consumer_key');
         $token_field = $req->get_parameter('oauth_token');
-        common_debug('consumer key = "'.$consumer_key.'"', __FILE__);
-        common_debug('token field = "'.$token_field.'"', __FILE__);
         $rt = new Token();
         $rt->consumer_key = $consumer_key;
         $rt->tok = $token_field;
         $rt->type = 0;
         $rt->state = 0;
-        common_debug('request token to look up: "'.print_r($rt,true).'"');
         if ($rt->find(true)) {
-            common_debug('found request token to authorize', __FILE__);
             $orig_rt = clone($rt);
             $rt->state = 1; # Authorized but not used
             if ($rt->update($orig_rt)) {
-                common_debug('updated request token so it is authorized', __FILE__);
                 return true;
             }
         }
@@ -227,7 +235,7 @@ class UserauthorizationAction extends Action
 
     # XXX: refactor with similar code in finishremotesubscribe.php
 
-    function save_remote_profile(&$req)
+    function saveRemoteProfile(&$req)
     {
         # FIXME: we should really do this when the consumer comes
         # back for an access token. If they never do, we've got stuff in a
@@ -295,15 +303,15 @@ class UserauthorizationAction extends Action
         }
 
         if ($avatar_url) {
-            if (!$this->add_avatar($profile, $avatar_url)) {
+            if (!$this->addAvatar($profile, $avatar_url)) {
                 return false;
             }
         }
 
         $user = common_current_user();
         $datastore = omb_oauth_datastore();
-        $consumer = $this->get_consumer($datastore, $req);
-        $token = $this->get_token($datastore, $req, $consumer);
+        $consumer = $this->getConsumer($datastore, $req);
+        $token = $this->getToken($datastore, $req, $consumer);
 
         $sub = new Subscription();
         $sub->subscriber = $user->id;
@@ -318,54 +326,60 @@ class UserauthorizationAction extends Action
         return true;
     }
 
-    function add_avatar($profile, $url)
+    function addAvatar($profile, $url)
     {
         $temp_filename = tempnam(sys_get_temp_dir(), 'listenee_avatar');
         copy($url, $temp_filename);
-        return $profile->setOriginal($temp_filename);
+        $imagefile = new ImageFile($profile->id, $temp_filename);
+        $filename = Avatar::filename($profile->id,
+                                     image_type_to_extension($imagefile->type),
+                                     null,
+                                     common_timestamp());
+        rename($temp_filename, Avatar::path($filename));
+        return $profile->setOriginal($filename);
     }
 
-    function show_accept_message($tok)
+    function showAcceptMessage($tok)
     {
         common_show_header(_('Subscription authorized'));
-        common_element('p', null,
+        $this->element('p', null,
                        _('The subscription has been authorized, but no '.
-                          'callback URL was passed. Check with the site\'s instructions for '.
-                          'details on how to authorize the subscription. Your subscription token is:'));
-        common_element('blockquote', 'token', $tok);
+                         'callback URL was passed. Check with the site\'s instructions for '.
+                         'details on how to authorize the subscription. Your subscription token is:'));
+        $this->element('blockquote', 'token', $tok);
         common_show_footer();
     }
 
-    function show_reject_message($tok)
+    function showRejectMessage($tok)
     {
         common_show_header(_('Subscription rejected'));
-        common_element('p', null,
+        $this->element('p', null,
                        _('The subscription has been rejected, but no '.
-                          'callback URL was passed. Check with the site\'s instructions for '.
-                          'details on how to fully reject the subscription.'));
+                         'callback URL was passed. Check with the site\'s instructions for '.
+                         'details on how to fully reject the subscription.'));
         common_show_footer();
     }
 
-    function store_request($req)
+    function storeRequest($req)
     {
         common_ensure_session();
         $_SESSION['userauthorizationrequest'] = $req;
     }
 
-    function clear_request()
+    function clearRequest()
     {
         common_ensure_session();
         unset($_SESSION['userauthorizationrequest']);
     }
 
-    function get_stored_request()
+    function getStoredRequest()
     {
         common_ensure_session();
         $req = $_SESSION['userauthorizationrequest'];
         return $req;
     }
 
-    function get_new_request()
+    function getNewRequest()
     {
         common_remove_magic_from_request();
         $req = OAuthRequest::from_request();
@@ -374,31 +388,22 @@ class UserauthorizationAction extends Action
 
     # Throws an OAuthException if anything goes wrong
 
-    function validate_request(&$req)
+    function validateRequest(&$req)
     {
         # OAuth stuff -- have to copy from OAuth.php since they're
         # all private methods, and there's no user-authentication method
-        common_debug('checking version', __FILE__);
-        $this->check_version($req);
-        common_debug('getting datastore', __FILE__);
+        $this->checkVersion($req);
         $datastore = omb_oauth_datastore();
-        common_debug('getting consumer', __FILE__);
-        $consumer = $this->get_consumer($datastore, $req);
-        common_debug('getting token', __FILE__);
-        $token = $this->get_token($datastore, $req, $consumer);
-        common_debug('checking timestamp', __FILE__);
-        $this->check_timestamp($req);
-        common_debug('checking nonce', __FILE__);
-        $this->check_nonce($datastore, $req, $consumer, $token);
-        common_debug('checking signature', __FILE__);
-        $this->check_signature($req, $consumer, $token);
-        common_debug('validating omb stuff', __FILE__);
-        $this->validate_omb($req);
-        common_debug('done validating', __FILE__);
+        $consumer = $this->getConsumer($datastore, $req);
+        $token = $this->getToken($datastore, $req, $consumer);
+        $this->checkTimestamp($req);
+        $this->checkNonce($datastore, $req, $consumer, $token);
+        $this->checkSignature($req, $consumer, $token);
+        $this->validateOmb($req);
         return true;
     }
 
-    function validate_omb(&$req)
+    function validateOmb(&$req)
     {
         foreach (array('omb_version', 'omb_listener', 'omb_listenee',
                        'omb_listenee_profile', 'omb_listenee_nickname',
@@ -470,19 +475,19 @@ class UserauthorizationAction extends Action
         }
         # optional stuff
         $fullname = $req->get_parameter('omb_listenee_fullname');
-        if ($fullname && strlen($fullname) > 255) {
+        if ($fullname && mb_strlen($fullname) > 255) {
             throw new OAuthException("Full name '$fullname' too long.");
         }
         $homepage = $req->get_parameter('omb_listenee_homepage');
-        if ($homepage && (!common_valid_http_url($homepage) || strlen($homepage) > 255)) {
+        if ($homepage && (!common_valid_http_url($homepage) || mb_strlen($homepage) > 255)) {
             throw new OAuthException("Invalid homepage '$homepage'");
         }
         $bio = $req->get_parameter('omb_listenee_bio');
-        if ($bio && strlen($bio) > 140) {
+        if ($bio && mb_strlen($bio) > 140) {
             throw new OAuthException("Bio too long '$bio'");
         }
         $location = $req->get_parameter('omb_listenee_location');
-        if ($location && strlen($location) > 255) {
+        if ($location && mb_strlen($location) > 255) {
             throw new OAuthException("Location too long '$location'");
         }
         $avatar = $req->get_parameter('omb_listenee_avatar');
@@ -513,7 +518,7 @@ class UserauthorizationAction extends Action
 
     # Snagged from OAuthServer
 
-    function check_version(&$req)
+    function checkVersion(&$req)
     {
         $version = $req->get_parameter("oauth_version");
         if (!$version) {
@@ -527,7 +532,7 @@ class UserauthorizationAction extends Action
 
     # Snagged from OAuthServer
 
-    function get_consumer($datastore, $req)
+    function getConsumer($datastore, $req)
     {
         $consumer_key = @$req->get_parameter("oauth_consumer_key");
         if (!$consumer_key) {
@@ -543,7 +548,7 @@ class UserauthorizationAction extends Action
 
     # Mostly cadged from OAuthServer
 
-    function get_token($datastore, &$req, $consumer)
+    function getToken($datastore, &$req, $consumer)
     {/*{{{*/
         $token_field = @$req->get_parameter('oauth_token');
         $token = $datastore->lookup_token($consumer, 'request', $token_field);
@@ -553,7 +558,7 @@ class UserauthorizationAction extends Action
         return $token;
     }
 
-    function check_timestamp(&$req)
+    function checkTimestamp(&$req)
     {
         $timestamp = @$req->get_parameter('oauth_timestamp');
         $now = time();
@@ -563,7 +568,7 @@ class UserauthorizationAction extends Action
     }
 
     # NOTE: don't call twice on the same request; will fail!
-    function check_nonce(&$datastore, &$req, $consumer, $token)
+    function checkNonce(&$datastore, &$req, $consumer, $token)
     {
         $timestamp = @$req->get_parameter('oauth_timestamp');
         $nonce = @$req->get_parameter('oauth_nonce');
@@ -574,9 +579,9 @@ class UserauthorizationAction extends Action
         return true;
     }
 
-    function check_signature(&$req, $consumer, $token)
+    function checkSignature(&$req, $consumer, $token)
     {
-        $signature_method = $this->get_signature_method($req);
+        $signature_method = $this->getSignatureMethod($req);
         $signature = $req->get_parameter('oauth_signature');
         $valid_sig = $signature_method->check_signature($req,
                                                         $consumer,
@@ -587,7 +592,7 @@ class UserauthorizationAction extends Action
         }
     }
 
-    function get_signature_method(&$req)
+    function getSignatureMethod(&$req)
     {
         $signature_method = @$req->get_parameter("oauth_signature_method");
         if (!$signature_method) {