foaf.php 10.4 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 27
class FoafAction extends Action
{
28
    function isReadOnly($args)
29
    {
30 31 32
        return true;
    }

Evan Prodromou's avatar
Evan Prodromou committed
33
    function prepare($args)
34
    {
Evan Prodromou's avatar
Evan Prodromou committed
35
        parent::prepare($args);
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

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

        if (empty($nickname_arg)) {
            $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;
        }
54

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

Evan Prodromou's avatar
Evan Prodromou committed
57
        if (!$this->user) {
58
            $this->clientError(_('No such user.'), 404);
Evan Prodromou's avatar
Evan Prodromou committed
59
            return false;
60 61
        }

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

Evan Prodromou's avatar
Evan Prodromou committed
64
        if (!$this->profile) {
65
            $this->serverError(_('User has no profile.'), 500);
Evan Prodromou's avatar
Evan Prodromou committed
66
            return false;
67 68
        }

Evan Prodromou's avatar
Evan Prodromou committed
69 70 71 72 73 74 75
        return true;
    }

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

76 77
        header('Content-Type: application/rdf+xml');

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

Evan Prodromou's avatar
Evan Prodromou committed
91
        // This is the document about the user
92

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

95 96
        // 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
97 98 99 100
                                             $this->user->uri));
        $this->element('mbox_sha1sum', null, sha1('mailto:' . $this->user->email));
        if ($this->profile->fullname) {
            $this->element('name', null, $this->profile->fullname);
101
        }
Evan Prodromou's avatar
Evan Prodromou committed
102 103
        if ($this->profile->homepage) {
            $this->element('homepage', array('rdf:resource' => $this->profile->homepage));
104
        }
105 106 107
        if ($this->profile->profileurl) {
            $this->element('weblog', array('rdf:resource' => $this->profile->profileurl));
        }
Evan Prodromou's avatar
Evan Prodromou committed
108
        if ($this->profile->bio) {
109
            $this->element('bio:olb', null, $this->profile->bio);
110
        }
Evan Prodromou's avatar
Evan Prodromou committed
111 112
        // XXX: more structured location data
        if ($this->profile->location) {
113 114
            $this->elementStart('based_near');
            $this->elementStart('geo:SpatialThing');
Evan Prodromou's avatar
Evan Prodromou committed
115
            $this->element('name', null, $this->profile->location);
116 117
            $this->elementEnd('geo:SpatialThing');
            $this->elementEnd('based_near');
118 119
        }

Evan Prodromou's avatar
Evan Prodromou committed
120
        $avatar = $this->profile->getOriginalAvatar();
121
        if ($avatar) {
122 123
            $this->elementStart('img');
            $this->elementStart('Image', array('rdf:about' => $avatar->url));
124
            foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
Evan Prodromou's avatar
Evan Prodromou committed
125 126
                $scaled = $this->profile->getAvatar($size);
                if (!$scaled->original) { // sometimes the original has one of our scaled sizes
127 128 129
                    $this->elementStart('thumbnail');
                    $this->element('Image', array('rdf:about' => $scaled->url));
                    $this->elementEnd('thumbnail');
130 131
                }
            }
132 133
            $this->elementEnd('Image');
            $this->elementEnd('img');
134 135
        }

136 137
        $person = $this->showMicrobloggingAccount($this->profile,
                                     common_root_url(), $this->user->uri, false);
138

Evan Prodromou's avatar
Evan Prodromou committed
139
        // Get people who subscribe to user
140 141

        $sub = new Subscription();
Evan Prodromou's avatar
Evan Prodromou committed
142
        $sub->subscribed = $this->profile->id;
143 144 145 146 147 148 149 150 151 152
        $sub->whereAdd('subscriber != subscribed');

        if ($sub->find()) {
            while ($sub->fetch()) {
                if ($sub->token) {
                    $other = Remote_profile::staticGet('id', $sub->subscriber);
                } else {
                    $other = User::staticGet('id', $sub->subscriber);
                }
                if (!$other) {
Evan Prodromou's avatar
Evan Prodromou committed
153
                    common_debug('Got a bad subscription: '.print_r($sub,true));
154 155 156 157 158
                    continue;
                }
                if (array_key_exists($other->uri, $person)) {
                    $person[$other->uri][0] = BOTH;
                } else {
159 160 161 162
                    $person[$other->uri] = array(LISTENER,
                                                 $other->id,
                                                 $other->nickname,
                                                 (empty($sub->token)) ? 'User' : 'Remote_profile');
163
                }
164 165 166
                $other->free();
                $other = null;
                unset($other);
167 168 169
            }
        }

170 171 172 173
        $sub->free();
        $sub = null;
        unset($sub);

174 175 176 177 178 179 180 181 182
        foreach ($person as $uri => $p) {
            list($type, $id, $nickname, $cls) = $p;
            if ($type == BOTH) {
                $this->element('knows', array('rdf:resource' => $uri));
            }
        }
        
        $this->elementEnd('Agent');

183 184

        foreach ($person as $uri => $p) {
Evan Prodromou's avatar
Evan Prodromou committed
185
            $foaf_url = null;
186 187 188
            list($type, $id, $nickname, $cls) = $p;
            if ($cls == 'User') {
                $foaf_url = common_local_url('foaf', array('nickname' => $nickname));
189
            }
190
            $profile = Profile::staticGet($id);
191 192
            $this->elementStart('Agent', array('rdf:about' => $uri));
            if ($type == BOTH) {
Evan Prodromou's avatar
Evan Prodromou committed
193
                $this->element('knows', array('rdf:resource' => $this->user->uri));
194
            }
195 196 197 198
            $this->showMicrobloggingAccount($profile,
                                   ($cls == 'User') ? common_root_url() : null,
                                   $uri,
                                   true);
199
            if ($foaf_url) {
200
                $this->element('rdfs:seeAlso', array('rdf:resource' => $foaf_url));
201
            }
202
            $this->elementEnd('Agent');
203
            if ($foaf_url) {
Evan Prodromou's avatar
Evan Prodromou committed
204
                $this->showPpd($foaf_url, $uri);
205
            }
206 207 208
            $profile->free();
            $profile = null;
            unset($profile);
209 210
        }

211
        $this->elementEnd('rdf:RDF');
Evan Prodromou's avatar
Evan Prodromou committed
212
        $this->endXML();
213 214
    }

Evan Prodromou's avatar
Evan Prodromou committed
215
    function showPpd($foaf_url, $person_uri)
216
    {
217 218 219 220
        $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');
221 222
    }

223
    function showMicrobloggingAccount($profile, $service=null, $useruri=null, $isSubscriber=false)
224
    {
225 226 227 228 229
        $attr = array();
        if ($useruri) {
            $attr['rdf:about'] = $useruri . '#acct';
        }

Evan Prodromou's avatar
Evan Prodromou committed
230
        // Their account
231
        $this->elementStart('holdsAccount');
232
        $this->elementStart('OnlineAccount', $attr);
233
        if ($service) {
234
            $this->element('accountServiceHomepage', array('rdf:resource' =>
235 236
                                                           $service));
        }
237
        $this->element('accountName', null, $profile->nickname);
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
        $this->element('accountProfilePage', array('rdf:resource' => $profile->profileurl));
        if ($useruri) {
            $this->element('sioc:account_of', array('rdf:resource'=>$useruri));
        }

        $person = array();

        if ($isSubscriber) {
             $this->element('sioc:follows', array('rdf:resource'=>$this->user->uri . '#acct'));
        } else {
            // Get people user is subscribed to
            $sub = new Subscription();
            $sub->subscriber = $profile->id;
            $sub->whereAdd('subscriber != subscribed');

            if ($sub->find()) {
                while ($sub->fetch()) {
                    if (!empty($sub->token)) {
                        $other = Remote_profile::staticGet('id', $sub->subscribed);
                    } else {
                        $other = User::staticGet('id', $sub->subscribed);
                    }
                    if (empty($other)) {
                        common_debug('Got a bad subscription: '.print_r($sub,true));
                        continue;
                    }
                    $this->element('sioc:follows', array('rdf:resource' => $other->uri.'#acct'));
                    $person[$other->uri] = array(LISTENEE,
                                                 $other->id,
                                                 $other->nickname,
                                                 (empty($sub->token)) ? 'User' : 'Remote_profile');
                    $other->free();
                    $other = null;
                    unset($other);
                }
            }

            $sub->free();
            $sub = null;
            unset($sub);
        }

280 281
        $this->elementEnd('OnlineAccount');
        $this->elementEnd('holdsAccount');
282 283

        return $person;
284
    }
Evan Prodromou's avatar
Evan Prodromou committed
285
}