| 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_idp_xrds_url() |
| 82 | { |
| 83 | global $globals; |
| 84 | return $globals->baseurl . '/openid/idp_xrds'; |
| 85 | } |
| 86 | |
| 87 | function get_user_xrds_url($user) |
| 88 | { |
| 89 | if (is_null($user)) { |
| 90 | return null; |
| 91 | } |
| 92 | global $globals; |
| 93 | return $globals->baseurl . '/openid/user_xrds/' . $user->hruid; |
| 94 | } |
| 95 | |
| 96 | function get_sreg_data($user) |
| 97 | { |
| 98 | if (is_null($user)) { |
| 99 | return null; |
| 100 | } |
| 101 | return array('fullname' => $user->fullName(), |
| 102 | 'nickname' => $user->displayName(), |
| 103 | 'dob' => null, |
| 104 | 'email' => $user->bestEmail(), |
| 105 | 'gender' => $user->isFemale() ? 'F' : 'M', |
| 106 | 'postcode' => null, |
| 107 | 'country' => null, |
| 108 | 'language' => null, |
| 109 | 'timezone' => null); |
| 110 | } |
| 111 | |
| 112 | function is_trusted_site($user, $url) |
| 113 | { |
| 114 | $res = XDB::query('SELECT COUNT(*) |
| 115 | FROM openid_trusted |
| 116 | WHERE (user_id = {?} OR user_id IS NULL) |
| 117 | AND url = {?}', |
| 118 | $user->id(), $url); |
| 119 | return $res->fetchOneCell() > 0; |
| 120 | } |
| 121 | |
| 122 | function add_trusted_site($user, $url) |
| 123 | { |
| 124 | XDB::execute("INSERT IGNORE INTO openid_trusted |
| 125 | SET user_id={?}, url={?}", |
| 126 | $user->id(), $url); |
| 127 | } |
| 128 | |
| 129 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 130 | ?> |