<?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
// Pdf ideas thanks to http://php.net/manual/en/imagick.setup.php

$convertpath = "/opt/local/bin/";

function topdf($image, $inpdff) {
  global $convertpath;
  $things = explode(".", $inpdff);
  $pdff = str_replace("." . $things[sizeof($things) - 1], ".pdf", $inpdff);
  $fhuh = file_get_contents($image);
  $icont = file_put_contents($inpdff, $fhuh);
  exec($convertpath . "convert " . $inpdff . " " . $pdff);
  if (1 == 2) { // ideas from http://php.net/manual/en/imagick.examples-1.php
   $img = new Imagick($image);
   $img->setImageFormat('pdf');
   $success = $img->writeImage($inpdff);
   return file_get_contents($inpdff);
  }
  return $pdff;
}

$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 Image'></input>&nbsp;<input type='submit' value='Show Image as Pdf' name='pdf' id='pdf'></input></form></div></body></html>";
$suffix = "";

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=400,height=250,resizable=yes');\n";
                  if (isset($_POST['pdf']) || isset($_GET['pdf'])) {
                    $suffix = " in PDF format";
                    $fpdf = topdf(urldecode($selname), $things[sizeof($things) - 1]);
                    $sbits .= "   wo.document.write('<embed style=\" width:100%;\" type=\"application/pdf\" src=\"http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/" . str_replace("/data_uri_yql.php", "", $_SERVER['REQUEST_URI']) . "/" . $fpdf . "\" />');\n";
                  } else {
                    $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;
                  if (isset($_POST['pdf']) || isset($_GET['pdf'])) {
                    $suffix = " in PDF format";
                    $fpdf = topdf(urldecode($selname), $things[sizeof($things) - 1]);
                    echo "<div align='center'><table align='center'><tr><td><embed style=' width:100%;' type='application/pdf' src='http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/" . str_replace("/data_uri_yql.php", "", $_SERVER['REQUEST_URI']) . "/" . $fpdf . "' /><br>" . urldecode($selname) . " looks like above" . $suffix . ".</td></tr></table></div>\n";
                  } else {
                    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;
?> 

