<?php
// GET DIRECTORY SIZE (in Mo) =================================================================================
function get_dir_size($dirpath, $exclude_extension='')
{
    $totalSize = 0;
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirpath));
    foreach ($iterator as $file)
        {
            $extension = pathinfo($file, PATHINFO_EXTENSION);
            if ($file->isFile() && $extension !==$exclude_extension) $totalSize += $file->getSize();
        }
    
    return round($totalSize / 1024 / 1024, 2);
}


// DISPLAY FILE SIZE (in Ko) =================================================================================
function display_fileSize($path, $object)
{
    if (is_file($path . '/' . $object)) 
    {
        return " &nbsp;&nbsp;|--- $object  &nbsp;&nbsp;" . ceil(filesize($path . '/' . $object) / 1024) . "  Ko<br>";
    }
}


// DISPLAY FILE SIZE (in Mo) =================================================================================
function display_dirSize($path, $object)
{
    if (is_dir($path . $object)) 
    {
        return " &nbsp;&nbsp;|--- $object  &nbsp;&nbsp;" . get_dir_size($path . $object) . "  Mo<br>";
    }
}


// ADD FILE OR DIRECTORY TO PROVIDED ARCHIVE FILE =================================================================================
function addToArchive($archive, $path, $relativePath, $object)
{
    if (is_file($path . '/' . $object))
    {
        $archive->addFile($path . '/' . $object, $relativePath . '/' . $object);
    }
    elseif (is_dir($path . '/' . $object))
    {
        $files = glob($path . '/' . $object . '/*');
        foreach ($files as $f) $archive->addFile($f, $relativePath . '/' . basename($f));
    }
}

// DISPLAY BUTTON TO DOWNLOAD A PROVIDED FILE =================================================================================
function download_button($file)
{
    echo '<form method="post">';
    echo "<input style='padding: 0.2rem 0.5rem;margin-top:1rem' type='submit' name='submit' value='DOWNLOAD ARCHIVE'/>";
    echo "<br> Backup also available in  <i>" . $file . "</i> <br> <br>";
    echo '</form>';

    if (isset($_POST['submit']))
    {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=' . basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
    }
}

// REMOVE USER DRAWINGS AND IMAGES LINKED TO A JSON FILE =================================================================================
function remove_user_data($jsonfile)
{   
    global $drawings_dir;
    global $images_dir;
    global $partial_backup_path;


    $dirPath = dirname($jsonfile);
    $dirName = basename($dirPath);

    // backup in backups/partial/data
    $zipFile = $partial_backup_path . $dirName . '_' . time() . '.zip';
    $archive = new ZipArchive();
    $archive->open($zipFile, ZipArchive::CREATE);
    addToArchive($archive, $dirPath, $dirName, basename($jsonfile));
    addToArchive($archive,$dirPath, $dirName . $drawings_dir, $drawings_dir);
    addToArchive($archive, $dirPath, $dirName . $images_dir, $images_dir);
    $archive->close();

    // remove drawings
    $drawingsFolder = $dirPath . $drawings_dir ;
    array_map('unlink', glob($drawingsFolder . '/*.json'));
    if (is_dir($drawingsFolder)) rmdir($drawingsFolder);

    // remove user images
    $imagesFolder = $dirPath . $images_dir;
    array_map('unlink', glob($imagesFolder . '/*'));
    if (is_dir($imagesFolder)) rmdir($imagesFolder);

    unlink($jsonfile);
}

//REMOVE PREVIEWS ============================================================================================================
function remove_all_previews($submited)
{
    global $social_preview_file;
    unlink(dirname($submited) . '/'. $social_preview_file);
    unlink($submited);
}


// DISPLAY BUTTON TO DOWNLOAD A PROVIDED FILE =================================================================================
function remove_button($file)
{
    global $model_data_file;
    global $preview_file;
    global $mask_file;

    $name = strtoupper(basename(dirname($file)));
    echo '<form method="post" style="display:inline-block;margin:0.2rem">';
    echo "<input style='padding:0.2rem 0.5rem;' type='submit' name='submit[$file]' value='$name'/>";
    echo '</form>';

    if (isset($_POST['submit']))
    {
        $submited = array_keys($_POST['submit']) [0];
         if ( is_file($submited) )
            {
                if(basename($submited)==$model_data_file) remove_user_data($submited);
                if (basename($submited)==$preview_file) remove_all_previews($submited);
                if (basename($submited)==$mask_file) unlink($submited);
                header("Refresh:0");
            }
    }
}

