]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/foaf.php
isReadOnly() now takes arguments
[quix0rs-gnu-social.git] / actions / foaf.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, 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 if (!defined('LACONICA')) { exit(1); }
21
22 define('LISTENER', 1);
23 define('LISTENEE', -1);
24 define('BOTH', 0);
25
26 class FoafAction extends Action
27 {
28     function isReadOnly($args)
29     {
30         return true;
31     }
32
33     function prepare($args)
34     {
35         parent::prepare($args);
36
37         $nickname_arg = $this->arg('nickname');
38
39         if (empty($nickname_arg)) {
40             $this->clientError(_('No such user.'), 404);
41             return false;
42         }
43
44         $this->nickname = common_canonical_nickname($nickname_arg);
45
46         // Permanent redirect on non-canonical nickname
47
48         if ($nickname_arg != $this->nickname) {
49             common_redirect(common_local_url('foaf',
50                                              array('nickname' => $this->nickname)),
51                             301);
52             return false;
53         }
54
55         $this->user = User::staticGet('nickname', $this->nickname);
56
57         if (!$this->user) {
58             $this->clientError(_('No such user.'), 404);
59             return false;
60         }
61
62         $this->profile = $this->user->getProfile();
63
64         if (!$this->profile) {
65             $this->serverError(_('User has no profile.'), 500);
66             return false;
67         }
68
69         return true;
70     }
71
72     function handle($args)
73     {
74         parent::handle($args);
75
76         header('Content-Type: application/rdf+xml');
77
78         $this->startXML();
79         $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
80                                               'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
81                                               'xmlns:rdfs' =>
82                                               'http://www.w3.org/2000/01/rdf-schema#',
83                                               'xmlns:geo' =>
84                                               'http://www.w3.org/2003/01/geo/wgs84_pos#',
85                                               'xmlns' => 'http://xmlns.com/foaf/0.1/'));
86
87         // This is the document about the user
88
89         $this->showPpd('', $this->user->uri);
90
91         // XXX: might not be a person
92         $this->elementStart('Person', array('rdf:about' =>
93                                              $this->user->uri));
94         $this->element('mbox_sha1sum', null, sha1('mailto:' . $this->user->email));
95         if ($this->profile->fullname) {
96             $this->element('name', null, $this->profile->fullname);
97         }
98         if ($this->profile->homepage) {
99             $this->element('homepage', array('rdf:resource' => $this->profile->homepage));
100         }
101         if ($this->profile->bio) {
102             $this->element('rdfs:comment', null, $this->profile->bio);
103         }
104         // XXX: more structured location data
105         if ($this->profile->location) {
106             $this->elementStart('based_near');
107             $this->elementStart('geo:SpatialThing');
108             $this->element('name', null, $this->profile->location);
109             $this->elementEnd('geo:SpatialThing');
110             $this->elementEnd('based_near');
111         }
112
113         $this->showMicrobloggingAccount($this->profile, common_root_url());
114
115         $avatar = $this->profile->getOriginalAvatar();
116
117         if ($avatar) {
118             $this->elementStart('img');
119             $this->elementStart('Image', array('rdf:about' => $avatar->url));
120             foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
121                 $scaled = $this->profile->getAvatar($size);
122                 if (!$scaled->original) { // sometimes the original has one of our scaled sizes
123                     $this->elementStart('thumbnail');
124                     $this->element('Image', array('rdf:about' => $scaled->url));
125                     $this->elementEnd('thumbnail');
126                 }
127             }
128             $this->elementEnd('Image');
129             $this->elementEnd('img');
130         }
131
132         // Get people user is subscribed to
133
134         $person = array();
135
136         $sub = new Subscription();
137         $sub->subscriber = $this->profile->id;
138         $sub->whereAdd('subscriber != subscribed');
139
140         if ($sub->find()) {
141             while ($sub->fetch()) {
142                 if (!empty($sub->token)) {
143                     $other = Remote_profile::staticGet('id', $sub->subscribed);
144                 } else {
145                     $other = User::staticGet('id', $sub->subscribed);
146                 }
147                 if (empty($other)) {
148                     common_debug('Got a bad subscription: '.print_r($sub,true));
149                     continue;
150                 }
151                 $this->element('knows', array('rdf:resource' => $other->uri));
152                 $person[$other->uri] = array(LISTENEE,
153                                              $other->id,
154                                              $other->nickname,
155                                              (empty($sub->token)) ? 'User' : 'Remote_profile');
156                 $other->free();
157                 $other = null;
158                 unset($other);
159             }
160         }
161
162         $sub->free();
163         $sub = null;
164         unset($sub);
165
166         // Get people who subscribe to user
167
168         $sub = new Subscription();
169         $sub->subscribed = $this->profile->id;
170         $sub->whereAdd('subscriber != subscribed');
171
172         if ($sub->find()) {
173             while ($sub->fetch()) {
174                 if ($sub->token) {
175                     $other = Remote_profile::staticGet('id', $sub->subscriber);
176                 } else {
177                     $other = User::staticGet('id', $sub->subscriber);
178                 }
179                 if (!$other) {
180                     common_debug('Got a bad subscription: '.print_r($sub,true));
181                     continue;
182                 }
183                 if (array_key_exists($other->uri, $person)) {
184                     $person[$other->uri][0] = BOTH;
185                 } else {
186                     $person[$other->uri] = array(LISTENER,
187                                                  $other->id,
188                                                  $other->nickname,
189                                                  (empty($sub->token)) ? 'User' : 'Remote_profile');
190                 }
191                 $other->free();
192                 $other = null;
193                 unset($other);
194             }
195         }
196
197         $sub->free();
198         $sub = null;
199         unset($sub);
200
201         $this->elementEnd('Person');
202
203         foreach ($person as $uri => $p) {
204             $foaf_url = null;
205             list($type, $id, $nickname, $cls) = $p;
206             if ($cls == 'User') {
207                 $foaf_url = common_local_url('foaf', array('nickname' => $nickname));
208             }
209             $profile = Profile::staticGet($id);
210             $this->elementStart('Person', array('rdf:about' => $uri));
211             if ($type == LISTENER || $type == BOTH) {
212                 $this->element('knows', array('rdf:resource' => $this->user->uri));
213             }
214             $this->showMicrobloggingAccount($profile, ($cls == 'User') ?
215                                             common_root_url() : null);
216             if ($foaf_url) {
217                 $this->element('rdfs:seeAlso', array('rdf:resource' => $foaf_url));
218             }
219             $this->elementEnd('Person');
220             if ($foaf_url) {
221                 $this->showPpd($foaf_url, $uri);
222             }
223             $profile->free();
224             $profile = null;
225             unset($profile);
226         }
227
228         $this->elementEnd('rdf:RDF');
229         $this->endXML();
230     }
231
232     function showPpd($foaf_url, $person_uri)
233     {
234         $this->elementStart('PersonalProfileDocument', array('rdf:about' => $foaf_url));
235         $this->element('maker', array('rdf:resource' => $person_uri));
236         $this->element('primaryTopic', array('rdf:resource' => $person_uri));
237         $this->elementEnd('PersonalProfileDocument');
238     }
239
240     function showMicrobloggingAccount($profile, $service=null)
241     {
242         // Their account
243         $this->elementStart('holdsAccount');
244         $this->elementStart('OnlineAccount');
245         if ($service) {
246             $this->element('accountServiceHomepage', array('rdf:resource' =>
247                                                            $service));
248         }
249         $this->element('accountName', null, $profile->nickname);
250         $this->element('homepage', array('rdf:resource' => $profile->profileurl));
251         $this->elementEnd('OnlineAccount');
252         $this->elementEnd('holdsAccount');
253     }
254 }