29 import(
'Dataface/Table.php');
60 private $emailColumn=null;
69 if ( $instance === 0 ){
71 if ( !defined(
'DATAFACE_AUTHENTICATIONTOOL_LOADED') ){
72 define(
'DATAFACE_AUTHENTICATIONTOOL_LOADED',
true);
80 $this->conf = $params;
81 $this->usersTable = ( isset($params[
'users_table']) ? $params[
'users_table'] : null);
82 $this->usernameColumn = ( isset($params[
'username_column']) ? $params[
'username_column'] : null);
83 $this->passwordColumn = (isset( $params[
'password_column']) ? $params[
'password_column'] : null);
84 $this->userLevelColumn = (isset( $params[
'user_level_column']) ? $params[
'user_level_column'] : null);
90 if ( isset( $type ) and $type != $this->authType ){
91 $this->authType = $type;
92 $this->delegate = null;
95 $module = basename($type);
97 DATAFACE_SITE_PATH.
'/modules/Auth/'.$module.
'/'.$module.
'.php',
98 DATAFACE_PATH.
'/modules/Auth/'.$module.
'/'.$module.
'.php'
100 foreach ( $module_path as
$path ){
101 if ( is_readable($path) ){
103 $classname =
'dataface_modules_'.$module;
104 $this->delegate =
new $classname;
114 if ( isset($this->delegate) and method_exists($this->delegate,
'getCredentials') ){
115 return $this->delegate->getCredentials();
117 $username = (isset($_REQUEST[
'UserName']) ? $_REQUEST[
'UserName'] : null);
118 $password = (isset($_REQUEST[
'Password']) ? $_REQUEST[
'Password'] : null);
119 return array(
'UserName'=>$username,
'Password'=>$password);
125 if ( !$this->authEnabled )
return true;
126 if ( isset($this->delegate) and method_exists($this->delegate,
'checkCredentials') ){
127 return $this->delegate->checkCredentials();
131 if ( !isset( $creds[
'UserName'] ) || !isset($creds[
'Password']) ){
136 import(
'Dataface/Serializer.php');
139 $sql =
"SELECT `".$this->usernameColumn.
"` FROM `".$this->usersTable.
"`
140 WHERE `".$this->usernameColumn.
"`='".addslashes(
141 $serializer->serialize($this->usernameColumn, $creds[
'UserName'])
143 AND `".$this->passwordColumn.
"`=".
144 $serializer->encrypt(
145 $this->passwordColumn,
146 "'".addslashes($serializer->serialize($this->passwordColumn, $creds[
'Password'])).
"'"
148 $res = mysql_query($sql,
$app->db());
149 if ( !$res )
throw new Exception(mysql_error(
$app->db()), E_USER_ERROR);
151 if ( mysql_num_rows($res) === 0 ){
155 while ( $row = mysql_fetch_row($res) ){
156 if ( strcmp($row[0], $creds[
'UserName'])===0 ){
161 @mysql_free_result($res);
170 if ( isset($this->delegate) and method_exists($this->delegate,
'setPassword') ){
171 return $this->delegate->setPassword($username, $password);
177 throw new Exception(
"Failed to set password because there is no logged in user.");
180 $user->setValue($this->passwordColumn, $password);
181 $res = $user->save();
183 throw new Exception($res->getMessage());
191 if ( !$this->authEnabled )
return true;
194 if (
$app->sessionEnabled() or
$app->autoSession ){
195 $app->startSession($this->conf);
197 $appdel =&
$app->getDelegate();
200 if ( isset($appdel) and method_exists($appdel,
'before_authenticate') ){
201 $appdel->before_authenticate();
204 if ( isset( $_REQUEST[
'-action'] ) and $_REQUEST[
'-action'] ==
'logout' ){
205 $app->startSession();
208 if ( isset($appdel) and method_exists($appdel,
'before_action_logout' ) ){
209 $res = $appdel->before_action_logout();
212 $username = @$_SESSION[
'UserName'];
215 import(
'Dataface/Utilities.php');
220 if ( isset($this->delegate) and method_exists($this->delegate,
'logout') ){
221 $this->delegate->logout();
223 if ( isset($_REQUEST[
'-redirect']) and !empty($_REQUEST[
'-redirect']) ){
224 $app->redirect($_REQUEST[
'-redirect']);
225 }
else if ( isset($_SESSION[
'-redirect']) ){
226 $redirect = $_SESSION[
'-redirect'];
227 unset($_SESSION[
'-redirect']);
228 $app->redirect($redirect);
232 $app->redirect(DATAFACE_SITE_HREF);
237 if ( isset( $_REQUEST[
'-action'] ) and $_REQUEST[
'-action'] ==
'login' ){
238 $app->startSession();
240 $app->redirect(DATAFACE_SITE_HREF.
'?--msg='.urlencode(
"You are logged in"));
245 $app->redirect(DATAFACE_SITE_HREF.
'?--msg='.urlencode(
"Sorry, you are currently locked out of the site due to failed login attempts. Please try again later, or contact a system administrator for help."));
252 if ( isset($creds[
'UserName']) and !$approved ){
257 df_translate(
'Incorrect Password',
258 'Sorry, you have entered an incorrect username /password combination. Please try again.'
262 }
else if ( !$approved ){
272 $_SESSION[
'UserName'] = $creds[
'UserName'];
274 import(
'Dataface/Utilities.php');
277 $msg = df_translate(
'You are now logged in',
'You are now logged in');
278 if ( isset( $_REQUEST[
'-redirect'] ) and !empty($_REQUEST[
'-redirect']) ){
280 $redirect = df_append_query($_REQUEST[
'-redirect'], array(
'--msg'=>$msg));
283 }
else if ( isset($_SESSION[
'-redirect']) ){
284 $redirect = $_SESSION[
'-redirect'];
285 unset($_SESSION[
'-redirect']);
286 $redirect = df_append_query($redirect, array(
'--msg'=>$msg));
291 $redirect =
$_SERVER[
'HOST_URI'].DATAFACE_SITE_HREF;
294 $redirect = preg_replace(
'/-action=login_prompt/',
'', $redirect);
295 $app->redirect($redirect);
299 if ( isset($this->delegate) and method_exists($this->delegate,
'authenticate') ){
300 $res = $this->delegate->authenticate();
308 if ( isset($this->conf[
'pre_auth_types']) ){
309 $pauthtypes = explode(
',',$this->conf[
'pre_auth_types']);
312 foreach ($pauthtypes as $pauthtype){
314 if ( isset($this->delegate) and method_exists($this->delegate,
'authenticate') ){
315 $res = $this->delegate->authenticate();
334 if ( !$this->authEnabled )
return true;
335 if ( isset($this->delegate) and method_exists($this->delegate,
'isLoggedIn') ){
336 return $this->delegate->isLoggedIn();
339 return (isset($_SESSION[
'UserName']) and $_SESSION[
'UserName']);
348 if ( !$this->authEnabled )
return true;
350 $query =&
$app->getQuery();
352 if ( @$query[
'--no-prompt'] ){
353 header(
"HTTP/1.0 401 Please Log In");
354 echo
"<html><body>Please Log In</body></html>";
358 if ( isset($this->delegate) and method_exists($this->delegate,
'showLoginPrompt') ){
359 return $this->delegate->showLoginPrompt($msg);
361 header(
"HTTP/1.1 401 Please Log In");
363 $url =
$app->url(
'-action=login_prompt');
365 if ( $msg ) $msgarray = array($msg);
366 else $msgarray = array();
367 if ( isset($query[
'--msg']) ){
368 $msgarray[] = $query[
'--msg'];
370 $msg = trim(implode(
'<br>',$msgarray));
371 if ( $msg ) $url .=
'&--msg='.urlencode($msg);
372 if ( $query[
'-action'] !=
'login' and $query[
'-action'] !=
'login_prompt' ) $_SESSION[
'-redirect'] = (isset(
$_SERVER[
'REQUEST_URI'])?
$_SERVER[
'REQUEST_URI']:
$app->url(
''));
374 $referer = @
$_SERVER[
'HTTP_REFERER'];
375 if ( !@$_SESSION[
'-redirect'] and $referer and strpos($referer, df_absolute_url(DATAFACE_SITE_URL)) === 0 ){
376 $_SESSION[
'-redirect'] = $referer;
379 header(
"Location: $url");
391 if ( !$this->authEnabled )
return $null;
392 if ( isset($this->delegate) and method_exists($this->delegate,
'getLoggedInUser') ){
393 $user =& $this->delegate->getLoggedInUser();
399 $user = df_get_record($this->usersTable, array($this->usernameColumn =>
'='.$_SESSION[
'UserName']));
401 $user =
new Dataface_Record($this->usersTable, array($this->usernameColumn => $_SESSION[
'UserName']));
410 if ( !$this->authEnabled )
return $null;
411 if ( isset($this->delegate) and method_exists($this->delegate,
'getLoggedInUsername') ){
412 return $this->delegate->getLoggedInUsername();
417 return $user->strval($this->usernameColumn);
424 $res = mysql_query(
"create table if not exists `dataface__failed_logins` (
425 `attempt_id` int(11) not null auto_increment primary key,
426 `ip_address` varchar(32) not null,
427 `username` varchar(32) not null,
428 `time_of_attempt` int(11) not null
430 if ( !$res )
throw new Exception(mysql_error(df_db()), E_USER_ERROR);
435 $del =
$app->getDelegate();
436 $method =
'loginFailed';
437 if ( isset($del) and method_exists($del, $method) ){
438 $del->$method($credentials[
'UserName'],
$_SERVER[
'REMOTE_ADDR'], time() );
441 $res = mysql_query(
"insert into `dataface__failed_logins` (ip_address,username,time_of_attempt) values (
442 '".addslashes(
$_SERVER[
'REMOTE_ADDR']).
"',
443 '".addslashes($credentials[
'UserName']).
"',
444 '".addslashes(time()).
"'
446 if ( !$res )
throw new Exception(mysql_error(df_db()), E_USER_ERROR);
453 $res = mysql_query(
"delete from `dataface__failed_logins` where ip_address='".addslashes(
$_SERVER[
'REMOTE_ADDR']).
"'", df_db());
454 if ( !$res )
throw new Exception(mysql_error(df_db()));
459 $res = mysql_query(
"delete from `dataface__failed_logins` where `time_of_attempt` < ".(time()-(60*30)), df_db());
460 if ( !$res )
throw new Exception(mysql_error(df_db()), E_USER_ERROR);
461 $res = mysql_query(
"select count(*) from `dataface__failed_logins` where `ip_address`='".addslashes(
$_SERVER[
'REMOTE_ADDR']).
"'", df_db());
462 if ( !$res )
throw new Exception(mysql_error(df_db()), E_USER_ERROR);
463 list($num) = mysql_fetch_row($res);
464 @mysql_free_result($res);
469 if ( !isset($this->emailColumn) ){
470 import(
'Dataface/Ontology.php');
473 if ( isset($this->conf[
'email_column']) ) $this->emailColumn = $this->conf[
'email_column'];
474 else $this->emailColumn = $ontology->getFieldname(
'email');
476 return $this->emailColumn;