3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 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 James Walker <james@status.net>
25 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
28 class OStatusInitAction extends Action
35 function prepare($args)
37 parent::prepare($args);
39 if (common_logged_in()) {
40 $this->clientError(_m('You can use the local subscription!'));
44 // Local user the remote wants to subscribe to
45 $this->nickname = $this->trimmed('nickname');
47 // Webfinger or profile URL of the remote user
48 $this->profile = $this->trimmed('profile');
53 function handle($args)
55 parent::handle($args);
57 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
58 /* Use a session token for CSRF protection. */
59 $token = $this->trimmed('token');
60 if (!$token || $token != common_session_token()) {
61 $this->showForm(_m('There was a problem with your session token. '.
62 'Try again, please.'));
65 $this->ostatusConnect();
71 function showForm($err = null)
74 if ($this->boolean('ajax')) {
75 header('Content-Type: text/xml;charset=utf-8');
76 $this->xw->startDocument('1.0', 'UTF-8');
77 $this->elementStart('html');
78 $this->elementStart('head');
79 $this->element('title', null, _m('Subscribe to user'));
80 $this->elementEnd('head');
81 $this->elementStart('body');
83 $this->elementEnd('body');
84 $this->elementEnd('html');
90 function showContent()
92 $this->elementStart('form', array('id' => 'form_ostatus_connect',
94 'class' => 'form_settings',
95 'action' => common_local_url('ostatusinit')));
96 $this->elementStart('fieldset');
97 $this->element('legend', null, sprintf(_m('Subscribe to %s'), $this->nickname));
98 $this->hidden('token', common_session_token());
100 $this->elementStart('ul', 'form_data');
101 $this->elementStart('li', array('id' => 'ostatus_nickname'));
102 $this->input('nickname', _m('User nickname'), $this->nickname,
103 _m('Nickname of the user you want to follow'));
104 $this->elementEnd('li');
105 $this->elementStart('li', array('id' => 'ostatus_profile'));
106 $this->input('profile', _m('Profile Account'), $this->profile,
107 _m('Your account id (i.e. user@identi.ca)'));
108 $this->elementEnd('li');
109 $this->elementEnd('ul');
110 $this->submit('submit', _m('Subscribe'));
111 $this->elementEnd('fieldset');
112 $this->elementEnd('form');
115 function ostatusConnect()
117 $opts = array('allowed_schemes' => array('http', 'https', 'acct'));
118 if (Validate::uri($this->profile, $opts)) {
119 $bits = parse_url($this->profile);
120 if ($bits['scheme'] == 'acct') {
121 $this->connectWebfinger($bits['path']);
123 $this->connectProfile($this->profile);
125 } elseif (strpos($this->profile, '@') !== false) {
126 $this->connectWebfinger($this->profile);
128 $this->clientError(_m("Must provide a remote profile."));
132 function connectWebfinger($acct)
136 $result = $w->lookup($acct);
138 $this->clientError(_m("Couldn't look up OStatus account profile."));
140 foreach ($result->links as $link) {
141 if ($link['rel'] == 'http://ostatus.org/schema/1.0/subscribe') {
142 // We found a URL - let's redirect!
144 $user = User::staticGet('nickname', $this->nickname);
145 $target_profile = common_local_url('userbyid', array('id' => $user->id));
147 $url = $w->applyTemplate($link['template'], $target_profile);
148 common_log(LOG_INFO, "Sending remote subscriber $acct to $url");
149 common_redirect($url, 303);
153 $this->clientError(_m("Couldn't confirm remote profile address."));
156 function connectProfile($subscriber_profile)
158 $user = User::staticGet('nickname', $this->nickname);
159 $target_profile = common_local_url('userbyid', array('id' => $user->id));
161 // @fixme hack hack! We should look up the remote sub URL from XRDS
162 $suburl = preg_replace('!^(.*)/(.*?)$!', '$1/main/ostatussub', $subscriber_profile);
163 $suburl .= '?profile=' . urlencode($target_profile);
165 common_log(LOG_INFO, "Sending remote subscriber $subscriber_profile to $suburl");
166 common_redirect($suburl, 303);
171 return _m('OStatus Connect');