<?php
include($O__libPath . "/functions/server/O__tableMaker.php");
include($O__libPath . "/functions/server/O__backupData.php");

$O__output = "";

function O__operation(){
	global $O__DBpath, $O__usersDataPath, $O__userID, $O__userIDtarget, $O__PTATmsg, $O__logNotes;
	
	if (isset($_POST["O__name"])){ $O__name = $_POST["O__name"]; }
	if (isset($_POST["O__groupName"])){ $O__groupName = $_POST["O__groupName"]; }
	if (isset($_POST["O__target"])){ $O__target = $_POST["O__target"]; }
	if (isset($_POST["O__source"])){ $O__source = $_POST["O__source"]; }
	if (isset($O__name)){ $O__nameEncrypted = O__xCrypt($O__name,"e"); }
	$O__operation = $_POST["O__operation"];

	switch($O__operation){
		case "userAdd":
			// Create XML
			$O__fileData = new DomDocument("1.0", "UTF-8");
			$O__userInfo = $O__fileData->createElement("user", "");
			
			// Set up identity
			$O__userInfo->appendChild($O__fileData->createElement("name", $O__name));
			$O__userInfo->appendChild($O__fileData->createElement("nameFirst", ""));
			$O__userInfo->appendChild($O__fileData->createElement("nameLast", ""));
			$O__userInfo->appendChild($O__fileData->createElement("pw", rand()));
			$O__userInfo->appendChild($O__fileData->createElement("groups", ""));
			$O__fileData->appendChild($O__userInfo);				
			
			// Set up permissions
			$O__permissions = $O__fileData->createElement("permissions", "");
			$O__permissionsPageType = $O__permissions->appendChild($O__fileData->createElement("pageType", ""));
				$O__permissionsPageType->appendChild($O__fileData->createElement("name", "O__users"));
				$O__permissionsPageType->appendChild($O__fileData->createElement("actions", "editSelf"));
			//$O__permissions->appendChild($O__fileData->createElement("pageType", ""));
			$O__userInfo->appendChild($O__permissions);
			
			// Save XML file
			O__XMLdata($O__usersDataPath . "users/" . O__xCrypt($O__name,"e") . ".xml", $O__fileData, 0);
			
			// Do a final check for file existence
			if (file_exists($O__usersDataPath . "users/" . O__xCrypt($O__name,"e") . ".xml")){
				$O__sysMsgHeadline = "Create User";
				$O__sysMsgBody = "Success. '" . $O__name . "' has been created.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
				$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;	
			} else {
				$O__sysMsgHeadline = "Create User";
				$O__sysMsgBody = "Failed. '" . $O__name . "' NOT created. Please contact your system administrator for assistance.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgAlert");
				$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;	
			}
			break;
			
		case "userDelete":
			If($O__userID != $O__target){
				$O__fileDataPath = $O__usersDataPath . O__xCrypt($O__target,"e") . ".xml";
				O__backupData($O__fileDataPath,1);

				// Do a final check for file existence
				if (!file_exists($O__fileDataPath)){					
					$O__sysMsgHeadline = "Delete User";
					$O__sysMsgBody = "Success. '" . $O__target . "' has been deleted.";
					$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
					$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
				} else {
					$O__sysMsgHeadline = "Delete User";
					$O__sysMsgBody = "Failed. '" . $O__target . "' NOT deleted. Please contact your system administrator for assistance.";
					$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgAlert");
					$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;		
				}			
				break;
			} else {
				$O__sysMsgHeadline = "Delete User";
				$O__sysMsgBody = "Failed. '" . $O__target . "' NOT deleted. You cannot delete yourself.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgAlert");
				$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;		
			}
		case "userGroupsUpdate":
			$O__fileData = O__XMLdata($O__usersDataPath . "users/" . O__xCrypt($O__target,"e") . ".xml", 0, 0);
			$O__fileData->groups = $O__source;
			$O__fileData = $O__fileData->asXML();			
			O__dataWrite($O__usersDataPath . "users/" . O__xCrypt($O__target,"e") . ".xml",$O__fileData);
			clearstatcache(); // clear cache so newest version can be loaded		
				
			$O__sysMsgHeadline = "User Groups Update";
			$O__sysMsgBody = "Success. Groups for '" . $O__name . "' have been updated.";
			$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
			$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			break;
			
		case "groupAdd":
			// Create XML
			$O__fileData = new DomDocument("1.0", "UTF-8");
			$O__groupInfo = $O__fileData->createElement("group", "");
			
			// Set up info
			$O__groupInfo->appendChild($O__fileData->createElement("name", $O__groupName));
			$O__groupInfo->appendChild($O__fileData->createElement("protected", 0));
			$O__fileData->appendChild($O__groupInfo);				
			
			// Set up permissions
			$O__permissions = $O__fileData->createElement("permissions", "");
			$O__permissionsPageType = $O__permissions->appendChild($O__fileData->createElement("pageType", ""));
				$O__permissionsPageType->appendChild($O__fileData->createElement("name", "O__users"));
				$O__permissionsPageType->appendChild($O__fileData->createElement("actions", "editSelf"));
			$O__groupInfo->appendChild($O__permissions);
			
			// Save XML file
			O__XMLdata($O__usersDataPath . "groups/" . strtolower($O__groupName) . ".xml", $O__fileData, 0);
			
			// Do a final check for file existence
			if (file_exists($O__usersDataPath . "groups/" . strtolower($O__groupName) . ".xml")){
				$O__sysMsgHeadline = "Create Group";
				$O__sysMsgBody = "Success. '" . $O__groupName . "' has been created.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
				$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;	
			} else {
				$O__sysMsgHeadline = "Create Group";
				$O__sysMsgBody = "Failed. '" . $O__groupName . "' NOT created. Please contact your system administrator for assistance.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgAlert");
				$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;	
			}
			break;
			
		case "groupDelete":
			//If($O__userID != $O__target){
				$O__fileDataPath = $O__usersDataPath . "groups/" . strtolower($O__target) . ".xml";
				O__backupData($O__fileDataPath,1);

				// Do a final check for file existence
				if (!file_exists($O__fileDataPath)){					
					$O__sysMsgHeadline = "Delete Group";
					$O__sysMsgBody = "Success. '" . $O__target . "' has been deleted.";
					$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
					$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
				} else {
					$O__sysMsgHeadline = "Delete Group";
					$O__sysMsgBody = "Failed. '" . $O__target . "' NOT deleted. Please contact your system administrator for assistance.";
					$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgAlert");
					$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;		
				}			
				break;
			
		case "groupRename":		
			// Check if exists
			if(file_exists($O__usersDataPath . "groups/" . strtolower($O__source) . ".xml")){					
				// Set system message & log
				$O__sysMsgHeadline = "Rename Group";
				$O__sysMsgBody = "Failed.  One with the same name exists.  Did not rename group '" . $O__target . "'.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgAlert");
				$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			} else {
				// Update XML name					
				$O__fileData = O__XMLdata($O__usersDataPath . "groups/" . "/" . strtolower($O__target) . ".xml", 0, 0);
				$O__fileData->name = $O__source;
				O__XMLdata($O__usersDataPath . "groups/" . "/" . strtolower($O__target) . ".xml", $O__fileData, 0);
				
				// Rename XML group filename
				rename($O__usersDataPath . "groups/" . strtolower($O__target) . ".xml", $O__usersDataPath . "groups/" . strtolower($O__source) . ".xml");

				// cleanup
				unset($O__fileData);
				clearstatcache(); // clear cache so newest version can be loaded*/	
	
				// Set system message & log
				$O__sysMsgHeadline = "Group Rename";
				$O__sysMsgBody = "Success.  Renamed '" . $O__target . "' to '" . $O__source . "'.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
				$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			}
			
			break;
	}

}

function O__createOutput(){
	global $O__DBpath, $O__usersDataPath, $O__userID, $O__PT, $O__PTATmsg;
	
	$O__output = "<h2>Users & Groups Manager</h2>";
	$O__output .= $O__PTATmsg;
	$O__output .= "<form name=\"O__userEdit\" method=\"post\">";
	$O__output .= "<input type=\"hidden\" name=\"O__target\" value=\"\">";
	$O__output .= "<input type=\"hidden\" name=\"O__source\" value=\"\">";
	$O__output .= "<input type=\"hidden\" name=\"O__operation\" value=\"\">";
	$O__output .= "<input type=\"hidden\" name=\"O__userIDtarget\" value=\"\">";

	// Groups table data	
	$O__output .= "<h3>Groups</h3>";
	$O__output .= "<p><b>Create:</b> ";
	$O__output .= "<a href=\"#\" onclick=\"javascript:O__operation('groupAdd','');\">New</a></p>";
	$O__output .= "<input type=\"hidden\" name=\"O__groupName\" value=\"\">";
	$O__colLabels = array("Group","Action");
	$O__filesAudit = O__dirToArray($O__usersDataPath . "groups/");
	
	for($i=0;$i<count($O__filesAudit);$i++){ // Only count the group files
		if (strpos($O__filesAudit[$i][0], ".xml") !== false){
			$O__filesArray[$i][0] = $O__filesAudit[$i][0];
			$O__filesArray[$i][1] = "f";
		}
	}
	sort($O__filesArray);
		
	for($i=0;$i<count($O__filesArray);$i++){
		$O__fileData = O__XMLdata($O__usersDataPath . "groups/" . $O__filesArray[$i][0], 0, 0);
		$O__data[$i][0] = $O__fileData->name;
		$O__data[$i][1] = "<a href=\"/" . $O__PT. "/" . strtolower($O__fileData->name) . "/editGroup\">Edit</a>";
		if ($O__fileData->protected != 1){ // Prevents unwanted renaming/deletion of groups
			$O__data[$i][1] .= " | <a href=\"#\" onclick=\"javascript:O__operation('groupRename','" .  $O__fileData->name ."');\">Rename</a>";
			$O__data[$i][1] .= " | <a href=\"#\" onclick=\"javascript:O__operation('groupDelete','" . $O__fileData->name . "');\">Delete</a>";
		}
	}
	
	$O__output .= O__tableMaker($O__colLabels,$O__data,"O__table","O__tableUsers",0);
	unset($O__data,$O__filesAudit,$O__filesArray);

	// Users table data
	$O__output .= "<h3>Users</h3>";
	$O__output .= "<p><b>Create:</b> ";
	$O__output .= "<a href=\"#\" onclick=\"javascript:O__operation('userAdd','');\">New</a></p>";
	$O__output .= "<input type=\"hidden\" name=\"O__name\" value=\"\">";
	$O__colLabels = array("User","First Name", "Last Name", "Groups", "Action");
	$O__filesAudit = O__dirToArray($O__usersDataPath . "users/");
	
	for($i=0;$i<count($O__filesAudit);$i++){ // Only count the user files
		if (strpos($O__filesAudit[$i][0], ".xml") !== false){
			$O__filesArray[$i][0] = $O__filesAudit[$i][0];
			$O__filesArray[$i][1] = "f";
		}
	}
	
	sort($O__filesArray);
		
	for($i=0;$i<count($O__filesArray);$i++){
		$O__fileData = O__XMLdata($O__usersDataPath . "users/" . $O__filesArray[$i][0], 0, 0);
		$O__data[$i][0] = $O__fileData->name;
		$O__data[$i][1] = $O__fileData->nameFirst;
		$O__data[$i][2] = $O__fileData->nameLast;
		$O__data[$i][3] = "<input type=\"text\" name=\"" . $O__fileData->name . "-groups\" id=\"" . $O__fileData->name . "-groups\" value=\"" . $O__fileData->groups . "\">";
		$O__data[$i][4] = "<a href=\"#\" onclick=\"javascript:O__operation('userGroupsUpdate','" . $O__fileData->name . "');\">Update Groups</a> | ";
		$O__data[$i][4] .= "<a href=\"/" . $O__PT. "/" . O__xCrypt($O__fileData->name,"e"). "/edit\">Edit User</a>";
		if ($O__userID != $O__fileData->name ){ $O__data[$i][4] .= " | <a href=\"#\" onclick=\"javascript:O__operation('userDelete','" . $O__fileData->name . "');\">Delete User</a>"; }
	}
	
	$O__output .= O__tableMaker($O__colLabels,$O__data,"O__table","O__tableUsers",0);
	unset($O__data,$O__filesAudit,$O__filesArray);
	
	$O__output .= "</form>";
	
	return $O__output;
}

if(isset($_POST["O__operation"])){ O__operation(); }
$O__output = O__createOutput();
?>
<script>
function O__operation(O__action, O__target){
	var O__form = document.forms["O__userEdit"];
		O__form.O__operation.value = O__action;

	switch (O__action){
		case "userDelete":
			var O__input = confirm("Are you sure you want to delete the user '" + O__target + "'?");
			if (O__input == 1){
				O__form.O__target.value = O__target;
				O__form.submit();
			}
			break;
			
		case "userAdd":
			var O__input = prompt("Please enter a username.");
			if (O__input == ""){
				alert("Please enter a name for this user.");
			} else if (O__input != null){
				O__form.O__name.value = O__input.replace(/ /g, "-");
				O__form.submit();
			}
			break;
		
		case "userGroupsUpdate":
			var O__input = confirm("Are you sure you want to update the group for '" + O__target + "'?");
			if (O__input == 1){
				O__groupsNew = O__target + "-groups";
				O__form.O__source.value = document.getElementById(O__groupsNew).value;
				O__form.O__target.value = O__target;
				O__form.submit();
			}
			break;
			
		case "groupAdd":
			var O__input = prompt("Please enter a group name.");
			if (O__input == ""){
				alert("Please enter a name for the new group.");
			} else if (O__input != null){
				O__form.O__groupName.value = O__input.replace(/ /g, "-");
				O__form.submit();
			}
			break;
			
		case "groupDelete":
			var O__input = confirm("Are you sure you want to delete the group '" + O__target + "'? Any users with this group will NOT be automatically updated and WILL be impacted.");
			if (O__input == 1){
				O__form.O__target.value = O__target;
				O__form.submit();
			}
			break;
		
		case "groupRename":
			var O__input = prompt("The current name is '" + O__target + "'.  Please enter a new name.");
			if (O__input == ""){
				alert("Please enter a new name."); 
			} else if (O__input != null){
				O__form.O__source.value = O__input;
				O__form.O__target.value = O__target;
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;
	}

}
</script>
