~ubuntu-branches/ubuntu/saucy/mediawiki-extensions/saucy

« back to all changes in this revision

Viewing changes to dist/mediawiki-extensions-openid/usr/share/mediawiki-extensions/openid/SpecialOpenIDServer.body.php

  • Committer: Bazaar Package Importer
  • Author(s): Romain Beauxis
  • Date: 2010-05-04 15:13:35 UTC
  • mfrom: (0.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20100504151335-54qeucg3ec108q28
Tags: 2.2
* Added Replaces:/Conflicts: to allow a proper upgrade.
Closes: #580066
* Fixed package descriptions.
Closes: #579667
* Patched mediawiki-extensions-fckeditor to make it work with
  php 5.3. The fix may not be perfect but at least it work.
  Not closing the bug (#579822) for now..

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Server.php -- Server side of OpenID site
 
4
 * Copyright 2006,2007 Internet Brands (http://www.internetbrands.com/)
 
5
 * Copyright 2007,2008 Evan Prodromou <evan@prodromou.name>
 
6
 *
 
7
 *  This program is free software; you can redistribute it and/or modify
 
8
 *  it under the terms of the GNU General Public License as published by
 
9
 *  the Free Software Foundation; either version 2 of the License, or
 
10
 *  (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 *  GNU General Public License for more details.
 
16
 *
 
17
 *  You should have received a copy of the GNU General Public License
 
18
 *  along with this program; if not, write to the Free Software
 
19
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 *
 
21
 * @author Evan Prodromou <evan@prodromou.name>
 
22
 * @addtogroup Extensions
 
23
 */
 
24
 
 
25
if (!defined('MEDIAWIKI'))
 
26
  exit(1);
 
27
 
 
28
require_once("Auth/OpenID/Server.php");
 
29
require_once("Auth/OpenID/Consumer.php");
 
30
 
 
31
# Special page for the server side of OpenID
 
32
# It has three major flavors:
 
33
# * no parameter is for external requests to validate a user.
 
34
# * 'Login' is we got a validation request but the
 
35
#   user wasn't logged in. We show them a form (see OpenIDServerLoginForm)
 
36
#   and they post the results, which go to OpenIDServerLogin
 
37
# * 'Trust' is when the user has logged in, but they haven't
 
38
#   specified whether it's OK to let the requesting site trust them.
 
39
#   If they haven't, we show them a form (see OpenIDServerTrustForm)
 
40
#   and let them post results which go to OpenIDServerTrust.
 
41
#
 
42
# OpenID has its own modes; we only handle two of them ('check_setup' and
 
43
# 'check_immediate') and let the OpenID libraries handle the rest.
 
44
#
 
45
# Output may be just a redirect, or a form if we need info.
 
46
 
 
47
class SpecialOpenIDServer extends SpecialOpenID {
 
48
 
 
49
        function SpecialOpenIDServer() {
 
50
                SpecialPage::SpecialPage("OpenIDServer", '', false);
 
51
        }
 
52
 
 
53
        function execute($par) {
 
54
 
 
55
                global $wgOut, $wgOpenIDClientOnly;
 
56
 
 
57
                wfLoadExtensionMessages( 'OpenID' );
 
58
 
 
59
                $this->setHeaders();
 
60
 
 
61
                # No server functionality if this site is only a client
 
62
                # Note: special page is un-registered if this flag is set,
 
63
                # so it'd be unusual to get here.
 
64
                
 
65
                if ($wgOpenIDClientOnly) {
 
66
                        $wgOut->showErrorPage('openiderror', 'openidclientonlytext');
 
67
                        return;
 
68
                }
 
69
                
 
70
                $server =& $this->getServer();
 
71
 
 
72
                switch ($par) {
 
73
                 case 'Login':
 
74
                        list($request, $sreg) = $this->FetchValues();
 
75
                        $result = $this->serverLogin($request);
 
76
                        if ($result) {
 
77
                                if (is_string($result)) {
 
78
                                        $this->LoginForm($request, $result);
 
79
                                        return;
 
80
                                } else {
 
81
                                        $this->Response($server, $result);
 
82
                                        return;
 
83
                                }
 
84
                        }
 
85
                        break;
 
86
                 case 'Trust':
 
87
                        list($request, $sreg) = $this->FetchValues();
 
88
                        $result = $this->Trust($request, $sreg);
 
89
                        if ($result) {
 
90
                                if (is_string($result)) {
 
91
                                        $this->TrustForm($request, $sreg, $result);
 
92
                                        return;
 
93
                                } else {
 
94
                                        $this->Response($server, $result);
 
95
                                        return;
 
96
                                }
 
97
                        }
 
98
                        break;
 
99
                 default:
 
100
                        if (strlen($par)) {
 
101
                                wfDebug("OpenID: aborting in user validation because the parameter was empty\n");
 
102
                                $wgOut->showErrorPage('openiderror', 'openiderrortext');
 
103
                                return;
 
104
                        } else {
 
105
                                $method = $_SERVER['REQUEST_METHOD'];
 
106
                                $query = null;
 
107
                                if ($method == 'GET') {
 
108
                                        $query = $_GET;
 
109
                                } else {
 
110
                                        $query = $_POST;
 
111
                                }
 
112
 
 
113
                                $request = $server->decodeRequest();
 
114
                                $sreg = $this->SregFromQuery($query);
 
115
                                $response = NULL;
 
116
                                break;
 
117
                        }
 
118
                }
 
119
 
 
120
                if (!isset($request)) {
 
121
                        wfDebug("OpenID: aborting in user validation because the request was missing\n");
 
122
                        $wgOut->showErrorPage('openiderror', 'openiderrortext');
 
123
                        return;
 
124
                }
 
125
 
 
126
                global $wgUser;
 
127
 
 
128
                switch ($request->mode) {
 
129
                 case "checkid_setup":
 
130
                        $response = $this->Check($server, $request, $sreg, false);
 
131
                        break;
 
132
                 case "checkid_immediate":
 
133
                        $response = $this->Check($server, $request, $sreg, true);
 
134
                        break;
 
135
                 default:
 
136
                        # For all the other parts, just let the libs do it
 
137
                        $response =& $server->handleRequest($request);
 
138
                }
 
139
 
 
140
                # OpenIDServerCheck returns NULL if some output (like a form)
 
141
                # has been done
 
142
 
 
143
                if (isset($response)) {
 
144
                        # We're done; clear values
 
145
                        $this->ClearValues();
 
146
                        $this->Response($server, $response);
 
147
                }
 
148
        }
 
149
 
 
150
        # Returns the full URL of the special page; we need to pass it around
 
151
        # for some requests
 
152
 
 
153
        function Url() {
 
154
                $nt = Title::makeTitleSafe(NS_SPECIAL, 'OpenIDServer');
 
155
                if (isset($nt)) {
 
156
                        return $nt->getFullURL();
 
157
                } else {
 
158
                        return NULL;
 
159
                }
 
160
        }
 
161
 
 
162
        # Returns an Auth_OpenID_Server from the libraries. Utility.
 
163
 
 
164
        function getServer() {
 
165
                global $wgOpenIDServerStorePath,
 
166
                  $wgOpenIDServerStoreType;
 
167
 
 
168
                $store = $this->getOpenIDStore($wgOpenIDServerStoreType,
 
169
                                                                           'server',
 
170
                                                                           array('path' => $wgOpenIDServerStorePath));
 
171
 
 
172
                return new Auth_OpenID_Server($store, $this->serverUrl());
 
173
        }
 
174
 
 
175
        # Checks a validation request. $imm means don't run any UI.
 
176
        # Fairly meticulous and step-by step, and uses assertions
 
177
        # to point out assumptions at each step.
 
178
        #
 
179
        # XXX: this should probably be broken up into multiple functions for
 
180
        # clarity.
 
181
 
 
182
        function Check($server, $request, $sreg, $imm = true) {
 
183
 
 
184
                global $wgUser, $wgOut;
 
185
 
 
186
                assert(isset($wgUser) && isset($wgOut));
 
187
                assert(isset($server));
 
188
                assert(isset($request));
 
189
                assert(isset($sreg));
 
190
                assert(isset($imm) && is_bool($imm));
 
191
 
 
192
                # Is the passed identity URL a user page?
 
193
 
 
194
                $url = $request->identity;
 
195
 
 
196
                assert(isset($url) && strlen($url) > 0);
 
197
 
 
198
                $name = $this->UrlToUserName($url);
 
199
 
 
200
                if (!isset($name) || strlen($name) == 0) {
 
201
                        wfDebug("OpenID: '$url' not a user page.\n");
 
202
                        return $request->answer(false, $this->serverUrl());
 
203
                }
 
204
 
 
205
                assert(isset($name) && strlen($name) > 0);
 
206
 
 
207
                # Is there a logged in user?
 
208
 
 
209
                if ($wgUser->getId() == 0) {
 
210
                        wfDebug("OpenID: User not logged in.\n");
 
211
                        if ($imm) {
 
212
                                return $request->answer(false, $this->serverUrl());
 
213
                        } else {
 
214
                                # Bank these for later
 
215
                                $this->SaveValues($request, $sreg);
 
216
                                $this->LoginForm($request);
 
217
                                return NULL;
 
218
                        }
 
219
                }
 
220
 
 
221
                assert($wgUser->getId() != 0);
 
222
 
 
223
                # Is the user page for the logged-in user?
 
224
 
 
225
                $user = User::newFromName($name);
 
226
 
 
227
                if (!isset($user) ||
 
228
                        $user->getId() != $wgUser->getId()) {
 
229
                        wfDebug("OpenID: User from url not logged in user.\n");
 
230
                        return $request->answer(false, $this->serverUrl());
 
231
                }
 
232
 
 
233
                assert(isset($user) && $user->getId() == $wgUser->getId() && $user->getId() != 0);
 
234
 
 
235
                # Is the user an OpenID user?
 
236
 
 
237
                $openid = $this->getUserUrl($user);
 
238
 
 
239
                if (isset($openid) && strlen($openid) > 0) {
 
240
                        wfDebug("OpenID: Not one of our users; logs in with OpenID.\n");
 
241
                        return $request->answer(false, $this->serverUrl());
 
242
                }
 
243
 
 
244
            assert(is_array($sreg));
 
245
 
 
246
                # Does the request require sreg fields that the user has not specified?
 
247
 
 
248
                if (array_key_exists('required', $sreg)) {
 
249
                        $notFound = false;
 
250
                        foreach ($sreg['required'] as $reqfield) {
 
251
                                if (is_null($this->GetUserField($user, $reqfield))) {
 
252
                                        $notFound = true;
 
253
                                        break;
 
254
                                }
 
255
                        }
 
256
                        if ($notFound) {
 
257
                                wfDebug("OpenID: Consumer demands info we don't have.\n");
 
258
                                return $request->answer(false, $this->serverUrl());
 
259
                        }
 
260
                }
 
261
 
 
262
                # Trust check
 
263
 
 
264
                $trust_root = $request->trust_root;
 
265
 
 
266
                assert(isset($trust_root) && is_string($trust_root) && strlen($trust_root) > 0);
 
267
 
 
268
                $trust = $this->GetUserTrust($user, $trust_root);
 
269
 
 
270
                # Is there a trust record?
 
271
 
 
272
                if (is_null($trust)) {
 
273
                        wfDebug("OpenID: No trust record.\n");
 
274
                        if ($imm) {
 
275
                                return $request->answer(false, $this->serverUrl());
 
276
                        } else {
 
277
                                # Bank these for later
 
278
                                $this->SaveValues($request, $sreg);
 
279
                                $this->TrustForm($request, $sreg);
 
280
                                return NULL;
 
281
                        }
 
282
                }
 
283
 
 
284
            assert(!is_null($trust));
 
285
 
 
286
                # Is the trust record _not_ to allow trust?
 
287
                # NB: exactly equal
 
288
 
 
289
                if ($trust === false) {
 
290
                        wfDebug("OpenID: User specified not to allow trust.\n");
 
291
                        return $request->answer(false, $this->serverUrl());
 
292
                }
 
293
 
 
294
        assert(isset($trust) && is_array($trust));
 
295
 
 
296
                # Does the request require sreg fields that the user has
 
297
                # not allowed us to pass, or has not specified?
 
298
 
 
299
                if (array_key_exists('required', $sreg)) {
 
300
                        $notFound = false;
 
301
                        foreach ($sreg['required'] as $reqfield) {
 
302
                                if (!in_array($reqfield, $trust) ||
 
303
                                        is_null($this->GetUserField($user, $reqfield))) {
 
304
                                        $notFound = true;
 
305
                                        break;
 
306
                                }
 
307
                        }
 
308
                        if ($notFound) {
 
309
                                wfDebug("OpenID: Consumer demands info user doesn't want shared.\n");
 
310
                                return $request->answer(false, $this->serverUrl());
 
311
                        }
 
312
                }
 
313
 
 
314
            # assert(all required sreg fields are in $trust)
 
315
 
 
316
                # XXX: run a hook here to check
 
317
 
 
318
                # SUCCESS
 
319
 
 
320
                $response_fields = array_intersect(array_unique(array_merge($sreg['required'], $sreg['optional'])),
 
321
                                                                                   $trust);
 
322
 
 
323
                $response = $request->answer(true);
 
324
 
 
325
            assert(isset($response));
 
326
 
 
327
                foreach ($response_fields as $field) {
 
328
                        $value = $this->GetUserField($user, $field);
 
329
                        if (!is_null($value)) {
 
330
                                $response->addField('sreg', $field, $value);
 
331
                        }
 
332
                }
 
333
 
 
334
                return $response;
 
335
        }
 
336
 
 
337
        # Get the user's configured trust value for a particular trust root.
 
338
        # Returns one of three values:
 
339
        # * NULL -> no stored trust preferences
 
340
        # * false -> stored trust preference is not to trust
 
341
        # * array -> possibly empty array of allowed profile fields; trust is OK
 
342
 
 
343
        function GetUserTrust($user, $trust_root) {
 
344
                static $allFields = array('nickname', 'fullname', 'email', 'language');
 
345
                global $wgOpenIDServerForceAllowTrust;
 
346
 
 
347
                foreach ($wgOpenIDServerForceAllowTrust as $force) {
 
348
                        if (preg_match($force, $trust_root)) {
 
349
                                return $allFields;
 
350
                        }
 
351
                }
 
352
 
 
353
                $trust_array = $this->GetUserTrustArray($user);
 
354
 
 
355
                if (array_key_exists($trust_root, $trust_array)) {
 
356
                        return $trust_array[$trust_root];
 
357
                } else {
 
358
                        return null; # Unspecified trust
 
359
                }
 
360
        }
 
361
 
 
362
        function SetUserTrust(&$user, $trust_root, $value) {
 
363
 
 
364
                $trust_array = $this->GetUserTrustArray($user);
 
365
 
 
366
                if (is_null($value)) {
 
367
                        if (array_key_exists($trust_root, $trust_array)) {
 
368
                                unset($trust_array[$trust_root]);
 
369
                        }
 
370
                } else {
 
371
                        $trust_array[$trust_root] = $value;
 
372
                }
 
373
 
 
374
                $this->SetUserTrustArray($user, $trust_array);
 
375
        }
 
376
 
 
377
        function GetUserTrustArray($user) {
 
378
                $trust_array = array();
 
379
                $trust_str = $user->getOption('openid_trust');
 
380
                if (strlen($trust_str) > 0) {
 
381
                        $trust_records = explode("\x1E", $trust_str);
 
382
                        foreach ($trust_records as $record) {
 
383
                                $fields = explode("\x1F", $record);
 
384
                                $trust_root = array_shift($fields);
 
385
                                if (count($fields) == 1 && strcmp($fields[0], 'no') == 0) {
 
386
                                        $trust_array[$trust_root] = false;
 
387
                                } else {
 
388
                                        $fields = array_map('trim', $fields);
 
389
                                        $fields = array_filter($fields, array($this, 'ValidField'));
 
390
                                        $trust_array[$trust_root] = $fields;
 
391
                                }
 
392
                        }
 
393
                }
 
394
                return $trust_array;
 
395
        }
 
396
 
 
397
        function SetUserTrustArray(&$user, $arr) {
 
398
                $trust_records = array();
 
399
                foreach ($arr as $root => $value) {
 
400
                        if ($value === false) {
 
401
                                $record = implode("\x1F", array($root, 'no'));
 
402
                        } else if (is_array($value)) {
 
403
                                if (count($value) == 0) {
 
404
                                        $record = $root;
 
405
                                } else {
 
406
                                        $value = array_map('trim', $value);
 
407
                                        $value = array_filter($value, array($this, 'ValidField'));
 
408
                                        $record = implode("\x1F", array_merge(array($root), $value));
 
409
                                }
 
410
                        } else {
 
411
                                continue;
 
412
                        }
 
413
                        $trust_records[] = $record;
 
414
                }
 
415
                $trust_str = implode("\x1E", $trust_records);
 
416
                $user->setOption('openid_trust', $trust_str);
 
417
        }
 
418
 
 
419
        function ValidField($name) {
 
420
                # XXX: eventually add timezone
 
421
                static $fields = array('nickname', 'email', 'fullname', 'language');
 
422
                return in_array($name, $fields);
 
423
        }
 
424
 
 
425
        function SregFromQuery($query) {
 
426
                $sreg = array('required' => array(), 'optional' => array(),
 
427
                                          'policy_url' => NULL);
 
428
                if (array_key_exists('openid.sreg.required', $query)) {
 
429
                        $sreg['required'] = explode(',', $query['openid.sreg.required']);
 
430
                }
 
431
                if (array_key_exists('openid.sreg.optional', $query)) {
 
432
                        $sreg['optional'] = explode(',', $query['openid.sreg.optional']);
 
433
                }
 
434
                if (array_key_exists('openid.sreg.policy_url', $query)) {
 
435
                        $sreg['policy_url'] = $query['openid.sreg.policy_url'];
 
436
                }
 
437
                return $sreg;
 
438
        }
 
439
 
 
440
        function SetUserField(&$user, $field, $value) {
 
441
                switch ($field) {
 
442
                 case 'fullname':
 
443
                        $user->setRealName($value);
 
444
                        return true;
 
445
                        break;
 
446
                 case 'email':
 
447
                        # FIXME: deal with validation
 
448
                        $user->setEmail($value);
 
449
                        return true;
 
450
                        break;
 
451
                 case 'language':
 
452
                        $user->setOption('language', $value);
 
453
                        return true;
 
454
                        break;
 
455
                 default:
 
456
                        return false;
 
457
                }
 
458
        }
 
459
 
 
460
        function GetUserField($user, $field) {
 
461
                switch ($field) {
 
462
                 case 'nickname':
 
463
                        return $user->getName();
 
464
                        break;
 
465
                 case 'fullname':
 
466
                        return $user->getRealName();
 
467
                        break;
 
468
                 case 'email':
 
469
                        return $user->getEmail();
 
470
                        break;
 
471
                 case 'language':
 
472
                        return $user->getOption('language');
 
473
                        break;
 
474
                 default:
 
475
                        return NULL;
 
476
                }
 
477
        }
 
478
 
 
479
        function Response(&$server, &$response) {
 
480
                global $wgOut;
 
481
 
 
482
                assert(!is_null($server));
 
483
                assert(!is_null($response));
 
484
 
 
485
                $wgOut->disable();
 
486
 
 
487
                $wr =& $server->encodeResponse($response);
 
488
 
 
489
                assert(!is_null($wr));
 
490
 
 
491
                header("Status: " . $wr->code);
 
492
 
 
493
                foreach ($wr->headers as $k => $v) {
 
494
                        header("$k: $v");
 
495
                }
 
496
 
 
497
                print $wr->body;
 
498
 
 
499
                return;
 
500
        }
 
501
 
 
502
        function LoginForm($request, $msg = null) {
 
503
 
 
504
                global $wgOut, $wgUser;
 
505
 
 
506
                $url = $request->identity;
 
507
                $name = $this->UrlToUserName($url);
 
508
                $trust_root = $request->trust_root;
 
509
 
 
510
                $instructions = wfMsg('openidserverlogininstructions', $url, $name, $trust_root);
 
511
 
 
512
                $username = wfMsg('yourname');
 
513
                $password = wfMsg('yourpassword');
 
514
                $ok = wfMsg('ok');
 
515
                $cancel = wfMsg('cancel');
 
516
 
 
517
                if (isset($msg)) {
 
518
                        $wgOut->addHTML("<p class='error'>{$msg}</p>");
 
519
                }
 
520
 
 
521
                $sk = $wgUser->getSkin();
 
522
 
 
523
                $wgOut->addHTML("<p>{$instructions}</p>" .
 
524
                                                '<form action="' . $sk->makeSpecialUrl('OpenIDServer/Login') . '" method="POST">' .
 
525
                                                '<table>' .
 
526
                                                "<tr><td><label for='username'>{$username}</label></td>" .
 
527
                                                '    <td><span id="username">' . htmlspecialchars($name) . '</span></td></tr>' .
 
528
                                                "<tr><td><label for='password'>{$password}</label></td>" .
 
529
                                                '    <td><input type="password" name="wpPassword" size="32" value="" /></td></tr>' .
 
530
                                                "<tr><td colspan='2'><input type='submit' name='wpOK' value='{$ok}' /> <input type='submit' name='wpCancel' value='{$cancel}' /></td></tr>" .
 
531
                                                '</table>' .
 
532
                                                '</form>');
 
533
        }
 
534
 
 
535
        function SaveValues($request, $sreg) {
 
536
                global $wgSessionStarted, $wgUser;
 
537
 
 
538
                if (!$wgSessionStarted) {
 
539
                        $wgUser->SetupSession();
 
540
                }
 
541
 
 
542
                $_SESSION['openid_server_request'] = $request;
 
543
                $_SESSION['openid_server_sreg'] = $sreg;
 
544
 
 
545
                return true;
 
546
        }
 
547
 
 
548
        function FetchValues() {
 
549
                return array($_SESSION['openid_server_request'], $_SESSION['openid_server_sreg']);
 
550
        }
 
551
 
 
552
        function ClearValues() {
 
553
                unset($_SESSION['openid_server_request']);
 
554
                unset($_SESSION['openid_server_sreg']);
 
555
                return true;
 
556
        }
 
557
 
 
558
        function serverLogin($request) {
 
559
 
 
560
                global $wgRequest, $wgUser;
 
561
 
 
562
                assert(isset($request));
 
563
 
 
564
                assert(isset($wgRequest));
 
565
 
 
566
                if ($wgRequest->getCheck('wpCancel')) {
 
567
                        return $request->answer(false);
 
568
                }
 
569
 
 
570
                $password = $wgRequest->getText('wpPassword');
 
571
 
 
572
                if (!isset($password) || strlen($password) == 0) {
 
573
                        return wfMsg('wrongpasswordempty');
 
574
                }
 
575
 
 
576
                assert (isset($password) && strlen($password) > 0);
 
577
 
 
578
                $url = $request->identity;
 
579
 
 
580
                assert(isset($url) && is_string($url) && strlen($url) > 0);
 
581
 
 
582
                $name = $this->UrlToUserName($url);
 
583
 
 
584
                assert(isset($name) && is_string($name) && strlen($name) > 0);
 
585
 
 
586
                $user = User::newFromName($name);
 
587
 
 
588
                assert(isset($user));
 
589
 
 
590
                if (!$user->checkPassword($password)) {
 
591
                        return wfMsg('wrongpassword');
 
592
                } else {
 
593
                        $id = $user->getId();
 
594
                        $wgUser = $user;
 
595
                        $wgUser->SetupSession();
 
596
                        $wgUser->SetCookies();
 
597
                        wfRunHooks('UserLoginComplete', array(&$wgUser));
 
598
                        return false;
 
599
                }
 
600
        }
 
601
 
 
602
        function TrustForm($request, $sreg, $msg = NULL) {
 
603
 
 
604
                global $wgOut, $wgUser;
 
605
 
 
606
                $url = $request->identity;
 
607
                $name = $this->UrlToUserName($url);
 
608
                $trust_root = $request->trust_root;
 
609
 
 
610
                $instructions = wfMsg('openidtrustinstructions', $trust_root);
 
611
                $allow = wfMsg('openidallowtrust', $trust_root);
 
612
 
 
613
                if (is_null($sreg['policy_url'])) {
 
614
                        $policy = wfMsg('openidnopolicy');
 
615
                } else {
 
616
                        $policy = wfMsg('openidpolicy', $sreg['policy_url']);
 
617
                }
 
618
 
 
619
                if (isset($msg)) {
 
620
                        $wgOut->addHTML("<p class='error'>{$msg}</p>");
 
621
                }
 
622
 
 
623
                $ok = wfMsg('ok');
 
624
                $cancel = wfMsg('cancel');
 
625
 
 
626
                $sk = $wgUser->getSkin();
 
627
 
 
628
                $wgOut->addHTML("<p>{$instructions}</p>" .
 
629
                                                '<form action="' . $sk->makeSpecialUrl('OpenIDServer/Trust') . '" method="POST">' .
 
630
                                                '<input name="wpAllowTrust" type="checkbox" value="on" checked="checked" id="wpAllowTrust">' .
 
631
                                                '<label for="wpAllowTrust">' . $allow . '</label><br />');
 
632
 
 
633
                $fields = array_filter(array_unique(array_merge($sreg['optional'], $sreg['required'])),
 
634
                                                           array($this, 'ValidField'));
 
635
 
 
636
                if (count($fields) > 0) {
 
637
                        $wgOut->addHTML('<table>');
 
638
                        foreach ($fields as $field) {
 
639
                                $wgOut->addHTML("<tr>");
 
640
                                $wgOut->addHTML("<th><label for='wpAllow{$field}'>");
 
641
                                $wgOut->addHTML(wfMsg("openid$field"));
 
642
                                $wgOut->addHTML("</label></th>");
 
643
                                $value = $this->GetUserField($wgUser, $field);
 
644
                                $wgOut->addHTML("</td>");
 
645
                                $wgOut->addHTML("<td> " . ((is_null($value)) ? '' : $value) . "</td>");
 
646
                                $wgOut->addHTML("<td>" . ((in_array($field, $sreg['required'])) ? wfMsg('openidrequired') : wfMsg('openidoptional')) . "</td>");
 
647
                                $wgOut->addHTML("<td><input name='wpAllow{$field}' id='wpAllow{$field}' type='checkbox'");
 
648
                                if (!is_null($value)) {
 
649
                                        $wgOut->addHTML(" value='on' checked='checked' />");
 
650
                                } else {
 
651
                                        $wgOut->addHTML(" disabled='disabled' />");
 
652
                                }
 
653
                                $wgOut->addHTML("</tr>");
 
654
                        }
 
655
                        $wgOut->addHTML('</table>');
 
656
                }
 
657
                $wgOut->addHTML("<input type='submit' name='wpOK' value='{$ok}' /> <input type='submit' name='wpCancel' value='{$cancel}' /></form>");
 
658
                return NULL;
 
659
        }
 
660
 
 
661
        function Trust($request, $sreg) {
 
662
                global $wgRequest, $wgUser;
 
663
 
 
664
                assert(isset($request));
 
665
                assert(isset($sreg));
 
666
                assert(isset($wgRequest));
 
667
 
 
668
                if ($wgRequest->getCheck('wpCancel')) {
 
669
                        return $request->answer(false);
 
670
                }
 
671
 
 
672
                $trust_root = $request->trust_root;
 
673
 
 
674
                assert(isset($trust_root) && strlen($trust_root) > 0);
 
675
 
 
676
                # If they don't want us to allow trust, save that.
 
677
 
 
678
                if (!$wgRequest->getCheck('wpAllowTrust')) {
 
679
 
 
680
                        $this->SetUserTrust($wgUser, $trust_root, false);
 
681
                        # Set'em and sav'em
 
682
                        $wgUser->saveSettings();
 
683
                } else {
 
684
 
 
685
                        $fields = array_filter(array_unique(array_merge($sreg['optional'], $sreg['required'])),
 
686
                                                                   array($this, 'ValidField'));
 
687
 
 
688
                        $allow = array();
 
689
 
 
690
                        foreach ($fields as $field) {
 
691
                                if ($wgRequest->getCheck('wpAllow' . $field)) {
 
692
                                        $allow[] = $field;
 
693
                                }
 
694
                        }
 
695
 
 
696
                        $this->SetUserTrust($wgUser, $trust_root, $allow);
 
697
                        # Set'em and sav'em
 
698
                        $wgUser->saveSettings();
 
699
                }
 
700
 
 
701
        }
 
702
 
 
703
        # Converts an URL to a user name, if possible
 
704
 
 
705
        function UrlToUserName($url) {
 
706
 
 
707
                global $wgArticlePath, $wgServer;
 
708
 
 
709
                # URL must be a string
 
710
 
 
711
                if (!isset($url) || !is_string($url) || strlen($url) == 0) {
 
712
                        return null;
 
713
                }
 
714
 
 
715
                # it must start with our server, case doesn't matter
 
716
 
 
717
                if (strpos(strtolower($url), strtolower($wgServer)) !== 0) {
 
718
                        return null;
 
719
                }
 
720
 
 
721
                $parts = parse_url($url);
 
722
 
 
723
                $relative = $parts['path'];
 
724
                if (!is_null($parts['query']) && strlen($parts['query']) > 0) {
 
725
                        $relative .= '?' . $parts['query'];
 
726
                }
 
727
 
 
728
                # Use regexps to extract user name
 
729
 
 
730
                $pattern = str_replace('$1', '(.*)', $wgArticlePath);
 
731
                $pattern = str_replace('?', '\?', $pattern);
 
732
                # Can't have a pound-sign in the relative, since that's for fragments
 
733
                if (!preg_match("#$pattern#", $relative, $matches)) {
 
734
                        return null;
 
735
                } else {
 
736
                        $titletext = urldecode($matches[1]);
 
737
                        $nt = Title::newFromText($titletext);
 
738
                        if (is_null($nt) || $nt->getNamespace() != NS_USER) {
 
739
                                return null;
 
740
                        } else {
 
741
                                return $nt->getText();
 
742
                        }
 
743
                }
 
744
        }
 
745
 
 
746
        function serverUrl() {
 
747
                return $this->fullURL('OpenIDServer');
 
748
        }
 
749
}