<?php
// data_uri_yql.php
// Yahoo Yql data.uri Tutorial - RJM Programming - 2015
// Thanks to https://developer.yahoo.com/yql/console/#h=select+*+from+data.uri+where+url%3D%22http%3A%2F%2Fwww.rjmprogramming.com.au%2FMyBusinessUnidad%2FWelcome_files%2Flogo.jpg%22

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

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

$firstbits = "<!doctype html>\n<html>\n<head>\n<title>Yahoo Yql data.uri Primer Tutorial</title></head>\n<body style='background-color: lightgray;'>\n";
$erroneousbits = "<h1 align='center'>Sorry, but " . urldecode($selname) . " is too big a media file to display.</h1><br>";
$formbits = "<h2 align='center'>Yahoo Yql data.uri Url</h2><br><div align='center'>\n<form action='./data_uri_yql.php' method='POST'>Url of media file: <input type='text' name='selname' id='selname' style='width:70%;' value='http://www.rjmprogramming.com.au/MyBusinessUnidad/Welcome_files/logo.jpg'></input><br><br><input type='submit' value='Show'></input></form></div></body></html>";

if ($selname != "") {
 		$pageContent = file_get_contents("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20data.uri%20where%20url%3D%22" . $selname . "%22&format=json&diagnostics=true&callback="); 
 		$json_output = json_decode($pageContent);  // .true

        // Confirm that results were returned before parsing
        if(!is_null($json_output->query->results)){
            foreach($json_output->query->results as $event){
                if (strpos($event, "data:") !== false) {
                  $things = explode("/", urldecode($selname));
                  $sbits = "<script type='text/javascript'>\n";
                  $sbits .= " var wo=null;\n";
                  $sbits .= " function wopen() {\n";
                  $sbits .= "   wo = window.open('', '" . $things[sizeof($things) - 1] . "', 'top=400,left=400,width=200,height=200,resizable=yes');\n";
                  $sbits .= "   wo.document.write('<img src=\"" . $event . "\" />');\n";
                  $sbits .= " }\n";
                  $sbits .= "</script>\n";
                  $firstbits = str_replace("<body", "<body onload='wopen();'", $firstbits);
                  $firstbits = str_replace("</head>", $sbits . "</head>", $firstbits);
                  echo $firstbits;
                  echo "<div align='center'><table align='center'><tr><td><img src='" . $event . "' /><br>" . urldecode($selname) . " looks like above.</td></tr></table></div>\n";
                  $firstbits = "";
                  $erroneousbits = "";
                } else if (strpos($event, "26049") !== false) {
                  $erroneousbits = "<h1 align='center'>Sorry, but " . urldecode($selname) . " is not an existant media file.</h1><br>";
                }
            }
        }
} else {
		$erroneousbits = "";
}

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

