]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/ostatusinit.php
341fdec629675dba997ed53e18035cb7d6429174
[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
31     var $nickname;
32     var $group;
33     var $profile;
34     var $err;
35
36     function prepare($args)
37     {
38         parent::prepare($args);
39
40         if (common_logged_in()) {
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             $this->element('title', null, _m('Subscribe to user'));
82             $this->elementEnd('head');
83             $this->elementStart('body');
84             $this->showContent();
85             $this->elementEnd('body');
86             $this->elementEnd('html');
87         } else {
88             $this->showPage();
89         }
90     }
91
92     function showContent()
93     {
94
95       $this->clientError(_m("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 &mdash; it doesn't even need to be a GNU social server &mdash; any server that speaks the OStatus protocol is suitable. A good place to get an account for yourself is www.status.net"));
96     
97         if ($this->group) {
98             $header = sprintf(_m('Join group %s'), $this->group);
99             $submit = _m('Join');
100         } else {
101             $header = sprintf(_m('Subscribe to %s'), $this->nickname);
102             $submit = _m('Subscribe');
103         }
104         $this->elementStart('form', array('id' => 'form_ostatus_connect',
105                                           'method' => 'post',
106                                           'class' => 'form_settings',
107                                           'action' => common_local_url('ostatusinit')));
108         $this->elementStart('fieldset');
109         $this->element('legend', null,  $header);
110         $this->hidden('token', common_session_token());
111
112         $this->elementStart('ul', 'form_data');
113         $this->elementStart('li', array('id' => 'ostatus_nickname'));
114         $this->input('nickname', _m('User nickname'), $this->nickname,
115                      _m('Nickname of the user you want to follow'));
116         $this->hidden('group', $this->group); // pass-through for magic links
117         $this->elementEnd('li');
118         $this->elementStart('li', array('id' => 'ostatus_profile'));
119         $this->input('profile', _m('Profile Account'), $this->profile,
120                      _m('Your account id (i.e. user@identi.ca)'));
121         $this->elementEnd('li');
122         $this->elementEnd('ul');
123         $this->submit('submit', $submit);
124         $this->elementEnd('fieldset');
125         $this->elementEnd('form');
126     }
127
128     function ostatusConnect()
129     {
130         $opts = array('allowed_schemes' => array('http', 'https', 'acct'));
131         if (Validate::uri($this->profile, $opts)) {
132             $bits = parse_url($this->profile);
133             if ($bits['scheme'] == 'acct') {
134                 $this->connectWebfinger($bits['path']);
135             } else {
136                 $this->connectProfile($this->profile);
137             }
138         } elseif (strpos($this->profile, '@') !== false) {
139             $this->connectWebfinger($this->profile);
140         } else {
141             $this->clientError(_m("Must provide a remote profile."));
142         }
143     }
144
145     function connectWebfinger($acct)
146     {
147         $target_profile = $this->targetProfile();
148
149         $disco = new Discovery;
150         $result = $disco->lookup($acct);
151         if (!$result) {
152             $this->clientError(_m("Couldn't look up OStatus account profile."));
153         }
154
155         foreach ($result->links as $link) {
156             if ($link['rel'] == 'http://ostatus.org/schema/1.0/subscribe') {
157                 // We found a URL - let's redirect!
158                 $url = Discovery::applyTemplate($link['template'], $target_profile);
159                 common_log(LOG_INFO, "Sending remote subscriber $acct to $url");
160                 common_redirect($url, 303);
161             }
162
163         }
164         $this->clientError(_m("Couldn't confirm remote profile address."));
165     }
166
167     function connectProfile($subscriber_profile)
168     {
169         $target_profile = $this->targetProfile();
170
171         // @fixme hack hack! We should look up the remote sub URL from XRDS
172         $suburl = preg_replace('!^(.*)/(.*?)$!', '$1/main/ostatussub', $subscriber_profile);
173         $suburl .= '?profile=' . urlencode($target_profile);
174
175         common_log(LOG_INFO, "Sending remote subscriber $subscriber_profile to $suburl");
176         common_redirect($suburl, 303);
177     }
178
179     /**
180      * Build the canonical profile URI+URL of the requested user or group
181      */
182     function targetProfile()
183     {
184         if ($this->nickname) {
185             $user = User::staticGet('nickname', $this->nickname);
186             if ($user) {
187                 return common_local_url('userbyid', array('id' => $user->id));
188             } else {
189                 $this->clientError("No such user.");
190             }
191         } else if ($this->group) {
192             $group = Local_group::staticGet('nickname', $this->group);
193             if ($group) {
194                 return common_local_url('groupbyid', array('id' => $group->group_id));
195             } else {
196                 $this->clientError("No such group.");
197             }
198         } else {
199             $this->clientError("No local user or group nickname provided.");
200         }
201     }
202
203     function title()
204     {
205       return _m('OStatus Connect');  
206     }
207
208   
209   
210 }