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

$isgif = false;
$psubject = '';
$gmtblurb = '';
$pngext = 'png';
$idata = '';
$ismobile = '';
$gmode = '';
$mydurl = '';
$ubits = explode(".htm", $_SERVER['HTTP_REFERER']);
$uubits = explode("/", $ubits[0]);
$lworld = $uubits[-1 + sizeof($uubits)]; 
$okay = 1;
$bigbits;
$atendis = '';
$postsubj = '';
$postfname = '';
$altmydurl = '';

if (isset($_POST['isgif']) || isset($_GET['isgif'])) {
  if (isset($_GET['isgif'])) {
  if (strlen($_GET['isgif']) > 0) { $isgif = true; } //$pngext = 'gif';
  } else if (isset($_POST['isgif'])) { 
  if (strlen($_POST['isgif']) > 0) { $isgif = true; } //$pngext = 'gif';
  }
}

if (isset($_POST['tdsubj']) || isset($_GET['tdsubj'])) {
  if (isset($_GET['tdsubj'])) {
  $psubject = explode("~", str_replace("+"," ",urldecode($_GET['tdsubj'])))[0];
  if (strpos(str_replace("+"," ",urldecode($_GET['tdsubj'])),"~") !== false) {
  $gmtblurb = explode("~", str_replace("+"," ",urldecode($_GET['tdsubj'])))[1];
  }
  } else {
  $psubject = explode("~", str_replace("+"," ",urldecode($_POST['tdsubj'])))[0];
  if (strpos(str_replace("+"," ",urldecode($_POST['tdsubj'])),"~") !== false) {
  $gmtblurb = explode("~", str_replace("+"," ",urldecode($_POST['tdsubj'])))[1];
  }
  }
}

if (isset($_POST['mysubject']) && isset($_POST['myfname'])) {
  if (strpos($_POST['mysubject'], ' via ') !== false && strpos(($_POST['mysubject'] . ''), 'animated_gif_slideshow.gif') !== false) {
    sleep(9);
    if (file_exists('../PHP/animegif/' . server_remote_addr() . '.gif')) {
    $altmydurl = @file_get_contents('../PHP/animegif/' . server_remote_addr() . '.gif');
    $postsubjs = explode(' via ', $_POST['mysubject']);
    $postsubj = str_replace(' via ' . $postsubjs[-1 + sizeof($postsubjs)], '', $_POST['mysubject']);
    $postfname = $_POST['myfname'];
    if (strpos($_POST['myfname'], '.' . $pngext) !== false) {
     $postfname = str_replace('.' . $pngext, '.gif', $_POST['myfname']);
    } else {
     $postfname = $_POST['myfname'];
    }
    $pngext = 'gif';
    } else {
    $postsubjs = explode(' via ', $_POST['mysubject']);
    $postsubj = str_replace(' via ' . $postsubjs[-1 + sizeof($postsubjs)], '', $_POST['mysubject']);
    $postfname = $_POST['myfname'];
    }
  } else {
    $postsubj = $_POST['mysubject'];
    $postfname = $_POST['myfname'];
  }
} else if (isset($_GET['mysubject']) && isset($_GET['myfname'])) {
  if (strpos($_GET['mysubject'], ' via ') !== false && strpos(($_GET['mysubject'] . ''), 'animated_gif_slideshow.gif') !== false) {
    sleep(9);
    if (file_exists('../PHP/animegif/' . server_remote_addr() . '.gif')) {
    $altmydurl = @file_get_contents('../PHP/animegif/' . server_remote_addr() . '.gif');
    $postsubjs = explode(' via ', $_GET['mysubject']);
    $postsubj = str_replace(' via ' . $postsubjs[-1 + sizeof($postsubjs)], '', $_GET['mysubject']);
    $postfname = $_GET['myfname'];
    if (strpos($_GET['myfname'], '.' . $pngext) !== false) {
     $postfname = str_replace('.' . $pngext, '.gif', $_GET['myfname']);
    } else {
     $postfname = $_GET['myfname'];
    }
    $pngext = 'gif';
    } else {
    $postsubjs = explode(' via ', $_GET['mysubject']);
    $postsubj = str_replace(' via ' . $postsubjs[-1 + sizeof($postsubjs)], '', $_GET['mysubject']);
    $postfname = $_GET['myfname'];
    }
  } else {
    $postsubj = $_GET['mysubject'];
    $postfname = $_GET['myfname'];
  }
}

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

function ourpremail($ris, $ssis, $tis, $his) {
    global $okay, $atendis, $idata, $pngext, $postfname, $postfname, $altmydurl;
    $imdata = "";
    $otherccs="";
    $xris=""; //$ris;
    $spareris="";
    $toaststyling="<style> .custom-alert { display: inline-block; visibility: visible; background-color: #666; color: #fff; text-align: enter; margin: 5% auto; padding: 12px 48px; } </style> ";
    $xxris=explode(",", $ris);
    for ($iy=0; $iy<sizeof($xxris); $iy++) {
      $xris.="<a target=_blank href=mailto:" . $xxris[$iy] . " title=EmailClient>" . $xxris[$iy] . "</a>";
    }
    if (isset($_GET['cc'])) {
      $xxris=explode(",", urldecode($_GET['cc']));
      $spareris="";
      for ($iy=0; $iy<sizeof($xxris); $iy++) {
       $spareris.="<a target=_blank href=mailto:" . $xxris[$iy] . " title=EmailClient>" . $xxris[$iy] . "</a>";
      }
      if ($_GET['cc'] != "") $otherccs.="<br>CC: " . $spareris;
    } else if (isset($_POST['cc'])) {
      $xxris=explode(",", urldecode($_POST['cc']));
      $spareris="";
      for ($iy=0; $iy<sizeof($xxris); $iy++) {
       $spareris.="<a target=_blank href=mailto:" . $xxris[$iy] . " title=EmailClient>" . $xxris[$iy] . "</a>";
      }
      if ($_POST['cc'] != "") $otherccs.="<br>CC: " . $spareris;
    }
    if (isset($_GET['bcc'])) {
      $xxris=explode(",", urldecode($_GET['bcc']));
      $spareris="";
      for ($iy=0; $iy<sizeof($xxris); $iy++) {
       $spareris.="<a target=_blank href=mailto:" . $xxris[$iy] . " title=EmailClient>" . $xxris[$iy] . "</a>";
      }
      if ($_GET['bcc'] != "") $otherccs.="<br>BCC: " . $spareris;
    } else if (isset($_POST['bcc'])) {
      $xxris=explode(",", urldecode($_POST['bcc']));
      $spareris="";
      for ($iy=0; $iy<sizeof($xxris); $iy++) {
       $spareris.="<a target=_blank href=mailto:" . $xxris[$iy] . " title=EmailClient>" . $xxris[$iy] . "</a>";
      }
      if ($_POST['bcc'] != "") $otherccs.="<br>BCC: " . $spareris;
    }
    $hgt="200";
    $xsis = $ssis;
    $altwin='';
    if (1 == 1 && $idata != "") {
      if ($altmydurl != '') {
      $imdata="<img src=//www.rjmprogramming.com.au/PHP/animegif/" . server_remote_addr() . ".gif></img>";
      } else {
      $imdata="<img src=data:image/" . $pngext . ";base64," . base64_encode($idata) . "></img>";
      }
      $hgt="480";
    }
    try {
       //file_put_contents("aaaaa.00aaa", $ris . " " . $xsis);
       //mail("rmetcalfe15@gmail.com", $xsis, "My Body", $his);
       mail($ris, $xsis, $tis, $his);
       $altwin = "<!doctype html><html><head>" . $toaststyling . "<script type='text/javascript'> var wo=null; function andthenx() { if (wo) { wo.close(); } }  </script></head><body onload=\" wo=window.open('','_blank','status=no,toolbar=no,menubar=yes,left=10,top=300,width=1350,height=" . $hgt . "'); wo.document.write('" . $toaststyling . "<div class=custom-alert>Email sent successfully <br>To: " . $xris . $otherccs . "<br>Subject: " . $ssis . "</div><br>" . $imdata . "<br>'); setTimeout(andthenx,7000); \"></body></html>";
    } 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());
        $altwin = "<!doctype html><html><head><script type='text/javascript'> var wo=null; function andthenx() { if (wo) { wo.close(); } }  </script></head><body onload=\" wo=window.open('','_blank','status=no,toolbar=no,menubar=yes,left=100,top=100,width=750,height=200'); wo.document.write('<p>" . $merr->getMessage() . " sending to <br>" . $ris . $otherccs . "</p>'); setTimeout(andthenx,7000);  \"></body></html>";
      } else {
        file_put_contents(dirname(__FILE__) . "/mail.prob", date('l jS \of F Y h:i:s A') . " ... " . $merr->getMessage());
        $altwin = "<!doctype html><html><head><script type='text/javascript'> var wo=null; function andthenx() { if (wo) { wo.close(); } }  </script></head><body onload=\" wo=window.open('','_blank','status=no,toolbar=no,menubar=yes,left=100,top=100,width=750,height=200'); wo.document.write('<p>" . $merr->getMessage() . " sending to <br>" . $ris . $otherccs . "</p>'); setTimeout(andthenx,7000);  \"></body></html>";
      }
    }
    if ($atendis != "") {
      echo $atendis;
      $atendis='';
      exit;
    } else if ($altwin != "") {
      echo $altwin;
      $altwin='';
      exit;
    }
}

function issetgetaudiodata($which) {
    global $bigbits;
    if (isset($_POST[$which])) {
      return true;
    } else if (sizeof($bigbits) == 2) {
                  if (strpos($bigbits[0], $which . "=") !== false) {
                    return true;
                  }
    }
    return false;
}

function getaudiodata($which) {
    global $bigbits;
    if (isset($_POST[$which])) {
      return $_POST[$which];
    } else if (sizeof($bigbits) == 2) {
                  if (strpos($bigbits[0], $which . "=") !== false) {
                    $xxx=explode($which . "=", $bigbits[0]);
                    $yyy=explode("&", $xxx[1]);
                    return $yyy[0];
                  }
    }
    return "";
}

function send_email($tem, $mysubject, $mybody) {
    global $idata, $bigbits, $atendis, $pngext, $postfname, $postfname, $altmydurl, $isgif, $psubject, $gmtblurb;
    if ($psubject != "") { $mysubject=$psubject; }
    $aext = ".wav";
    $cno = 0;
    $mobilewav = "";
    $pref="";
    $defone = ""; //"Please see or download image (first) before any download and double clicking of image clickable and applicable web application below:";
    $eol = PHP_EOL;
    if (issetgetaudiodata('mapclick1') && issetgetaudiodata('mapcoords1') && issetgetaudiodata('mapaudio1')) {
     if (urldecode(getaudiodata('mapcoords1')) != "") {
       $cfound = 0;
       $cno = 1;
       while ((issetgetaudiodata('mapclick' . $cno) || issetgetaudiodata('mapaudio' . $cno)) && issetgetaudiodata('mapcoords' . $cno)) {
                if (issetgetaudiodata('mapclick' . $cno)) {
                if (urldecode(getaudiodata('mapclick' . $cno)) != "" && urldecode(getaudiodata('mapcoords' . $cno)) != "") {
                  $cfound = 1;
                  if ($defone == "") {
                    $defone = $pref . "Please see or download image (first) before any download and double clicking of image clickable and applicable web application below ... please note mobile web email clients are preferred to mobile email clients:";
                  } else if (strpos($defone, " audio") !== false) {
                    $defone = $pref . "Please see or download image (first) and audios (second) before any download and double clicking of image clickable and applicable web application below ... please note mobile web email clients are preferred to mobile email clients:";
                  } else {
                    $defone = $pref . "Please see or download image (first) before any download and double clicking of image clickable and applicable web application below ... please note mobile web email clients are preferred to mobile email clients:";
                  }
                }
                }
                if (issetgetaudiodata('mapaudio' . $cno)) {
                if (urldecode(getaudiodata('mapaudio' . $cno)) != "" && urldecode(getaudiodata('mapcoords' . $cno)) != "") {
                  $cfound = 1;
                  if ($defone == "") {
                    $defone = $pref . "Please see or download audio (first) before any download and double clicking of image clickable and applicable web application below ... please note mobile web email clients are preferred to mobile email clients:";
                  } else if (strpos($defone, "download image") !== false) {
                    $defone = $pref . "Please see or download image (first) and audios (second) before any download and double clicking of image clickable and applicable web application below ... please note mobile web email clients are preferred to mobile email clients:";
                  } else {
                    $defone = $pref . "Please see or download audios (first) before any download and double clicking of image clickable and applicable web application below ... please note mobile web email clients are preferred to mobile email clients:";
                  }
                }
                } else if (sizeof($bigbits) == 2) {
                  if (strpos($bigbits[0], "mapaudio") !== false) {
                    $pref = ""; //"huh ";
                    $defone = $pref . $defone;
                  }
                }
                $cno++;
       }
       $cno = $cfound;
     }
    } else if (issetgetaudiodata('mapclick1') && issetgetaudiodata('mapcoords1')) {
     //if (urldecode(getaudiodata('mapclick1') != "" && urldecode(getaudiodata('mapcoords1') != "") {
     if (urldecode(getaudiodata('mapcoords1')) != "") {
       $cfound = 0;
       $cno = 1;
       while (issetgetaudiodata('mapclick' . $cno) && issetgetaudiodata('mapcoords' . $cno)) {
                if (urldecode(getaudiodata('mapclick' . $cno)) != "" && urldecode(getaudiodata('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 && strpos($mybody, '/') === false) {
            if (file_exists($mybody)) unlink($mybody);
            $mybody='http://www.rjmprogramming.com.au/HTMLCSS/' . $mybody;
          }
        } else if (strpos('~' . $mybody, '~http') === false && strpos($mybody, '/') === false) {
          if (file_exists($mybody)) {
            $mybodys='http://www.rjmprogramming.com.au/HTMLCSS/' . $mybody;
            $contents=@file_get_contents($mybody);
            unlink($mybody);
                        $mybody=$mybodys;
            $idata = $contents;
          }
        }
        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);
    		if ($altmydurl != '') {
    		$filename = str_replace('.png','.gif',$fs[-1 + sizeof($fs)]);
    		$content = chunk_split(base64_encode($altmydurl));
    		} else {
    		$filename = $fs[-1 + sizeof($fs)];
    		if ($isgif || strpos($filename, ".png") !== false && strpos($contents, "data:image/gif;base64,") !== false) {
    		  $filename=str_replace(".png",".gif",$filename);
    		} 
    		$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 ($cno != 0) {
    		  $headers .= $defone . $eol . $eol;
    		} else if ($gmtblurb != "") { 
    		  $headers .= "Please see attached " . $gmtblurb . " image 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 {
			  $origcno = $cno;
    		  $headers .= "--" . $separator . $eol; //"--";
    		  $befdots = explode(".", $filename);


              while (issetgetaudiodata('mapcoords' . $cno) && issetgetaudiodata('mapaudio' . $cno)) {  // allow for audio
                if (urldecode(getaudiodata('mapaudio' . $cno)) != "" && urldecode(getaudiodata('mapcoords' . $cno)) != "") {
                 $aext = ".wav";
                 if (issetgetaudiodata('mapext' . $cno)) {
                   if (getaudiodata('mapext' . $cno) != "") $aext=urldecode(getaudiodata('mapext' . $cno));
                 }
                 $mobilewav .= "<br>";
                 //$mobilewav .= "<audio id='au" . $cno . "' controls='controls' autobuffer='autobuffer' autoplay='false'> ";
                 $ais="audio";
                 $bigais="";
                 if (issetgetaudiodata('maptype' . $cno)) {
                   if (str_replace("img","",urldecode(getaudiodata('maptype' . $cno))) != "") {
                     $bigais=urldecode(getaudiodata('maptype' . $cno));
                     $bs=explode("/",$bigais);
                     $ais=$bs[0];
                   } else {
                     $bigais=$ais . '/' . strtolower(str_replace(".","",$aext));
                   }
                 }
                 $mobilewav .= "<" . $ais . " id='au" . $cno . "' controls='controls' autobuffer='autobuffer'> ";
                 if (strpos(urldecode(getaudiodata('mapaudio' . $cno)), ";base64,") !== false) {
                 if (strpos(urldecode(getaudiodata('mapaudio' . $cno)), "data:;base64,") !== false) {
                 $mobilewav .= '    <source id="sc' . $cno . '" src="data:' . $bigais . substr(urldecode(getaudiodata('mapaudio' . $cno)),5) . '" /> ';
                 } else {
                 $mobilewav .= '    <source id="sc' . $cno . '" src="' . urldecode(getaudiodata('mapaudio' . $cno)) . '" /> ';
                 }
                 } else {
                 $mobilewav .= '    <source id="sc' . $cno . '" src="data:audio/x-wav;base64,' . getaudiodata('mapaudio' . $cno) . '" /> ';
                 }
                 $mobilewav .= "</" . $ais . "> ";
                 $mobilewav .= "<br>";
    		     $content = chunk_split(getaudiodata('mapaudio' . $cno));
    		     $fwav=$befdots[0] . "_" . $cno . $aext; //".wav";
    		     $headers .= "Content-Type: application/octet-stream; name=\"" . $fwav . "\"" . $eol;
    		     $headers .= "Content-Transfer-Encoding: base64" . $eol;
    		     $headers .= "Content-Disposition: attachment;filename=\"" . $fwav . "\"" . $eol;
    		     $headers .= $content . $eol . $eol;
    		     $headers .= "--" . $separator . $eol; //"--";
                }
                $cno++;
			  }
              $cno = $origcno;

    		  $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><div id="audiostuff"></div>';
    		  $somehtml .= '</body>';
    		  $somehtml .= '</html>';

              if (issetgetaudiodata('mapclick1') && issetgetaudiodata('mapcoords1') && issetgetaudiodata('mapaudio1')) {
              while (issetgetaudiodata('mapclick' . $cno) && issetgetaudiodata('mapcoords' . $cno) && issetgetaudiodata('mapaudio' . $cno)) {
                if (urldecode(getaudiodata('mapclick' . $cno)) != "" && urldecode(getaudiodata('mapaudio' . $cno)) == "" && urldecode(getaudiodata('mapcoords' . $cno)) != "") {
                 $somehtml = str_replace('<area shape', '<area shape="rect" alt="Navigate ' . $cno . '" title="Navigate ' . $cno . ' to ' . urldecode(getaudiodata('mapclick' . $cno)) . '" coords="' . urldecode(getaudiodata('mapcoords' . $cno)) . '" href="' . urldecode(getaudiodata('mapclick' . $cno)) . '" target="_blank" />' . '<area shape', $somehtml);
                } else if (urldecode(getaudiodata('mapclick' . $cno)) != "" && urldecode(getaudiodata('mapaudio' . $cno)) != "" && urldecode(getaudiodata('mapcoords' . $cno)) != "") {
                 $somehtml = str_replace('<area shape', '<area shape="rect" alt="Listen and Navigate ' . $cno . '" title="Listen ' . ' to ' . $befdots[0] . '_' . $cno . $aext . ' and Navigate ' . $cno . ' to ' . urldecode(getaudiodata('mapclick' . $cno)) . '" coords="' . urldecode(getaudiodata('mapcoords' . $cno)) . '" href="' . urldecode(getaudiodata('mapclick' . $cno)) . '" target="_blank" style="  display:block; border:3px solid pink; " onclick=" var auis=document.getElementById(' . "'" . "au" . $cno . "'" . '); if (auis != null) { auis.play(); } else { var px=document.URL.replace(' . "'" . '(1)' . "','" . '' . "'" . ').replace(' . "'" . '(2)' . "','" . '' . "'" . ').replace(' . "'" . '(3)' . "','" . '' . "'" . ').replace(' . "'" . '(4)' . "','" . '' . "'" . ').replace(' . "'" . '(5)' . "','" . '' . "'" . ').replace(' . "'mobile_" . $befdots[0] . '.htm' . "','" . $befdots[0] . '_' . $cno . $aext . '' . "'" . ').replace(' . "'" . $befdots[0] . '.html' . "','" . $befdots[0] . '_' . $cno . $aext . '' . "'" . '); window.open(px' . ",'_blank','top=50,left=50,height=100,width=300'" . ");" . ' } " />' . '<area shape', $somehtml);
                } else if (urldecode(getaudiodata('mapclick' . $cno)) == "" && urldecode(issetgetaudiodata('mapaudio' . $cno)) != "" && urldecode(getaudiodata('mapcoords' . $cno)) != "") {
                 $somehtml = str_replace('<area shape', '<area shape="rect" alt="Listen ' . $cno . '" title="Listen ' . $cno . ' to ' . $befdots[0] . '_' . $cno . $aext . '' . '" coords="' . urldecode(getaudiodata('mapcoords' . $cno)) . '" href="#" target="_blank" style=" display:block; border:2px solid yellow; " onclick=" var auis=document.getElementById(' . "'" . "au" . $cno . "'" . '); if (auis != null) { auis.play(); } else {  var px=document.URL.replace(' . "'" . '(1)' . "','" . '' . "'" . ').replace(' . "'" . '(2)' . "','" . '' . "'" . ').replace(' . "'" . '(3)' . "','" . '' . "'" . ').replace(' . "'" . '(4)' . "','" . '' . "'" . ').replace(' . "'" . '(5)' . "','" . '' . "'" . ').replace(' . "'mobile_" . $befdots[0] . '.htm' . "','" . $befdots[0] . '_' . $cno . $aext . '' . "'" . ').replace(' . "'" . $befdots[0] . '.html' . "','" . $befdots[0] . '_' . $cno . $aext . '' . "'" . '); window.open(px' . ",'_blank','top=50,left=50,height=100,width=300'" . ");" . ' } " />' . '<area shape', $somehtml);
                }
                $cno++;
			  }
              } else {
              while (issetgetaudiodata('mapclick' . $cno) && issetgetaudiodata('mapcoords' . $cno)) {
                if (urldecode(getaudiodata('mapclick' . $cno)) != "" && urldecode(getaudiodata('mapcoords' . $cno)) != "") {
                 $somehtml = str_replace('<area shape', '<area shape="rect" alt="Navigate ' . $cno . '" title="Navigate ' . $cno . ' to ' . urldecode(getaudiodata('mapclick' . $cno)) . '" coords="' . urldecode(getaudiodata('mapcoords' . $cno)) . '" href="' . urldecode(getaudiodata('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=\"" . $befdots[0] . ".html" . "\"" . $eol;
    		  $headers .= "Content-Transfer-Encoding: base64" . $eol;
    		  $headers .= "Content-Disposition: attachment;filename=\"" . $befdots[0] . ".html" . "\"" . $eol;
    		  $headers .= $content . $eol . $eol;
    		  if ('domobile' == 'domobile') {
    		    $headers .= "--" . $separator . $eol; //"--";
    		    $content=chunk_split(base64_encode(str_replace('style="zoom: 50%;" src="' . $filename,  'style="zoom: 50%;" src="data:image/' . $pngext . ';base64,' . base64_encode($contents), str_replace("</body>", $mobilewav . "</body>", $somehtml))));
    		    $filename="mobile_" . $befdots[0] . ".htm";
    		    $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 == '') {    // on fail
    		
    		//file_put_contents("aaa.aaa", "fail");
    		
          ourpremail($tem, $mysubject, $mybody, $headers);
        }
    }
    return "";
}

  if (isset($_GET['lookfor'])) {  // audio content
            while (!file_exists("../MarkItUp/html/audio/audio" . $_GET['lookfor'] . "_" . str_replace(":","",str_replace(".","",server_remote_addr())) . ".wav")) {
              sleep(5);
            }
            //sleep(2);
            $audioc = file_get_contents("../MarkItUp/html/audio/audio" . $_GET['lookfor'] . "_" . str_replace(":","",str_replace(".","",server_remote_addr())) . ".wav");
            unlink("../MarkItUp/html/audio/audio" . $_GET['lookfor'] . "_" . str_replace(":","",str_replace(".","",server_remote_addr())) . ".wav");
            echo "<html><head><sc" . "ript type='text/javascript'> parent.document.getElementById('mapaudio" . $_GET['lookfor'] . "').value='" . base64_encode($audioc) . "'; parent.document.getElementById('mapxaudio" . $_GET['lookfor'] . "').value='" . base64_encode($audioc) . "';  </sc" . "ript></head><body></body></html>";
            exit;
  } else {  // email work
  
            if ($altmydurl != "") {
    		 $mydurl = $altmydurl;
    		 $idata = $mydurl;
            } else {
            $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(explode("&from=",$mydurl)[0]);
    		}
    		}


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'])) {
            if ($altmydurl != "") {
    		 $mydurl = $altmydurl;
    		 $idata = $mydurl;
            } else {
            $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(explode("&from=",$mydurl)[0]);
    		}
    		}
    }
  }
  if (isset($_GET['mode'])) $gmode .= $_GET['mode'];
  if (isset($_POST['mode'])) $gmode .= $_POST['mode'];
       //file_put_contents("aaaaa.0aaa", $lworld . " " . $postfname);
  if (strlen(str_replace("*feedback_", "", ("*" . $postfname))) != strlen(("*" . $postfname))) {
   $tem = urldecode($_POST['to']);
       //file_put_contents("aaaaa.02aaa", $lworld . " " . $postfname);
   if (strlen(str_replace("." . $pngext . "*", "", ($postfname . "*"))) != strlen(($postfname . "*")) && strlen(str_replace("*" . $lworld . "_", "", ("*" . $postfname))) != strlen(("*" . $postfname))) {
       //file_put_contents("aaaaa.002aaa", $lworld . " " . $postfname);
    		//file_put_contents("aaa.aaa", "aaa.aaa");
    if ($mydurl != '') {
    $idata = base64_decode(explode("&from=",$mydurl)[0]);
    //file_put_contents(urldecode($postfname), explode("&from=",$mydurl)[0]);
    if (isset($_GET['showimage']) || isset($_POST['showimage'])) {
            if ($altmydurl != "") {
      $atendis="<html><body onload=\" top.document.getElementById('eimg').innerHTML='<img src=//www.rjmprogramming.com.au/PHP/animegif/" . server_remote_addr() . ".gif></img>'; \"></body></html>";      
            } else {
      $atendis="<html><body onload=\" top.document.getElementById('eimg').innerHTML='<img src=data:image/" . $pngext . ";base64," . explode("&from=",urldecode($_POST['mydurl']))[0] . "></img>'; \"></body></html>";      
			}
    }
    } else {
    $idata = base64_decode(explode("&from=",urldecode($_POST['mydurl']))[0]);
    //file_put_contents(urldecode($postfname), explode("&from=",($_POST['mydurl']))[0]);
    if (isset($_GET['showimage']) || isset($_POST['showimage'])) {
            if ($altmydurl != "") {
      $atendis="<html><body onload=\" top.document.getElementById('eimg').innerHTML='<img src=//www.rjmprogramming.com.au/PHP/animegif/" . server_remote_addr() . ".gif></img>'; \"></body></html>";      
            } else {
      $atendis="<html><body onload=\" top.document.getElementById('eimg').innerHTML='<img src=data:image/" . $pngext . ";base64," . explode("&from=",urldecode($_POST['mydurl']))[0] . "></img>'; \"></body></html>";      
			}
    }
    }
    // //file_put_contents($postfname, $idata);
    //file_put_contents(urldecode($postfname), $idata);
    		//file_put_contents("aaa.aaa", "");
    $mysubject = $postsubj;
    		//file_put_contents("aaa.aaa", urldecode($mysubject));
    $mybody = $postfname;
    		//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 . " " . $postfname);
   if (strlen(str_replace("." . $pngext . "*", "", ($postfname . "*"))) != strlen(($postfname . "*")) && strlen(str_replace("*" . $lworld . "_", "", ("*" . $postfname))) != strlen(("*" . $postfname))) {
    if ($altmydurl != '') {
    $idata = $altmydurl; //base64_decode(explode("&from=",$altmydurl)[0]);
    } else if ($mydurl != '') {
    $idata = base64_decode(explode("&from=",$mydurl)[0]);
    } else {
    $idata = base64_decode(explode("&from=",urldecode($_POST['mydurl']))[0]);
    }
    file_put_contents($postfname, $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 . " " . $postfname);
   if (strlen(str_replace("." . $pngext . "*", "", ($postfname . "*"))) != strlen(($postfname . "*")) && strlen(str_replace("*" . $lworld . "_", "", ("*" . $postfname))) != strlen(("*" . $postfname))) {
       //file_put_contents("aaaaa.002aaa", $lworld . " " . $postfname);
    		//file_put_contents("aaa.aaa", "aaa.aaa");
    if ($altmydurl != '') {
    $idata = $altmydurl; //base64_decode(explode("&from=",$altmydurl)[0]);
    } else if ($mydurl != '') {
    $idata = base64_decode(explode("&from=",$mydurl)[0]);
    } else {
    $idata = base64_decode(explode("&from=",urldecode($_POST['mydurl']))[0]);
    }
    //file_put_contents($postfname, $idata);
    		//file_put_contents("aaa.aaa", "");
    $mysubject = $postsubj;
    		//file_put_contents("aaa.aaa", urldecode($mysubject));
    $mybody = $postfname;
    		//file_put_contents("aaa.aaa", $_SERVER['REMOTE_ADDR']);
    send_email($tem, urldecode($mysubject), urldecode($mybody));
    if ($okay == 0) {
      echo "<html><head><scr" . "ipt type='text/javascript'> parent.document.getElementById('estatus').style.color='red'; parent.document.getElementById('estatus').innerHTML=' X '; </scr" . "ipt></head><body></body></html>"; 
    } else {
      echo "<html><head><scr" . "ipt type='text/javascript'> parent.document.getElementById('estatus').style.color='green'; parent.document.getElementById('estatus').innerHTML=' &#10003; '; </scr" . "ipt></head><body></body></html>"; 
    }
   } 
  }
}

}

?>
