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 // TRANS: Client error displayed when using remote subscribe for a local entity.
59 $this->clientError(_('You can use the local subscription!'));
63 $this->nickname = $this->trimmed('nickname');
64 $this->profile_url = $this->trimmed('profile_url');
69 function handle($args)
71 parent::handle($args);
73 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
74 /* Use a session token for CSRF protection. */
75 $token = $this->trimmed('token');
76 if (!$token || $token != common_session_token()) {
77 $this->showForm(_('There was a problem with your session token. '.
78 'Try again, please.'));
81 $this->remoteSubscription();
87 function showForm($err=null)
93 function showPageNotice()
96 $this->element('div', 'error', $this->err);
98 // TRANS: Page notice for remote subscribe. This message contains Markdown links.
99 // TRANS: Ensure to keep the correct markup of [link description](link).
100 $inst = _('To subscribe, you can [login](%%action.login%%),' .
101 ' or [register](%%action.register%%) a new ' .
102 ' account. If you already have an account ' .
103 ' on a [compatible microblogging site](%%doc.openmublog%%), ' .
104 ' enter your profile URL below.');
105 $output = common_markup_to_html($inst);
106 $this->elementStart('div', 'instructions');
108 $this->elementEnd('div');
114 // TRANS: Page title for Remote subscribe.
115 return _('Remote subscribe');
118 function showContent()
120 /* The id 'remotesubscribe' conflicts with the
121 button on profile page. */
122 $this->elementStart('form', array('id' => 'form_remote_subscribe',
124 'class' => 'form_settings',
125 'action' => common_local_url('remotesubscribe')));
126 $this->elementStart('fieldset');
127 // TRANS: Field legend on page for remote subscribe.
128 $this->element('legend', _('Subscribe to a remote user'));
129 $this->hidden('token', common_session_token());
131 $this->elementStart('ul', 'form_data');
132 $this->elementStart('li');
133 // TRANS: Field label on page for remote subscribe.
134 $this->input('nickname', _('User nickname'), $this->nickname,
135 // TRANS: Field title on page for remote subscribe.
136 _('Nickname of the user you want to follow.'));
137 $this->elementEnd('li');
138 $this->elementStart('li');
139 // TRANS: Field label on page for remote subscribe.
140 $this->input('profile_url', _('Profile URL'), $this->profile_url,
141 // TRANS: Field title on page for remote subscribe.
142 _('URL of your profile on another compatible microblogging service.'));
143 $this->elementEnd('li');
144 $this->elementEnd('ul');
145 // TRANS: Button text on page for remote subscribe.
146 $this->submit('submit', _m('BUTTON','Subscribe'));
147 $this->elementEnd('fieldset');
148 $this->elementEnd('form');
151 function remoteSubscription()
153 if (!$this->nickname) {
154 // TRANS: Form validation error on page for remote subscribe when no user was provided.
155 $this->showForm(_('No such user.'));
159 $user = User::staticGet('nickname', $this->nickname);
161 $this->profile_url = $this->trimmed('profile_url');
163 if (!$this->profile_url) {
164 // TRANS: Form validation error on page for remote subscribe when no user profile was found.
165 $this->showForm(_('No such user.'));
169 if (!common_valid_http_url($this->profile_url)) {
170 // TRANS: Form validation error on page for remote subscribe when an invalid profile URL was provided.
171 $this->showForm(_('Invalid profile URL (bad format).'));
176 $service = new OMB_Service_Consumer($this->profile_url,
178 omb_oauth_datastore());
179 } catch (OMB_InvalidYadisException $e) {
180 // TRANS: Form validation error on page for remote subscribe when no the provided profile URL
181 // TRANS: does not contain expected data.
182 $this->showForm(_('Not a valid profile URL (no YADIS document or ' .
183 'invalid XRDS defined).'));
187 if ($service->getServiceURI(OAUTH_ENDPOINT_REQUEST) ==
188 common_local_url('requesttoken') ||
189 User::staticGet('uri', $service->getRemoteUserURI())) {
190 // TRANS: Form validation error on page for remote subscribe.
191 $this->showForm(_('That is a local profile! Login to subscribe.'));
196 $service->requestToken();
197 } catch (OMB_RemoteServiceException $e) {
198 // TRANS: Form validation error on page for remote subscribe when the remote service is not providing a request token.
199 $this->showForm(_('Could not get a request token.'));
203 /* Create an OMB_Profile from $user. */
204 $profile = $user->getProfile();
206 common_log_db_error($user, 'SELECT', __FILE__);
207 // TRANS: Server error displayed on page for remote subscribe when user does not have a matching profile.
208 $this->serverError(_('User without matching profile.'));
212 $target_url = $service->requestAuthorization(
213 profile_to_omb_profile($user->uri, $profile),
214 common_local_url('finishremotesubscribe'));
216 common_ensure_session();
218 $_SESSION['oauth_authorization_request'] = serialize($service);
220 /* Redirect to the remote service for authorization. */
221 common_redirect($target_url, 303);