3 * Handler for remote subscription
9 * @author Evan Prodromou <evan@controlyourself.ca>
10 * @author Robin Millette <millette@controlyourself.ca>
11 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
12 * @link http://laconi.ca/
14 * Laconica - a distributed open-source microblogging tool
15 * Copyright (C) 2008, 2009, Control Yourself, 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('LACONICA')) {
35 require_once INSTALLDIR.'/lib/omb.php';
36 require_once INSTALLDIR.'/extlib/libomb/service_consumer.php';
37 require_once INSTALLDIR.'/extlib/libomb/profile.php';
40 * Handler for remote subscription
44 * @author Evan Prodromou <evan@controlyourself.ca>
45 * @author Robin Millette <millette@controlyourself.ca>
46 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
47 * @link http://laconi.ca/
50 class RemotesubscribeAction extends Action
56 function prepare($args)
58 parent::prepare($args);
60 if (common_logged_in()) {
61 $this->clientError(_('You can use the local subscription!'));
65 $this->nickname = $this->trimmed('nickname');
66 $this->profile_url = $this->trimmed('profile_url');
71 function handle($args)
73 parent::handle($args);
75 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
76 /* Use a session token for CSRF protection. */
77 $token = $this->trimmed('token');
78 if (!$token || $token != common_session_token()) {
79 $this->showForm(_('There was a problem with your session token. '.
80 'Try again, please.'));
83 $this->remoteSubscription();
89 function showForm($err=null)
95 function showPageNotice()
98 $this->element('div', 'error', $this->err);
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 return _('Remote subscribe');
117 function showContent()
119 /* The id 'remotesubscribe' conflicts with the
120 button on profile page. */
121 $this->elementStart('form', array('id' => 'form_remote_subscribe',
123 'class' => 'form_settings',
124 'action' => common_local_url('remotesubscribe')));
125 $this->elementStart('fieldset');
126 $this->element('legend', _('Subscribe to a remote user'));
127 $this->hidden('token', common_session_token());
129 $this->elementStart('ul', 'form_data');
130 $this->elementStart('li');
131 $this->input('nickname', _('User nickname'), $this->nickname,
132 _('Nickname of the user you want to follow'));
133 $this->elementEnd('li');
134 $this->elementStart('li');
135 $this->input('profile_url', _('Profile URL'), $this->profile_url,
136 _('URL of your profile on another compatible microblogging service'));
137 $this->elementEnd('li');
138 $this->elementEnd('ul');
139 $this->submit('submit', _('Subscribe'));
140 $this->elementEnd('fieldset');
141 $this->elementEnd('form');
144 function remoteSubscription()
146 if (!$this->nickname) {
147 $this->showForm(_('No such user.'));
151 $user = User::staticGet('nickname', $this->nickname);
153 $this->profile_url = $this->trimmed('profile_url');
155 if (!$this->profile_url) {
156 $this->showForm(_('No such user'));
160 if (!common_valid_http_url($this->profile_url)) {
161 $this->showForm(_('Invalid profile URL (bad format)'));
166 $service = new OMB_Service_Consumer($this->profile_url,
168 omb_oauth_datastore());
169 } catch (OMB_InvalidYadisException $e) {
170 $this->showForm(_('Not a valid profile URL (no YADIS document or ' .
171 'no or invalid XRDS defined).'));
175 if ($service->getServiceURI(OAUTH_ENDPOINT_REQUEST) ==
176 common_local_url('requesttoken') ||
177 User::staticGet('uri', $service->getRemoteUserURI())) {
178 $this->showForm(_('That’s a local profile! Login to subscribe.'));
183 $service->requestToken();
184 } catch (OMB_RemoteServiceException $e) {
185 $this->showForm(_('Couldn’t get a request token.'));
189 /* Create an OMB_Profile from $user. */
190 $profile = $user->getProfile();
192 common_log_db_error($user, 'SELECT', __FILE__);
193 $this->serverError(_('User without matching profile'));
197 $target_url = $service->requestAuthorization(
198 profile_to_omb_profile($user->uri, $profile),
199 common_local_url('finishremotesubscribe'));
201 common_ensure_session();
203 $_SESSION['oauth_authorization_request'] = serialize($service);
205 /* Redirect to the remote service for authorization. */
206 common_redirect($target_url, 303);