<?php

include("core/backend.php");

define('MEDTRANS_LOAD_AGENT', 400006);
define('MEDTRANS_UPLOADS', 4505);

#
//include("core/medtrans_function.php");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $in = $_POST;
    $out = array();
    $x = array();
    $ext = pathinfo($_FILES['file_contents']['name'], PATHINFO_EXTENSION);
    $in['extension'] = $ext;
    if ($in['agent_id'] != '' and $in['agent_id'] > 0) {

        $storage_path = "wwwadmin/upload/"; //Must be in the config file 
        $in["part"] = "AGT";
        $storage_path .= $in["part"] . "/"; // ALL AGENT FILES APART
        // you can be only be agent on this path creating documnent storage
        // 1. we need your local drive folder
        $in['action'] = MEDTRANS_LOAD_AGENT;
        $in["pid"] = 115;
        $x = $savvyext->savvyext_api($in);
        $user_folder = $x["folder"] . "/" . $in['file_type'] . "/";
        $in["folder"] = $x["folder"];
        $uploaddir = realpath('./') . '/../' . $storage_path . $user_folder . $ext . '/';
       
        mkdir($uploaddir, 0777, true);
        do {
            $uploadname = 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;
            $in["file_name"] = $uploadname; // need this in back end
            $in['action'] = MEDTRANS_UPLOADS;
            // file_type is in the array already
            $out = $savvyext->savvyext_api($in);
						$ret = $out["retval"];
            $out = $in;
            $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";
        }
    } else {
        $out["status"] = "Invalid Agent ID";
        $out['internal_return'] = -1;
        $out['internal_error'] = "File upload failed";
    }

    echo json_encode($out);
} else {
    echo "{\"status\":\"Invalid request method\"}\n";
}

// vi:ts=2

