<?php

/*
 * ==========================================================
 * UPLOAD.PHP
 * ==========================================================
 *
 * Manage all uploads of front-end and admin. � 2022-202cryptopaymentin.dev. All rights reserved.
 *
 */

require_once('functions.php');
if (CRPAY_CLOUD) {
    crpay_cloud_load();
}
if (defined('CRPAY_CROSS_DOMAIN') && CRPAY_CROSS_DOMAIN) {
    header('Access-Control-Allow-Origin: *');
}
if (isset($_FILES['file'])) {
    if (0 < $_FILES['file']['error']) {
        die(json_encode(['error', 'Cryptopayment: Error into upload.php file.']));
    } else {
        $file_name = crpay_sanatize_file_name($_FILES['file']['name']);
        $infos = pathinfo($file_name);
        $path_cloud = crpay_cloud_path_part();
        $path = __DIR__ . '/uploads' . $path_cloud;
        $url = CRPAY_URL . 'uploads' . $path_cloud;
        $is_checkout = crpay_isset($_GET, 'target') == 'checkout-file';
        $directory = $is_checkout ? '/checkout' : false;
        if (sb_is_allowed_extension(crpay_isset($infos, 'extension'))) {
            if (!file_exists($path)) {
                mkdir($path, 0755, true);
            }
            if ($directory) {
                $path .= $directory;
                $url .= $directory;
                if (!file_exists($path)) {
                    mkdir($path, 0755, true);
                }
            }
            if (!CRPAY_CLOUD && !file_exists($path . '/index.html')) {
                crpay_file($path . '/index.html', '');
            }
            $file_name = crpay_random_string() . '_' . crpay_string_slug($file_name);
            $path .= '/' . $file_name;
            $url .= '/' . $file_name;
            move_uploaded_file($_FILES['file']['tmp_name'], $path);
            if ($is_checkout && CRPAY_CLOUD && defined('CLOUD_AWS_S3')) {
                $url_aws = crpay_cloud_aws_s3($path);
                if (strpos($url_aws, 'http') === 0) {
                    $url = $url_aws;
                    unlink($path);
                }
            }
            die(json_encode([true, $url, $file_name]));
        } else {
            die(json_encode([false, 'The file you are trying to upload has an extension that is not allowed.']));
        }
    }
} else {
    die(json_encode([false, 'Cryptopayment Error: Key file in $_FILES not found.']));
}

function sb_is_allowed_extension($extension) {
    $extension = strtolower($extension);
    $allowed_extensions = ['oga', 'json', 'psd', 'ai', 'jpg', 'jpeg', 'png', 'gif', 'webp', 'pdf', 'doc', 'docx', 'key', 'ppt', 'odt', 'xls', 'xlsx', 'zip', 'rar', 'mp3', 'm4a', 'ogg', 'wav', 'mp4', 'mov', 'wmv', 'avi', 'mpg', 'ogv', '3gp', '3g2', 'mkv', 'txt', 'ico', 'csv', 'ttf', 'font', 'css', 'scss'];
    return in_array($extension, $allowed_extensions) || (defined('CRPAY_FILE_EXTENSIONS') && in_array($extension, CRPAY_FILE_EXTENSIONS));
}

?>