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
22 require_once 'diogenes/diogenes.core.session.inc.php';
23 require_once 'diogenes/diogenes.core.logger.inc.php';
25 /** This class describes a Diogenes session.
27 class DiogenesSession
extends DiogenesCoreSession
{
30 /** is this a native Diogenes account? */
36 function DiogenesSession() {
37 $this->DiogenesCoreSession();
38 $this->username
= "anonymous";
39 $this->perms
->addFlag('public');
43 /** Try to do a Diogenes authentication.
45 * @param page the calling page (by reference)
47 function doAuth(&$page) {
50 if ($this->perms
->hasflag("auth"))
53 /* do we have authentication tokens for auth ? */
54 if (isset($_REQUEST['login']) && isset($_REQUEST['response'])) {
55 // remember login for a year
56 setcookie('DiogenesLogin',$_REQUEST['login'],(time()+
25920000));
59 $res = $globals->db
->query("SELECT user_id,username,password,firstname,lastname,perms FROM {$globals->tauth['native']} WHERE username='{$_REQUEST['login']}'");
60 if (!list($uid,$username,$password,$firstname,$lastname,$perms) = mysql_fetch_row($res)) {
61 $page->info(__("Authentication error!"));
62 $this->doLogin($page);
64 mysql_free_result($res);
67 if ($_REQUEST['response'] != md5("{$_REQUEST['login']}:$password:{$this->challenge}"))
69 // log the login failure
70 $logger = new DiogenesCoreLogger($uid);
71 $logger->log("auth_fail",$_REQUEST['login']);
72 $page->info(__("Authentication error!"));
73 $this->doLogin($page);
78 $this->username
= $username;
79 $this->firstname
= $firstname;
80 $this->lastname
= $lastname;
81 $this->fullname
= $firstname . ($lastname ?
" $lastname" : "");
84 $logstr = $this->username
. (empty($page->alias
) ?
"" : "@{$page->alias}");
85 $_SESSION['log'] = new DiogenesCoreLogger($this->uid
);
86 $_SESSION['log']->log("auth_ok",$logstr);
88 // set user permissions
89 $this->perms
->addFlag('auth');
90 if ($perms == "admin") {
91 $this->perms
->addflag('root');
95 $this->doLogin($page);
100 /** Try to login for WebDAV (plain-text password).
102 * Return true for success, false for failure.
104 function doAuthWebDAV($user,$pass)
108 if ($this->perms
->hasflag("auth"))
113 $res = $globals->db
->query("SELECT user_id,username,perms FROM {$globals->tauth['native']} WHERE username='$user' AND password='$pass'");
114 if (!list($uid,$user,$perms) = mysql_fetch_row($res))
117 // retrieve user info
119 $this->username
= $user;
122 $_SESSION['log'] = new DiogenesWebDAVLogger($this->uid
,$this->auth
,$this->username
);
124 // set user permissions
125 $this->perms
->addFlag('auth');
126 if ($perms == "admin") {
127 $this->perms
->addflag('root');
134 /** Display login screen.
136 function doLogin(&$page) {
137 $page->assign('greeting',__("Diogenes login"));
138 $page->assign('msg_connexion', __("Connexion"));
139 $page->assign('msg_password',__("password"));
140 $page->assign('msg_submit',__("Submit"));
141 $page->assign('msg_username', __("username"));
143 if (isset($_COOKIE['DiogenesLogin']))
144 $page->assign('username', $_COOKIE['DiogenesLogin']);
145 $page->assign('post',htmlentities($page->script_uri()));
146 $page->assign('challenge',$this->challenge
);
147 $page->assign('md5',$page->url("md5.js"));
148 $page->display('login.tpl');
153 /** Read a user's permissions for a given barrel.
155 * @param alias the name of the barrel
157 function setBarrelPerms($alias) {
160 // if the user is logged in, refresh his/her permissions
161 if ($this->perms
->hasflag('auth')) {
162 if ($this->perms
->hasflag('root')) {
163 $this->perms
->addflag('user');
164 $this->perms
->addflag('admin');
166 $this->perms
->rmflag('user');
167 $this->perms
->rmflag('admin');
170 // read site specific permissions
171 $res = $globals->db
->query("SELECT perms FROM diogenes_perm WHERE alias='{$alias}' AND auth='{$this->auth}' AND uid='{$this->uid}'");
172 if (list($tmp) = mysql_fetch_row($res)) {
173 $this->perms
->addflag('user');
174 $this->perms
->addflag($tmp);
176 mysql_free_result($res);