/**
 * Creates a list of all the files under the given directory.
 *
 * @param	string	$directory			Directory to list the files of
 * @param	bool	$ignore_directories	Ignores sub directories if set to TRUE
 * @param	array	$ommit				Array of directories to ommit
 * @return	array	$files				File list
 */
function wpmove_list_all_files($directory, $ignore_directories = FALSE, $ommit = array())
{
    $files = array();
    if (is_array($fileList = glob($directory . "/*"))) {
        foreach ($fileList as $file) {
            if (is_file($file)) {
                array_push($files, $file);
            } elseif (!$ignore_directories) {
                $skip = FALSE;
                foreach ($ommit as $dir) {
                    if ($file == $dir) {
                        $skip = TRUE;
                    }
                }
                if (!$skip) {
                    if (count(@scandir($file)) > 2) {
                        $files = array_merge($files, wpmove_list_all_files($file, FALSE, $ommit));
                    } else {
                        array_push($files, $file);
                    }
                }
            }
        }
    }
    return $files;
}
        /**
         * Generates the backup manager page of the plugin.
         *
         * @param void
         * @return void
         */
        function print_backup_manager_page()
        {
            if ($_POST && 'manage' == $_POST['act'] && check_admin_referer('wpmove_backup_manager_submit')) {
                // Set the appropriate target directory depending on the form submitted
                if (isset($_POST['wpmove_current_backups'])) {
                    $move_target = WPMOVE_OLD_BACKUP_DIR;
                } else {
                    if (isset($_POST['wpmove_old_backups'])) {
                        $move_target = WPMOVE_BACKUP_DIR;
                    }
                }
                // If there's an actual array sent
                if (isset($_POST['files']) && is_array($_POST['files'])) {
                    // Sanitize the POST data
                    $files = array_map('sanitize_text_field', $_POST['files']);
                    $action = sanitize_text_field($_POST['action']);
                    // Do what's requested
                    if ('delete' == $action) {
                        foreach ($files as $file) {
                            unlink($file);
                        }
                    } else {
                        if ('toggle' == $action) {
                            foreach ($files as $file) {
                                rename($file, trailingslashit($move_target) . basename($file));
                            }
                        } else {
                            if ('convert' == $action) {
                                foreach ($files as $file) {
                                    wpmove_convert_db_backup($file);
                                }
                            }
                        }
                    }
                }
            } else {
                if ($_POST && 'create' == $_POST['act'] && check_admin_referer('wpmove_backup_manager_create_backup')) {
                    // Load plugin settings
                    $wpmove_options = $this->get_admin_options();
                    // An array to hold backup files that will be uploaded
                    $backups = array();
                    // Create a backup of the database
                    wpmove_create_db_backup($wpmove_options['db_chunk_size']);
                    if (isset($_POST['wpmove_create_full_backup'])) {
                        // List all of the files inside the main directory
                        $abspath = substr(ABSPATH, 0, strlen(ABSPATH) - 1);
                        $files = wpmove_list_all_files($abspath, FALSE, array(WPMOVE_DIR, WPMOVE_BACKUP_DIR, WPMOVE_OLD_BACKUP_DIR));
                        // Create chunks from the selected files
                        $chunks = wpmove_divide_into_chunks($files, $wpmove_options['fs_chunk_size']);
                        // To prevent overwriting archives created in the same second
                        $chunk_id = 1;
                        // Create an archive of the each chunk
                        foreach ($chunks as $chunk) {
                            wpmove_create_archive($chunk, ABSPATH, $chunk_id++);
                        }
                    }
                }
            }
            ?>
			<div class="wrap">
				<div id="icon-tools" class="icon32">
					<br>
				</div>
				<h2><?php 
            _e('Backup Manager', 'WPMove');
            ?>
 <a class="add-new-h2" href="<?php 
            echo esc_url(admin_url('tools.php?page=wpmove-backup-manager&do=create'));
            ?>
" title="Create A Backup"><?php 
            _e('Backup Now', 'WPMove');
            ?>
</a></h2>
				<h3><?php 
            _e('Backup Now', 'WPMove');
            ?>
</h3>
				<p>
					<?php 
            _e('You can always create backups of your WordPress installation to use as restoration points. Select one of the methods below to create a quick backup.', 'WPMove');
            ?>
				</p>
				<form method="post" action="<?php 
            echo esc_url(admin_url('tools.php?page=wpmove-backup-manager'));
            ?>
">
					<input name="act" type="hidden" value="create" />
					<?php 
            wp_nonce_field('wpmove_backup_manager_create_backup');
            submit_button(__('Create a Database Backup', 'WPMove'), 'secondary', 'wpmove_create_database_backup', FALSE);
            echo '&nbsp;';
            submit_button(__('Create a Full Backup', 'WPMove'), 'secondary', 'wpmove_create_full_backup', FALSE);
            ?>
				</form>			
				<br>
				<h3><?php 
            _e('Current Backups', 'WPMove');
            ?>
</h3>
				<p>
					<?php 
            _e('Below are the files stored under your backup directory. These files will be used if you choose to complete the migration.', 'WPMove');
            ?>
				</p>
				<?php 
            // List all current backup files
            $current_backups = wpmove_list_all_files(WPMOVE_BACKUP_DIR, TRUE);
            // List all old backup files
            $old_backups = wpmove_list_all_files(WPMOVE_OLD_BACKUP_DIR);
            // List all converted database backup files
            $converted_backups = wpmove_list_all_files(WPMOVE_CONVERTED_BACKUP_DIR);
            ?>
				<form method="post" action="<?php 
            echo esc_url(admin_url('tools.php?page=wpmove-backup-manager'));
            ?>
">
					<?php 
            wp_nonce_field('wpmove_backup_manager_submit');
            ?>
					<input name="act" type="hidden" value="manage" />
					<div class="tablenav top">
						<div class="alignleft actions">
							<select name="action" size="1" height="1">
								<option value="toggle"><?php 
            _e('Archive', 'WPMove');
            ?>
</option>
								<option value="convert"><?php 
            _e('Convert', 'WPMove');
            ?>
</option>
								<option value="delete"><?php 
            _e('Delete', 'WPMove');
            ?>
</option>
							</select>
							<?php 
            submit_button(__('Apply', 'WPMove'), 'secondary', 'wpmove_current_backups', FALSE);
            ?>
						</div>
					</div>
					<table class="wp-list-table widefat fixed" cellspacing="0">
						<thead>
							<tr>
								<th scope="col" id="cb" class="manage-column column-cb check-column" style>
									<input type="checkbox">
								</th>
								<th scope="col" id="name" class="manage-column column-name" style>
									<a href="#"><?php 
            _e('Name', 'WPMove');
            ?>
</a>
								</th>
								<th scope="col" id="type" class="manage-column column-type" style>
									<a href="#"><?php 
            _e('Type', 'WPMove');
            ?>
</a>
								</th>
								<th scope="col" id="size" class="manage-column column-size" style>
									<a href="#"><?php 
            _e('Size', 'WPMove');
            ?>
</a>
								</th>
								<th scope="col" id="date" class="manage-column column-date" style>
									<a href="#"><?php 
            _e('Date Created', 'WPMove');
            ?>
</a>
								</th>
							</tr>
						</thead>
						<tfoot>
							<tr>
								<th scope="col" id="cb" class="manage-column column-cb check-column" style>
									<input type="checkbox">
								</th>
								<th scope="col" id="name" class="manage-column column-name" style>
									<a href="#"><?php 
            _e('Name', 'WPMove');
            ?>
</a>
								</th>
								<th scope="col" id="type" class="manage-column column-type" style>
									<a href="#"><?php 
            _e('Type', 'WPMove');
            ?>
</a>
								</th>
								<th scope="col" id="size" class="manage-column column-size" style>
									<a href="#"><?php 
            _e('Size', 'WPMove');
            ?>
</a>
								</th>
								<th scope="col" id="date" class="manage-column column-date" style>
									<a href="#"><?php 
            _e('Date Created', 'WPMove');
            ?>
</a>
								</th>
							</tr>
						</tfoot>
						<tbody id="the-list">
						<?php 
            // Display a message if no backup files found
            if (count($current_backups) > 1) {
                // For zebra striping
                $i = 0;
                // Display all current backups starting with database backups
                foreach ($current_backups as $file) {
                    // Get the file extension
                    $ext = substr($file, -3, 3);
                    // Decide the type of the backup
                    if ('sql' == $ext) {
                        preg_match('/DBBackup-([0-9]*).sql/', basename($file), $timestamp);
                        $type = __('Database Backup', 'WPMove');
                    } else {
                        if ('zip' == $ext) {
                            preg_match('/Backup-([0-9]*).zip/', basename($file), $timestamp);
                            $type = __('Filesystem Backup', 'WPMove');
                        } else {
                            continue;
                        }
                    }
                    // For zebra striping
                    if ($i % 2 !== 0) {
                        $class = ' class="alternate"';
                    } else {
                        $class = '';
                    }
                    // Display the row
                    echo '	<tr id="file-' . $i . '" valign="middle"' . $class . '>
												<th scope="row" class="check-column">
													<input id="file-' . $i . '" name="files[]" type="checkbox" value="' . $file . '">
												</th>
												<td class="column-name">
													<strong>
														<a href="' . esc_url(trailingslashit(WPMOVE_BACKUP_URL) . basename($file)) . '">' . esc_html(basename($file)) . '</a>
													</strong>
												</td>
												<td class="column-type">
													<a href="#">' . esc_html($type) . '</a>
												</td>
												<td class="column-size">
													' . esc_html(round(filesize($file) / 1024, 2)) . ' KB
												</td>
												<td class="column-date">
													' . esc_html(date('d.m.Y H:i:s', substr($timestamp['1'], 0, 10))) . '
												</td>
											</tr>';
                    // Increase the counter for zebra striping
                    $i++;
                }
            } else {
                echo '<tr class="no-items">
									  	<td class="colspanchange" colspan="5">
									  		' . __('No backup files found.', 'WPMove') . '
									  	</td>
									  </tr>';
            }
            ?>
						</tbody>
					</table>
				</form>
				<br>
				<h3><?php 
            _e('Old Backups', 'WPMove');
            ?>
</h3>
				<p>
					<?php 
            _e('Below are the files stored under your old backup directory. These files will not be used while completing the migration unless you unarchive them.', 'WPMove');
            ?>
				</p>
				<form method="post" action="<?php 
            echo esc_url(admin_url('tools.php?page=wpmove-backup-manager'));
            ?>
">
					<?php 
            wp_nonce_field('wpmove_backup_manager_submit');
            ?>
					<input name="act" type="hidden" value="manage" />
					<div class="tablenav top">
						<div class="alignleft actions">
							<select name="action" size="1" height="1">
								<option value="toggle"><?php 
            _e('Unarchive', 'WPMove');
            ?>
</option>
								<option value="convert"><?php 
            _e('Convert', 'WPMove');
            ?>
</option>
								<option value="delete"><?php 
            _e('Delete', 'WPMove');
            ?>
</option>
							</select>
							<?php 
            submit_button(__('Apply', 'WPMove'), 'secondary', 'wpmove_old_backups', FALSE);
            ?>
						</div>
					</div>
					<table class="wp-list-table widefat fixed" cellspacing="0">
						<thead>
							<tr>
								<th scope="col" id="cb" class="manage-column column-cb check-column" style>
									<input type="checkbox">
								</th>
								<th scope="col" id="name" class="manage-column column-name" style>
									<a href="#"><?php 
            _e('Name', 'WPMove');
            ?>
</a>
								</th>
								<th scope="col" id="type" class="manage-column column-type" style>
									<a href="#"><?php 
            _e('Type', 'WPMove');
            ?>
</a>
								</th>
								<th scope="col" id="size" class="manage-column column-size" style>
									<a href="#"><?php 
            _e('Size', 'WPMove');
            ?>
</a>
								</th>
								<th scope="col" id="date" class="manage-column column-date" style>
									<a href="#"><?php 
            _e('Date Created', 'WPMove');
            ?>
</a>
								</th>
							</tr>
						</thead>
						<tfoot>
							<tr>
								<th scope="col" id="cb" class="manage-column column-cb check-column" style>
									<input type="checkbox">
								</th>
								<th scope="col" id="name" class="manage-column column-name" style>
									<a href="#"><?php 
            _e('Name', 'WPMove');
            ?>
</a>
								</th>
								<th scope="col" id="type" class="manage-column column-type" style>
									<a href="#"><?php 
            _e('Type', 'WPMove');
            ?>
</a>
								</th>
								<th scope="col" id="size" class="manage-column column-size" style>
									<a href="#"><?php 
            _e('Size', 'WPMove');
            ?>
</a>
								</th>
								<th scope="col" id="date" class="manage-column column-date" style>
									<a href="#"><?php 
            _e('Date Created', 'WPMove');
            ?>
</a>
								</th>
							</tr>
						</tfoot>
						<tbody id="the-list">
						<?php 
            // Display a message if no backup files found
            if (count($old_backups) > 1) {
                // For zebra striping
                $i = 0;
                // Display all old backups starting with database backups
                foreach ($old_backups as $file) {
                    // Get the file extension
                    $ext = substr($file, -3, 3);
                    // Decide the backup type
                    if ($ext == 'sql') {
                        preg_match('/DBBackup-([0-9]*).sql/', basename($file), $timestamp);
                        $type = __('Database Backup', 'WPMove');
                    } else {
                        if ($ext == 'zip') {
                            preg_match('/Backup-([0-9]*).zip/', basename($file), $timestamp);
                            $type = __('Filesystem Backup', 'WPMove');
                        } else {
                            continue;
                        }
                    }
                    // For zebra striping
                    if ($i % 2 !== 0) {
                        $class = ' class="alternate"';
                    } else {
                        $class = '';
                    }
                    // Display the row
                    echo '	<tr id="file-' . $i . '" valign="middle"' . $class . '>
												<th scope="row" class="check-column">
													<input id="file-' . $i . '" name="files[]" type="checkbox" value="' . $file . '">
												</th>
												<td class="column-name">
													<strong>
														<a href="' . esc_url(trailingslashit(WPMOVE_OLD_BACKUP_URL) . basename($file)) . '">' . esc_html(basename($file)) . '</a>
													</strong>
												</td>
												<td class="column-type">
													<a href="#">' . esc_html($type) . '</a>
												</td>
												<td class="column-size">
													' . esc_html(round(filesize($file) / 1024, 2)) . ' KB
												</td>
												<td class="column-date">
													' . esc_html(date('d.m.Y H:i:s', substr($timestamp['1'], 0, 10))) . '
												</td>
											</tr>';
                    // For zebra striping
                    $i++;
                }
            } else {
                echo '<tr class="no-items">
									  	<td class="colspanchange" colspan="5">
									  		' . __('No backup files found.', 'WPMove') . '
									  	</td>
									  </tr>';
            }
            ?>
						</tbody>
					</table>
				</form>
				<br>
				<h3><?php 
            _e('Converted Database Backups', 'WPMove');
            ?>
</h3>
				<p>
					<?php 
            _e('Below are the converted database backup files which, unlike the files listed above, can be used outside WordPress Move. You may need the converted versions of your databsae backups if the plugin fails to migrate your installation properly. These files will not be used by the plugin at any stage.', 'WPMove');
            ?>
				</p>
				<form method="post" action="<?php 
            echo esc_url(admin_url('tools.php?page=wpmove-backup-manager'));
            ?>
">
					<?php 
            wp_nonce_field('wpmove_backup_manager_submit');
            ?>
					<input name="act" type="hidden" value="manage" />
					<div class="tablenav top">
						<div class="alignleft actions">
							<select name="action" size="1" height="1">
								<option value="delete"><?php 
            _e('Delete', 'WPMove');
            ?>
</option>
							</select>
							<?php 
            submit_button(__('Apply', 'WPMove'), 'secondary', 'wpmove_converted_backups', FALSE);
            ?>
						</div>
					</div>
					<table class="wp-list-table widefat fixed" cellspacing="0">
						<thead>
							<tr>
								<th scope="col" id="cb" class="manage-column column-cb check-column" style>
									<input type="checkbox">
								</th>
								<th scope="col" id="name" class="manage-column column-name" style>
									<a href="#"><?php 
            _e('Name', 'WPMove');
            ?>
</a>
								</th>
								<th scope="col" id="type" class="manage-column column-type" style>
									<a href="#"><?php 
            _e('Type', 'WPMove');
            ?>
</a>
								</th>
								<th scope="col" id="size" class="manage-column column-size" style>
									<a href="#"><?php 
            _e('Size', 'WPMove');
            ?>
</a>
								</th>
								<th scope="col" id="date" class="manage-column column-date" style>
									<a href="#"><?php 
            _e('Date Created', 'WPMove');
            ?>
</a>
								</th>
							</tr>
						</thead>
						<tfoot>
							<tr>
								<th scope="col" id="cb" class="manage-column column-cb check-column" style>
									<input type="checkbox">
								</th>
								<th scope="col" id="name" class="manage-column column-name" style>
									<a href="#"><?php 
            _e('Name', 'WPMove');
            ?>
</a>
								</th>
								<th scope="col" id="type" class="manage-column column-type" style>
									<a href="#"><?php 
            _e('Type', 'WPMove');
            ?>
</a>
								</th>
								<th scope="col" id="size" class="manage-column column-size" style>
									<a href="#"><?php 
            _e('Size', 'WPMove');
            ?>
</a>
								</th>
								<th scope="col" id="date" class="manage-column column-date" style>
									<a href="#"><?php 
            _e('Date Created', 'WPMove');
            ?>
</a>
								</th>
							</tr>
						</tfoot>
						<tbody id="the-list">
						<?php 
            // Display a message if no backup files found
            if (count($converted_backups) > 1) {
                // For zebra striping
                $i = 0;
                // Display all current backups starting with database backups
                foreach ($converted_backups as $file) {
                    // Get the file extension
                    $ext = substr($file, -3, 3);
                    // Decide the type of the backup
                    if ($ext == 'sql') {
                        preg_match('/DBBackup-([0-9]*).sql/', basename($file), $timestamp);
                        $type = __('Database Backup', 'WPMove');
                    } else {
                        continue;
                    }
                    // For zebra striping
                    if ($i % 2 !== 0) {
                        $class = ' class="alternate"';
                    } else {
                        $class = '';
                    }
                    // Display the row
                    echo '	<tr id="file-' . $i . '" valign="middle"' . $class . '>
												<th scope="row" class="check-column">
													<input id="file-' . $i . '" name="files[]" type="checkbox" value="' . $file . '">
												</th>
												<td class="column-name">
													<strong>
														<a href="' . esc_url(trailingslashit(WPMOVE_CONVERTED_BACKUP_URL) . basename($file)) . '">' . esc_html(basename($file)) . '</a>
													</strong>
												</td>
												<td class="column-type">
													<a href="#">' . esc_html($type) . '</a>
												</td>
												<td class="column-size">
													' . esc_html(round(filesize($file) / 1024, 2)) . ' KB
												</td>
												<td class="column-date">
													' . esc_html(date('d.m.Y H:i:s', substr($timestamp['1'], 0, 10))) . '
												</td>
											</tr>';
                    // Increase the counter for zebra striping
                    $i++;
                }
            } else {
                echo '<tr class="no-items">
									  	<td class="colspanchange" colspan="5">
									  		' . __('No converted database backup files found. You can convert a database backup file using the Convert option from the dropdown lists above.', 'WPMove') . '
									  	</td>
									  </tr>';
            }
            ?>
						</tbody>
					</table>
				</form>
			</div>
			<?php 
        }