]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/ostatusinit.php
8dc68e4b0185bd0eaaa5f5cadfad38d3e27e5cde
[quix0rs-gnu-social.git] / plugins / OStatus / actions / ostatusinit.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 /**
21  * @package OStatusPlugin
22  * @maintainer James Walker <james@status.net>
23  */
24
25 if (!defined('STATUSNET')) {
26     exit(1);
27 }
28
29 class OStatusInitAction extends Action
30 {
31     var $nickname;
32     var $tagger;
33     var $peopletag;
34     var $group;
35     var $profile;
36     var $err;
37
38     function prepare($args)
39     {
40         parent::prepare($args);
41
42         if (common_logged_in()) {
43             // TRANS: Client error.
44             $this->clientError(_m('You can use the local subscription!'));
45             return false;
46         }
47
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');
53
54         // Webfinger or profile URL of the remote user
55         $this->profile = $this->trimmed('profile');
56
57         return true;
58     }
59
60     function handle($args)
61     {
62         parent::handle($args);
63
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.'));
71                 return;
72             }
73             $this->ostatusConnect();
74         } else {
75             $this->showForm();
76         }
77     }
78
79     function showForm($err = null)
80     {
81         $this->err = $err;
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');
87             // TRANS: Form title.
88             $this->element('title', null, _m('TITLE','Subscribe to user'));
89             $this->elementEnd('head');
90             $this->elementStart('body');
91             $this->showContent();
92             $this->elementEnd('body');
93             $this->elementEnd('html');
94         } else {
95             $this->showPage();
96         }
97     }
98
99     function showContent()
100     {
101         if ($this->group) {
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 people tag, %2$s is a tagger's name.
108             $header = sprintf(_m('Subscribe to people tagged %1$s by %2$s'), $this->peopletag, $this->tagger);
109             // TRANS: Button text to subscribe to a people tag.
110             $submit = _m('BUTTON','Subscribe');
111             // TRANS: Button text.
112         } else {
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');
117         }
118         $this->elementStart('form', array('id' => 'form_ostatus_connect',
119                                           'method' => 'post',
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());
125
126         $this->elementStart('ul', 'form_data');
127         $this->elementStart('li', array('id' => 'ostatus_nickname'));
128
129         if ($this->group) {
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.'));
134         } else {
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);
141         }
142
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');
154     }
155
156     function ostatusConnect()
157     {
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']);
163             } else {
164                 $this->connectProfile($this->profile);
165             }
166         } elseif (strpos($this->profile, '@') !== false) {
167             $this->connectWebfinger($this->profile);
168         } else {
169             // TRANS: Client error.
170             $this->clientError(_m("Must provide a remote profile."));
171         }
172     }
173
174     function connectWebfinger($acct)
175     {
176         $target_profile = $this->targetProfile();
177
178         $disco = new Discovery;
179         $result = $disco->lookup($acct);
180         if (!$result) {
181             // TRANS: Client error.
182             $this->clientError(_m('Could not look up OStatus account profile.'));
183         }
184
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);
191             }
192
193         }
194         // TRANS: Client error.
195         $this->clientError(_m('Could not confirm remote profile address.'));
196     }
197
198     function connectProfile($subscriber_profile)
199     {
200         $target_profile = $this->targetProfile();
201
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);
205
206         common_log(LOG_INFO, "Sending remote subscriber $subscriber_profile to $suburl");
207         common_redirect($suburl, 303);
208     }
209
210     /**
211      * Build the canonical profile URI+URL of the requested user or group
212      */
213     function targetProfile()
214     {
215         if ($this->nickname) {
216             $user = User::staticGet('nickname', $this->nickname);
217             if ($user) {
218                 return common_local_url('userbyid', array('id' => $user->id));
219             } else {
220                 // TRANS: Client error.
221                 $this->clientError("No such user.");
222             }
223         } else if ($this->group) {
224             $group = Local_group::staticGet('nickname', $this->group);
225             if ($group) {
226                 return common_local_url('groupbyid', array('id' => $group->group_id));
227             } else {
228                 // TRANS: Client error.
229                 $this->clientError("No such group.");
230             }
231         } else if ($this->peopletag && $this->tagger) {
232             $user = User::staticGet('nickname', $this->tagger);
233             if (empty($user)) {
234                 $this->clientError("No such user.");
235             }
236
237             $peopletag = Profile_list::getByTaggerAndTag($user->id, $this->peopletag);
238             if ($peopletag) {
239                 return common_local_url('profiletagbyid',
240                     array('tagger_id' => $user->id, 'id' => $peopletag->id));
241             }
242             $this->clientError("No such people tag.");
243         } else {
244             // TRANS: Client error.
245             $this->clientError("No local user or group nickname provided.");
246         }
247     }
248
249     function title()
250     {
251       // TRANS: Page title.
252       return _m('OStatus Connect');
253     }
254 }