<?php
// world.php
// RJM Programming
// March, 2016
// Help out world.html with canvas element data email attachments

$idata = '';
$ismobile = '';
$gmode = '';
$mydurl = '';

function ourpremail($ris, $ssis, $tis, $his) {
    $xsis = $ssis;
    try {
       //file_put_contents("aaaaa.aaa", $his);
       //mail("rmetcalfe15@gmail.com", $xsis, "My Body", $his);
       mail($ris, $xsis, $tis, $his);
    } catch (Exception $merr) {
      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 send_email($tem, $mysubject, $mybody) {
    global $idata;
    $eol = PHP_EOL;

    if (strpos($tem, ",") !== false) {
        $hs = explode(",", $tem);
        for ($ihs=0; $ihs<sizeof($hs); $ihs++) {
           if (strpos($hs[$ihs], "@") !== false) {
             send_email($hs[$ihs], $mysubject, $mybody);
           } 
        }
    } else {
        $headers = 'From: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
        $headers = 'Reply-To: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
                   //     'X-Mailer: PHP/' . phpversion();
        $contents = "";
        if ($idata != "") $contents = $idata;
        if (strpos('~' . $mybody, '~http') !== false || $contents != "") {
          if ($contents == "") $contents = @file_get_contents($mybody);
          if ($contents != '') {

    		date_default_timezone_set('Australia/Perth');

            //$mysubject .= ' ... ' . $mybody;
    		$fs = explode("/", $mybody);
    		$filename = $fs[-1 + sizeof($fs)];
    		$content = chunk_split(base64_encode($contents));
    		//$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;

    		$headers .= "Please see attached image below:" . $eol . $eol;
            //ourpremail($tem, $mysubject, "", $headers . "--" . $separator . "--");

    		// attachment
    		$headers .= "--" . $separator . $eol;
    		$headers .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
    		$headers .= "Content-Transfer-Encoding: base64" . $eol;
    		$headers .= "Content-Disposition: attachment;filename=\"" . $filename . "\"" . $eol;
    		$headers .= $content . $eol . $eol;
    		$headers .= "--" . $separator . "--";
    		
    		//file_put_contents("aaa.aaa", $filename);
            
            ourpremail($tem, $mysubject, "", $headers);
          }
        } 
        if ($contents == '') {
    		
    		//file_put_contents("aaa.aaa", "");
    		
          ourpremail($tem, $mysubject, $mybody, $headers);
        }
    }
    return "";
}

            $bigbits = explode("&mydurl=", file_get_contents('php://input'));
    		if (sizeof($bigbits) == 2) { 
    		 $mydurl = urldecode($bigbits[1]);
    		// file_put_contents("aaaaa.aaaaa", $mydurl);
    		}


if (isset($_GET['mode']) || isset($_POST['mode']) || isset($_POST['ismobile'])) {
    		//file_put_contents("aaa.aaa", "a.a");
  if (isset($_POST['ismobile'])) {
    $gmode .= " ";
    $ismobile = $_POST['ismobile'];
    if (!isset($_POST['mydurl']) && $mydurl == '') { // && isset($_POST['myfname'])) {
            $bigbits = explode("&mydurl=", file_get_contents('php://input'));
    		if (sizeof($bigbits) == 2) { 
    		 $mydurl = urldecode($bigbits[1]);
    		 //file_put_contents("aaaa.aaaa", $mydurl);
    		}
    }
  }
  if (isset($_GET['mode'])) $gmode .= $_GET['mode'];
  if (isset($_POST['mode'])) $gmode .= $_POST['mode'];
  if ($gmode != '' && $ismobile == '' && (isset($_POST['mydurl']) || $mydurl != '') && isset($_POST['myfname'])) {
    		//file_put_contents("aaa.aaa", "aa.aa");
   //$tem = urldecode($_POST['to']);
   if (strlen(str_replace(".png*", "", ($_POST['myfname'] . "*"))) != strlen(($_POST['myfname'] . "*")) && strlen(str_replace("*world_", "", ("*" . $_POST['myfname']))) != strlen(("*" . $_POST['myfname']))) {
    if ($mydurl != '') {
    $idata = base64_decode($mydurl);
    } else {
    $idata = base64_decode(urldecode($_POST['mydurl']));
    }
    file_put_contents($_POST['myfname'], $idata);
   }
  } else if ($ismobile != '' && (isset($_POST['mydurl']) || $mydurl != '') && isset($_POST['myfname']) && isset($_POST['to'])) {
    		//file_put_contents("aaa.aaa", "aaa.aa");
   $tem = urldecode($_POST['to']);
   if (strlen(str_replace(".png*", "", ($_POST['myfname'] . "*"))) != strlen(($_POST['myfname'] . "*")) && strlen(str_replace("*world_", "", ("*" . $_POST['myfname']))) != strlen(("*" . $_POST['myfname']))) {
    		//file_put_contents("aaa.aaa", "aaa.aaa");
    if ($mydurl != '') {
    $idata = base64_decode($mydurl);
    } else {
    $idata = base64_decode(urldecode($_POST['mydurl']));
    }
    //file_put_contents($_POST['myfname'], $idata);
    		//file_put_contents("aaa.aaa", "");
    $mysubject = $_POST['mysubject'];
    		//file_put_contents("aaa.aaa", urldecode($mysubject));
    $mybody = $_POST['myfname'];
    		//file_put_contents("aaa.aaa", urldecode($mysubject) . " ... " . urldecode($mybody));
    send_email($tem, urldecode($mysubject), urldecode($mybody));
   } 
  }
}

?>
