<?php/********************************************************************************************
* BlueShoes Framework; This file is part of the php application framework.
* NOTE: This code is stripped (obfuscated). To get the clean documented code goto 
*       www.blueshoes.org and register for the free open source *DEVELOPER* version or 
*       buy the commercial version.
*       
*       In case you've already got the developer version, then this is one of the few 
*       packages/classes that is only available to *PAYING* customers.
*       To get it go to www.blueshoes.org and buy a commercial version.
* 
* @copyright www.blueshoes.org
* @author    Samuel Blume <sam at blueshoes dot org>
* @author    Andrej Arn <andrej at blueshoes dot org>
*/?><?php
define('BS_CUG_VERSION',      '4.5.$Revision: 1.4 $');if (!@$APP) require_once($_SERVER['DOCUMENT_ROOT'] . $GLOBALS['APP']['path']['diffFromDocRootToGlobalConf'] . 'global.conf.php');require_once($APP['path']['core'] . 'auth/Bs_Password.class.php');require_once($APP['path']['core'] . 'html/form/Bs_Form.class.php');require_once($APP['path']['core'] . 'html/table/Bs_HtmlTable.class.php');include_once($APP['path']['core'] . 'net/http/session/Bs_SimpleSession.class.php');define('BS_CUG_CASE_SENSITIVE_YES',           0);define('BS_CUG_CASE_SENSITIVE_ONLY_USERNAME', 1);define('BS_CUG_CASE_SENSITIVE_ONLY_PASSWORD', 2);define('BS_CUG_CASE_SENSITIVE_NO',            3);class Bs_Cug extends Bs_Object {var $_APP;var $form;var $recoverPasswordByUsernameForm;var $bsSession;var $_postVars;var $_cugName;var $redirectPage = 'frameset.php';var $formAction;var $formTarget = '_top';var $language = 'en';var $maxNumAttempts = 0;var $unixLikeDoubleTimeout = TRUE;var $checkForHack = TRUE;var $actionOnHack;var $logAttempts = 1;var $checkCaseSensitive = 0;var $userFieldNames = array(
'user'          => 'user', 
'pass'          => 'pass', 
'isActive'      => 'isActive', 
'startDatetime' => 'startDatetime', 
'endDatetime'   => 'endDatetime', 
'email'         => 'email'
);var $errorMsg;var $_registeredValidators;function Bs_Cug($cugName, $postData=NULL) {parent::Bs_Object(); $this->_APP      = &$GLOBALS['APP'];if (!is_null($postData)) {$this->_postVars = &$postData;} else {$this->_postVars = $_POST;}
$this->_cugName  = $cugName;if (isSet($GLOBALS['Bs_SimpleSession'])) {$this->bsSession =& $GLOBALS['Bs_SimpleSession'];} elseif (isSet($GLOBALS['bsSession'])) {$this->bsSession =& $GLOBALS['bsSession'];} else {$this->bsSession =& $GLOBALS['Bs_SimpleSession'];}
}
function letMeIn() {return $this->bsSession->isRegistered($this->_cugName);}
function logout() {$this->bsSession->unRegister($this->_cugName);}
function getLoginForm() {$this->loadLoginForm();return $this->form->getForm();}
function treatLoginForm() {if (!isSet($this->form)) $this->loadLoginForm();$ret = '';if ((isSet($this->_postVars['bs_form']['step'])) && ($this->_postVars['bs_form']['step'] == '2')) {$this->form->setReceivedValues($this->_postVars);$this->form->postLoadTrigger(); $isOk = $this->form->validate();if ($isOk) {$isOk = $this->_validateLogin();if (!$isOk) {$this->form->addError($this->errorMsg);}
}
if ($isOk) {$this->bsSession->register($this->_cugName, $dev0='1');return TRUE;} else {$ret .= $this->form->getForm(TRUE);}
} else {$ret .= $this->form->getForm();}
return $ret;}
function treatRecoverPasswordByUsernameForm() {$this->_postVars = $_POST; $this->loadRecoverPasswordByUsernameForm();$ret = '';if ((isSet($this->_postVars['bs_form']['step'])) && ($this->_postVars['bs_form']['step'] == '2') && ($this->_postVars['bs_form']['name'] == 'recoverPasswordByUsername')) {$this->recoverPasswordByUsernameForm->setReceivedValues($this->_postVars);$this->recoverPasswordByUsernameForm->postLoadTrigger();$isOk = $this->recoverPasswordByUsernameForm->validate();if ($isOk) {$t        = $this->recoverPasswordByUsernameForm->getFieldValue('username');$username = $t[0];$status = $this->recoverPasswordByUsername($username, TRUE);if ($status === TRUE) {return 'Your password has been sent to the email address registered for your user account.<br>After receiving this, please log-in again and change the password to a new one of your choice.<br><br>';} elseif ($status === FALSE) { return 'Your password has been sent to the email address registered for your user account.<br>After receiving this, please log-in again and change the password to a new one of your choice.<br><br>';} else {$ret .= $this->errorMsg;$this->loadRecoverPasswordByUsernameForm(TRUE);$ret .= $this->recoverPasswordByUsernameForm->getForm();return $ret;}
} else {$ret .= $this->recoverPasswordByUsernameForm->getForm(TRUE);}
} else {$ret .= $this->recoverPasswordByUsernameForm->getForm();}
return $ret;}
function redirect() {redirect($this->redirectPage);}
function registerValidator($func, $obj=NULL) {$this->_registeredValidators[] = array($func, &$obj);}
function _validateLogin($dataHash) {if (is_array($this->_registeredValidators)) {reset($this->_registeredValidators);while (list($k) = each($this->_registeredValidators)) {if (is_object($this->_registeredValidators[$k][1])) {if (method_exists($this->_registeredValidators[$k][1], $this->_registeredValidators[$k][0])) {$status = call_user_func(array(&$this->_registeredValidators[$k][1], $this->_registeredValidators[$k][0]), $dataHash);if ($status !== TRUE) return $status;}
} else {if (function_exists($this->_registeredValidators[$k][0])) {$status = $this->_registeredValidators[$k][0]($dataHash);if ($status !== TRUE) return $status;}
}
}
}
return TRUE;}
function _validateLoginData($validateData) {if ($this->checkCaseSensitive == BS_CUG_CASE_SENSITIVE_ONLY_USERNAME) {$validateData['user']     = strToLower($validateData['user']);$validateData['sentUser'] = strToLower($validateData['sentUser']);} elseif ($this->checkCaseSensitive == BS_CUG_CASE_SENSITIVE_ONLY_PASSWORD) {$validateData['pass']     = strToLower($validateData['pass']);$validateData['sentPass'] = strToLower($validateData['sentPass']);} elseif ($this->checkCaseSensitive == BS_CUG_CASE_SENSITIVE_NO) {$validateData['user']     = strToLower($validateData['user']);$validateData['sentUser'] = strToLower($validateData['sentUser']);$validateData['pass']     = strToLower($validateData['pass']);$validateData['sentPass'] = strToLower($validateData['sentPass']);}
if ($validateData['user'] !== $validateData['sentUser']) {if (strToLower($validateData['user']) === strToLower($validateData['sentUser'])) {$this->errorMsg = 'Case of username or password wrong. Please try again.';return            'username case';} else {$this->errorMsg = 'Username or password wrong. Please try again.';return            'username';}
} elseif ($validateData['pass'] !== $validateData['sentPass']) {if (strToLower($validateData['pass']) === strToLower($validateData['sentPass'])) {$this->errorMsg = 'Case of username or password wrong. Please try again.';return            'password case';} else {$this->errorMsg = 'Username or password wrong. Please try again.';return            'password';}
} elseif (isSet($validateData['isActive'])      && !$validateData['isActive']) {$this->errorMsg = 'Account inactive.';return            'inactive';} elseif (isSet($validateData['startDatetime']) && ($validateData['startDatetime'] != '0000-00-00 00:00:00') && ($validateData['startDatetime'] > gmdate("Y-m-d H:i:s"))) {$this->errorMsg = 'Account will be activated on ' . $validateData['startDatetime'] . '.';return            'inactive earlybird';} elseif (isSet($validateData['endDatetime'])   && ($validateData['endDatetime'] != '0000-00-00 00:00:00') && ($validateData['endDatetime'] < gmdate("Y-m-d H:i:s"))) {$this->errorMsg = 'Account has expired on ' . $validateData['endDatetime'] . '.';return            'inactive expired';}
return TRUE;}
function loadLoginForm() {if (isSet($this->form)) return;$this->form =& new Bs_Form();$this->form->internalName = 'Login';$this->form->name         = 'Login';$this->form->mode         = 'login';$this->form->language     = $this->language;if (isSet($this->formAction)) $this->form->action = $this->formAction;if (isSet($this->formTarget)) $this->form->target = $this->formTarget;$container1 =& new Bs_FormContainer();$container1->name         = "login";$container1->caption      = array('en'=>'Login', 'de'=>'Anmelden');$container1->orderId      = 1000;$this->form->elementContainer->addElement($container1);$FormField =& new Bs_FormFieldText();$FormField->name          = 'username';$FormField->caption       = array('en'=>'Username', 'de'=>'Benutzername', 'fr'=>"Nom d'utilisateur", 'it'=>'Nome utente');$FormField->editability   = 'always';$FormField->minLength     = 1;$FormField->maxLength     = 30;$FormField->orderId       = 1000;$FormField->bsDataType    = 'username';$FormField->bsDataInfo    = 1;$FormField->must          = TRUE;$FormField->trim          = 'none';$FormField->mustBeUnique  = TRUE;$container1->addElement($FormField);unset($FormField);$FormField =& new Bs_FormFieldPassword();$FormField->name          = 'password';$FormField->caption       = array('en'=>'Password', 'de'=>'Passwort', 'fr'=>'Mot de passe', 'it'=>'Password');$FormField->editability   = 'always';$FormField->minLength     = 1;$FormField->maxLength     = 30;$FormField->orderId       = 900;$FormField->bsDataType    = 'password';$FormField->bsDataInfo    = 1;$FormField->must          = TRUE;$FormField->trim          = 'none';$container1->addElement($FormField);unset($FormField);$FormField =& new Bs_FormFieldSubmit();$FormField->name         = "submit";$FormField->editability  = 'always';$FormField->caption      = 'Login';$container1->addElement($FormField);unset($FormField);}
function loadRecoverPasswordByUsernameForm($doItAnyway=FALSE) {if (!$doItAnyway && isSet($this->recoverPasswordByUsernameForm)) return;$form =& new Bs_Form();$form->internalName = "recoverPasswordByUsername";$form->name         = "recoverPasswordByUsername";$form->mode         = "add";$form->language     = $this->language;$FormField =& new Bs_FormFieldText();$FormField->name          = 'username';$FormField->caption       = array('en'=>'Username', 'de'=>'Benutzername', 'fr'=>"Nom d'utilisateur", 'it'=>'Nome utente');$FormField->editability   = 'always';$FormField->minLength     = 1;$FormField->maxLength     = 30;$FormField->orderId       = 1000;$FormField->bsDataType    = 'text';$FormField->bsDataInfo    = 1;$FormField->must          = TRUE;$FormField->trim          = 'none';$form->elementContainer->addElement($FormField);unset($FormField);$FormField =& new Bs_FormFieldSubmit();$FormField->name         = "submit";$FormField->editability  = 'always';$FormField->caption      = 'Request password';$form->elementContainer->addElement($FormField);unset($FormField);$this->recoverPasswordByUsernameForm = &$form;}
function looksLikeHack($password, $username=null) {$looksLikeHack = FALSE; if (!isSet($GLOBALS['Bs_Password'])) {$GLOBALS['Bs_Password'] =& new Bs_Password();}
$_Bs_Password = &$GLOBALS['Bs_Password'];$looksLikeHack = $_Bs_Password->looksLikeHack($password, $username);if (isEx($looksLikeHack)) $looksLikeHack = FALSE; return $looksLikeHack;}
function createPassword() {if (!isSet($GLOBALS['Bs_Password'])) {$GLOBALS['Bs_Password'] =& new Bs_Password();}
$Bs_Password =& $GLOBALS['Bs_Password'];return $Bs_Password->createPronounceablePassword(8, 'lnn', TRUE, 'random');}
function _logAttempt() {}
function _prepareLogData($isOk, $failedReason='', $isUserError=TRUE, $looksLikeHack=FALSE, $realData=NULL) {$logArray = array();if (is_array($realData)) {if (isSet($realData['realUserID']))   $logArray['realUserID']   = $realData['realUserID'];if (isSet($realData['realUsername'])) $logArray['realUsername'] = $realData['realUsername'];if (isSet($realData['realPassword'])) $logArray['realPassword'] = $realData['realPassword'];}
$logArray['sentUsername']  = isSet($this->_postVars['username']) ? $this->_postVars['username'] : '';$logArray['sentPassword']  = isSet($this->_postVars['password']) ? $this->_postVars['password'] : '';$logArray['isOk']          = $isOk;$logArray['failedReason']  = $failedReason;$logArray['isUserError']   = $isUserError;$logArray['looksLikeHack'] = $looksLikeHack;$logArray['sessionId']     = $this->bsSession->getSid();$logArray['ip']            = $GLOBALS['HTTP_SERVER_VARS']['REMOTE_ADDR'];$logArray['ipResolved']    = @$GLOBALS['HTTP_SERVER_VARS']['REMOTE_HOST'];$logArray['userAgent']     = @$GLOBALS['HTTP_SERVER_VARS']['HTTP_USER_AGENT'];$logArray['httpReferer']   = @$GLOBALS['HTTP_SERVER_VARS']['HTTP_REFERER'];$logArray['formTimeTaken'] = mktime() - $this->_postVars['bs_form']['startTimestamp'];$logArray['formLanguage']  = $this->_postVars['bs_form']['language'];$logArray['eventDatetime'] = gmdate('Y-m-d H:i:s');return $logArray;}
function _hackAlert() {if (@$this->_APP['emailSysop']) {mail($this->_APP['emailSysop'], 'hacker detected', 'the login form has detected a possible hacker. maybe better have a look yourself.');}
}
function _timeoutOnLoginFailure($secs) {if ($secs > 1) { set_time_limit($secs + 15);sleep($secs); }
}
}
?>