]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/xrdaction.php
OAuth: Fix rare problem in which request tokens were sometimes being
[quix0rs-gnu-social.git] / lib / xrdaction.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 XrdAction extends Action
30 {
31     const PROFILEPAGE = 'http://webfinger.net/rel/profile-page';
32     const UPDATESFROM = 'http://schemas.google.com/g/2010#updates-from';
33     const HCARD = 'http://microformats.org/profile/hcard';
34     
35     public $uri;
36
37     public $user;
38
39     public $xrd;
40
41     function handle()
42     {
43         $nick    = $this->user->nickname;
44         $profile = $this->user->getProfile();
45
46         if (empty($this->xrd)) {
47             $xrd = new XRD();
48         } else {
49             $xrd = $this->xrd;
50         }
51
52         if (empty($xrd->subject)) {
53             $xrd->subject = self::normalize($this->uri);
54         }
55
56         if (Event::handle('StartXrdActionAliases', array(&$xrd, $this->user))) {
57             
58             // Possible aliases for the user
59             
60             $uris = array($this->user->uri, $profile->profileurl);
61             
62             // FIXME: Webfinger generation code should live somewhere on its own
63             
64             $path = common_config('site', 'path');
65             
66             if (empty($path)) {
67                 $uris[] = sprintf('acct:%s@%s', $nick, common_config('site', 'server'));
68             }
69             
70             foreach ($uris as $uri) {
71                 if ($uri != $xrd->subject) {
72                     $xrd->alias[] = $uri;
73                 }
74             }
75             
76             Event::handle('EndXrdActionAliases', array(&$xrd, $this->user));
77         }
78
79         if (Event::handle('StartXrdActionLinks', array(&$xrd, $this->user))) {
80             
81             $xrd->links[] = array('rel' => self::PROFILEPAGE,
82                                   'type' => 'text/html',
83                                   'href' => $profile->profileurl);
84             
85             // hCard
86             $xrd->links[] = array('rel' => self::HCARD,
87                                   'type' => 'text/html',
88                                   'href' => common_local_url('hcard', array('nickname' => $nick)));
89             
90             // XFN
91             $xrd->links[] = array('rel' => 'http://gmpg.org/xfn/11',
92                                   'type' => 'text/html',
93                                   'href' => $profile->profileurl);
94             // FOAF
95             $xrd->links[] = array('rel' => 'describedby',
96                                   'type' => 'application/rdf+xml',
97                                   'href' => common_local_url('foaf',
98                                                              array('nickname' => $nick)));
99             
100             $xrd->links[] = array('rel' => 'http://apinamespace.org/atom',
101                                   'type' => 'application/atomsvc+xml',
102                                   'href' => common_local_url('ApiAtomService', array('id' => $nick)));
103
104             if (common_config('site', 'fancy')) {
105                 $apiRoot = common_path('api/', true);
106             } else {
107                 $apiRoot = common_path('index.php/api/', true);
108             }
109             
110             $xrd->links[] = array('rel' => 'http://apinamespace.org/twitter',
111                                   'href' => $apiRoot,
112                                   'property' => array(array('type' => 'http://apinamespace.org/twitter/username',
113                                                             'value' => $nick)));
114
115             Event::handle('EndXrdActionLinks', array(&$xrd, $this->user));
116         }
117
118         header('Content-type: application/xrd+xml');
119         print $xrd->toXML();
120     }
121     
122     /**
123      * Given a "user id" make sure it's normalized to either a webfinger
124      * acct: uri or a profile HTTP URL.
125      */
126     
127     public static function normalize($user_id)
128     {
129         if (substr($user_id, 0, 5) == 'http:' ||
130             substr($user_id, 0, 6) == 'https:' ||
131             substr($user_id, 0, 5) == 'acct:') {
132             return $user_id;
133         }
134
135         if (strpos($user_id, '@') !== FALSE) {
136             return 'acct:' . $user_id;
137         }
138
139         return 'http://' . $user_id;
140     }
141
142     public static function isWebfinger($user_id)
143     {
144         $uri = self::normalize($user_id);
145
146         return (substr($uri, 0, 5) == 'acct:');
147     }
148
149     /**
150      * Is this action read-only?
151      *
152      * @param array $args other arguments
153      *
154      * @return boolean is read only action?
155      */
156     function isReadOnly($args)
157     {
158         return true;
159     }
160 }