Exemplo n.º 1
0
function download_file()
{
    global $xoopsLogger, $rmTpl, $runFiles, $xoopsSecurity;
    $xoopsLogger->activated = false;
    $url = RMHttpRequest::post('url', 'string', '');
    $cred = RMHttpRequest::post('credentials', 'string', '');
    $type = RMHttpRequest::post('type', 'string', '');
    $dir = RMHttpRequest::post('dir', 'string', '');
    $ftpdata = RMHttpRequest::post('ftp', 'string', '');
    if ($url == '') {
        jsonReturn(__('Invalid parameters!', 'rmcommon'));
    }
    // Request access
    $query = explode("?", $url);
    $query[1] = ($query[1] != '' ? $query[1] . '&' : '') . 'action=identity' . ($cred != '' ? '&l=' . $cred : '');
    $response = json_decode(RMHttpRequest::load_url($query[0], $query[1], true), true);
    //$response = json_decode(file_get_contents($url.'&), true);
    if ($response['error'] == 1) {
        jsonReturn($response['message']);
    }
    //jsonReturn($response['data']['url']);
    if (!is_dir(XOOPS_CACHE_PATH . '/updates/')) {
        mkdir(XOOPS_CACHE_PATH . '/updates/', 511);
    }
    if (!file_put_contents(XOOPS_CACHE_PATH . '/updates/' . $type . '-' . $dir . '.zip', file_get_contents($response['data']['url']))) {
        jsonReturn(__('Unable to download update file!', 'rmcommon'));
    }
    // Get files list
    $details = json_decode(RMHttpRequest::load_url($url . '&action=update-details', '', true), true);
    if ($details['error'] == 1) {
        jsonReturn($details['message']);
    }
    $hash = $details['data']['hash'];
    $file_hash = md5_file(XOOPS_CACHE_PATH . '/updates/' . $type . '-' . $dir . '.zip');
    if ($hash != $file_hash) {
        @unlink(XOOPS_CACHE_PATH . '/updates/' . $type . '-' . $dir . '.zip');
        jsonReturn(__('The package file could be corrupted. Aborting!', 'rmcommon'));
    }
    // Extract files
    $zip = new ZipArchive();
    $res = $zip->open(XOOPS_CACHE_PATH . '/updates/' . $type . '-' . $dir . '.zip');
    if ($res !== TRUE) {
        jsonReturn(__('ERROR: unable to open downloaded zip file!', 'rmcommon'));
    }
    $rmUtil = RMUtilities::get();
    $source = XOOPS_CACHE_PATH . '/updates/' . $type . '-' . $dir;
    if (is_dir($source)) {
        $rmUtil->delete_directory($source);
    }
    $zip->extractTo($source);
    $zip->close();
    // Delete downloaded zip
    unlink(XOOPS_CACHE_PATH . '/updates/' . $type . '-' . $dir . '.zip');
    // Prepare to copy files
    $target = XOOPS_ROOT_PATH . '/modules/';
    if ($type == 'plugin') {
        $target .= 'rmcommon/plugins/' . $dir;
    } else {
        $target .= $dir;
    }
    if (!is_dir($target)) {
        jsonReturn(sprintf(__('Target path "%s" does not exists!', 'rmcommon'), $target));
    }
    /**
     * When rmcommon is the module to be updated then we need
     * to make a backup before to delete files
     */
    $excluded = array();
    if ($dir == 'rmcommon') {
        $excluded = array($target . '/plugins');
    }
    if (is_writable($target) && !empty($target)) {
        $target = str_replace('\\', '/', $target);
        // Deletes dir content to replace with new files
        RMUtilities::delete_directory($target, false, $excluded);
        // Copy new files
        $source = rtrim(str_replace('\\', '/', $source), '/');
        $odir = opendir($source);
        while (($file = readdir($odir)) !== false) {
            if ($file == '.' || $file == '..') {
                continue;
            }
            @rename($source . '/' . $file, $target . '/' . $file);
        }
        closedir($odir);
        RMUtilities::delete_directory($source);
    } else {
        if ($ftpdata == '') {
            jsonReturn(__('FTP configuration not specified!', 'rmcommon'));
        }
        parse_str($ftpdata);
        if ($ftp_server == '' || $ftp_user == '' || $ftp_pass == '') {
            jsonReturn(__('FTP configuration not valid!', 'rmcommon'));
        }
        $target = str_replace('\\', '/', $target);
        global $ftpConfig;
        $ftpConfig->server = $ftp_server;
        $ftpConfig->user = $ftp_user;
        $ftpConfig->pass = $ftp_pass;
        $ftpConfig->dir = $ftp_dir;
        $ftpConfig->port = $ftp_port > 0 ? $ftp_port : 21;
        $ftp = new RMFtpClient($ftp_server, $ftp_port > 0 ? $ftp_port : 21, $ftp_user, $ftp_pass);
        if (!$ftp->connect()) {
            jsonReturn(sprintf(__('Unable to connect FTP server %s', 'rmcommon'), '<strong>' . $ftp_server . '</strong>'));
        }
        $ftpConfig->base = $ftpConfig->dir . '/modules/' . ($type == 'plugin' ? 'rmcommon/plugins/' : '') . $dir;
        $ftpConfig->source = $source;
        $ftpConfig->target = $target;
        // Clean current element directory
        deleteFTPDir($ftpConfig->base, $ftp, false);
        // Copy new files
    }
    // Update uploads file
    $updates = unserialize(base64_decode(file_get_contents(XOOPS_CACHE_PATH . '/updates.chk')));
    $new = array();
    foreach ($updates['updates'] as $upd) {
        if ($upd['data']['type'] == $type && $upd['data']['dir'] == $dir) {
            continue;
        }
        $new[] = $upd;
    }
    file_put_contents(XOOPS_CACHE_PATH . '/updates.chk', base64_encode(serialize(array('date' => $updates['date'], 'total' => intval($updates['total']) - 1, 'updates' => $new))));
    if (!empty($runFiles)) {
        jsonReturn(__('Executing files...', 'rmcommon'), 0, array('run' => json_encode($runFiles)));
    } else {
        jsonReturn(sprintf(__('%s has been updated', 'rmcommon'), '<strong>' . $dir . '</strong>'), 0);
    }
}