<?php
include("../../../DTM/scripts/WriteMe_v2-2.php");

//commented out on 4th of jan 2023
/* 
chdir("../databases");

if(!$_GET["SiteName"]) {
	echo "SiteName1 not placed in GET";
	exit;
}
// version control 08042019. changed the below line from $_GET["P"] to $_GET["D"]
//version control 15102022. changed below to become a formal structure instead...


//get all GETs into a nice array...
foreach(explode("&", $_SERVER['QUERY_STRING']) as $ServQTemp){
	$ServQTemp = explode("=", $ServQTemp);
	$HTTPReq[$ServQTemp[0]] = $ServQTemp[1];
}

//CREATE FILE, IF REQUIRED
$filename = $HTTPReq['SiteName'];
if(!file_exists($filename)){
	$fh = fopen($filename, 'w');
	//get keys for headers...
	$headerstring = array_keys($HTTPReq);
	$ddd = "";	
	foreach($headerstring as $headerstring_temp){
		if($ddd == ""){
			$ddd = $headerstring_temp;
		}else{
			$ddd = $ddd . "," . $headerstring_temp;
		}
	}
	fwrite($fh, $ddd);
	fwrite($fh, "\n");
	echo 'just created a new file for you with the filename: ' . $filename;
	fclose($fh);
}

//GET HEADER OF FILE
$header_of_file = readlineoffile($filename,0);
$headerarray_file = explode(",",$header_of_file);
$headerarray_file = array_map('trim', $headerarray_file);
$headerarray_HTTP = array_keys($HTTPReq);

//CHECK THAT ALL HTTP KEYS ARE IN THE FILE HEADER
foreach($headerarray_HTTP as $headerarray_HTTP_temp){
	if(in_array($headerarray_HTTP_temp,$headerarray_file)==FALSE){
		//we ened to add this to the end of the header in the file...
		$header_of_file = trim($header_of_file);
		$header_of_file = $header_of_file . "," . $headerarray_HTTP_temp;
		$header_of_file = $header_of_file . "\n";
		//re-write header...
		writelineoffile($filename,0,$header_of_file);
	}
}

//GET NEW FILE HEADER
$header_of_file = readlineoffile($filename,0);
$headerarray_file = explode(",",$header_of_file);
$headerarray_file = array_map('trim', $headerarray_file);

//CREATE WRITING STRING
foreach($headerarray_file as $headerarray_file_Temp){
	if($headerarray_file_Temp == "SiteName"){
		$String_Me = date('d/m/y h:i:s A');
	}else{
		if(in_array($headerarray_file_Temp,$headerarray_HTTP)){
			//ok we have an item
			$String_Me = $String_Me . "," . $HTTPReq[$headerarray_file_Temp];
		} else{
			$String_Me = $String_Me . ",";
		}
	}
}
$String_Me = $String_Me . "\n";
file_put_contents($filename, $String_Me, FILE_APPEND);
echo "Added following data to " . $filename . ": " .$String_Me;


function readlineoffile($Fname,$LineNo){
		$line = '';
		$data = file($Fname);
		$line = $data[$LineNo];
		return $line;	
}

function writelineoffile($Fname,$LineNo,$newlineinfo){
	$data = file($Fname);
	$data[$LineNo] = $newlineinfo;
	file_put_contents($Fname, $data);
}

 */
?>