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

$idata = '';
$ismobile = '';
$gmode = '';
$mydurl = '';
$ubits = explode(".htm", $_SERVER['HTTP_REFERER']);
$uubits = explode("/", $ubits[0]);
$lworld = $uubits[-1 + sizeof($uubits)]; 


function ourpremail($ris, $ssis, $tis, $his) {
    $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) {
      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;
    $cno = 0;
    $eol = PHP_EOL;
    if (isset($_POST['mapclick1']) && isset($_POST['mapcoords1'])) {
     //if (urldecode($_POST['mapclick1']) != "" && urldecode($_POST['mapcoords1']) != "") {
     if (urldecode($_POST['mapcoords1']) != "") {
       $cfound = 0;
       $cno = 1;
       while (isset($_POST['mapclick' . $cno]) && isset($_POST['mapcoords' . $cno])) {
                if (urldecode($_POST['mapclick' . $cno]) != "" && urldecode($_POST['mapcoords' . $cno]) != "") {
                  $cfound = 1;
                }
                $cno++;
       }
       $cno = $cfound;
     }
    }

    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 {
        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 = "";
        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;

			if ($isclickable) {
    		  $headers .= "Please see or download image (first) before any download and double clicking of image clickable web application below:" . $eol . $eol;
    		} else {
    		  $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;
    		
			if ($cno == 0) {
    		  $headers .= "--" . $separator . "--";
			} else {
    		  $headers .= "--" . $separator . $eol; //"--";
    		  $befdots = explode(".", $filename);
    		  $somehtml = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">';
    		  $somehtml .= '<html>';
    		  $somehtml .= '<head>';
    		  $somehtml .= '<title>Web Application for Clickable ' . $filename . '</title>';
    		  $somehtml .= '<body>';
    		  //$somehtml .= '<img style="zoom: 50%;" src="' . $filename . '" usemap="#' . str_replace(".","",str_replace("_","",str_replace("-","",$filename)))  . '" width="1692" height="1133" alt="click map" border="0" />';
    		  $somehtml .= '<img style="zoom: 50%;" src="' . $filename . '" usemap="#' . str_replace(".","",str_replace("_","",str_replace("-","",$filename)))  . '" alt="click map" border="0" />';
    		  $somehtml .= '<map id="' . str_replace(".","",str_replace("_","",str_replace("-","",$filename))) . '" name="' . str_replace(".","",str_replace("_","",str_replace("-","",$filename))) . '">';
    		  $somehtml .= '<area shape="default" nohref alt="" />';
    		  $somehtml .= '</map>';
    		  $somehtml .= '</body>';
    		  $somehtml .= '</html>';

              while (isset($_POST['mapclick' . $cno]) && isset($_POST['mapcoords' . $cno])) {
                if (urldecode($_POST['mapclick' . $cno]) != "" && urldecode($_POST['mapcoords' . $cno]) != "") {
                 $somehtml = str_replace('<area shape', '<area shape="rect" alt="Navigate ' . $cno . '" title="Navigate ' . $cno . ' to ' . urldecode($_POST['mapclick' . $cno]) . '" coords="' . urldecode($_POST['mapcoords' . $cno]) . '" href="' . urldecode($_POST['mapclick' . $cno]) . '" target="_blank" />' . '<area shape', $somehtml);
                }
                $cno++;
			  }

    		  $content=chunk_split(base64_encode($somehtml));
    		  $filename=$befdots[0] . ".html";
    		  $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) { 
    		 //if (strlen($bigbits[1]) < 35) {
             //  sleep(13);
             //  $bigbits = explode("&mydurl=", file_get_contents('php://input'));
    		 //}
             //$putfp = fopen('php://input', 'r');
             //$putdata = '';
             //while ($data = fread($putfp, 1024)) {
             // $putdata .= $data;
             //}
             //fclose($putfp);
             //$bigbits[1]=$putdata;
    		 $mydurl = urldecode($bigbits[1]);
    		 //file_put_contents("aaaaa.aaaaa", $mydurl);
    		 if (isset($_POST['mydurl'])) {
    		   if (strlen(urldecode($_POST['mydurl'])) > strlen($mydurl)) {
    		     $mydurl=urldecode($_POST['mydurl']);
    		   }
    		 }
             $idata = base64_decode($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($_POST['mydurl'])) {
    		   if (strlen(urldecode($_POST['mydurl'])) > strlen($mydurl)) {
    		     $mydurl=urldecode($_POST['mydurl']);
    		   }
    		 }
             $idata = base64_decode($mydurl);
    		}
    }
  }
  if (isset($_GET['mode'])) $gmode .= $_GET['mode'];
  if (isset($_POST['mode'])) $gmode .= $_POST['mode'];
       //file_put_contents("aaaaa.0aaa", $lworld . " " . $_POST['myfname']);
  if (strlen(str_replace("*feedback_", "", ("*" . $_POST['myfname']))) != strlen(("*" . $_POST['myfname']))) {
   $tem = urldecode($_POST['to']);
       //file_put_contents("aaaaa.02aaa", $lworld . " " . $_POST['myfname']);
   if (strlen(str_replace(".png*", "", ($_POST['myfname'] . "*"))) != strlen(($_POST['myfname'] . "*")) && strlen(str_replace("*" . $lworld . "_", "", ("*" . $_POST['myfname']))) != strlen(("*" . $_POST['myfname']))) {
       //file_put_contents("aaaaa.002aaa", $lworld . " " . $_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", $_SERVER['REMOTE_ADDR']);
    send_email($tem, urldecode($mysubject), urldecode($mybody));
   } 
  } else if ($gmode != '' && $ismobile == '' && (isset($_POST['mydurl']) || $mydurl != '') && isset($_POST['myfname'])) {
    		//file_put_contents("aaa.aaa", "aa.aa");
   //$tem = urldecode($_POST['to']);
       //file_put_contents("aaaaa.1aaa", $lworld . " " . $_POST['myfname']);
   if (strlen(str_replace(".png*", "", ($_POST['myfname'] . "*"))) != strlen(($_POST['myfname'] . "*")) && strlen(str_replace("*" . $lworld . "_", "", ("*" . $_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']);
       //file_put_contents("aaaaa.02aaa", $lworld . " " . $_POST['myfname']);
   if (strlen(str_replace(".png*", "", ($_POST['myfname'] . "*"))) != strlen(($_POST['myfname'] . "*")) && strlen(str_replace("*" . $lworld . "_", "", ("*" . $_POST['myfname']))) != strlen(("*" . $_POST['myfname']))) {
       //file_put_contents("aaaaa.002aaa", $lworld . " " . $_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", $_SERVER['REMOTE_ADDR']);
    send_email($tem, urldecode($mysubject), urldecode($mybody));
   } 
  }
}

?>
