<?php
// spec_img.php
// RJM Programming
// October, 2016
// Look for an image spec in the hosting domain and glob it

 $imgurls="";
 $urlsq=str_replace('&', ',', $_SERVER['QUERY_STRING']) . ",";

 if (isset($_POST['imgurls'])) {
   if (strpos($_POST['imgurls'], "//") !== false) {
    $imgurls=str_replace(",~~","", str_replace("-,",",",($_POST['imgurls'] . ",~~")));
    $urlsq=$_POST['imgurls'] . ",";
   } else {
    $imgurls=str_replace(",~~","", str_replace("-,",",",urldecode($_POST['imgurls'] . ",~~")));
    $urlsq=urldecode($_POST['imgurls']) . ",";
   }
 } else if (isset($_GET['imgurls'])) {
   if (strpos($_GET['imgurls'], "//") !== false) {
    $imgurls=str_replace(",~~","", str_replace("-,",",",($_GET['imgurls'] . ",~~")));
   } else {
    $imgurls=str_replace(",~~","", str_replace("-,",",",urldecode($_GET['imgurls'] . ",~~")));
   }
 }

   $protistwo=explode("//", $imgurls);

   if (sizeof($protistwo) == 1) {

   if (isset($_POST['documentrooturl'])) {
    $urldomain=$_POST['documentrooturl'];
   } else if (isset($_GET['documentrooturl'])) {
    $urldomain=$_GET['documentrooturl'];
   } else {
    $urldomain="http://" . $_SERVER['SERVER_NAME'] . "/";
   }
   if (strpos($urldomain, "/") === false && strpos($urldomain, "\\") === false && strpos($urldomain, ":") === false) {
    $urldomain=urldecode($urldomain);
   }

     $imgurls=str_replace(",", ("," . $urldomain), ($urldomain . $imgurls)); 
     $protistwo=explode("//", $imgurls);
     $urlsq=str_replace("=", ("=" . $urldomain), str_replace(",", ("," . $urldomain), $urlsq)); 
   }

   if (isset($_POST['documentrooturl'])) {
    $urldomain=$_POST['documentrooturl'];
   } else if (isset($_GET['documentrooturl'])) {
    $urldomain=$_GET['documentrooturl'];
   } else {
    $urldomain=$protistwo[0] . "//" . $_SERVER['SERVER_NAME'] . "/";
   }
   if (strpos($urldomain, "/") === false && strpos($urldomain, "\\") === false && strpos($urldomain, ":") === false) {
    $urldomain=urldecode($urldomain);
   }


 if (isset($_POST['documentroot'])) {
  $changeto=$_POST['documentroot'];
 } else if (isset($_GET['documentroot'])) {
  $changeto=$_GET['documentroot'];
 } else {
  $changeto=$_SERVER['DOCUMENT_ROOT'];
 }
 if (strpos($changeto, "/") === false && strpos($changeto, "\\") === false && strpos($changeto, ":") === false) {
  $changeto=urldecode($changeto);
 }
 if (strpos($changeto, "/") !== false) $changeto = str_replace("//","/",($changeto . "/"));
 if (strpos($changeto, "\\") !== false) $changeto = str_replace("\\\\","\\",($changeto . "\\"));

 $htmlis="<!doctype html><html><body></body></html>";
 if ($imgurls != "") {
   $csvs = explode(",", $imgurls);
   for ($i=0; $i<sizeof($csvs); $i++) {
    if (strpos($csvs[$i], $urldomain) !== false) {
     foreach (glob(str_replace($urldomain, $changeto ,$csvs[$i])) as $filename) {
      if (strpos($urlsq, str_replace($changeto, $urldomain, $filename) . "-,") === false) {
       $htmlis=str_replace("</body>", "<img style='width:100%;' title='image from " . $urldomain . "' src='" . str_replace($changeto, $urldomain, $filename) . "'></img><br></body>", $htmlis);       
      }
     }
    }
   }
 }
 echo $htmlis;
?>


