<?php
function throwAdminsForSelect( $iAdminId ){
	$oDB = DataBase::getInstance( );
	if( $iAdminId == - 1 ){
		return $oDB->throwFileArraySmall( DB_ADMINS, 'iAdmin', 'sName' );
	} else{
		$aAdmin = throwAdmin( $iAdminId );
		return Array( $aAdmin['iAdmin'] => $aAdmin['sName'] );
	}
}
function throwAdminsPrivilages( $iAdmin ){
	$oDB = DataBase::getInstance( );
	$aFile = $oDB->throwFileArray( DB_ADMINS_PRIVILAGES );
	if( isset( $aFile ) ){
		$iCount = count( $aFile );
		for( $i = 0;$i < $iCount;$i++ ){
			if( $aFile[$i]['iAdmin'] == $iAdmin ){
				$aReturn[$aFile[$i]['sActions']] = true;
			}
		}
	}
	$aReturn['admins-privilages'] = true;
	if( isset( $aReturn ) ){
		return $aReturn;
	}
}
function saveAdmin( $aForm ){
	$oDB = DataBase::getInstance( );
	if( isset( $aForm['iAdmin'] ) && is_numeric( $aForm['iAdmin'] ) ){
		$sParam = 'iAdmin';
		$oDB->deleteInFile( DB_ADMINS_PRIVILAGES, $aForm['iAdmin'], 'iAdmin' );
		$aData = $oDB->throwData( DB_ADMINS, $aForm['iAdmin'], 'iAdmin' );
		if( !isset( $aForm['sPass'] ) || empty( $aForm['sPass'] ) ) $aForm['sPass'] = $aData['sPass'];
		else $aForm['sPass'] = md5( $aForm['sPass'] );
	} else{
		$aForm['sPass'] = md5( $aForm['sPass'] );
		$sParam = null;
	}
	$aForm = changeMassTxt( $aForm, '' );
	$aForm['iAdmin'] = $oDB->save( DB_ADMINS, $aForm, $sParam, 'sort' );
	if( isset( $aForm['aPrivilagesForm'] ) ){
		foreach( $aForm['aPrivilagesForm'] as $aForm['sActions'] => $iValue ){
			$oDB->save( DB_ADMINS_PRIVILAGES, $aForm );
		}
	}
	return $aForm['iAdmin'];
}
function saveAdminSelf( $aForm ){
	$aForm['iAdmin'] = $_SESSION['iAdminId'];
	if( !isset( $aForm['iAdmin'] ) || $aForm['iAdmin'] <= 0 ) die( 'HACKING ATTEMPT' );
	$oDB = DataBase::getInstance( );
	if( isset( $aForm['iAdmin'] ) && is_numeric( $aForm['iAdmin'] ) ){
		$sParam = 'iAdmin';
		$aData = $oDB->throwData( DB_ADMINS, $aForm['iAdmin'], 'iAdmin' );
		$aForm['sLogin'] = $aData['sLogin']; //Nie można zmienić loginu
		if( !isset( $aForm['sPass'] ) || empty( $aForm['sPass'] ) ) $aForm['sPass'] = $aData['sPass'];
		else $aForm['sPass'] = md5( $aForm['sPass'] );
	}
	$aForm = changeMassTxt( $aForm, '' );
	$oDB->save( DB_ADMINS, $aForm, $sParam, 'sort' );
	return $aForm['iAdmin'];
}
function deleteAdmin( $iAdmin ){
	$oDB = DataBase::getInstance( );
	$oDB->deleteInFile( DB_ADMINS, $iAdmin, 'iAdmin' );
	$oDB->deleteInFile( DB_ADMINS_PRIVILAGES, $iAdmin, 'iAdmin' );
}
function listAdmins( $sFile = 'admins_list.tpl' ){
	$oDB = DataBase::getInstance( );
	$oTpl = TplParser::getInstance( );
	$aAdmins = $oDB->executeSQL( 'SELECT * FROM `' . $oDB->throwTableName( DB_ADMINS ) . '` ' . $oDB->generateDefaultSort( DB_ADMINS ), true, DataBase::ASSOCIATIVE, 'iAdmin' );
	$oTpl->assign( 'aAdmins', $aAdmins );
	return $oTpl->parse( $sFile );
}
function checkAdminLogin( $sLogin, $sPass, $sKey ){
	$oDB = DataBase::getInstance( );
	$aData = $oDB->throwData( DB_ADMINS, $sLogin, 'sLogin' );
	if( isset( $aData ) && $aData['sPass'] == md5( $sPass ) && $aData['iStatus'] ){
		$_SESSION[$sKey] = true;
		$_SESSION['iAdminId'] = $aData['iAdmin'];
		return 1;
	} else return 0;
}
function checkAdminPrivilages( $aActions ){
	if( $aActions['f'] == 'admins' && ( $aActions['a'] == 'privilages' || $aActions['a'] == 'self' ) ) return 0;
	if( isset( $_SESSION['iAdminId'] ) ){
		if( $_SESSION['iAdminId'] == - 1 ) return true;
		foreach( $GLOBALS['aPrivilages'] as $sAction => $sValue ){
			if( $aActions['f'] . '-' . $aActions['a'] == $sAction ){
				$sAction2Check = $sAction;
				break;
			} elseif( $aActions['f'] == $sAction ){
				$sAction2Check = $sAction;
				break;
			}
		}
		if( isset( $sAction2Check ) ){
			$aPrivilagesAdmin = throwAdminsPrivilages( $_SESSION['iAdminId'] );
			if( isset( $aPrivilagesAdmin[$sAction2Check] ) ) $bOk = true;
		} else{
			$bOk = true;
		}
	}
	if( !isset( $bOk ) ){
		header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=admins-privilages' );
		exit;
	}
}
?>