<?

/*
FlexCMS (http://www.flexcms.com)
Flexible, Powerful, Affordable.

Contents of this file are protected by the copyright
notice found at http://www.flexcms.com/flex/pages/copyright.html,
and its usage is subject to the terms of the licensing agreement 
available at http://www.flexcms.com/flex/pages/license.html
*/


	
	// ========================================================================================
	
	$PageTitle = '<img src="'.$ImagesURL.'/admin/admincp_backup.gif" width="20" height="21" align="middle"> '.$MsgText[0][584];
	$BrowserTitle = $MsgText[0][584];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="' . $MainURL . '/options.html">'.$MsgText[0][4].'</a> > '.$MsgText[0][584];
	
	if ($LoggedIn == 'y') {
		
		ReadPermissions();
		Global $Permissions;
		
		if ($Permissions[23] == 'y') {
		
			
			$PageContents = '<br><b>'.$MsgText[0][597].'</b><form name="SubmitForm" action="'.$MainURL.'/admin/backup-restore-confirm" method="post">';


			$ItemCodes['D'] = $MsgText[0][586];
			$ItemCodes['S'] = $MsgText[0][588];
			$ItemCodes['I'] = $MsgText[0][590];
			

			$dir = './backups/';
			
			if (is_dir($dir)) {
				if ($dh[$dir] = opendir($dir)) {
		   			while (($file = readdir($dh[$dir])) !== false) {
						if ($file != '.' && $file != '..') {
							if (filetype($dir . $file) == 'file') {
								
								if (substr($file,(strlen($file)-8)) == '.backup') {
									
									$FileHandle = fopen($dir.$file, "rb");
									$FileContents = fread($FileHandle, 500);
									fclose($FileHandle);
									
									list ($BackupFileData, $Trash) = explode('$%$%$%$%$%$%$', $FileContents, 2);
									
									list ($DateCreated, $ItemsList, $FlexVersion) = explode('+', $BackupFileData, 3);
																
									
									//if ($FlexVersion == $Settings['iv']) {
									
									$BackupFiles['DateCode'][] = $DateCreated;
									$BackupFiles['ItemsList'][] = $ItemsList;
									$BackupFiles['Filename'][] = $file;
									$BackupFiles['Version'][] = $FlexVersion;
									
									//}
																		
								} 
															
							}
						}
					}
					closedir($dh[$dir]);
				}
			}
			
			
			for ($x = 0; $x < count($BackupFiles['DateCode']); $x++) {
				for ($y = 0; $y < (count($BackupFiles['DateCode'])-1); $y++) {
				
					if ($BackupFiles['DateCode'][$y] < $BackupFiles['DateCode'][$y+1]) {
					
						$SwapA = $BackupFiles['DateCode'][$y];
						$SwapB = $BackupFiles['ItemsList'][$y];
						$SwapC = $BackupFiles['Filename'][$y];
						$SwapD = $BackupFiles['Version'][$y];
						
						$BackupFiles['DateCode'][$y] = $BackupFiles['DateCode'][$y+1];
						$BackupFiles['ItemsList'][$y] = $BackupFiles['ItemsList'][$y+1];
						$BackupFiles['Filename'][$y] = $BackupFiles['Filename'][$y+1];
						$BackupFiles['Version'][$y] = $BackupFiles['Version'][$y+1];
						
						$BackupFiles['DateCode'][$y+1] = $SwapA;
						$BackupFiles['ItemsList'][$y+1] = $SwapB;
						$BackupFiles['Filename'][$y+1] = $SwapC;
						$BackupFiles['Version'][$y+1] = $SwapD;
					
					}
				
				}
			}
			
			
			
			for ($x = 0; $x < count($BackupFiles['DateCode']); $x++) {
				
				$BackupFilesFound = true;
			
				if ($BackupFiles['Version'][$x] != $Settings['iv']) {
					$BackupFiles['Version'][$x] = '<font color="#ff0000">'.$BackupFiles['Version'][$x].'</font>';
				}
			
									
				$ItemsListPrint = '';
				$ItemsListSplit = explode('-', $BackupFiles['ItemsList'][$x]);
				for ($z = 0; $z < count($ItemsListSplit); $z++) {
					if ($ItemsListPrint != '') {
						$ItemsListPrint .= ', ';
					}
					$ItemsListPrint .= $ItemCodes[$ItemsListSplit[$z]];
				}
									
				$Checked = '';
				if ($FirstOneFound != true) {
					$Checked = ' checked';
					$FirstOneFound = true;
				}
				
				$PageContents .= '<br><input type="radio" name="BackupFile" value="'.$BackupFiles['Filename'][$x].'"'.$Checked.'> <b>'.date('M j, Y @ g:ia',$BackupFiles['DateCode'][$x]).' (v'.$BackupFiles['Version'][$x].')</b> - '.$MsgText[0][598].': '.$ItemsListPrint.'<br>';
			}
			
			if ($BackupFilesFound == true) {
			
				$PageContents .= '<br><br><br><b><font color="#ff0000">'.$MsgText[0][599].'</font></b><br><br><br><input type="submit" value="'.$MsgText[0][584].'" style="'.$FormStyle.'"></form>';
				
			}
			else {
			
				$PageContents .= '<br>'.$MsgText[0][600];
			
			}
			
		
		}
		else {
		
			$PageContents = $MsgText[0][97];
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}



?>
