]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/ostatusinit.php
* i18n/L10n update
[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') && !defined('LACONICA')) { exit(1); }
26
27
28 class OStatusInitAction extends Action
29 {
30     var $nickname;
31     var $group;
32     var $profile;
33     var $err;
34
35     function prepare($args)
36     {
37         parent::prepare($args);
38
39         if (common_logged_in()) {
40             // TRANS: Client error.
41             $this->clientError(_m('You can use the local subscription!'));
42             return false;
43         }
44
45         // Local user or group the remote wants to subscribe to
46         $this->nickname = $this->trimmed('nickname');
47         $this->group = $this->trimmed('group');
48
49         // Webfinger or profile URL of the remote user
50         $this->profile = $this->trimmed('profile');
51
52         return true;
53     }
54
55     function handle($args)
56     {
57         parent::handle($args);
58
59         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
60             /* Use a session token for CSRF protection. */
61             $token = $this->trimmed('token');
62             if (!$token || $token != common_session_token()) {
63                 $this->showForm(_m('There was a problem with your session token. '.
64                                   'Try again, please.'));
65                 return;
66             }
67             $this->ostatusConnect();
68         } else {
69             $this->showForm();
70         }
71     }
72
73     function showForm($err = null)
74     {
75         $this->err = $err;
76         if ($this->boolean('ajax')) {
77             header('Content-Type: text/xml;charset=utf-8');
78             $this->xw->startDocument('1.0', 'UTF-8');
79             $this->elementStart('html');
80             $this->elementStart('head');
81             // TRANS: Form title.
82             $this->element('title', null, _m('Subscribe to user'));
83             $this->elementEnd('head');
84             $this->elementStart('body');
85             $this->showContent();
86             $this->elementEnd('body');
87             $this->elementEnd('html');
88         } else {
89             $this->showPage();
90         }
91     }
92
93     function showContent()
94     {
95         if ($this->group) {
96             // TRANS: Form legend.
97             $header = sprintf(_m('Join group %s'), $this->group);
98             // TRANS: Button text.
99             $submit = _m('BUTTON','Join');
100         } else {
101             // TRANS: Form legend.
102             $header = sprintf(_m('Subscribe to %s'), $this->nickname);
103             // TRANS: Button text.
104             $submit = _m('BUTTON','Subscribe');
105         }
106         $this->elementStart('form', array('id' => 'form_ostatus_connect',
107                                           'method' => 'post',
108                                           'class' => 'form_settings',
109                                           'action' => common_local_url('ostatusinit')));
110         $this->elementStart('fieldset');
111         $this->element('legend', null,  $header);
112         $this->hidden('token', common_session_token());
113
114         $this->elementStart('ul', 'form_data');
115         $this->elementStart('li', array('id' => 'ostatus_nickname'));
116         // TRANS: Field label.
117         $this->input('nickname', _m('User nickname'), $this->nickname,
118                      _m('Nickname of the user you want to follow.'));
119         $this->hidden('group', $this->group); // pass-through for magic links
120         $this->elementEnd('li');
121         $this->elementStart('li', array('id' => 'ostatus_profile'));
122         // TRANS: Field label.
123         $this->input('profile', _m('Profile Account'), $this->profile,
124                       // TRANS: Tooltip for field label "Profile Account".
125                      _m('Your account id (e.g. user@identi.ca).'));
126         $this->elementEnd('li');
127         $this->elementEnd('ul');
128         $this->submit('submit', $submit);
129         $this->elementEnd('fieldset');
130         $this->elementEnd('form');
131     }
132
133     function ostatusConnect()
134     {
135         $opts = array('allowed_schemes' => array('http', 'https', 'acct'));
136         if (Validate::uri($this->profile, $opts)) {
137             $bits = parse_url($this->profile);
138             if ($bits['scheme'] == 'acct') {
139                 $this->connectWebfinger($bits['path']);
140             } else {
141                 $this->connectProfile($this->profile);
142             }
143         } elseif (strpos($this->profile, '@') !== false) {
144             $this->connectWebfinger($this->profile);
145         } else {
146             // TRANS: Client error.
147             $this->clientError(_m("Must provide a remote profile."));
148         }
149     }
150
151     function connectWebfinger($acct)
152     {
153         $target_profile = $this->targetProfile();
154
155         $disco = new Discovery;
156         $result = $disco->lookup($acct);
157         if (!$result) {
158             // TRANS: Client error.
159             $this->clientError(_m("Couldn't look up OStatus account profile."));
160         }
161
162         foreach ($result->links as $link) {
163             if ($link['rel'] == 'http://ostatus.org/schema/1.0/subscribe') {
164                 // We found a URL - let's redirect!
165                 $url = Discovery::applyTemplate($link['template'], $target_profile);
166                 common_log(LOG_INFO, "Sending remote subscriber $acct to $url");
167                 common_redirect($url, 303);
168             }
169
170         }
171         // TRANS: Client error.
172         $this->clientError(_m("Couldn't confirm remote profile address."));
173     }
174
175     function connectProfile($subscriber_profile)
176     {
177         $target_profile = $this->targetProfile();
178
179         // @fixme hack hack! We should look up the remote sub URL from XRDS
180         $suburl = preg_replace('!^(.*)/(.*?)$!', '$1/main/ostatussub', $subscriber_profile);
181         $suburl .= '?profile=' . urlencode($target_profile);
182
183         common_log(LOG_INFO, "Sending remote subscriber $subscriber_profile to $suburl");
184         common_redirect($suburl, 303);
185     }
186
187     /**
188      * Build the canonical profile URI+URL of the requested user or group
189      */
190     function targetProfile()
191     {
192         if ($this->nickname) {
193             $user = User::staticGet('nickname', $this->nickname);
194             if ($user) {
195                 return common_local_url('userbyid', array('id' => $user->id));
196             } else {
197                 // TRANS: Client error.
198                 $this->clientError("No such user.");
199             }
200         } else if ($this->group) {
201             $group = Local_group::staticGet('nickname', $this->group);
202             if ($group) {
203                 return common_local_url('groupbyid', array('id' => $group->group_id));
204             } else {
205                 // TRANS: Client error.
206                 $this->clientError("No such group.");
207             }
208         } else {
209             // TRANS: Client error.
210             $this->clientError("No local user or group nickname provided.");
211         }
212     }
213
214     function title()
215     {
216       // TRANS: Page title.
217       return _m('OStatus Connect');
218     }
219 }