<?php
// mylist.php
// Help out mylist.html with media and large data sets
// RJM Programming
// March, 2017

$types = array("audio/wav","audio/x-wav","audio/x-pn-realaudio","audio/x-mpegurl","audio/x-aiff","audio/mpeg","audio/mid",
      "audio/basic","audio/ogg","video/x-sgi-movie","video/x-msvideo","video/quicktime","audio/mp3","video/mp4","video/mpeg",
      "video/x-la-asf","video/ogg","video/webm","audio/mp4", "image/jpeg", "image/jpeg", "image/png", "image/gif", "image/bmp", "image/tif");  
$exts = array(".wav",".wav",".ram",".m3u",".aiff",".mp3",".rmi",
      ".snd",".ogg",".movie",".avi",".mov",".mp3",".m4v",".mpeg",
      ".lsx",".ogv",".webm",".m4a", ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tif"); 
$ourmime="audio/wav";
$udirname="";

function relative_to_absolute($inth) {
    global $udirname;
    //$low = strtolower($inth);
    $outth = $inth;
    $ideas = array(" href='", ' href="', " Href='", ' Href="', " HREF='", ' HREF="', " href=", " Href=", " HREF=", " src='", ' src="', " Src='", ' Src="', " SRC='", ' SRC="', " src=", " Src=", " SRC=");
    if ($udirname != "") {
     for ($m=0; $m<sizeof($ideas); $m++) {
      $huhs = explode($ideas[$m], $inth);
      if (sizeof($huhs) > 1) {
        for ($ii=(sizeof($huhs) - 1); $ii>=1; $ii--) {
          if (strtolower(str_replace("file", "http", strtolower(substr($huhs[$ii], 0, 4)))) == "http") {
            $outth = $outth;
          } else if (substr($huhs[$ii], 0, 1) != "/" && substr($huhs[$ii], 0, 1) != "'" && substr($huhs[$ii], 0, 1) != '"') {
            $outth = str_replace($huhs[$ii], $udirname . $huhs[$ii], $outth);
          } else if (substr($huhs[$ii], 1, 1) != "/" && substr($huhs[$ii], 0, 1) != "'" && substr($huhs[$ii], 0, 1) != '"') {
            $outth = str_replace($huhs[$ii], substr($udirname, 0, (strlen($udirname) - 1)) . $huhs[$ii], $outth);
          }
        }
      }
     }
    }
    return $outth;
}

if (isset($_GET['mediafile'])) {
  if (strpos(urldecode($_GET['mediafile']) . "?", ".xml?") !== false) $isxml = true;
  if ($isxml && urldecode($_GET['mediafile']) != "") $fb = " ... file=" . urldecode($_GET['mediafile']);
  $hparts = j_parseUrl(urldecode($_GET['mediafile']));
  $udirs = explode("/", urldecode($_GET['mediafile']));
  if ($hparts['authority'] == $hparts['host'] && $hparts['userinfo'] == "") $hparts['authority'] = "";
  if ($udirs[sizeof($udirs) - 1] == "") {
    $udirname = urldecode($_GET['mediafile']);
  } else if (strpos($hparts['path'], "/") !== false) {
    $udirname = $hparts['scheme'] . "://" . $hparts['userinfo'] . $hparts['authority'] . $hparts['host'] . $hparts['port'] . str_replace("//", "/", str_replace("/" . $udirs[sizeof($udirs) - 1], "/", $hparts['path']));
  } else {
    $udirname = $hparts['scheme'] . "://" . $hparts['userinfo'] . $hparts['authority'] . $hparts['host'] . $hparts['port'] . $hparts['path'] . "/";
  }
  //echo $udirname;
}


if (isset($_GET['mediafile'])) {
 if (strpos(urldecode($_GET['mediafile']),".") !== false) {  
  $parts=explode(".",urldecode($_GET['mediafile']));
  $uparts=explode("/",urldecode($_GET['mediafile']));
  for ($it=0; $it<sizeof($exts); $it++) {
    if ("." . $parts[-1 + sizeof($parts)] == $exts[$it]) $ourmime=$exts[$it];
  } 
  if ($ourmime == "") {
   $hhcont=@file_get_contents(urldecode($_GET['mediafile']));
   echo relative_to_absolute($hhcont);
   exit;
  } else {
   header('Content-Type: ' . $ourmime);
   if (isset($_GET['download'])) {
    header('Content-Disposition: attachment; filename="' . $uparts[-1 + sizeof($uparts)] . '"');
   } else {
    header('Content-Disposition: inline');
   }
   echo file_get_contents(urldecode($_GET['mediafile']));
   exit;
  }
 }
} else if (isset($_POST['all'])) {
  if (1 == 1) {
            $hcont=@file_get_contents("mylist.htm");
            $hcont=str_replace('locationsearch=location.search','locationsearch=("' . urldecode($_POST['all']) . '")', $hcont);
            //echo $hcont;
            $subject = urldecode($_POST['subject']);
            $to = urldecode($_POST['emailee']);
            $eol = PHP_EOL;
            $headers = 'From: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
            $headers .= 'Reply-To: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
      
            $filename="mylist.htm"; 
    		$content = chunk_split(base64_encode($hcont));
    		//$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 attachment 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 . "--";
    		
            mail($to, $subject, "", $headers);  
            echo "<html><body onload=' alert(\"Email sent.\"); if (1 == 2) { parent.document.getElementById(\"mytbody\").innerHTML=parent.inmytbody; parent.locationsearch=\"" . urldecode($_POST['all']) . "\";  parent.addlotsofrows(); } '></body></html>";
  } else {
            echo "<html><body onload=' parent.document.getElementById(\"mytbody\").innerHTML=parent.inmytbody; parent.locationsearch=\"" . urldecode($_POST['all']) . "\";  parent.addlotsofrows(); '></body></html>";
  }
} else if (isset($_GET['c'])) {
  echo "";
} else {
  $hcont=@file_get_contents("mylist.htm");
  echo $hcont;
}

?>
