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') && !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 * preview() - display profile for a remote user
36 * success() - redirects to subscriptions page on subscribe
38 class OStatusSubAction extends Action
40 protected $profile_uri; // provided acct: or URI of remote entity
41 protected $oprofile; // Ostatus_profile of remote entity, if valid
44 * Show the initial form, when we haven't yet been given a valid
47 function showInputForm()
49 $user = common_current_user();
51 $profile = $user->getProfile();
53 $this->elementStart('form', array('method' => 'post',
54 'id' => 'form_ostatus_sub',
55 'class' => 'form_settings',
56 'action' => $this->selfLink()));
58 $this->hidden('token', common_session_token());
60 $this->elementStart('fieldset', array('id' => 'settings_feeds'));
62 $this->elementStart('ul', 'form_data');
63 $this->elementStart('li');
64 $this->input('profile',
67 _m("OStatus user's address, like nickname@example.com or http://example.net/nickname"));
68 $this->elementEnd('li');
69 $this->elementEnd('ul');
71 $this->submit('validate', _m('Continue'));
73 $this->elementEnd('fieldset');
75 $this->elementEnd('form');
79 * Show the preview-and-confirm form. We've got a valid remote
80 * profile and are ready to poke it!
82 * This controls the wrapper form; actual profile display will
83 * be in previewUser() or previewGroup() depending on the type.
85 function showPreviewForm()
87 $ok = $this->preview();
89 // @fixme maybe provide a cancel button or link back?
93 $this->elementStart('div', 'entity_actions');
94 $this->elementStart('ul');
95 $this->elementStart('li', 'entity_subscribe');
96 $this->elementStart('form', array('method' => 'post',
97 'id' => 'form_ostatus_sub',
98 'class' => 'form_remote_authorize',
101 $this->elementStart('fieldset');
102 $this->hidden('token', common_session_token());
103 $this->hidden('profile', $this->profile_uri);
104 if ($this->oprofile->isGroup()) {
105 $this->submit('submit', _m('Join'), 'submit', null,
106 _m('Join this group'));
108 $this->submit('submit', _m('Confirm'), 'submit', null,
109 _m('Subscribe to this user'));
111 $this->elementEnd('fieldset');
112 $this->elementEnd('form');
113 $this->elementEnd('li');
114 $this->elementEnd('ul');
115 $this->elementEnd('div');
119 * Show a preview for a remote user's profile
120 * @return boolean true if we're ok to try subscribing
124 $oprofile = $this->oprofile;
125 $profile = $oprofile->localProfile();
127 $cur = common_current_user();
128 if ($cur->isSubscribed($profile)) {
129 $this->element('div', array('class' => 'error'),
130 _m("You are already subscribed to this user."));
136 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
137 $avatarUrl = $avatar ? $avatar->displayUrl() : false;
139 $this->showEntity($profile,
140 $profile->profileurl,
146 function showEntity($entity, $profile, $avatar, $note)
148 $nickname = $entity->nickname;
149 $fullname = $entity->fullname;
150 $homepage = $entity->homepage;
151 $location = $entity->location;
154 $avatar = Avatar::defaultImage(AVATAR_PROFILE_SIZE);
157 $this->elementStart('div', 'entity_profile vcard');
158 $this->elementStart('dl', 'entity_depiction');
159 $this->element('dt', null, _('Photo'));
160 $this->elementStart('dd');
161 $this->element('img', array('src' => $avatar,
162 'class' => 'photo avatar',
163 'width' => AVATAR_PROFILE_SIZE,
164 'height' => AVATAR_PROFILE_SIZE,
165 'alt' => $nickname));
166 $this->elementEnd('dd');
167 $this->elementEnd('dl');
169 $this->elementStart('dl', 'entity_nickname');
170 $this->element('dt', null, _('Nickname'));
171 $this->elementStart('dd');
172 $hasFN = ($fullname !== '') ? 'nickname' : 'fn nickname';
173 $this->elementStart('a', array('href' => $profile,
174 'class' => 'url '.$hasFN));
175 $this->raw($nickname);
176 $this->elementEnd('a');
177 $this->elementEnd('dd');
178 $this->elementEnd('dl');
180 if (!is_null($fullname)) {
181 $this->elementStart('dl', 'entity_fn');
182 $this->elementStart('dd');
183 $this->elementStart('span', 'fn');
184 $this->raw($fullname);
185 $this->elementEnd('span');
186 $this->elementEnd('dd');
187 $this->elementEnd('dl');
189 if (!is_null($location)) {
190 $this->elementStart('dl', 'entity_location');
191 $this->element('dt', null, _('Location'));
192 $this->elementStart('dd', 'label');
193 $this->raw($location);
194 $this->elementEnd('dd');
195 $this->elementEnd('dl');
198 if (!is_null($homepage)) {
199 $this->elementStart('dl', 'entity_url');
200 $this->element('dt', null, _('URL'));
201 $this->elementStart('dd');
202 $this->elementStart('a', array('href' => $homepage,
204 $this->raw($homepage);
205 $this->elementEnd('a');
206 $this->elementEnd('dd');
207 $this->elementEnd('dl');
210 if (!is_null($note)) {
211 $this->elementStart('dl', 'entity_note');
212 $this->element('dt', null, _('Note'));
213 $this->elementStart('dd', 'note');
215 $this->elementEnd('dd');
216 $this->elementEnd('dl');
218 $this->elementEnd('div');
222 * Redirect on successful remote user subscription
226 $cur = common_current_user();
227 $url = common_local_url('subscriptions', array('nickname' => $cur->nickname));
228 common_redirect($url, 303);
232 * Pull data for a remote profile and check if it's valid.
233 * Fills out error UI string in $this->error
234 * Fills out $this->oprofile on success.
238 function pullRemoteProfile()
240 $this->profile_uri = $this->trimmed('profile');
242 if (Validate::email($this->profile_uri)) {
243 $this->oprofile = Ostatus_profile::ensureWebfinger($this->profile_uri);
244 } else if (Validate::uri($this->profile_uri)) {
245 $this->oprofile = Ostatus_profile::ensureProfileURL($this->profile_uri);
247 $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");
248 common_debug('Invalid address format.', __FILE__);
252 } catch (FeedSubBadURLException $e) {
253 $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");
254 common_debug('Invalid URL or could not reach server.', __FILE__);
255 } catch (FeedSubBadResponseException $e) {
256 $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
257 common_debug('Cannot read feed; server returned error.', __FILE__);
258 } catch (FeedSubEmptyException $e) {
259 $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
260 common_debug('Cannot read feed; server returned an empty page.', __FILE__);
261 } catch (FeedSubBadHTMLException $e) {
262 $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
263 common_debug('Bad HTML, could not find feed link.', __FILE__);
264 } catch (FeedSubNoFeedException $e) {
265 $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
266 common_debug('Could not find a feed linked from this URL.', __FILE__);
267 } catch (FeedSubUnrecognizedTypeException $e) {
268 $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
269 common_debug('Not a recognized feed type.', __FILE__);
270 } catch (Exception $e) {
271 // Any new ones we forgot about
272 $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");
273 common_debug(sprintf('Bad feed URL: %s %s', get_class($e), $e->getMessage()), __FILE__);
279 function validateRemoteProfile()
281 if ($this->oprofile->isGroup()) {
282 // Send us to the group subscription form for conf
283 $target = common_local_url('ostatusgroup', array(), array('profile' => $this->profile_uri));
284 common_redirect($target, 303);
289 * Attempt to finalize subscription.
290 * validateFeed must have been run first.
292 * Calls showForm on failure or success on success.
296 // And subscribe the current user to the local profile
297 $user = common_current_user();
298 $local = $this->oprofile->localProfile();
299 if ($user->isSubscribed($local)) {
300 // TRANS: OStatus remote subscription dialog error.
301 $this->showForm(_m('Already subscribed!'));
302 } elseif ($this->oprofile->subscribeLocalToRemote($user)) {
305 // TRANS: OStatus remote subscription dialog error.
306 $this->showForm(_m('Remote subscription failed!'));
310 function prepare($args)
312 parent::prepare($args);
314 if (!common_logged_in()) {
315 // XXX: selfURL() didn't work. :<
316 common_set_returnto($_SERVER['REQUEST_URI']);
317 if (Event::handle('RedirectToLogin', array($this, null))) {
318 common_redirect(common_local_url('login'), 303);
323 if ($this->pullRemoteProfile()) {
324 $this->validateRemoteProfile();
330 * Handle the submission.
332 function handle($args)
334 parent::handle($args);
335 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
343 * Handle posts to this form
348 function handlePost()
351 $token = $this->trimmed('token');
352 if (!$token || $token != common_session_token()) {
353 $this->showForm(_('There was a problem with your session token. '.
354 'Try again, please.'));
358 if ($this->oprofile) {
359 if ($this->arg('submit')) {
368 * Show the appropriate form based on our input state.
370 function showForm($err=null)
375 if ($this->boolean('ajax')) {
376 header('Content-Type: text/xml;charset=utf-8');
377 $this->xw->startDocument('1.0', 'UTF-8');
378 $this->elementStart('html');
379 $this->elementStart('head');
380 $this->element('title', null, _m('Subscribe to user'));
381 $this->elementEnd('head');
382 $this->elementStart('body');
383 $this->showContent();
384 $this->elementEnd('body');
385 $this->elementEnd('html');
394 * @return string Title of the page
399 // TRANS: Page title for OStatus remote subscription form
400 return _m('Confirm');
404 * Instructions for use
406 * @return instructions for use
409 function getInstructions()
411 return _m('You can subscribe to users from other supported sites. Paste their address or profile URI below:');
414 function showPageNotice()
416 if (!empty($this->error)) {
417 $this->element('p', 'error', $this->error);
422 * Content area of the page
424 * Shows a form for associating a remote OStatus account with this
430 function showContent()
432 if ($this->oprofile) {
433 $this->showPreviewForm();
435 $this->showInputForm();
439 function showScripts()
441 parent::showScripts();
442 $this->autofocus('feedurl');
447 return common_local_url('ostatussub');