<?php
/*=======================================================================
 *
 * ftp.inc.php
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/

include_once ("../include/mcglobal.inc.php");


// ***********************
// *** Basic Functions ***
// ***********************

// Get FTP connection handle (OLD VERSION, STILL USED)
// Returns handle or empty string if no connection established
function ftpGetConnection ($ftp_server = FTP_SERVER, $ftp_user_name = FTP_USER, $ftp_user_pass = FTP_PASSWORD, $ftp_ssl = "0") {
  // Get base connection
  if ($ftp_ssl == "1") :
     $connectionId = ftp_ssl_connect("$ftp_server");
  else :
     $connectionId = ftp_connect("$ftp_server");
  endif;
  // Login with user account and password
  if ($connectionId) :
     $login_result = ftp_login($connectionId, "$ftp_user_name", "$ftp_user_pass"); 
     // Check if connection is established
     if ((!$connectionId) || (!$login_result)) :
        $connectionId = "";
     endif;
  else :
     $connectionId = "";
  endif;
  return $connectionId;
}

// Get FTP connection handle
// Returns handle or empty string if no connection established
function ftpConnect ($ftp_server, $ftp_user_name, $ftp_user_pass, $ftp_ssl = "0") {
  // Get base connection
  if ($ftp_ssl == "1") :
     $connectionId = ftp_ssl_connect("$ftp_server");
  else :
     $connectionId = ftp_connect("$ftp_server");
  endif;
  // Login with user account and password
  $login_result = ftp_login($connectionId, $ftp_user_name, $ftp_user_pass); 
  // Check if connection is established
  if ((!$connectionId) || (!$login_result)) :
     $connectionId = "";
  endif;
  return $connectionId;
}

// Quit FTP connection
function ftpQuitConnection ($connectionId) {
   ftp_quit($connectionId);
}

// FTP upload for a single specified file
// Returns boolean
function ftpUploadSingleFile ($connectionId, $source_file, $destination_file) {
  $upload = FALSE;
  if ($connectionId != "") :
     $upload = ftp_put($connectionId, $destination_file, $source_file, FTP_BINARY);
  endif;
  return $upload;
}

// FTP upload for a single specified file
// Returns boolean
function ftpDownloadSingleFile ($connectionId, $source_file, $destination_file) {
  $download = FALSE;
  if ($connectionId != "") :
     $download = ftp_get($connectionId, $source_file, $destination_file, FTP_BINARY);    
  endif;
  return $download;
}

// FTP delete a single specified file
// Returns boolean
function ftpDeleteSingleFile ($connectionId, $destination_file) {
  $delete = FALSE;
  if ($connectionId != "") :
     $delete = ftp_delete($connectionId, $destination_file);
  endif;
  return $delete;
}

// Gets a list of files of the specified directory
// Returns array or value FALSE
function ftpGetDirectoryContent ($connectionId, $directory) {
  $retValue = FALSE;
  if ($connectionId != "") :
     $retValue = ftp_nlist($connectionId, $directory);
  endif;
  return $retValue;
}


// ************
// *** SFTP ***
// ************

// Get SSH connection handle
function sshConnect ($ftp_server, $ftp_user_name, $ftp_user_pass) {
  $sshConnectionId = ssh2_connect($ftp_server, 22);
  if (!ssh2_auth_password($sshConnectionId, $ftp_user_name, $ftp_user_pass)) :
     $sshConnectionId = "";
  endif;
  return $sshConnectionId;
}

// Get SFTP connection handle
function sftpConnect ($ftp_server, $ftp_user_name, $ftp_user_pass) {
  $sshConnectionId = sshConnect($ftp_server, $ftp_user_name, $ftp_user_pass);
  $connectionId = "";
  if ($sshConnectionId != "") :
     $connectionId = ssh2_sftp($sshConnectionId);
  endif;
  return $connectionId;
}

// Quit SFTP connection
function sftpQuitConnection ($connectionId) {
   ssh2_exec($connectionId, 'exit');
}

// SFTP upload for a single specified file
// Returns boolean
function sftpUploadSingleFile ($connectionId, $source_file, $destination_file, $createMode = "") {
  $upload = false;
  if ($connectionId != "") :
     // $upload = ssh2_scp_send($connectionId, $destination_file, $source_file, $createMode);
     $sftpStream = @fopen('ssh2.sftp://'.$connectionId.$destination_file, 'wb');
     $localStream = @fopen($source_file, "rb");
     if ($sftpStream) :
         // $content = @file_get_contents($source_file);
         // $content = @fread($localStream, 4000000);
         $content = @fread($localStream, filesize($source_file));
         if (!($content === false)) :
            if (!(@fwrite($sftpStream, $content) === false)) {
                $upload = true;
            }
         endif;
     endif;
     @fclose($sftpStream);
     @fclose($localStream);
  endif;
  return $upload;
}

// SFTP upload for a single specified file
function sftpDownloadSingleFile ($connectionId, $source_file, $destination_file) {
  $download = false;
  if ($connectionId != "") :
     // $download = ssh2_scp_recv($connectionId, $destination_file, $source_file);
     $sftpStream = @fopen('ssh2.sftp://'.$connectionId.$destination_file, 'rb');
     $localStream = @fopen($source_file, "wb");
     if ($sftpStream) :
        $sftpFileSize = filesize('ssh2.sftp://'.$connectionId.$destination_file);
        /*
        $content = @fread($sftpStream, $sftpFileSize);
        if (!($content === false)) :
            if (!(@fwrite($localStream, $content) === false)) {
                $download = true;
            }
        endif;
        */
        $read = 0;
        $download = true;
        while($read < $sftpFileSize && ($buffer = fread($sftpStream, $sftpFileSize - $read))) {
               $read += strlen($buffer);
               if (fwrite($localStream, $buffer) === false) :
                  $download = false;
               endif;
        }
     endif;
     @fclose($sftpStream);
     @fclose($localStream);
  endif;
  return $download;
}

// SFTP delete a single specified file
// Returns boolean
function sftpDeleteSingleFile ($connectionId, $destination_file) {
  $delete = FALSE;
  if ($connectionId != "") :
     // $sftp = ssh2_sftp($connectionId);
     // $delete = ssh2_sftp_unlink($sftp, $destination_file);
     $delete = ssh2_sftp_unlink($connectionId, $destination_file);
  endif;
  return $delete;
}

// Gets a list of files of the specified directory
function sftpGetDirectoryContent ($connectionId, $directory) {
  $retValue = FALSE;
  if ($connectionId != "") :
     $handle = opendir("ssh2.sftp://" . $connectionId . "/" . $directory);
     $retValue = array();
     while (false != ($entry = readdir($handle))){
         $retValue[] = $entry;
     }
  endif;
  return $retValue;
}


// *************************
// *** Complex Functions ***
// *************************

// FTP transfer for a single specified file
// Returns error array, "0" eq. "operation completed, OK"
function ftpOperation ($f_ftp_ssl, $mode = "", $source_file, $destination_file = "", $ftp_server = "", $ftp_user_name = "", $ftp_user_pass = "", $ftp_path_src = "", $ftp_path_dest = "") {
	$errNo = "101"; $errDesc = "No transfer mode specified";
	$source_file = trim($source_file);
	$destination_file = trim($destination_file);
	$ftp_path_src = trim($ftp_path_src);
	$ftp_path_dest = trim($ftp_path_dest);
	if ($f_ftp_ssl != "" && is_numeric($f_ftp_ssl)) :
	   if ((($mode == "UPLOAD" || $mode == "DOWNLOAD") && $source_file != "") || ($mode == "DELETE"  && $destination_file != "")) :
        if ($destination_file == "") :
           $destination_file = $source_file;
        endif;
        if ($ftp_path_dest != "" && substr($ftp_path_dest,0,1) != "/" && substr($ftp_path_dest,-1) != "/") : 
           $ftp_path_dest = "/" . $ftp_path_dest . "/";
        endif;
        if ($f_ftp_ssl == "2") :
           // $connId = sshConnect($ftp_server, $ftp_user_name, $ftp_user_pass);
           $connId = sftpConnect($ftp_server, $ftp_user_name, $ftp_user_pass);
        else :
           $connId = ftpConnect($ftp_server, $ftp_user_name, $ftp_user_pass, $f_ftp_ssl);
        endif;
        if ($connId != "") :
           if ($mode == "UPLOAD") :
              if ($f_ftp_ssl == "2") :
                 $op = sftpUploadSingleFile($connId, $ftp_path_src . $source_file, $ftp_path_dest . $destination_file);
              else :
                 $op = ftpUploadSingleFile($connId, $ftp_path_src . $source_file, $ftp_path_dest . $destination_file);
              endif;
           elseif ($mode == "DOWNLOAD") :
              if ($f_ftp_ssl == "2") :
                 $op = sftpDownloadSingleFile($connId, $ftp_path_src . $source_file, $ftp_path_dest . $destination_file);
              else :
                 $op = ftpDownloadSingleFile($connId, $ftp_path_src . $source_file, $ftp_path_dest . $destination_file);
              endif;
           elseif ($mode == "DELETE") :
              if ($f_ftp_ssl == "2") :
                 $op = sftpDeleteSingleFile($connId, $ftp_path_dest . $destination_file);
              else :
                 $op = ftpDeleteSingleFile($connId, $ftp_path_dest . $destination_file);
              endif;
           endif;
           if ($op) :
              if ($f_ftp_ssl == "2") :
                 sftpQuitConnection($connId);
              else :
                 ftpQuitConnection($connId);
              endif;
              $errNo = "0"; $errDesc = "OK";
           else :
              $errNo = "104"; $errDesc = "Transfer failed";
           endif;
        else :
           $errNo = "103"; $errDesc = "No connection";
        endif;
     else :
        $errNo = "102"; $errDesc = "No file specified";
     endif;
  endif;
  return array($errNo, $errDesc);
}

// FTP upload for a single specified file
function ftpUpload ($source_file, $destination_file = "", $ftp_server, $ftp_user_name, $ftp_user_pass, $ftp_path_src, $ftp_path_dest, $f_ftp_ssl = "0") {
  return ftpOperation($f_ftp_ssl, "UPLOAD", $source_file, $destination_file, $ftp_server, $ftp_user_name, $ftp_user_pass, $ftp_path_src, $ftp_path_dest);
}

// FTP download for a single specified file
function ftpDownload ($source_file, $destination_file = "", $ftp_server, $ftp_user_name, $ftp_user_pass, $ftp_path_src, $ftp_path_dest, $f_ftp_ssl = "0") {
	return ftpOperation($f_ftp_ssl, "DOWNLOAD", $source_file, $destination_file, $ftp_server, $ftp_user_name, $ftp_user_pass, $ftp_path_src, $ftp_path_dest);
}

// FTP delete for a single specified file
function ftpDelete ($destination_file, $ftp_server, $ftp_user_name, $ftp_user_pass, $ftp_path_dest, $f_ftp_ssl = "0") {
	return ftpOperation($f_ftp_ssl, "DELETE", "", $destination_file, $ftp_server, $ftp_user_name, $ftp_user_pass, "", $ftp_path_dest);
}

// FTP directory listing for a single specified file
function ftpDir ($ftp_server, $ftp_user_name, $ftp_user_pass, $ftp_path_dest, $mode = "", $f_ftp_ssl = "0") {
	$errNo = "0"; $errDesc = "OK";
  $remoteFileList = array();
  if ($f_ftp_ssl == "2") :
     $connId = sftpConnect($ftp_server, $ftp_user_name, $ftp_user_pass);
  else :
     $connId = ftpConnect($ftp_server, $ftp_user_name, $ftp_user_pass, $f_ftp_ssl);
  endif;
  if ($connId != "") :
     if ($mode == "1") :
        // ...
     else :
        if ($f_ftp_ssl == "2") :
           $remoteFileList = sftpGetDirectoryContent($connId, $ftp_path_dest);  // Get filenames only [default]
        else :
           $remoteFileList = ftpGetDirectoryContent($connId, $ftp_path_dest);  // Get filenames only [default]
        endif;
     endif;
     if ($f_ftp_ssl == "2") :
        sftpQuitConnection($connId);
     else :
        ftpQuitConnection($connId);
     endif;
  else :
     $errNo = "101"; $errDesc = "Remote location not reachable";
  endif;
  return array($remoteFileList, $errNo, $errDesc);
}
?>




