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
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()) {
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, $other);
156 // Get people who subscribe to user
158 $sub = new Subscription();
159 $sub->subscribed = $this->profile->id;
160 $sub->whereAdd('subscriber != subscribed');
163 while ($sub->fetch()) {
165 $other = Remote_profile::staticGet('id', $sub->subscriber);
167 $other = User::staticGet('id', $sub->subscriber);
170 common_debug('Got a bad subscription: '.print_r($sub,true));
173 if (array_key_exists($other->uri, $person)) {
174 $person[$other->uri][0] = BOTH;
176 $person[$other->uri] = array(LISTENER, $other);
181 $this->elementEnd('Person');
183 foreach ($person as $uri => $p) {
185 if ($p[1] instanceof User) {
186 $foaf_url = common_local_url('foaf', array('nickname' => $p[1]->nickname));
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));
193 $this->showMicrobloggingAccount($this->profile, ($p[1] instanceof User) ?
194 common_root_url() : null);
196 $this->element('rdfs:seeAlso', array('rdf:resource' => $foaf_url));
198 $this->elementEnd('Person');
200 $this->showPpd($foaf_url, $uri);
204 $this->elementEnd('rdf:RDF');
208 function showPpd($foaf_url, $person_uri)
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');
216 function showMicrobloggingAccount($profile, $service=null)
219 $this->elementStart('holdsAccount');
220 $this->elementStart('OnlineAccount');
222 $this->element('accountServiceHomepage', array('rdf:resource' =>
225 $this->element('accountName', null, $profile->nickname);
226 $this->element('homepage', array('rdf:resource' => $profile->profileurl));
227 $this->elementEnd('OnlineAccount');
228 $this->elementEnd('holdsAccount');