]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/remotesubscribe.php
fix notices in dumpschema
[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
48 class RemotesubscribeAction extends Action
49 {
50     var $nickname;
51     var $profile_url;
52     var $err;
53
54     function prepare($args)
55     {
56         parent::prepare($args);
57
58         if (common_logged_in()) {
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                 $this->showForm(_('There was a problem with your session token. '.
78                                   'Try again, please.'));
79                 return;
80             }
81             $this->remoteSubscription();
82         } else {
83             $this->showForm();
84         }
85     }
86
87     function showForm($err=null)
88     {
89         $this->err = $err;
90         $this->showPage();
91     }
92
93     function showPageNotice()
94     {
95         if ($this->err) {
96             $this->element('div', 'error', $this->err);
97         } else {
98             $inst = _('To subscribe, you can [login](%%action.login%%),' .
99                       ' or [register](%%action.register%%) a new ' .
100                       ' account. If you already have an account ' .
101                       ' on a [compatible microblogging site](%%doc.openmublog%%), ' .
102                       ' enter your profile URL below.');
103             $output = common_markup_to_html($inst);
104             $this->elementStart('div', 'instructions');
105             $this->raw($output);
106             $this->elementEnd('div');
107         }
108     }
109
110     function title()
111     {
112         return _('Remote subscribe');
113     }
114
115     function showContent()
116     {
117         /* The id 'remotesubscribe' conflicts with the
118            button on profile page. */
119         $this->elementStart('form', array('id' => 'form_remote_subscribe',
120                                           'method' => 'post',
121                                           'class' => 'form_settings',
122                                           'action' => common_local_url('remotesubscribe')));
123         $this->elementStart('fieldset');
124         $this->element('legend', _('Subscribe to a remote user'));
125         $this->hidden('token', common_session_token());
126
127         $this->elementStart('ul', 'form_data');
128         $this->elementStart('li');
129         $this->input('nickname', _('User nickname'), $this->nickname,
130                      _('Nickname of the user you want to follow'));
131         $this->elementEnd('li');
132         $this->elementStart('li');
133         $this->input('profile_url', _('Profile URL'), $this->profile_url,
134                      _('URL of your profile on another compatible microblogging service'));
135         $this->elementEnd('li');
136         $this->elementEnd('ul');
137         $this->submit('submit', _('Subscribe'));
138         $this->elementEnd('fieldset');
139         $this->elementEnd('form');
140     }
141
142     function remoteSubscription()
143     {
144         if (!$this->nickname) {
145             $this->showForm(_('No such user.'));
146             return;
147         }
148
149         $user = User::staticGet('nickname', $this->nickname);
150
151         $this->profile_url = $this->trimmed('profile_url');
152
153         if (!$this->profile_url) {
154             $this->showForm(_('No such user.'));
155             return;
156         }
157
158         if (!common_valid_http_url($this->profile_url)) {
159             $this->showForm(_('Invalid profile URL (bad format)'));
160             return;
161         }
162
163         try {
164             $service = new OMB_Service_Consumer($this->profile_url,
165                                                 common_root_url(),
166                                                 omb_oauth_datastore());
167         } catch (OMB_InvalidYadisException $e) {
168             $this->showForm(_('Not a valid profile URL (no YADIS document or ' .
169                               'invalid XRDS defined).'));
170             return;
171         }
172
173         if ($service->getServiceURI(OAUTH_ENDPOINT_REQUEST) ==
174             common_local_url('requesttoken') ||
175             User::staticGet('uri', $service->getRemoteUserURI())) {
176             $this->showForm(_('That’s a local profile! Login to subscribe.'));
177             return;
178         }
179
180         try {
181             $service->requestToken();
182         } catch (OMB_RemoteServiceException $e) {
183             $this->showForm(_('Couldn’t get a request token.'));
184             return;
185         }
186
187         /* Create an OMB_Profile from $user. */
188         $profile = $user->getProfile();
189         if (!$profile) {
190             common_log_db_error($user, 'SELECT', __FILE__);
191             $this->serverError(_('User without matching profile.'));
192             return;
193         }
194
195         $target_url = $service->requestAuthorization(
196                                    profile_to_omb_profile($user->uri, $profile),
197                                    common_local_url('finishremotesubscribe'));
198
199         common_ensure_session();
200
201         $_SESSION['oauth_authorization_request'] = serialize($service);
202
203         /* Redirect to the remote service for authorization. */
204         common_redirect($target_url, 303);
205     }
206 }
207 ?>