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()
102 // TRANS: Form legend. %s is a group name.
103 $header = sprintf(_m('Join group %s'), $this->group);
104 // TRANS: Button text to join a group.
105 $submit = _m('BUTTON','Join');
106 } else if ($this->peopletag && $this->tagger) {
107 // TRANS: Form legend. %1$s is a list, %2$s is a tagger's name.
108 $header = sprintf(_m('Subscribe to list %1$s by %2$s'), $this->peopletag, $this->tagger);
109 // TRANS: Button text to subscribe to a list.
110 $submit = _m('BUTTON','Subscribe');
111 // TRANS: Button text.
113 // TRANS: Form legend. %s is a nickname.
114 $header = sprintf(_m('Subscribe to %s'), $this->nickname);
115 // TRANS: Button text to subscribe to a profile.
116 $submit = _m('BUTTON','Subscribe');
118 $this->elementStart('form', array('id' => 'form_ostatus_connect',
120 'class' => 'form_settings',
121 'action' => common_local_url('ostatusinit')));
122 $this->elementStart('fieldset');
123 $this->element('legend', null, $header);
124 $this->hidden('token', common_session_token());
126 $this->elementStart('ul', 'form_data');
127 $this->elementStart('li', array('id' => 'ostatus_nickname'));
130 // TRANS: Field label.
131 $this->input('group', _m('Group nickname'), $this->group,
132 // TRANS: Field title.
133 _m('Nickname of the group you want to join.'));
135 // TRANS: Field label.
136 $this->input('nickname', _m('User nickname'), $this->nickname,
137 // TRANS: Field title.
138 _m('Nickname of the user you want to follow.'));
139 $this->hidden('tagger', $this->tagger);
140 $this->hidden('peopletag', $this->peopletag);
143 $this->elementEnd('li');
144 $this->elementStart('li', array('id' => 'ostatus_profile'));
145 // TRANS: Field label.
146 $this->input('profile', _m('Profile Account'), $this->profile,
147 // TRANS: Tooltip for field label "Profile Account".
148 _m('Your account ID (e.g. user@identi.ca).'));
149 $this->elementEnd('li');
150 $this->elementEnd('ul');
151 $this->submit('submit', $submit);
152 $this->elementEnd('fieldset');
153 $this->elementEnd('form');
156 function ostatusConnect()
158 $opts = array('allowed_schemes' => array('http', 'https', 'acct'));
159 if (Validate::uri($this->profile, $opts)) {
160 $bits = parse_url($this->profile);
161 if ($bits['scheme'] == 'acct') {
162 $this->connectWebfinger($bits['path']);
164 $this->connectProfile($this->profile);
166 } elseif (strpos($this->profile, '@') !== false) {
167 $this->connectWebfinger($this->profile);
169 // TRANS: Client error.
170 $this->clientError(_m('Must provide a remote profile.'));
174 function connectWebfinger($acct)
176 $target_profile = $this->targetProfile();
178 $disco = new Discovery;
179 $result = $disco->lookup($acct);
181 // TRANS: Client error.
182 $this->clientError(_m('Could not look up OStatus account profile.'));
185 foreach ($result->links as $link) {
186 if ($link['rel'] == 'http://ostatus.org/schema/1.0/subscribe') {
187 // We found a URL - let's redirect!
188 $url = Discovery::applyTemplate($link['template'], $target_profile);
189 common_log(LOG_INFO, "Sending remote subscriber $acct to $url");
190 common_redirect($url, 303);
194 // TRANS: Client error.
195 $this->clientError(_m('Could not confirm remote profile address.'));
198 function connectProfile($subscriber_profile)
200 $target_profile = $this->targetProfile();
202 // @fixme hack hack! We should look up the remote sub URL from XRDS
203 $suburl = preg_replace('!^(.*)/(.*?)$!', '$1/main/ostatussub', $subscriber_profile);
204 $suburl .= '?profile=' . urlencode($target_profile);
206 common_log(LOG_INFO, "Sending remote subscriber $subscriber_profile to $suburl");
207 common_redirect($suburl, 303);
211 * Build the canonical profile URI+URL of the requested user or group
213 function targetProfile()
215 if ($this->nickname) {
216 $user = User::staticGet('nickname', $this->nickname);
218 return common_local_url('userbyid', array('id' => $user->id));
220 // TRANS: Client error.
221 $this->clientError(_m('No such user.'));
223 } else if ($this->group) {
224 $group = Local_group::staticGet('nickname', $this->group);
226 return common_local_url('groupbyid', array('id' => $group->group_id));
228 // TRANS: Client error.
229 $this->clientError(_m('No such group.'));
231 } else if ($this->peopletag && $this->tagger) {
232 $user = User::staticGet('nickname', $this->tagger);
234 $this->clientError(_m('No such user.'));
237 $peopletag = Profile_list::getByTaggerAndTag($user->id, $this->peopletag);
239 return common_local_url('profiletagbyid',
240 array('tagger_id' => $user->id, 'id' => $peopletag->id));
242 $this->clientError(_m('No such list.'));
244 // TRANS: Client error.
245 $this->clientError(_m('No local user or group nickname provided.'));
251 // TRANS: Page title.
252 return _m('OStatus Connect');