]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/remotesubscribe.php
Merge branch '1.0.x' of gitorious.org:statusnet/mainline into inline-comments
[quix0rs-gnu-social.git] / actions / remotesubscribe.php
1 <?php
2 /**
3  * Handler for remote subscription
4  *
5  * PHP version 5
6  *
7  * @category Action
8  * @package  StatusNet
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/
13  *
14  * StatusNet - the distributed open-source microblogging tool
15  * Copyright (C) 2008, 2009, StatusNet, Inc.
16  *
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.
21  *
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.
26  *
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/>.
29  **/
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
32
33 require_once INSTALLDIR.'/lib/omb.php';
34 require_once INSTALLDIR.'/extlib/libomb/service_consumer.php';
35 require_once INSTALLDIR.'/extlib/libomb/profile.php';
36
37 /**
38  * Handler for remote subscription
39  *
40  * @category Action
41  * @package  StatusNet
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/
46  */
47 class RemotesubscribeAction extends Action
48 {
49     var $nickname;
50     var $profile_url;
51     var $err;
52
53     function prepare($args)
54     {
55         parent::prepare($args);
56
57         if (common_logged_in()) {
58             $this->clientError(_('You can use the local subscription!'));
59             return false;
60         }
61
62         $this->nickname    = $this->trimmed('nickname');
63         $this->profile_url = $this->trimmed('profile_url');
64
65         return true;
66     }
67
68     function handle($args)
69     {
70         parent::handle($args);
71
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.'));
78                 return;
79             }
80             $this->remoteSubscription();
81         } else {
82             $this->showForm();
83         }
84     }
85
86     function showForm($err=null)
87     {
88         $this->err = $err;
89         $this->showPage();
90     }
91
92     function showPageNotice()
93     {
94         if ($this->err) {
95             $this->element('div', 'error', $this->err);
96         } else {
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');
104             $this->raw($output);
105             $this->elementEnd('div');
106         }
107     }
108
109     function title()
110     {
111         return _('Remote subscribe');
112     }
113
114     function showContent()
115     {
116         /* The id 'remotesubscribe' conflicts with the
117            button on profile page. */
118         $this->elementStart('form', array('id' => 'form_remote_subscribe',
119                                           'method' => 'post',
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());
125
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');
139     }
140
141     function remoteSubscription()
142     {
143         if (!$this->nickname) {
144             $this->showForm(_('No such user.'));
145             return;
146         }
147
148         $user = User::staticGet('nickname', $this->nickname);
149
150         $this->profile_url = $this->trimmed('profile_url');
151
152         if (!$this->profile_url) {
153             $this->showForm(_('No such user.'));
154             return;
155         }
156
157         if (!common_valid_http_url($this->profile_url)) {
158             $this->showForm(_('Invalid profile URL (bad format).'));
159             return;
160         }
161
162         try {
163             $service = new OMB_Service_Consumer($this->profile_url,
164                                                 common_root_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).'));
169             return;
170         }
171
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.'));
176             return;
177         }
178
179         try {
180             $service->requestToken();
181         } catch (OMB_RemoteServiceException $e) {
182             $this->showForm(_('Could not get a request token.'));
183             return;
184         }
185
186         /* Create an OMB_Profile from $user. */
187         $profile = $user->getProfile();
188         if (!$profile) {
189             common_log_db_error($user, 'SELECT', __FILE__);
190             $this->serverError(_('User without matching profile.'));
191             return;
192         }
193
194         $target_url = $service->requestAuthorization(
195                                    profile_to_omb_profile($user->uri, $profile),
196                                    common_local_url('finishremotesubscribe'));
197
198         common_ensure_session();
199
200         $_SESSION['oauth_authorization_request'] = serialize($service);
201
202         /* Redirect to the remote service for authorization. */
203         common_redirect($target_url, 303);
204     }
205 }