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 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
130 $avatarUrl = $avatar ? $avatar->displayUrl() : false;
132 $this->showEntity($profile,
133 $profile->profileurl,
139 * Show a preview for a remote group's profile
141 function previewGroup()
143 $oprofile = $this->oprofile;
144 $group = $oprofile->localGroup();
146 $this->showEntity($group,
147 $group->getProfileUrl(),
148 $group->homepage_logo,
149 $group->description);
153 function showEntity($entity, $profile, $avatar, $note)
155 $nickname = $entity->nickname;
156 $fullname = $entity->fullname;
157 $homepage = $entity->homepage;
158 $location = $entity->location;
161 $avatar = Avatar::defaultImage(AVATAR_PROFILE_SIZE);
164 $this->elementStart('div', 'entity_profile vcard');
165 $this->elementStart('dl', 'entity_depiction');
166 $this->element('dt', null, _('Photo'));
167 $this->elementStart('dd');
168 $this->element('img', array('src' => $avatar,
169 'class' => 'photo avatar',
170 'width' => AVATAR_PROFILE_SIZE,
171 'height' => AVATAR_PROFILE_SIZE,
172 'alt' => $nickname));
173 $this->elementEnd('dd');
174 $this->elementEnd('dl');
176 $this->elementStart('dl', 'entity_nickname');
177 $this->element('dt', null, _('Nickname'));
178 $this->elementStart('dd');
179 $hasFN = ($fullname !== '') ? 'nickname' : 'fn nickname';
180 $this->elementStart('a', array('href' => $profile,
181 'class' => 'url '.$hasFN));
182 $this->raw($nickname);
183 $this->elementEnd('a');
184 $this->elementEnd('dd');
185 $this->elementEnd('dl');
187 if (!is_null($fullname)) {
188 $this->elementStart('dl', 'entity_fn');
189 $this->elementStart('dd');
190 $this->elementStart('span', 'fn');
191 $this->raw($fullname);
192 $this->elementEnd('span');
193 $this->elementEnd('dd');
194 $this->elementEnd('dl');
196 if (!is_null($location)) {
197 $this->elementStart('dl', 'entity_location');
198 $this->element('dt', null, _('Location'));
199 $this->elementStart('dd', 'label');
200 $this->raw($location);
201 $this->elementEnd('dd');
202 $this->elementEnd('dl');
205 if (!is_null($homepage)) {
206 $this->elementStart('dl', 'entity_url');
207 $this->element('dt', null, _('URL'));
208 $this->elementStart('dd');
209 $this->elementStart('a', array('href' => $homepage,
211 $this->raw($homepage);
212 $this->elementEnd('a');
213 $this->elementEnd('dd');
214 $this->elementEnd('dl');
217 if (!is_null($note)) {
218 $this->elementStart('dl', 'entity_note');
219 $this->element('dt', null, _('Note'));
220 $this->elementStart('dd', 'note');
222 $this->elementEnd('dd');
223 $this->elementEnd('dl');
225 $this->elementEnd('div');
229 * Redirect on successful remote user subscription
231 function successUser()
233 $cur = common_current_user();
234 $url = common_local_url('subscriptions', array('nickname' => $cur->nickname));
235 common_redirect($url, 303);
239 * Redirect on successful remote group join
241 function successGroup()
243 $cur = common_current_user();
244 $url = common_local_url('usergroups', array('nickname' => $cur->nickname));
245 common_redirect($url, 303);
249 * Pull data for a remote profile and check if it's valid.
250 * Fills out error UI string in $this->error
251 * Fills out $this->oprofile on success.
255 function validateFeed()
257 $profile_uri = trim($this->arg('profile'));
259 if ($profile_uri == '') {
260 $this->showForm(_m('Empty remote profile URL!'));
263 $this->profile_uri = $profile_uri;
265 // @fixme validate, normalize bla bla
267 $oprofile = Ostatus_profile::ensureProfile($this->profile_uri);
268 $this->oprofile = $oprofile;
270 } catch (FeedSubBadURLException $e) {
271 $this->error = _m('Invalid URL or could not reach server.');
272 } catch (FeedSubBadResponseException $e) {
273 $this->error = _m('Cannot read feed; server returned error.');
274 } catch (FeedSubEmptyException $e) {
275 $this->error = _m('Cannot read feed; server returned an empty page.');
276 } catch (FeedSubBadHTMLException $e) {
277 $this->error = _m('Bad HTML, could not find feed link.');
278 } catch (FeedSubNoFeedException $e) {
279 $this->error = _m('Could not find a feed linked from this URL.');
280 } catch (FeedSubUnrecognizedTypeException $e) {
281 $this->error = _m('Not a recognized feed type.');
282 } catch (FeedSubException $e) {
283 // Any new ones we forgot about
284 $this->error = sprintf(_m('Bad feed URL: %s %s'), get_class($e), $e->getMessage());
291 * Attempt to finalize subscription.
292 * validateFeed must have been run first.
294 * Calls showForm on failure or successUser/successGroup on success.
298 // And subscribe the current user to the local profile
299 $user = common_current_user();
301 if (!$this->oprofile->subscribe()) {
302 $this->showForm(_m("Failed to set up server-to-server subscription."));
306 if ($this->oprofile->isGroup()) {
307 $group = $this->oprofile->localGroup();
308 if ($user->isMember($group)) {
309 $this->showForm(_m('Already a member!'));
310 } elseif (Group_member::join($this->oprofile->group_id, $user->id)) {
311 $this->successGroup();
313 $this->showForm(_m('Remote group join failed!'));
316 $local = $this->oprofile->localProfile();
317 if ($user->isSubscribed($local)) {
318 $this->showForm(_m('Already subscribed!'));
319 } elseif ($this->oprofile->subscribeLocalToRemote($user)) {
320 $this->successUser();
322 $this->showForm(_m('Remote subscription failed!'));
327 function prepare($args)
329 parent::prepare($args);
331 if (!common_logged_in()) {
332 // XXX: selfURL() didn't work. :<
333 common_set_returnto($_SERVER['REQUEST_URI']);
334 if (Event::handle('RedirectToLogin', array($this, null))) {
335 common_redirect(common_local_url('login'), 303);
340 $this->profile_uri = $this->arg('profile');
346 * Handle the submission.
348 function handle($args)
350 parent::handle($args);
351 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
354 if ($this->arg('profile')) {
355 $this->validateFeed();
363 * Handle posts to this form
368 function handlePost()
371 $token = $this->trimmed('token');
372 if (!$token || $token != common_session_token()) {
373 $this->showForm(_('There was a problem with your session token. '.
374 'Try again, please.'));
378 if ($this->validateFeed()) {
379 if ($this->arg('submit')) {
388 * Show the appropriate form based on our input state.
390 function showForm($err=null)
395 if ($this->boolean('ajax')) {
396 header('Content-Type: text/xml;charset=utf-8');
397 $this->xw->startDocument('1.0', 'UTF-8');
398 $this->elementStart('html');
399 $this->elementStart('head');
400 $this->element('title', null, _m('Subscribe to user'));
401 $this->elementEnd('head');
402 $this->elementStart('body');
403 $this->showContent();
404 $this->elementEnd('body');
405 $this->elementEnd('html');
414 * @return string Title of the page
419 return _m('Authorize subscription');
423 * Instructions for use
425 * @return instructions for use
428 function getInstructions()
430 return _m('You can subscribe to users from other supported sites. Paste their address or profile URI below:');
433 function showPageNotice()
435 if (!empty($this->error)) {
436 $this->element('p', 'error', $this->error);
441 * Content area of the page
443 * Shows a form for associating a remote OStatus account with this
449 function showContent()
451 if ($this->oprofile) {
452 $this->showPreviewForm();
454 $this->showInputForm();
458 function showScripts()
460 parent::showScripts();
461 $this->autofocus('feedurl');