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 $ok = $this->previewGroup();
93 $ok = $this->previewUser();
96 // @fixme maybe provide a cancel button or link back?
100 $this->elementStart('div', 'entity_actions');
101 $this->elementStart('ul');
102 $this->elementStart('li', 'entity_subscribe');
103 $this->elementStart('form', array('method' => 'post',
104 'id' => 'form_ostatus_sub',
105 'class' => 'form_remote_authorize',
107 common_local_url('ostatussub')));
108 $this->elementStart('fieldset');
109 $this->hidden('token', common_session_token());
110 $this->hidden('profile', $this->profile_uri);
111 if ($this->oprofile->isGroup()) {
112 $this->submit('submit', _m('Join'), 'submit', null,
113 _m('Join this group'));
115 $this->submit('submit', _m('Subscribe'), 'submit', null,
116 _m('Subscribe to this user'));
118 $this->elementEnd('fieldset');
119 $this->elementEnd('form');
120 $this->elementEnd('li');
121 $this->elementEnd('ul');
122 $this->elementEnd('div');
126 * Show a preview for a remote user's profile
127 * @return boolean true if we're ok to try subscribing
129 function previewUser()
131 $oprofile = $this->oprofile;
132 $profile = $oprofile->localProfile();
134 $cur = common_current_user();
135 if ($cur->isSubscribed($profile)) {
136 $this->element('div', array('class' => 'error'),
137 _m("You are already subscribed to this user."));
143 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
144 $avatarUrl = $avatar ? $avatar->displayUrl() : false;
146 $this->showEntity($profile,
147 $profile->profileurl,
154 * Show a preview for a remote group's profile
155 * @return boolean true if we're ok to try joining
157 function previewGroup()
159 $oprofile = $this->oprofile;
160 $group = $oprofile->localGroup();
162 $cur = common_current_user();
163 if ($cur->isMember($group)) {
164 $this->element('div', array('class' => 'error'),
165 _m("You are already a member of this group."));
171 $this->showEntity($group,
172 $group->getProfileUrl(),
173 $group->homepage_logo,
174 $group->description);
179 function showEntity($entity, $profile, $avatar, $note)
181 $nickname = $entity->nickname;
182 $fullname = $entity->fullname;
183 $homepage = $entity->homepage;
184 $location = $entity->location;
187 $avatar = Avatar::defaultImage(AVATAR_PROFILE_SIZE);
190 $this->elementStart('div', 'entity_profile vcard');
191 $this->elementStart('dl', 'entity_depiction');
192 $this->element('dt', null, _('Photo'));
193 $this->elementStart('dd');
194 $this->element('img', array('src' => $avatar,
195 'class' => 'photo avatar',
196 'width' => AVATAR_PROFILE_SIZE,
197 'height' => AVATAR_PROFILE_SIZE,
198 'alt' => $nickname));
199 $this->elementEnd('dd');
200 $this->elementEnd('dl');
202 $this->elementStart('dl', 'entity_nickname');
203 $this->element('dt', null, _('Nickname'));
204 $this->elementStart('dd');
205 $hasFN = ($fullname !== '') ? 'nickname' : 'fn nickname';
206 $this->elementStart('a', array('href' => $profile,
207 'class' => 'url '.$hasFN));
208 $this->raw($nickname);
209 $this->elementEnd('a');
210 $this->elementEnd('dd');
211 $this->elementEnd('dl');
213 if (!is_null($fullname)) {
214 $this->elementStart('dl', 'entity_fn');
215 $this->elementStart('dd');
216 $this->elementStart('span', 'fn');
217 $this->raw($fullname);
218 $this->elementEnd('span');
219 $this->elementEnd('dd');
220 $this->elementEnd('dl');
222 if (!is_null($location)) {
223 $this->elementStart('dl', 'entity_location');
224 $this->element('dt', null, _('Location'));
225 $this->elementStart('dd', 'label');
226 $this->raw($location);
227 $this->elementEnd('dd');
228 $this->elementEnd('dl');
231 if (!is_null($homepage)) {
232 $this->elementStart('dl', 'entity_url');
233 $this->element('dt', null, _('URL'));
234 $this->elementStart('dd');
235 $this->elementStart('a', array('href' => $homepage,
237 $this->raw($homepage);
238 $this->elementEnd('a');
239 $this->elementEnd('dd');
240 $this->elementEnd('dl');
243 if (!is_null($note)) {
244 $this->elementStart('dl', 'entity_note');
245 $this->element('dt', null, _('Note'));
246 $this->elementStart('dd', 'note');
248 $this->elementEnd('dd');
249 $this->elementEnd('dl');
251 $this->elementEnd('div');
255 * Redirect on successful remote user subscription
257 function successUser()
259 $cur = common_current_user();
260 $url = common_local_url('subscriptions', array('nickname' => $cur->nickname));
261 common_redirect($url, 303);
265 * Redirect on successful remote group join
267 function successGroup()
269 $cur = common_current_user();
270 $url = common_local_url('usergroups', array('nickname' => $cur->nickname));
271 common_redirect($url, 303);
275 * Pull data for a remote profile and check if it's valid.
276 * Fills out error UI string in $this->error
277 * Fills out $this->oprofile on success.
281 function validateFeed()
283 $profile_uri = trim($this->arg('profile'));
285 if ($profile_uri == '') {
286 $this->showForm(_m('Empty remote profile URL!'));
289 $this->profile_uri = $profile_uri;
291 // @fixme validate, normalize bla bla
293 $oprofile = Ostatus_profile::ensureProfile($this->profile_uri);
294 $this->oprofile = $oprofile;
296 } catch (FeedSubBadURLException $e) {
297 $this->error = _m('Invalid URL or could not reach server.');
298 } catch (FeedSubBadResponseException $e) {
299 $this->error = _m('Cannot read feed; server returned error.');
300 } catch (FeedSubEmptyException $e) {
301 $this->error = _m('Cannot read feed; server returned an empty page.');
302 } catch (FeedSubBadHTMLException $e) {
303 $this->error = _m('Bad HTML, could not find feed link.');
304 } catch (FeedSubNoFeedException $e) {
305 $this->error = _m('Could not find a feed linked from this URL.');
306 } catch (FeedSubUnrecognizedTypeException $e) {
307 $this->error = _m('Not a recognized feed type.');
308 } catch (FeedSubException $e) {
309 // Any new ones we forgot about
310 $this->error = sprintf(_m('Bad feed URL: %s %s'), get_class($e), $e->getMessage());
317 * Attempt to finalize subscription.
318 * validateFeed must have been run first.
320 * Calls showForm on failure or successUser/successGroup on success.
324 // And subscribe the current user to the local profile
325 $user = common_current_user();
327 if ($this->oprofile->isGroup()) {
328 $group = $this->oprofile->localGroup();
329 if ($user->isMember($group)) {
330 $this->showForm(_m('Already a member!'));
331 } elseif (Group_member::join($this->oprofile->group_id, $user->id)) {
332 $this->successGroup();
334 $this->showForm(_m('Remote group join failed!'));
337 $local = $this->oprofile->localProfile();
338 if ($user->isSubscribed($local)) {
339 $this->showForm(_m('Already subscribed!'));
340 } elseif ($this->oprofile->subscribeLocalToRemote($user)) {
341 $this->successUser();
343 $this->showForm(_m('Remote subscription failed!'));
348 function prepare($args)
350 parent::prepare($args);
352 if (!common_logged_in()) {
353 // XXX: selfURL() didn't work. :<
354 common_set_returnto($_SERVER['REQUEST_URI']);
355 if (Event::handle('RedirectToLogin', array($this, null))) {
356 common_redirect(common_local_url('login'), 303);
361 $this->profile_uri = $this->arg('profile');
367 * Handle the submission.
369 function handle($args)
371 parent::handle($args);
372 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
375 if ($this->arg('profile')) {
376 $this->validateFeed();
384 * Handle posts to this form
389 function handlePost()
392 $token = $this->trimmed('token');
393 if (!$token || $token != common_session_token()) {
394 $this->showForm(_('There was a problem with your session token. '.
395 'Try again, please.'));
399 if ($this->validateFeed()) {
400 if ($this->arg('submit')) {
409 * Show the appropriate form based on our input state.
411 function showForm($err=null)
416 if ($this->boolean('ajax')) {
417 header('Content-Type: text/xml;charset=utf-8');
418 $this->xw->startDocument('1.0', 'UTF-8');
419 $this->elementStart('html');
420 $this->elementStart('head');
421 $this->element('title', null, _m('Subscribe to user'));
422 $this->elementEnd('head');
423 $this->elementStart('body');
424 $this->showContent();
425 $this->elementEnd('body');
426 $this->elementEnd('html');
435 * @return string Title of the page
440 return _m('Authorize subscription');
444 * Instructions for use
446 * @return instructions for use
449 function getInstructions()
451 return _m('You can subscribe to users from other supported sites. Paste their address or profile URI below:');
454 function showPageNotice()
456 if (!empty($this->error)) {
457 $this->element('p', 'error', $this->error);
462 * Content area of the page
464 * Shows a form for associating a remote OStatus account with this
470 function showContent()
472 if ($this->oprofile) {
473 $this->showPreviewForm();
475 $this->showInputForm();
479 function showScripts()
481 parent::showScripts();
482 $this->autofocus('feedurl');