3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @package OStatusPlugin
22 * @maintainer Brion Vibber <brion@status.net>
25 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
30 * showInputForm() - form asking for a remote profile account or URL
31 * We end up back here on errors
33 * showPreviewForm() - surrounding form for preview-and-confirm
34 * previewUser() - display profile for a remote user
35 * previewGroup() - display profile for a remote group
37 * successUser() - redirects to subscriptions page on subscribe
38 * successGroup() - redirects to groups page on join
40 class OStatusSubAction extends Action
42 protected $profile_uri; // provided acct: or URI of remote entity
43 protected $oprofile; // Ostatus_profile of remote entity, if valid
46 * Show the initial form, when we haven't yet been given a valid
49 function showInputForm()
51 $user = common_current_user();
53 $profile = $user->getProfile();
55 $this->elementStart('form', array('method' => 'post',
56 'id' => 'form_ostatus_sub',
57 'class' => 'form_settings',
59 common_local_url('ostatussub')));
61 $this->hidden('token', common_session_token());
63 $this->elementStart('fieldset', array('id' => 'settings_feeds'));
65 $this->elementStart('ul', 'form_data');
66 $this->elementStart('li');
67 $this->input('profile',
68 _m('Address or profile URL'),
70 _m('Enter the profile URL of a PubSubHubbub-enabled feed'));
71 $this->elementEnd('li');
72 $this->elementEnd('ul');
74 $this->submit('validate', _m('Continue'));
76 $this->elementEnd('fieldset');
78 $this->elementEnd('form');
82 * Show the preview-and-confirm form. We've got a valid remote
83 * profile and are ready to poke it!
85 * This controls the wrapper form; actual profile display will
86 * be in previewUser() or previewGroup() depending on the type.
88 function showPreviewForm()
90 if ($this->oprofile->isGroup()) {
91 $this->previewGroup();
96 $this->elementStart('div', 'entity_actions');
97 $this->elementStart('ul');
98 $this->elementStart('li', 'entity_subscribe');
99 $this->elementStart('form', array('method' => 'post',
100 'id' => 'form_ostatus_sub',
101 'class' => 'form_remote_authorize',
103 common_local_url('ostatussub')));
104 $this->elementStart('fieldset');
105 $this->hidden('token', common_session_token());
106 $this->hidden('profile', $this->profile_uri);
107 if ($this->oprofile->isGroup()) {
108 $this->submit('submit', _m('Join'), 'submit', null,
109 _m('Join this group'));
111 $this->submit('submit', _m('Subscribe'), 'submit', null,
112 _m('Subscribe to this user'));
114 $this->elementEnd('fieldset');
115 $this->elementEnd('form');
116 $this->elementEnd('li');
117 $this->elementEnd('ul');
118 $this->elementEnd('div');
122 * Show a preview for a remote user's profile
124 function previewUser()
126 $oprofile = $this->oprofile;
127 $profile = $oprofile->localProfile();
129 $this->showEntity($profile);
133 * Show a preview for a remote group's profile
135 function previewGroup()
137 $oprofile = $this->oprofile;
138 $group = $oprofile->localGroup();
140 $this->showEntity($group);
144 function showEntity($entity)
146 $nickname = $entity->nickname;
147 $profile = $entity->profileurl;
148 $fullname = $entity->fullname;
149 $homepage = $entity->homepage;
151 $location = $entity->location;
152 $avatar = $entity->avatarurl;
154 $this->elementStart('div', 'entity_profile vcard');
155 $this->elementStart('dl', 'entity_depiction');
156 $this->element('dt', null, _('Photo'));
157 $this->elementStart('dd');
159 $this->element('img', array('src' => $avatar,
160 'class' => 'photo avatar',
161 'width' => AVATAR_PROFILE_SIZE,
162 'height' => AVATAR_PROFILE_SIZE,
163 'alt' => $nickname));
165 $this->elementEnd('dd');
166 $this->elementEnd('dl');
168 $this->elementStart('dl', 'entity_nickname');
169 $this->element('dt', null, _('Nickname'));
170 $this->elementStart('dd');
171 $hasFN = ($fullname !== '') ? 'nickname' : 'fn nickname';
172 $this->elementStart('a', array('href' => $profile,
173 'class' => 'url '.$hasFN));
174 $this->raw($nickname);
175 $this->elementEnd('a');
176 $this->elementEnd('dd');
177 $this->elementEnd('dl');
179 if (!is_null($fullname)) {
180 $this->elementStart('dl', 'entity_fn');
181 $this->elementStart('dd');
182 $this->elementStart('span', 'fn');
183 $this->raw($fullname);
184 $this->elementEnd('span');
185 $this->elementEnd('dd');
186 $this->elementEnd('dl');
188 if (!is_null($location)) {
189 $this->elementStart('dl', 'entity_location');
190 $this->element('dt', null, _('Location'));
191 $this->elementStart('dd', 'label');
192 $this->raw($location);
193 $this->elementEnd('dd');
194 $this->elementEnd('dl');
197 if (!is_null($homepage)) {
198 $this->elementStart('dl', 'entity_url');
199 $this->element('dt', null, _('URL'));
200 $this->elementStart('dd');
201 $this->elementStart('a', array('href' => $homepage,
203 $this->raw($homepage);
204 $this->elementEnd('a');
205 $this->elementEnd('dd');
206 $this->elementEnd('dl');
209 if (!is_null($bio)) {
210 $this->elementStart('dl', 'entity_note');
211 $this->element('dt', null, _('Note'));
212 $this->elementStart('dd', 'note');
214 $this->elementEnd('dd');
215 $this->elementEnd('dl');
217 $this->elementEnd('div');
221 * Redirect on successful remote user subscription
223 function successUser()
225 $cur = common_current_user();
226 $url = common_local_url('subscriptions', array('nickname' => $cur->nickname));
227 common_redirect($url, 303);
231 * Redirect on successful remote group join
233 function successGroup()
235 $cur = common_current_user();
236 $url = common_local_url('usergroups', array('nickname' => $cur->nickname));
237 common_redirect($url, 303);
241 * Pull data for a remote profile and check if it's valid.
242 * Fills out error UI string in $this->error
243 * Fills out $this->oprofile on success.
247 function validateFeed()
249 $profile_uri = trim($this->arg('profile'));
251 if ($profile_uri == '') {
252 $this->showForm(_m('Empty remote profile URL!'));
255 $this->profile_uri = $profile_uri;
257 // @fixme validate, normalize bla bla
259 $oprofile = Ostatus_profile::ensureProfile($this->profile_uri);
260 $this->oprofile = $oprofile;
262 } catch (FeedSubBadURLException $e) {
263 $this->error = _m('Invalid URL or could not reach server.');
264 } catch (FeedSubBadResponseException $e) {
265 $this->error = _m('Cannot read feed; server returned error.');
266 } catch (FeedSubEmptyException $e) {
267 $this->error = _m('Cannot read feed; server returned an empty page.');
268 } catch (FeedSubBadHTMLException $e) {
269 $this->error = _m('Bad HTML, could not find feed link.');
270 } catch (FeedSubNoFeedException $e) {
271 $this->error = _m('Could not find a feed linked from this URL.');
272 } catch (FeedSubUnrecognizedTypeException $e) {
273 $this->error = _m('Not a recognized feed type.');
274 } catch (FeedSubException $e) {
275 // Any new ones we forgot about
276 $this->error = sprintf(_m('Bad feed URL: %s %s'), get_class($e), $e->getMessage());
283 * Attempt to finalize subscription.
284 * validateFeed must have been run first.
286 * Calls showForm on failure or successUser/successGroup on success.
290 // And subscribe the current user to the local profile
291 $user = common_current_user();
293 if (!$this->oprofile->subscribe()) {
294 $this->showForm(_m("Failed to set up server-to-server subscription."));
298 if ($this->oprofile->isGroup()) {
299 $group = $this->oprofile->localGroup();
300 if ($user->isMember($group)) {
301 $this->showForm(_m('Already a member!'));
302 } elseif (Group_member::join($this->oprofile->group_id, $user->id)) {
303 $this->successGroup();
305 $this->showForm(_m('Remote group join failed!'));
308 $local = $this->oprofile->localProfile();
309 if ($user->isSubscribed($local)) {
310 $this->showForm(_m('Already subscribed!'));
311 } elseif ($this->oprofile->subscribeLocalToRemote($user)) {
312 $this->successUser();
314 $this->showForm(_m('Remote subscription failed!'));
319 function prepare($args)
321 parent::prepare($args);
323 if (!common_logged_in()) {
324 // XXX: selfURL() didn't work. :<
325 common_set_returnto($_SERVER['REQUEST_URI']);
326 if (Event::handle('RedirectToLogin', array($this, null))) {
327 common_redirect(common_local_url('login'), 303);
332 $this->profile_uri = $this->arg('profile');
338 * Handle the submission.
340 function handle($args)
342 parent::handle($args);
343 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
346 if ($this->arg('profile')) {
347 $this->validateFeed();
355 * Handle posts to this form
360 function handlePost()
363 $token = $this->trimmed('token');
364 if (!$token || $token != common_session_token()) {
365 $this->showForm(_('There was a problem with your session token. '.
366 'Try again, please.'));
370 if ($this->validateFeed()) {
371 if ($this->arg('subscribe')) {
380 * Show the appropriate form based on our input state.
382 function showForm($err=null)
387 if ($this->boolean('ajax')) {
388 header('Content-Type: text/xml;charset=utf-8');
389 $this->xw->startDocument('1.0', 'UTF-8');
390 $this->elementStart('html');
391 $this->elementStart('head');
392 $this->element('title', null, _m('Subscribe to user'));
393 $this->elementEnd('head');
394 $this->elementStart('body');
395 $this->showContent();
396 $this->elementEnd('body');
397 $this->elementEnd('html');
406 * @return string Title of the page
411 return _m('Authorize subscription');
415 * Instructions for use
417 * @return instructions for use
420 function getInstructions()
422 return _m('You can subscribe to users from other supported sites. Paste their address or profile URI below:');
425 function showPageNotice()
428 $this->element('p', 'error', $this->error);
433 * Content area of the page
435 * Shows a form for associating a remote OStatus account with this
441 function showContent()
443 if ($this->oprofile) {
444 $this->showPreviewForm();
446 $this->showInputForm();
450 function showScripts()
452 parent::showScripts();
453 $this->autofocus('feedurl');