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')) {
29 class OStatusInitAction extends Action
38 function prepare($args)
40 parent::prepare($args);
42 if (common_logged_in()) {
43 // TRANS: Client error.
44 $this->clientError(_m('You can use the local subscription!'));
48 // Local user or group the remote wants to subscribe to
49 $this->nickname = $this->trimmed('nickname');
50 $this->tagger = $this->trimmed('tagger');
51 $this->peopletag = $this->trimmed('peopletag');
52 $this->group = $this->trimmed('group');
54 // Webfinger or profile URL of the remote user
55 $this->profile = $this->trimmed('profile');
60 function handle($args)
62 parent::handle($args);
64 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
65 /* Use a session token for CSRF protection. */
66 $token = $this->trimmed('token');
67 if (!$token || $token != common_session_token()) {
68 // TRANS: Client error displayed when the session token does not match or is not given.
69 $this->showForm(_m('There was a problem with your session token. '.
70 'Try again, please.'));
73 $this->ostatusConnect();
79 function showForm($err = null)
82 if ($this->boolean('ajax')) {
83 $this->startHTML('text/xml;charset=utf-8');
84 $this->elementStart('head');
86 $this->element('title', null, _m('TITLE','Subscribe to user'));
87 $this->elementEnd('head');
88 $this->elementStart('body');
90 $this->elementEnd('body');
97 function showContent()
101 // TRANS: Form legend. %s is a group name.
102 $header = sprintf(_m('Join group %s'), $this->group);
103 // TRANS: Button text to join a group.
104 $submit = _m('BUTTON','Join');
105 } else if ($this->peopletag && $this->tagger) {
106 // TRANS: Form legend. %1$s is a list, %2$s is a lister's name.
107 $header = sprintf(_m('Subscribe to list %1$s by %2$s'), $this->peopletag, $this->tagger);
108 // TRANS: Button text to subscribe to a list.
109 $submit = _m('BUTTON','Subscribe');
110 // TRANS: Button text.
112 // TRANS: Form legend. %s is a nickname.
113 $header = sprintf(_m('Subscribe to %s'), $this->nickname);
114 // TRANS: Button text to subscribe to a profile.
115 $submit = _m('BUTTON','Subscribe');
117 $this->elementStart('form', array('id' => 'form_ostatus_connect',
119 'class' => 'form_settings',
120 'action' => common_local_url('ostatusinit')));
121 $this->elementStart('fieldset');
122 $this->element('legend', null, $header);
123 $this->hidden('token', common_session_token());
125 $this->elementStart('ul', 'form_data');
126 $this->elementStart('li', array('id' => 'ostatus_nickname'));
129 // TRANS: Field label.
130 $this->input('group', _m('Group nickname'), $this->group,
131 // TRANS: Field title.
132 _m('Nickname of the group you want to join.'));
134 // TRANS: Field label.
135 $this->input('nickname', _m('User nickname'), $this->nickname,
136 // TRANS: Field title.
137 _m('Nickname of the user you want to follow.'));
138 $this->hidden('tagger', $this->tagger);
139 $this->hidden('peopletag', $this->peopletag);
142 $this->elementEnd('li');
143 $this->elementStart('li', array('id' => 'ostatus_profile'));
144 // TRANS: Field label.
145 $this->input('profile', _m('Profile Account'), $this->profile,
146 // TRANS: Tooltip for field label "Profile Account".
147 _m('Your account ID (e.g. user@identi.ca).'));
148 $this->elementEnd('li');
149 $this->elementEnd('ul');
150 $this->submit('submit', $submit);
151 $this->elementEnd('fieldset');
152 $this->elementEnd('form');
155 function ostatusConnect()
157 $opts = array('allowed_schemes' => array('http', 'https', 'acct'));
158 if (Validate::uri($this->profile, $opts)) {
159 $bits = parse_url($this->profile);
160 if ($bits['scheme'] == 'acct') {
161 $this->connectWebfinger($bits['path']);
163 $this->connectProfile($this->profile);
165 } elseif (strpos($this->profile, '@') !== false) {
166 $this->connectWebfinger($this->profile);
168 // TRANS: Client error.
169 $this->clientError(_m('Must provide a remote profile.'));
173 function connectWebfinger($acct)
175 $target_profile = $this->targetProfile();
177 $disco = new Discovery;
178 $xrd = $disco->lookup($acct);
180 $link = $xrd->get('http://ostatus.org/schema/1.0/subscribe');
181 if (!is_null($link)) {
182 // We found a URL - let's redirect!
183 if (!empty($link->template)) {
184 $url = Discovery::applyTemplate($link->template, $target_profile);
188 common_log(LOG_INFO, "Sending remote subscriber $acct to $url");
189 common_redirect($url, 303);
191 // TRANS: Client error.
192 $this->clientError(_m('Could not confirm remote profile address.'));
195 function connectProfile($subscriber_profile)
197 $target_profile = $this->targetProfile();
199 // @fixme hack hack! We should look up the remote sub URL from XRDS
200 $suburl = preg_replace('!^(.*)/(.*?)$!', '$1/main/ostatussub', $subscriber_profile);
201 $suburl .= '?profile=' . urlencode($target_profile);
203 common_log(LOG_INFO, "Sending remote subscriber $subscriber_profile to $suburl");
204 common_redirect($suburl, 303);
208 * Build the canonical profile URI+URL of the requested user or group
210 function targetProfile()
212 if ($this->nickname) {
213 $user = User::getKV('nickname', $this->nickname);
215 return common_local_url('userbyid', array('id' => $user->id));
217 // TRANS: Client error.
218 $this->clientError(_m('No such user.'));
220 } else if ($this->group) {
221 $group = Local_group::getKV('nickname', $this->group);
223 return common_local_url('groupbyid', array('id' => $group->group_id));
225 // TRANS: Client error.
226 $this->clientError(_m('No such group.'));
228 } else if ($this->peopletag && $this->tagger) {
229 $user = User::getKV('nickname', $this->tagger);
231 // TRANS: Client error.
232 $this->clientError(_m('No such user.'));
235 $peopletag = Profile_list::getByTaggerAndTag($user->id, $this->peopletag);
237 return common_local_url('profiletagbyid',
238 array('tagger_id' => $user->id, 'id' => $peopletag->id));
240 // TRANS: Client error.
241 $this->clientError(_m('No such list.'));
243 // TRANS: Client error.
244 $this->clientError(_m('No local user or group nickname provided.'));
250 // TRANS: Page title.
251 return _m('OStatus Connect');