Merge commit 'origin/fusionax' into account
[platal.git] / modules / openid / openid.inc.php
CommitLineData
a1af4a99
AA
1<?php
2/***************************************************************************
8d84c630 3 * Copyright (C) 2003-2009 Polytechnique.org *
a1af4a99
AA
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
22require_once "Auth/OpenID/Discover.php";
23
24function 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
36function get_openid_url()
37{
38 global $globals;
39 return $globals->baseurl . '/openid';
40}
41
42function 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
2d8779e2
AA
51function get_user_by_alias($x) {
52 if (is_null($x)) {
53 return null;
54 }
0001ba7a
FB
55 global $globals;
56 // Should we check the publicity of the alias?
57 $user = User::getSilent($x . '@' . $globals->mail->alias_dom);
2d8779e2
AA
58 return $user ? $user : null;
59
60}
61
a1af4a99
AA
62function get_user_openid_url($user)
63{
64 if (is_null($user)) {
65 return null;
66 }
67 global $globals;
68 return $globals->baseurl . '/openid/' . $user->hruid;
69}
70
ab66bf7f
AA
71function get_idp_xrds_url()
72{
73 global $globals;
74 return $globals->baseurl . '/openid/idp_xrds';
75}
76
a1af4a99
AA
77function get_user_xrds_url($user)
78{
79 if (is_null($user)) {
80 return null;
81 }
82 global $globals;
83 return $globals->baseurl . '/openid/user_xrds/' . $user->hruid;
84}
85
12d4424c
AA
86function get_sreg_data($user)
87{
88 if (is_null($user)) {
89 return null;
90 }
91 return array('fullname' => $user->fullName(),
92 'nickname' => $user->displayName(),
93 'dob' => null,
94 'email' => $user->bestEmail(),
95 'gender' => $user->isFemale() ? 'F' : 'M',
96 'postcode' => null,
97 'country' => null,
98 'language' => null,
99 'timezone' => null);
100}
a1af4a99 101
7eaf07e9
AA
102function is_trusted_site($user, $url)
103{
104 $res = XDB::query('SELECT COUNT(*)
105 FROM openid_trusted
19f4d908 106 WHERE (user_id = {?} OR user_id IS NULL)
7eaf07e9
AA
107 AND url = {?}',
108 $user->id(), $url);
109 return $res->fetchOneCell() > 0;
110}
a1af4a99 111
2e5fbf5e
AA
112function add_trusted_site($user, $url)
113{
114 XDB::execute("INSERT IGNORE INTO openid_trusted
115 SET user_id={?}, url={?}",
116 $user->id(), $url);
117}
118
a1af4a99 119// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0001ba7a 120?>