class_LospAuth.inc.php 4.83 KB
<?php
/**
 * @file	LospAuth.inc.php
 * @category freeSN
 * @mailto	code [at] netz.coop
 * @version 0.4.200901
 * @link http://netz.coop
 * 
 *  @copyright Copyright by netz.coop e.G. 2015
 *  
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *   This program 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 General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
/**
 *	class is responsible for the SESSIONs, COOKIEs and makes the login process
 */
class LospAuth {

	/**
	 *	Funktion überprüft den Login
	 *
	 *	Ist ein Anwender angemeldet oder war seine Anmeldung erfolgreich, gibt die Funktion
	 *	true zurück, andernfalls false.
	 *	inspiriert von http://www.selfphp.info/praxisbuch/praxisbuchseite.php?site=351&group=69
	 *
	 */
	private static function checkLogin(Account &$param_Account, $_POST_ID, $_POST_Password, $_GET_LOGOUT){

		session_start();

		if (!isset($_SESSION["ID"]) && !isset($_POST_ID)) {
			setcookie("HostProfileID", "", time() - CONFIG::getSettings('Timeout'));
			return false;
		}

		if (($_POST_ID)) {
			if (!isset($_SESSION["versuch"])) {
				$_SESSION["versuch"]=1;
			} else {
				$_SESSION["versuch"]++;
			}

			if (!isset($_COOKIE[session_name()])) {
				setcookie("HostProfileID", "", time() - CONFIG::getSettings('Timeout'));
				return Error::newError('DevError',"Session Falsch","!isset(_COOKIE[session_name()])==".(!isset($_COOKIE[session_name()]))."");
			}

			if($param_Account->checkPassword(trim($_POST_ID),trim($_POST_Password))) {
				$_SESSION["ID"]=$param_Account->getObjVar('ID');
				return true;
			} else {
				return Error::newError('UserError',"ID oder Passwort falsch","256");
			}
		}

		if ($_GET_LOGOUT=="logout") {
			session_destroy();
			session_unset();
			return false;
		} else if($_SESSION["ID"]) {

			$param_Account->setObjVar('ID', $_SESSION["ID"]);
			return true;
		} else {
//			D::li("checkLogin(Account &$param_Account, $_POST_ID, $_POST_Password, $_GET_LOGOUT)".$_SESSION["ID"],1,1)			;
			return Error::newError('UserError',"no login data",E_USER_ERROR);

		}
	}

	public static function login(Account &$HostAccount, iStorable &$param_iStorable){
		$id = $password = $logout = null;
		if(array_key_exists('ID', $_POST)) {
			$id = $_POST['ID'];
		}
		if(array_key_exists('password', $_POST)) {
			$password = $_POST['password'];
		}
		if(array_key_exists('p', $_GET) && array_key_exists(0, $_GET['p'])) {
			$logout = $_GET['p'][0];
		}

		if(self::checkLogin($HostAccount, $id, $password, $logout)) {
			$HostAccount->loadObject($HostAccount, $param_iStorable);
			$HostAccount->setObjVar('LoginState', true);

			$HostAccount->loadMainProfile($param_iStorable);

			self::chooseActiveProfile($HostAccount, $param_iStorable);

			if(array_key_exists('loadRight', $_GET) && is_array($_GET['loadRight'])) {
//D::show($_GET['loadRight'], 'loadRight     setRightRestrictions');
				$HostAccount->setRightRestrictions($_GET['loadRight']);
			}


			if($id && $password) {
				$var_doLoadLevel = array('login');
				Controller::loadLevel($HostAccount, $param_iStorable, $var_doLoadLevel);
			}
			return true;
		} else {
			if($logout=='logout') {
				$var_doLoadLevel = array('logout');
				Controller::loadLevel($HostAccount, $param_iStorable, $var_doLoadLevel);
			}
			$HostAccount->setNoLoginMainProfile($param_iStorable);
//			D::li($HostAccount,1,1);
			return false;
		}
	}

	private static function chooseActiveProfile(Account &$HostAccount, iStorable &$param_iStorable) {
		if(array_key_exists('HostProfileID', $_GET) && $_GET['HostProfileID']){
			if($HostAccount->checkIsChooseAble($param_iStorable, $_GET['HostProfileID'])){
				setcookie('HostProfileID', $_GET['HostProfileID']);
				Account::setActiveProfile($HostAccount, $param_iStorable, $_GET['HostProfileID']);
			} else {
				Account::setActiveProfile($HostAccount, $param_iStorable, $HostAccount->getMainProfile()->getID());
			}
		} else if(array_key_exists('HostProfileID', $_COOKIE) && $_COOKIE['HostProfileID']) {
			if($HostAccount->checkIsChooseAble($param_iStorable, $_COOKIE['HostProfileID'])){
				Account::setActiveProfile($HostAccount, $param_iStorable, $_COOKIE['HostProfileID']);
			} else {
				Account::setActiveProfile($HostAccount, $param_iStorable, $HostAccount->getMainProfile()->getID());
			}
		} else if(is_object($HostAccount->getMainProfile())) {
			Account::setActiveProfile($HostAccount, $param_iStorable, $HostAccount->getMainProfile()->getID());
		}
	}

}



?>