diogenes.core.session.inc.php

00001 <?php
00002 /*
00003  * Copyright (C) 2003-2004 Polytechnique.org
00004  * http://opensource.polytechnique.org/
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 require_once dirname(__FILE__).'/diogenes.core.logger.inc.php';
00022 require_once dirname(__FILE__).'/diogenes.flagset.inc.php';
00023 
00025 $diogenes_core_usercache = array();
00026 
00029 class DiogenesCoreSession {
00031   var $challenge;
00032 
00034   var $uid;
00036   var $username;
00038   var $perms;
00039   
00042   function DiogenesCoreSession()
00043   {
00044     $this->challenge = md5(uniqid(rand(), 1));
00045     $this->perms = new flagset("");
00046   }
00047 
00048 
00053   function hasPerms($level)
00054   {
00055     return $this->perms->hasflag($level);
00056   }
00057 
00058 
00064   function doAuth(&$page) {
00065     global $globals;
00066     echo "DiogenesCoreSession::doAuth needs to be overriden";
00067     exit;
00068 
00069     // if we are already autentified, return
00070     if ($this->perms->hasflag("auth"))
00071       return;
00072 
00073     // do we have authentication tokens for auth ?
00074     if (isset($_REQUEST['some_token_needed_for_auth'])) {
00075       // here goes the authentication code
00076     } else {
00077       $this->doLogin($page);
00078     }
00079   }
00080 
00081 
00086   function doLogin(&$page) {
00087     echo "DiogenesCoreSession::doLogin needs to be overriden";
00088     exit;
00089   }
00090 
00091 
00103   function getUserId($auth,$username) {
00104     global $diogenes_core_usercache, $globals;
00105 
00106     if (isset($diogenes_core_usercache[$auth]) and ($uid = array_search($username, $diogenes_core_usercache[$auth])))
00107     {
00108       
00109       // retrieve the result from cache
00110       return $uid;
00111       
00112     } else {
00113     
00114       // lookup the user id in database
00115       $uid = call_user_func(array($globals->session,'lookupUserId'),$auth,$username);
00116             
00117       // cache this result
00118       $diogenes_core_usercache[$auth][$uid] = $username;      
00119       return $uid;
00120     }    
00121     
00122   }
00123 
00124 
00136   function getUsername($auth,$uid) {
00137     global $diogenes_core_usercache, $globals;
00138 
00139     if (isset($diogenes_core_usercache[$auth][$uid])) {
00140       
00141       // retrieve result from cache
00142       return $diogenes_core_usercache[$auth][$uid];
00143       
00144     } else {
00145       
00146       // lookup the user id in database
00147       $username = call_user_func(array($globals->session,'lookupUsername'),$auth,$uid);
00148     
00149       // cache this result
00150       $diogenes_core_usercache[$auth][$uid] = $username;
00151       
00152       return $username;
00153     }
00154         
00155   }
00156   
00157 
00165   function lookupUserId($auth, $username)
00166   {
00167     global $globals;
00168     
00169     $res = $globals->db->query("select user_id from {$globals->tauth[$auth]} where username='$username'");
00170     list($uid) = mysql_fetch_row($res);
00171     mysql_free_result($res);
00172     
00173     return $uid;
00174   }
00175 
00176   
00184   function lookupUsername($auth, $uid)
00185   {
00186     global $globals;
00187    
00188     $res = $globals->db->query("select username from {$globals->tauth[$auth]} where user_id='$uid'");
00189     list($username) = mysql_fetch_row($res);
00190     mysql_free_result($res);    
00191     
00192     return $username;    
00193   }
00194   
00195 }
00196 
00197 ?>

Generated on Fri Jan 11 01:20:08 2008 for Diogenes by  doxygen 1.5.1