<?php
if (!defined("root")) {
	define("root", $_SERVER['DOCUMENT_ROOT']);
}
require(root."/___simple_config.php");
session_start();
require(root."/".adminFolder."/_common.php");
require(root."/".adminFolder."/_lib/db.php");
require(root."/".adminFolder."/_lib/main.php");
require(root."/".adminFolder."/_lib/auth.php");
require(root."/".adminFolder."/_lib/bootstraps.php");

$pageSubOf = "settings";
$pageName = "user_authorization";
$pageTitle = "User Authorization/Management";
$thisUrl = "users/authorization";
$ajax_page = "ajax_user_authorization.php";
$pageUrl = "/".adminFolder."/$thisUrl";
$a = cekpost(cleanQuery($_GET["a"]), "");
$err = "";
if ($a=="section_main") {
	if (!authorizedModule("user_authorization_main", $ses_kduser)) {
		unAuthorizedAccess("user_authorization_main");
	} else {
		$fil_username = trim(cekpost(cleanQuery($_REQUEST["fil_username"]), ""));
		$fil_inisial = trim(cekpost(cleanQuery($_REQUEST["fil_inisial"]), ""));
		$fil_isactive = trim(cekpost(cleanQuery($_REQUEST["fil_isactive"]), ""));
		$swhere = '';
		if ($fil_username!="") { $swhere .= " AND a.username LIKE '%$fil_username%' "; }
		if ($fil_inisial!="") { $swhere .= " AND a.inisial LIKE '%$fil_inisial%' "; }
		if ($fil_isactive!="") { $swhere .= " AND a.isactive = '$fil_isactive' "; }
		
		$rs=array(); $q="SELECT a.* FROM _users a WHERE kduser!=0 $swhere";
		callRS($rs, $q);
		if (count($rs)!=0) {
			$ld = 20;
			$cp = cekpost(cleanQuery($_GET["cp"]), "1");
			$tp = intval(count($rs) / $ld) + (count($rs)%$ld==0?0:1);
			$sr = ($cp-1) * $ld;
			$pagination = '';
			if ($tp!=1) {
				$pagination .= ('<ul class="pagination pagination-sm">');
					$disabled = '';
					$purl = '#';
					$onclick = '';
					if ($cp<=1) {
						$disabled = 'disabled';
						$onclick = ' onclick="return false;" ';
					}
					$pagination .= ('<li class="'.$disabled.'"><a href="'.$purl.'" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', 1);return false;"').'>&lt;&lt;</a></li>');
					$pagination .= ('<li class="'.$disabled.'"><a href="'.$purl.'" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.($cp-1).');return false;"').'>&lt;</a></li>');
					for ($c=1;$c<=$tp;$c++) {
						$active = '';
						$purl = '#';
						$disabled = '';
						$onclick = '';
						if ($c==$cp) {
							$active = 'active';
						} else {
							$purl = '#';
							$onclick = ' onclick="filterReload(\''.$pageName.'\', '.$c.');return false;" ';
						}
						$pagination .= ('<li class="'.$active.' '.$disabled.'"><a href="'.$purl.'" '.$onclick.'>'.$c.'</a></li>');
					}
					$disabled = '';
					$onclick = '';
					if ($cp>=$tp) {
						$disabled = 'disabled';
						$onclick = ' onclick="return false;" ';
					}
					$pagination .= ('<li class="'.$disabled.'"><a href="#" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.($cp+1).');return false;"').'>&gt;</a></li>');
					$pagination .= ('<li class="'.$disabled.'"><a href="#" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.$tp.');return false;"').'>&gt;&gt;</a></li>');
				$pagination .= ('</ul>');
			}
			cout('<div class="alert alert-info" role="alert"><strong>Total whole data</strong> <span class="badge">'.count($rs).'</span></div>');
			
			if ($tp!=1) {
				cout($pagination);
			}
			cout('<table class="table table-striped table-bordered">');
				cout('<thead>');
					cout('<tr>');
						cout('<th class="text-center">#ID</th>');
						if (authorizedModule("user_authorization_element_username", $ses_kduser)) {
							cout('<th class="text-center">Username</th>');
						}
						if (authorizedModule("user_authorization_element_initial", $ses_kduser)) {
							cout('<th class="text-center">Initial</th>');
						}
						if (authorizedModule("user_authorization_element_authorizations", $ses_kduser)) {
							cout('<th class="text-center">Auth</th>');
						}
						if (authorizedModule("user_authorization_element_active", $ses_kduser)) {
							cout('<th class="text-center">Active Status</th>');
						}
						cout('<th class=""></th>');
					cout('</tr>');
				cout('</thead>');
				cout('<tbody>');
				$rsmo=array(); $qmo="SELECT COUNT(kdmod) AS tmod FROM _admin_mods";
				callRS($rsmo, $qmo);
				$cnt = 0;
				for ($i=$sr;$i<count($rs) && $cnt<$ld;$i++) {
					$cnt++;
					$kduser = $rs[$i]["kduser"];
					if (intval($rs[$i]["isactive"])==0) {
						$isactive = '<a href="#" title="Activate" onclick="chActive(\''.$pageName.'\', '.$kduser.', '.$rs[$i]["isactive"].');return false;">Non-Active</a>';
					} else {
						$isactive = '<a href="#" title="Deactivate" onclick="chActive(\''.$pageName.'\', '.$kduser.', '.$rs[$i]["isactive"].');return false;">Active</a>';
					}
					
					if (intval($rs[$i]["ison"])==0) {
						$ison = '<a href="#" title="Set Online" onclick="chBankOnOff(\''.$pageName.'\', '.$kduser.', '.$rs[$i]["ison"].');return false;">OFF line</a>';
					} else {
						$ison = '<a href="#" title="Set Offline" onclick="chBankOnOff(\''.$pageName.'\', '.$kduser.', '.$rs[$i]["ison"].');return false;">ON line</a>';
					}
					
					$rsd=array(); $qd="SELECT COUNT(kdauth) AS tauth FROM _user_auth WHERE isallowed=1 AND kduser='$kduser'";
					callRS($rsd, $qd);
					cout('<tr>');
						cout('<td class="text-center">'.$kduser.'</td>');
						if (authorizedModule("user_authorization_element_username", $ses_kduser)) {
							cout('<td class="text-center"><a href="'.$pageUrl.'/view/'.$kduser.'/">'.$rs[$i]["username"].'</a></td>');
						}
						if (authorizedModule("user_authorization_element_initial", $ses_kduser)) {
							cout('<td class="text-center"><a href="'.$pageUrl.'/view/'.$kduser.'/">'.$rs[$i]["inisial"].'</a></td>');
						}
						if (authorizedModule("user_authorization_element_authorizations", $ses_kduser)) {
							cout('<td class="text-center">'.$rsd[0]["tauth"].' / '.$rsmo[0]["tmod"].'</td>');
						}
						if (authorizedModule("user_authorization_element_active", $ses_kduser)) {
							cout('<td class="text-center isactive'.$kduser.'">'.$isactive.'</td>');
						}
						cout('<td class="text-center">');
							if (authorizedModule("user_authorization_edit", $ses_kduser)) {
								cout('<a href="'.$pageUrl.'/edit/'.$kduser.'/"><span class="glyphicon glyphicon-pencil"></span><span class="hidden-xs hidden-sm"> Edit</span></a>');
								cout(BSTextSeparator());
							}
							if (authorizedModule("user_authorization_change_password", $ses_kduser)) {
								cout('<a href="'.$pageUrl.'/chpass/'.$kduser.'/"><span class="glyphicon glyphicon-lock"></span><span class="hidden-xs hidden-sm"> Ch. Pass.</span></a>');
								cout(BSTextSeparator());
							}
							if (authorizedModule("user_authorization_authorization", $ses_kduser)) {
								cout('<a href="'.$pageUrl.'/auth/'.$kduser.'/"><span class="glyphicon glyphicon-eye-open"></span><span class="hidden-xs hidden-sm"> Auth</span></a>');
								cout(BSTextSeparator());
							}
							if (authorizedModule("user_authorization_delete", $ses_kduser)) {
								cout('<a href="'.$pageUrl.'/delete/'.$kduser.'/"><span class="glyphicon glyphicon-trash"></span><span class="hidden-xs hidden-sm"> Delete</span></a>');
							}
						cout('</td>');
					cout('</tr>');
				}
				cout('</tbody>');
			cout('</table>');
			
			if ($tp!=1) {
				cout($pagination);
			}
			cout('<div class="alert alert-info" role="alert"><strong>Total whole data</strong> <span class="badge">'.count($rs).'</span></div>');
			$act_ket = "Users: Main List";
			if ($swhere!="") { $act_ket .= ": ".cleanQuery($swhere).""; }
			updateUserActivities($act_ket);
		} else {
			cout('<div class="alert alert-info" role="alert">');
				cout('<strong>No data available.</strong> Please add new data for display.');
			cout('</div>');
		}
	}
}
if ($a=="chactive") {
	$kd = cekpost(cleanQuery($_REQUEST["kd"]), "");
	$ca = cekpost(cleanQuery($_REQUEST["ca"]), "");
	$kd = (trim($kd)=="undefined")?'':$kd;
	$ca = (trim($ca)=="undefined")?'':$ca;
	if ($kd!="" && $kd!=0 && $ca!="") {
		$na = $ca==0?1:0;
		$modby = $_SESSION["user"]["inisial"];
		$modtime = time();
		$qu = "UPDATE _users SET isactive='$na', modby='$modby', modtime='$modtime' WHERE kduser='$kd'";
		execRS($qu);
		$act_ket = "Users: Ch. Active [kd: $kd][from Act: $ca][to Act: $na]";
		updateUserActivities($act_ket);
		if (intval($na)==0) {
			$isactive = '<a href="#" title="Activate" onclick="chActive(\''.$pageName.'\', '.$kd.', '.$na.');return false;">Non-Active</a>';
		} else {
			$isactive = '<a href="#" title="Deactivate" onclick="chActive(\''.$pageName.'\', '.$kd.', '.$na.');return false;">Active</a>';
		}
		echo $isactive;
	}
}

if ($a=="section_view") {
	if (!authorizedModule("user_authorization_view", $ses_kduser)) {
		unAuthorizedAccess("user_authorization_view");
	} else {
		$id = cekpost(cleanQuery($_GET["id"]), "0");
		$rs=array(); $q="SELECT a.* FROM _users a WHERE kduser='$id'";
		callRS($rs, $q);
		if (intval($rs[0]["isactive"])==0) {
			$isactive = '<a href="#" title="Activate" onclick="chActive(\''.$pageName.'\', '.$rs[0]["kduser"].', '.$rs[0]["isactive"].');return false;">Non-Active</a>';
		} else {
			$isactive = '<a href="#" title="Deactivate" onclick="chActive(\''.$pageName.'\', '.$rs[0]["kduser"].', '.$rs[0]["isactive"].');return false;">Active</a>';
		}
		$creby = (trim($rs[0]["creby"])!=""?'By: <strong>'.$rs[0]["creby"].'</strong>':'By: &lt;<em>no data</em>&gt;').(trim($rs[0]["cretime"])?' ('.date('d M Y, H:i:s A', $rs[0]["cretime"]).')':' &lt;<em>no data</em>&gt;');
		$modby = (trim($rs[0]["modby"])!=""?'By: <strong>'.$rs[0]["modby"].'</strong>':'By: &lt;<em>no data</em>&gt;').(trim($rs[0]["modtime"])?' ('.date('d M Y, H:i:s A', $rs[0]["modtime"]).')':' &lt;<em>no data</em>&gt;');
		cout('<dl class="dl-horizontal dldata">');
			if (authorizedModule("user_authorization_element_username", $ses_kduser)) {
				cout('<dt>UserName</dt>');
				cout('<dd>'.$rs[0]["username"].'</dd>');
			}
			if (authorizedModule("user_authorization_element_initial", $ses_kduser)) {
				cout('<dt>Initial</dt>');
				cout('<dd>'.$rs[0]["inisial"].'</dd>');
			}
			if (authorizedModule("user_authorization_element_active", $ses_kduser)) {
				cout('<dt>Active Status</dt>');
				cout('<dd class="isactive'.$rs[0]["kduser"].'">'.$isactive.'</dd>');
			}
			if (authorizedModule("user_authorization_element_creation", $ses_kduser)) {
				cout('<dt>Created</dt>');
				cout('<dd>'.$creby.'</dd>');
			}
			if (authorizedModule("user_authorization_element_modification", $ses_kduser)) {
				cout('<dt>Modified</dt>');
				cout('<dd>'.$modby.'</dd>');
			}
			
		cout('</dl>');
		
		$act_ket = "Banks: View: [kduser: ".$rs[0]["kduser"]."][namabank: ".$rs[0]["namabank"]."][inisial: ".$rs[0]["inisial"]."]";
		updateUserActivities($act_ket);
		exit;
	}
}
if ($a=="do_delete") {
	$kirim = cekpost(cleanQuery($_REQUEST["kirim"]), "");
	if ($kirim=="1") {
		$delby = $_SESSION["user"]["inisial"];
		$deltime = time();
		$kduser = cekpost(cleanQuery($_POST["kduser"]), "0");
		$rs=array(); $q="SELECT * FROM banks WHERE kduser='$kduser'";
		callRS($rs, $q);
		
		$q = "DELETE FROM banks WHERE kduser='$kduser'";
		execRS($q);
		$act_ket = "Banks: Delete: Deleted [kduser: $kduser][namabank: ".$rs[0]["namabank"]."][inisial: ".$rs[0]["inisial"]."][isactive: ".$rs[0]["isactive"]."]";
		updateUserActivities($act_ket);
		cout('<script type="text/javascript">parent.window.location="'.$pageUrl.'/";</script>');
		exit;
	}
}
if ($a=="section_delete") {
	if (!authorizedModule("user_authorization_delete", $ses_kduser)) {
		unAuthorizedAccess("user_authorization_delete");
	} else {
		$id = cekpost(cleanQuery($_GET["id"]), "0");
		$rs=array(); $q="SELECT * FROM _users WHERE kduser='$id'";
		callRS($rs, $q);
		
		cout('<form method="post" target="quick_iframe" action="/'.adminFolder.'/_ajax/'.$ajax_page.'?a=do_delete" role="form">');
			cout('<input type="hidden" name="kirim" id="kirim" value="1" />');
			cout('<input type="hidden" name="kduser" id="kduser" value="'.$rs[0]["kduser"].'" />');
			cout('<dl class="dl-horizontal dldata">');
				if (authorizedModule("user_authorization_element_username", $ses_kduser)) {
					cout('<dt>Bank Name</dt>');
					cout('<dd>'.$rs[0]["username"].'</dd>');
				}
				if (authorizedModule("user_authorization_element_initial", $ses_kduser)) {
					cout('<dt>Initial</dt>');
					cout('<dd>'.$rs[0]["inisial"].'</dd>');
				}
				cout('<dt></dt>');
				cout('<dd><button class="btn btn-primary" type="submit">Delete</button></dd>');
			cout('</dl>');
		cout('</form>');
		$viewFrame = 0;
		if ($viewFrame==1) {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="100" style="overflow:;border:1px red solid;" visibility="" scrolling="yes" frameborder="1"></iframe>');
		} else {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="11" style="overflow:hidden;" visibility="hidden" scrolling="no" frameborder="0"></iframe>');
		}
		$act_ket = "Users: Deleting... [kd: ".$rs[0]["kd"]."][username: ".$rs[0]["username"]."]";
		updateUserActivities($act_ket);
		exit;
	}
}
if ($a=="do_auth") {
	$kirim = cekpost(cleanQuery($_REQUEST["kirim"]), "");
	$err = "";
	if ($kirim=="1") {
		$kduser = cekpost(cleanQuery($_REQUEST["kduser"]), "0");
		$qdel = "DELETE FROM _user_auth WHERE kduser=".$kduser."";
		execRS($qdel);
		$rstmp=array(); $qtmp = "SELECT a.*, mod_name, mod_label, page_name, page_label FROM _user_auth_tmp a 
			LEFT JOIN _admin_mods b ON b.kdmod=a.kdmod 
			LEFT JOIN _admin_pages c ON c.kdpage=b.kdpage 
			WHERE a.kduser='".$kduser."'";
		callRS($rstmp, $qtmp);
		
		$qtmp = "SELECT a.*, page_name, page_label FROM _admin_mods a
			LEFT JOIN _admin_pages b ON b.kdpage=a.kdpage
		";
		callRS($rstmp, $qtmp);
		$rs_t = array(); $q_t = "SELECT * FROM _user_auth_tmp WHERE kduser='".$kduser."'";
		callRS($rs_t, $q_t);
		$att = array();
		for ($tt=0;$tt<count($rs_t);$tt++) {
			$att[$rs_t[$tt]["kdmod"]] = $rs_t[$tt]["isallowed"];
		}
		
		$kdauth = getID("_user_auth", "kdauth");
		$query = "INSERT INTO _user_auth (kdauth, kduser, kdmod, isallowed) VALUES";
		#$str = '';
		$cnt = 0;
		for ($t=0;$t<count($rstmp);$t++) {
			$cnt+= 1;
			$kdauth += 1;
			$kdmod = $rstmp[$t]["kdmod"];
			$isallowed = cekpost(cleanQuery($_REQUEST[$rstmp[$t]["mod_name"]]), "")!=""?1:0;
			if ($cnt>1) { $query .= " , "; }
			$query .= "('".$kdauth."', '".$kduser."', '".$kdmod."', '".$isallowed."')";
			#$str .= ''.$rstmp[$t]["mod_name"].': ['.cekpost(cleanQuery($_REQUEST[$rstmp[$t]["mod_name"]]), "").']'."<br/>\n";
		}
		#echo $query;
		#exit;
		execRS($query);
		#echo $str;
		#exit;
		$qdel = "DELETE FROM _user_auth_tmp WHERE kduser=".$kduser."";
		execRS($qdel);
		cout('<script type="text/javascript">parent.window.location="'.$pageUrl.'/view/'.$kduser.'/";</script>');
		exit;
	}
	exit;
}
if ($a=="section_auth") {
	if (!authorizedModule("user_authorization_authorization", $ses_kduser)) {
		unAuthorizedAccess("user_authorization_authorization");
	} else {
		$id = cekpost(cleanQuery($_GET["id"]), "0");
		$kduser = $id;
		$rs=array(); $q="SELECT * FROM _users WHERE kduser='$id'";
		callRS($rs, $q);
		
		$rsmod=array(); $qmod = "SELECT * FROM _admin_mods";
		callRS($rsmod, $qmod);
		$rsd=array(); $qd = "SELECT * FROM _user_auth WHERE kduser='".$kduser."'";
		callRS($rsd, $qd);
		$rstmp=array(); $qtmp = "SELECT * FROM _user_auth_tmp WHERE kduser='".$kduser."'";
		callRS($rstmp, $qtmp);
		if (count($rstmp)!=0) {
			# jika tmp ga kosong, berarti ada isinya, done;
		} else {
			#jika tmp kosong, berarti harus di isi, ambil dari data _user_auth
			$kdtmp = getID("_user_auth_tmp", "kd");
			$qi = "INSERT INTO _user_auth_tmp (kd, kduser, kdmod, isallowed) VALUES ";
			$cnt = 0;
			if (count($rsd)!=0) {
				#jika data _user_auth ga kosong, berarti ada isinya, masukin ke tmp
				for ($d=0;$d<count($rsd);$d++) {
					$cnt += 1;
					$kdtmp+= 1;
					if ($cnt>1) { $qi .= " , "; }
					$qi .= "('".$kdtmp."', '".$kduser."', '".$rsd[$d]["kdmod"]."', '".$rsd[$d]["isallowed"]."')";
				}
			} else {
				#jika data _user_auth pun kosong, berarti ambil dari _admin_mods dan isallowed = 0;
				for ($d=0;$d<count($rsmod);$d++) {
					$cnt += 1;
					$kdtmp+= 1;
					if ($cnt>1) { $qi .= " , "; }
					$qi .= "('".$kdtmp."', '".$kduser."', '".$rsmod[$d]["kdmod"]."', '0')";
				}
			}
			execRS($qi);
		}
		#panggil sekali lagi utk pembacaan data
		$rstmp=array(); $qtmp = "SELECT a.*, mod_name, mod_label, page_name, page_label FROM _user_auth_tmp a 
			LEFT JOIN _admin_mods b ON b.kdmod=a.kdmod 
			LEFT JOIN _admin_pages c ON c.kdpage=b.kdpage 
			WHERE a.kduser='".$kduser."'";
		$qtmp = "SELECT c.*, mod_name, mod_label, page_name, page_label FROM _admin_mods a
			LEFT JOIN _admin_pages b ON b.kdpage=a.kdpage
			LEFT JOIN _user_auth_tmp c ON c.kdmod=a.kdmod
			WHERE c.kduser='".$kduser."'
		";
		$qtmp = "SELECT c.*, mod_name, mod_label, page_name, page_label FROM _admin_pages a
			LEFT JOIN _admin_mods b ON b.kdpage=a.kdpage
			LEFT JOIN _user_auth_tmp c ON c.kdmod=b.kdmod
			WHERE c.kduser='".$kduser."'
		";
		$qtmp = "SELECT a.*, page_name, page_label FROM _admin_mods a
			LEFT JOIN _admin_pages b ON b.kdpage=a.kdpage
		";
		callRS($rstmp, $qtmp);
		
		$rs_t = array(); $q_t = "SELECT * FROM _user_auth_tmp WHERE kduser='".$kduser."'";
		callRS($rs_t, $q_t);
		$att = array();
		for ($tt=0;$tt<count($rs_t);$tt++) {
			$att[$rs_t[$tt]["kdmod"]] = $rs_t[$tt]["isallowed"];
		}
		#printR($att);
		#echo $qtmp;
		
		$str = '<table class="table table-striped"><tr><td>kd</td><td><input type="checkbox" onclick="selectAll(\'all-auth\', $(this));" /></td><td>kdmod</td><td>mod_name</td><td>mod_label</td><td>isallowed</td></tr>';
		$p_name = $rstmp[0]["page_name"];
		for ($t=0;$t<count($rstmp);$t++) {
			$tp_name = $rstmp[$t]["page_name"];
			if ($t==0) {
				$str .= '<tr>
					<td>Page</td>
					<td colspan="2"><input class="all-auth" type="checkbox" onclick="selectAll(\'page-auth-'.$p_name.'\', $(this));" /></td>
					<td>'.$rstmp[$t]["page_name"].'</td>
					<td colspan="2">'.$rstmp[$t]["page_label"].'</td>
				</tr>';
				$p_name = $rstmp[$t]["page_name"];
			}
			if ($p_name!=$tp_name) {
				$p_name = $rstmp[$t]["page_name"];
				$str .= '<tr>
					<td colspan="6"></td>
				</tr>';
				$str .= '<tr>
					<td>Page</td>
					<td colspan="2"><input class="all-auth" type="checkbox" onclick="selectAll(\'page-auth-'.$p_name.'\', $(this));" /></td>
					<td>'.$rstmp[$t]["page_name"].'</td>
					<td colspan="2">'.$rstmp[$t]["page_label"].'</td>
				</tr>';
			}
			$checked = $rstmp[$t]["isallowed"]==1?' checked="checked" ':'';
			$checked = $att[$rstmp[$t]["kdmod"]]==1?' checked="checked" ':'';
			$str .= '<tr>
				<td>'.$rstmp[$t]["kd"].'</td>
				<td><input type="checkbox" class="all-auth page-auth-'.$p_name.'" id="'.$rstmp[$t]["mod_name"].'" '.$checked.' name="'.$rstmp[$t]["mod_name"].'" /></td>
				<td>'.$rstmp[$t]["kdmod"].'</td>
				<td>'.$rstmp[$t]["mod_name"].'</td>
				<td>'.$rstmp[$t]["mod_label"].'</td>
				<!--<td>'.$rstmp[$t]["isallowed"].'</td>-->
				<td>'.$att[$rstmp[$t]["kdmod"]].'</td>
			</tr>';
		}
		$str .= '</table>';
		
		cout('<form method="post" target="quick_iframe" action="/'.adminFolder.'/_ajax/'.$ajax_page.'?a=do_auth" role="form">');
			cout('<input type="hidden" name="kirim" id="kirim" value="1" />');
			cout('<input type="hidden" name="kduser" id="kduser" value="'.$rs[0]["kduser"].'" />');
			cout('<dl class="dl-horizontal dldata">');
				if (authorizedModule("user_authorization_element_username", $ses_kduser)) {
					cout('<dt>Username / Initial</dt>');
					cout('<dd>'.$rs[0]["username"].' / '.$rs[0]["inisial"].'</dd>');
				}
				cout('<dt></dt>');
				cout('<dd><button class="btn btn-primary" type="submit">Save</button></dd>');
				if (authorizedModule("user_authorization_element_authorizations", $ses_kduser)) {
					cout('<dt>Authoriztions</dt>');
					cout('<dd>'.$str.'</dd>');
				}
				cout('<dt></dt>');
				cout('<dd><button class="btn btn-primary" type="submit">Save</button></dd>');
			cout('</dl>');
		cout('</form>');
		$viewFrame = 1;
		if ($viewFrame==1) {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="100" style="overflow:;border:1px red solid;" visibility="" scrolling="yes" frameborder="1"></iframe>');
		} else {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="11" style="overflow:hidden;" visibility="hidden" scrolling="no" frameborder="0"></iframe>');
		}
		$act_ket = "Users: Deleting... [kd: ".$rs[0]["kd"]."][username: ".$rs[0]["username"]."]";
		updateUserActivities($act_ket);
		exit;
	}
}
if ($a=="do_chpass") {
	$kirim = cekpost(cleanQuery($_REQUEST["kirim"]), "");
	$err = "";
	if ($kirim=="1") {
		$kduser = cekpost(cleanQuery($_REQUEST["kduser"]), "0");
		$npass = cekpost(cleanQuery($_REQUEST["npass"]), "");
		$rnpass = cekpost(cleanQuery($_REQUEST["rnpass"]), "");
		if ($npass=="") { $err.= BSErrorMessage('New Password must be filled'); }
		if ($rnpass=="") { $err.= BSErrorMessage('Re-typed New Password must be filled'); }
		if ($npass!="" && $rnpass!="" && $npass!=$rnpass) { $err.= BSErrorMessage('New Password typed did not matched'); }
		if ($err=="") {
			$modby = $_SESSION["user"]["inisial"];
			$modtime = time();
			$ipass = saltEncoding($npass);
			$q = "UPDATE _users SET pass='$ipass', modby='$modby', modtime='$modtime' WHERE kduser='$kduser'";
			execRS($q);
			$act_ket = "Users: Change Password: Updated [kduser: $kduser]";
			updateUserActivities($act_ket);
			cout('<script type="text/javascript">parent.window.location="'.$pageUrl.'/view/'.$kduser.'/";</script>');
		} else {
			cout('<script type="text/javascript">parent.showError(\''.$err.'\');</script>');
		}
		exit;
	}
	exit;
}
if ($a=="section_chpass") {
	if (!authorizedModule("user_authorization_change_password", $ses_kduser)) {
		unAuthorizedAccess("user_authorization_change_password");
	} else {
		$id = cekpost(cleanQuery($_GET["id"]), "0");
		$rs=array(); $q="SELECT * FROM _users WHERE kduser='$id'";
		callRS($rs, $q);
		cout('<form method="post" target="quick_iframe" action="/'.adminFolder.'/_ajax/'.$ajax_page.'?a=do_chpass" role="form">');
			cout('<input type="hidden" name="kirim" id="kirim" value="1" />');
			cout('<input type="hidden" name="kduser" id="kduser" value="'.$rs[0]["kduser"].'" />');
			cout('<dl class="dl-horizontal dldata">');
				if (authorizedModule("user_authorization_element_username", $ses_kduser)) {
					cout('<dt>UserName</dt>');
					cout('<dd>'.$rs[0]["username"].'</dd>');
				}
				if (authorizedModule("user_authorization_element_username", $ses_kduser)) {
					cout('<dt>Initial</dt>');
					cout('<dd>'.$rs[0]["inisial"].'</dd>');
				}
				if (authorizedModule("user_authorization_element_password", $ses_kduser)) {
					cout('<dt><label for="npass">New Password</label></dt>');
					cout('<dd><input class="form-control" type="password" name="npass" id="npass" /></dd>');
					
					cout('<dt><label for="rnpass">Re-Type New Password</label></dt>');
					cout('<dd><input class="form-control" type="password" name="rnpass" id="rnpass" /></dd>');
				}
				cout('<dt></dt>');
				cout('<dd class="err"></dd>');
				
				cout('<dt></dt>');
				cout('<dd><button class="btn btn-primary" type="submit">Update</button></dd>');
			cout('</dl>');
		cout('</form>');
		$viewFrame = 0;
		if ($viewFrame==1) {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="100" style="overflow:;border:1px red solid;" visibility="" scrolling="yes" frameborder="1"></iframe>');
		} else {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="11" style="overflow:hidden;" visibility="hidden" scrolling="no" frameborder="0"></iframe>');
		}
		$act_ket = "Banks: Editing... [kd: ".$rs[0]["kd"]."][nama: ".$rs[0]["nama"]."]";
		updateUserActivities($act_ket);
		exit;
	}
}
if ($a=="do_edit") {
	$kirim = cekpost(cleanQuery($_REQUEST["kirim"]), "");
	$err = "";
	if ($kirim=="1") {
		$kduser = cekpost(cleanQuery($_REQUEST["kduser"]), "0");
		$username = cekpost(cleanQuery($_REQUEST["username"]), "");
		$inisial = cekpost(cleanQuery($_REQUEST["inisial"]), "");
		$isactive = cekpost(cleanQuery($_REQUEST["isactive"]), 0);
		if ($username=="") { $err.= BSErrorMessage('UserName must be filled'); }
		if ($inisial=="") { $err.= BSErrorMessage('Initial must be filled'); }
		if ($err=="") {
			$modby = $_SESSION["user"]["inisial"];
			$modtime = time();
			$q = "UPDATE _users SET username='$username', inisial='$inisial', isactive='$isactive', modby='$modby', modtime='$modtime' 
				WHERE kduser='$kduser'";
			execRS($q);
			$act_ket = "Users: Edit: Updated [kduser: $kduser][username: $username][inisial: $inisial][isactive: $isactive]";
			updateUserActivities($act_ket);
			cout('<script type="text/javascript">parent.window.location="'.$pageUrl.'/view/'.$kduser.'/";</script>');
		} else {
			cout('<script type="text/javascript">parent.showError(\''.$err.'\');</script>');
		}
		exit;
	}
	exit;
}
if ($a=="section_edit") {
	if (!authorizedModule("user_authorization_edit", $ses_kduser)) {
		unAuthorizedAccess("user_authorization_edit");
	} else {
		$id = cekpost(cleanQuery($_GET["id"]), "0");
		$rs=array(); $q="SELECT * FROM _users WHERE kduser='$id'";
		callRS($rs, $q);
		cout('<form method="post" target="quick_iframe" action="/'.adminFolder.'/_ajax/'.$ajax_page.'?a=do_edit" role="form">');
			cout('<input type="hidden" name="kirim" id="kirim" value="1" />');
			cout('<input type="hidden" name="kduser" id="kduser" value="'.$rs[0]["kduser"].'" />');
			cout('<dl class="dl-horizontal dldata">');
				if (authorizedModule("user_authorization_element_username", $ses_kduser)) {
					cout('<dt><label for="username">UserName</label></dt>');
					cout('<dd><input class="form-control" type="text" name="username" id="username" value="'.$rs[0]["username"].'" /></dd>');
				}
				if (authorizedModule("user_authorization_element_initial", $ses_kduser)) {
					cout('<dt><label for="inisial">Initial</label></dt>');
					cout('<dd><input class="form-control" type="text" name="inisial" id="inisial" value="'.$rs[0]["inisial"].'" /></dd>');
				}
				if (authorizedModule("user_authorization_element_active", $ses_kduser)) {
					$isactive0 = intval($rs[0]["isactive"])==0?'selected="selected"':'';
					$isactive1 = intval($rs[0]["isactive"])==1?'selected="selected"':'';
					cout('<dt><label for="isactive">Active Status</label></dt>');
					cout('<dd>
						<select name="isactive" id="isactive" class="form-control">
							<option value="0" '.$isactive0.'>Non-Active</option>
							<option value="1" '.$isactive1.'>Active</option>
						</select>
					</dd>');
				}
				cout('<dt></dt>');
				cout('<dd class="err"></dd>');
				
				cout('<dt></dt>');
				cout('<dd><button class="btn btn-primary" type="submit">Update</button></dd>');
			cout('</dl>');
		cout('</form>');
		$viewFrame = 0;
		if ($viewFrame==1) {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="100" style="overflow:;border:1px red solid;" visibility="" scrolling="yes" frameborder="1"></iframe>');
		} else {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="11" style="overflow:hidden;" visibility="hidden" scrolling="no" frameborder="0"></iframe>');
		}
		$act_ket = "Banks: Editing... [kd: ".$rs[0]["kd"]."][nama: ".$rs[0]["nama"]."]";
		updateUserActivities($act_ket);
		exit;
	}
}
if ($a=="do_add") {
	$kirim = cekpost(cleanQuery($_REQUEST["kirim"]), "");
	$err = "";
	if ($kirim=="1") {
		$username = strtolower(trim(cekpost(cleanQuery($_REQUEST["username"]), "")));
		$pass = cekpost(cleanQuery($_REQUEST["pass"]), "");
		$rpass = cekpost(cleanQuery($_REQUEST["rpass"]), "");
		$inisial = cekpost(cleanQuery($_REQUEST["inisial"]), "");
		$isactive = cekpost(cleanQuery($_REQUEST["isactive"]), 0);
		if ($username=="") { $err.= BSErrorMessage('UserName must be filled'); }
		if ($pass=="") { $err.= BSErrorMessage('Password must be filled'); }
		if ($rpass=="") { $err.= BSErrorMessage('Re-typed Password must be filled'); }
		if ($inisial=="") { $err.= BSErrorMessage('Initial must be filled'); }
		if ($username!="") {
			$rsc=array(); $qc="SELECT * FROM _users WHERE username='".$username."'";
			callRS($rsc, $qc);
			if (count($rsc)!=0) { $err.= BSErrorMessage('UserName already exists'); }
		}
		if ($pass!="" && $rpass!="" && $pass!=$rpass) { $err.= BSErrorMessage('Password typed did not matched'); }
		if ($err=="") {
			$creby = $_SESSION["user"]["inisial"];
			$cretime = time();
			$kduser = getID("_users", "kduser");
			$ipass = saltEncoding($pass);
			$q = "INSERT INTO _users (kduser, username, pass, inisial, isactive, creby, cretime) 
				VALUES ('$kduser', '$username', '$ipass', '$inisial', '$isactive', '$creby', '$cretime')";
			execRS($q);
			$act_ket = "Users: Add [kd: $kd][username: $username][inisial: $inisial][isactive: $isactive]";
			updateUserActivities($act_ket);
			cout('<script type="text/javascript">parent.window.location="'.$pageUrl.'/view/'.$kduser.'/";</script>');
		} else {
			cout('<script type="text/javascript">parent.showError(\''.$err.'\');</script>');
		}
		exit;
	}
	exit;
}
if ($a=="section_add") {
	if (!authorizedModule("user_authorization_add", $ses_kduser)) {
		unAuthorizedAccess("user_authorization_add");
	} else {
		cout('<form method="post" target="quick_iframe" action="/'.adminFolder.'/_ajax/'.$ajax_page.'?a=do_add" role="form">');
			cout('<input type="hidden" name="kirim" id="kirim" value="1" />');
			cout('<dl class="dl-horizontal dldata">');
				cout('<dt><label for="username">UserName</label></dt>');
				cout('<dd><input class="form-control" type="text" name="username" id="username" /></dd>');
				
				cout('<dt><label for="inisial">Initial</label></dt>');
				cout('<dd><input class="form-control" type="text" name="inisial" id="inisial" /></dd>');
				
				cout('<dt><label for="pass">Password</label></dt>');
				cout('<dd><input class="form-control" type="password" name="pass" id="pass" /></dd>');
				
				cout('<dt><label for="rpass">Re-Type Password</label></dt>');
				cout('<dd><input class="form-control" type="password" name="rpass" id="rpass" /></dd>');
				
				cout('<dt><label for="isactive">Active Status</label></dt>');
				cout('<dd>
					<select name="isactive" id="isactive" class="form-control">
						<option value="0">Non-Active</option>
						<option value="1">Active</option>
					</select>
				</dd>');
				
				cout('<dt></dt>');
				cout('<dd class="err"></dd>');
				
				cout('<dt></dt>');
				cout('<dd><button class="btn btn-primary" type="submit">Add</button></dd>');
			cout('</dl>');
		cout('</form>');
		$viewFrame = 0;
		if ($viewFrame==1) {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="100" style="overflow:;border:1px red solid;" visibility="" scrolling="yes" frameborder="1"></iframe>');
		} else {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="11" style="overflow:hidden;" visibility="hidden" scrolling="no" frameborder="0"></iframe>');
		}
		$act_ket = "Users: Adding...";
		updateUserActivities($act_ket);
		exit;
	}
}
?>