<?php
// signature_signature.php
// RJM Programming
// May, 2017
// Help out signature_signature.html with large data set signatures

$oursubj="Signature's Signature";
$htmlis="";
$divcont="";
$data=["",
";00000::1;-112,148,112,146,112,142,112,134,112,130,112,123,112,114,112,108,112,101,112,94,112,88,112,81,112,75,112,71,112,65,112,60,112,56,113,55,114,52,116,48,118,47,119,46,122,44,124,44,127,43,130,42,134,42,136,42,140,42,144,42,147,42,151,42,156,42,159,42,163,43,167,45,169,46,171,47,173,49,175,51,176,52,177,53,177,55,178,56,178,57,178,59,178,62,178,63,178,66,176,68,173,70,171,73,165,78,162,79,158,82,153,85,147,88,136,92,131,95,127,97,123,98,121,99,118,100,115,101,113,102,111,102,110,103,109,103,108,103,107,103,108,103,110,103,113,103,118,104,123,105,129,107,136,110,143,115,150,119,159,125,168,130,177,137,186,143,191,145,198,150,205,154,207,155,211,158,215,160,217,161,219,162,220,162,221,163,222,163,222,164,223,164,223,165,224,165,224,166,225,166,224,166,-340,166,340,165,340,163,340,160,340,156,340,148,340,137,340,125,340,112,340,99,340,88,342,78,346,68,348,65,351,58,354,52,358,48,362,44,366,43,373,41,379,41,385,41,391,42,398,47,405,51,412,56,416,59,427,67,433,72,440,78,453,89,458,95,467,103,476,112,484,120,492,129,499,136,502,139,504,141,509,145,511,148,514,150,515,151,517,152,518,152,519,152,520,152,521,150,522,149,523,148,524,146,526,143,529,137,530,134,532,124,535,114,537,104,539,93,541,86,543,76,545,67,547,58,550,52,551,50,554,46,557,42,560,41,564,41,568,41,572,41,578,44,583,49,590,54,598,61,612,72,617,78,626,86,634,96,641,105,647,116,653,126,656,131,660,140,663,149,666,158,667,161,669,167,670,172,671,175,672,177,672,178,673,179,674,179,674,177,674,173"];

function ourpremail($ris, $ssis, $tis, $his) {
    global $okay;
    $xsis = $ssis;
    try {
       //file_put_contents("aaaaa.00aaa", $ris . " " . $xsis);
       //mail("rmetcalfe15@gmail.com", $xsis, "My Body", $his);
       mail($ris, $xsis, $tis, $his);
    } catch (Exception $merr) {
      $okay = 0;
      if (file_exists(dirname(__FILE__) . "/mail.prob")) {
        file_put_contents(dirname(__FILE__) . "/mail.prob", file_get_contents(dirname(__FILE__) . "/mail.prob") . "\n\n" . date('l jS \of F Y h:i:s A') . " ... " . $merr->getMessage());
      } else {
        file_put_contents(dirname(__FILE__) . "/mail.prob", date('l jS \of F Y h:i:s A') . " ... " . $merr->getMessage());
      }
    }
}

function my_s_s_server_remote_addr() {
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
            $rma = '000000'.$rma;
    elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
            $rma = '00000'.$rma;
    elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
            $rma = '0000'.$rma;
    elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
            $rma = '000'.$rma;
    elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
            $rma = '00'.$rma;
    elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
            $rma = '0'.$rma;
    return $rma;
}

$eol = PHP_EOL;
if (isset($_POST['emailto']) && isset($_POST['contentto'])) {
        if (isset($_POST['htmlto'])) {
          if (strlen($_POST['htmlto']) > 0) {
          $htmlis=urldecode($_POST['htmlto']);
          //if (strpos($htmlis, "</head>") === false || 1 == 1) {
          //   $htmlis=str_replace("</body>", "<img style=\"position:absolute;top:0;left:0;\" src=\"" . $_POST['contentto'] . "\" title=\"User of Signature Signature\"></img></body>", $htmlis);
          //} else {
          //   $htmlis=str_replace("</head>", "<style> body {  background: #fff url(" . urldecode($_POST['contentto']) . "); } </style>\n</head>", $htmlis);
          //}
          }
        } else if (sizeof(explode('<',urldecode($_POST['emailto']))) > 1) {
          $htmlis=str_replace(explode('<',urldecode($_POST['emailto']))[0] . '<','<',urldecode($_POST['emailto']));
          //if (strpos($htmlis, "</head>") === false || 1 == 1) {
             //$htmlis=str_replace("<body", "<head><style> body {  background: #fff url(" . urldecode($_POST['contentto']) . "); } </style>\n</head><body", $htmlis);
          //   $htmlis=str_replace("</body>", "<img style=\"position:absolute;top:0;left:0\" src=\"" . $_POST['contentto'] . "\" title=\"User of Signature Signature\"></img></body>", $htmlis);
          //} else {
          //   $htmlis=str_replace("</head>", "<style> body {  background: #fff url(" . urldecode($_POST['contentto']) . "); } </style>\n</head>", $htmlis);
          //}
        }
        if (strlen($htmlis) > 0) {
           $oursubj="Congratulations. You have been sent a Postcard";
        }
        if (isset($_POST['subject'])) {
           if (strlen($_POST['subject']) > 0) {
            $oursubj=urldecode($_POST['subject']);
           }
        }
        if (isset($_POST['from'])) {
        if ($_POST['from'] != '') {
        $headers = 'From: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
        $headers .= 'Reply-To: ' . urldecode($_POST['from']) . $eol;
        } else {
        $headers = 'From: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
        $headers .= 'Reply-To: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
        }
        } else {
        $headers = 'From: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
        $headers .= 'Reply-To: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
        }
        if (isset($_POST['cc'])) {
        if ($_POST['CC'] != '') {
        $headers .= 'CC: ' . urldecode($_POST['cc']) . $eol;
        }
        }
        if (isset($_POST['bcc'])) {
        if ($_POST['BCC'] != '') {
        $headers .= 'BCC: ' . urldecode($_POST['bcc']) . $eol;
        }
        }
                   //     'X-Mailer: PHP/' . phpversion();
        $contents = str_replace('data:image/png;base64,', '', str_replace('data:image/jpg;base64,', '', str_replace('data:image/jpeg;base64,', '', urldecode($_POST['contentto']))));
        if ($contents != "") {
          if ($contents != '') {

    		date_default_timezone_set('Australia/Perth');

            //$mysubject .= ' ... ' . $mybody;
    		//$fs = explode("/", $mybody);
    		//$filename = $fs[-1 + sizeof($fs)];
    		$content = chunk_split(base64_encode($contents));
    		if ($htmlis == "") {
    		$content = "<!doctype html><html><body><img src=\"" . $_POST['contentto'] . "\" title=\"User of Signature Signature\"></img></body></html>";
            } else {
            //$content=$htmlis;
            $hbits=explode("</head>", str_replace("<head>","</head>",$htmlis));
            $bbits=explode("</body", str_replace("<body","</body",$htmlis));
            $hdrbit="<head><style> img {  opacity:0.7; position:absolute; top:0; left:0; z-index:1;  } \n :not(img) { opacity:1.0; width:100; text-align:right; z-index:9; background-color: transparent; } </style></head>";
            $bodybit="<body>";
            $endbit="";
            if (sizeof($hbits) > 1) {
            $hdrbit=str_replace("<head><style>", "<head> " . $hbits[1] . "\n<style>", $hdrbit);
            }
            if (sizeof($bbits) > 1) {
            $bodybit="<body" . explode(">",$bbits[1])[0] . ">"; 
            $endbit = substr($bbits[1],strlen(explode(">",$bbits[1])[0] . ">"));
            }
    		$content = "<!doctype html><html>" . $hdrbit . $bodybit . "<img src=\"" . $_POST['contentto'] . "\" title=\"User of Signature Signature\"></img>" . $endbit . "</body></html>";
            }
  //file_put_contents("user_of_s_s.html", $bbits[1] . "..." . $endbit . ":::" . $htmlis);
  //file_put_contents("user_of_s_s.htm", $content);
    		//$uid = md5(uniqid(time()));

    		// a random hash will be necessary to send mixed content
    		$separator = md5(time());

    		$headers .= "MIME-Version: 1.0" . $eol;
    		//$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;
    		//$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
    		//$headers .= "This is a MIME encoded message." . $eol . $eol;

    		// message
    		//$headers .= "--" . $separator . $eol;
    		//$headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
    		//$headers .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;

			//if ($cno != 0) {
    		//  $headers .= $defone . $eol . $eol;
    		//} else {
    		//  $headers .= "" . $eol . $eol;
    		//}
            //ourpremail($tem, $mysubject, "", $headers . "--" . $separator . "--");

    		// attachment
    		$filename="user_of_signature_signature_today.html";
    		//$headers .= "--" . $separator . $eol;
    		$headers .= "Content-Type: text/html; charset=utf-8" . $eol; //application/octet-stream; name=\"" . $filename . "\"" . $eol;
    		//$headers .= "Content-Transfer-Encoding: base64" . $eol;
    		//$headers .= "Content-Disposition: attachment;filename=\"" . $filename . "\"" . $eol; //inline" . $eol; //attachment;filename=\"" . $filename . "\"" . $eol;
    		$headers .= $content . $eol . $eol;
          }
        }                      

        //$headers .= "--" . $separator . "--";
    
    		
    		//file_put_contents("aaa.aaa", $filename);
            
        ourpremail(explode('<',urldecode($_POST['emailto']))[0], $oursubj, "", $headers);
        exit;
                         
}


if (isset($_POST['sscoords'])) {
    $thiscont=file_get_contents(dirname(__FILE__) . "/signature_signature.php");
    if (strpos($thiscont, ";" . my_s_s_server_remote_addr() . ";") !== false) {
      $divcont=explode('"',explode(";" . my_s_s_server_remote_addr() . ";", $thiscont)[1])[0];
      $thiscont=str_replace(";" . my_s_s_server_remote_addr() . ";" . $divcont, ";" . my_s_s_server_remote_addr() . ";" . $_POST['sscoords'], $thiscont);
    } else {
      $thiscont=str_replace('"' . ']' . ';', '",' . "\n" . '"' . ";" . my_s_s_server_remote_addr() . ";" . $_POST['sscoords'] . '"' . ']' . ';', $thiscont);
    }
    file_put_contents(dirname(__FILE__) . "/signature_signature.php", $thiscont);
    header("Location: ./signature_signature.html");
} else {
    $thiscont=file_get_contents(dirname(__FILE__) . "/signature_signature.php");
    if (strpos($thiscont, ";" . my_s_s_server_remote_addr() . ";") !== false) {
      $divcont=explode('"',explode(";" . my_s_s_server_remote_addr() . ";", $thiscont)[1])[0];
      echo "<!doctype html><html><body onload=\" if (parent.document.getElementById('longsignature')) {  parent.document.getElementById('longsignature').innerHTML='" . $divcont . "'; }  \"></body></html>";
    }
}
?>
