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 header('Content-Type: text/xml;charset=utf-8');
84 $this->xw->startDocument('1.0', 'UTF-8');
85 $this->elementStart('html');
86 $this->elementStart('head');
88 $this->element('title', null, _m('TITLE','Subscribe to user'));
89 $this->elementEnd('head');
90 $this->elementStart('body');
92 $this->elementEnd('body');
93 $this->elementEnd('html');
99 function showContent()
103 // TRANS: Form legend. %s is a group name.
104 $header = sprintf(_m('Join group %s'), $this->group);
105 // TRANS: Button text to join a group.
106 $submit = _m('BUTTON','Join');
107 } else if ($this->peopletag && $this->tagger) {
108 // TRANS: Form legend. %1$s is a list, %2$s is a tagger's name.
109 $header = sprintf(_m('Subscribe to list %1$s by %2$s'), $this->peopletag, $this->tagger);
110 // TRANS: Button text to subscribe to a list.
111 $submit = _m('BUTTON','Subscribe');
112 // TRANS: Button text.
114 // TRANS: Form legend. %s is a nickname.
115 $header = sprintf(_m('Subscribe to %s'), $this->nickname);
116 // TRANS: Button text to subscribe to a profile.
117 $submit = _m('BUTTON','Subscribe');
119 $this->elementStart('form', array('id' => 'form_ostatus_connect',
121 'class' => 'form_settings',
122 'action' => common_local_url('ostatusinit')));
123 $this->elementStart('fieldset');
124 $this->element('legend', null, $header);
125 $this->hidden('token', common_session_token());
127 $this->elementStart('ul', 'form_data');
128 $this->elementStart('li', array('id' => 'ostatus_nickname'));
131 // TRANS: Field label.
132 $this->input('group', _m('Group nickname'), $this->group,
133 // TRANS: Field title.
134 _m('Nickname of the group you want to join.'));
136 // TRANS: Field label.
137 $this->input('nickname', _m('User nickname'), $this->nickname,
138 // TRANS: Field title.
139 _m('Nickname of the user you want to follow.'));
140 $this->hidden('tagger', $this->tagger);
141 $this->hidden('peopletag', $this->peopletag);
144 $this->elementEnd('li');
145 $this->elementStart('li', array('id' => 'ostatus_profile'));
146 // TRANS: Field label.
147 $this->input('profile', _m('Profile Account'), $this->profile,
148 // TRANS: Tooltip for field label "Profile Account".
149 _m('Your account ID (e.g. user@identi.ca).'));
150 $this->elementEnd('li');
151 $this->elementEnd('ul');
152 $this->submit('submit', $submit);
153 $this->elementEnd('fieldset');
154 $this->elementEnd('form');
157 function ostatusConnect()
159 $opts = array('allowed_schemes' => array('http', 'https', 'acct'));
160 if (Validate::uri($this->profile, $opts)) {
161 $bits = parse_url($this->profile);
162 if ($bits['scheme'] == 'acct') {
163 $this->connectWebfinger($bits['path']);
165 $this->connectProfile($this->profile);
167 } elseif (strpos($this->profile, '@') !== false) {
168 $this->connectWebfinger($this->profile);
170 // TRANS: Client error.
171 $this->clientError(_m('Must provide a remote profile.'));
175 function connectWebfinger($acct)
177 $target_profile = $this->targetProfile();
179 $disco = new Discovery;
180 $result = $disco->lookup($acct);
182 // TRANS: Client error.
183 $this->clientError(_m('Could not look up OStatus account profile.'));
186 foreach ($result->links as $link) {
187 if ($link['rel'] == 'http://ostatus.org/schema/1.0/subscribe') {
188 // We found a URL - let's redirect!
189 $url = Discovery::applyTemplate($link['template'], $target_profile);
190 common_log(LOG_INFO, "Sending remote subscriber $acct to $url");
191 common_redirect($url, 303);
195 // TRANS: Client error.
196 $this->clientError(_m('Could not confirm remote profile address.'));
199 function connectProfile($subscriber_profile)
201 $target_profile = $this->targetProfile();
203 // @fixme hack hack! We should look up the remote sub URL from XRDS
204 $suburl = preg_replace('!^(.*)/(.*?)$!', '$1/main/ostatussub', $subscriber_profile);
205 $suburl .= '?profile=' . urlencode($target_profile);
207 common_log(LOG_INFO, "Sending remote subscriber $subscriber_profile to $suburl");
208 common_redirect($suburl, 303);
212 * Build the canonical profile URI+URL of the requested user or group
214 function targetProfile()
216 if ($this->nickname) {
217 $user = User::staticGet('nickname', $this->nickname);
219 return common_local_url('userbyid', array('id' => $user->id));
221 // TRANS: Client error.
222 $this->clientError(_m('No such user.'));
224 } else if ($this->group) {
225 $group = Local_group::staticGet('nickname', $this->group);
227 return common_local_url('groupbyid', array('id' => $group->group_id));
229 // TRANS: Client error.
230 $this->clientError(_m('No such group.'));
232 } else if ($this->peopletag && $this->tagger) {
233 $user = User::staticGet('nickname', $this->tagger);
235 // TRANS: Client error.
236 $this->clientError(_m('No such user.'));
239 $peopletag = Profile_list::getByTaggerAndTag($user->id, $this->peopletag);
241 return common_local_url('profiletagbyid',
242 array('tagger_id' => $user->id, 'id' => $peopletag->id));
244 // TRANS: Client error.
245 $this->clientError(_m('No such list.'));
247 // TRANS: Client error.
248 $this->clientError(_m('No local user or group nickname provided.'));
254 // TRANS: Page title.
255 return _m('OStatus Connect');