• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

classes/class.ilBaseAuthentication.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00024 
00035 include_once 'Auth/Auth.php';
00036 
00037 class ilBaseAuthentication
00038 {
00039 
00040         /*
00041          * Pear object (Auth) 
00042          * @var object
00043          */
00044         var $auth = null;
00045 
00046 
00047 
00048         /*
00049          * session id
00050          * @var string
00051          */
00052         var $sid = '';
00053 
00054         /*
00055          * username
00056          * @var string
00057          */
00058         var $username = '';
00059 
00060         /*
00061          * password
00062          * @var string
00063          */
00064         var $password = '';
00065 
00066 
00067         /*
00068          * client id
00069          * @var string
00070          */
00071         var $client = '';
00072 
00073         function ilBaseAuthentication()
00074         {
00075                 $this->__setMessage('');
00076                 $this->__setMessageCode('Client');
00077                 $this->check_setting = true;
00078         }
00079 
00080 
00081         // Set/Get
00082         function setClient($a_client)
00083         {
00084                 $this->client = $a_client;
00085                 $_COOKIE['ilClientId'] = $a_client;
00086         }
00087         function getClient()
00088         {
00089                 return $this->client;
00090         }
00091         function setUsername($a_username)
00092         {
00093                 $this->username = $a_username;
00094                 $_POST['username'] = $a_username;
00095         }
00096         function getUsername()
00097         {
00098                 return $this->username;
00099         }
00100         function setPassword($a_password)
00101         {
00102                 $this->password = $a_password;
00103                 $_POST['password'] = $a_password;
00104         }
00105         function getPassword()
00106         {
00107                 return $this->password;
00108         }
00109         function setSid($a_sid)
00110         {
00111                 $this->sid = $a_sid;
00112                 $_COOKIE['PHPSESSID'] = $this->sid;
00113         }
00114         function getSid()
00115         {
00116                 return $this->sid;
00117         }
00118 
00119         function getMessage()
00120         {
00121                 return $this->message;
00122         }
00123         function getMessageCode()
00124         {
00125                 return $this->message_code;
00126         }
00127         function __setMessage($a_message)
00128         {
00129                 $this->message = $a_message;
00130         }
00131         function __setMessageCode($a_message_code)
00132         {
00133                 $this->message_code = $a_message_code;
00134         }
00135 
00136         function authenticate()
00137         {
00138                 if(!$this->getClient())
00139                 {
00140                         $this->__setMessage('No client given');
00141                         return false;
00142                 }
00143                 if(!$this->getUsername())
00144                 {
00145                         $this->__setMessage('No username given');
00146                         return false;
00147                 }
00148                 // Read ilias ini
00149                 if(!$this->__buildDSN())
00150                 {
00151                         return false;
00152                 }
00153                 if(!$this->__setSessionSaveHandler())
00154                 {
00155                         return false;
00156                 }
00157                 if(!$this->__buildAuth())
00158                 {
00159                         return false;
00160                 }
00161                 $this->auth->start();
00162 
00163                 if(!$this->auth->getAuth())
00164                 {
00165                         $this->__getAuthStatus();
00166 
00167                         return false;
00168                 }                       
00169 
00170                 $this->setSid(session_id());
00171 
00172                 return true;
00173         }
00174 
00175         function start()
00176         {
00177                 if(!$this->getSid())
00178                 {
00179                         $this->__setMessage('No session id given');
00180                         return false;
00181                 }
00182 
00183                 $this->auth->start();
00184 
00185                 return true;
00186         }
00187         
00188         function validateSession()
00189         {
00190                 if(!$this->getClient())
00191                 {
00192                         $this->__setMessage('No client given');
00193                         return false;
00194                 }
00195                 if(!$this->getSid())
00196                 {
00197                         $this->__setMessage('No session id given');
00198                         return false;
00199                 }
00200                 
00201                 if(!$this->__buildAuth())
00202                 {
00203                         return false;
00204                 }
00205                 if(!$this->__setSessionSaveHandler())
00206                 {
00207                         return false;
00208                 }
00209 
00210                 $this->auth->start();
00211                 if(!$this->auth->getAuth())
00212                 {
00213                         $this->__setMessage('Session not valid');
00214                         
00215                         return false;
00216                 }
00217                 
00218                 return true;
00219         }
00220 
00221         function logout()
00222         {
00223                 if(!$this->getClient())
00224                 {
00225                         $this->__setMessage('No client given');
00226                         return false;
00227                 }
00228                 if(!$this->getSid())
00229                 {
00230                         $this->__setMessage('No session id given');
00231                         return false;
00232                 }
00233                 // logged auth users are authenticated
00234                 // No preperations are required
00235                 #if(!$this->__buildAuth())
00236                 #{
00237                 #       return false;
00238                 #}
00239                 #if(!$this->__setSessionSaveHandler())
00240                 #{
00241                 #       return false;
00242                 #}
00243                 
00244                 // And finally logout
00245                 #$this->auth->start();
00246                 $this->auth->logout();
00247                 session_destroy();
00248 
00249                 return true;
00250 
00251         }
00252 
00253         function __buildDSN()
00254         {
00255                 include_once './classes/class.ilIniFile.php';
00256 
00257                 // get ilias ini file
00258                 $this->ilias_ini =& new ilIniFile('./ilias.ini.php');
00259                 $this->ilias_ini->read();
00260 
00261                 if(!@file_exists("./".$this->ilias_ini->readVariable('clients','path')."/".$this->getClient()."/client.ini.php"))
00262                 {
00263                         $this->__setMessageCode('Client');
00264                         $this->__setMessage('Client does not exist');
00265 
00266                         return false;
00267                 }
00268                 
00269                 $this->ini =& new ilIniFile("./".$this->ilias_ini->readVariable('clients','path')."/".$this->getClient()."/client.ini.php");
00270                 $this->ini->read();
00271                 
00272                 $this->dsn = $this->ini->readVariable("db","type").
00273                                          "://".$this->ini->readVariable("db", "user").
00274                                          ":".$this->ini->readVariable("db", "pass").
00275                                          "@".$this->ini->readVariable("db", "host").
00276                                          "/".$this->ini->readVariable("db", "name");
00277 
00278                 return true;
00279         }               
00280 
00281         function __buildAuth()
00282         {
00283 
00284 
00285                 $this->auth_params = array(
00286                         'dsn'             => $this->dsn,
00287                         'table'       => $this->ini->readVariable("auth", "table"),
00288                         'usernamecol' => $this->ini->readVariable("auth", "usercol"),
00289                         'passwordcol' => $this->ini->readVariable("auth", "passcol")
00290                         );
00291 
00292                 $this->auth = new Auth("DB", $this->auth_params,"",false);
00293 
00294                 return true;
00295         }
00296 
00297         function __setSessionSaveHandler()
00298         {
00299                 include_once './include/inc.db_session_handler.php';
00300                 include_once './classes/class.ilUtil.php';
00301                 include_once './classes/class.ilErrorHandling.php';
00302                 include_once './classes/class.ilDBx.php';
00303 
00304                 
00305                 $GLOBALS['ilDB'] =& new ilDBx($this->dsn);
00306 
00307                 if(ini_get('session.save_handler') != 'user')
00308                 {
00309                         ini_set("session.save_handler", "user");
00310                 }
00311                 if(!db_set_save_handler())
00312                 {
00313                         $this->__setMessageCode('Server');
00314                         $this->__setMessage('Cannot set session handler');
00315 
00316                         return false;
00317                 }
00318 
00319                 return true;
00320         }
00321 
00322         function __getAuthStatus()
00323         {
00324                 switch($this->auth->getStatus())
00325                 {
00326                         case AUTH_EXPIRED:
00327                                 $this->__setMessageCode('Server');
00328                                 $this->__setMessage('Session expired');
00329 
00330                                 return false;
00331 
00332                         case AUTH_IDLED:
00333                                 $this->__setMessageCode('Server');
00334                                 $this->__setMessage('Session idled');
00335                                 
00336                                 return false;
00337                                 
00338                         case AUTH_WRONG_LOGIN:
00339                         default:
00340                                 $this->__setMessageCode('Client');
00341                                 $this->__setMessage('Wrong login');
00342 
00343                                 return false;
00344                                 
00345                                 
00346                 }
00347         }
00348 }
00349 ?>

Generated on Fri Dec 13 2013 11:57:53 for ILIAS Release_3_6_x_branch .rev 46809 by  doxygen 1.7.1