]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/remotesubscribe.php
Update translator documentation.
[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             // TRANS: Client error displayed when using remote subscribe for a local entity.
59             $this->clientError(_('You can use the local subscription!'));
60             return false;
61         }
62
63         $this->nickname    = $this->trimmed('nickname');
64         $this->profile_url = $this->trimmed('profile_url');
65
66         return true;
67     }
68
69     function handle($args)
70     {
71         parent::handle($args);
72
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                 // TRANS: Client error displayed when the session token does not match or is not given.
78                 $this->showForm(_('There was a problem with your session token. '.
79                                   'Try again, please.'));
80                 return;
81             }
82             $this->remoteSubscription();
83         } else {
84             $this->showForm();
85         }
86     }
87
88     function showForm($err=null)
89     {
90         $this->err = $err;
91         $this->showPage();
92     }
93
94     function showPageNotice()
95     {
96         if ($this->err) {
97             $this->element('div', 'error', $this->err);
98         } else {
99             // TRANS: Page notice for remote subscribe. This message contains Markdown links.
100             // TRANS: Ensure to keep the correct markup of [link description](link).
101             $inst = _('To subscribe, you can [login](%%action.login%%),' .
102                       ' or [register](%%action.register%%) a new ' .
103                       ' account. If you already have an account ' .
104                       ' on a [compatible microblogging site](%%doc.openmublog%%), ' .
105                       ' enter your profile URL below.');
106             $output = common_markup_to_html($inst);
107             $this->elementStart('div', 'instructions');
108             $this->raw($output);
109             $this->elementEnd('div');
110         }
111     }
112
113     function title()
114     {
115         // TRANS: Page title for Remote subscribe.
116         return _('Remote subscribe');
117     }
118
119     function showContent()
120     {
121         /* The id 'remotesubscribe' conflicts with the
122            button on profile page. */
123         $this->elementStart('form', array('id' => 'form_remote_subscribe',
124                                           'method' => 'post',
125                                           'class' => 'form_settings',
126                                           'action' => common_local_url('remotesubscribe')));
127         $this->elementStart('fieldset');
128         // TRANS: Field legend on page for remote subscribe.
129         $this->element('legend', _('Subscribe to a remote user'));
130         $this->hidden('token', common_session_token());
131
132         $this->elementStart('ul', 'form_data');
133         $this->elementStart('li');
134         // TRANS: Field label on page for remote subscribe.
135         $this->input('nickname', _('User nickname'), $this->nickname,
136                      // TRANS: Field title on page for remote subscribe.
137                      _('Nickname of the user you want to follow.'));
138         $this->elementEnd('li');
139         $this->elementStart('li');
140         // TRANS: Field label on page for remote subscribe.
141         $this->input('profile_url', _('Profile URL'), $this->profile_url,
142                      // TRANS: Field title on page for remote subscribe.
143                      _('URL of your profile on another compatible microblogging service.'));
144         $this->elementEnd('li');
145         $this->elementEnd('ul');
146         // TRANS: Button text on page for remote subscribe.
147         $this->submit('submit', _m('BUTTON','Subscribe'));
148         $this->elementEnd('fieldset');
149         $this->elementEnd('form');
150     }
151
152     function remoteSubscription()
153     {
154         if (!$this->nickname) {
155             // TRANS: Form validation error on page for remote subscribe when no user was provided.
156             $this->showForm(_('No such user.'));
157             return;
158         }
159
160         $user = User::staticGet('nickname', $this->nickname);
161
162         $this->profile_url = $this->trimmed('profile_url');
163
164         if (!$this->profile_url) {
165             // TRANS: Form validation error on page for remote subscribe when no user profile was found.
166             $this->showForm(_('No such user.'));
167             return;
168         }
169
170         if (!common_valid_http_url($this->profile_url)) {
171             // TRANS: Form validation error on page for remote subscribe when an invalid profile URL was provided.
172             $this->showForm(_('Invalid profile URL (bad format).'));
173             return;
174         }
175
176         try {
177             $service = new OMB_Service_Consumer($this->profile_url,
178                                                 common_root_url(),
179                                                 omb_oauth_datastore());
180         } catch (OMB_InvalidYadisException $e) {
181             // TRANS: Form validation error on page for remote subscribe when no the provided profile URL
182             // TRANS: does not contain expected data.
183             $this->showForm(_('Not a valid profile URL (no YADIS document or ' .
184                               'invalid XRDS defined).'));
185             return;
186         }
187
188         if ($service->getServiceURI(OAUTH_ENDPOINT_REQUEST) ==
189             common_local_url('requesttoken') ||
190             User::staticGet('uri', $service->getRemoteUserURI())) {
191             // TRANS: Form validation error on page for remote subscribe.
192             $this->showForm(_('That is a local profile! Login to subscribe.'));
193             return;
194         }
195
196         try {
197             $service->requestToken();
198         } catch (OMB_RemoteServiceException $e) {
199             // TRANS: Form validation error on page for remote subscribe when the remote service is not providing a request token.
200             $this->showForm(_('Could not get a request token.'));
201             return;
202         }
203
204         /* Create an OMB_Profile from $user. */
205         $profile = $user->getProfile();
206         if (!$profile) {
207             common_log_db_error($user, 'SELECT', __FILE__);
208             // TRANS: Server error displayed on page for remote subscribe when user does not have a matching profile.
209             $this->serverError(_('User without matching profile.'));
210             return;
211         }
212
213         $target_url = $service->requestAuthorization(
214                                    profile_to_omb_profile($user->uri, $profile),
215                                    common_local_url('finishremotesubscribe'));
216
217         common_ensure_session();
218
219         $_SESSION['oauth_authorization_request'] = serialize($service);
220
221         /* Redirect to the remote service for authorization. */
222         common_redirect($target_url, 303);
223     }
224 }