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