foaf.php 11.8 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/>.
 */

mmn's avatar
mmn committed
20
if (!defined('GNUSOCIAL')) { 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.
mmn's avatar
mmn committed
27
class FoafAction extends ManagedAction
28
{
29
    function isReadOnly($args)
30
    {
31 32 33
        return true;
    }

mmn's avatar
mmn committed
34
    protected function doPreparation()
35
    {
36 37 38
        $nickname_arg = $this->arg('nickname');

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

        $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);
        }
52

53
        $this->user = User::getKV('nickname', $this->nickname);
54

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

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

Evan Prodromou's avatar
Evan Prodromou committed
62
        if (!$this->profile) {
63
            // TRANS: Error message displayed when referring to a user without a profile.
64
            $this->serverError(_('User has no profile.'), 500);
65 66
        }

Evan Prodromou's avatar
Evan Prodromou committed
67 68 69
        return true;
    }

mmn's avatar
mmn committed
70
    public function showPage()
Evan Prodromou's avatar
Evan Prodromou committed
71
    {
72 73
        header('Content-Type: application/rdf+xml');

Evan Prodromou's avatar
Evan Prodromou committed
74
        $this->startXML();
75
        $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
76 77 78 79 80
                                              '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#',
81 82 83 84
                                              'xmlns:bio' =>
                                              'http://purl.org/vocab/bio/0.1/',
                                              'xmlns:sioc' =>
                                              'http://rdfs.org/sioc/ns#',
85 86
                                              'xmlns' => 'http://xmlns.com/foaf/0.1/'));

Evan Prodromou's avatar
Evan Prodromou committed
87
        // This is the document about the user
88

89
        $this->showPpd('', $this->user->getUri());
90

91
        // Would be nice to tell if they were a Person or not (e.g. a #person usertag?)
92
        $this->elementStart('Agent', array('rdf:about' => $this->user->getUri()));
93
        if (common_config('foaf', 'mbox_sha1sum') && $this->user->email) {
94 95
            $this->element('mbox_sha1sum', null, sha1('mailto:' . $this->user->email));
        }
Evan Prodromou's avatar
Evan Prodromou committed
96 97
        if ($this->profile->fullname) {
            $this->element('name', null, $this->profile->fullname);
98
        }
Evan Prodromou's avatar
Evan Prodromou committed
99 100
        if ($this->profile->homepage) {
            $this->element('homepage', array('rdf:resource' => $this->profile->homepage));
101
        }
102 103 104
        if ($this->profile->profileurl) {
            $this->element('weblog', array('rdf:resource' => $this->profile->profileurl));
        }
Evan Prodromou's avatar
Evan Prodromou committed
105
        if ($this->profile->bio) {
106
            $this->element('bio:olb', null, $this->profile->bio);
107
        }
108

109 110 111 112 113 114 115
        $location = $this->profile->getLocation();
        if ($location) {
            $attr = array();
            if ($location->getRdfURL()) {
                $attr['rdf:about'] = $location->getRdfURL();
            }
            $location_name = $location->getName();
116

117
            $this->elementStart('based_near');
118 119 120 121 122 123 124 125
            $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) {
126
                $this->element('geo:long', null, $location->lon);
127 128 129 130
            }
            if ($location->getURL()) {
                $this->element('page', array('rdf:resource'=>$location->getURL()));
            }
131 132
            $this->elementEnd('geo:SpatialThing');
            $this->elementEnd('based_near');
133 134
        }

135
        try {
136
            $avatar = Avatar::getUploaded($this->profile);
137
            $this->elementStart('img');
138
            $this->elementStart('Image', array('rdf:about' => $avatar->displayUrl()));
139
            foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
140
                try {
141
                    $scaled = $this->profile->getAvatar($size);
142
                    $this->elementStart('thumbnail');
143
                    $this->element('Image', array('rdf:about' => $scaled->displayUrl()));
144
                    $this->elementEnd('thumbnail');
145 146
                } catch (Exception $e) {
                    // This avatar did not exist
147 148
                }
            }
149 150
            $this->elementEnd('Image');
            $this->elementEnd('img');
151
        } catch (NoAvatarException $e) {
152
            // No avatar for this user!
153 154
        }

155
        $person = $this->showMicrobloggingAccount($this->profile,
156
                                     common_root_url(), $this->user->getUri(),
157 158
                                     /*$fetchSubscriptions*/true,
                                     /*$isSubscriber*/false);
159

Evan Prodromou's avatar
Evan Prodromou committed
160
        // Get people who subscribe to user
161 162

        $sub = new Subscription();
Evan Prodromou's avatar
Evan Prodromou committed
163
        $sub->subscribed = $this->profile->id;
164 165 166 167
        $sub->whereAdd('subscriber != subscribed');

        if ($sub->find()) {
            while ($sub->fetch()) {
168
                $profile = Profile::getKV('id', $sub->subscriber);
169
                if (!$profile instanceof Profile) {
Evan Prodromou's avatar
Evan Prodromou committed
170
                    common_debug('Got a bad subscription: '.print_r($sub,true));
171 172
                    continue;
                }
173 174 175
                $other_uri = $profile->getUri();
                if (array_key_exists($other_uri, $person)) {
                    $person[$other_uri][0] = BOTH;
176
                } else {
177 178 179
                    $person[$other_uri] = array(LISTENER,
                                                $profile->id,
                                                $profile->nickname,
180
                                                $profile->isLocal() ? 'local' : 'remote');
181
                }
182
                unset($profile);
183 184 185
            }
        }

186 187
        unset($sub);

188
        foreach ($person as $uri => $p) {
189
            list($type, $id, $nickname, $local) = $p;
190 191 192 193
            if ($type == BOTH) {
                $this->element('knows', array('rdf:resource' => $uri));
            }
        }
194

195 196
        $this->elementEnd('Agent');

197 198

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

226
        $this->elementEnd('rdf:RDF');
Evan Prodromou's avatar
Evan Prodromou committed
227
        $this->endXML();
228 229
    }

Evan Prodromou's avatar
Evan Prodromou committed
230
    function showPpd($foaf_url, $person_uri)
231
    {
232 233 234 235
        $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');
236 237
    }

238 239
    /**
     * Output FOAF <account> bit for the given profile.
240
     *
241 242 243 244 245 246
     * @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.
247
     *
248 249 250 251
     * @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)
252
    {
253 254 255 256 257
        $attr = array();
        if ($useruri) {
            $attr['rdf:about'] = $useruri . '#acct';
        }

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

        $person = array();

273
        if ($fetchSubscriptions) {
274 275 276 277 278 279 280
            // Get people user is subscribed to
            $sub = new Subscription();
            $sub->subscriber = $profile->id;
            $sub->whereAdd('subscriber != subscribed');

            if ($sub->find()) {
                while ($sub->fetch()) {
281
                    $profile = Profile::getKV('id', $sub->subscribed);
282
                    if (empty($profile)) {
283 284 285
                        common_debug('Got a bad subscription: '.print_r($sub,true));
                        continue;
                    }
286 287 288 289 290
                    $other_uri = $profile->getUri();
                    $this->element('sioc:follows', array('rdf:resource' => $other_uri.'#acct'));
                    $person[$other_uri] = array(LISTENEE,
                                                $profile->id,
                                                $profile->nickname,
291
                                                $profile->isLocal() ? 'local' : 'remote');
292
                    unset($profile);
293 294 295 296
                }
            }

            unset($sub);
297 298
        } else if ($isSubscriber) {
            // Just declare that they follow the user whose FOAF we're showing.
299
            $this->element('sioc:follows', array('rdf:resource' => $this->user->getUri() . '#acct'));
300 301
        }

302
        $this->elementEnd('OnlineAccount');
303
        $this->elementEnd('account');
304 305

        return $person;
306
    }
Evan Prodromou's avatar
Evan Prodromou committed
307
}