function wp_beifen_process_ajax_request($request_options)
{
    // Get options
    $options = get_option(WP_BEIFEN_OPTIONS);
    $bkp_id = substr($request_options['id'], 7);
    // Load required class definitions and create instances
    require_once $options['plugin_location'] . 'classes' . DS . 'file.php';
    require_once $options['plugin_location'] . 'classes' . DS . 'database.php';
    $bkp_db = new XinitBackupDatabaseHelper();
    $bkp_file = new XinitBackupFileHelper();
    $the_backup = $bkp_db->getBackupByID($bkp_id);
    $bkp_file->deleteFolder($the_backup->location);
    $bkp_db->deleteBackupEntry($bkp_id);
    $result['status'] = __("Success", WP_BEIFEN_DOMAIN);
    $result['message'] = __("Backup deleted", WP_BEIFEN_DOMAIN);
    $result['id'] = $bkp_id;
    return $result;
}
function create_scheduled_backup($request_options)
{
    // Modify time limit
    set_time_limit($request_options['backup_timeout']);
    // Get options
    $options = get_option(WP_BEIFEN_OPTIONS);
    // A shortcut for DIRECTORY_SEPARATOR
    if (!defined('DS')) {
        define('DS', DIRECTORY_SEPARATOR);
    }
    // Defines for recursive function use
    define('WP_BEIFEN_DIR', $options['plugin_backup_directory']);
    define('WP_BEIFEN_CURRENT_BACKUP', WP_BEIFEN_DIR . $request_options['backup_name'] . DS);
    define('WP_BEIFEN_CURRENT_DB_DIR', WP_BEIFEN_CURRENT_BACKUP . 'database' . DS);
    // Load required class definitions and create instances
    require_once $options['plugin_location'] . 'classes' . DS . 'file.php';
    require_once $options['plugin_location'] . 'classes' . DS . 'database.php';
    $bkp_db = new XinitBackupDatabaseHelper();
    $bkp_file = new XinitBackupFileHelper();
    // Check for existing backups with same name
    if ($bkp_db->existingBackup($request_options['backup_name'])) {
        $result['status'] = __("Error", WP_BEIFEN_DOMAIN);
        $result['message'] = __("There is already a backup with this name. Please change the name!", WP_BEIFEN_DOMAIN);
        return $result;
    }
    // Check if main backup directory is writable
    if (!is_writable(WP_BEIFEN_DIR)) {
        $result['status'] = __("Error", WP_BEIFEN_DOMAIN);
        $result['message'] = __("The backup folder is not writable. Please check write-permissions!", WP_BEIFEN_DOMAIN);
        return $result;
    }
    // Check if backup destination directory is writable and existing
    if (!@mkdir(WP_BEIFEN_CURRENT_BACKUP) && !file_exists(WP_BEIFEN_CURRENT_BACKUP)) {
        $result['status'] = __("Error", WP_BEIFEN_DOMAIN);
        $result['message'] = __("Could not create backup folder. Please check write-permissions!", WP_BEIFEN_DOMAIN);
        return $result;
    }
    // if DB backup, check db destionation
    if ($request_options['backup_type'] == 'Complete' || $request_options['backup_type'] == 'DB') {
        if (!file_exists(WP_BEIFEN_CURRENT_DB_DIR) && !mkdir(WP_BEIFEN_CURRENT_DB_DIR)) {
            $result['status'] = __("Error", WP_BEIFEN_DOMAIN);
            $result['message'] = __("The database backup folder is not writable. Please check write-permissions!", WP_BEIFEN_DOMAIN);
            return $result;
        }
    }
    // Windows path hack
    if (DS == '/') {
        define('ABSPATH_CUSTOM', ABSPATH);
    } else {
        $custom_abspath = substr(ABSPATH, 0, -1);
        define('ABSPATH_CUSTOM', $custom_abspath);
    }
    // Exclude backup directories
    $exclude = array();
    // Exclude the backup itself
    $exclude[] = WP_BEIFEN_CURRENT_BACKUP;
    // Exclude existing backups?
    if (!$options['include_backup_directory']) {
        $exclude[] = WP_BEIFEN_DIR;
    }
    // Do it!
    switch ($request_options['backup_type']) {
        case 'Complete':
            // Complete backup
            // Database backup
            foreach ($bkp_db->getTableNames() as $table_name) {
                $dump = $bkp_db->getTableDump($table_name);
                if ($dump != '') {
                    $bkp_file->writeTextToFile(WP_BEIFEN_CURRENT_DB_DIR . $table_name . '.sql', $dump);
                }
                unset($dump);
            }
            // File Backup
            $bkp_file->copyFolderRec(ABSPATH_CUSTOM, WP_BEIFEN_CURRENT_BACKUP . 'root' . DS, $exclude);
            break;
        case 'Files':
            // files only backup
            $bkp_file->copyFolderRec(ABSPATH_CUSTOM, WP_BEIFEN_CURRENT_BACKUP . 'root' . DS, $exclude);
            break;
        case 'DB':
            // DB only backup
            foreach ($bkp_db->getTableNames() as $table_name) {
                $dump = $bkp_db->getTableDump($table_name);
                if ($dump != '') {
                    $bkp_file->writeTextToFile(WP_BEIFEN_CURRENT_DB_DIR . $table_name . '.sql', $dump);
                }
                unset($dump);
            }
            break;
        default:
            $result['status'] = __("Error", WP_BEIFEN_DOMAIN);
            $result['message'] = __("This is not a valid backup type!", WP_BEIFEN_DOMAIN);
            return $result;
    }
    // Zip backup, if requested
    if ($request_options["compress_backup"] == 'Yes') {
        require_once $options['plugin_location'] . 'classes' . DS . 'zip.php';
        $zipper = new XinitBackupZipHelper();
        $zipper->createZip(WP_BEIFEN_CURRENT_BACKUP . $request_options['backup_name'] . '.zip', WP_BEIFEN_CURRENT_BACKUP);
    }
    // Create index.html and .htaccess
    $empty_html = '<html><body></body></html>';
    $htaccess_l1 = "RewriteEngine on\n";
    $htaccess_l2 = "RewriteRule (.*) " . get_bloginfo('url') . "/ [R=301,L]";
    $bkp_file->writeTextToFile(WP_BEIFEN_CURRENT_BACKUP . 'index.html', $empty_html, 'a');
    $bkp_file->writeTextToFile(WP_BEIFEN_CURRENT_BACKUP . '.htaccess', $htaccess_l1, 'w');
    $bkp_file->writeTextToFile(WP_BEIFEN_CURRENT_BACKUP . '.htaccess', $htaccess_l2, 'a');
    $bkp_file->writeTextToFile(WP_BEIFEN_CURRENT_DB_DIR . 'index.html', $empty_html, 'a');
    $bkp_file->writeTextToFile(WP_BEIFEN_CURRENT_DB_DIR . '.htaccess', $htaccess_l1, 'w');
    $bkp_file->writeTextToFile(WP_BEIFEN_CURRENT_DB_DIR . '.htaccess', $htaccess_l2, 'a');
    // If everythings ok ...
    $zipped = $request_options["compress_backup"] == 'Yes' ? "TRUE" : "FALSE";
    $bkp_db->insertNewBackupEntry($request_options['backup_name'], WP_BEIFEN_CURRENT_BACKUP, $request_options['backup_type'], $zipped);
    $result['status'] = __("Success", WP_BEIFEN_DOMAIN);
    $result['message'] = __("Backup has been successfully created!", WP_BEIFEN_DOMAIN);
    if ($options['enable_debugging'] == true) {
        $memory_usage = memory_get_usage() / 1048576;
        $result['message'] .= '<br/>Memory usage: ' . number_format($memory_usage, 2) . ' MB';
    }
    return $result;
}
function wp_beifen_process_ajax_request($request_options)
{
    // Check if directory exists
    $request_options['backup_directory'] = wp_beifen_clean_path($request_options['backup_directory']);
    if (!file_exists($request_options['backup_directory'])) {
        // No, can create it?
        if (!@mkdir($request_options['backup_directory'])) {
            // No, return error message
            $result['status'] = __("Error", WP_BEIFEN_DOMAIN);
            $result['message'] = __("The backup directory does not exist and cannot be created!", WP_BEIFEN_DOMAIN);
            return $result;
        }
    }
    // Is file a directory ?
    if (!is_dir($request_options['backup_directory'])) {
        // No, return error message
        $result['status'] = __("Error", WP_BEIFEN_DOMAIN);
        $result['message'] = __("The path is not a valid directory!", WP_BEIFEN_DOMAIN);
        return $result;
    }
    // Is directory writable?
    if (!is_writable($request_options['backup_directory'])) {
        // No, return error message
        $result['status'] = __("Error", WP_BEIFEN_DOMAIN);
        $result['message'] = __("The backup directory is not writable!", WP_BEIFEN_DOMAIN);
        return $result;
    }
    // Is execution time valid?
    if ($request_options['default_timeout'] < 0 && $request_options['default_timeout'] > 299) {
        // No, return error message
        $result['status'] = __("Error", WP_BEIFEN_DOMAIN);
        $result['message'] = __("The execution time limit is invalid!", WP_BEIFEN_DOMAIN);
        return $result;
    }
    // All checks ok, update options
    if (!defined('DS')) {
        define('DS', DIRECTORY_SEPARATOR);
    }
    // Check for trailing slash
    if ($request_options['backup_directory'][strlen($request_options['backup_directory']) - 1] != DS) {
        $request_options['backup_directory'] .= DS;
    }
    //get and change options
    $options = get_option(WP_BEIFEN_OPTIONS);
    $options['plugin_backup_directory'] = $request_options['backup_directory'];
    $options['plugin_ready'] = true;
    $options['default_timeout'] = $request_options['default_timeout'];
    if ($request_options['enable_debugging'] == 'yes') {
        $options['enable_debugging'] = true;
    } else {
        $options['enable_debugging'] = false;
    }
    if ($request_options['include_backup_directory'] == 'yes') {
        $options['include_backup_directory'] = true;
    } else {
        $options['include_backup_directory'] = false;
    }
    if ($request_options['backup_schedule'] == 'yes') {
        $options['backup_schedule'] = true;
        wp_schedule_event(time(), 'hourly', 'beifen_hourly_backup_check');
    } else {
        $options['backup_schedule'] = false;
        wp_clear_scheduled_hook('beifen_hourly_backup_check');
    }
    // update options
    update_option(WP_BEIFEN_OPTIONS, $options);
    // Create index.php and .htaccess
    require_once $options['plugin_location'] . 'classes' . DS . 'file.php';
    $empty_html = '<html><body></body></html>';
    $htaccess_l1 = "RewriteEngine on\n";
    $htaccess_l2 = "RewriteRule (.*) " . get_bloginfo('url') . "/ [R=301,L]";
    $file = new XinitBackupFileHelper();
    $file->writeTextToFile($request_options['backup_directory'] . 'index.html', $empty_html, 'a');
    $file->writeTextToFile($request_options['backup_directory'] . '.htaccess', $htaccess_l1, 'w');
    $file->writeTextToFile($request_options['backup_directory'] . '.htaccess', $htaccess_l2, 'a');
    // Return success message
    $result['status'] = __("Success", WP_BEIFEN_DOMAIN);
    $result['message'] = __("All changes were saved successfully!", WP_BEIFEN_DOMAIN);
    return $result;
}
function delete_old_backup($old_bkp_name)
{
    // Get options
    $options = get_option(WP_BEIFEN_OPTIONS);
    // Load required class definitions and create instances
    require_once $options['plugin_location'] . 'classes' . DS . 'file.php';
    require_once $options['plugin_location'] . 'classes' . DS . 'database.php';
    $bkp_db = new XinitBackupDatabaseHelper();
    $bkp_file = new XinitBackupFileHelper();
    $bkp_id = $bkp_db->getIDByName($old_bkp_name);
    $the_backup = $bkp_db->getBackupByID($bkp_id);
    if (file_exists($the_backup->location)) {
        $bkp_file->deleteFolder($the_backup->location);
    }
    $bkp_db->deleteBackupEntry($bkp_id);
}
function readDumpFolder($folder)
{
    // Get options
    $options = get_option(WP_BEIFEN_OPTIONS);
    require_once $options['plugin_location'] . 'classes' . DS . 'file.php';
    $bkp_file = new XinitBackupFileHelper();
    $dump = '';
    if (file_exists($folder)) {
        $folderhandle = opendir($folder);
        if ($folderhandle) {
            while (($entry = readdir($folderhandle)) !== false) {
                if ($entry == false || $entry == '..' || $entry == '.' || $entry == '.htaccess' || $entry == 'index.html') {
                    continue;
                }
                $dump .= $bkp_file->readTextFromFile($folder . $entry);
            }
        } else {
            die("Cannot read Folder!");
        }
    } else {
        die("Folder does not exist!");
    }
    return $dump;
}