<?php
// php_content_type.php
// PHP Content Type Tutorial 
// RJM Programming - August, 2015
// Thanks to http://www.sitepoint.com/web-foundations/mime-types-complete-list/
// http://www.rjmprogramming.com.au/Welcome_files/shapeimage_1.jpg

$selname = "";
if (isset($_POST['selname'])) $selname = $_POST['selname'];
if (isset($_GET['selname'])) $selname = $_GET['selname'];

$scrbits = "<script type='text/javascript'>\n var nextmode=\"\"; var addto=\"myixframe\"; var axfcnt=1; var maxrec=-1; var nextaim=-2; var cnt=-1; var nexturl=''; var nextrec=0; function testit() {\n var previt=document.getElementById(\"myixframe\"); it=previt; if (it == null && document.getElementById(\"selname\").value != \"\") {\n if (document.body.innerHTML.indexOf(\"i\" + \"frame\") == -1) {\n document.body.innerHTML=document.body.innerHTML.replace(\"<div \", \"<div align=center id=mydiv2><if\" + \"rame width=400 height=400 style=background-color:lightgreen id=myixframe src=./php_content_type.php?selname=\" + encodeURIComponent(document.getElementById(\"selname\").value) + \"; title=i\" + \"frame></i\" + \"frame></div><div \");\n }\n document.getElementById(\"myixframe\").src=\"./php_content_type.php?selname=\" + encodeURIComponent(document.getElementById(\"selname\").value);\n  it=document.getElementById(\"myixframe\");\n }\n

 if (previt == null) {
   nextrec=2;
   var mydiv=document.getElementById(\"mydiv\"); 
   var mytable=\"<br><br><table style='background-color: pink;' id='mytable'><tbody id='mytbody'><tr><th>Delay in Seconds</th><th>Overlay or Add</th><th style='width:70%;'>Url</th></tr><tr id='tr1'><td><input id='td1_2' type='number' value='5'></input></td><td><select id='td1_3'><option value='Overlay'>Overlay</option></select></td><td style='width:70%;'><input style='width:92%;' id='td1_1' type='url' value='" . urldecode($selname) . "'></input></td></tr><tr id='tr2'></td><td><input id='td2_2' type='number' value='5'></input></td><td><select id='td2_3'><option value='Overlay'>Overlay</option><option value='Add'>Add</option></select></td><td style='width:70%;'><input onblur='addit(this.value, this.id);' style='width:92%;' id='td2_1' type='url' value=''></input></tr></tbody></table><div id='d3' align='center'></div>\";
   for (var iyt=3; iyt<75; iyt++) {
     mytable=mytable.replace(\"</tbody>\", \"<tr style='visibility:hidden;' id='tr\" + iyt + \"'></td><td><input id='td\" + iyt + \"_2' type='number' value='5'></input></td><td><select id='td\" + iyt + \"_3'><option value='Overlay'>Overlay</option><option value='Add'>Add</option></select></td><td style='width:70%;'><input onblur='addit(this.value, this.id);' style='width:92%;' id='td\" + iyt + \"_1' type='url' value=''></input></tr>\" + \"</tbody>\");
   }
   mydiv.innerHTML+=mytable;
   document.getElementById(\"td1_1\").value=document.getElementById(\"selname\").value;
 }

 }
 
 function lookoutfor() {
   if (nexturl != '') {
    if (nextaim < 0) {
      cnt=-1;
      nexturl=document.getElementById(\"td\" + nextrec + \"_1\").value;
      nextaim=document.getElementById(\"td\" + nextrec + \"_2\").value;
      nextmode=document.getElementById(\"td\" + nextrec + \"_3\").value;
    }
    cnt=eval(1 + cnt);
    if (cnt == nextaim) {
      if (nextmode == 'Add') {
        axfcnt=eval(1 + axfcnt);
        addto=\"myixframe\";
        if (axfcnt > 1) addto=\"myixframe\" + axfcnt;
        //alert(addto);
      }
      if (document.getElementById(addto)) {
        document.getElementById(addto).src=nexturl;
      } else {
        //axfcnt=eval(1 + axfcnt);
        //addto=\"myixframe\" + axfcnt;
        //alert(addto);
        document.getElementById(\"mydiv2\").innerHTML+=\"<if\" + \"rame width=400 height=400 style=background-color:lightgreen id=myixframe\" + axfcnt + \" src=./php_content_type.php?selname=\" + encodeURIComponent(nexturl) + \"; title=i\" + \"frame></i\" + \"frame>\";
      }
      nextaim=-2;
      cnt=-1;
      nextrec=eval(eval(nextrec % maxrec) + 1);
      if (nextrec == 1) {
        axfcnt=1;
        addto=\"myixframe\";
      }
    }
   }
   setTimeout(lookoutfor, 1000);
 }
 
 function addit(tval, tid) {
   if (nexturl == '') {
     nexturl=tval;
   }
   if (nexturl != '' && maxrec < 0) {
     maxrec=2;
     lookoutfor();
   } else {
     maxrec=eval(tid.replace('td','').replace('_1',''));
   }
   if (nexturl != '') {
    document.getElementById(\"tr\" + eval(1 + maxrec)).style.visibility='visible';
   }
 }
 
 \n </script>";

if (strpos($selname, "/") !== false) $selname = urlencode($selname);

$stybits = "<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>";
$firstbits = "<!doctype html>\n<html>\n<head>\n<title>PHP Content Type Primer Tutorial</title>" . $stybits . $scrbits . "</head>\n<body style='background-color: lightblue;'>\n";
$erroneousbits = "<h1 align='center'>Sorry, but " . urldecode($selname) . " is too big a media file to display.</h1><br>";
$formbits = "<h1 align='center'>PHP Content Type Url</h1><br><div id='mydiv' align='center'>\n<form target='_blank' action='./php_content_type.php' method='POST'>Url: <input type='url' name='selname' id='selname' style='width:70%;' value='http://www.rjmprogramming.com.au/MyBusinessUnidad/Welcome_files/logo.jpg'></input><br><br><input onclick=' if (document.getElementById(\"selname\").value != \"\") { window.open(document.getElementById(\"selname\").value, \"_blank\");  } ' type='button' value='Navigate via Web Browser'></input>&nbsp;<input type='submit' title='Would prefer http://www.rjmprogramming.com.au URL please' value='Show via PHP Content Type to New Webpage' name='pdf' id='pdf'></input>&nbsp;\n<input type='button' onclick=' testit(); ' title='Would prefer http://www.rjmprogramming.com.au URL please' value='Show PHP Content Type Below (as first of many, perhaps)' id='viaixframe'>\n</input></form></div></body></html>";
$suffix = "";

if ($selname != "") {
        $prefix = "application/";
        $qbits = explode("?", urldecode($selname));
        $otherbits = explode(".", $qbits[0]);
        //echo urldecode($selname);
        
        $firstbits = "";
        $erroneousbits = "";
        $formbits = "";
        
        // Thanks to http://www.sitepoint.com/web-foundations/mime-types-complete-list/
        if (!file_exists("our_mime.list")) file_put_contents("our_mime.list", file_get_contents("http://www.sitepoint.com/web-foundations/mime-types-complete-list/"));
        $mimelist = file_get_contents("our_mime.list");
 
        // Mime type considerations below ...
 
        $ct = "";
        $cells = explode("</td>", $mimelist);
        
        for ($ii=0; $ii<(sizeof($cells) - 1); $ii+=2) {
          $precells = explode(">", $cells[$ii]);
          $extis = strtolower(str_replace(".", "", $precells[sizeof($precells) - 1]));
          if ($ct == "" && strtolower($otherbits[sizeof($otherbits) - 1]) == $extis) {
           $precells = explode(">", $cells[1 + $ii]);
           $ct = $precells[sizeof($precells) - 1];
          }
        }

        if ($ct == "") {
         if (strpos(strtolower(urldecode($selname)), ".") === false) {
          $ct = "text/html";
         } else {
 
          if (strtolower($otherbits[sizeof($otherbits) - 1]) == "m4v") {
            $prefix = "video/";
            $otherbits[sizeof($otherbits) - 1] = "mpeg";
          }
          if (strtolower($otherbits[sizeof($otherbits) - 1]) == "m4a") {
            $prefix = "audio/";
            $otherbits[sizeof($otherbits) - 1] = "mpeg";
          }

          if (strtolower($otherbits[sizeof($otherbits) - 1]) == "html") $prefix = "text/";
          if (strtolower($otherbits[sizeof($otherbits) - 1]) == "htm") $prefix = "text/";
        
          if (strtolower($otherbits[sizeof($otherbits) - 1]) == "jpg") $prefix = "image/";
          if (strtolower($otherbits[sizeof($otherbits) - 1]) == "jpeg") $prefix = "image/";
          if (strtolower($otherbits[sizeof($otherbits) - 1]) == "gif") $prefix = "image/";
          if (strtolower($otherbits[sizeof($otherbits) - 1]) == "bmp") $prefix = "image/";
          if (strtolower($otherbits[sizeof($otherbits) - 1]) == "tif") $prefix = "image/";
          if (strtolower($otherbits[sizeof($otherbits) - 1]) == "tiff") $prefix = "image/";
          if (strtolower($otherbits[sizeof($otherbits) - 1]) == "png") $prefix = "image/";
          $ct = $prefix . strtolower($otherbits[sizeof($otherbits) - 1]);
         }
        }
        //echo $ct;
        header('Content-Type: ' . $ct);
        header('Content-Disposition: inline');
        echo file_get_contents(urldecode($selname));
        
} else {
		$erroneousbits = "";
}

echo $firstbits;
echo $erroneousbits;
echo $formbits;
?> 

