<?php 
// legend_via_map.php
// RJM Programming
// October, 2016
// Return webpage contents
$cno = 0;

function oururlencode($conte) {
  return str_replace("+","%20",urlencode($conte));
}

function strposa($haystack, $needles=array(), $offset=0) { // thanks to http://stackoverflow.com/questions/6284553/using-an-array-as-needles-in-strpos
        $chr = array();
        foreach($needles as $needle) {
                $res = strpos($haystack, $needle, $offset);
                if ($res !== false) $chr[$needle] = $res;
        }
        if(empty($chr)) return false;
        return min($chr);
}

function getheaders($contents, $mybod) {
        global $cno;
        $mybods=explode(";", $mybod);
        $defone = ""; //"Please see or download image (first) before any download and double clicking of image clickable and applicable web application below:";
        $eol = PHP_EOL;
        $headers="";
        if (isset($_GET['from'])) {
        if ($_GET['from'] != '') {
        $headers = 'From: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
        $headers .= 'Reply-To: ' . urldecode($_GET['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($_GET['cc'])) {
        if ($_GET['CC'] != '') {
        $headers .= 'CC: ' . urldecode($_GET['cc']) . $eol;
        }
        }
        if (isset($_GET['bcc'])) {
        if ($_GET['BCC'] != '') {
        $headers .= 'BCC: ' . urldecode($_GET['bcc']) . $eol;
        }
        } 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;
        }
        }
        if ($contents != '') {
    	  date_default_timezone_set('Australia/Perth');
    	  // a random hash will be necessary to send mixed content
    	  $separator = md5(time());
          for ($icno=0; $icno<sizeof($mybods); $icno++) {
          
            if ($icno != 0) {
              $contents = @file_get_contents($mybods[$icno]);
            }

    		$fs = explode("/", $mybods[$icno]);
    		$filename = $fs[-1 + sizeof($fs)];
    		$content = chunk_split(base64_encode($contents));

    		$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 ($icno != 0) {
    		  $headers .= $defone . $eol . $eol;
    		} else {
    		  $headers .= "Please see attached audio file 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;
    		$cno++;
    	 }
       }
       return $headers;
}

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());
      }
    }
}

  $find_media = array('.wav', '.m4a', '.m4v', '.mp4', '.mp3', '.mp2', '.bmp', '.tif', '.png', '.jpeg', 'jpg', '.gif', '.ogg', '.mov', '.flv');
  $cont = "";
  $delim = "";

  if (isset($_POST['to']) && isset($_POST['urlbig']) && isset($_POST['subject'])) {
    $mybody=urldecode($_POST['urlbig']);
    $mysubject=urldecode($_POST['subject']);
    $myflist="";
    if (isset($_POST['url'])) {
    $f = strpos($_POST['url'], '%');
      if ($f === false) {
       if ($_POST['url'] != "") $cont=@file_get_contents(($_POST['url']));
       $myflist=($_POST['url']);
  if ($_POST['url'] != "") $delim = ";";
      } else {
       if ($_POST['url'] != "") $cont=@file_get_contents(urldecode($_POST['url']));
       $myflist=urldecode($_POST['url']);
  if ($_POST['url'] != "") $delim = ";";
      }
    }
    $myfiles=explode(";", $mybody);
    if (sizeof($myfiles) > 1) {
      for ($ii=1; $ii<sizeof($myfiles); $ii++) {
        $myone=explode("&", $myfiles[$ii]);
        if (strpos($myflist, $myone[0]) === false) {
          $myflist.=$delim .  $myone[0];
  $delim = ";";
          if ($cont == "") {
            $cont=@file_get_contents($myone[0]);
          }
        }
      }
    }
    if (isset($_POST['body'])) {
      $mysubject.=" " . urldecode($_POST['body']);
    } else {
      $mysubject.=" Please attach media file " . str_replace(";"," and ",str_replace("file://", "", $myflist));
    }
    if (strpos($_POST['to'], "@") === false || strlen($cont) == 0) {
    echo "<!doctype html><html><head><scr" . "ipt type='text/javascript'> function aemail(ame) { window.top.location = ame.href; } </scr" . "ipt></head><body onload='document.getElementById(\"aemail\").click();'><a id='aemail' onclick=' if (navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/Firefox/i)) { aemail(this); } ' href='mailto:" . urldecode($_POST['to']) . "?subject=" . oururlencode($mysubject) . "&body=" .  oururlencode($mybody) . "' title='Email' style='display:none;'></a></body></html>";
    } else {
    ourpremail(urldecode($_POST['to']), $mysubject, $mybody, getheaders($cont, $myflist));
    }

  } else if (isset($_GET['to']) && isset($_GET['urlbig']) && isset($_GET['subject'])) {
    $mybody=urldecode($_GET['urlbig']);
    $mysubject=urldecode($_GET['subject']);
    $myflist="";
    if (isset($_GET['url'])) {
    $f = strpos($_GET['url'], '%');
      if ($f === false) {
       if ($_GET['url'] != "") $cont=@file_get_contents(($_GET['url']));
       $myflist=($_GET['url']);
  if ($_GET['url'] != "") $delim = ";";
      } else {
       if ($_GET['url'] != "") $cont=@file_get_contents(urldecode($_GET['url']));
  if ($_GET['url'] != "") $delim = ";";
       $myflist=urldecode($_GET['url']);
      }
    }
    $myfiles=explode(";", $mybody);
    if (sizeof($myfiles) > 1) {
      for ($ii=1; $ii<sizeof($myfiles); $ii++) {
        $myone=explode("&", $myfiles[$ii]);
        if (strpos($myflist, $myone[0]) === false) {
          $myflist.=$delim .  $myone[0];
  $delim = ";";
          if ($cont == "") $cont=@file_get_contents($myone[0]);
        }
      }
    }
    if (isset($_GET['body'])) {
      $mysubject.=" " . urldecode($_GET['body']);
    } else {
      $mysubject.=" Please attach media file " . str_replace(";"," and ",str_replace("file://", "", $myflist));
    }
    if (strpos($_GET['to'], "@") === false || strlen($cont) == 0) {
    echo "<!doctype html><html><head><scr" . "ipt type='text/javascript'> function aemail(ame) { window.top.location = ame.href; } </scr" . "ipt></head><body onload='document.getElementById(\"aemail\").click();'><a id='aemail' onclick=' if (navigator.userAgent.match(/Android/i)) { aemail(this); } ' href='mailto:" . urldecode($_GET['to']) . "?subject=" . oururlencode($mysubject) . "&body=" .  oururlencode($mybody) . "' title='Email' style='display:none;'></a></body></html>";
    } else {
    ourpremail(urldecode($_GET['to']), $mysubject, $mybody, getheaders($cont, $myflist));
    }

  } else if (isset($_GET['url'])) {
    $f = strpos($_GET['url'], '%');
    if ($f === false) {
    if ((isset($_GET['to']) || strpos($_GET['url'], 'file:') === 0) && strpos($_GET['url'], '.wav') !== false) {
    $cont=file_get_contents($_GET['url']);
    if (isset($_GET['to']) && isset($_GET['urlbig']) && isset($_GET['subject'])) {
    $mybody=urldecode($_GET['urlbig']);
    $mysubject=urldecode($_GET['subject']);
    $myflist=$_GET['url'];
    $myfiles=explode(";", $mybody);
    if (sizeof($myfiles) > 1) {
      for ($ii=1; $ii<sizeof($myfiles); $ii++) {
        $myone=explode("&", $myfiles[$ii]);
        if (strpos($myflist, $myone[0]) === false) {
          $myflist.=";" .  $myone[0];
          if ($cont == "") $cont=@file_get_contents($myone[0]);
        }
      }
    }
    if (isset($_GET['body'])) {
      $mysubject.=" " . urldecode($_GET['body']);
    } else {
      $mysubject.=" Please attach media file " . str_replace(";"," and ",str_replace("file://", "", $myflist));
    }
    if (strpos($_GET['to'], "@") === false) {
    echo "<!doctype html><html><head><scr" . "ipt type='text/javascript'> function aemail(ame) { window.top.location = ame.href; } </scr" . "ipt></head><body onload='document.getElementById(\"aemail\").click();'><a id='aemail' onclick=' if (navigator.userAgent.match(/Android/i)) { aemail(this); } ' href='mailto:" . urldecode($_GET['to']) . "?subject=" . oururlencode($mysubject) . "&body=" .  oururlencode($mybody) . "' title='Email' style='display:none;'></a></body></html>";
    } else {
    ourpremail(urldecode($_GET['to']), $mysubject, $mybody, getheaders($cont, $myflist));
    }
    } else {
    header('Cache-Control: max-age=604800');
    header('Content-type: audio/x-wav');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . filesize($_GET['url']));    
    echo $cont;
    }
    } else {
    echo file_get_contents($_GET['url']);
    }
    } else if ($f < 7) {
    if ((isset($_GET['to']) || strpos($_GET['url'], 'file:') === 0) && strpos(urldecode($_GET['url']), '.wav') !== false) {
    $cont=file_get_contents(urldecode($_GET['url']));
    if (isset($_GET['to']) && isset($_GET['urlbig']) && isset($_GET['subject'])) {
    $mybody=urldecode($_GET['urlbig']);
    $mysubject=urldecode($_GET['subject']);
    $myflist=urldecode($_GET['url']);
    $myfiles=explode(";", $mybody);
    if (sizeof($myfiles) > 1) {
      for ($ii=1; $ii<sizeof($myfiles); $ii++) {
        $myone=explode("&", $myfiles[$ii]);
        if (strpos($myflist, $myone[0]) === false) {
          $myflist.=";" .  $myone[0];
          if ($cont == "") $cont=@file_get_contents($myone[0]);
        }
      }
    }
    if (isset($_GET['body'])) {
      $mysubject.=" " . urldecode($_GET['body']);
    } else {
      $mysubject.=" Please attach Audio File " . str_replace(";"," and ",str_replace("file://", "", $myflist));
    }
    if (strpos($_GET['to'], "@") === false) {
    echo "<!doctype html><html><head><scr" . "ipt type='text/javascript'> function aemail(ame) { window.top.location = ame.href; } </scr" . "ipt></head><body onload='document.getElementById(\"aemail\").click();'><a id='aemail' onclick=' if (navigator.userAgent.match(/Android/i)) { aemail(this); } ' href='mailto:" . urldecode($_GET['to']) . "?subject=" . oururlencode($mysubject) . "&body=" .  oururlencode($mybody) . "' title='Email' style='display:none;'></a></body></html>";
    } else {
    ourpremail(urldecode($_GET['to']), $mysubject, $mybody, getheaders($cont, $myflist));
    }
    } else {
    header('Cache-Control: max-age=604800');
    header('Content-type: audio/x-wav');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . filesize(urldecode($_GET['url'])));    
    echo $cont;
    }
    } else {
     echo file_get_contents(urldecode($_GET['url']));
    }
    } else {
    if ((isset($_GET['to']) || strpos($_GET['url'], 'file:') === 0) && strpos($_GET['url'], '.wav') !== false) {
    $cont=file_get_contents($_GET['url']);
    if (isset($_GET['to']) && isset($_GET['urlbig']) && isset($_GET['subject'])) {
    $mybody=urldecode($_GET['urlbig']);
    $mysubject=urldecode($_GET['subject']);
    $myflist=$_GET['url'];
    $myfiles=explode(";", $mybody);
    if (sizeof($myfiles) > 1) {
      for ($ii=1; $ii<sizeof($myfiles); $ii++) {
        $myone=explode("&", $myfiles[$ii]);
        if (strpos($myflist, $myone[0]) === false) {
          $myflist.=";" .  $myone[0];
          if ($cont == "") $cont=@file_get_contents($myone[0]);
        }
      }
    }
    if (isset($_GET['body'])) {
      $mysubject.=" " . urldecode($_GET['body']);
    } else {
      $mysubject.=" Please attach Audio File " . str_replace(";"," and ",str_replace("file://", "", $myflist));
    }
    if (strpos($_GET['to'], "@") === false) {
    echo "<!doctype html><html><head><scr" . "ipt type='text/javascript'> function aemail(ame) { window.top.location = ame.href; } </scr" . "ipt></head><body onload='document.getElementById(\"aemail\").click();'><a id='aemail' onclick=' if (navigator.userAgent.match(/Android/i)) { aemail(this); } ' href='mailto:" . urldecode($_GET['to']) . "?subject=" . oururlencode($mysubject) . "&body=" .  oururlencode($mybody) . "' title='Email' style='display:none;'></a></body></html>";
    } else {
    ourpremail(urldecode($_GET['to']), $mysubject, $mybody, getheaders($cont, $myflist));
    }
    } else {
    header('Cache-Control: max-age=604800');
    header('Content-type: audio/x-wav');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . filesize($_GET['url']));    
    echo $cont;
    }
    } else {
    echo file_get_contents($_GET['url']);
    }
    }
  } else {
    echo "";
  }
  
?>
