<?php
if(!isset($_SESSION)){
session_start();
}

if (isset($_POST['type']) || isset($_GET['type'])){
		require("db_connect.php");




		if ($_POST['type'] == "list") {
      			
      			date_default_timezone_set('Asia/Baghdad');
				$datess = date("Y-m-d_h-i-s", time());


      			$op  = array();
                $op1  = array();
                
      			$dir = '../backup/';
      			$lastMod = 0;
				if ($handle = opendir('../backup')) {

				    while (false !== ($entry = readdir($handle))) {

				        if ($entry != "." && $entry != ".." && substr($entry,0,4) == "back"  ) {

				            $lastMod = filectime($dir.$entry);
				            $lastMod1 = filemtime($dir.$entry);
				            
				            
				            array_push($op,$entry);
                            array_push($op1,$lastMod1);
				        }
				    }

				    closedir($handle);
				}
				
				$c = array_combine($op, $op1);
				// print_r($c);
                arsort($c);
                // print_r($c);
				echo json_encode($c);
				// print_r($ss);
				exit;
			

		}



		if ($_POST['type'] == "delete") {
      			
      			date_default_timezone_set('Asia/Baghdad');
				$datess = date("Y-m-d_h-i-s", time());


      			$op  = array('is_success'=>false, 'data'=>'');
               	
               	$file_name = $_POST['file_name'];
                
      			$dir = '../backup/';
      			
      			$file = $dir.$file_name;
				
				if (file_exists($file)) {

					if (!unlink($file)) 
					{ 
						$op['is_success'] = false;
						$op['data'] 	  = "$file_name cannot be deleted due to an error";
						echo json_encode($op);

					  
					} 
					else
					{ 
						$op['is_success'] = true;
						$op['data'] 	  = "$file_name has been deleted Successfully";
						echo json_encode($op);

					} 

				   
				}else{
					$op['is_success'] = false;
					$op['data'] 	  = "$file_name Not Exsist";
					echo json_encode($op);
				}
				
			
				exit;
			

		}
	}
	else{
		header("Location:../");
		exit;
	}
?>
