<?php      
/*=======================================================================
 *
 * cron_export.php
 *
  =======================================================================*/


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


if ($argv[1] == "acapella7890") :

   // Error reporting
   error_reporting(E_ERROR | E_WARNING | E_PARSE);

   $path = getAbsoluteSystemPath();
   $logFile = $path . "/log/sys_auto_export.log";


   // Special (S)FTP upload location
   $ftp_server = getParameterValue("0", "FTP_SERVER", "101");
   $ftp_user_name = getParameterValue("0", "FTP_USER", "101");
   $ftp_user_pass = getParameterValue("0", "FTP_PASSWORD", "101");
   $f_ftp_ssl = "0";
   // if ($sendMode == "SFTP") : $f_ftp_ssl = "2"; endif;
   $localPath = getParameterValue("0", "FTP_LOCALPATH", "101");
   if ($localPath == "") :
      $localPath = "../export/download/";
   endif;
   $remotePath = getParameterValue("0", "FTP_UPLOADPATH", "101");
   if ($remotePath == "") :
      $remotePath = "/stadtbote/";
   endif;

// *********************************************************************
$debug = false;
if ($debug) :
   $f_ftp_ssl = "2";
   $ftp_server = getParameterValue("0", "FTP_SERVER_MPS1", "0");
   $ftp_user_name = getParameterValue("0", "FTP_USER_MPS1", "0");
   $ftp_user_pass = getParameterValue("0", "FTP_PASSWORD_MPS1", "0");
   // $localPath = getParameterValue("0", "FTP_LOCALPATH_MPS1", "0");
   $localPath = $path . "/export/download/";
   $remotePath = getParameterValue("0", "FTP_REMOTEPATH_MPS1", "0");
endif;
// *********************************************************************


   // Get real file names to be uploaded
   $expfFileNamesToBeUploaded = getColVectorFromDB2ArrayByClause("exportfiles", "expf_name", "expf_ftpupload = '0' AND cs_id = '0'", "expf_id", "", "");  
   // Get crypted file names to be uploaded
   $expfCryptFileNamesToBeUploaded = getColVectorFromDB2ArrayByClause("exportfiles", "expf_cryptname", "expf_ftpupload = '0' AND cs_id = '0'", "expf_id", "", "");

   // Key array
   $expfKeyArray = array_keys($expfFileNamesToBeUploaded);
   $expfKeyArrayLen = count($expfKeyArray);

   if ($expfKeyArrayLen != "" && is_numeric($expfKeyArrayLen) && $expfKeyArrayLen > 0) :

      // Exports
      for ($i = 0; $i < $expfKeyArrayLen; $i++) :

          $expfId = $expfKeyArray[$i];
          $fileName = $expfFileNamesToBeUploaded[$expfId];
          $cryptFilename = $expfCryptFileNamesToBeUploaded[$expfId];

          if ($fileName != "" && $cryptFilename != "") :
             $opArray = ftpUpload($cryptFilename, $fileName, $ftp_server, $ftp_user_name, $ftp_user_pass, $localPath, $remotePath, $f_ftp_ssl);
             if ($opArray[0] == "0") :
                $state = "OK";
                $res = updateStmt("exportfiles", "expf_id", $expfId, array("expf_ftpupload", "1"), "");
                if (!($db->affected_rows > 0)) :
                   $state = "NOK:ERR_UPLOADED_BUT_FLAG_FAILED";
                endif;
             else :
                $state = "NOK";
             endif;
          else :
             $state = "NOK:ERR_BAD_FILENAME_OR_CRYPTNAME";
          endif;

          $currentTime = getDateTime("0");
          writeToFile($logFile, "OP=UPLOAD|DATETIME=" . $currentTime . "|FILENAME=" . $fileName . "|CRYPTFILENAME=" . $cryptFilename . "|STATE=" . $state);
$debug = true;
if ($debug) : 
   echo $currentTime . " " . $expfId . " " . $filename . " " . $cryptFilename . " " . $state . "\n";
endif;
      endfor;
   endif;
endif;      
?>
