]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/foaf.php
Improved type-hint for following methods:
[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(array $args=array())
30     {
31         return true;
32     }
33
34     function prepare(array $args=array())
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(array $args=array())
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->getUri());
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' => $this->user->getUri()));
97         if ($this->user->email) {
98             $this->element('mbox_sha1sum', null, sha1('mailto:' . $this->user->email));
99         }
100         if ($this->profile->fullname) {
101             $this->element('name', null, $this->profile->fullname);
102         }
103         if ($this->profile->homepage) {
104             $this->element('homepage', array('rdf:resource' => $this->profile->homepage));
105         }
106         if ($this->profile->profileurl) {
107             $this->element('weblog', array('rdf:resource' => $this->profile->profileurl));
108         }
109         if ($this->profile->bio) {
110             $this->element('bio:olb', null, $this->profile->bio);
111         }
112
113         $location = $this->profile->getLocation();
114         if ($location) {
115             $attr = array();
116             if ($location->getRdfURL()) {
117                 $attr['rdf:about'] = $location->getRdfURL();
118             }
119             $location_name = $location->getName();
120
121             $this->elementStart('based_near');
122             $this->elementStart('geo:SpatialThing', $attr);
123             if ($location_name) {
124                 $this->element('name', null, $location_name);
125             }
126             if ($location->lat) {
127                 $this->element('geo:lat', null, $location->lat);
128             }
129             if ($location->lon) {
130                 $this->element('geo:long', null, $location->lon);
131             }
132             if ($location->getURL()) {
133                 $this->element('page', array('rdf:resource'=>$location->getURL()));
134             }
135             $this->elementEnd('geo:SpatialThing');
136             $this->elementEnd('based_near');
137         }
138
139         try {
140             $avatar = Avatar::getUploaded($this->profile);
141             $this->elementStart('img');
142             $this->elementStart('Image', array('rdf:about' => $avatar->displayUrl()));
143             foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
144                 try {
145                     $scaled = $this->profile->getAvatar($size);
146                     $this->elementStart('thumbnail');
147                     $this->element('Image', array('rdf:about' => $scaled->displayUrl()));
148                     $this->elementEnd('thumbnail');
149                 } catch (Exception $e) {
150                     // This avatar did not exist
151                 }
152             }
153             $this->elementEnd('Image');
154             $this->elementEnd('img');
155         } catch (NoAvatarException $e) {
156             // No avatar for this user!
157         }
158
159         $person = $this->showMicrobloggingAccount($this->profile,
160                                      common_root_url(), $this->user->getUri(),
161                                      /*$fetchSubscriptions*/true,
162                                      /*$isSubscriber*/false);
163
164         // Get people who subscribe to user
165
166         $sub = new Subscription();
167         $sub->subscribed = $this->profile->id;
168         $sub->whereAdd('subscriber != subscribed');
169
170         if ($sub->find()) {
171             while ($sub->fetch()) {
172                 $profile = Profile::getKV('id', $sub->subscriber);
173                 if (!$profile instanceof Profile) {
174                     common_debug('Got a bad subscription: '.print_r($sub,true));
175                     continue;
176                 }
177                 $other_uri = $profile->getUri();
178                 if (array_key_exists($other_uri, $person)) {
179                     $person[$other_uri][0] = BOTH;
180                 } else {
181                     $person[$other_uri] = array(LISTENER,
182                                                 $profile->id,
183                                                 $profile->nickname,
184                                                 $profile->isLocal() ? 'local' : 'remote');
185                 }
186                 unset($profile);
187             }
188         }
189
190         unset($sub);
191
192         foreach ($person as $uri => $p) {
193             list($type, $id, $nickname, $local) = $p;
194             if ($type == BOTH) {
195                 $this->element('knows', array('rdf:resource' => $uri));
196             }
197         }
198
199         $this->elementEnd('Agent');
200
201
202         foreach ($person as $uri => $p) {
203             $foaf_url = null;
204             list($type, $id, $nickname, $local) = $p;
205             if ($local == 'local') {
206                 $foaf_url = common_local_url('foaf', array('nickname' => $nickname));
207             }
208             $profile = Profile::getKV($id);
209             $this->elementStart('Agent', array('rdf:about' => $uri));
210             if ($type == BOTH) {
211                 $this->element('knows', array('rdf:resource' => $this->user->getUri()));
212             }
213             $this->showMicrobloggingAccount($profile,
214                                    ($local == 'local') ? common_root_url() : null,
215                                    $uri,
216                                    /*$fetchSubscriptions*/false,
217                                    /*$isSubscriber*/($type == LISTENER || $type == BOTH));
218             if ($foaf_url) {
219                 $this->element('rdfs:seeAlso', array('rdf:resource' => $foaf_url));
220             }
221             $this->elementEnd('Agent');
222             if ($foaf_url) {
223                 $this->showPpd($foaf_url, $uri);
224             }
225             $profile->free();
226             $profile = null;
227             unset($profile);
228         }
229
230         $this->elementEnd('rdf:RDF');
231         $this->endXML();
232     }
233
234     function showPpd($foaf_url, $person_uri)
235     {
236         $this->elementStart('PersonalProfileDocument', array('rdf:about' => $foaf_url));
237         $this->element('maker', array('rdf:resource' => $person_uri));
238         $this->element('primaryTopic', array('rdf:resource' => $person_uri));
239         $this->elementEnd('PersonalProfileDocument');
240     }
241
242     /**
243      * Output FOAF <account> bit for the given profile.
244      *
245      * @param Profile $profile
246      * @param mixed $service Root URL of this StatusNet instance for a local
247      *                       user, otherwise null.
248      * @param mixed $useruri URI string for the referenced profile..
249      * @param boolean $fetchSubscriptions Should we load and list all their subscriptions?
250      * @param boolean $isSubscriber if not fetching subs, we can still mark the user as following the current page.
251      *
252      * @return array if $fetchSubscribers is set, return a list of info on those
253      *               subscriptions.
254      */
255     function showMicrobloggingAccount($profile, $service=null, $useruri=null, $fetchSubscriptions=false, $isSubscriber=false)
256     {
257         $attr = array();
258         if ($useruri) {
259             $attr['rdf:about'] = $useruri . '#acct';
260         }
261
262         // Their account
263         $this->elementStart('account');
264         $this->elementStart('OnlineAccount', $attr);
265         if ($service) {
266             $this->element('accountServiceHomepage', array('rdf:resource' =>
267                                                            $service));
268         }
269         $this->element('accountName', null, $profile->nickname);
270         $this->element('accountProfilePage', array('rdf:resource' => $profile->profileurl));
271         if ($useruri) {
272             $this->element('sioc:account_of', array('rdf:resource'=>$useruri));
273         }
274
275         $person = array();
276
277         if ($fetchSubscriptions) {
278             // Get people user is subscribed to
279             $sub = new Subscription();
280             $sub->subscriber = $profile->id;
281             $sub->whereAdd('subscriber != subscribed');
282
283             if ($sub->find()) {
284                 while ($sub->fetch()) {
285                     $profile = Profile::getKV('id', $sub->subscribed);
286                     if (empty($profile)) {
287                         common_debug('Got a bad subscription: '.print_r($sub,true));
288                         continue;
289                     }
290                     $other_uri = $profile->getUri();
291                     $this->element('sioc:follows', array('rdf:resource' => $other_uri.'#acct'));
292                     $person[$other_uri] = array(LISTENEE,
293                                                 $profile->id,
294                                                 $profile->nickname,
295                                                 $profile->isLocal() ? 'local' : 'remote');
296                     unset($profile);
297                 }
298             }
299
300             unset($sub);
301         } else if ($isSubscriber) {
302             // Just declare that they follow the user whose FOAF we're showing.
303             $this->element('sioc:follows', array('rdf:resource' => $this->user->getUri() . '#acct'));
304         }
305
306         $this->elementEnd('OnlineAccount');
307         $this->elementEnd('account');
308
309         return $person;
310     }
311 }