<?php

if (preg_match('/funcauthen.php/i', $_SERVER['PHP_SELF']))
{ header("location:index.php");
  exit;
  }

function authen()
{ global $dbConnection;
		include "common/config/config.php";
		if (isset($_SESSION['SES_USERNAME']) && isset($_SESSION['SES_USERPASS']))
		{ $sql_="SELECT username,
																		userpass 
											FROM $db_appusers 
											WHERE username='".$_SESSION['SES_USERNAME']."' 
																	AND userpass='".$_SESSION['SES_USERPASS']."' 
																	AND accessstatus='1'
											";
				$res_=mysqli_query($dbConnection, $sql_);
				if (mysqli_num_rows($res_)<1)
				{ return false;
						}
				else
				{ $sql="UPDATE $db_appusers 
												SET logintime='".$wktsekarangdb."' 
												WHERE userid='".$_SESSION['SES_USERID']."' 
																		AND username='".$_SESSION['SES_USERNAME']."'
												";
						$res=mysqli_query($dbConnection, $sql);
						return true;
						}
				}		
		else
		{ return false;
				}  
		}

function fullaccess($menuid, $usergroupid)
{ global $dbConnection;
		if ($_SESSION['SES_GROUPID']!='1') // bukan super admin
		{ include "common/config/config.php";
				$qcheck="SELECT semua 
													FROM $db_appauthorizations
													WHERE appgroupid='".$usergroupid."'
																			AND appmenuid='".$menuid."'
													";
				$rcheck=mysqli_query($dbConnection, $qcheck);
				$dcheck=mysqli_fetch_array($rcheck);
				if ($dcheck['semua']==1)
				{ return true;
						}
				else
				{ return false;
						}		
				}		
		else if ($_SESSION['SES_GROUPID']=='1') // super admin
		{ return true;
				}		
		}

function readaccess($menuid, $usergroupid)
{ global $dbConnection;
		if ($_SESSION['SES_GROUPID']!='1') // bukan super admin
		{ include "common/config/config.php";
				$qcheck="SELECT baca 
													FROM $db_appauthorizations
													WHERE appgroupid='".$usergroupid."'
																			AND appmenuid='".$menuid."'
													";
				$rcheck=mysqli_query($dbConnection, $qcheck);
				$dcheck=mysqli_fetch_array($rcheck);
				if ($dcheck['baca']==1)
				{ return true;
						}
				else
				{ return false;
						}		
				}		
		else if ($_SESSION['SES_GROUPID']=='1') // super admin
		{ return true;
				}		
		}

function addaccess($menuid, $usergroupid)
{ global $dbConnection;
		if ($_SESSION['SES_GROUPID']!='1') // bukan super admin
		{ include "common/config/config.php";
				$qcheck="SELECT tambah 
													FROM $db_appauthorizations
													WHERE appgroupid='".$usergroupid."'
																			AND appmenuid='".$menuid."'
													";
				$rcheck=mysqli_query($dbConnection, $qcheck);
				$dcheck=mysqli_fetch_array($rcheck);
				if ($dcheck['tambah']==1)
				{ return true;
						}
				else
				{ return false;
						}		
				}		
		else if ($_SESSION['SES_GROUPID']=='1') // super admin
		{ return true;
				}		
		}

function editaccess($menuid, $usergroupid)
{ global $dbConnection;
		if ($_SESSION['SES_GROUPID']!='1') // bukan super admin
		{ include "common/config/config.php";
				$qcheck="SELECT edit 
													FROM $db_appauthorizations
													WHERE appgroupid='".$usergroupid."'
																			AND appmenuid='".$menuid."'
													";
				$rcheck=mysqli_query($dbConnection, $qcheck);
				$dcheck=mysqli_fetch_array($rcheck);
				if ($dcheck['edit']==1)
				{ return true;
						}
				else
				{ return false;
						}		
				}		
		else if ($_SESSION['SES_GROUPID']=='1') // super admin
		{ return true;
				}		
		}

function deleteaccess($menuid, $usergroupid)
{ global $dbConnection;
		if ($_SESSION['SES_GROUPID']!='1') // bukan super admin
		{ include "common/config/config.php";
				$qcheck="SELECT hapus 
													FROM $db_appauthorizations
													WHERE appgroupid='".$usergroupid."'
																			AND appmenuid='".$menuid."'
													";
				$rcheck=mysqli_query($dbConnection, $qcheck);
				$dcheck=mysqli_fetch_array($rcheck);
				if ($dcheck['hapus']==1)
				{ return true;
						}
				else
				{ return false;
						}		
				}		
		else if ($_SESSION['SES_GROUPID']=='1') // super admin
		{ return true;
				}		
		}

?>