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