foaf.php 11.9 KB
Newer Older
Evan Prodromou's avatar
Evan Prodromou committed
1 2
<?php
/*
3
 * StatusNet - the distributed open-source microblogging tool
4
 * Copyright (C) 2008, 2009, StatusNet, Inc.
Evan Prodromou's avatar
Evan Prodromou committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

20
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
Evan Prodromou's avatar
Evan Prodromou committed
21 22 23 24 25

define('LISTENER', 1);
define('LISTENEE', -1);
define('BOTH', 0);

26
// @todo XXX: Documentation missing.
27 28
class FoafAction extends Action
{
29
    function isReadOnly($args)
30
    {
31 32 33
        return true;
    }

Evan Prodromou's avatar
Evan Prodromou committed
34
    function prepare($args)
35
    {
Evan Prodromou's avatar
Evan Prodromou committed
36
        parent::prepare($args);
37 38 39 40

        $nickname_arg = $this->arg('nickname');

        if (empty($nickname_arg)) {
41
            // TRANS: Client error displayed when requesting Friends of a Friend feed without providing a user nickname.
42 43 44 45 46 47 48 49 50 51 52 53 54 55
            $this->clientError(_('No such user.'), 404);
            return false;
        }

        $this->nickname = common_canonical_nickname($nickname_arg);

        // Permanent redirect on non-canonical nickname

        if ($nickname_arg != $this->nickname) {
            common_redirect(common_local_url('foaf',
                                             array('nickname' => $this->nickname)),
                            301);
            return false;
        }
56

Evan Prodromou's avatar
Evan Prodromou committed
57
        $this->user = User::staticGet('nickname', $this->nickname);
58

Evan Prodromou's avatar
Evan Prodromou committed
59
        if (!$this->user) {
60
            // TRANS: Client error displayed when requesting Friends of a Friend feed for an object that is not a user.
61
            $this->clientError(_('No such user.'), 404);
Evan Prodromou's avatar
Evan Prodromou committed
62
            return false;
63 64
        }

Evan Prodromou's avatar
Evan Prodromou committed
65
        $this->profile = $this->user->getProfile();
66

Evan Prodromou's avatar
Evan Prodromou committed
67
        if (!$this->profile) {
68
            // TRANS: Error message displayed when referring to a user without a profile.
69
            $this->serverError(_('User has no profile.'), 500);
Evan Prodromou's avatar
Evan Prodromou committed
70
            return false;
71 72
        }

Evan Prodromou's avatar
Evan Prodromou committed
73 74 75 76 77 78 79
        return true;
    }

    function handle($args)
    {
        parent::handle($args);

80 81
        header('Content-Type: application/rdf+xml');

Evan Prodromou's avatar
Evan Prodromou committed
82
        $this->startXML();
83
        $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
84 85 86 87 88
                                              'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
                                              'xmlns:rdfs' =>
                                              'http://www.w3.org/2000/01/rdf-schema#',
                                              'xmlns:geo' =>
                                              'http://www.w3.org/2003/01/geo/wgs84_pos#',
89 90 91 92
                                              'xmlns:bio' =>
                                              'http://purl.org/vocab/bio/0.1/',
                                              'xmlns:sioc' =>
                                              'http://rdfs.org/sioc/ns#',
93 94
                                              'xmlns' => 'http://xmlns.com/foaf/0.1/'));

Evan Prodromou's avatar
Evan Prodromou committed
95
        // This is the document about the user
96

Evan Prodromou's avatar
Evan Prodromou committed
97
        $this->showPpd('', $this->user->uri);
98

99 100
        // Would be nice to tell if they were a Person or not (e.g. a #person usertag?)
        $this->elementStart('Agent', array('rdf:about' =>
Evan Prodromou's avatar
Evan Prodromou committed
101
                                             $this->user->uri));
102 103 104
        if ($this->user->email) {
            $this->element('mbox_sha1sum', null, sha1('mailto:' . $this->user->email));
        }
Evan Prodromou's avatar
Evan Prodromou committed
105 106
        if ($this->profile->fullname) {
            $this->element('name', null, $this->profile->fullname);
107
        }
Evan Prodromou's avatar
Evan Prodromou committed
108 109
        if ($this->profile->homepage) {
            $this->element('homepage', array('rdf:resource' => $this->profile->homepage));
110
        }
111 112 113
        if ($this->profile->profileurl) {
            $this->element('weblog', array('rdf:resource' => $this->profile->profileurl));
        }
Evan Prodromou's avatar
Evan Prodromou committed
114
        if ($this->profile->bio) {
115
            $this->element('bio:olb', null, $this->profile->bio);
116
        }
117

118 119 120 121 122 123 124
        $location = $this->profile->getLocation();
        if ($location) {
            $attr = array();
            if ($location->getRdfURL()) {
                $attr['rdf:about'] = $location->getRdfURL();
            }
            $location_name = $location->getName();
125

126
            $this->elementStart('based_near');
127 128 129 130 131 132 133 134
            $this->elementStart('geo:SpatialThing', $attr);
            if ($location_name) {
                $this->element('name', null, $location_name);
            }
            if ($location->lat) {
                $this->element('geo:lat', null, $location->lat);
            }
            if ($location->lon) {
135
                $this->element('geo:long', null, $location->lon);
136 137 138 139
            }
            if ($location->getURL()) {
                $this->element('page', array('rdf:resource'=>$location->getURL()));
            }
140 141
            $this->elementEnd('geo:SpatialThing');
            $this->elementEnd('based_near');
142 143
        }

Evan Prodromou's avatar
Evan Prodromou committed
144
        $avatar = $this->profile->getOriginalAvatar();
145
        if ($avatar) {
146 147
            $this->elementStart('img');
            $this->elementStart('Image', array('rdf:about' => $avatar->url));
148
            foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
Evan Prodromou's avatar
Evan Prodromou committed
149 150
                $scaled = $this->profile->getAvatar($size);
                if (!$scaled->original) { // sometimes the original has one of our scaled sizes
151 152 153
                    $this->elementStart('thumbnail');
                    $this->element('Image', array('rdf:about' => $scaled->url));
                    $this->elementEnd('thumbnail');
154 155
                }
            }
156 157
            $this->elementEnd('Image');
            $this->elementEnd('img');
158 159
        }

160
        $person = $this->showMicrobloggingAccount($this->profile,
161 162 163
                                     common_root_url(), $this->user->uri,
                                     /*$fetchSubscriptions*/true,
                                     /*$isSubscriber*/false);
164

Evan Prodromou's avatar
Evan Prodromou committed
165
        // Get people who subscribe to user
166 167

        $sub = new Subscription();
Evan Prodromou's avatar
Evan Prodromou committed
168
        $sub->subscribed = $this->profile->id;
169 170 171 172
        $sub->whereAdd('subscriber != subscribed');

        if ($sub->find()) {
            while ($sub->fetch()) {
173 174
                $profile = Profile::staticGet('id', $sub->subscriber);
                if (empty($profile)) {
Evan Prodromou's avatar
Evan Prodromou committed
175
                    common_debug('Got a bad subscription: '.print_r($sub,true));
176 177
                    continue;
                }
178 179 180 181
                $user = $profile->getUser();
                $other_uri = $profile->getUri();
                if (array_key_exists($other_uri, $person)) {
                    $person[$other_uri][0] = BOTH;
182
                } else {
183 184 185 186
                    $person[$other_uri] = array(LISTENER,
                                                $profile->id,
                                                $profile->nickname,
                                                $user ? 'local' : 'remote');
187
                }
188
                unset($profile);
189 190 191
            }
        }

192 193
        unset($sub);

194
        foreach ($person as $uri => $p) {
195
            list($type, $id, $nickname, $local) = $p;
196 197 198 199
            if ($type == BOTH) {
                $this->element('knows', array('rdf:resource' => $uri));
            }
        }
200

201 202
        $this->elementEnd('Agent');

203 204

        foreach ($person as $uri => $p) {
Evan Prodromou's avatar
Evan Prodromou committed
205
            $foaf_url = null;
206 207
            list($type, $id, $nickname, $local) = $p;
            if ($local == 'local') {
208
                $foaf_url = common_local_url('foaf', array('nickname' => $nickname));
209
            }
210
            $profile = Profile::staticGet($id);
211 212
            $this->elementStart('Agent', array('rdf:about' => $uri));
            if ($type == BOTH) {
Evan Prodromou's avatar
Evan Prodromou committed
213
                $this->element('knows', array('rdf:resource' => $this->user->uri));
214
            }
215
            $this->showMicrobloggingAccount($profile,
216
                                   ($local == 'local') ? common_root_url() : null,
217
                                   $uri,
218 219
                                   /*$fetchSubscriptions*/false,
                                   /*$isSubscriber*/($type == LISTENER || $type == BOTH));
220
            if ($foaf_url) {
221
                $this->element('rdfs:seeAlso', array('rdf:resource' => $foaf_url));
222
            }
223
            $this->elementEnd('Agent');
224
            if ($foaf_url) {
Evan Prodromou's avatar
Evan Prodromou committed
225
                $this->showPpd($foaf_url, $uri);
226
            }
227 228 229
            $profile->free();
            $profile = null;
            unset($profile);
230 231
        }

232
        $this->elementEnd('rdf:RDF');
Evan Prodromou's avatar
Evan Prodromou committed
233
        $this->endXML();
234 235
    }

Evan Prodromou's avatar
Evan Prodromou committed
236
    function showPpd($foaf_url, $person_uri)
237
    {
238 239 240 241
        $this->elementStart('PersonalProfileDocument', array('rdf:about' => $foaf_url));
        $this->element('maker', array('rdf:resource' => $person_uri));
        $this->element('primaryTopic', array('rdf:resource' => $person_uri));
        $this->elementEnd('PersonalProfileDocument');
242 243
    }

244 245
    /**
     * Output FOAF <account> bit for the given profile.
246
     *
247 248 249 250 251 252
     * @param Profile $profile
     * @param mixed $service Root URL of this StatusNet instance for a local
     *                       user, otherwise null.
     * @param mixed $useruri URI string for the referenced profile..
     * @param boolean $fetchSubscriptions Should we load and list all their subscriptions?
     * @param boolean $isSubscriber if not fetching subs, we can still mark the user as following the current page.
253
     *
254 255 256 257
     * @return array if $fetchSubscribers is set, return a list of info on those
     *               subscriptions.
     */
    function showMicrobloggingAccount($profile, $service=null, $useruri=null, $fetchSubscriptions=false, $isSubscriber=false)
258
    {
259 260 261 262 263
        $attr = array();
        if ($useruri) {
            $attr['rdf:about'] = $useruri . '#acct';
        }

Evan Prodromou's avatar
Evan Prodromou committed
264
        // Their account
265
        $this->elementStart('account');
266
        $this->elementStart('OnlineAccount', $attr);
267
        if ($service) {
268
            $this->element('accountServiceHomepage', array('rdf:resource' =>
269 270
                                                           $service));
        }
271
        $this->element('accountName', null, $profile->nickname);
272 273 274 275 276 277 278
        $this->element('accountProfilePage', array('rdf:resource' => $profile->profileurl));
        if ($useruri) {
            $this->element('sioc:account_of', array('rdf:resource'=>$useruri));
        }

        $person = array();

279
        if ($fetchSubscriptions) {
280 281 282 283 284 285 286
            // Get people user is subscribed to
            $sub = new Subscription();
            $sub->subscriber = $profile->id;
            $sub->whereAdd('subscriber != subscribed');

            if ($sub->find()) {
                while ($sub->fetch()) {
287 288
                    $profile = Profile::staticGet('id', $sub->subscribed);
                    if (empty($profile)) {
289 290 291
                        common_debug('Got a bad subscription: '.print_r($sub,true));
                        continue;
                    }
292 293 294 295 296 297 298
                    $user = $profile->getUser();
                    $other_uri = $profile->getUri();
                    $this->element('sioc:follows', array('rdf:resource' => $other_uri.'#acct'));
                    $person[$other_uri] = array(LISTENEE,
                                                $profile->id,
                                                $profile->nickname,
                                                $user ? 'local' : 'remote');
299
                    unset($profile);
300 301 302 303
                }
            }

            unset($sub);
304 305 306
        } else if ($isSubscriber) {
            // Just declare that they follow the user whose FOAF we're showing.
            $this->element('sioc:follows', array('rdf:resource' => $this->user->uri . '#acct'));
307 308
        }

309
        $this->elementEnd('OnlineAccount');
310
        $this->elementEnd('account');
311 312

        return $person;
313
    }
Evan Prodromou's avatar
Evan Prodromou committed
314
}