<?php
@session_start();
set_time_limit (3600);
error_reporting(E_ALL ^ E_NOTICE);

if(trim($_SESSION['accountName'])=="" &&  trim($_SESSION['accountID'])=="" && trim($_SESSION['pgid'])!="i3g3e3nious7")
{
	echo '<script>';
	echo 'location.replace("index.php");';
	echo '</script>';
}
else
{
	include("dbconnect.php");
	include("header.php");


		
?>

<table width="100%" cellpadding="0" cellspacing="0" border="0">
	<!-- <tr>
		<td></td>
		<td align="center" class="fs14 black"><u><b>Data</b></u></td>
	</tr> -->
	<tr>
		<td width="200" class="fs12" valign="top">
		<?php
		include("sidemenu.php");
		?>
		</td>
		<td width="700" class="fs12" valign="top">
			<?php
					date_default_timezone_set('UTC');
					backup_tables();
			?>
		</td>
	</tr>
</table>
<?php
include("footer.php");
}
			/* backup the db OR just a table */
			function backup_tables($tables = '*')
			{

			//get all of the tables
			if($tables == '*')
			{
				$tables = array();
			//	$result=$dbh->exec('SHOW TABLES');
				$result = mysql_query('SHOW TABLES');
				while($row = mysql_fetch_row($result))
				//while($row=$result->fetch())
				{
					$tables[] = $row[0];
				}
			}
			else
			{
				$tables = is_array($tables) ? $tables : explode(',',$tables);
			}

			//cycle through
			foreach($tables as $table)
			{
			$result = mysql_query('SELECT * FROM '.$table);

			//$result = $dbh->prepare('SELECT * FROM '.$table);
			
			//$result->execute();
		
			//	$num_fields=
			$num_fields = mysql_num_fields($result);

			$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));

			$return.= "\n\n".$row2[1].";\n\n";

			for ($i = 0; $i < $num_fields; $i++)
			{
				//while($row = $result->fetch())
				while($row = @mysql_fetch_array($result))
				{
					$return.= 'INSERT INTO '.$table.' VALUES(';
					for($j=0; $j<$num_fields; $j++)
					{
						$row[$j] = addslashes($row[$j]);
						$row[$j] = @ereg_replace("\n","\\n",$row[$j]);
						if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
						if ($j<($num_fields-1)) { $return.= ','; }
					}
					$return.= ");\n";
					}
				}
			$return.="\n\n\n";
			}

			//save file
			$handle = fopen('backup/db-backup-'.date("Y-m-d g i a", time()+19800).'.sql','w+');

			// echo '<img src ="image.gif" >';



			fwrite($handle,$return);


			echo "<br /><br /><br /><br /><br /><br /><br /><br /><br /><h1> Backup Data Successfully Completed</h1>";


			fclose($handle);
		}

?>
			