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