]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/foaf.php
Update copyright dates in files modified in 2009
[quix0rs-gnu-social.git] / actions / foaf.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, Control Yourself, 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('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                 } else {
150                     $other = User::staticGet('id', $sub->subscriber);
151                 }
152                 if (!$other) {
153                     common_debug('Got a bad subscription: '.print_r($sub,true));
154                     continue;
155                 }
156                 if (array_key_exists($other->uri, $person)) {
157                     $person[$other->uri][0] = BOTH;
158                 } else {
159                     $person[$other->uri] = array(LISTENER,
160                                                  $other->id,
161                                                  $other->nickname,
162                                                  (empty($sub->token)) ? 'User' : 'Remote_profile');
163                 }
164                 $other->free();
165                 $other = null;
166                 unset($other);
167             }
168         }
169
170         $sub->free();
171         $sub = null;
172         unset($sub);
173
174         foreach ($person as $uri => $p) {
175             list($type, $id, $nickname, $cls) = $p;
176             if ($type == BOTH) {
177                 $this->element('knows', array('rdf:resource' => $uri));
178             }
179         }
180         
181         $this->elementEnd('Agent');
182
183
184         foreach ($person as $uri => $p) {
185             $foaf_url = null;
186             list($type, $id, $nickname, $cls) = $p;
187             if ($cls == 'User') {
188                 $foaf_url = common_local_url('foaf', array('nickname' => $nickname));
189             }
190             $profile = Profile::staticGet($id);
191             $this->elementStart('Agent', array('rdf:about' => $uri));
192             if ($type == BOTH) {
193                 $this->element('knows', array('rdf:resource' => $this->user->uri));
194             }
195             $this->showMicrobloggingAccount($profile,
196                                    ($cls == 'User') ? common_root_url() : null,
197                                    $uri,
198                                    true);
199             if ($foaf_url) {
200                 $this->element('rdfs:seeAlso', array('rdf:resource' => $foaf_url));
201             }
202             $this->elementEnd('Agent');
203             if ($foaf_url) {
204                 $this->showPpd($foaf_url, $uri);
205             }
206             $profile->free();
207             $profile = null;
208             unset($profile);
209         }
210
211         $this->elementEnd('rdf:RDF');
212         $this->endXML();
213     }
214
215     function showPpd($foaf_url, $person_uri)
216     {
217         $this->elementStart('PersonalProfileDocument', array('rdf:about' => $foaf_url));
218         $this->element('maker', array('rdf:resource' => $person_uri));
219         $this->element('primaryTopic', array('rdf:resource' => $person_uri));
220         $this->elementEnd('PersonalProfileDocument');
221     }
222
223     function showMicrobloggingAccount($profile, $service=null, $useruri=null, $isSubscriber=false)
224     {
225         $attr = array();
226         if ($useruri) {
227             $attr['rdf:about'] = $useruri . '#acct';
228         }
229
230         // Their account
231         $this->elementStart('holdsAccount');
232         $this->elementStart('OnlineAccount', $attr);
233         if ($service) {
234             $this->element('accountServiceHomepage', array('rdf:resource' =>
235                                                            $service));
236         }
237         $this->element('accountName', null, $profile->nickname);
238         $this->element('accountProfilePage', array('rdf:resource' => $profile->profileurl));
239         if ($useruri) {
240             $this->element('sioc:account_of', array('rdf:resource'=>$useruri));
241         }
242
243         $person = array();
244
245         if ($isSubscriber) {
246              $this->element('sioc:follows', array('rdf:resource'=>$this->user->uri . '#acct'));
247         } else {
248             // Get people user is subscribed to
249             $sub = new Subscription();
250             $sub->subscriber = $profile->id;
251             $sub->whereAdd('subscriber != subscribed');
252
253             if ($sub->find()) {
254                 while ($sub->fetch()) {
255                     if (!empty($sub->token)) {
256                         $other = Remote_profile::staticGet('id', $sub->subscribed);
257                     } else {
258                         $other = User::staticGet('id', $sub->subscribed);
259                     }
260                     if (empty($other)) {
261                         common_debug('Got a bad subscription: '.print_r($sub,true));
262                         continue;
263                     }
264                     $this->element('sioc:follows', array('rdf:resource' => $other->uri.'#acct'));
265                     $person[$other->uri] = array(LISTENEE,
266                                                  $other->id,
267                                                  $other->nickname,
268                                                  (empty($sub->token)) ? 'User' : 'Remote_profile');
269                     $other->free();
270                     $other = null;
271                     unset($other);
272                 }
273             }
274
275             $sub->free();
276             $sub = null;
277             unset($sub);
278         }
279
280         $this->elementEnd('OnlineAccount');
281         $this->elementEnd('holdsAccount');
282
283         return $person;
284     }
285 }