<?php
#	* Records Node Inforatmion in Database
#	* Uses Workgroup as Group.
#	* If "DetectNodeNumber" is turned on.  The Node ID will be created from a combination of the Group Number (Generated from the vnc_groups table)
#		and the Node number in the computer name after the last hyphen "NODE-02".  So if the group DM is "1" the node ID is 000102.
# * Automatic NodeID's that are generated from the number counter (If "DetectNodeNumber" is set to false) are taken from 99999999 counting backwards.
# * If Matching computer UniqueComputerID is found in the database then that record is used and the data is overwritten/updated.
# * If Matching NodeID is found in the Database then that record is used and the data is overwritten/updated.  (This is check after the UniqueComputerID does not match)
# * Preceeding Zero's "00023" are ignored by the viewer and repeater at the time of the connection.  Putting them there or not there have no effect other than visual.


	error_reporting(E_ALL);
	ini_set('display_errors', 1);

	$bDebug = True;

	$DB_Host = "MySQL";
	$DB_User = "dm_dm";
	$DB_Pssword = "stupidputty";
	$DB_Name = "DesktopMasters.com";

	$GroupPadding = 4; // Padding of zeros's in front of the Group's Number.
	$PathToLauncherFiles = "uvnc";
	$ViewerURL = "dm-07:55900";

#------------------------------------------------------------------------------------
# Get JASON data

//Make sure that it is a POST request.
	if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0){
		throw new Exception('Request method must be POST!');
	}
//Recieve and Attempt to decode the incoming RAW post data from JSON.
	$aComputerData = JSon_Decode(Trim(File_Get_Contents("php://input")), True);
//If json_decode failed, the JSON is invalid.
	if(!is_array($aComputerData)){
		throw new Exception('Received content contained invalid JSON!');
	}

#------------------------------------------------------------------------------------
# Process the JSON.

	$Group = $aComputerData['DomainName'];
	$authToken = $aComputerData['AuthToken'];
	$NodeID = $aComputerData['NodeID'];
		If ($bDebug) { Echo 'NodeID supplied by POST: "'.$NodeID.'"'."\r\n"; }

	If ($authToken != "12365548844523357") {
		Throw New Exception('Internal Error. Now fuck off...!');
	}

#	Var_Dump($decoded);
#	Print_R($aComputerData);

	If ($bDebug) { Echo  "ComputerName: ".$aComputerData['ComputerName']."\r\n"; }
	If ($bDebug) { Echo  "Group: ".$Group."\r\n"; }
	If ($bDebug) { Echo  "UniqueComputerID: ".$aComputerData['UniqueComputerID']."\r\n"; }

#------------------------------------------------------------------------------------
# Main Code
# Exclude: UniqueComputerID|COMPUTERNAME|DOMAINNAME|UniqueComputerID|NODEID|COMPUTERDESCRIPTION|ProxyHost|ProxyPort


	$hDB = new MySQLi($DB_Host, $DB_User, $DB_Pssword, $DB_Name, 3306);
		if ($hDB -> connect_error) {
			die("Connection failed: " . $hDB -> connect_error . "\r\n");
		}
		If ($bDebug) { Echo "Host Connect Info: " . $hDB -> host_info . "\r\n"; }
	$GroupID = GetGroupID($Group);
		If ($bDebug) { Echo  "Found GroupID=".$GroupID."\r\n"; }
	$NodeID = RecordNodeID($aComputerData, $GroupID);
	SaveLauncherFile($aComputerData);

	$hDB -> Close();

//-> Give NodeID for the client connection (The Necessary Output)
	Echo "The NodeID is ".$NodeID;
	Exit;


#------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------
Function RecordNodeID($aComputerData, $GroupID) {
	Global $bDebug, $DB_Host, $DB_User, $DB_Pssword, $DB_Name, $hDB, $NodeID, $GroupPadding, $NodeDisplayName, $iNodeRecordID;

	$UniqueComputerID = $aComputerData['UniqueComputerID'];
	$ComputerName = $aComputerData['ComputerName'];
	$NodeNumber = "";
	$qSQL = "";
		
// Build NodeID
	If ($NodeID == "") { //Checks to see if a NodeID was submitted.
		If ($bDebug) { Echo  "Submitted NodeID is empty.  Trying to build a new one\r\n"; }
		If (($aComputerData['DetectNodeNumber']) AND (StrPos($ComputerName, "-") !== False)) { // If the Detect node number switch is turned on and computer name has a hyphen it will read the number hafter the hyphen so "NODE-02" would return "02".
			If ($bDebug) { Echo  "NodeID is empty and computer name has a hyphen.  Building a new one...\r\n"; }
			$aComputerName = Explode("-", $ComputerName);
			If (Is_Numeric($aComputerName[Count($aComputerName)-1])) {
				$NodeNumber = $aComputerName[Count($aComputerName)-1];
				$NodeID =  Str_Pad($GroupID, $GroupPadding, "0", STR_PAD_LEFT).$NodeNumber;
				If ($bDebug) { Echo  "Built NodeID: ".$NodeID."\r\n"; }
			}
		} Else { // Get Generic NodeID Number
			$Result = $hDB->Query('SELECT * FROM vnc_options  WHERE OptionName="NextGenericNodeID";'); // Get next generic NodeID number from the Database.
			$RowsReturned = $Result -> num_rows;
			If ($bDebug) { Echo  "Rows Returned from vnc_options: ". $RowsReturned ."\r\n"; }
			$Result -> Data_Seek(0);
			$Row = $Result -> Fetch_Assoc();
			If ($RowsReturned == 0) { // If there is no id 1 make a new one.  (First time run?)
				If ($bDebug) { Echo  "No Generic NodeID number found in the database.  Creating a new line\r\n"; }
				$NodeID = 999999999;
				$qSQL = "INSERT INTO vnc_options (OptionName, Value) VALUES ('NextGenericNodeID', '". ($NodeID - 1) ."');"; // Create new/first record with next node ID.
			} Else {
				$NodeID = $Row['Value'];
				If ($bDebug) { Echo  "Found Generic Node ID '".$NodeID."' in the database.. \r\n"; }
				$qSQL = "UPDATE vnc_options SET Value=". ($NodeID - 1) ." WHERE id=".$Row['id'].";"; // Create new/first record with next node ID.
			}
			If ($hDB -> Query($qSQL) !== True) {
				Echo "Error: " . $qSQL . "\r\n" . $hDB -> error . "\r\n";
			}
		}
	} Else {
		If ($bDebug) { Echo  "'".$NodeID."' Was previously submitted.  Using this instead of creating one.\r\n"; }
	}

	$Result = $hDB -> Query('SELECT * FROM vnc_nodes WHERE UniqueComputerID LIKE "'.$UniqueComputerID.'";');
	If(! $Result ) {
  	Die('(1) Could not get data: ' . mysql_error()."\r\n");
	}

	$NodeDisplayName = GetNodeDisplayName($aComputerData);
		
	$Result -> Data_Seek(0);
	$Row = $Result -> Fetch_Assoc();

// Compare to existing records or make a new one.
	If ($Row['UniqueComputerID'] == "") {
		If ($bDebug) { Echo  "No UniqueComputerID in records found\r\n"; }
 		If ($NodeID != "")	{
			If ($bDebug) { Echo  "NodeID has a value to work with\r\n"; }
			If ($bDebug) { Echo  'Searching for records with: "'.$NodeID.'"'."\r\n"; }
			$Result = $hDB -> Query('SELECT * FROM vnc_nodes WHERE NodeID="'.$NodeID.'";');
				If(! $Result ) {
	  			Die('(2) Could not get data: ' . mysql_error()."\r\n");
				}
			$Result -> Data_Seek(0);
			$Row = $Result -> Fetch_Assoc();
			If ($Row['NodeID'] <> $NodeID) {
				If ($bDebug) { Echo  "No NodeID Value Found\r\n"; }
				$qSQL = "INSERT INTO vnc_nodes (id, NodeID, ComputerName, Location, ComputerType, DisplayName, NodeGroup, DomainName, UniqueComputerID) VALUES (0, '".$NodeID."', '".$aComputerData['ComputerName']."', '".$aComputerData['ComputerLocation']."', '".$aComputerData['ComputerType']."', '".Str_Replace("'", "\\'", $aComputerData['ComputerDescription'])."', '".$aComputerData['DomainName']."', '".$aComputerData['DomainName']."', '".$aComputerData['UniqueComputerID']."');";
			} Else {
				If ($bDebug) { Echo  "Found a matching NodeID Value: ".$Row['NodeID']."\r\n"; }
				$qSQL = "UPDATE vnc_nodes SET NodeID='".$NodeID."', ComputerName='".$aComputerData['ComputerName']."', Location='".$aComputerData['ComputerLocation']."', ComputerType='".$aComputerData['ComputerType']."', DisplayName='".Str_Replace("'", "\\'", $aComputerData['ComputerDescription'])."', NodeGroup='".$aComputerData['DomainName']."', DomainName='".$aComputerData['DomainName']."', UniqueComputerID='".$aComputerData['UniqueComputerID']."' WHERE id=".$Row['id'].";";
			}
		} Else {
			If ($bDebug) { Echo  "Submitted NodeID is empty.  Adding new record\r\n"; }
			$qSQL = "INSERT INTO vnc_nodes (id, NodeID, ComputerName, Location, ComputerType, DisplayName, NodeGroup, DomainName, UniqueComputerID) VALUES (0, '".$NodeID."', '".Str_Replace("'", "\\'", $aComputerData['ComputerDescription'])."', '".$aComputerData['ComputerName']."', '".$aComputerData['DomainName']."', '".$aComputerData['DomainName']."', '".$aComputerData['UniqueComputerID']."');";
		}
	} Else {
		If ($bDebug) { Echo  "UniqueComputerID matches database.  Updating Record\r\n"; }
		$qSQL = "UPDATE vnc_nodes SET NodeID='".$NodeID."', ComputerName='".$aComputerData['ComputerName']."', Location='".$aComputerData['ComputerLocation']."', ComputerType='".$aComputerData['ComputerType']."', DisplayName='".Str_Replace("'", "\\'", $aComputerData['ComputerDescription'])."', NodeGroup='".$aComputerData['DomainName']."', DomainName='".$aComputerData['DomainName']."', UniqueComputerID='".$aComputerData['UniqueComputerID']."' WHERE id=".$Row['id'].";";
	}

	If ($hDB -> Query($qSQL) === True) {
		If ($bDebug) { Echo  "New record created/updated successfully\r\n"; }
		Return $NodeID;
	} Else {
		Echo "Error: " . $qSQL . "\r\n" . $hDB -> error . "\r\n";
	}
}

#########################################
Function GetGroupID($Group) {
	Global $bDebug, $DB_Host, $DB_User, $DB_Pssword, $DB_Name, $hDB;
		
	$Result = $hDB -> Query('SELECT * FROM vnc_groups WHERE name LIKE "'.$Group.'";');
		If(! $Result ) {
	  	Die('(3) Could not get data: ' . mysql_error()."\r\n");
		}
	$RowsReturned = $Result -> num_rows;
	If ($bDebug) { Echo  "Rows Returned from vnc_groups: ". $RowsReturned ."\r\n"; }
	$Result -> Data_Seek(0);
	$Row = $Result -> Fetch_Assoc();
	$Id = $Row['id'];
	If ($Id > 0) {
		Return $Id;
	}
		
	$qSQL = "INSERT INTO vnc_groups (id, name, displayname) VALUES (0, '".$Group."', '".$Group."');";
	If ($hDB -> Query($qSQL) === True) {
#		If ($bDebug) { Echo  "New record created successfully\r\n"; }
	} Else {
		Echo "Error: " . $qSQL . "\r\n" . $hDB -> error . "\r\n";
	}

	$Result = $hDB -> Query('SELECT * FROM vnc_groups WHERE name LIKE "'.$Group.'";');
		If(! $Result ) {
	  	Die('(4) Could not get data: ' . mysql_error()."\r\n");
		}
	$Result -> Data_Seek(0);
	$Row = $Result -> Fetch_Assoc();
	$Id = $Row['id'];
	If ($Id > 0) {
		Return $Id;
	}

			
	Return "X";
#	while($row = mysql_fetch_array($Results, MYSQL_ASSOC)) {			
#		Print_R($Row);
#		echo "EMP ID :{$row['emp_id']}  <br> ".			         "EMP NAME : {$row['emp_name']} <br> ".			         "EMP SALARY : {$row['emp_salary']} <br> ".			         "--------------------------------<br>";
#	}
}

#########################################
Function ConvertArrayToVariables($aArray) {
	$aNewArray = Array();
	$aKeys = Array_Keys($aArray);

	For ($iRow = 0; $iRow < Count($aArray); $iRow++) {
		$aNewArray["%".$aKeys[$iRow]."%"] = $aArray[$aKeys[$iRow]];
	}
#	Print_R($aArray);
#	Print_R($aNewArray);
	Return $aNewArray;
}

Function GetNodeDisplayName($aComputerData) {
	Global $bDebug, $DB_Host, $DB_User, $DB_Pssword, $DB_Name, $hDB, $NodeID, $GroupPadding;

	If (IsSet($aComputerData['DisplayNameTemplate'])) {
		$sDisplayNameTemplate = $aComputerData['DisplayNameTemplate'];
	} Else {
		$Result = $hDB -> Query('SELECT * FROM vnc_options WHERE OptionName="DisplayNameTemplate";');
			If(! $Result ) {	Die('(5) Could not get data: ' . mysql_error()."\r\n");	}
		$RowsReturned = $Result -> num_rows;
		If ($RowsReturned > 0) {
			$Result -> Data_Seek(0);
			$Row = $Result -> Fetch_Assoc();
			$sDisplayNameTemplate = $Row['Value'];
		} Else {
			$sDisplayNameTemplate = "%DomainName%.%ComputerName% - %ComputerDescription% (%NodeID%)"; //-> Default value to set because no option was found.
			If ($hDB -> Query("INSERT INTO vnc_options (OptionName, Value) VALUES ('DisplayNameTemplate', '".$sDisplayNameTemplate."');") !== True) {
				Echo "Error: " . $qSQL . "\r\n" . $hDB -> error . "\r\n";
			}
		}
	}
	
	$NodeDisplayName = Str_iReplace("%NODEID%", $NodeID, $sDisplayNameTemplate);
	$aKeys = Array_Keys(ConvertArrayToVariables($aComputerData));
	$NodeDisplayName = Str_iReplace($aKeys, $aComputerData, $NodeDisplayName);
		
	Echo "Display Name: " . $NodeDisplayName . "\r\n";
#	Echo Print_R($aKeys)."\r\n";

	Return $NodeDisplayName;
}

#########################################
Function SaveLauncherFile($aComputerData) {
	Global $bDebug, $DB_Host, $DB_User, $DB_Pssword, $DB_Name, $hDB, $NodeID, $GroupPadding, $NodeDisplayName, $PathToLauncherFiles, $ViewerURL;

#	$hVNCFile = FOpen($PathToLauncherFiles."/".$NodeDisplayName.".vnc", "w");
	$sVNCFile = $PathToLauncherFiles."/".Str_Replace("/", "-", $NodeDisplayName).".vnc";
			
	If (! File_Exists(DirName($PathToLauncherFiles))) {
		MkDir(DirName($PathToLauncherFiles), 0776, True);
	}

	Echo "Deleteing ".$PathToLauncherFiles."/*(".$NodeID.")*\n";
	ForEach (Glob($PathToLauncherFiles."/*(".$NodeID.")*") as $FileName) {
		Echo "Deleting $FileName"."\n";
		UnLink($FileName);
	}

	If (! File_Exists($sVNCFile)) {
		Copy($PathToLauncherFiles."/Default.vnc", $sVNCFile);
	}

	INI_Write($sVNCFile, "connection", "host", "ID");
	INI_Write($sVNCFile, "connection", "port", $NodeID);
	If (IsSet($ViewerURL)) {
		Echo 'Writing Viewer Config Files with: "'.$ViewerURL.'"'."\r\n";
		$aViewerURL = Explode(":", $ViewerURL);
		INI_Write($sVNCFile, "connection", 'proxyhost', $aViewerURL[0]);
		INI_Write($sVNCFile, "connection", 'proxyport', $aViewerURL[1]);
	} Else {
		If (IsSet($aComputerData['ProxyHost'])) { INI_Write($sVNCFile, "connection", 'proxyhost', $aComputerData['ProxyHost']); }
		If (IsSet($aComputerData['ProxyPort'])) { INI_Write($sVNCFile, "connection", 'proxyport', $aComputerData['ProxyPost']); }
	}
}

#########################################
Function INI_Write($FileName, $Section, $Key, $Value)
{
#	echo Shell_Exec("ls -hal ".$FileName)."\r\n";
#	echo Shell_Exec("cat ".$FileName)."\r\n";
	$aIni = Parse_Ini_File($FileName, True); //-> Read File and convert to array
	
	If (IsSet($aIni[$Section][$Key])) { $aIni[$Section][$Key] = $Value; } //-> Set Value if in file.

	$hFile = FOpen($FileName, "w"); //-> Open File for output
	While (List($xSection, $aKeys) = Each($aIni)) { //-> Write File Data back to file
		FWrite($hFile, "[".$xSection."]\r\n");
		While (List($xKey, $xValue) = Each($aKeys)) {
			FWrite($hFile, $xKey."=".$xValue."\r\n");
		}
	}
	FClose($hFile);
}

#########################################
Function SavePostDataExtraInfo($aComputerData)
{
	$aKeys = Array_Keys($aComputerData);

	For ($iRow = 0; $iRow < Count($aComputerData); $iRow++) {
		$qSQL = "";
		$qSQL = $qSQL . "";
		$aNewArray["%".$aKeys[$iRow]."%"] = $aArray[$aKeys[$iRow]];
	}
#	Print_R($aArray);
#	Print_R($aNewArray);
	Return $aNewArray;
}
#INSERT Competitors (cName) SELECT DISTINCT Name FROM CompResults cr WHERE NOT EXISTS (SELECT * FROM Competitors c WHERE cr.Name = c.cName)
#MERGE INTO Competitors AS Target USING (SELECT DISTINCT Name FROM CompResults) AS Source ON Target.Name = Source.Name WHEN NOT MATCHED THEN INSERT (Name) VALUES (Source.Name);
#$qSQL = "insert into vnc_nodes(NodeID) select "'.$NodeID.'" from vnc_nodes where not exists (select * from TABLE_TESTING where R_COMPONENT_ID='1942' and oprid ='Test');
#$qSQL = "INSERT INTO vnc_nodes (id, NodeID, DisplayName, ComputerName, NodeGroup, DomainName, UniqueComputerID) VALUES (0, '".$NodeID."', '".Str_Replace("'", "\\'", $NodeDisplayName)."', '".$aComputerData['ComputerName']."', '".$aComputerData['DomainName']."', '".$aComputerData['DomainName']."', '".$aComputerData['UniqueComputerID']."');";
?>