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