<?php
include("core/backend.php");
//include("core/medtrans_function.php");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
	$in = $_POST;
	$out = array();
	$ext = pathinfo($_FILES['file_contents']['name'], PATHINFO_EXTENSION);
	
        $storage_path = "wwwadmin/upload/"; //Must be in the config file 
        
        // you can be agent / member / support  creating documnent storage
        
        $user_folder = "ABCDEFGHIJK001"."/";
        
        $uploaddir = realpath('./') . '/../'.$storage_path.$user_folder.$ext.'/';
        
        // RETIRED $uploaddir = realpath('./') . '/../wwwadmin/upload/'.$ext.'/';
        
        
        
	mkdir($uploaddir, 0777, true);
	do {
		$uploadname = strtoupper(uniqid("")) . '-' . strtoupper(uniqid("")) . '-' .strtoupper(uniqid("")) .'-' .strtoupper(uniqid("")) . '.' .$ext;
		$uploadfile = $uploaddir . $uploadname;
	} while(file_exists($uploadfile));
	if (move_uploaded_file($_FILES['file_contents']['tmp_name'], $uploadfile)) {
		$in["upload_file_path"] = $uploadfile;
		$in["upload_file_name"] = $uploadname;
		$out = $savvyext->savvyext_api($in);
		$ret = $out["retval"];
		$out['internal_return'] = $ret; // this is reserved array parameter - to be caprured and reoved before you use the out array()
		$out["upload_file_path"] = $uploadfile;
		$out["upload_file_name"] = $uploadname;
/*
		if ($ret!=0) {
			unlink($uploadfile); // Do we delete when failed?
		}
*/
	} else {
		$out['internal_return'] = -1;
		$out['internal_error'] = "File upload failed";
	}
	echo json_encode($out);
} else {
	echo "{\"status\":\"Invalid request method\"}\n";
}

// vi:ts=2

