[code=php]
<?php
/**
 * Loginclass
 * This class is build to build a protected login
 * 
 * @author: Arjan Veldkamp (ArieMedia.nl)
 * @version: 0.1
 **/
class inloggen {
	
	/**
	 * Database class
	 *
	 * @param Smarty databaseclass
	 **/
	private $oDB;
	/**
	 * Save succesvol validated information
	 *
	 * @param void
	 **/
	protected $aUser = array('naam' => '', 'pass' => '');
	
	/**
	 * Save the exception
	 *
	 * @param string
	 **/
	protected $sError = 'Geen errors om te laten zien';
	
	/**
	 * Here we store the databasesettings
	 *
	 * @param array
	 **/
	 
	 protected $aData = array('tabel', 'fields' => array(), 'controllers' => array());
	/**
	 * The constructor
	 * Here we start the databaseconnection
	 * If you dont need a connection use false
	 *
	 * @param boolean
	 * @return void
	 **/
	public function __construct($bDB=true) {
		session_start();
/** Laad hier je AdoDB-class en stel hem in.
		require_once 'pad/naar/globals/globals.php';
		require_once _MAINPATH_ .'shell/engine.php';
		$oShell = new Shell();
		$this->oDB = $oShell->oDB;
*/
	}
	
	/**
	 * The setter
	 * So we can savely set the name and password
	 *
	 * @param string, string
	 * @return void
	 **/
	
	public function __set($sKey, $sVal) {
		try {
			switch($sKey) {
				case 'Name':
					$this->_ValidateName($sVal);
				break;
				case 'Pass':
					$this->_ValidatePass($sVal);
				break;
				case 'Fields':
					$this->_ValidateFields($sVal);
				break;
				case 'Tabel':
					$this->aData['tabel'] = $sVal;
				break;
				case 'NameField':
					$this->aData['controllers']['naam'] = $sVal;
				break;
				case 'PassField':
					$this->aData['controllers']['pass'] = $sVal;
				break;
			}
		} catch(Exception $e) {
			$this->sError = '<strong>FOUT: </strong><br>Bericht: '.$e->getMessage().' op regel '.$e->getLine();
		}
	}
	
	/**
	 * The getter
	 * We can get the userinformation and debugging information
	 *
	 * @param string
	 * @return ???
	 **/
	
	public function __get($sKey) {
		switch($sKey) {
			case 'aUser': return $this->aUser; break;
			case 'sError': return $this->sError; break;
			default: return false; break;
		}
	}
	
	/**
	 * The login
	 * Here we make the session
	 *
	 * @param void
	 * @return boolean
	 **/
	
	public function Login() {
		try {
			if(count($this->aData['fields']) == 0) {
				throw new Exception('Er zijn geen velden gezet, gebruik $oClass->Fields = array()');	
			} else {
				$sFields = implode(', ', $this->aData['fields']);
			}
			
			if(count($this->aData['controllers']) == 0) {
				throw new Exception('Er zijn geen controllers gezet, gebruik $oClass->Controllers = array()');	
			} else {
				$sControl = 'WHERE '.$this->aData['controllers']['naam'] .'= "'.$this->aUser['naam'].'"
							AND '.$this->aData['controllers']['pass'] .' = "'.$this->aUser['pass'].'"';
			}
			
			if(empty($this->aData['tabel'])) {
				throw new Exception('Er is geen tabel ingesteld!');	
			} else {
				$sTabel = $this->aData['tabel'];	
			}
			
			$sql = 'SELECT '.$sFields.'
					FROM '.$sTabel.' '
					.$sControl;
					
			if(!$this->oDB->Execute($sql)) {
				throw new Exception('Query is mislukt!<br>'.$sql);
			} else {
				$rs = $this->oDB->Execute($sql);
				if($rs->RecordCount() > 0) {
					$iFields = count($this->aData['fields']);
					for($i=0; $i<$iFields; $i++) {
						$_SESSION[$this->aData['fields'][$i]] = $rs->Fields($this->aData['fields'][$i]);
					}
					return true;
				} else return false;
			}
		}
		catch(Exception $e) {
			$this->sError = '<strong>FOUT: </strong><br>Bericht: '.$e->getMessage().' op regel '.$e->getLine();	
		}
	}
	
	/**
	 * The namevalidator
	 * if succesfull we will save it in $this->aUser['naam']
	 * 
	 * @param string
	 * @return boolean
	 **/
	
	final protected function _ValidateName($sName) {
		if(!empty($sName) && preg_match('/[a-zA-Z0-9]+/', $sName, $aMatch)) {
			$this->aUser['naam'] = htmlentities($aMatch[0], ENT_QUOTES);
			return true;
		} else {
			throw new Exception('Validatie gebruikersnaam komt niet overeen!');
			return false;
		}
	}
	
	/**
	 * The passwordvalidator
	 * if succesfull we will save it in $this->aUser['pass']
	 * 
	 * @param string
	 * @return boolean
	 **/
	
	final protected function _ValidatePass($sPass) {
		if(!empty($sPass) && preg_match('/([a-zA-Z0-9]{8})/', $sPass, $aMatch)) {
			$this->aUser['pass'] = md5($aMatch[0]);
			return true;
		} else {
			throw new Exception('Validatie wachtwoord komt niet overeen!');
			return false;
		}
	}
	
	/**
	 * Fieldsvalidator
	 *
	 * @param array
	 * @return void
	 **/
	
	final protected function _ValidateFields($aFields) {
		if(is_array($aFields)) {
			$this->aData['fields'] = $aFields;
		} else {
			throw new Exception('Tabelvelden moeten in een array staan!');	
		}
	}
	
	final protected function _ValidateControllers($aControllers) {
		if(is_array($aControllers)) {
			$this->aData['controllers'] = $aControllers;			
		} else {
			throw new Exception ('Controllers moeten in een array staan!');	
		}
	}
}
