<?

/*
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][583];
	$BrowserTitle = $MsgText[0][583];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="' . $MainURL . '/options.html">'.$MsgText[0][4].'</a> > '.$MsgText[0][583];
	
	if ($LoggedIn == 'y') {
		
		ReadPermissions();
		Global $Permissions;
		
		if ($Permissions[22] == 'y') {
		
			ReadFormArguments();
			Global $FormArguments;
			
			if (count($FormArguments) > 0) {
			
				if ($FormArguments['BackupItems-Database'] == 'y') {
				
					if ($ItemsList != '') {
						$ItemsList .= '-';
					}
					$ItemsList .= 'D';
										
					
					$query = "show table status";
					$result = mysql_query($query) or die (mysql_error());
					while ($row = mysql_fetch_array($result)) {
			
						$DBCode .= 'DROP TABLE IF EXISTS `{DBPrefix}'.$row['Name'].'`;'."\r\n".'CREATE TABLE `{DBPrefix}'.$row['Name'].'` (';
						
						
						$FieldNames = '';
						$FieldsPrint = '';
																	
												
						$query2 = "describe `".$row['Name']."`";
						$result2 = mysql_query($query2) or die (mysql_error());
						while ($row2 = mysql_fetch_array($result2)) {
						
							$FieldNames[] = $row2['Field'];
							
							if ($row2['Null'] == 'YES') {
								$NullNotNull = 'NULL';
							}
							else {
								$NullNotNull = 'NOT NULL';
							}
							
							$Default = '';
							if ($row2['Default'] != '') {
								$Default = ' default \''.addslashes($row2['Default']).'\'';
							}
							
							$ExtraInfo = '';
							if ($row2['Extra'] != '') {
								$ExtraInfo = ' '.$row2['Extra'];
							}
							
							
							$DBCode .= '`'.$row2['Field'].'` '.$row2['Type'].' '.$NullNotNull.$Default.$ExtraInfo.',';

							if ($row2['Key'] == 'PRI') {
								$PrimaryKey = $row2['Field'];
							}
							
						}
							
						$DBCode .= 'PRIMARY KEY (`'.$PrimaryKey.'`)) TYPE=MyISAM;'."\r\n";
						
						
						for ($x = 0; $x < count($FieldNames); $x++) {
							if ($FieldsPrint != '') {
								$FieldsPrint .= ', ';
							}
							$FieldsPrint .= '`'.addslashes($FieldNames[$x]).'`';
						}
						
						
						$ValuesPrint = '';
						
						$query3 = "select * from `".$row['Name']."`";
						$result3 = mysql_query($query3) or die (mysql_error());
						if (mysql_num_rows($result3) > 0) {
							while ($row3 = mysql_fetch_array($result3)) {
						
								if ($ValuesPrint != '') {
									$ValuesPrint .= ', ';
								}
							
								$ValuesPrint .= '(';
															
								for ($x = 0; $x < (count($row3)/2); $x++) {
									if ($x > 0) {
										$ValuesPrint .= ', ';
									}
									$ValuesPrint .= '\''.str_replace("\r",'\r',str_replace("\n",'\n',addslashes($row3[$x]))).'\'';
								}
								
									$ValuesPrint .= ')';
												
							}
						
													
							$DBCode .= 'INSERT INTO `{DBPrefix}'.$row['Name'].'` ('.$FieldsPrint.') VALUES '.$ValuesPrint.';'."\r\n";
							
						}
															
					}
					
						
					
					$FileContentsString .= $DBCode;
		
					if ($FilesString) {
						$FilesString .= '+++'.'./db_backup.sql'.'%%%'.strlen($DBCode);
					}
					else {
						$FilesString = './db_backup.sql'.'%%%'.strlen($DBCode);
					}
					
					
					
				}
				if ($FormArguments['BackupItems-Sources'] == 'y') {
				
					if ($ItemsList != '') {
						$ItemsList .= '-';
					}
					$ItemsList .= 'S';
					
					
					$dir = './';

					if (is_dir($dir)) {
						if ($dh[$dir] = opendir($dir)) {
							while (($file = readdir($dh[$dir])) !== false) {
								if ($file != '.' && $file != '..' && $file != 'error_log' && $file != 'settings.php' && $file != '.htaccess') {
									if (filetype($dir . $file) == 'dir') {
										//$DirectoriesArray[] = $dir.$file;
										//ReadDirectory($dir . $file.'/');
									}
									else {
										$FilesArray[] = $dir.$file;
										$FileSizesArray[] = filesize($dir.$file);
									}
								}
							}
							closedir($dh[$dir]);
						}										
					}

					
					for ($i = 0; $i < count($FilesArray); $i++) {
	
						$handle = fopen($FilesArray[$i], "rb");
						$FileContentsString .= fread($handle, $FileSizesArray[$i]);
						fclose($handle);
						
						//$FileContentsString .= file_get_contents($FilesArray[$i]);
						
							
						if ($FilesString) {
							$FilesString .= '+++'.$FilesArray[$i].'%%%'.$FileSizesArray[$i];
						}
						else {
							$FilesString = $FilesArray[$i].'%%%'.$FileSizesArray[$i];
						}

					}
					
					
					
					
				}
				if ($FormArguments['BackupItems-Images'] == 'y') {
				
					if ($ItemsList != '') {
						$ItemsList .= '-';
					}
					$ItemsList .= 'I';
					
					
					$FilesArray = '';
					$FileSizesArray = '';
					$DirectoriesArray = '';
					
					
					// recursive function to spider the directory tree
					function ReadDirectory1($dir) {
						
						Global $DirectoriesArray, $FilesArray, $FileSizesArray;

						if (is_dir($dir)) {
							if ($dh[$dir] = opendir($dir)) {
					   			while (($file = readdir($dh[$dir])) !== false) {
									if ($file != '' && $file != '.' && $file != '..' && $file != 'error_log' && $file != 'settings.php' && $file != '.htaccess') {
										if (filetype($dir . $file) == 'dir') {
											$DirectoriesArray[] = $dir.$file;
											ReadDirectory1($dir . $file.'/');
										}
										else {
											$FilesArray[] = $dir.$file;
											$FileSizesArray[] = filesize($dir.$file);
										}
									}
								}
								closedir($dh[$dir]);
							}
						}
					
					}
					
					// start the spider process
					ReadDirectory1('./images/');
					
					// start the spider process
					ReadDirectory1('./pg_images/');


					for ($i = 0; $i < count($DirectoriesArray); $i++) {
	
						if ($DirectoriesString) {
							$DirectoriesString .= '+++'.$DirectoriesArray[$i];
						}
						else {
							$DirectoriesString = $DirectoriesArray[$i];
						}
	
					}


					for ($i = 0; $i < count($FilesArray); $i++) {
	
						$handle = fopen($FilesArray[$i], "rb");
						$FileContentsString .= fread($handle, $FileSizesArray[$i]);
						fclose($handle);
	
						if ($FilesString) {
							$FilesString .= '+++'.$FilesArray[$i].'%%%'.$FileSizesArray[$i];
						}
						else {
							$FilesString = $FilesArray[$i].'%%%'.$FileSizesArray[$i];
						}

					}
					
					
				}
				
				
				$FinalFileString = GetTimestamp() . '+' . $ItemsList . '+' . $Settings['iv'] . '$%$%$%$'.'%$%$%$' . $DirectoriesString . '+%+%+'.'%+%+%' . $FilesString . '$%$%$%$'.'%$%$%$' . $FileContentsString;				
				
				$BackupFilename = 'backup_'.date('Y',GetTimestamp()).date('m',GetTimestamp()).date('d',GetTimestamp()).'_'.rand('1000','9999').'.backup';
				
				$handle = fopen('./backups/'.$BackupFilename, 'wb');
				fwrite($handle, $FinalFileString);
				fclose($handle);
				
				
				if ($Settings['FilesCHMOD'] != '') {
					@chmod('./backups/'.$BackupFilename, $Settings['FilesCHMOD']);
				}
				
				
				$PageContents = '<br><b>'.$MsgText[0][592].'<a href="'.$MainURL.'/admin/backup-download/'.$BackupFilename.'" target="_blank">'.$MsgText[0][593].'</a><br><br><br><a href="'.$MainURL.'/options.html">'.$MsgText[0][594].'</a></b></b>';
				
				
				
			}
			else {
				$PageContents = $MsgText[0][98];
			}
			
		
		}
		else {
		
			$PageContents = $MsgText[0][97];
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}



?>
