]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/foaf.php
change function headers to K&R style
[quix0rs-gnu-social.git] / actions / foaf.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, 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 is_readonly()
29     {
30         return true;
31     }
32
33     function handle($args)
34     {
35         parent::handle($args);
36
37         $nickname = $this->trimmed('nickname');
38
39         $user = User::staticGet('nickname', $nickname);
40
41         if (!$user) {
42             common_user_error(_('No such user.'), 404);
43             return;
44         }
45
46         $profile = $user->getProfile();
47
48         if (!$profile) {
49             common_server_error(_('User has no profile.'), 500);
50             return;
51         }
52
53         header('Content-Type: application/rdf+xml');
54
55         common_start_xml();
56         common_element_start('rdf:RDF', array('xmlns:rdf' =>
57                                               'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
58                                               'xmlns:rdfs' =>
59                                               'http://www.w3.org/2000/01/rdf-schema#',
60                                               'xmlns:geo' =>
61                                               'http://www.w3.org/2003/01/geo/wgs84_pos#',
62                                               'xmlns' => 'http://xmlns.com/foaf/0.1/'));
63
64         # This is the document about the user
65
66         $this->show_ppd('', $user->uri);
67
68         # XXX: might not be a person
69         common_element_start('Person', array('rdf:about' =>
70                                              $user->uri));
71         common_element('mbox_sha1sum', null, sha1('mailto:' . $user->email));
72         if ($profile->fullname) {
73             common_element('name', null, $profile->fullname);
74         }
75         if ($profile->homepage) {
76             common_element('homepage', array('rdf:resource' => $profile->homepage));
77         }
78         if ($profile->bio) {
79             common_element('rdfs:comment', null, $profile->bio);
80         }
81         # XXX: more structured location data
82         if ($profile->location) {
83             common_element_start('based_near');
84             common_element_start('geo:SpatialThing');
85             common_element('name', null, $profile->location);
86             common_element_end('geo:SpatialThing');
87             common_element_end('based_near');
88         }
89
90         $this->show_microblogging_account($profile, common_root_url());
91
92         $avatar = $profile->getOriginalAvatar();
93
94         if ($avatar) {
95             common_element_start('img');
96             common_element_start('Image', array('rdf:about' => $avatar->url));
97             foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
98                 $scaled = $profile->getAvatar($size);
99                 if (!$scaled->original) { # sometimes the original has one of our scaled sizes
100                     common_element_start('thumbnail');
101                     common_element('Image', array('rdf:about' => $scaled->url));
102                     common_element_end('thumbnail');
103                 }
104             }
105             common_element_end('Image');
106             common_element_end('img');
107         }
108
109         # Get people user is subscribed to
110
111         $person = array();
112
113         $sub = new Subscription();
114         $sub->subscriber = $profile->id;
115         $sub->whereAdd('subscriber != subscribed');
116         
117         if ($sub->find()) {
118             while ($sub->fetch()) {
119                 if ($sub->token) {
120                     $other = Remote_profile::staticGet('id', $sub->subscribed);
121                 } else {
122                     $other = User::staticGet('id', $sub->subscribed);
123                 }
124                 if (!$other) {
125                     common_debug('Got a bad subscription: '.print_r($sub,TRUE));
126                     continue;
127                 }
128                 common_element('knows', array('rdf:resource' => $other->uri));
129                 $person[$other->uri] = array(LISTENEE, $other);
130             }
131         }
132
133         # Get people who subscribe to user
134
135         $sub = new Subscription();
136         $sub->subscribed = $profile->id;
137         $sub->whereAdd('subscriber != subscribed');
138
139         if ($sub->find()) {
140             while ($sub->fetch()) {
141                 if ($sub->token) {
142                     $other = Remote_profile::staticGet('id', $sub->subscriber);
143                 } else {
144                     $other = User::staticGet('id', $sub->subscriber);
145                 }
146                 if (!$other) {
147                     common_debug('Got a bad subscription: '.print_r($sub,TRUE));
148                     continue;
149                 }
150                 if (array_key_exists($other->uri, $person)) {
151                     $person[$other->uri][0] = BOTH;
152                 } else {
153                     $person[$other->uri] = array(LISTENER, $other);
154                 }
155             }
156         }
157
158         common_element_end('Person');
159
160         foreach ($person as $uri => $p) {
161             $foaf_url = null;
162             if ($p[1] instanceof User) {
163                 $foaf_url = common_local_url('foaf', array('nickname' => $p[1]->nickname));
164             }
165             $profile = Profile::staticGet($p[1]->id);
166             common_element_start('Person', array('rdf:about' => $uri));
167             if ($p[0] == LISTENER || $p[0] == BOTH) {
168                 common_element('knows', array('rdf:resource' => $user->uri));
169             }
170             $this->show_microblogging_account($profile, ($p[1] instanceof User) ?
171                                               common_root_url() : null);
172             if ($foaf_url) {
173                 common_element('rdfs:seeAlso', array('rdf:resource' => $foaf_url));
174             }
175             common_element_end('Person');
176             if ($foaf_url) {
177                 $this->show_ppd($foaf_url, $uri);
178             }
179         }
180
181         common_element_end('rdf:RDF');
182     }
183
184     function show_ppd($foaf_url, $person_uri)
185     {
186         common_element_start('PersonalProfileDocument', array('rdf:about' => $foaf_url));
187         common_element('maker', array('rdf:resource' => $person_uri));
188         common_element('primaryTopic', array('rdf:resource' => $person_uri));
189         common_element_end('PersonalProfileDocument');
190     }
191
192     function show_microblogging_account($profile, $service=null)
193     {
194         # Their account
195         common_element_start('holdsAccount');
196         common_element_start('OnlineAccount');
197         if ($service) {
198             common_element('accountServiceHomepage', array('rdf:resource' =>
199                                                            $service));
200         }
201         common_element('accountName', null, $profile->nickname);
202         common_element('homepage', array('rdf:resource' => $profile->profileurl));
203         common_element_end('OnlineAccount');
204         common_element_end('holdsAccount');
205     }
206 }