]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/foaf.php
redirect FOAF file on non-canonical nickname
[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()
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 ($sub->token) {
143                     $other = Remote_profile::staticGet('id', $sub->subscribed);
144                 } else {
145                     $other = User::staticGet('id', $sub->subscribed);
146                 }
147                 if (!$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, $other);
153             }
154         }
155
156         // Get people who subscribe to user
157
158         $sub = new Subscription();
159         $sub->subscribed = $this->profile->id;
160         $sub->whereAdd('subscriber != subscribed');
161
162         if ($sub->find()) {
163             while ($sub->fetch()) {
164                 if ($sub->token) {
165                     $other = Remote_profile::staticGet('id', $sub->subscriber);
166                 } else {
167                     $other = User::staticGet('id', $sub->subscriber);
168                 }
169                 if (!$other) {
170                     common_debug('Got a bad subscription: '.print_r($sub,true));
171                     continue;
172                 }
173                 if (array_key_exists($other->uri, $person)) {
174                     $person[$other->uri][0] = BOTH;
175                 } else {
176                     $person[$other->uri] = array(LISTENER, $other);
177                 }
178             }
179         }
180
181         $this->elementEnd('Person');
182
183         foreach ($person as $uri => $p) {
184             $foaf_url = null;
185             if ($p[1] instanceof User) {
186                 $foaf_url = common_local_url('foaf', array('nickname' => $p[1]->nickname));
187             }
188             $this->profile = Profile::staticGet($p[1]->id);
189             $this->elementStart('Person', array('rdf:about' => $uri));
190             if ($p[0] == LISTENER || $p[0] == BOTH) {
191                 $this->element('knows', array('rdf:resource' => $this->user->uri));
192             }
193             $this->showMicrobloggingAccount($this->profile, ($p[1] instanceof User) ?
194                                               common_root_url() : null);
195             if ($foaf_url) {
196                 $this->element('rdfs:seeAlso', array('rdf:resource' => $foaf_url));
197             }
198             $this->elementEnd('Person');
199             if ($foaf_url) {
200                 $this->showPpd($foaf_url, $uri);
201             }
202         }
203
204         $this->elementEnd('rdf:RDF');
205         $this->endXML();
206     }
207
208     function showPpd($foaf_url, $person_uri)
209     {
210         $this->elementStart('PersonalProfileDocument', array('rdf:about' => $foaf_url));
211         $this->element('maker', array('rdf:resource' => $person_uri));
212         $this->element('primaryTopic', array('rdf:resource' => $person_uri));
213         $this->elementEnd('PersonalProfileDocument');
214     }
215
216     function showMicrobloggingAccount($profile, $service=null)
217     {
218         // Their account
219         $this->elementStart('holdsAccount');
220         $this->elementStart('OnlineAccount');
221         if ($service) {
222             $this->element('accountServiceHomepage', array('rdf:resource' =>
223                                                            $service));
224         }
225         $this->element('accountName', null, $profile->nickname);
226         $this->element('homepage', array('rdf:resource' => $profile->profileurl));
227         $this->elementEnd('OnlineAccount');
228         $this->elementEnd('holdsAccount');
229     }
230 }