]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/foaf.php
Merge branch 'master' of gitorious.org:statusnet/mainline into testing
[quix0rs-gnu-social.git] / actions / foaf.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, 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('STATUSNET') && !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:bio' =>
86                                               'http://purl.org/vocab/bio/0.1/',
87                                               'xmlns:sioc' =>
88                                               'http://rdfs.org/sioc/ns#',
89                                               'xmlns' => 'http://xmlns.com/foaf/0.1/'));
90
91         // This is the document about the user
92
93         $this->showPpd('', $this->user->uri);
94
95         // Would be nice to tell if they were a Person or not (e.g. a #person usertag?)
96         $this->elementStart('Agent', array('rdf:about' =>
97                                              $this->user->uri));
98         if ($this->user->email) {
99             $this->element('mbox_sha1sum', null, sha1('mailto:' . $this->user->email));
100         }
101         if ($this->profile->fullname) {
102             $this->element('name', null, $this->profile->fullname);
103         }
104         if ($this->profile->homepage) {
105             $this->element('homepage', array('rdf:resource' => $this->profile->homepage));
106         }
107         if ($this->profile->profileurl) {
108             $this->element('weblog', array('rdf:resource' => $this->profile->profileurl));
109         }
110         if ($this->profile->bio) {
111             $this->element('bio:olb', null, $this->profile->bio);
112         }
113         
114         $location = $this->profile->getLocation();
115         if ($location) {
116             $attr = array();
117             if ($location->getRdfURL()) {
118                 $attr['rdf:about'] = $location->getRdfURL();
119             }
120             $location_name = $location->getName();
121             
122             $this->elementStart('based_near');
123             $this->elementStart('geo:SpatialThing', $attr);
124             if ($location_name) {
125                 $this->element('name', null, $location_name);
126             }
127             if ($location->lat) {
128                 $this->element('geo:lat', null, $location->lat);
129             }
130             if ($location->lon) {
131                 $this->element('geo:long', null, $location->lon);
132             }
133             if ($location->getURL()) {
134                 $this->element('page', array('rdf:resource'=>$location->getURL()));
135             }
136             $this->elementEnd('geo:SpatialThing');
137             $this->elementEnd('based_near');
138         }
139
140         $avatar = $this->profile->getOriginalAvatar();
141         if ($avatar) {
142             $this->elementStart('img');
143             $this->elementStart('Image', array('rdf:about' => $avatar->url));
144             foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
145                 $scaled = $this->profile->getAvatar($size);
146                 if (!$scaled->original) { // sometimes the original has one of our scaled sizes
147                     $this->elementStart('thumbnail');
148                     $this->element('Image', array('rdf:about' => $scaled->url));
149                     $this->elementEnd('thumbnail');
150                 }
151             }
152             $this->elementEnd('Image');
153             $this->elementEnd('img');
154         }
155
156         $person = $this->showMicrobloggingAccount($this->profile,
157                                      common_root_url(), $this->user->uri, false);
158
159         // Get people who subscribe to user
160
161         $sub = new Subscription();
162         $sub->subscribed = $this->profile->id;
163         $sub->whereAdd('subscriber != subscribed');
164
165         if ($sub->find()) {
166             while ($sub->fetch()) {
167                 $profile = Profile::staticGet('id', $sub->subscriber);
168                 if (empty($profile)) {
169                     common_debug('Got a bad subscription: '.print_r($sub,true));
170                     continue;
171                 }
172                 $user = $profile->getUser();
173                 $other_uri = $profile->getUri();
174                 if (array_key_exists($other_uri, $person)) {
175                     $person[$other_uri][0] = BOTH;
176                 } else {
177                     $person[$other_uri] = array(LISTENER,
178                                                 $profile->id,
179                                                 $profile->nickname,
180                                                 $user ? 'local' : 'remote');
181                 }
182                 unset($profile);
183             }
184         }
185
186         unset($sub);
187
188         foreach ($person as $uri => $p) {
189             list($type, $id, $nickname, $local) = $p;
190             if ($type == BOTH) {
191                 $this->element('knows', array('rdf:resource' => $uri));
192             }
193         }
194         
195         $this->elementEnd('Agent');
196
197
198         foreach ($person as $uri => $p) {
199             $foaf_url = null;
200             list($type, $id, $nickname, $local) = $p;
201             if ($local == 'local') {
202                 $foaf_url = common_local_url('foaf', array('nickname' => $nickname));
203             }
204             $profile = Profile::staticGet($id);
205             $this->elementStart('Agent', array('rdf:about' => $uri));
206             if ($type == BOTH) {
207                 $this->element('knows', array('rdf:resource' => $this->user->uri));
208             }
209             $this->showMicrobloggingAccount($profile,
210                                    ($local == 'local') ? common_root_url() : null,
211                                    $uri,
212                                    true);
213             if ($foaf_url) {
214                 $this->element('rdfs:seeAlso', array('rdf:resource' => $foaf_url));
215             }
216             $this->elementEnd('Agent');
217             if ($foaf_url) {
218                 $this->showPpd($foaf_url, $uri);
219             }
220             $profile->free();
221             $profile = null;
222             unset($profile);
223         }
224
225         $this->elementEnd('rdf:RDF');
226         $this->endXML();
227     }
228
229     function showPpd($foaf_url, $person_uri)
230     {
231         $this->elementStart('PersonalProfileDocument', array('rdf:about' => $foaf_url));
232         $this->element('maker', array('rdf:resource' => $person_uri));
233         $this->element('primaryTopic', array('rdf:resource' => $person_uri));
234         $this->elementEnd('PersonalProfileDocument');
235     }
236
237     function showMicrobloggingAccount($profile, $service=null, $useruri=null, $isSubscriber=false)
238     {
239         $attr = array();
240         if ($useruri) {
241             $attr['rdf:about'] = $useruri . '#acct';
242         }
243
244         // Their account
245         $this->elementStart('account');
246         $this->elementStart('OnlineAccount', $attr);
247         if ($service) {
248             $this->element('accountServiceHomepage', array('rdf:resource' =>
249                                                            $service));
250         }
251         $this->element('accountName', null, $profile->nickname);
252         $this->element('accountProfilePage', array('rdf:resource' => $profile->profileurl));
253         if ($useruri) {
254             $this->element('sioc:account_of', array('rdf:resource'=>$useruri));
255         }
256
257         $person = array();
258
259         if ($isSubscriber) {
260              $this->element('sioc:follows', array('rdf:resource'=>$this->user->uri . '#acct'));
261         } else {
262             // Get people user is subscribed to
263             $sub = new Subscription();
264             $sub->subscriber = $profile->id;
265             $sub->whereAdd('subscriber != subscribed');
266
267             if ($sub->find()) {
268                 while ($sub->fetch()) {
269                     $profile = Profile::staticGet('id', $sub->subscribed);
270                     if (empty($profile)) {
271                         common_debug('Got a bad subscription: '.print_r($sub,true));
272                         continue;
273                     }
274                     $user = $profile->getUser();
275                     $other_uri = $profile->getUri();
276                     $this->element('sioc:follows', array('rdf:resource' => $other_uri.'#acct'));
277                     $person[$other_uri] = array(LISTENEE,
278                                                 $profile->id,
279                                                 $profile->nickname,
280                                                 $user ? 'local' : 'remote');
281                     unset($profile);
282                 }
283             }
284
285             unset($sub);
286         }
287
288         $this->elementEnd('OnlineAccount');
289         $this->elementEnd('account');
290
291         return $person;
292     }
293 }