<?php 
session_start();
 
if($_SESSION['CheckPWD']==false){header("Location: login.php");}

$myTopCaption_Text="FTP SERVER";
$MyFormName=str_replace('.php', '', basename($_SERVER['PHP_SELF']));
$MyADDFormName="ftp_add";
$tblName="apps";
$MyIDFieldName="IDApp";
$ListExist=true;
$prefix=$MyFormName."@";
$pFind=$prefix."FindWhat";
$frmOpenNew='"'.$MyIDFieldName.'","0","'.$MyADDFormName.'",0,0' ;

$frmOpen="";
$num_rows=1000;

$RecNo=0;   
$FromRec=1 ;
$pgMax=1000;

include 'include/top_vars.php';  
include 'include/header.php';
 
  // time()+3600 = 1 Hour
setcookie("FTP_SERVER_FOLDER", $_SESSION['FTP_SERVER_FOLDER'], time()+3600);
$_SESSION['FTP_FOLDER']="FTP_SERVER_FOLDER";
 
# ==============================
#$dir = "/";

#// Open a directory, and read its contents
#if (is_dir($dir)){
#  if ($dh = opendir($dir)){
#    while (($file = readdir($dh)) !== false){
#      echo "filename:" . $file . "<br>";
#    }
#   closedir($dh);
# }
#}
# ==============================	

?>

 
<head>
 <link href="logos/LT.png" rel="shortcut icon" type="image/x-icon"> 
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<meta name="description" content="">
</head>

<link rel="stylesheet" href="css/tab2023.css" />  

<form id="<?php echo $MyFormName;?>" name="<?php echo $MyFormName;?>" autocomplete="off">
<body OnLoad="document.<?php echo $MyFormName.'.'.$prefix ?>FindWhat.focus();">

<div class="vsboxDetail hide1">
	<h1> ΚΑΤΑΣΤΑΣΗ ΑΡΧΕΙΩΝ </h1>    
</div>	

<?php # include 'include/top_grid_list_buttons.php';  ?>
 
 <a hidden id="download_file" href="" download> </a>
 
<div class="scroll" style="margin-bottom:0px;padding-bottom:0px;width:100%;" > 
<div class="sticky">
  <table class="gridList"  > 
    <tr>
		<td class="c w50">A/A<a onclick="grdsrt('IDApp');" class="sortpic"/></td>
		<td>ΟΝΟΜΑ<a onclick="grdsrt('Perigrafi');" class="sortpic"/></td>
		<td class="l w120 hide1" > ΗΜΕΡΟΜΗΝΙΑ <a onclick="grdsrt('FtpServer');" class="sortpic"/></td>
		<td class="l w120 hide1"> ΜΕΓΕΘΟΣ </td>
		<td class="l w120"> ΕΝΕΡΓΕΙΑ </td>
	</tr> 
  </table>
</div>
 
 <table class="gridListNoCaption"  > 
 
<?php

// echo "****************** > ".$_SERVER['DOCUMENT_ROOT'];

$ftp_server = "visualsoft.gr";
$ftp_user = "customers_user";
$ftp_pass = "P@ssw0rdFtp!";

//$ftp_server = "127.0.0.1";
//$ftp_user = "customers_user";
//$ftp_pass = "P@ssw0rdFtp!";
$depth = 10;
	
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); 
 
    if ( ftp_login($conn_id, $ftp_user, $ftp_pass)) 
    {
      #  echo "Connected as $ftp_user@$ftp_server\n";
    } 
    else 
    {
      #  echo "Couldn't connect as $ftp_user@$ftp_server\n";
    }
# =====================================	
ftp_set_option($conn_id, FTP_USEPASVADDRESS, false); // set ftp option
ftp_pasv($conn_id, true); //make connection to passive mode
# =====================================  
 # if(is_numeric($pgNo)){	 } else { $pgNo=1;}

//$file_list = ftp_nlist($conn_id, ".");
//var_dump($file_list);
 
# =====================================  
//dirname(__FILE__)
//echo dirname(__FILE__);
//echo ftp_pwd($conn_id); 
 
?>	 
	 
 <tr class='tablerowdata' style='background:#F8FAB5;' >
	<td class='r w50' ></td>
	<td class='l b' style='color:red;font-size:1.2em;'>/..</td>
	<td class='c w120 hide1'></td>
	<td class='r w120 hide1'></td>
	<td class="c w120" >
	
<!--
	<a class="icon icon-plus-sign fa fa-database" style="display:inline-block;font-size:1.3em;" title="Δημιουργία Φακέλου." onclick="javascript:downloadfile('..','mkdir');"> </a> 
	<a class="icon icon-folder-open-alt" style="display:inline-block;margin:0 5px 0 5px;font-size:1.3em;" title="Upload File στον Φάκελο αυτό." onclick="javascript: select_files('\','Upload');"> </a> 

<a class='icon icon-plus-sign' href="javascript:upload_divHide();"></a> 
<a style="width:100%;" class='button folderOpen_only' href="javascript:upload_divHide();"></a> 
-->
	<a class="icon icon-plus-sign" style="display:inline-block;" title="Δημιουργία Φακέλου." onclick="javascript:downloadfile('..','mkdir');"></a> 
	<a class="icon icon-folder-open-alt" style="display:inline-block;margin:0 5px 0 5px;" title="Upload File στον Φάκελο αυτό." onclick="javascript: select_files('\','Upload');"> </a> 

	</td>						
 </tr> 
									
<?php
$iRow=0;
 
$Typos="";
$Hmerominia="";
$Size1="";
$folder="/";
$endColorF="green";		  
  $contents = ftp_nlist($conn_id, '/');
 
     foreach($contents as $key=>$dat) {
  $iRow+=1;
		  $Typos="";
		  $Hmerominia="";
		  $Size1="";
			if(ftp_size($conn_id, $key) == '-1'){
						$Typos="DIR";	
						$Typos=is_dir($dat);
						$folder=$dat;
						$folder = str_replace('/', '', $dat);
									echo "<tr class='tablerowdata' style='background:#F8FAB5;' >";
									echo "<td class='r w50' >$iRow</td>";
									echo "<td class='l b' style='color:red;font-size:1.2em;'>$dat</td>";
									echo "<td class='c w120 hide1'>$Hmerominia</td>";
									echo "<td class='r w120 hide1'>$Size1</td>";
									?>
									<td class="c w120" >
										<a class="icon icon-plus-sign" style="display:inline-block;font-size:1.3em;" title="Δημιουργία Φακέλου." onclick="javascript:downloadfile('..','mkdir');"> </a> 
										<a class="icon icon-folder-open-alt" style="display:inline-block;margin:0 5px 0 5px;font-size:1.3em;" title="Upload Files στον Φάκελο αυτό." onclick="javascript:select_files('<?php echo $folder ?>','Upload');"> </a> 
									    <a class="icon icon-trash" style="display: inline-block;font-size:1.3em;" title="Διαγραφή Φακέλου..."   onclick="javascript:downloadfile('<?php echo $dat ?>','rmdir')"> </a> 
									</td>
									<?php
										
								    echo "</tr>";
					$newdir = ftp_nlist($conn_id, $dat);
				
						foreach($newdir as $key=>$dat) {
									$iRow+=1;
									$Typos="";
									$size =ftp_size($conn_id, $dat);
									$mdate = ftp_mdtm($conn_id, $dat);
									$Hmerominia=date("d/m/Y", $mdate);
 
									$s1=round(($size/1024),2); 
									if($s1<1024) {
											$Size1=$s1 . " KB";
										}else{
											$Size1 = round($s1/1024,2);
											$Size1= $Size1 . " MB" ;
										}
							
									echo "<tr class='tablerowdata'> ";
									echo "<td class='r w50' >$iRow</td>";
									echo "<td class='l'>$dat</td>";
									echo "<td class='c w120 hide1'>$Hmerominia</td>";
									echo "<td class='r w120 hide1' style='padding-right:10px;'>$Size1</td>";
									?>
									<td class='c w120' style="display: block;padding:5px;">
										<a class="icon icon-circle-arrow-down"  style="display: inline-block;font-size:1.3em;" title="Download File" onclick="javascript:downloadfile('<?php echo $dat ?>','down')"> </a> 
										<a class="icon icon-folder-open-alt" style="display:inline-block;margin:0 5px 0 5px;font-size:1.3em;" title="Upload File στον Φάκελο αυτό." onclick="javascript:select_files('<?php echo $folder ?>','Upload');"> </a> 
										<a class="icon icon-trash" style="display: inline-block;font-size:1.3em;" title="Delete File"   onclick="javascript:downloadfile('<?php echo $dat ?>','delete')"> </a> 
									</td>
									<?php
									
									echo "</tr>";
						#echo $key."=>".$dat."<br>" . "\r\n";
						}	   
				}else{
									echo "<tr class='tablerowdata'> ";
									echo "<td class='r w50' >$iRow</td>";
									echo "<td class='l'>$dat</td>";
									echo "<td class='c w120 hide1'>$Hmerominia</td>";
									echo "<td class='r w120 hide1'>$Size</td>";
									echo "<td class='r w120'> </td>";
									echo "</tr>";
					}
				echo "</tr>";
       }

#$buff = ftp_rawlist($conn_id, '/');
#echo "<br>"  ;
 
# =======================
 
# =======================
 ftp_close($conn_id);
 
// ========================================
//  Pelates List
// ========================================
 
 for ($x = $iRow+1; $x <= $pgNo*$RecsPP; $x++) {
	echo "<tr class='tablerowdata'> ";
	echo "<td class='r w50' ></td>";
	echo "<td class='l'></td>";
	echo "<td class='c w120 hide1'></td>";
	echo "<td class='r w120 hide1'></td>";
	echo "<td class='r w120'> </td>";
	echo "</tr>";
 }	
 ?> 
</table>
<?php include 'include/recs_div_bottom.php'; ?>
</div>
 
<?php include 'include/recs_no_max_div.php'; ?>	
</body>
</form> 

<div id="waiting" class="loader"  ></div> 
<div id="waiting_text" hidden class="loader_button" > 	Παρακαλώ περιμένετε...</div>
<form id="formforupload" method="post" enctype="multipart/form-data" hidden > </form> 
<script type="text/javascript" src="js/mycheck.js"></script>  
<?php 
  include 'include/footer2020.php';
?>

<script language="JavaScript" type="text/JavaScript">
 
function select_files(val,f) {
	var myform = document.getElementById("formforupload");
	myform.action="upload2/index.php?myF=" + val + "&f="+f;
	myform.submit();
	}
	
function downloadfile(e,command){
 
 
	
// var myform = document.getElementById("<?php echo $MyFormName;?>");
// myform.action="_ftp_get.php?file=" + e ;
// myform.submit();
//window.open("_ftp_get.php?file=" + e+"&c="+command );
//return;

if(command=="down") {
	   var myBtn = document.getElementById("download_file");
			 myBtn.href="/ftp"+e; 
				myBtn.click(); 
			  return ;
				}
	
 	
if(command=="delete") {
				var msg= "To αρχείo "+e+", πρόκειται να διαγραφεί από τον Server. Είσαι σίγουρος;"
				if (confirm(msg) ) {} else {return;	}
				}
 
e=e.replaceAll("/", "%2F");
	
 //  waiting.style.display = "initial";
//  waitingbuttondiv.style.display = "initial"

	  alert ("_dl_file.php?file="+e+"&c="+command);

	



	 var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
				//waiting.style.display = "none";
				//waiting_text.style.display = 'none';	 
				  alert (this.responseText);
			 if(command=="delete" || command=="mkdir"   ){ location.reload();}
		   }
        };
        xmlhttp.open("GET", "_dl_file.php?file="+e+"&c="+command, true);
        xmlhttp.send();  	
}	
</script>
