3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2009-2010, 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')) {
32 * showInputForm() - form asking for a remote profile account or URL
33 * We end up back here on errors
35 * showPreviewForm() - surrounding form for preview-and-confirm
36 * preview() - display profile for a remote user
38 * success() - redirects to subscriptions page on subscribe
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',
58 'action' => $this->selfLink()));
60 $this->hidden('token', common_session_token());
62 $this->elementStart('fieldset', array('id' => 'settings_feeds'));
64 $this->elementStart('ul', 'form_data');
65 $this->elementStart('li');
66 $this->input('profile',
67 // TRANS: Field label for a field that takes an OStatus user address.
70 // TRANS: Tooltip for field label "Subscribe to".
71 _m('OStatus user\'s address, like nickname@example.com or http://example.net/nickname'));
72 $this->elementEnd('li');
73 $this->elementEnd('ul');
74 // TRANS: Button text.
75 $this->submit('validate', _m('BUTTON','Continue'));
77 $this->elementEnd('fieldset');
79 $this->elementEnd('form');
83 * Show the preview-and-confirm form. We've got a valid remote
84 * profile and are ready to poke it!
86 * This controls the wrapper form; actual profile display will
87 * be in previewUser() or previewGroup() depending on the type.
89 function showPreviewForm()
91 $ok = $this->preview();
93 // @fixme maybe provide a cancel button or link back?
97 $this->elementStart('div', 'entity_actions');
98 $this->elementStart('ul');
99 $this->elementStart('li', 'entity_subscribe');
100 $this->elementStart('form', array('method' => 'post',
101 'id' => 'form_ostatus_sub',
102 'class' => 'form_remote_authorize',
105 $this->elementStart('fieldset');
106 $this->hidden('token', common_session_token());
107 $this->hidden('profile', $this->profile_uri);
108 if ($this->oprofile->isGroup()) {
109 $this->submit('submit', _m('Join'), 'submit', null,
110 // TRANS: Button text.
111 // TRANS: Tooltip for button "Join".
112 _m('BUTTON','Join this group'));
114 // TRANS: Button text.
115 $this->submit('submit', _m('BUTTON','Confirm'), 'submit', null,
116 // TRANS: Tooltip for button "Confirm".
117 _m('Subscribe to this user'));
119 $this->elementEnd('fieldset');
120 $this->elementEnd('form');
121 $this->elementEnd('li');
122 $this->elementEnd('ul');
123 $this->elementEnd('div');
127 * Show a preview for a remote user's profile
128 * @return boolean true if we're ok to try subscribing
132 $oprofile = $this->oprofile;
133 $profile = $oprofile->localProfile();
135 $cur = common_current_user();
136 if ($cur->isSubscribed($profile)) {
137 $this->element('div', array('class' => 'error'),
138 _m("You are already subscribed to this user."));
144 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
145 $avatarUrl = $avatar ? $avatar->displayUrl() : false;
147 $this->showEntity($profile,
148 $profile->profileurl,
154 function showEntity($entity, $profile, $avatar, $note)
156 $nickname = $entity->nickname;
157 $fullname = $entity->fullname;
158 $homepage = $entity->homepage;
159 $location = $entity->location;
162 $avatar = Avatar::defaultImage(AVATAR_PROFILE_SIZE);
165 $this->elementStart('div', 'entity_profile vcard');
166 $this->elementStart('dl', 'entity_depiction');
167 $this->element('dt', null, _m('Photo'));
168 $this->elementStart('dd');
169 $this->element('img', array('src' => $avatar,
170 'class' => 'photo avatar',
171 'width' => AVATAR_PROFILE_SIZE,
172 'height' => AVATAR_PROFILE_SIZE,
173 'alt' => $nickname));
174 $this->elementEnd('dd');
175 $this->elementEnd('dl');
177 $this->elementStart('dl', 'entity_nickname');
178 $this->element('dt', null, _m('Nickname'));
179 $this->elementStart('dd');
180 $hasFN = ($fullname !== '') ? 'nickname' : 'fn nickname';
181 $this->elementStart('a', array('href' => $profile,
182 'class' => 'url '.$hasFN));
183 $this->raw($nickname);
184 $this->elementEnd('a');
185 $this->elementEnd('dd');
186 $this->elementEnd('dl');
188 if (!is_null($fullname)) {
189 $this->elementStart('dl', 'entity_fn');
190 $this->elementStart('dd');
191 $this->elementStart('span', 'fn');
192 $this->raw($fullname);
193 $this->elementEnd('span');
194 $this->elementEnd('dd');
195 $this->elementEnd('dl');
197 if (!is_null($location)) {
198 $this->elementStart('dl', 'entity_location');
199 $this->element('dt', null, _m('Location'));
200 $this->elementStart('dd', 'label');
201 $this->raw($location);
202 $this->elementEnd('dd');
203 $this->elementEnd('dl');
206 if (!is_null($homepage)) {
207 $this->elementStart('dl', 'entity_url');
208 $this->element('dt', null, _m('URL'));
209 $this->elementStart('dd');
210 $this->elementStart('a', array('href' => $homepage,
212 $this->raw($homepage);
213 $this->elementEnd('a');
214 $this->elementEnd('dd');
215 $this->elementEnd('dl');
218 if (!is_null($note)) {
219 $this->elementStart('dl', 'entity_note');
220 $this->element('dt', null, _m('Note'));
221 $this->elementStart('dd', 'note');
223 $this->elementEnd('dd');
224 $this->elementEnd('dl');
226 $this->elementEnd('div');
230 * Redirect on successful remote user subscription
234 $cur = common_current_user();
235 $url = common_local_url('subscriptions', array('nickname' => $cur->nickname));
236 common_redirect($url, 303);
240 * Pull data for a remote profile and check if it's valid.
241 * Fills out error UI string in $this->error
242 * Fills out $this->oprofile on success.
246 function pullRemoteProfile()
248 $this->profile_uri = $this->trimmed('profile');
250 if (Validate::email($this->profile_uri)) {
251 $this->oprofile = Ostatus_profile::ensureWebfinger($this->profile_uri);
252 } else if (Validate::uri($this->profile_uri)) {
253 $this->oprofile = Ostatus_profile::ensureProfileURL($this->profile_uri);
255 // TRANS: Error text.
256 $this->error = _m("Sorry, we could not reach that address. Please make sure that the OStatus address is like nickname@example.com or http://example.net/nickname.");
257 common_debug('Invalid address format.', __FILE__);
261 } catch (FeedSubBadURLException $e) {
262 // TRANS: Error text.
263 $this->error = _m("Sorry, we could not reach that address. Please make sure that the OStatus address is like nickname@example.com or http://example.net/nickname.");
264 common_debug('Invalid URL or could not reach server.', __FILE__);
265 } catch (FeedSubBadResponseException $e) {
266 // TRANS: Error text.
267 $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
268 common_debug('Cannot read feed; server returned error.', __FILE__);
269 } catch (FeedSubEmptyException $e) {
270 // TRANS: Error text.
271 $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
272 common_debug('Cannot read feed; server returned an empty page.', __FILE__);
273 } catch (FeedSubBadHTMLException $e) {
274 // TRANS: Error text.
275 $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
276 common_debug('Bad HTML, could not find feed link.', __FILE__);
277 } catch (FeedSubNoFeedException $e) {
278 // TRANS: Error text.
279 $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
280 common_debug('Could not find a feed linked from this URL.', __FILE__);
281 } catch (FeedSubUnrecognizedTypeException $e) {
282 // TRANS: Error text.
283 $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
284 common_debug('Not a recognized feed type.', __FILE__);
285 } catch (Exception $e) {
286 // Any new ones we forgot about
287 // TRANS: Error text.
288 $this->error = _m("Sorry, we could not reach that address. Please make sure that the OStatus address is like nickname@example.com or http://example.net/nickname.");
289 common_debug(sprintf('Bad feed URL: %s %s', get_class($e), $e->getMessage()), __FILE__);
295 function validateRemoteProfile()
297 if ($this->oprofile->isGroup()) {
298 // Send us to the group subscription form for conf
299 $target = common_local_url('ostatusgroup', array(), array('profile' => $this->profile_uri));
300 common_redirect($target, 303);
305 * Attempt to finalize subscription.
306 * validateFeed must have been run first.
308 * Calls showForm on failure or success on success.
312 // And subscribe the current user to the local profile
313 $user = common_current_user();
314 $local = $this->oprofile->localProfile();
315 if ($user->isSubscribed($local)) {
316 // TRANS: OStatus remote subscription dialog error.
317 $this->showForm(_m('Already subscribed!'));
318 } elseif (Subscription::start($user, $local)) {
321 // TRANS: OStatus remote subscription dialog error.
322 $this->showForm(_m('Remote subscription failed!'));
326 function prepare($args)
328 parent::prepare($args);
330 if (!common_logged_in()) {
331 // XXX: selfURL() didn't work. :<
332 common_set_returnto($_SERVER['REQUEST_URI']);
333 if (Event::handle('RedirectToLogin', array($this, null))) {
334 common_redirect(common_local_url('login'), 303);
339 if ($this->pullRemoteProfile()) {
340 $this->validateRemoteProfile();
346 * Handle the submission.
348 function handle($args)
350 parent::handle($args);
351 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
359 * Handle posts to this form
364 function handlePost()
367 $token = $this->trimmed('token');
368 if (!$token || $token != common_session_token()) {
369 $this->showForm(_m('There was a problem with your session token. '.
370 'Try again, please.'));
374 if ($this->oprofile) {
375 if ($this->arg('submit')) {
384 * Show the appropriate form based on our input state.
386 function showForm($err=null)
391 if ($this->boolean('ajax')) {
392 header('Content-Type: text/xml;charset=utf-8');
393 $this->xw->startDocument('1.0', 'UTF-8');
394 $this->elementStart('html');
395 $this->elementStart('head');
396 // TRANS: Form title.
397 $this->element('title', null, _m('Subscribe to user'));
398 $this->elementEnd('head');
399 $this->elementStart('body');
400 $this->showContent();
401 $this->elementEnd('body');
402 $this->elementEnd('html');
411 * @return string Title of the page
416 // TRANS: Page title for OStatus remote subscription form
417 return _m('Confirm');
421 * Instructions for use
423 * @return instructions for use
426 function getInstructions()
428 // TRANS: Instructions.
429 return _m('You can subscribe to users from other supported sites. Paste their address or profile URI below:');
432 function showPageNotice()
434 if (!empty($this->error)) {
435 $this->element('p', 'error', $this->error);
440 * Content area of the page
442 * Shows a form for associating a remote OStatus account with this
447 function showContent()
449 if ($this->oprofile) {
450 $this->showPreviewForm();
452 $this->showInputForm();
456 function showScripts()
458 parent::showScripts();
459 $this->autofocus('feedurl');
464 return common_local_url('ostatussub');
468 * Disable the send-notice form at the top of the page.
469 * This is really just a hack for the broken CSS in the Cloudy theme,
470 * I think; copying from other non-notice-navigation pages that do this
471 * as well. There will be plenty of others also broken.
473 * @fixme fix the cloudy theme
474 * @fixme do this in a more general way
476 function showNoticeForm() {