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