]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/foaf.php
Merge branch '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                 if ($sub->token) {
166                     $other = Remote_profile::staticGet('id', $sub->subscriber);
167                     $profile = Profile::staticGet('id', $sub->subscriber);
168                 } else {
169                     $other = User::staticGet('id', $sub->subscriber);
170                     $profile = Profile::staticGet('id', $sub->subscriber);
171                 }
172                 if (!$other) {
173                     common_debug('Got a bad subscription: '.print_r($sub,true));
174                     continue;
175                 }
176                 if (array_key_exists($other->uri, $person)) {
177                     $person[$other->uri][0] = BOTH;
178                 } else {
179                     $person[$other->uri] = array(LISTENER,
180                                                  $other->id,
181                                                  $profile->nickname,
182                                                  (empty($sub->token)) ? 'User' : 'Remote_profile');
183                 }
184                 $other->free();
185                 $other = null;
186                 unset($other);
187                 $profile->free();
188                 $profile = null;
189                 unset($profile);
190             }
191         }
192
193         $sub->free();
194         $sub = null;
195         unset($sub);
196
197         foreach ($person as $uri => $p) {
198             list($type, $id, $nickname, $cls) = $p;
199             if ($type == BOTH) {
200                 $this->element('knows', array('rdf:resource' => $uri));
201             }
202         }
203         
204         $this->elementEnd('Agent');
205
206
207         foreach ($person as $uri => $p) {
208             $foaf_url = null;
209             list($type, $id, $nickname, $cls) = $p;
210             if ($cls == 'User') {
211                 $foaf_url = common_local_url('foaf', array('nickname' => $nickname));
212             }
213             $profile = Profile::staticGet($id);
214             $this->elementStart('Agent', array('rdf:about' => $uri));
215             if ($type == BOTH) {
216                 $this->element('knows', array('rdf:resource' => $this->user->uri));
217             }
218             $this->showMicrobloggingAccount($profile,
219                                    ($cls == 'User') ? common_root_url() : null,
220                                    $uri,
221                                    true);
222             if ($foaf_url) {
223                 $this->element('rdfs:seeAlso', array('rdf:resource' => $foaf_url));
224             }
225             $this->elementEnd('Agent');
226             if ($foaf_url) {
227                 $this->showPpd($foaf_url, $uri);
228             }
229             $profile->free();
230             $profile = null;
231             unset($profile);
232         }
233
234         $this->elementEnd('rdf:RDF');
235         $this->endXML();
236     }
237
238     function showPpd($foaf_url, $person_uri)
239     {
240         $this->elementStart('PersonalProfileDocument', array('rdf:about' => $foaf_url));
241         $this->element('maker', array('rdf:resource' => $person_uri));
242         $this->element('primaryTopic', array('rdf:resource' => $person_uri));
243         $this->elementEnd('PersonalProfileDocument');
244     }
245
246     function showMicrobloggingAccount($profile, $service=null, $useruri=null, $isSubscriber=false)
247     {
248         $attr = array();
249         if ($useruri) {
250             $attr['rdf:about'] = $useruri . '#acct';
251         }
252
253         // Their account
254         $this->elementStart('account');
255         $this->elementStart('OnlineAccount', $attr);
256         if ($service) {
257             $this->element('accountServiceHomepage', array('rdf:resource' =>
258                                                            $service));
259         }
260         $this->element('accountName', null, $profile->nickname);
261         $this->element('accountProfilePage', array('rdf:resource' => $profile->profileurl));
262         if ($useruri) {
263             $this->element('sioc:account_of', array('rdf:resource'=>$useruri));
264         }
265
266         $person = array();
267
268         if ($isSubscriber) {
269              $this->element('sioc:follows', array('rdf:resource'=>$this->user->uri . '#acct'));
270         } else {
271             // Get people user is subscribed to
272             $sub = new Subscription();
273             $sub->subscriber = $profile->id;
274             $sub->whereAdd('subscriber != subscribed');
275
276             if ($sub->find()) {
277                 while ($sub->fetch()) {
278                     if (!empty($sub->token)) {
279                         $other = Remote_profile::staticGet('id', $sub->subscribed);
280                         $profile = Profile::staticGet('id', $sub->subscribed);
281                     } else {
282                         $other = User::staticGet('id', $sub->subscribed);
283                         $profile = Profile::staticGet('id', $sub->subscribed);
284                     }
285                     if (empty($other)) {
286                         common_debug('Got a bad subscription: '.print_r($sub,true));
287                         continue;
288                     }
289                     $this->element('sioc:follows', array('rdf:resource' => $other->uri.'#acct'));
290                     $person[$other->uri] = array(LISTENEE,
291                                                  $other->id,
292                                                  $profile->nickname,
293                                                  (empty($sub->token)) ? 'User' : 'Remote_profile');
294                     $other->free();
295                     $other = null;
296                     unset($other);
297                     $profile->free();
298                     $profile = null;
299                     unset($profile);
300                 }
301             }
302
303             $sub->free();
304             $sub = null;
305             unset($sub);
306         }
307
308         $this->elementEnd('OnlineAccount');
309         $this->elementEnd('account');
310
311         return $person;
312     }
313 }