]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/foaf.php
83f7d97d5883cabad98d736fe753db24a94e9b6d
[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         }
44
45         $this->nickname = common_canonical_nickname($nickname_arg);
46
47         // Permanent redirect on non-canonical nickname
48
49         if ($nickname_arg != $this->nickname) {
50             common_redirect(common_local_url('foaf',
51                                              array('nickname' => $this->nickname)),
52                             301);
53         }
54
55         $this->user = User::getKV('nickname', $this->nickname);
56
57         if (!$this->user) {
58             // TRANS: Client error displayed when requesting Friends of a Friend feed for an object that is not a user.
59             $this->clientError(_('No such user.'), 404);
60         }
61
62         $this->profile = $this->user->getProfile();
63
64         if (!$this->profile) {
65             // TRANS: Error message displayed when referring to a user without a profile.
66             $this->serverError(_('User has no profile.'), 500);
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         try {
141             $avatar = Avatar::getUploaded($this->profile);
142             $this->elementStart('img');
143             $this->elementStart('Image', array('rdf:about' => $avatar->displayUrl()));
144             foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
145                 try {
146                     $scaled = $this->profile->getAvatar($size);
147                     $this->elementStart('thumbnail');
148                     $this->element('Image', array('rdf:about' => $scaled->displayUrl()));
149                     $this->elementEnd('thumbnail');
150                 } catch (Exception $e) {
151                     // This avatar did not exist
152                 }
153             }
154             $this->elementEnd('Image');
155             $this->elementEnd('img');
156         } catch (NoAvatarException $e) {
157             // No avatar for this user!
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::getKV('id', $sub->subscriber);
174                 if (empty($profile)) {
175                     common_debug('Got a bad subscription: '.print_r($sub,true));
176                     continue;
177                 }
178                 $other_uri = $profile->getUri();
179                 if (array_key_exists($other_uri, $person)) {
180                     $person[$other_uri][0] = BOTH;
181                 } else {
182                     $person[$other_uri] = array(LISTENER,
183                                                 $profile->id,
184                                                 $profile->nickname,
185                                                 $profile->isLocal() ? 'local' : 'remote');
186                 }
187                 unset($profile);
188             }
189         }
190
191         unset($sub);
192
193         foreach ($person as $uri => $p) {
194             list($type, $id, $nickname, $local) = $p;
195             if ($type == BOTH) {
196                 $this->element('knows', array('rdf:resource' => $uri));
197             }
198         }
199
200         $this->elementEnd('Agent');
201
202
203         foreach ($person as $uri => $p) {
204             $foaf_url = null;
205             list($type, $id, $nickname, $local) = $p;
206             if ($local == 'local') {
207                 $foaf_url = common_local_url('foaf', array('nickname' => $nickname));
208             }
209             $profile = Profile::getKV($id);
210             $this->elementStart('Agent', array('rdf:about' => $uri));
211             if ($type == BOTH) {
212                 $this->element('knows', array('rdf:resource' => $this->user->uri));
213             }
214             $this->showMicrobloggingAccount($profile,
215                                    ($local == 'local') ? common_root_url() : null,
216                                    $uri,
217                                    /*$fetchSubscriptions*/false,
218                                    /*$isSubscriber*/($type == LISTENER || $type == BOTH));
219             if ($foaf_url) {
220                 $this->element('rdfs:seeAlso', array('rdf:resource' => $foaf_url));
221             }
222             $this->elementEnd('Agent');
223             if ($foaf_url) {
224                 $this->showPpd($foaf_url, $uri);
225             }
226             $profile->free();
227             $profile = null;
228             unset($profile);
229         }
230
231         $this->elementEnd('rdf:RDF');
232         $this->endXML();
233     }
234
235     function showPpd($foaf_url, $person_uri)
236     {
237         $this->elementStart('PersonalProfileDocument', array('rdf:about' => $foaf_url));
238         $this->element('maker', array('rdf:resource' => $person_uri));
239         $this->element('primaryTopic', array('rdf:resource' => $person_uri));
240         $this->elementEnd('PersonalProfileDocument');
241     }
242
243     /**
244      * Output FOAF <account> bit for the given profile.
245      *
246      * @param Profile $profile
247      * @param mixed $service Root URL of this StatusNet instance for a local
248      *                       user, otherwise null.
249      * @param mixed $useruri URI string for the referenced profile..
250      * @param boolean $fetchSubscriptions Should we load and list all their subscriptions?
251      * @param boolean $isSubscriber if not fetching subs, we can still mark the user as following the current page.
252      *
253      * @return array if $fetchSubscribers is set, return a list of info on those
254      *               subscriptions.
255      */
256     function showMicrobloggingAccount($profile, $service=null, $useruri=null, $fetchSubscriptions=false, $isSubscriber=false)
257     {
258         $attr = array();
259         if ($useruri) {
260             $attr['rdf:about'] = $useruri . '#acct';
261         }
262
263         // Their account
264         $this->elementStart('account');
265         $this->elementStart('OnlineAccount', $attr);
266         if ($service) {
267             $this->element('accountServiceHomepage', array('rdf:resource' =>
268                                                            $service));
269         }
270         $this->element('accountName', null, $profile->nickname);
271         $this->element('accountProfilePage', array('rdf:resource' => $profile->profileurl));
272         if ($useruri) {
273             $this->element('sioc:account_of', array('rdf:resource'=>$useruri));
274         }
275
276         $person = array();
277
278         if ($fetchSubscriptions) {
279             // Get people user is subscribed to
280             $sub = new Subscription();
281             $sub->subscriber = $profile->id;
282             $sub->whereAdd('subscriber != subscribed');
283
284             if ($sub->find()) {
285                 while ($sub->fetch()) {
286                     $profile = Profile::getKV('id', $sub->subscribed);
287                     if (empty($profile)) {
288                         common_debug('Got a bad subscription: '.print_r($sub,true));
289                         continue;
290                     }
291                     $other_uri = $profile->getUri();
292                     $this->element('sioc:follows', array('rdf:resource' => $other_uri.'#acct'));
293                     $person[$other_uri] = array(LISTENEE,
294                                                 $profile->id,
295                                                 $profile->nickname,
296                                                 $profile->isLocal() ? 'local' : 'remote');
297                     unset($profile);
298                 }
299             }
300
301             unset($sub);
302         } else if ($isSubscriber) {
303             // Just declare that they follow the user whose FOAF we're showing.
304             $this->element('sioc:follows', array('rdf:resource' => $this->user->uri . '#acct'));
305         }
306
307         $this->elementEnd('OnlineAccount');
308         $this->elementEnd('account');
309
310         return $person;
311     }
312 }