<?php
/*=======================================================================
 *
 * exportdata_ftp_auto.php
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/


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

$errMsg = "";
$f_fileName = "";
if (isset($argv[1]) && $argv[1] == "acapella7890") :
   if (isset($argv[2]) && $argv[2] != "") : 
      $f_fileName = $argv[2];
      if (isset($argv[3]) && $argv[3] != "") : 
         $hq_id = $argv[3];
         if (isset($argv[4]) && $argv[4] != "") :
            $customerId = $argv[4];
         else :
            $errMsg = "autoexport.ftp.upload failed by missing customerId";
         endif;
      else :
         $errMsg = "autoexport.ftp.upload failed by missing hq_id";
      endif;
   else :
      $errMsg = "autoexport.ftp.upload failed by missing f_fileName";
   endif;
else :
   $errMsg = "autoexport.ftp.upload failed by access code";
endif;

if ($errMsg != "") :
   $f_fileName = "";
endif;


if ($f_fileName != "" && $hq_id != "") :

   // Get the secret filename
   getDbFieldValues("exportfiles",array("expf_cryptname"),array("hq_id",$hq_id,"cs_id",$customerId,"expf_name",$f_fileName));
   $f_secretFileName = $expf_cryptname;

   if ($f_secretFileName != "") :

      $ftp_server = getParameterValue("0", "FTP_SERVER", $hq_id);
      $ftp_user_name = getParameterValue("0", "FTP_USER", $hq_id);
      $ftp_user_pass = getParameterValue("0", "FTP_PASSWORD", $hq_id);
      $ftp_ssl = "0";

      $export_path = getParameterValue("0", "EXPORT_PATH", $hq_id);
      $ftp_uploadpath = getParameterValue("0", "FTP_UPLOADPATH", $hq_id);

      $errorOccurred = FALSE;

      $connId = ftpGetConnection($ftp_server, $ftp_user_name, $ftp_user_pass, $ftp_ssl);

      if ($connId != "") :
         $op = ftpUploadSingleFile($connId, $export_path . $f_secretFileName, $ftp_uploadpath . $f_fileName);
         if (!$op) : $errorOccurred = TRUE; endif;
      else :
         $errorOccurred = TRUE;
      endif;

      ftpQuitConnection($connId);

      if ($errorOccurred) :
         $errMsg = "autoexport.ftp.upload failed by execution";
      else :
         // Update the flag if ftp upload was successfully
         updateStmt("exportfiles", "expf_name",$f_fileName, array("expf_ftpupload", "1"));
      endif;
   else :
      // $errMsg = "autoexport.ftp.upload failed by missing f_secretFileName";
   endif;
endif;

if ($errMsg != "") :
   sendInternalMail($errMsg . " [f_fileName:" . $f_fileName . "] " . " [hq_id:" . $hq_id . "] " . " [customerId:" . $customerId . "] ");
   $f_fileName = "";
endif;
?>
