]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/foaf.php
Allow unauthenticated users to view /api/statuses/replies/id.format
[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         $this->nickname = $this->trimmed('nickname');
37
38         $this->user = User::staticGet('nickname', $this->nickname);
39
40         if (!$this->user) {
41             $this->clientError(_('No such user.'), 404);
42             return false;
43         }
44
45         $this->profile = $this->user->getProfile();
46
47         if (!$this->profile) {
48             $this->serverError(_('User has no profile.'), 500);
49             return false;
50         }
51
52         return true;
53     }
54
55     function handle($args)
56     {
57         parent::handle($args);
58
59         header('Content-Type: application/rdf+xml');
60
61         $this->startXML();
62         $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
63                                               'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
64                                               'xmlns:rdfs' =>
65                                               'http://www.w3.org/2000/01/rdf-schema#',
66                                               'xmlns:geo' =>
67                                               'http://www.w3.org/2003/01/geo/wgs84_pos#',
68                                               'xmlns' => 'http://xmlns.com/foaf/0.1/'));
69
70         // This is the document about the user
71
72         $this->showPpd('', $this->user->uri);
73
74         // XXX: might not be a person
75         $this->elementStart('Person', array('rdf:about' =>
76                                              $this->user->uri));
77         $this->element('mbox_sha1sum', null, sha1('mailto:' . $this->user->email));
78         if ($this->profile->fullname) {
79             $this->element('name', null, $this->profile->fullname);
80         }
81         if ($this->profile->homepage) {
82             $this->element('homepage', array('rdf:resource' => $this->profile->homepage));
83         }
84         if ($this->profile->bio) {
85             $this->element('rdfs:comment', null, $this->profile->bio);
86         }
87         // XXX: more structured location data
88         if ($this->profile->location) {
89             $this->elementStart('based_near');
90             $this->elementStart('geo:SpatialThing');
91             $this->element('name', null, $this->profile->location);
92             $this->elementEnd('geo:SpatialThing');
93             $this->elementEnd('based_near');
94         }
95
96         $this->showMicrobloggingAccount($this->profile, common_root_url());
97
98         $avatar = $this->profile->getOriginalAvatar();
99
100         if ($avatar) {
101             $this->elementStart('img');
102             $this->elementStart('Image', array('rdf:about' => $avatar->url));
103             foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
104                 $scaled = $this->profile->getAvatar($size);
105                 if (!$scaled->original) { // sometimes the original has one of our scaled sizes
106                     $this->elementStart('thumbnail');
107                     $this->element('Image', array('rdf:about' => $scaled->url));
108                     $this->elementEnd('thumbnail');
109                 }
110             }
111             $this->elementEnd('Image');
112             $this->elementEnd('img');
113         }
114
115         // Get people user is subscribed to
116
117         $person = array();
118
119         $sub = new Subscription();
120         $sub->subscriber = $this->profile->id;
121         $sub->whereAdd('subscriber != subscribed');
122
123         if ($sub->find()) {
124             while ($sub->fetch()) {
125                 if ($sub->token) {
126                     $other = Remote_profile::staticGet('id', $sub->subscribed);
127                 } else {
128                     $other = User::staticGet('id', $sub->subscribed);
129                 }
130                 if (!$other) {
131                     common_debug('Got a bad subscription: '.print_r($sub,true));
132                     continue;
133                 }
134                 $this->element('knows', array('rdf:resource' => $other->uri));
135                 $person[$other->uri] = array(LISTENEE, $other);
136             }
137         }
138
139         // Get people who subscribe to user
140
141         $sub = new Subscription();
142         $sub->subscribed = $this->profile->id;
143         $sub->whereAdd('subscriber != subscribed');
144
145         if ($sub->find()) {
146             while ($sub->fetch()) {
147                 if ($sub->token) {
148                     $other = Remote_profile::staticGet('id', $sub->subscriber);
149                 } else {
150                     $other = User::staticGet('id', $sub->subscriber);
151                 }
152                 if (!$other) {
153                     common_debug('Got a bad subscription: '.print_r($sub,true));
154                     continue;
155                 }
156                 if (array_key_exists($other->uri, $person)) {
157                     $person[$other->uri][0] = BOTH;
158                 } else {
159                     $person[$other->uri] = array(LISTENER, $other);
160                 }
161             }
162         }
163
164         $this->elementEnd('Person');
165
166         foreach ($person as $uri => $p) {
167             $foaf_url = null;
168             if ($p[1] instanceof User) {
169                 $foaf_url = common_local_url('foaf', array('nickname' => $p[1]->nickname));
170             }
171             $this->profile = Profile::staticGet($p[1]->id);
172             $this->elementStart('Person', array('rdf:about' => $uri));
173             if ($p[0] == LISTENER || $p[0] == BOTH) {
174                 $this->element('knows', array('rdf:resource' => $this->user->uri));
175             }
176             $this->showMicrobloggingAccount($this->profile, ($p[1] instanceof User) ?
177                                               common_root_url() : null);
178             if ($foaf_url) {
179                 $this->element('rdfs:seeAlso', array('rdf:resource' => $foaf_url));
180             }
181             $this->elementEnd('Person');
182             if ($foaf_url) {
183                 $this->showPpd($foaf_url, $uri);
184             }
185         }
186
187         $this->elementEnd('rdf:RDF');
188         $this->endXML();
189     }
190
191     function showPpd($foaf_url, $person_uri)
192     {
193         $this->elementStart('PersonalProfileDocument', array('rdf:about' => $foaf_url));
194         $this->element('maker', array('rdf:resource' => $person_uri));
195         $this->element('primaryTopic', array('rdf:resource' => $person_uri));
196         $this->elementEnd('PersonalProfileDocument');
197     }
198
199     function showMicrobloggingAccount($profile, $service=null)
200     {
201         // Their account
202         $this->elementStart('holdsAccount');
203         $this->elementStart('OnlineAccount');
204         if ($service) {
205             $this->element('accountServiceHomepage', array('rdf:resource' =>
206                                                            $service));
207         }
208         $this->element('accountName', null, $profile->nickname);
209         $this->element('homepage', array('rdf:resource' => $profile->profileurl));
210         $this->elementEnd('OnlineAccount');
211         $this->elementEnd('holdsAccount');
212     }
213 }