<?

/*
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') {
		
			ReadFormArguments();
			Global $FormArguments;
			
			if (count($FormArguments) > 0) {
							
				$FileHandle = fopen('./backups/'.$FormArguments['BackupFile'], "rb");
				$FileContents = fread($FileHandle, filesize('./backups/'.$FormArguments['BackupFile']));
				fclose($FileHandle);
				
				list ($BackupFileData, $BackupFileContents) = explode('$%$%$%$%$%$%$', $FileContents, 2);
									
				list ($DateCreated, $ItemsList, $FlexVersion) = explode('+', $BackupFileData, 3);
				
				
				list ($FileMap, $FileData) = explode('$%$%$%$%$%$%$', $BackupFileContents, 2);
				
				
				list($DirectoriesString,$FilesString) = explode('+%+%+%+%+%',$FileMap,2);
	
				$DirectoriesArray = explode('+++',$DirectoriesString);
				$FilesArray = explode('+++',$FilesString);
	
				for ($i = 0; $i < count($DirectoriesArray); $i++) {
		
					@mkdir($DirectoriesArray[$i], 0777);
					
					if ($Settings['FoldersCHMOD'] != '') {
						@chmod($DirectoriesArray[$i], $Settings['FoldersCHMOD']);
					}					
			
				}
				
				
				
				$Offset = 0;
				for ($i = 0; $i < count($FilesArray); $i++) {
	
					list ($FileName,$FileSize) = explode('%%%',$FilesArray[$i],2);
		
					$handle = fopen($FileName, 'wb');
					fwrite($handle, substr($FileData,$Offset,$FileSize));
					fclose($handle);
					
					if ($Settings['FilesCHMOD'] != '') {
						@chmod($FileName, $Settings['FilesCHMOD']);
					}
														
			
					$Offset += $FileSize;
		
				}
				
				
					
				if (file_exists('./db_backup.sql')) {
				
					/*
					$query = "show table status";
					$result = mysql_query($query) or die (mysql_error());
					while ($row = mysql_fetch_array($result)) {
					
						$query2 = "drop table `".$row['Name']."`";
						$result2 = mysql_query($query2) or die (mysql_error());
					
					}
					*/
				
				
					$handle = fopen('./db_backup.sql', "rb");
					$FileContentsString = fread($handle, filesize('./db_backup.sql'));
					fclose($handle);
		
					
					$QueryCounter = 0;
					$FileLines = split("\n", $FileContentsString);
					for ($i = 0; $i < count($FileLines); $i++) {
					
						if (substr($FileLines[$i],0,1) != '#' && $FileLines[$i] != '') {
							$QueryLines[$QueryCounter] .= str_replace('{DBPrefix}',$DBPrefix,$FileLines[$i]);
							
							if (substr($FileLines[$i],strlen($FileLines[$i])-2,1) == ';') {
								//print $QueryLines[$QueryCounter].'<br><br>';
								$QueryCounter++;
							}
						}
					
					}
		
					for ($i = 0; $i < count($QueryLines); $i++) {
			
					
						if ($QueryLines[$i] != '' && strlen($QueryLines[$i]) > 10) {
							
							if (ord(substr($QueryLines[$i],strlen($QueryLines[$i])-1,1)) == 13) {
								$QueryLines[$i] = substr($QueryLines[$i],0,strlen($QueryLines[$i])-1);
							}
						
							$query = $QueryLines[$i];

							//print '<br><br><br><br>'.$query.'<br><br><br><br>';
							$result = mysql_query($query) or die (mysql_error());
						}
		
					}
					
					
					@unlink('./db_backup.sql');
				
				}
				
				
				
				$PageContents = '<br><br><b>'.$MsgText[0][607].'<br><br><a href="'.$MainURL.'/options.html">'.$MsgText[0][594].'</a></b>';
				
				
			
			}
			else {
				$PageContents = $MsgText[0][98];
			}
			
		
		}
		else {
		
			$PageContents = $MsgText[0][97];
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}



?>
