]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/ostatusinit.php
Merge commit 'refs/merge-requests/230' of git://gitorious.org/statusnet/mainline...
[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     
102         if ($this->group) {
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 lister'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.
113         } else {
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');
118         }
119         $this->elementStart('form', array('id' => 'form_ostatus_connect',
120                                           'method' => 'post',
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());
126
127         $this->elementStart('ul', 'form_data');
128         $this->elementStart('li', array('id' => 'ostatus_nickname'));
129
130         if ($this->group) {
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.'));
135         } else {
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);
142         }
143
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');
155     }
156
157     function ostatusConnect()
158     {
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']);
164             } else {
165                 $this->connectProfile($this->profile);
166             }
167         } elseif (strpos($this->profile, '@') !== false) {
168             $this->connectWebfinger($this->profile);
169         } else {
170             // TRANS: Client error.
171             $this->clientError(_m('Must provide a remote profile.'));
172         }
173     }
174
175     function connectWebfinger($acct)
176     {
177         $target_profile = $this->targetProfile();
178
179         $disco = new Discovery;
180         $result = $disco->lookup($acct);
181         if (!$result) {
182             // TRANS: Client error.
183             $this->clientError(_m('Could not look up OStatus account profile.'));
184         }
185
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);
192             }
193
194         }
195         // TRANS: Client error.
196         $this->clientError(_m('Could not confirm remote profile address.'));
197     }
198
199     function connectProfile($subscriber_profile)
200     {
201         $target_profile = $this->targetProfile();
202
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);
206
207         common_log(LOG_INFO, "Sending remote subscriber $subscriber_profile to $suburl");
208         common_redirect($suburl, 303);
209     }
210
211     /**
212      * Build the canonical profile URI+URL of the requested user or group
213      */
214     function targetProfile()
215     {
216         if ($this->nickname) {
217             $user = User::staticGet('nickname', $this->nickname);
218             if ($user) {
219                 return common_local_url('userbyid', array('id' => $user->id));
220             } else {
221                 // TRANS: Client error.
222                 $this->clientError(_m('No such user.'));
223             }
224         } else if ($this->group) {
225             $group = Local_group::staticGet('nickname', $this->group);
226             if ($group) {
227                 return common_local_url('groupbyid', array('id' => $group->group_id));
228             } else {
229                 // TRANS: Client error.
230                 $this->clientError(_m('No such group.'));
231             }
232         } else if ($this->peopletag && $this->tagger) {
233             $user = User::staticGet('nickname', $this->tagger);
234             if (empty($user)) {
235                 // TRANS: Client error.
236                 $this->clientError(_m('No such user.'));
237             }
238
239             $peopletag = Profile_list::getByTaggerAndTag($user->id, $this->peopletag);
240             if ($peopletag) {
241                 return common_local_url('profiletagbyid',
242                     array('tagger_id' => $user->id, 'id' => $peopletag->id));
243             }
244             // TRANS: Client error.
245             $this->clientError(_m('No such list.'));
246         } else {
247             // TRANS: Client error.
248             $this->clientError(_m('No local user or group nickname provided.'));
249         }
250     }
251
252     function title()
253     {
254       // TRANS: Page title.
255       return _m('OStatus Connect');
256     }
257 }