<?php
/**
 * VFM - veno file manager move or copy files
 *
 * PHP version >= 5.3
 *
 * @category  PHP
 * @package   VenoFileManager
 * @author    Nicola Franchini <support@veno.it>
 * @copyright 2013 Nicola Franchini
 * @license   https://veno.es/shop/help Veno Commercial License
 * @link      http://filemanager.veno.it/
 */
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) 
    || (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest')
) {
    exit;
}
require_once dirname(__DIR__).'/class/class.actions.php';
require_once dirname(__DIR__).'/class/class.downloader.php';
require_once dirname(__DIR__).'/class/class.setup.php';
require_once dirname(__DIR__).'/class/class.utils.php';
require_once dirname(__DIR__).'/class/class.gatekeeper.php';

$setUp = new SetUp();
$gateKeeper = new GateKeeper();
$downloader = new Downloader();
$actions = new Actions();

$copy = isset($_POST['copy']) ? (bool)$_POST['copy'] : false;
$setmove = isset($_POST['setmove']) ? $_POST['setmove'] : false;
$dest = isset($_POST['dest']) ? $_POST['dest'] : false;
$hash = isset($_POST['h']) ? $_POST['h'] : false;
$time = isset($_POST['t']) ? $_POST['t'] : false;

$salt = $setUp->getConfig('salt');
$starting_dir = $setUp->getConfig('starting_dir');

if ($hash && $time && $dest && $setmove
    && $gateKeeper->isUserLoggedIn() 
    && ($gateKeeper->isAllowed('move_enable') || $gateKeeper->isAllowed('copy_enable'))
) { 
    if (md5($salt.$time) === $hash) {
        $setmove = explode(',', $setmove);
        $destcoded = urldecode($dest);

        if (defined('VFM_DEMO') && VFM_DEMO === true) {
            foreach ($setmove as $piece) {
                $clean = urldecode(base64_decode($piece));
                if (Utils::isDemoProtected($clean)) {
                    echo $setUp->getString('not_allowed');
                    exit;
                }
            }
        }

        $absoluteDest = '../../' . ltrim($destcoded, './');
        $cleandest = $destcoded;
        if (strpos($destcoded, $starting_dir) === 0) {
            $cleandest = substr($destcoded, strlen($starting_dir));
        }
        
        // Se la destinazione è la root, usa il nome della starting_dir come placeholder
        if (empty($cleandest) || $cleandest === './' || $cleandest === '/') {
            $cleandest = ltrim($starting_dir, './');
        }

        $counter = 0;
        $total = count($setmove);

        foreach ($setmove as $pezzo) {
            if ($downloader->checkFile($pezzo, '../') == true) {
                $fileclean = ltrim(urldecode(base64_decode($pezzo)), './');
                $myfile = "../../" . $fileclean;
                $filepathinfo = Utils::mbPathinfo($fileclean);
                $basename = $filepathinfo['basename'];
                
                if ($copy) {
                    if (file_exists($myfile)) {
                        $filesize = filesize($myfile);

                        if ($actions->checkUserSpace($myfile, $filesize) == false) {
                            Utils::setError('<i class="bi bi-x-circle"></i> '.$setUp->getString('available_space_exhausted').': <strong>'.$basename.'</strong> ('.$setUp->formatSize($filesize).') ');
                            echo "ok";
                            exit;
                        }
                    } else {
                        continue;
                    }
                }
                if ($actions->renameFile($myfile, $absoluteDest.'/'.$basename, $basename, true, $copy)) {
                    $counter++;
                }
            }
        }

        if ($counter > 0) {
            if ($total == 1) {
                $counter = $basename;
            }
            if ($copy) {
                Utils::setSuccess('<strong>'.$counter.'</strong> '.$setUp->getString('files_copied_to').': <i class="bi bi-folder2-open"></i> <strong>'.$cleandest.'</strong>');
            } else {
                Utils::setSuccess('<strong>'.$counter.'</strong> '.$setUp->getString('files_moved_to').': <i class="bi bi-folder2-open"></i> <strong>'.$cleandest.'</strong>');
            }
        }
        echo "ok";
        exit;
    } else {
        echo $setUp->getString('not_allowed');
    }
} else {
    echo "Not enough data";
}
exit;
