]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/ostatusinit.php
Merge branch 'master' 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         if ($this->boolean('ajax')) {
72             header('Content-Type: text/xml;charset=utf-8');
73             $this->xw->startDocument('1.0', 'UTF-8');
74             $this->elementStart('html');
75             $this->elementStart('head');
76             $this->element('title', null, _('Subscribe to user'));
77             $this->elementEnd('head');
78             $this->elementStart('body');
79             $this->showContent();
80             $this->elementEnd('body');
81             $this->elementEnd('html');
82         } else {
83             $this->showPage();
84         }
85     }
86
87     function showContent()
88     {
89         $this->elementStart('form', array('id' => 'form_ostatus_connect',
90                                           'method' => 'post',
91                                           'class' => 'form_settings',
92                                           'action' => common_local_url('ostatusinit')));
93         $this->elementStart('fieldset');
94         $this->element('legend', null,  sprintf(_('Subscribe to %s'), $this->nickname));
95         $this->hidden('token', common_session_token());
96
97         $this->elementStart('ul', 'form_data');
98         $this->elementStart('li', array('id' => 'ostatus_nickname'));
99         $this->input('nickname', _('User nickname'), $this->nickname,
100                      _('Nickname of the user you want to follow'));
101         $this->elementEnd('li');
102         $this->elementStart('li', array('id' => 'ostatus_profile'));
103         $this->input('acct', _('Profile Account'), $this->acct,
104                      _('Your account id (i.e. user@identi.ca)'));
105         $this->elementEnd('li');
106         $this->elementEnd('ul');
107         $this->submit('submit', _('Subscribe'));
108         $this->elementEnd('fieldset');
109         $this->elementEnd('form');
110     }
111
112     function ostatusConnect()
113     {
114       $w = new Webfinger;
115
116       $result = $w->lookup($this->acct);
117       foreach ($result->links as $link) {
118           if ($link['rel'] == 'http://ostatus.org/schema/1.0/subscribe') {
119               // We found a URL - let's redirect!
120
121               $user = User::staticGet('nickname', $this->nickname);
122
123               $feed_url = common_local_url('ApiTimelineUser',
124                                            array('id' => $user->id,
125                                                  'format' => 'atom'));
126               $url = $w->applyTemplate($link['template'], $feed_url);
127
128               common_redirect($url, 303);
129           }
130
131       }
132       
133     }
134     
135     function title()
136     {
137       return _('OStatus Connect');  
138     }
139   
140 }