<?php
require_once __DIR__ . '/../config/init.php';
if (Input('upload') == 'Submit') {
    $FileUrls = isset($_POST['copy_url']) ? $_POST['copy_url'] : array();
    $filePathToSave = $RootPath . '/upload_file/fm';
    if (is_dir($filePathToSave)) {
        @mkdir($filePathToSave);
    }

    if (count($FileUrls)) {
        foreach ($FileUrls as $singleurl) {
            $down_URL = urlexpander($singleurl);
            $donwloadLink = trim($down_URL);
            $NewFileName = trim(str_replace('%20', ' ', $down_URL));
            $urlInfo = pathinfo($donwloadLink);
            $fileNameInfo = pathinfo($NewFileName);
            $fileName = ($fileNameInfo['filename'] ? $fileNameInfo['filename'] : str_replace('%20', ' ', $urlInfo['filename']));
            $fileExt = explode('.', $NewFileName);
            $fileExt = end($fileExt);
            if (strtolower($fileExt) == 'mp3' || strtolower($fileExt) == 'zip')
                urlCopy($donwloadLink, $filePathToSave . '/' . $fileName . '.' . $fileExt);
        }
    }
    $countfiles = count($_FILES['uploadfile']['name']);
    for ($i = 0; $i < $countfiles; $i++) {
        $UpFileType = strtolower(pathinfo($_FILES['uploadfile']['name'][$i], PATHINFO_EXTENSION));
        $allowtype = array('mp3', 'zip');
        if (in_array($UpFileType, $allowtype)) {
            $filename = $_FILES['uploadfile']['name'][$i];
            move_uploaded_file($_FILES['uploadfile']['tmp_name'][$i], $filePathToSave . '/' . $filename);
        }
    }
} else if (Input('deletefile')) {
    $filepath = @base64_decode(Input('deletefile'));
    $re = '/upload_file\/fm/m';
    if (preg_match($re, $RootPath . '/' . $filepath)) {
        if (is_file($RootPath . '/' . $filepath)) {
            unlink($RootPath . '/' . $filepath);
        }
        if (is_dir($RootPath . '/' . $filepath)) {
            rmdirr($RootPath . '/' . $filepath);
        }
    }
} else if (Input('decompress')) {
    $filename = @base64_decode(Input('decompress'));
    $dirpath = Input('dir_path');
    if (file_exists($RootPath . '/' . $dirpath . '/' . $filename)) {
        unZipFile($RootPath . '/' . $dirpath . '/' . $filename, $RootPath . '/' . $dirpath);
    }

    header('Location: multi.php?cid=' . Input('cid') . '&dir_path=' . $dirpath);
    exit;
}


header('Location: multi.php?cid=' . Input('cid'));
