]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/ostatusinit.php
Merge branch 'nightly' into deletenotice_form_fix
[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     protected function prepare(array $args=array())
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         }
46
47         // Local user or group the remote wants to subscribe to
48         $this->nickname = $this->trimmed('nickname');
49         $this->tagger = $this->trimmed('tagger');
50         $this->peopletag = $this->trimmed('peopletag');
51         $this->group = $this->trimmed('group');
52
53         // Webfinger or profile URL of the remote user
54         $this->profile = $this->trimmed('profile');
55
56         return true;
57     }
58
59     protected function handle()
60     {
61         parent::handle();
62
63         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
64             /* Use a session token for CSRF protection. */
65             $token = $this->trimmed('token');
66             if (!$token || $token != common_session_token()) {
67                 // TRANS: Client error displayed when the session token does not match or is not given.
68                 $this->showForm(_m('There was a problem with your session token. '.
69                                   'Try again, please.'));
70                 return;
71             }
72             $this->ostatusConnect();
73         } else {
74             $this->showForm();
75         }
76     }
77
78     function showForm($err = null)
79     {
80         $this->err = $err;
81         if ($this->boolean('ajax')) {
82             $this->startHTML('text/xml;charset=utf-8');
83             $this->elementStart('head');
84             // TRANS: Form title.
85             $this->element('title', null, _m('TITLE','Subscribe to user'));
86             $this->elementEnd('head');
87             $this->elementStart('body');
88             $this->showContent();
89             $this->elementEnd('body');
90             $this->endHTML();
91         } else {
92             $this->showPage();
93         }
94     }
95
96     function showContent()
97     {
98     
99         if ($this->group) {
100             // TRANS: Form legend. %s is a group name.
101             $header = sprintf(_m('Join group %s'), $this->group);
102             // TRANS: Button text to join a group.
103             $submit = _m('BUTTON','Join');
104         } else if ($this->peopletag && $this->tagger) {
105             // TRANS: Form legend. %1$s is a list, %2$s is a lister's name.
106             $header = sprintf(_m('Subscribe to list %1$s by %2$s'), $this->peopletag, $this->tagger);
107             // TRANS: Button text to subscribe to a list.
108             $submit = _m('BUTTON','Subscribe');
109             // TRANS: Button text.
110         } else {
111             // TRANS: Form legend. %s is a nickname.
112             $header = sprintf(_m('Subscribe to %s'), $this->nickname);
113             // TRANS: Button text to subscribe to a profile.
114             $submit = _m('BUTTON','Subscribe');
115         }
116         $this->elementStart('form', array('id' => 'form_ostatus_connect',
117                                           'method' => 'post',
118                                           'class' => 'form_settings',
119                                           'action' => common_local_url('ostatusinit')));
120         $this->elementStart('fieldset');
121         $this->element('legend', null,  $header);
122         $this->hidden('token', common_session_token());
123
124         $this->elementStart('ul', 'form_data');
125         $this->elementStart('li', array('id' => 'ostatus_nickname'));
126
127         if ($this->group) {
128             // TRANS: Field label.
129             $this->input('group', _m('Group nickname'), $this->group,
130                          // TRANS: Field title.
131                          _m('Nickname of the group you want to join.'));
132         } else {
133             // TRANS: Field label.
134             $this->input('nickname', _m('User nickname'), $this->nickname,
135                          // TRANS: Field title.
136                          _m('Nickname of the user you want to follow.'));
137             $this->hidden('tagger', $this->tagger);
138             $this->hidden('peopletag', $this->peopletag);
139         }
140
141         $this->elementEnd('li');
142         $this->elementStart('li', array('id' => 'ostatus_profile'));
143         // TRANS: Field label.
144         $this->input('profile', _m('Profile Account'), $this->profile,
145                       // TRANS: Tooltip for field label "Profile Account".
146                      _m('Your account ID (e.g. user@example.com).'));
147         $this->elementEnd('li');
148         $this->elementEnd('ul');
149         $this->submit('submit', $submit);
150         $this->elementEnd('fieldset');
151         $this->elementEnd('form');
152     }
153
154     function ostatusConnect()
155     {
156         $opts = array('allowed_schemes' => array('http', 'https', 'acct'));
157         if (Validate::uri($this->profile, $opts)) {
158             $bits = parse_url($this->profile);
159             if ($bits['scheme'] == 'acct') {
160                 $this->connectWebfinger($bits['path']);
161             } else {
162                 $this->connectProfile($this->profile);
163             }
164         } elseif (strpos($this->profile, '@') !== false) {
165             $this->connectWebfinger($this->profile);
166         } else {
167             // TRANS: Client error.
168             $this->clientError(_m('Must provide a remote profile.'));
169         }
170     }
171
172     function connectWebfinger($acct)
173     {
174         $target_profile = $this->targetProfile();
175
176         $disco = new Discovery;
177         $xrd = $disco->lookup($acct);
178
179         $link = $xrd->get('http://ostatus.org/schema/1.0/subscribe');
180         if (!is_null($link)) {
181             // We found a URL - let's redirect!
182             if (!empty($link->template)) {
183                 $url = Discovery::applyTemplate($link->template, $target_profile);
184             } else {
185                 $url = $link->href;
186             }
187             common_log(LOG_INFO, "Sending remote subscriber $acct to $url");
188             common_redirect($url, 303);
189         }
190         // TRANS: Client error.
191         $this->clientError(_m('Could not confirm remote profile address.'));
192     }
193
194     function connectProfile($subscriber_profile)
195     {
196         $target_profile = $this->targetProfile();
197
198         // @fixme hack hack! We should look up the remote sub URL from XRDS
199         $suburl = preg_replace('!^(.*)/(.*?)$!', '$1/main/ostatussub', $subscriber_profile);
200         $suburl .= '?profile=' . urlencode($target_profile);
201
202         common_log(LOG_INFO, "Sending remote subscriber $subscriber_profile to $suburl");
203         common_redirect($suburl, 303);
204     }
205
206     /**
207      * Build the canonical profile URI+URL of the requested user or group
208      */
209     function targetProfile()
210     {
211         if ($this->nickname) {
212             $user = User::getKV('nickname', $this->nickname);
213             if ($user) {
214                 return common_local_url('userbyid', array('id' => $user->id));
215             } else {
216                 // TRANS: Client error.
217                 $this->clientError(_m('No such user.'));
218             }
219         } else if ($this->group) {
220             $group = Local_group::getKV('nickname', $this->group);
221             if ($group instanceof Local_group) {
222                 return common_local_url('groupbyid', array('id' => $group->group_id));
223             } else {
224                 // TRANS: Client error.
225                 $this->clientError(_m('No such group.'));
226             }
227         } else if ($this->peopletag && $this->tagger) {
228             $user = User::getKV('nickname', $this->tagger);
229             if (empty($user)) {
230                 // TRANS: Client error.
231                 $this->clientError(_m('No such user.'));
232             }
233
234             $peopletag = Profile_list::getByTaggerAndTag($user->id, $this->peopletag);
235             if ($peopletag) {
236                 return common_local_url('profiletagbyid',
237                     array('tagger_id' => $user->id, 'id' => $peopletag->id));
238             }
239             // TRANS: Client error.
240             $this->clientError(_m('No such list.'));
241         } else {
242             // TRANS: Client error.
243             $this->clientError(_m('No local user or group nickname provided.'));
244         }
245     }
246
247     function title()
248     {
249       // TRANS: Page title.
250       return _m('OStatus Connect');
251     }
252 }