<?php 
/*
 *
 * sflog!
 * Copyright (C) 2005 Alberto Sfolcini <a.sfolcini@gmail.com>
 *
 * This file is a part of sflog!.
 *
 * sflog! is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * sflog! is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */
  
  error_reporting(E_USER_ERROR);
  
  require_once(dirname(__FILE__)."/../../cms/Validator.class.php");
  require_once(dirname(__FILE__)."/../../cms/Auth.class.php");
  require_once(dirname(__FILE__)."/../../cms/Errors.inc.php");
  require_once(dirname(__FILE__)."/../../cms/Constants.inc.php");
  require_once(dirname(__FILE__)."/../../common/File.class.php");
  require_once(dirname(__FILE__)."/../../common/utils.func.php");
  
  
  // Check for authorizations
  $auth = new Auth();
  if (!$auth->isAuth()){
  	header("Location: login.php");
  	die();
  }
  // ------ end auth's check
  	  
  unset($backupID);

  /**
   * Delete action
   */
  if (isset($_GET['delete'])){
  	$id = $_GET['delete'];
  	$fn = dirname(__FILE__).'/../..'.BACKUP_DIR.$id;
  	if (file_exists($fn)){
  		/*
  		 * This forces to run under unix/linux enviroment, this is not a big deal,
  		 * but rm_dir() does not work here and I have no time to check it out.
  		 */
  		shell_exec('rm -f '.$fn);
  	}
  }

  /**
   * Restore action
   */
  if (isset($_GET['restore'])){
  	$id = $_GET['restore'];
  	$fn = dirname(__FILE__).'/../..'.BACKUP_DIR.$id;
  	//$fn = '.'.BACKUP_DIR.$id;
  	if (file_exists($fn)){
		$command = './restoreBackup '.$fn;
		$output = shell_exec($command);
		print('RESTORED with tarball '.$id);			  
  	}
  }


  if (isset($_POST['backupID'])){
  	if (!($_POST['backupID']=='')){
	  	$backupID = $_POST['backupID']; 	  	
		 /**
		  * Create Backup tarball with date of today
	  	  */
			$command = './doBackup '.'..'.BACKUP_DIR.date('YmdGis');
			$output = shell_exec($command);			  
  	}
  }
  
  error_reporting(E_USER_ERROR);
  $fileID = '';
  if (trim($_FILES['fileID']['name'])!=''){	
  	$uploaddir = dirname(__FILE__).'/../..'.BACKUP_DIR;
  	$uploadfile = $uploaddir . basename($_FILES['fileID']['name']);
  	if (move_uploaded_file($_FILES['fileID']['tmp_name'], $uploadfile)) {
     	// ok
  	} else {
    	print('<font color="red">'.UPLOAD_BACKUP_FAILED.'</font>');
  	}
  }
  
?>	
<table border="0">
 <tr>
   <td>	
		<form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
			<input type="hidden" name="backupID" value="<?=date("Ymd");?>">
			<table cellspacing="0" cellpaddgin="0" border="0">
				<tr>
					<td>
						<input type="SUBMIT" value="make blogs backup" size="1">
					</td>
				</tr>
			</table>
		</form>
   </td>
   <td>
		<form name="external" action="<?=$_SERVER['PHP_SELF']?>" method="POST" enctype="multipart/form-data">
			<input type="hidden" name="MAX_FILE_SIZE" value="0">
			&nbsp;|&nbsp;backup file&nbsp;<input type="file" name="fileID">
			<input type="submit" value="upload">
		</form>
   </td>
 </tr>
</table>
<?php
  
  
?>
<table width="99%" cellpadding="4" cellspacing="1" border="0" background="grey">
	<tr bgcolor="#C7D3E0">
		<td width="1%">&nbsp;</td>
		<td width="20%"><b>tarrball file<b></td>
		<td width="1%"><b>dl</b></td>
		<td width="30%"><b>backup date<b></td>
		<td width="10%"><b>size<b></td>
		<td width="10%"><b>restore?<b></td>
	</tr>
<?php
	$file = new File();
	$dir = dirname(__FILE__).'/../..'.BACKUP_DIR;
	$files=$file->scandir($dir,1);
	foreach($files as $key=>$val) {
		if ($val[0]!=".") {
			$id = $val;
			$text = date("F d Y H:i:s", filectime($dir.$id));
			$size = filesize($dir.$id);	
?>
	<tr bgcolor="white">
		<td><a href="<?=$_SERVER['PHP_SELF']."?delete=".$id?>"><img src="./images/delete.png" width="16px" height="16px" border="0" alt="delete"></a></td>
		<td><?=$id?></td>
		<td><a href="<?='..'.BACKUP_DIR.$id?>"><img src="./images/save.png" alt="download" border="0" width="16" height="16"></a></td>
		<td><?=$text?></td>
		<td><?=$size.' Bytes'?></td>
		<td><a onClick="javascript:if (confirm('<?=BACKUP_RESTORE_QUESTION?>')) location.href='<?=$_SERVER['PHP_SELF']."?restore=".$id?>'">restore</a></td>
	</tr>
<?php
	}} // end for
?>
	<tr bgcolor="white">
		<td colspan="6"><hr size="-1"></td>
	</tr>
</table>
