<?php 
	error_reporting(0);
	date_default_timezone_set('Asia/Kuala_Lumpur');
	/*
	error_reporting(E_ALL);
	ini_set('display_errors', TRUE);
	ini_set('display_startup_errors', TRUE);
	*/
	
	
	
	$id				= $_GET['id'];
	$site			= $_GET['site'];
	$f_type			= $_GET['f_type'];
	$content		= urldecode($_GET['content']);
	$content_data	= json_decode($content,true);
	
	$filename		= $f_type."/".$id.".txt";
	$filename_all	= $f_type."/all.txt";
	
	
	if($_GET['action']=='create'){
		if(!file_exists($filename)){
			//if not yet create, create-kan
			$handle 	= fopen($filename, "wb");
			fwrite($handle, $content);
			fclose($handle);
		}
		if(file_exists($filename)){
			//if got, for now just update-kan
			$handle 	= fopen($filename, "wb");
			fwrite($handle, $content);
			fclose($handle);
		}
		
		
		
		
		
		
		if(!file_exists($filename_all)){
			//prepare data for _all
			$content_all[$f_type][$id] = $content_data;
			//if status Y, not yet create, create-kan
			if($content_data['Status']=='Y'){
				$content_all = json_encode($content_all);
				$handle 	= fopen($filename_all, "wb");
				fwrite($handle, $content_all);
				fclose($handle);
			}
		}
		if(file_exists($filename_all)){
			//if got, gather info and update it
			$content_all_data	= file_get_contents($filename_all);
			$content_all		= json_decode($content_all_data,true);
			
			if($content_data['Status']=='Y'){
				$content_all[$f_type][$id] = $content_data;
				ksort($content_all[$f_type]);
			}else if($content_data['Status']=='N'){
				unset($content_all[$f_type][$id]); //won't throw back error so should be can straight unset
			}
			$content_all = json_encode($content_all);
			$handle 	= fopen($filename_all, "wb");
			fwrite($handle, $content_all);
			fclose($handle);
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	if($_GET['action']=='update'){
		if(!file_exists($filename)){
			//in case if not yet create, create-kan
			$handle 	= fopen($filename, "wb");
			fwrite($handle, $content);
			fclose($handle);
		}
		if(file_exists($filename)){
			//if got, for now just update-kan
			$handle 	= fopen($filename, "wb");
			fwrite($handle, $content);
			fclose($handle);
		}
		
		
		
		
		
		
		if(!file_exists($filename_all)){
			//prepare data for _all
			$content_all[$f_type][$id] = $content_data;
			//if status Y, not yet create, create-kan
			if($content_data['Status']=='Y'){
				$content_all = json_encode($content_all);
				$handle 	= fopen($filename_all, "wb");
				fwrite($handle, $content_all);
				fclose($handle);
			}
		}
		if(file_exists($filename_all)){
			//if got, gather info and update it
			$content_all_data	= file_get_contents($filename_all);
			$content_all		= json_decode($content_all_data,true);
			
			if($content_data['Status']=='Y'){
				$content_all[$f_type][$id] = $content_data;
				ksort($content_all[$f_type]);
			}else if($content_data['Status']=='N'){
				unset($content_all[$f_type][$id]); //won't throw back error so should be can straight unset
			}
			$content_all = json_encode($content_all);
			$handle 	= fopen($filename_all, "wb");
			fwrite($handle, $content_all);
			fclose($handle);
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	if($_GET['action']=='read'){
		if(file_exists($filename)){
			//if dah ada, gather info
			$info = file_get_contents($filename);
			//return info
			echo $info;
		}
	}
	
	
	
	if($_GET['action']=='read_all'){
		if(file_exists($filename_all)){
			//if dah ada, gather info
			$info = file_get_contents($filename_all);
			//return info
			echo $info;
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	if($_GET['action']=='delete'){
		if(file_exists($filename)){
			unlink($filename); //delete it
		}
		
		
		if(!file_exists($filename_all)){
			//no file, do nothing
		}
		if(file_exists($filename_all)){
			//if got, gather info and delete it
			$content_all_data	= file_get_contents($filename_all);
			$content_all		= json_decode($content_all_data,true);
			
			unset($content_all[$f_type][$id]); //won't throw back error so should be can straight unset
			
			$content_all = json_encode($content_all);
			$handle 	= fopen($filename_all, "wb");
			fwrite($handle, $content_all);
			fclose($handle);
		}
	}
?>