<?php
/**
* Project: web.framework: the PHP5 MVC framework
* File: LoginChecker.tester.class.php
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* You may contact the authors of web.framework by e-mail at:
* hide@address.com
*
* The latest version of web.framework can be obtained from:
* http://sourceforge.net/projects/webframework
*
* @link http://sourceforge.net/projects/webframework
* @copyright 2005 Marcin Staniszczak
* @author Marcin Staniszczak <hide@address.com>
* @version 1.0.0
*/
/**
* Check UserName and Password
*
* @name LoginChecker
* @version 1.0.0
* @package web.framework
* @subpackage AdvancedTesters
*
* @author Marcin Staniszczak
* @copyright 2005 Marcin Staniszczak
*
* Required table:
* Your table with user must have login and password column
* (see parameters UserNameCol and PasswordCol).
* You must too add second tabkle, with adational informations:
*
* MySQL:
* <pre>
* CREATE TABLE login_details (
* iduser INT NOT NULL,
* lastlogin INT NOT NULL,
* lasttry INT NOT NULL,
* trycount INT NOT NULL,
*
* PRIMARY KEY(iduser)
* );
* </pre>
* PostgreSQL:
* <pre>
* CREATE TABLE login_details (
* iduser int NOT NULL,
* lastlogin INT NOT NULL,
* lasttry INT NOT NULL,
* trycount INT NOT NULL,
*
* CONSTRAINT PK_logins_iduser PRIMARY KEY(iduser)
* );
* </pre>
*
* CREATE INDEX IDX_logins_login ON logins (login);
*
* iduser must show ID user form your users table.
* You can add all of this colums to you'r users
* table, and set in properties userTable and
* detailsTable as this same table.
*
* For more information, please look at example
*/
class LoginChecker extends AAdvancedTester {
private $objDBConnection = null;
private $strDatasource = '';
private $strTablePrefix = '';
private $strUserNameFiledName = 'UserName';
private $strPasswordFieldName = 'Password';
private $strMethod = 'post';
private $strUserTableName = 'users';
private $strUserNameRowName = 'Login';
private $strPasswordRowName = 'Password';
private $strDetailsTable = 'login_details';
private $intLockAfter = 3;
private $intLockTime = 300;
/**
* The class constructor
*
* @access public
* @param array web.framework configuration array
* @param array tester's setting array - array('name'=>'value')
*/
public function __construct($arrConfiguration, $arrParameters) {
if (!isset($arrParameters['datasource'])) {
throw new WF_AdvancedTester_RequiredParameter_Exception(sprintf(Languages::$MESSAGES[WebFramework::$strLanguage]['EXCEPTIONS']['VALIDATOR']['TESTERS']['PARAMETER_REQUIRED'], 'LoginChecker', 'datasource'));
}
$this->strDatasource = $arrParameters['datasource'];
$this->objDBConnection = DBConnections::construct($arrConfiguration)->getDBConnection($this->strDatasource);
/*
* set prefix
*/
if (isset($arrConfiguration['datasources'][$this->strDatasource]['params']['prefix'])) {
$this->strTablePrefix = $arrConfiguration['datasources'][$this->strDatasource]['params']['prefix'];
}
/*
* set user name and password fields name (HTML Form)
*/
if (isset($arrParameters['userNameFieldName'])) {
$this->strUserNameFiledName = $arrParameters['userNameFieldName'];
}
if (isset($arrParameters['passwordFieldName'])) {
$this->strPasswordFieldName = $arrParameters['passwordFieldName'];
}
if (isset($arrParameters['method'])) { //POST/GET
if (strcmp($arrParameters['method'], 'post')!=0 && strcmp($arrParameters['method'], 'get')!=0) {
throw new WF_AdvancedTester_RequiredParameter_Exception(sprintf(Languages::$MESSAGES[WebFramework::$strLanguage]['EXCEPTIONS']['VALIDATOR']['TESTERS']['PARAMETER_VALUE'], 'LoginChecker', 'get, post (default)'));
}
$this->strMethod = $arrParameters['method'];
}
/*
* set user name table and rows names
*/
if (isset($arrParameters['userTable'])) {
$this->strUserTableName = $arrParameters['userTable'];
}
if (isset($arrParameters['userNameRowName'])) {
$this->strUserNameRowName = $arrParameters['userNameRowName'];
}
if (isset($arrParameters['passwordRowName'])) {
$this->strPasswordRowName = $arrParameters['passwordRowName'];
}
/*
* set temap table name
*/
if (isset($arrParameters['detailsTable'])) {
$this->strDetailsTable = $arrParameters['detailsTable'];
}
/*
* Locking parameters
*/
if (isset($arrParameters['lockAfter'])) {
$this->intLockAfter = intval($arrParameters['lockAfter']);
}
if (isset($arrParameters['lockTime'])) {
$this->intLockTime = intval($arrParameters['lockTime']);
}
}
/**
* This is tester's main method
*
* @access public
* @param array array with values - array('post'=>array(...), 'get'=>array(...))
* @return array array(mixed_value, array('post'=>name_of_post_fields, 'get'=>name_of_get_fields))
* @throws WF_Tester_RequiredParameter_Exception, WF_Exception
*/
public function execute($arrValues){
$arrFields = array(
'post' => array_keys($arrValues['post']),
'get' => array_keys($arrValues['get']),
);
$strUserName = $arrValues[$this->strMethod][$this->strUserNameFiledName];
$strPassword = $arrValues[$this->strMethod][$this->strPasswordFieldName];
if (file_exists(WEBFRAMEWORK_CORE_DIR.'Validators/AdvancedTesters/LoginChecker/SQL/SQLQueries.'.$this->strDatasource.'.class.php')) {
require_once(WEBFRAMEWORK_CORE_DIR.'Validators/AdvancedTesters/LoginChecker/SQL/SQLQueries.'.$this->strDatasource.'.class.php');
} else {
throw new WF_Exception(sprintf(Languages::$MESSAGES[WebFramework::$strLanguage]['EXCEPTIONS']['REQUIRE'], 'SQLQueries.'.$this->strDatasource.'.class.php'));
}
SQLQueries::setDBConnection($this->objDBConnection);
SQLQueries::setUsersTable($this->strTablePrefix.$this->strUserTableName);
SQLQueries::setDetailsTable($this->strTablePrefix.$this->strDetailsTable);
SQLQueries::setPasswordRow($this->strPasswordRowName);
SQLQueries::setUserNameRow($this->strUserNameRowName);
$arrUserInfo = SQLQueries::getUser($strUserName);
if ($arrUserInfo !== null) { //user exist
$strPassword = md5($strPassword);
if ((strcmp($strPassword, $arrUserInfo[$this->strPasswordRowName])===0) &&
(($arrUserInfo['trycount']<$this->intLockAfter) || ($arrUserInfo['lasttry']+$this->intLockTime<time()))) {
SQLQueries::updateLoginTime($arrUserInfo['iduser']);
return array($arrUserInfo, $arrFields);
} else {
if (($arrUserInfo['trycount']>=$this->intLockAfter) && ($arrUserInfo['lasttry']+$this->intLockTime>time())) {
return array((int)((($arrUserInfo['lasttry']+$this->intLockTime)-time())/60)+1, $arrFields);
} else {
if (($arrUserInfo['trycount']>=$this->intLockAfter) && ($arrUserInfo['lasttry']+$this->intLockTime<time())) {
SQLQueries::updateTryCount($arrUserInfo['iduser'], 1);
} else {
SQLQueries::updateTryCount($arrUserInfo['iduser'], $arrUserInfo['trycount']+1);
}
return array(false, $arrFields);
}
}
}
return array(false, $arrFields);
}
}
?>