<?php

/**
 * PHP Server-Side Example for Fine Uploader (traditional endpoint handler).
 * Maintained by Widen Enterprises.
 *
 * This example:
 *  - handles chunked and non-chunked requests
 *  - supports the concurrent chunking feature
 *  - assumes all upload requests are multipart encoded
 *  - supports the delete file feature
 *
 * Follow these steps to get up and running with Fine Uploader in a PHP environment:
 *
 * 1. Setup your client-side code, as documented on http://docs.fineuploader.com.
 *
 * 2. Copy this file and handler.php to your server.
 *
 * 3. Ensure your php.ini file contains appropriate values for
 *    max_input_time, upload_max_filesize and post_max_size.
 *
 * 4. Ensure your "chunks" and "files" folders exist and are writable.
 *    "chunks" is only needed if you have enabled the chunking feature client-side.
 *
 * 5. If you have chunking enabled in Fine Uploader, you MUST set a value for the `chunking.success.endpoint` option.
 *    This will be called by Fine Uploader when all chunks for a file have been successfully uploaded, triggering the
 *    PHP server to combine all parts into one file. This is particularly useful for the concurrent chunking feature,
 *    but is now required in all cases if you are making use of this PHP example.
 */

// Include the upload handler class
require_once "file-upload-handler.php";

$uploader = new UploadHandler();

// Specify the list of valid extensions, ex. array("jpeg", "xml", "bmp")
$uploader->allowedExtensions = array(); // all files types allowed by default

// Specify max file size in bytes.
$uploader->sizeLimit = null;

// Specify the input name set in the javascript.
$uploader->inputName = "qqfile"; // matches Fine Uploader's default inputName value by default

// If you want to use the chunking/resume feature, specify the folder to temporarily save parts.
$uploader->chunksFolder = "chunks";

$method = get_request_method();

$randomid = $_REQUEST['randomid'];
if (!(strlen($randomid) > 0))
{
    $randomid = $_GET['randomid'];
}
if (!(strlen($randomid) > 0))
{
    $randomid = $_POST['randomid'];
}

// This will retrieve the "intended" request method.  Normally, this is the
// actual method of the request.  Sometimes, though, the intended request method
// must be hidden in the parameters of the request.  For example, when attempting to
// delete a file using a POST request. In that case, "DELETE" will be sent along with
// the request in a "_method" parameter.
function get_request_method() {
    if (isset($_POST["_method"]) && $_POST["_method"] != null) {
        return $_POST["_method"];
    }

    return $_SERVER["REQUEST_METHOD"];
}

if ($method == "POST") {
    header("Content-Type: text/plain");

    // Assumes you have a chunking.success.endpoint set to point here with a query parameter of "done".
    // For example: /myserver/handlers/endpoint.php?done
    if (isset($_GET["done"])) {
        $result = $uploader->combineChunks("../var/tmp");
    }
    // Handles upload requests
    else {
        // Call handleUpload() with the name of the folder, relative to PHP's getcwd()
        $result = $uploader->handleUpload("../var/tmp");

        // To return a name used for uploaded file you can use the following line.
        $result["uploadName"] = $uploader->getUploadName();
    }

    // Salva os arquivos que foram enviados num .txt com o randomId
    $randomFileName = '/tmp/file_upload_' . $randomid .  '.txt';
    if (file_exists($randomFileName)) {
        $formerContent = json_decode(file_get_contents($randomFileName));
        $formerContent[] = $result;
        file_put_contents($randomFileName, json_encode($formerContent));
    } else {
        file_put_contents($randomFileName, json_encode(array($result)));
    }

    echo json_encode($result);
}
// for delete file requests
else if ($method == "DELETE") {
    $result = $uploader->handleDelete("../var/tmp");

    // Retira o arquivo que foi removido do .txt com o randomId
    $randomFileName = '/tmp/file_upload_' . $randomid .  '.txt';
    if (file_exists($randomFileName)) {
        $formerContent = json_decode(file_get_contents($randomFileName));
        foreach ($formerContent as $keyContent => $content) {
            if ($result['success'] && ($result['uuid'] == $content->uuid)) {
                unset($formerContent[$keyContent]);
            }
        }
        file_put_contents($randomFileName, json_encode(array_values($formerContent)));
    }

    echo json_encode($result);
}
else {
    header("HTTP/1.0 405 Method Not Allowed");
}

?>