123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- <?php
- if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
- class OStatusSubAction extends Action
- {
- protected $profile_uri;
- protected $oprofile;
- protected function prepare(array $args=array())
- {
- parent::prepare($args);
- if (!common_logged_in()) {
-
- common_set_returnto($_SERVER['REQUEST_URI']);
- if (Event::handle('RedirectToLogin', array($this, null))) {
- common_redirect(common_local_url('login'), 303);
- }
- return false;
- }
- if ($this->pullRemoteProfile()) {
- $this->validateRemoteProfile();
- }
- return true;
- }
-
- protected function handle()
- {
- parent::handle();
- if ($_SERVER['REQUEST_METHOD'] == 'POST') {
- $this->handlePost();
- } else {
- $this->showForm();
- }
- }
-
- function showInputForm()
- {
- $this->elementStart('form', array('method' => 'post',
- 'id' => 'form_ostatus_sub',
- 'class' => 'form_settings',
- 'action' => $this->selfLink()));
- $this->hidden('token', common_session_token());
- $this->elementStart('fieldset', array('id' => 'settings_feeds'));
- $this->elementStart('ul', 'form_data');
- $this->elementStart('li');
- $this->input('profile',
-
- _m('Subscribe to'),
- $this->profile_uri,
-
- _m('OStatus user\'s address, like nickname@example.com or http://example.net/nickname.'));
- $this->elementEnd('li');
- $this->elementEnd('ul');
-
- $this->submit('validate', _m('BUTTON','Continue'));
- $this->elementEnd('fieldset');
- $this->elementEnd('form');
- }
-
- function showPreviewForm()
- {
- $ok = $this->preview();
- if (!$ok) {
-
- return;
- }
- $this->elementStart('div', 'entity_actions');
- $this->elementStart('ul');
- $this->elementStart('li', 'entity_subscribe');
- $this->elementStart('form', array('method' => 'post',
- 'id' => 'form_ostatus_sub',
- 'class' => 'form_remote_authorize',
- 'action' =>
- $this->selfLink()));
- $this->elementStart('fieldset');
- $this->hidden('token', common_session_token());
- $this->hidden('profile', $this->profile_uri);
- if ($this->oprofile->isGroup()) {
-
- $this->submit('submit', _m('Join'), 'submit', null,
-
- _m('BUTTON','Join this group'));
- } else {
-
- $this->submit('submit', _m('BUTTON','Confirm'), 'submit', null,
-
- _m('Subscribe to this user'));
- }
- $this->elementEnd('fieldset');
- $this->elementEnd('form');
- $this->elementEnd('li');
- $this->elementEnd('ul');
- $this->elementEnd('div');
- }
-
- function preview()
- {
-
- $profile = $this->oprofile->localProfile();
- if ($this->scoped->isSubscribed($profile)) {
- $this->element('div', array('class' => 'error'),
-
- _m('You are already subscribed to this user.'));
- $ok = false;
- } else {
- $ok = true;
- }
- $avatarUrl = $profile->avatarUrl(AVATAR_PROFILE_SIZE);
- $this->showEntity($profile,
- $profile->profileurl,
- $avatarUrl,
- $profile->bio);
- return $ok;
- }
- function showEntity($entity, $profile, $avatar, $note)
- {
- $nickname = $entity->nickname;
- $fullname = $entity->fullname;
- $homepage = $entity->homepage;
- $location = $entity->location;
- $this->elementStart('div', 'entity_profile vcard');
- $this->element('img', array('src' => $avatar,
- 'class' => 'photo avatar entity_depiction',
- 'width' => AVATAR_PROFILE_SIZE,
- 'height' => AVATAR_PROFILE_SIZE,
- 'alt' => $nickname));
- $hasFN = ($fullname !== '') ? 'nickname' : 'fn nickname entity_nickname';
- $this->elementStart('a', array('href' => $profile,
- 'class' => 'url '.$hasFN));
- $this->raw($nickname);
- $this->elementEnd('a');
- if (!is_null($fullname)) {
- $this->elementStart('div', 'fn entity_fn');
- $this->raw($fullname);
- $this->elementEnd('div');
- }
- if (!is_null($location)) {
- $this->elementStart('div', 'label entity_location');
- $this->raw($location);
- $this->elementEnd('div');
- }
- if (!is_null($homepage)) {
- $this->elementStart('a', array('href' => $homepage,
- 'class' => 'url entity_url'));
- $this->raw($homepage);
- $this->elementEnd('a');
- }
- if (!is_null($note)) {
- $this->elementStart('div', 'note entity_note');
- $this->raw($note);
- $this->elementEnd('div');
- }
- $this->elementEnd('div');
- }
-
- function success()
- {
- $url = common_local_url('subscriptions', array('nickname' => $this->scoped->nickname));
- common_redirect($url, 303);
- }
-
- function pullRemoteProfile()
- {
- $this->profile_uri = $this->trimmed('profile');
- try {
- if (Validate::email($this->profile_uri)) {
- $this->oprofile = Ostatus_profile::ensureWebfinger($this->profile_uri);
- } else if (Validate::uri($this->profile_uri)) {
- $this->oprofile = Ostatus_profile::ensureProfileURL($this->profile_uri);
- } else {
-
-
- $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.");
- common_debug('Invalid address format.', __FILE__);
- return false;
- }
- return true;
- } catch (FeedSubBadURLException $e) {
-
-
- $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.');
- common_debug('Invalid URL or could not reach server.', __FILE__);
- } catch (FeedSubBadResponseException $e) {
-
- $this->error = _m('Sorry, we could not reach that feed. Please try that OStatus address again later.');
- common_debug('Cannot read feed; server returned error.', __FILE__);
- } catch (FeedSubEmptyException $e) {
-
- $this->error = _m('Sorry, we could not reach that feed. Please try that OStatus address again later.');
- common_debug('Cannot read feed; server returned an empty page.', __FILE__);
- } catch (FeedSubBadHTMLException $e) {
-
- $this->error = _m('Sorry, we could not reach that feed. Please try that OStatus address again later.');
- common_debug('Bad HTML, could not find feed link.', __FILE__);
- } catch (FeedSubNoFeedException $e) {
-
- $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
- common_debug('Could not find a feed linked from this URL.', __FILE__);
- } catch (FeedSubUnrecognizedTypeException $e) {
-
- $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
- common_debug('Not a recognized feed type.', __FILE__);
- } catch (Exception $e) {
-
-
-
- $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.");
- common_debug(sprintf('Bad feed URL: %s %s', get_class($e), $e->getMessage()), __FILE__);
- }
- return false;
- }
- function validateRemoteProfile()
- {
-
- if ($this->oprofile->isGroup()) {
- $target = common_local_url('ostatusgroup', array(), array('profile' => $this->profile_uri));
- common_redirect($target, 303);
- } else if ($this->oprofile->isPeopletag()) {
- $target = common_local_url('ostatuspeopletag', array(), array('profile' => $this->profile_uri));
- common_redirect($target, 303);
- }
- }
-
- function saveFeed()
- {
-
- $local = $this->oprofile->localProfile();
- if ($this->scoped->isSubscribed($local)) {
-
- $this->showForm(_m('Already subscribed!'));
- } elseif (Subscription::start($this->scoped, $local)) {
- $this->success();
- } else {
-
- $this->showForm(_m('Remote subscription failed!'));
- }
- }
-
- function handlePost()
- {
-
- $token = $this->trimmed('token');
- if (!$token || $token != common_session_token()) {
-
- $this->showForm(_m('There was a problem with your session token. '.
- 'Try again, please.'));
- return;
- }
- if ($this->oprofile) {
- if ($this->arg('submit')) {
- $this->saveFeed();
- return;
- }
- }
- $this->showForm();
- }
-
- function showForm($err=null)
- {
- if ($err) {
- $this->error = $err;
- }
- if ($this->boolean('ajax')) {
- $this->startHTML('text/xml;charset=utf-8');
- $this->elementStart('head');
-
- $this->element('title', null, _m('Subscribe to user'));
- $this->elementEnd('head');
- $this->elementStart('body');
- $this->showContent();
- $this->elementEnd('body');
- $this->endHTML();
- } else {
- $this->showPage();
- }
- }
-
- function title()
- {
-
- return _m('Confirm');
- }
-
- function getInstructions()
- {
-
- return _m('You can subscribe to users from other supported sites. Paste their address or profile URI below:');
- }
- function showPageNotice()
- {
- if (!empty($this->error)) {
- $this->element('p', 'error', $this->error);
- }
- }
-
- function showContent()
- {
- if ($this->oprofile) {
- $this->showPreviewForm();
- } else {
- $this->showInputForm();
- }
- }
- function showScripts()
- {
- parent::showScripts();
- $this->autofocus('feedurl');
- }
- function selfLink()
- {
- return common_local_url('ostatussub');
- }
-
- function showNoticeForm() {
-
- }
- }
|