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