function um_file_uuencode($file)
{
    $content = file_get_contents($file);
    return um_data_encode($content);
}
function um_client_upgrade_to_package($package, $settings, $force = true, $update_offline = false)
{
    $applied_updates = array();
    $rollback = false;
    if (!is_object($package)) {
        return false;
    }
    if (!$update_offline) {
        um_client_db_connect($settings);
        um_component_db_connect();
        foreach ($package->updates as $update) {
            $success = um_client_apply_update($update, $settings, $force);
            if (!$success) {
                echo '<p /><strong>Failed</strong> on:<br />';
                um_client_print_update($update, $settings);
                $rollback = true;
                break;
            }
            array_push($applied_updates, $update);
        }
        if ($rollback) {
            foreach ($applied_updates as $update) {
                $success = um_client_rollback_update($update, $settings);
            }
            return false;
        }
        um_client_db_save_package($package, $settings);
        foreach ($package->updates as $update) {
            um_client_db_save_update($update);
        }
        um_db_update_setting('current_update', $package->id);
        //process_sql_commit();
    } else {
        $data_queries = '';
        $schema_queries = '';
        $zip = new ZipArchive();
        $temp_files = array();
        $md5_dir = md5("update_" . $package->id);
        // Get the temp path on the server
        $path_script = explode('/', $_SERVER['SCRIPT_FILENAME']);
        $path_server = explode('/', $_SERVER['DOCUMENT_ROOT']);
        unset($path_script[count($path_script) - 1]);
        $tempDir = implode('/', $path_script) . '/temp/';
        for ($i = 0; $i < count($path_server); $i++) {
            unset($path_script[$i]);
        }
        $tempDirServer = '/' . implode('/', $path_script) . '/temp/';
        $package_name = 'package_' . $package->id . '.oum';
        $zipArchive = $tempDir . $package_name;
        $zipArchiveServer = $tempDirServer . $package_name;
        @unlink($zipArchive);
        if ($zip->open($zipArchive, ZIPARCHIVE::CREATE) === true) {
            foreach ($package->updates as $update) {
                $filename = '';
                switch ($update->type) {
                    case 'code':
                    case 'binary':
                        $md5_name = md5($path_remote . $update->filename);
                        $success = um_client_create_update_file($update->data, $tempDir . $md5_name);
                        $zip->addFile($tempDir . $md5_name, $update->type . '/' . $update->filename);
                        $temp_files[] = $tempDir . $md5_name;
                        break;
                    case 'db_data':
                        $data = um_data_decode($update->data);
                        if (substr($data, strlen($data) - 1, 1) != ';') {
                            $data_queries .= $data . ";" . $update->db_table . ";" . $update->db_field . ";" . $update->db_field_value . ";" . $update->id . ";\n\n";
                        } else {
                            $data_queries .= $data . $update->db_table . ";" . $update->db_field . ";" . $update->db_field_value . ";" . $update->id . ";\n\n";
                        }
                        break;
                    case 'db_schema':
                        $data = um_data_decode($update->data);
                        if (substr($data, strlen($data) - 1, 1) != ';') {
                            $schema_queries .= $data . ";" . $update->id . ";\n\n";
                        } else {
                            $schema_queries .= $data . $update->id . ";\n\n";
                        }
                        break;
                }
            }
            // Creating the schema sql script
            $success = um_client_create_update_file(um_data_encode($schema_queries), $tempDir . "schema_db");
            $zip->addFile($tempDir . "schema_db", '01_package_' . $package->id . '_schema.sql');
            $temp_files[] = $tempDir . "schema_db";
            // Creating the data sql script
            $success = um_client_create_update_file(um_data_encode($data_queries), $tempDir . "data_db");
            $zip->addFile($tempDir . "data_db", '02_package_' . $package->id . '_data.sql');
            $temp_files[] = $tempDir . "data_db";
            // Creating the package info file
            $package_info = $package;
            unset($package_info->updates);
            $success = um_client_create_update_file(um_data_encode(json_encode($package_info)), $tempDir . "info_package");
            $zip->addFile($tempDir . "info_package", 'info_package');
            $temp_files[] = $tempDir . "info_package";
            $zip->close();
            // Clean temp files
            foreach ($temp_files as $file) {
                @unlink($file);
            }
            chdir($tempDir);
            header("Content-type: application/zip");
            header("Content-Disposition: attachment; filename=" . $package_name);
            header("Pragma: no-cache");
            header("Expires: 0");
            readfile($package_name);
            return true;
        }
    }
    return true;
}