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


$HTTP_POST_VARS = !empty($HTTP_POST_VARS) ? $HTTP_POST_VARS : $_POST;
$HTTP_GET_VARS = !empty($HTTP_GET_VARS) ? $HTTP_GET_VARS : $_GET;
$HTTP_COOKIE_VARS = !empty($HTTP_COOKIE_VARS) ? $HTTP_COOKIE_VARS : $_COOKIE;
$HTTP_SERVER_VARS = !empty($HTTP_SERVER_VARS) ? $HTTP_SERVER_VARS : $_SERVER;
$HTTP_ENV_VARS = !empty($HTTP_ENV_VARS) ? $HTTP_ENV_VARS : $_ENV;
$HTTP_POST_FILES = !empty($HTTP_POST_FILES) ? $HTTP_POST_FILES : $_FILES;

if (!isset($PHP_SELF))
  $PHP_SELF = $_SERVER["PHP_SELF"];

if (substr(phpversion(), 0, 3) >= "5.1") :
   date_default_timezone_set('Europe/Berlin');
endif;


// *****************************************************************************

include_once ("htmlMimeMail.php");


function getHttpVars($httpVars)
{
  global $HTTP_GET_VARS, $HTTP_POST_VARS;
  
  $retArr = array();
  foreach($httpVars as $hVar)
    $retArr[] = (isset($HTTP_GET_VARS[$hVar])) ? $HTTP_GET_VARS[$hVar] : 
      ((isset($HTTP_POST_VARS[$hVar])) ? $HTTP_POST_VARS[$hVar] : "");
  return $retArr;
}

// Gets all script-parameters (HTTP_GET_VARS and HTTP_POST_VARS).
// If "$mode == 1" then all id-parameters (only these!) will be decoded
function getSecHttpVars($getSecHttpVarsMode = "0", $httpVars)
{
  global $HTTP_GET_VARS, $HTTP_POST_VARS;

  $retArr = getHttpVars($httpVars);
  $i = 0;
  foreach ($httpVars as $par) {
      global $$par;
      $$par = $retArr[$i];
      $i++;
  }
  // Decryption of the fields if encrypted
  if ($getSecHttpVarsMode == "1") :
     foreach ($httpVars as $par) {
        $$par = dc($$par);
     }         
  endif;  
  return $retArr;
}  

// 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;
}

// *****************************************************************************

getSecHttpVars("0", array("auth", "imei", "crashdumpdata", "without_log", "without_mail"));


if ($auth == "comegetsomexxxx9753" && $imei != "" && $crashdumpdata != "") :

   // Check writing log 
   $writeLog = true;
   if (!isset($without_log)) : $without_log = ""; endif;
   if ($without_log == "1") : $writeLog = false; endif;

   // Check sending
   $sendMail = true;
   if (!isset($without_mail)) : $without_mail = ""; endif;
   if ($without_mail == "1") : $sendMail = false; endif;

   // Decode BASE64
   $crashdumpdata = base64_decode($crashdumpdata);

   // Uncompress
   $crashdumpdata = gzinflate(substr($crashdumpdata,10,-8));
   
   // Current timestamp
   $currentTime = date("Y-m-d H:i:s");

   // calling client IP
   $currentClientIP = trim($_SERVER['REMOTE_ADDR']);
   
   // Append to log file
   if ($writeLog) :
      writeToFile($imei . ".log", $currentTime . " | " . $currentClientIP);
      writeToFile($imei . ".log", $crashdumpdata);
      writeToFile($imei . ".log", "-------------------------------------------------------------------------------------");
   endif;

   // Send mail to admin@assecutor.de
   if ($sendMail) :
      $mailObj = new htmlMimeMail();
      $mailObj->setFrom("admin@assecutor.de");
      $mailObj->setCc("sven.carstensen@appcreation.de");
      // $mailObj->setBcc($mailBccAddress);
      $mailObj->setSubject("NEW ANDROID CRASHDUMP! (" . $currentTime . " | " . $currentClientIP . ")");
  
      // HTML
      if (false) :
         $mailtextHtml  = "<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=ISO-8859-1\"></head>";
         $mailtextHtml .= "<body>";
         $mailtextHtml .= "<h4><b>NEW ANDROID CRASHDUMP!</b></h4><br><br>";
         $mailtextHtml .= "<b>Timestamp: " . $currentTime . "</b><br>";
         $mailtextHtml .= "<b>Client-IP: " . $currentClientIP . "</b><br><br><br>";
         $mailtextHtml .= "<b>Dump:</b><br>";
         $mailtextHtml .= nl2br($crashdumpdata);
         $mailtextHtml .= "</body>";
         $mailtextHtml .= "</html>";
         $mailObj->setHtml($mailtextHtml, null, "./");
      endif;
   
      // PLAIN-TEXT
      if (true) :
         $mailObj->setSubject("NEW ANDROID CRASHDUMP! (" . $currentTime . " | " . $currentClientIP . ")");
         $mailtext .= $crashdumpdata;
         $mailObj->setText($mailtext);
      endif;
   
      $mailResult = $mailObj->send(array("dev@assecutor.de"), 'smtp');
      if ($mailResult) :
         echo "DATA LOGGED! MAIL SENT!";
      else :
         echo "DATA LOGGED! MAIL NOT SENT!";
      endif;
      $mailObj = NULL;
   endif;

else :
   $randNum = rand(1, 10);
   if ($randNum == 1) :
      echo "SO NICHT!";
   elseif ($randNum == 2) :
      echo "ONCE MORE AGAIN!";
   elseif ($randNum == 3) :
      echo "LMAA! ;-)";
   elseif ($randNum == 4) :
      echo "OCH KOMM...!";
   elseif ($randNum == 5) :
      echo "HASTES BALD...?!";
   elseif ($randNum == 6) :
      echo "TRY IT AGAIN, BABY!";
   elseif ($randNum == 7) :
      echo "COME GET SOME!";
   elseif ($randNum == 8) :
      echo "SHAKE IT, BABY!";
   elseif ($randNum == 9) :
      echo "LOOOOOOS.......ER! ;-) ;-)";
   elseif ($randNum == 10) :
      echo "I WILL RIP OFF YOUR HEAD AND SHIT DOWN YOUR NECK! ;-)";
   else :
      echo "NU ABER...!";
   endif;
endif;
?>
