3 * Handler for remote subscription
9 * @author Evan Prodromou <evan@status.net>
10 * @author Robin Millette <millette@status.net>
11 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
12 * @link http://status.net/
14 * StatusNet - the distributed open-source microblogging tool
15 * Copyright (C) 2008, 2009, StatusNet, Inc.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU Affero General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU Affero General Public License for more details.
27 * You should have received a copy of the GNU Affero General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
33 require_once INSTALLDIR.'/lib/omb.php';
34 require_once INSTALLDIR.'/extlib/libomb/service_consumer.php';
35 require_once INSTALLDIR.'/extlib/libomb/profile.php';
38 * Handler for remote subscription
42 * @author Evan Prodromou <evan@status.net>
43 * @author Robin Millette <millette@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
45 * @link http://status.net/
47 class RemotesubscribeAction extends Action
53 function prepare($args)
55 parent::prepare($args);
57 if (common_logged_in()) {
58 $this->clientError(_('You can use the local subscription!'));
62 $this->nickname = $this->trimmed('nickname');
63 $this->profile_url = $this->trimmed('profile_url');
68 function handle($args)
70 parent::handle($args);
72 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
73 /* Use a session token for CSRF protection. */
74 $token = $this->trimmed('token');
75 if (!$token || $token != common_session_token()) {
76 $this->showForm(_('There was a problem with your session token. '.
77 'Try again, please.'));
80 $this->remoteSubscription();
86 function showForm($err=null)
92 function showPageNotice()
95 $this->element('div', 'error', $this->err);
97 $inst = _('To subscribe, you can [login](%%action.login%%),' .
98 ' or [register](%%action.register%%) a new ' .
99 ' account. If you already have an account ' .
100 ' on a [compatible microblogging site](%%doc.openmublog%%), ' .
101 ' enter your profile URL below.');
102 $output = common_markup_to_html($inst);
103 $this->elementStart('div', 'instructions');
105 $this->elementEnd('div');
111 return _('Remote subscribe');
114 function showContent()
116 /* The id 'remotesubscribe' conflicts with the
117 button on profile page. */
118 $this->elementStart('form', array('id' => 'form_remote_subscribe',
120 'class' => 'form_settings',
121 'action' => common_local_url('remotesubscribe')));
122 $this->elementStart('fieldset');
123 $this->element('legend', _('Subscribe to a remote user'));
124 $this->hidden('token', common_session_token());
126 $this->elementStart('ul', 'form_data');
127 $this->elementStart('li');
128 $this->input('nickname', _('User nickname'), $this->nickname,
129 _('Nickname of the user you want to follow.'));
130 $this->elementEnd('li');
131 $this->elementStart('li');
132 $this->input('profile_url', _('Profile URL'), $this->profile_url,
133 _('URL of your profile on another compatible microblogging service.'));
134 $this->elementEnd('li');
135 $this->elementEnd('ul');
136 $this->submit('submit', _('Subscribe'));
137 $this->elementEnd('fieldset');
138 $this->elementEnd('form');
141 function remoteSubscription()
143 if (!$this->nickname) {
144 $this->showForm(_('No such user.'));
148 $user = User::staticGet('nickname', $this->nickname);
150 $this->profile_url = $this->trimmed('profile_url');
152 if (!$this->profile_url) {
153 $this->showForm(_('No such user.'));
157 if (!common_valid_http_url($this->profile_url)) {
158 $this->showForm(_('Invalid profile URL (bad format).'));
163 $service = new OMB_Service_Consumer($this->profile_url,
165 omb_oauth_datastore());
166 } catch (OMB_InvalidYadisException $e) {
167 $this->showForm(_('Not a valid profile URL (no YADIS document or ' .
168 'invalid XRDS defined).'));
172 if ($service->getServiceURI(OAUTH_ENDPOINT_REQUEST) ==
173 common_local_url('requesttoken') ||
174 User::staticGet('uri', $service->getRemoteUserURI())) {
175 $this->showForm(_('That is a local profile! Login to subscribe.'));
180 $service->requestToken();
181 } catch (OMB_RemoteServiceException $e) {
182 $this->showForm(_('Could not get a request token.'));
186 /* Create an OMB_Profile from $user. */
187 $profile = $user->getProfile();
189 common_log_db_error($user, 'SELECT', __FILE__);
190 $this->serverError(_('User without matching profile.'));
194 $target_url = $service->requestAuthorization(
195 profile_to_omb_profile($user->uri, $profile),
196 common_local_url('finishremotesubscribe'));
198 common_ensure_session();
200 $_SESSION['oauth_authorization_request'] = serialize($service);
202 /* Redirect to the remote service for authorization. */
203 common_redirect($target_url, 303);