<?php
error_reporting (E_ALL ^ E_NOTICE);
error_reporting(E_ERROR | E_PARSE);
if (!defined("root")) {
	define("root", $_SERVER['DOCUMENT_ROOT']);
}
$timezone = "Asia/Jakarta";
if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
if (!function_exists("islocal")) {
	function islocal() {
		$http_hosts = array("localhost", "av88.localhost");
		if (in_array($_SERVER["HTTP_HOST"], $http_hosts)) {
			return true;
		} else {
			return false;
		}
	}
}
function mainDomain() {
	if (islocal()) {
		$mainHttpHost = "http://av88.localhost";
	} else {
		$mainHttpHost = "av88.charycannia.com";
	}
	return $mainHttpHost;
}
function vname(&$var, $scope=false, $prefix='unique', $suffix='value') {
	if($scope) $vals = $scope;
	else      $vals = $GLOBALS;
	$old = $var;
	$var = $new = $prefix.rand().$suffix;
	$vname = FALSE;
	foreach($vals as $key => $val) {
		if($val === $new) $vname = $key;
	}
	$var = $old;
	return $vname;
}
function cvdb () {
	if (islocal()) {
		$DBSource = "localhost";
		$DBSource = "127.0.0.1";
		$UIDDB = "root";
		$UPWDDB = "";
		$DBName = "av88";
	} else {
		$DBSource = "localhost";
		$DBSource = "127.0.0.1";
		$UIDDB = "agenvip8_av88";
		$UPWDDB = "6(!vCa#*AR)c";
		$DBName = "agenvip8_av88";
		
		$DBSource = "localhost";
		$DBSource = "127.0.0.1";
		$UIDDB = "chary107_av88";
		$UPWDDB = "6(!vCa#*AR)c";
		$DBName = "chary107_av88";
	}
	return array("dbs"=>$DBSource, "dbu"=>$UIDDB, "dbp"=>$UPWDDB, "dbn"=>$DBName);
}
function cvdb_log () {
	if (islocal()) {
		$DBSource = "localhost";
		$DBSource = "127.0.0.1";
		$UIDDB = "root";
		$UPWDDB = "";
		$DBName = "av88_log";
	} else {
		$DBSource = "localhost";
		$DBSource = "127.0.0.1";
		$UIDDB = "agenvip8_av88";
		$UPWDDB = "6(!vCa#*AR)c";
		$DBName = "agenvip8_av88";
		
		$DBSource = "localhost";
		$DBSource = "127.0.0.1";
		$UIDDB = "chary107_av88";
		$UPWDDB = "6(!vCa#*AR)c";
		$DBName = "chary107_av88_log";
	}
	return array("dbs"=>$DBSource, "dbu"=>$UIDDB, "dbp"=>$UPWDDB, "dbn"=>$DBName);
}

function showMySQLError ($parSQL, $callertype) {
	die("<br />\n<span style='font-family:verdana;font-size:12px;'><br />\nError Number:<br />\n<span style='font-weight:bold;'>".mysql_errno()."</span>,<br />\nError Message:<br />\n<span style='font-weight:bold;'>".mysql_error()."</span><br />query: <span style='font-weight:bold;'>".$parSQL."</span>\n<br /><span>CallerNVariable= [".$callertype."]</span>");
	//die("query error");
}
function callRS(&$rs, $parSQL) {
	#$theVariable = vname(&$rs);
	$theVariable = vname($rs);
	$setup = cvdb();
	$con = mysql_connect($setup["dbs"], $setup["dbu"], $setup["dbp"]);
	if (!$con) showMySQLError($parSQL, "callRS: ".$theVariable);
	$gege = mysql_select_db($setup["dbn"], $con);
	if (!$gege) showMySQLError($parSQL, "callRS: ".$theVariable);
	if (!($result = mysql_query($parSQL, $con))) showMySQLError($parSQL, "callRS: ".$theVariable);
	$cnt = -1;
	while(($row = mysql_fetch_array($result))==true){
		$cnt++; $rs[$cnt] = $row;
	}
	if (!mysql_close($con)) showMySQLError($parSQL, "callRS: ".$theVariable);
}
function execRS($parSQL) {
	$theVariable = vname($parSQL);
	$setup = cvdb();
	$con = mysql_connect($setup["dbs"], $setup["dbu"], $setup["dbp"]);
	if (!$con) showMySQLError($parSQL, "execRS: ".$theVariable);
	$gege = mysql_select_db($setup["dbn"], $con);
	if (!$gege) showMySQLError($parSQL, "execRS: ".$theVariable);
	if (!($result = mysql_query($parSQL, $con))) showMySQLError($parSQL, "execRS: ".$theVariable);
	if (!mysql_close($con)) showMySQLError($parSQL, "execRS: ".$theVariable);
}
function getID($tablename, $fieldname) {
	$rsGetID=array(); $qGetId="SELECT MAX(".$fieldname.") AS fieldID FROM ".$tablename."";
	callRS($rsGetID, $qGetId);
	$getID=(count($rsGetID)!=0 && $rsGetID[0]['fieldID']!=null) ?($rsGetID[0]['fieldID']+1):1;
	return $getID;
}

/*Begin Log*/
function callRSL(&$rs, $parSQL) {
	$theVariable = vname($rs);
	$setup = cvdb_log();
	$con = mysql_connect($setup["dbs"], $setup["dbu"], $setup["dbp"]);
	if (!$con) showMySQLError($parSQL, "callRS: ".$theVariable);
	$gege = mysql_select_db($setup["dbn"], $con);
	if (!$gege) showMySQLError($parSQL, "callRS: ".$theVariable);
	if (!($result = mysql_query($parSQL, $con))) showMySQLError($parSQL, "callRS: ".$theVariable);
	$cnt = -1;
	while(($row = mysql_fetch_array($result))==true){
		$cnt++; $rs[$cnt] = $row;
	}
	if (!mysql_close($con)) showMySQLError($parSQL, "callRS: ".$theVariable);
}
function execRSL($parSQL) {
	$theVariable = vname($parSQL);
	$setup = cvdb_log();
	$con = mysql_connect($setup["dbs"], $setup["dbu"], $setup["dbp"]);
	if (!$con) showMySQLError($parSQL, "execRS: ".$theVariable);
	$gege = mysql_select_db($setup["dbn"], $con);
	if (!$gege) showMySQLError($parSQL, "execRS: ".$theVariable);
	if (!($result = mysql_query($parSQL, $con))) showMySQLError($parSQL, "execRS: ".$theVariable);
	if (!mysql_close($con)) showMySQLError($parSQL, "execRS: ".$theVariable);
}
function getIDL($tablename, $fieldname) {
	$rsGetID=array(); $qGetId="SELECT MAX(".$fieldname.") AS fieldID FROM ".$tablename."";
	callRSL($rsGetID, $qGetId);
	$getID=(count($rsGetID)!=0 && $rsGetID[0]['fieldID']!=null) ?($rsGetID[0]['fieldID']+1):1;
	return $getID;
}
/*End Log*/
?>