<?php

// Writes a string to a file optional with or without linefeed
function writeToFile($fileName, $stringToOperate, $mode = 'a', $noLf = "") {
  $fileHandle = fopen($fileName, $mode);
  if ($noLf == "") : $stringToOperate .= "\n"; endif;
  $opCode = fwrite($fileHandle, $stringToOperate);
  fclose($fileHandle);
  return $opCode;
}

function sendHttpPostRedirect ($url, $postArray = array(), $httpXFowardedFor = "") {
	global $debug;

  $retVal = "NO_DATA";

  if ($debug) :
     // print_r($_POST);
     $currentTime = date("Y-m-d H:i:s");
     writeToFile("uploader_post.log", "[" . $currentTime . " | " . trim($_SERVER['HTTP_X_FORWARDED_FOR']) . "]");
  endif;

  if ($url != "" && is_array($postArray) && count($postArray) > 0) :

     $fields = $postArray;

     if ($httpXFowardedFor == "1") :
        $fields["clientIP"] = trim($_SERVER['HTTP_X_FORWARDED_FOR']);
     endif;

     if (!isset($fields_string))
       $fields_string = "";
     foreach($fields as $key=>$value) { $fields_string .= $key.'='.urlencode($value).'&'; }

     rtrim($fields_string, '&');

     $ch = curl_init();
     curl_setopt($ch,CURLOPT_URL, $url);
     curl_setopt($ch,CURLOPT_POST, count($fields));
     curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
     if (isset($returnResponse) && $returnResponse != "") :
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, "1");
     endif;
     $result = curl_exec($ch);
     curl_close($ch);
     
     if (!($result === false)) :
        if (isset($returnResponse) && $returnResponse == "") :
           $retVal = "SENT_OK";
        else :
           $retVal = $result;
        endif;
     endif;     
  endif;

  return $retVal;
}


$debug = true;
$url = 'https://mps2.assecutor.de/uploader/uploader_ALL_REDIRECT_RECEIVER.php';
$retVal = sendHttpPostRedirect($url, $_POST, "1");

// if ($retVal != "SENT_OK") {
   echo "OK";
// };
?>
