e4a7b1236c32d9f1e1a0b1aa28a5b7b95fd3611d
[platal.git] / modules / openid / openid.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 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 General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 require_once "Auth/OpenID/Discover.php";
23
24 function init_openid_server()
25 {
26 // Initialize a filesystem-based store
27 $store_location = dirname(__FILE__) . '/../../spool/openid/store';
28 require_once "Auth/OpenID/FileStore.php";
29 $store = new Auth_OpenID_FileStore($store_location);
30
31 // Create an OpenId server
32 require_once 'Auth/OpenID/Server.php';
33 return new Auth_OpenID_Server($store, get_openid_url());
34 }
35
36 function get_openid_url()
37 {
38 global $globals;
39 return $globals->baseurl . '/openid';
40 }
41
42 function get_user($x) {
43 if (is_null($x)) {
44 return null;
45 }
46 $user = User::getSilent($x);
47 return $user ? $user : null;
48
49 }
50
51 function get_user_by_alias($x) {
52 if (is_null($x)) {
53 return null;
54 }
55 // TODO such a function should probably be provided in the User class
56 // or at least not here
57 $res = XDB::query('SELECT u.user_id
58 FROM auth_user_md5 AS u
59 INNER JOIN auth_user_quick AS q USING(user_id)
60 INNER JOIN aliases AS a ON (a.id = u.user_id AND type != \'homonyme\')
61 WHERE u.perms IN(\'admin\', \'user\')
62 AND q.emails_alias_pub = \'public\'
63 AND a.alias = {?}',
64 $x);
65 if (list($uid) = $res->fetchOneRow()) {
66 $user = User::getSilent($uid);
67 }
68 return $user ? $user : null;
69
70 }
71
72 function get_user_openid_url($user)
73 {
74 if (is_null($user)) {
75 return null;
76 }
77 global $globals;
78 return $globals->baseurl . '/openid/' . $user->hruid;
79 }
80
81 function get_user_xrds_url($user)
82 {
83 if (is_null($user)) {
84 return null;
85 }
86 global $globals;
87 return $globals->baseurl . '/openid/user_xrds/' . $user->hruid;
88 }
89
90 function get_sreg_data($user)
91 {
92 if (is_null($user)) {
93 return null;
94 }
95 return array('fullname' => $user->fullName(),
96 'nickname' => $user->displayName(),
97 'dob' => null,
98 'email' => $user->bestEmail(),
99 'gender' => $user->isFemale() ? 'F' : 'M',
100 'postcode' => null,
101 'country' => null,
102 'language' => null,
103 'timezone' => null);
104 }
105
106
107 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
108 ?>