3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 define('LISTENER', 1);
23 define('LISTENEE', -1);
26 class FoafAction extends Action
28 function isReadOnly($args)
33 function prepare($args)
35 parent::prepare($args);
37 $nickname_arg = $this->arg('nickname');
39 if (empty($nickname_arg)) {
40 $this->clientError(_('No such user.'), 404);
44 $this->nickname = common_canonical_nickname($nickname_arg);
46 // Permanent redirect on non-canonical nickname
48 if ($nickname_arg != $this->nickname) {
49 common_redirect(common_local_url('foaf',
50 array('nickname' => $this->nickname)),
55 $this->user = User::staticGet('nickname', $this->nickname);
58 $this->clientError(_('No such user.'), 404);
62 $this->profile = $this->user->getProfile();
64 if (!$this->profile) {
65 $this->serverError(_('User has no profile.'), 500);
72 function handle($args)
74 parent::handle($args);
76 header('Content-Type: application/rdf+xml');
79 $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
80 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
82 'http://www.w3.org/2000/01/rdf-schema#',
84 'http://www.w3.org/2003/01/geo/wgs84_pos#',
85 'xmlns' => 'http://xmlns.com/foaf/0.1/'));
87 // This is the document about the user
89 $this->showPpd('', $this->user->uri);
91 // XXX: might not be a person
92 $this->elementStart('Person', array('rdf:about' =>
94 $this->element('mbox_sha1sum', null, sha1('mailto:' . $this->user->email));
95 if ($this->profile->fullname) {
96 $this->element('name', null, $this->profile->fullname);
98 if ($this->profile->homepage) {
99 $this->element('homepage', array('rdf:resource' => $this->profile->homepage));
101 if ($this->profile->bio) {
102 $this->element('rdfs:comment', null, $this->profile->bio);
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');
113 $this->showMicrobloggingAccount($this->profile, common_root_url());
115 $avatar = $this->profile->getOriginalAvatar();
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');
128 $this->elementEnd('Image');
129 $this->elementEnd('img');
132 // Get people user is subscribed to
136 $sub = new Subscription();
137 $sub->subscriber = $this->profile->id;
138 $sub->whereAdd('subscriber != subscribed');
141 while ($sub->fetch()) {
142 if (!empty($sub->token)) {
143 $other = Remote_profile::staticGet('id', $sub->subscribed);
145 $other = User::staticGet('id', $sub->subscribed);
148 common_debug('Got a bad subscription: '.print_r($sub,true));
151 $this->element('knows', array('rdf:resource' => $other->uri));
152 $person[$other->uri] = array(LISTENEE,
155 (empty($sub->token)) ? 'User' : 'Remote_profile');
166 // Get people who subscribe to user
168 $sub = new Subscription();
169 $sub->subscribed = $this->profile->id;
170 $sub->whereAdd('subscriber != subscribed');
173 while ($sub->fetch()) {
175 $other = Remote_profile::staticGet('id', $sub->subscriber);
177 $other = User::staticGet('id', $sub->subscriber);
180 common_debug('Got a bad subscription: '.print_r($sub,true));
183 if (array_key_exists($other->uri, $person)) {
184 $person[$other->uri][0] = BOTH;
186 $person[$other->uri] = array(LISTENER,
189 (empty($sub->token)) ? 'User' : 'Remote_profile');
201 $this->elementEnd('Person');
203 foreach ($person as $uri => $p) {
205 list($type, $id, $nickname, $cls) = $p;
206 if ($cls == 'User') {
207 $foaf_url = common_local_url('foaf', array('nickname' => $nickname));
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));
214 $this->showMicrobloggingAccount($profile, ($cls == 'User') ?
215 common_root_url() : null);
217 $this->element('rdfs:seeAlso', array('rdf:resource' => $foaf_url));
219 $this->elementEnd('Person');
221 $this->showPpd($foaf_url, $uri);
228 $this->elementEnd('rdf:RDF');
232 function showPpd($foaf_url, $person_uri)
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');
240 function showMicrobloggingAccount($profile, $service=null)
243 $this->elementStart('holdsAccount');
244 $this->elementStart('OnlineAccount');
246 $this->element('accountServiceHomepage', array('rdf:resource' =>
249 $this->element('accountName', null, $profile->nickname);
250 $this->element('homepage', array('rdf:resource' => $profile->profileurl));
251 $this->elementEnd('OnlineAccount');
252 $this->elementEnd('holdsAccount');