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
36 function prepare($args)
38 parent::prepare($args);
40 if (common_logged_in()) {
41 // TRANS: Client error.
42 $this->clientError(_m('You can use the local subscription!'));
46 // Local user or group the remote wants to subscribe to
47 $this->nickname = $this->trimmed('nickname');
48 $this->group = $this->trimmed('group');
50 // Webfinger or profile URL of the remote user
51 $this->profile = $this->trimmed('profile');
56 function handle($args)
58 parent::handle($args);
60 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
61 /* Use a session token for CSRF protection. */
62 $token = $this->trimmed('token');
63 if (!$token || $token != common_session_token()) {
64 $this->showForm(_m('There was a problem with your session token. '.
65 'Try again, please.'));
68 $this->ostatusConnect();
74 function showForm($err = null)
77 if ($this->boolean('ajax')) {
78 header('Content-Type: text/xml;charset=utf-8');
79 $this->xw->startDocument('1.0', 'UTF-8');
80 $this->elementStart('html');
81 $this->elementStart('head');
83 $this->element('title', null, _m('Subscribe to user'));
84 $this->elementEnd('head');
85 $this->elementStart('body');
87 $this->elementEnd('body');
88 $this->elementEnd('html');
94 function showContent()
98 // TRANS: Form legend.
99 $header = sprintf(_m('Join group %s'), $this->group);
100 // TRANS: Button text.
101 $submit = _m('BUTTON','Join');
103 // TRANS: Form legend.
104 $header = sprintf(_m('Subscribe to %s'), $this->nickname);
105 // TRANS: Button text.
106 $submit = _m('BUTTON','Subscribe');
108 $this->elementStart('form', array('id' => 'form_ostatus_connect',
110 'class' => 'form_settings',
111 'action' => common_local_url('ostatusinit')));
112 $this->elementStart('fieldset');
113 $this->element('legend', null, $header);
114 $this->hidden('token', common_session_token());
116 $this->elementStart('ul', 'form_data');
117 $this->elementStart('li', array('id' => 'ostatus_nickname'));
118 // TRANS: Field label.
119 $this->input('nickname', _m('User nickname'), $this->nickname,
120 _m('Nickname of the user you want to follow.'));
121 $this->hidden('group', $this->group); // pass-through for magic links
122 $this->elementEnd('li');
123 $this->elementStart('li', array('id' => 'ostatus_profile'));
124 // TRANS: Field label.
125 $this->input('profile', _m('Profile Account'), $this->profile,
126 _m("Your account id (i.e. user@status.net) -- with GNU social, users do not use one server to communicate in the way that Facebook and Twitter users do. Instead, users are spread out over a network of servers and different sites. You can run your own server, or you can sign up for one of the public servers -- it doesn't even need to be a GNU social server -- any server that speaks the OStatus protocol is suitable. A good place to get an account for yourself is www.status.net"));
127 $this->elementEnd('li');
128 $this->elementEnd('ul');
129 $this->submit('submit', $submit);
130 $this->elementEnd('fieldset');
131 $this->elementEnd('form');
134 function ostatusConnect()
136 $opts = array('allowed_schemes' => array('http', 'https', 'acct'));
137 if (Validate::uri($this->profile, $opts)) {
138 $bits = parse_url($this->profile);
139 if ($bits['scheme'] == 'acct') {
140 $this->connectWebfinger($bits['path']);
142 $this->connectProfile($this->profile);
144 } elseif (strpos($this->profile, '@') !== false) {
145 $this->connectWebfinger($this->profile);
147 // TRANS: Client error.
148 $this->clientError(_m("Must provide a remote profile."));
152 function connectWebfinger($acct)
154 $target_profile = $this->targetProfile();
156 $disco = new Discovery;
157 $result = $disco->lookup($acct);
159 // TRANS: Client error.
160 $this->clientError(_m("Couldn't look up OStatus account profile."));
163 foreach ($result->links as $link) {
164 if ($link['rel'] == 'http://ostatus.org/schema/1.0/subscribe') {
165 // We found a URL - let's redirect!
166 $url = Discovery::applyTemplate($link['template'], $target_profile);
167 common_log(LOG_INFO, "Sending remote subscriber $acct to $url");
168 common_redirect($url, 303);
172 // TRANS: Client error.
173 $this->clientError(_m("Couldn't confirm remote profile address."));
176 function connectProfile($subscriber_profile)
178 $target_profile = $this->targetProfile();
180 // @fixme hack hack! We should look up the remote sub URL from XRDS
181 $suburl = preg_replace('!^(.*)/(.*?)$!', '$1/main/ostatussub', $subscriber_profile);
182 $suburl .= '?profile=' . urlencode($target_profile);
184 common_log(LOG_INFO, "Sending remote subscriber $subscriber_profile to $suburl");
185 common_redirect($suburl, 303);
189 * Build the canonical profile URI+URL of the requested user or group
191 function targetProfile()
193 if ($this->nickname) {
194 $user = User::staticGet('nickname', $this->nickname);
196 return common_local_url('userbyid', array('id' => $user->id));
198 // TRANS: Client error.
199 $this->clientError("No such user.");
201 } else if ($this->group) {
202 $group = Local_group::staticGet('nickname', $this->group);
204 return common_local_url('groupbyid', array('id' => $group->group_id));
206 // TRANS: Client error.
207 $this->clientError("No such group.");
210 // TRANS: Client error.
211 $this->clientError("No local user or group nickname provided.");
217 // TRANS: Page title.
218 return _m('OStatus Connect');