<?php
/**
 * Copyright since 2007 PrestaShop SA and Contributors
 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License version 3.0
 * that is bundled with this package in the file LICENSE.md.
 * It is also available through the world-wide-web at this URL:
 * https://opensource.org/licenses/AFL-3.0
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
 * @copyright Since 2007 PrestaShop SA and Contributors
 * @license   https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
 */
if (!defined('_PS_VERSION_')) {
    exit;
}

class SpmreviewsadvUploadModuleFrontController extends ModuleFrontController
{

    public function postProcess()
    {
        header("Access-Control-Allow-Origin: *");
       /* $HTTP_X_REQUESTED_WITH = isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? $_SERVER['HTTP_X_REQUESTED_WITH'] : '';
        if ($HTTP_X_REQUESTED_WITH != 'XMLHttpRequest') {
            exit;
        }*/

        $name_module = 'spmreviewsadv';
        include_once(_PS_MODULE_DIR_.$name_module.'/spmreviewsadv.php');
        $obj_spmreviewsadv = new spmreviewsadv();


        $token = Tools::getValue('token');
        $token_orig = $obj_spmreviewsadv->getokencron();
        if($token_orig !=$token)
            die('Invalid token.');



        ob_start();


        $status = 'success';
        $message = '';

        $is_error = 0;
        $name_file = '';
        $size_file = '';

        include_once(_PS_MODULE_DIR_.$name_module.'/classes/spmreviewsadvhelp.class.php');
        $obj = new spmreviewsadvhelp();



        $action = Tools::getValue('action');

        switch ($action) {
            case 'add':
                $files = $_FILES['files'];
                $data_files_return = $obj->uploadTmpFile(array('files'=>$files));
                $is_error = $data_files_return['is_error'];
                $status = $data_files_return['status'];
                $message = $data_files_return['message'];
                $size_file = $data_files_return['size_file'];
                $name_file = $data_files_return['name_file'];

                break;
            case 'del':
                $name = Tools::getValue('name');
                $obj->deleteTmpFile(array('name'=>$name));

                break;
            case 'deletefile':
                include_once(_PS_MODULE_DIR_.$name_module.'/spmreviewsadv.php');
                $obj_spmreviewsadv = new spmreviewsadv();

                if($obj_spmreviewsadv->is_demo){
                    $status = 'error';
                    $message = 'Feature disabled on the demo mode!';
                } else {
                    $id = Tools::getValue('item_id');
                    $obj->deleteFile(array('id'=>$id));
                }
            break;
            default:
                $status = 'error';
                $message = 'Unknown parameters!';
            break;
        }



        $response = new stdClass();
        ob_get_clean();
        $response->status = $status;
        $response->message = $message;

        if($is_error == 0 && $action == 'add'){
            $response->params = array('size' => $size_file,'name'=>$name_file);
        }



        echo json_encode($response);
        exit;

    }

}