ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilBaseAuthentication.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 
34 include_once 'Auth/Auth.php';
35 
36 define('IL_AUTH_MD5',1);
37 define('IL_AUTH_PLAIN',2);
38 
40 {
41 
42  /*
43  * Pear object (Auth)
44  * @var object
45  */
46  var $auth = null;
47 
48 
49 
50  /*
51  * session id
52  * @var string
53  */
54  var $sid = '';
55 
56  /*
57  * username
58  * @var string
59  */
60  var $username = '';
61 
62  /*
63  * password
64  * @var string
65  */
66  var $password = '';
67 
68 
69  /*
70  * client id
71  * @var string
72  */
73  var $client = '';
74 
76  {
77  $this->__setMessage('');
78  $this->__setMessageCode('Client');
79  $this->check_setting = true;
80  }
81 
82 
83  // Set/Get
84  function setClient($a_client)
85  {
86  $this->client = $a_client;
87  $_COOKIE['ilClientId'] = $a_client;
88  }
89  function getClient()
90  {
91  return $this->client;
92  }
93  function setUsername($a_username)
94  {
95  $this->username = $a_username;
96  $_POST['username'] = $a_username;
97  }
98  function getUsername()
99  {
100  return $this->username;
101  }
102  function setPassword($a_password)
103  {
104  $this->password = $a_password;
105  $_POST['password'] = $a_password;
106  }
107  function getPassword()
108  {
109  return $this->password;
110  }
111  function setSid($a_sid)
112  {
113  $this->sid = $a_sid;
114  $_COOKIE['PHPSESSID'] = $this->sid;
115  }
116  function getSid()
117  {
118  return $this->sid;
119  }
120 
121  function getMessage()
122  {
123  return $this->message;
124  }
125  function getMessageCode()
126  {
127  return $this->message_code;
128  }
129  function __setMessage($a_message)
130  {
131  $this->message = $a_message;
132  }
133  function __setMessageCode($a_message_code)
134  {
135  $this->message_code = $a_message_code;
136  }
137 
138  function setPasswordType($a_type)
139  {
140  $this->password_type = $a_type;
141  }
142  function getPasswordType()
143  {
144  return isset($this->password_type) ? $this->password_type : IL_AUTH_PLAIN;
145  }
146 
147  function authenticate()
148  {
149  if(!$this->getClient())
150  {
151  $this->__setMessage('No client given');
152  return false;
153  }
154  if(!$this->getUsername())
155  {
156  $this->__setMessage('No username given');
157  return false;
158  }
159  // Read ilias ini
160  if(!$this->__buildDSN())
161  {
162  return false;
163  }
164  if(!$this->__setSessionSaveHandler())
165  {
166  return false;
167  }
168  if(!$this->__buildAuth())
169  {
170  return false;
171  }
172  $this->auth->start();
173 
174  if(!$this->auth->getAuth())
175  {
176  $this->__getAuthStatus();
177 
178  return false;
179  }
180 
181  $this->setSid(session_id());
182 
183  return true;
184  }
185 
186  function start()
187  {
188  if(!$this->getSid())
189  {
190  $this->__setMessage('No session id given');
191  return false;
192  }
193 
194  $this->auth->start();
195 
196  return true;
197  }
198 
199  function validateSession()
200  {
201  if(!$this->getClient())
202  {
203  $this->__setMessage('No client given');
204  return false;
205  }
206  if(!$this->getSid())
207  {
208  $this->__setMessage('No session id given');
209  return false;
210  }
211 
212  if(!$this->__buildAuth())
213  {
214  return false;
215  }
216  if(!$this->__setSessionSaveHandler())
217  {
218  return false;
219  }
220 
221  $this->auth->start();
222  if(!$this->auth->getAuth())
223  {
224  $this->__setMessage('Session not valid');
225 
226  return false;
227  }
228 
229  return true;
230  }
231 
232  function logout()
233  {
234  if(!$this->getClient())
235  {
236  $this->__setMessage('No client given');
237  return false;
238  }
239  if(!$this->getSid())
240  {
241  $this->__setMessage('No session id given');
242  return false;
243  }
244  // logged auth users are authenticated
245  // No preperations are required
246  #if(!$this->__buildAuth())
247  #{
248  # return false;
249  #}
250  #if(!$this->__setSessionSaveHandler())
251  #{
252  # return false;
253  #}
254 
255  // And finally logout
256  #$this->auth->start();
257  $this->auth->logout();
258  session_destroy();
259 
260  return true;
261 
262  }
263 
264  function __buildDSN()
265  {
266  include_once './classes/class.ilIniFile.php';
267 
268  // get ilias ini file
269  $this->ilias_ini =& new ilIniFile('./ilias.ini.php');
270  $this->ilias_ini->read();
271 
272  if(!@file_exists("./".$this->ilias_ini->readVariable('clients','path')."/".$this->getClient()."/client.ini.php"))
273  {
274  $this->__setMessageCode('Client');
275  $this->__setMessage('Client does not exist');
276 
277  return false;
278  }
279 
280  $this->ini =& new ilIniFile("./".$this->ilias_ini->readVariable('clients','path')."/".$this->getClient()."/client.ini.php");
281  $this->ini->read();
282 
283  $this->dsn = $this->ini->readVariable("db","type").
284  "://".$this->ini->readVariable("db", "user").
285  ":".$this->ini->readVariable("db", "pass").
286  "@".$this->ini->readVariable("db", "host").
287  "/".$this->ini->readVariable("db", "name");
288 
289  return true;
290  }
291 
292  function __buildAuth()
293  {
294  // BEGIN WebDAV
295  // The realm is needed to support a common session between Auth_HTTP and Auth.
296  // It also helps us to distinguish between parallel sessions run on different clients.
297  // Common session only works if we use a common session name starting with "_authhttp".
298  // We must use the "_authttp" prefix, because it is hardcoded in the session name of
299  // class Auth_HTTP.
300  // Note: The realm and sessionName used here, must be the same as in
301  // class ilAuthUtils. Otherwise, Soap clients won't be able to log
302  // in to ILIAS.
303  $realm = $this->getClient();
304  // END WebDAV
305 
306  $this->auth_params = array(
307  'dsn' => $this->dsn,
308  'table' => $this->ini->readVariable("auth", "table"),
309  'usernamecol' => $this->ini->readVariable("auth", "usercol"),
310  'passwordcol' => $this->ini->readVariable("auth", "passcol"),
311  'sessionName' => "_authhttp".md5($realm)
312  );
313 
314  if($this->getPasswordType() == IL_AUTH_MD5)
315  {
316  $this->auth_params['cryptType'] = 'none';
317  }
318 
319  require_once 'class.ilAuthContainerMDB2.php';
320  $authContainerDB = new ilAuthContainerMDB2($this->auth_params);
321  $this->auth = new Auth($authContainerDB, $this->auth_params,"",false);
322 
323  return true;
324  }
325 
327  {
328  include_once './include/inc.db_session_handler.php';
329  include_once "./Services/Utilities/classes/class.ilUtil.php";
330  include_once './classes/class.ilErrorHandling.php';
331  include_once './classes/class.ilDBx.php';
332 
333 
334  $GLOBALS['ilDB'] =& new ilDBx($this->dsn);
335 
336  if(ini_get('session.save_handler') != 'user')
337  {
338  ini_set("session.save_handler", "user");
339  }
340  if(!db_set_save_handler())
341  {
342  $this->__setMessageCode('Server');
343  $this->__setMessage('Cannot set session handler');
344 
345  return false;
346  }
347 
348  return true;
349  }
350 
351  function __getAuthStatus()
352  {
353  switch($this->auth->getStatus())
354  {
355  case AUTH_EXPIRED:
356  $this->__setMessageCode('Server');
357  $this->__setMessage('Session expired');
358 
359  return false;
360 
361  case AUTH_IDLED:
362  $this->__setMessageCode('Server');
363  $this->__setMessage('Session idled');
364 
365  return false;
366 
367  case AUTH_WRONG_LOGIN:
368  default:
369  $this->__setMessageCode('Client');
370  $this->__setMessage('Wrong Login or Password');
371 
372  return false;
373 
374 
375  }
376  }
377 }
378 ?>