]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/ostatusinit.php
Merge branch 'master' of gitorious.org:statusnet/mainline into testing
[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 $profile;
33     var $err;
34
35     function prepare($args)
36     {
37         parent::prepare($args);
38
39         if (common_logged_in()) {
40             $this->clientError(_m('You can use the local subscription!'));
41             return false;
42         }
43
44         // Local user the remote wants to subscribe to
45         $this->nickname = $this->trimmed('nickname');
46         
47         // Webfinger or profile URL of the remote user
48         $this->profile = $this->trimmed('profile');
49
50         return true;
51     }
52     
53     function handle($args)
54     {
55         parent::handle($args);
56
57         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
58             /* Use a session token for CSRF protection. */
59             $token = $this->trimmed('token');
60             if (!$token || $token != common_session_token()) {
61                 $this->showForm(_m('There was a problem with your session token. '.
62                                   'Try again, please.'));
63                 return;
64             }
65             $this->ostatusConnect();
66         } else {
67             $this->showForm();
68         }
69     }
70     
71     function showForm($err = null)
72     {
73         $this->err = $err;
74         if ($this->boolean('ajax')) {
75             header('Content-Type: text/xml;charset=utf-8');
76             $this->xw->startDocument('1.0', 'UTF-8');
77             $this->elementStart('html');
78             $this->elementStart('head');
79             $this->element('title', null, _m('Subscribe to user'));
80             $this->elementEnd('head');
81             $this->elementStart('body');
82             $this->showContent();
83             $this->elementEnd('body');
84             $this->elementEnd('html');
85         } else {
86             $this->showPage();
87         }
88     }
89
90     function showContent()
91     {
92         $this->elementStart('form', array('id' => 'form_ostatus_connect',
93                                           'method' => 'post',
94                                           'class' => 'form_settings',
95                                           'action' => common_local_url('ostatusinit')));
96         $this->elementStart('fieldset');
97         $this->element('legend', null,  sprintf(_m('Subscribe to %s'), $this->nickname));
98         $this->hidden('token', common_session_token());
99
100         $this->elementStart('ul', 'form_data');
101         $this->elementStart('li', array('id' => 'ostatus_nickname'));
102         $this->input('nickname', _m('User nickname'), $this->nickname,
103                      _m('Nickname of the user you want to follow'));
104         $this->elementEnd('li');
105         $this->elementStart('li', array('id' => 'ostatus_profile'));
106         $this->input('profile', _m('Profile Account'), $this->profile,
107                      _m('Your account id (i.e. user@identi.ca)'));
108         $this->elementEnd('li');
109         $this->elementEnd('ul');
110         $this->submit('submit', _m('Subscribe'));
111         $this->elementEnd('fieldset');
112         $this->elementEnd('form');
113     }
114
115     function ostatusConnect()
116     {
117         $opts = array('allowed_schemes' => array('http', 'https', 'acct'));
118         if (Validate::uri($this->profile, $opts)) {
119             $bits = parse_url($this->profile);
120             if ($bits['scheme'] == 'acct') {
121                 $this->connectWebfinger($bits['path']);
122             } else {
123                 $this->connectProfile($this->profile);
124             }
125         } elseif (strpos($this->profile, '@') !== false) {
126             $this->connectWebfinger($this->profile);
127         } else {
128             $this->clientError(_m("Must provide a remote profile."));
129         }
130     }
131
132     function connectWebfinger($acct)
133     {
134         $w = new Webfinger;
135
136         $result = $w->lookup($acct);
137         if (!$result) {
138             $this->clientError(_m("Couldn't look up OStatus account profile."));
139         }
140         foreach ($result->links as $link) {
141             if ($link['rel'] == 'http://ostatus.org/schema/1.0/subscribe') {
142                 // We found a URL - let's redirect!
143
144                 $user = User::staticGet('nickname', $this->nickname);
145                 $target_profile = common_local_url('userbyid', array('id' => $user->id));
146
147                 $url = $w->applyTemplate($link['template'], $target_profile);
148                 common_log(LOG_INFO, "Sending remote subscriber $acct to $url");
149                 common_redirect($url, 303);
150             }
151
152         }
153         $this->clientError(_m("Couldn't confirm remote profile address."));
154     }
155
156     function connectProfile($subscriber_profile)
157     {
158         $user = User::staticGet('nickname', $this->nickname);
159         $target_profile = common_local_url('userbyid', array('id' => $user->id));
160
161         // @fixme hack hack! We should look up the remote sub URL from XRDS
162         $suburl = preg_replace('!^(.*)/(.*?)$!', '$1/main/ostatussub', $subscriber_profile);
163         $suburl .= '?profile=' . urlencode($target_profile);
164
165         common_log(LOG_INFO, "Sending remote subscriber $subscriber_profile to $suburl");
166         common_redirect($suburl, 303);
167     }
168
169     function title()
170     {
171       return _m('OStatus Connect');  
172     }
173   
174 }