3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 require_once dirname(__FILE__
).'/diogenes.core.logger.inc.php';
22 require_once dirname(__FILE__
).'/diogenes.flagset.inc.php';
24 /** cache of user <=> id matches */
25 $diogenes_core_usercache = array();
27 /** This class describes a Diogenes session.
29 class DiogenesCoreSession
{
30 /** authentication challenge */
35 /** username (login) */
42 function DiogenesCoreSession()
44 $this->challenge
= md5(uniqid(rand(), 1));
45 $this->perms
= new flagset("");
49 /** Does the user have a given permission level.
53 function hasPerms($level)
55 return $this->perms
->hasflag($level);
59 /** Perform authentication. This needs to be overriden to do
62 * @param page the calling page (by reference)
64 function doAuth(&$page) {
66 echo "DiogenesCoreSession::doAuth needs to be overriden";
69 // if we are already autentified, return
70 if ($this->perms
->hasflag("auth"))
73 // do we have authentication tokens for auth ?
74 if (isset($_REQUEST['some_token_needed_for_auth'])) {
75 // here goes the authentication code
77 $this->doLogin($page);
82 /** Display login screen. Needs to be overriden!
84 * @param page the page asking for authentication
86 function doLogin(&$page) {
87 echo "DiogenesCoreSession::doLogin needs to be overriden";
92 /** Returns the user id associated with a given username.
93 * We use caching to avoid unnecessary database requests.
95 * Actual lookup is performed by the lookupUserId function.
97 * @param $auth the authentication method
98 * @param $username the username to look up
100 * @see DiogenesLoggerView
103 function getUserId($auth,$username) {
104 global $diogenes_core_usercache, $globals;
106 if (isset($diogenes_core_usercache[$auth]) and ($uid = array_search($username, $diogenes_core_usercache[$auth])))
109 // retrieve the result from cache
114 // lookup the user id in database
115 $uid = call_user_func(array($globals->session
,'lookupUserId'),$auth,$username);
118 $diogenes_core_usercache[$auth][$uid] = $username;
125 /** Returns the username associated with a given user id.
126 * We use caching to avoid unnecessary database requests.
128 * Actual lookup is performed by the lookupUsername function.
130 * @param $auth the authentication method
131 * @param $uid the username to look up
133 * @see DiogenesLoggerView
134 * @see lookupUsername
136 function getUsername($auth,$uid) {
137 global $diogenes_core_usercache, $globals;
139 if (isset($diogenes_core_usercache[$auth][$uid])) {
141 // retrieve result from cache
142 return $diogenes_core_usercache[$auth][$uid];
146 // lookup the user id in database
147 $username = call_user_func(array($globals->session
,'lookupUsername'),$auth,$uid);
150 $diogenes_core_usercache[$auth][$uid] = $username;
158 /** Look up the user id associated with a given username.
160 * @param $auth the authentication method
161 * @param $username the username to look up
163 * @see DiogenesLoggerView
165 function lookupUserId($auth, $username)
169 $res = $globals->db
->query("select user_id from {$globals->tauth[$auth]} where username='$username'");
170 list($uid) = mysql_fetch_row($res);
171 mysql_free_result($res);
177 /** Looks up the username associated with a given user id.
179 * @param $auth the authentication method
180 * @param $uid the username to look up
182 * @see DiogenesLoggerView
184 function lookupUsername($auth, $uid)
188 $res = $globals->db
->query("select username from {$globals->tauth[$auth]} where user_id='$uid'");
189 list($username) = mysql_fetch_row($res);
190 mysql_free_result($res);