<?php
/*<outscirt><version>20200929</version></outscirt>*/

function O__XMLdata($O__path,$O__data,$O__flag){
	global $O__signLog,$O__userID,$O__DBpath;
	
	$O__dataType = gettype($O__data); // is it a zero integer or xml object?
	
	if ($O__dataType == "object"){ // save XML
				
		// Format XML file
		$O__data->formatOutput = true;
		$O__data = $O__data->saveXML();

		// Save XML file
		O__dataWrite($O__path,$O__data);
	} else if ($O__flag == 0){ // load XML file
		
		libxml_use_internal_errors(true);
		$O__data = O__dataRead($O__path);
		$O__data = simplexml_load_string($O__data);

		if ($O__data === false){ // If XML is invalid, log the error and swap in placeholder XML
			$O__logNotes = "Bad XML file: ";
			foreach (libxml_get_errors() as $O__dataError) { $O__logNotes .= trim($O__dataError->message) . " | "; }
			$O__logNotes = substr($O__logNotes,0,-3); // trim off last pipe
			if ($O__signLog == 1){ O__logger("errors",substr($O__path,strlen($O__DBpath)),"Error",$O__userID,$O__logNotes); } //remove O__DBpath from log			
			$O__data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><O__root></O__root>";
			$O__data = simplexml_load_string($O__data);
		}
		
		return $O__data;		
	} else if ($O__flag == 1){ // read XML string
		
		libxml_use_internal_errors(true);
		$O__data = simplexml_load_string($O__data);
		$O__logNotes = "Bad XML string: ";
		foreach (libxml_get_errors() as $O__dataError) { $O__logNotes .= trim($O__dataError->message) . " | "; }
		if ($O__data === false){ // If XML is invalid, log the error and insert unknown
			if ($O__signLog == 1){ O__logger("errors",$O__path,"Error",$O__userID,$O__logNotes); } //remove O__DBpath from log
			$O__data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><O__root></O__root>";
			$O__data = simplexml_load_string($O__data);
		}
		
		return $O__data;		
	}
}
?>
