<?php
/**
 * VFM - veno file manager: ajax/get-remote.php
 *
 * Download files from remote URL
 *
 * PHP version >= 5.3
 *
 * @category  PHP
 * @package   VenoFileManager
 * @author    Nicola Franchini <support@veno.it>
 * @copyright 2013-2022 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.utils.php';
require_once dirname(__DIR__).'/class/class.setup.php';
require_once dirname(__DIR__).'/class/class.location.php';
require_once dirname(__DIR__).'/class/class.gatekeeper.php';

$location_dir = urldecode(filter_input(INPUT_POST, 'get_location', FILTER_SANITIZE_SPECIAL_CHARS));
$location = new Location('../../'.$location_dir);
$setUp = new SetUp();
$gateKeeper = new GateKeeper();

if ($location->editAllowed('../../')
    && $gateKeeper->isAllowed('upload_enable')
    && $setUp->getConfig('remote_uploader') === true
) {
    include_once dirname(__DIR__).'/class/class.actions.php';
    include_once dirname(__DIR__).'/class/class.uploader.php';
    include_once dirname(__DIR__).'/class/class.logger.php';

    $uploader = new Uploader();

    if (!function_exists('curl_version')) {
        Utils::setError('<span><i class="bi bi-exclamation-triangle"></i> Enable server Curl');
        exit;
    }

    $getfile = filter_input(INPUT_POST, 'get_upload_url', FILTER_VALIDATE_URL);

    if (!$getfile) {
        Utils::setError('<span><i class="bi bi-exclamation-triangle"></i> '.$setUp->getLangString('invalid_url'));
        exit;
    }

    $getfile = Uploader::removeQueryString($getfile);

    // Get size of remote file.
    $filesize = $uploader->getRemoteSize($getfile);

    if (!$filesize) {
        exit;
    }

    $getfileinfo = Utils::mbPathinfo($getfile);
    $extension = Utils::getFileExtension($getfile);
    $filename = Utils::normalizeStr(Utils::checkMagicQuotes($getfileinfo['filename']));
    // append date to filename
    $filename = $setUp->getConfig('overwrite_files') === 'date' ? $filename.'_'.date('Y-m-d_G-i-s') : $filename;

    $setfile = '../../'.$location_dir.Uploader::safeExtension($filename.'.'.$extension, $extension);

    // Check if file can be uploaded.
    if (!$uploader->veryFile($setfile, $filesize, true)) {
        exit;
    }

    $fp = fopen($setfile, 'wb');
    if (!$fp) {
        Utils::setError('<span><i class="bi bi-exclamation-triangle"></i> '.$setUp->getLangString('failed_upload').'</span>');
        exit;
    }

    $ch = curl_init($getfile);
    curl_setopt_array(
        $ch, 
        array(
            CURLOPT_FILE => $fp,
            CURLOPT_HEADER => false,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_FOLLOWLOCATION => false,
        )
    );

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $curlError = curl_error($ch);

    fclose($fp);

    if ($curlError) {
        @unlink($setfile);
        Utils::setError($curlError);
        if (is_resource($ch)) {
            curl_close($ch);
        }
        exit;
    }

    if (!$response) {
        @unlink($setfile);
        $message = '<span><i class="bi bi-exclamation-triangle"></i> '.$setUp->getLangString('error_downloading_remote_file').'</span>';
        Utils::setError($message);
        if (is_resource($ch)) {
            curl_close($ch);
        }
        exit;
    }

    Actions::updateUserSpace(false, false, $filesize);
    $message = array(
        'user' => $gateKeeper->getUserInfo('name'),
        'action' => 'ADD',
        'type' => 'file',
        'item' => $location_dir.$filename.'.'.$extension,
    );
    Logger::log($message, '../');
    if ($setUp->getConfig('notify_upload')) {
        Logger::emailNotification('./'.$location_dir.$filename.'.'.$extension, 'upload');
    }
    Utils::setSuccess(' <span><i class="bi bi-check-circle"></i> <strong>'.$filename.'.'.$extension.'</strong></span> ');

    if (is_resource($ch)) {
        curl_close($ch);
    }
    unset($ch);

} else {
    $message = '<span><i class="bi bi-exclamation-triangle"></i> '.$setUp->getLangString('upload_not_allowed').'</span> ';
    Utils::setError($message);
}
exit;
