]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/foaf.php
Merge branch 'master' into 1.0.x
[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 // @todo XXX: Documentation missing.
27 class FoafAction extends Action
28 {
29     function isReadOnly($args)
30     {
31         return true;
32     }
33
34     function prepare($args)
35     {
36         parent::prepare($args);
37
38         $nickname_arg = $this->arg('nickname');
39
40         if (empty($nickname_arg)) {
41             // TRANS: Client error displayed when requesting Friends of a Friend feed without providing a user nickname.
42             $this->clientError(_('No such user.'), 404);
43             return false;
44         }
45
46         $this->nickname = common_canonical_nickname($nickname_arg);
47
48         // Permanent redirect on non-canonical nickname
49
50         if ($nickname_arg != $this->nickname) {
51             common_redirect(common_local_url('foaf',
52                                              array('nickname' => $this->nickname)),
53                             301);
54             return false;
55         }
56
57         $this->user = User::staticGet('nickname', $this->nickname);
58
59         if (!$this->user) {
60             // TRANS: Client error displayed when requesting Friends of a Friend feed for an object that is not a user.
61             $this->clientError(_('No such user.'), 404);
62             return false;
63         }
64
65         $this->profile = $this->user->getProfile();
66
67         if (!$this->profile) {
68             // TRANS: Error message displayed when referring to a user without a profile.
69             $this->serverError(_('User has no profile.'), 500);
70             return false;
71         }
72
73         return true;
74     }
75
76     function handle($args)
77     {
78         parent::handle($args);
79
80         header('Content-Type: application/rdf+xml');
81
82         $this->startXML();
83         $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
84                                               'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
85                                               'xmlns:rdfs' =>
86                                               'http://www.w3.org/2000/01/rdf-schema#',
87                                               'xmlns:geo' =>
88                                               'http://www.w3.org/2003/01/geo/wgs84_pos#',
89                                               'xmlns:bio' =>
90                                               'http://purl.org/vocab/bio/0.1/',
91                                               'xmlns:sioc' =>
92                                               'http://rdfs.org/sioc/ns#',
93                                               'xmlns' => 'http://xmlns.com/foaf/0.1/'));
94
95         // This is the document about the user
96
97         $this->showPpd('', $this->user->uri);
98
99         // Would be nice to tell if they were a Person or not (e.g. a #person usertag?)
100         $this->elementStart('Agent', array('rdf:about' =>
101                                              $this->user->uri));
102         if ($this->user->email) {
103             $this->element('mbox_sha1sum', null, sha1('mailto:' . $this->user->email));
104         }
105         if ($this->profile->fullname) {
106             $this->element('name', null, $this->profile->fullname);
107         }
108         if ($this->profile->homepage) {
109             $this->element('homepage', array('rdf:resource' => $this->profile->homepage));
110         }
111         if ($this->profile->profileurl) {
112             $this->element('weblog', array('rdf:resource' => $this->profile->profileurl));
113         }
114         if ($this->profile->bio) {
115             $this->element('bio:olb', null, $this->profile->bio);
116         }
117
118         $location = $this->profile->getLocation();
119         if ($location) {
120             $attr = array();
121             if ($location->getRdfURL()) {
122                 $attr['rdf:about'] = $location->getRdfURL();
123             }
124             $location_name = $location->getName();
125
126             $this->elementStart('based_near');
127             $this->elementStart('geo:SpatialThing', $attr);
128             if ($location_name) {
129                 $this->element('name', null, $location_name);
130             }
131             if ($location->lat) {
132                 $this->element('geo:lat', null, $location->lat);
133             }
134             if ($location->lon) {
135                 $this->element('geo:long', null, $location->lon);
136             }
137             if ($location->getURL()) {
138                 $this->element('page', array('rdf:resource'=>$location->getURL()));
139             }
140             $this->elementEnd('geo:SpatialThing');
141             $this->elementEnd('based_near');
142         }
143
144         $avatar = $this->profile->getOriginalAvatar();
145         if ($avatar) {
146             $this->elementStart('img');
147             $this->elementStart('Image', array('rdf:about' => $avatar->url));
148             foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
149                 $scaled = $this->profile->getAvatar($size);
150                 if (!$scaled->original) { // sometimes the original has one of our scaled sizes
151                     $this->elementStart('thumbnail');
152                     $this->element('Image', array('rdf:about' => $scaled->url));
153                     $this->elementEnd('thumbnail');
154                 }
155             }
156             $this->elementEnd('Image');
157             $this->elementEnd('img');
158         }
159
160         $person = $this->showMicrobloggingAccount($this->profile,
161                                      common_root_url(), $this->user->uri,
162                                      /*$fetchSubscriptions*/true,
163                                      /*$isSubscriber*/false);
164
165         // Get people who subscribe to user
166
167         $sub = new Subscription();
168         $sub->subscribed = $this->profile->id;
169         $sub->whereAdd('subscriber != subscribed');
170
171         if ($sub->find()) {
172             while ($sub->fetch()) {
173                 $profile = Profile::staticGet('id', $sub->subscriber);
174                 if (empty($profile)) {
175                     common_debug('Got a bad subscription: '.print_r($sub,true));
176                     continue;
177                 }
178                 $user = $profile->getUser();
179                 $other_uri = $profile->getUri();
180                 if (array_key_exists($other_uri, $person)) {
181                     $person[$other_uri][0] = BOTH;
182                 } else {
183                     $person[$other_uri] = array(LISTENER,
184                                                 $profile->id,
185                                                 $profile->nickname,
186                                                 $user ? 'local' : 'remote');
187                 }
188                 unset($profile);
189             }
190         }
191
192         unset($sub);
193
194         foreach ($person as $uri => $p) {
195             list($type, $id, $nickname, $local) = $p;
196             if ($type == BOTH) {
197                 $this->element('knows', array('rdf:resource' => $uri));
198             }
199         }
200
201         $this->elementEnd('Agent');
202
203
204         foreach ($person as $uri => $p) {
205             $foaf_url = null;
206             list($type, $id, $nickname, $local) = $p;
207             if ($local == 'local') {
208                 $foaf_url = common_local_url('foaf', array('nickname' => $nickname));
209             }
210             $profile = Profile::staticGet($id);
211             $this->elementStart('Agent', array('rdf:about' => $uri));
212             if ($type == BOTH) {
213                 $this->element('knows', array('rdf:resource' => $this->user->uri));
214             }
215             $this->showMicrobloggingAccount($profile,
216                                    ($local == 'local') ? common_root_url() : null,
217                                    $uri,
218                                    /*$fetchSubscriptions*/false,
219                                    /*$isSubscriber*/($type == LISTENER || $type == BOTH));
220             if ($foaf_url) {
221                 $this->element('rdfs:seeAlso', array('rdf:resource' => $foaf_url));
222             }
223             $this->elementEnd('Agent');
224             if ($foaf_url) {
225                 $this->showPpd($foaf_url, $uri);
226             }
227             $profile->free();
228             $profile = null;
229             unset($profile);
230         }
231
232         $this->elementEnd('rdf:RDF');
233         $this->endXML();
234     }
235
236     function showPpd($foaf_url, $person_uri)
237     {
238         $this->elementStart('PersonalProfileDocument', array('rdf:about' => $foaf_url));
239         $this->element('maker', array('rdf:resource' => $person_uri));
240         $this->element('primaryTopic', array('rdf:resource' => $person_uri));
241         $this->elementEnd('PersonalProfileDocument');
242     }
243
244     /**
245      * Output FOAF <account> bit for the given profile.
246      *
247      * @param Profile $profile
248      * @param mixed $service Root URL of this StatusNet instance for a local
249      *                       user, otherwise null.
250      * @param mixed $useruri URI string for the referenced profile..
251      * @param boolean $fetchSubscriptions Should we load and list all their subscriptions?
252      * @param boolean $isSubscriber if not fetching subs, we can still mark the user as following the current page.
253      *
254      * @return array if $fetchSubscribers is set, return a list of info on those
255      *               subscriptions.
256      */
257     function showMicrobloggingAccount($profile, $service=null, $useruri=null, $fetchSubscriptions=false, $isSubscriber=false)
258     {
259         $attr = array();
260         if ($useruri) {
261             $attr['rdf:about'] = $useruri . '#acct';
262         }
263
264         // Their account
265         $this->elementStart('account');
266         $this->elementStart('OnlineAccount', $attr);
267         if ($service) {
268             $this->element('accountServiceHomepage', array('rdf:resource' =>
269                                                            $service));
270         }
271         $this->element('accountName', null, $profile->nickname);
272         $this->element('accountProfilePage', array('rdf:resource' => $profile->profileurl));
273         if ($useruri) {
274             $this->element('sioc:account_of', array('rdf:resource'=>$useruri));
275         }
276
277         $person = array();
278
279         if ($fetchSubscriptions) {
280             // Get people user is subscribed to
281             $sub = new Subscription();
282             $sub->subscriber = $profile->id;
283             $sub->whereAdd('subscriber != subscribed');
284
285             if ($sub->find()) {
286                 while ($sub->fetch()) {
287                     $profile = Profile::staticGet('id', $sub->subscribed);
288                     if (empty($profile)) {
289                         common_debug('Got a bad subscription: '.print_r($sub,true));
290                         continue;
291                     }
292                     $user = $profile->getUser();
293                     $other_uri = $profile->getUri();
294                     $this->element('sioc:follows', array('rdf:resource' => $other_uri.'#acct'));
295                     $person[$other_uri] = array(LISTENEE,
296                                                 $profile->id,
297                                                 $profile->nickname,
298                                                 $user ? 'local' : 'remote');
299                     unset($profile);
300                 }
301             }
302
303             unset($sub);
304         } else if ($isSubscriber) {
305             // Just declare that they follow the user whose FOAF we're showing.
306             $this->element('sioc:follows', array('rdf:resource' => $this->user->uri . '#acct'));
307         }
308
309         $this->elementEnd('OnlineAccount');
310         $this->elementEnd('account');
311
312         return $person;
313     }
314 }