]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/ostatusinit.php
Merge branch 'master' of git@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 $acct;
33     var $err;
34
35     function prepare($args)
36     {
37         parent::prepare($args);
38
39         if (common_logged_in()) {
40             $this->clientError(_('You can use the local subscription!'));
41             return false;
42         }
43
44         $this->nickname    = $this->trimmed('nickname');
45         $this->acct = $this->trimmed('acct');
46
47         return true;
48     }
49     
50     function handle($args)
51     {
52         parent::handle($args);
53
54         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
55             /* Use a session token for CSRF protection. */
56             $token = $this->trimmed('token');
57             if (!$token || $token != common_session_token()) {
58                 $this->showForm(_('There was a problem with your session token. '.
59                                   'Try again, please.'));
60                 return;
61             }
62             $this->ostatusConnect();
63         } else {
64             $this->showForm();
65         }
66     }
67     
68     function showForm($err = null)
69     {
70       $this->err = $err;
71       $this->showPage();
72
73     }
74
75     function showContent()
76     {
77         $this->elementStart('form', array('id' => 'form_ostatus_connect',
78                                           'method' => 'post',
79                                           'class' => 'form_settings',
80                                           'action' => common_local_url('ostatusinit')));
81         $this->elementStart('fieldset');
82         $this->element('legend', _('Subscribe to a remote user'));
83         $this->hidden('token', common_session_token());
84
85         $this->elementStart('ul', 'form_data');
86         $this->elementStart('li');
87         $this->input('nickname', _('User nickname'), $this->nickname,
88                      _('Nickname of the user you want to follow'));
89         $this->elementEnd('li');
90         $this->elementStart('li');
91         $this->input('acct', _('Profile Account'), $this->acct,
92                      _('Your account id (i.e. user@identi.ca)'));
93         $this->elementEnd('li');
94         $this->elementEnd('ul');
95         $this->submit('submit', _('Subscribe'));
96         $this->elementEnd('fieldset');
97         $this->elementEnd('form');
98     }        
99
100     function ostatusConnect()
101     {
102       $w = new Webfinger;
103
104       $result = $w->lookup($this->acct);
105       foreach ($result->links as $link) {
106           if ($link['rel'] == 'http://ostatus.org/schema/1.0/subscribe') {
107               // We found a URL - let's redirect!
108
109               $user = User::staticGet('nickname', $this->nickname);
110
111               $feed_url = common_local_url('ApiTimelineUser',
112                                            array('id' => $user->id,
113                                                  'format' => 'atom'));
114               $url = $w->applyTemplate($link['template'], $feed_url);
115
116               common_redirect($url, 303);
117           }
118
119       }
120       
121     }
122     
123     function title()
124     {
125       return _('OStatus Connect');  
126     }
127   
128 }