<?php
 // find_images_via_size.php
 // RJM Programming
 // October, 2019
set_time_limit(2400);

$isIE=false;
$detpref="<DETAILS><SUMMARY></SUMMARY>";
$detsuff="</DETAILS>";
  if (!isset($argc)) {
$arr_browsers = ["MSIE", "Edge"]; // thanks to https://artisansweb.net/detect-browser-php-javascript/
$agent = $_SERVER['HTTP_USER_AGENT'];
foreach ($arr_browsers as $browser) {
 if (strpos($agent, $browser) !== false) {
  $detpref="";
  $detsuff="";
        $isIE=true;
            break;
 }
}
  } else {
  $detpref="";
  $detsuff="";
  }

function valit($inname, $indef) { 
  global $scriptis, $detpref, $detsuff, $formis;
  if (!isset($argc)) {
    if (isset($_GET[$inname])) {
      if ($formis == "" && $detpref == strtoupper($detpref) && $detpref != "") { $detpref=strtolower($detpref);  $detsuff=strtolower($detsuff);  $formis=str_replace("</summary>", "Image Find Parameters</summary>",$detpref);   if ($scriptis == "") {  $scriptis="<scri" . "pt type='text/javascript'>   </scr" . "ipt><hr>" . strtolower($detsuff);  }  } else { $detpref=""; $detsuff=""; }
      if ($inname == "wo" || $inname == "ho" || $inname == "wsort" || $inname == "hsort" || $inname == "fsort") {
      if ($scriptis == "") {  $scriptis="<scri" . "pt type='text/javascript'>   </scr" . "ipt>" . strtolower($detsuff);  }
      $scriptis=str_replace("</scr", " document.getElementById('" . $inname . "').value='" . str_replace("+"," ",urldecode($_GET[$inname])) . "'; </scr", $scriptis);
      return $indef;
      } else if ($inname == "wv" || $inname == "hv") {
      return explode("e",explode("v",explode("%",explode("p",strtolower(str_replace("+"," ",urldecode($_GET[$inname]))))[0])[0])[0])[0];
      } else {
      return str_replace("+"," ",urldecode($_GET[$inname]));
      }
    }
  }
  return $indef;
}

function huhit($ct) {
  $bits=explode("\n", $ct);
  $retct="";
  for ($ir=0; $ir<sizeof($bits); $ir++) {
    $subbits=explode("<a", $bits[$ir]);
    if (sizeof($subbits) > 1) {
      $subsub=explode("href=", $subbits[1]);
      $retct.=$subbits[0] . "</td><TD\tid=t" . $ir . "><a\tID=a" . $ir . "\tonmouseover=\"document.body.style.background='linear-gradient(rgba(255,255,255,0.8),rgba(255,255,255,0.8)),URL('+this.href+')'+String.fromCharCode(9)+'repeat-y';\"\t" . "onmouseout=\"document.body.style.background='';\"\t" . str_replace(" ","\t",str_replace("/./", "/", str_replace("a target", "a\ttarget", str_replace("blank title", "blank\ttitle", str_replace("img style", "img\tstyle", str_replace("; src", ";\tsrc", str_replace("\" ", "\"\t", $subsub[0]))))))) . "href=" .  str_replace(" ","%20",str_replace("/./", "/", str_replace("a target", "a\ttarget", str_replace("img style", "img\tstyle", str_replace("; src", ";\tsrc", str_replace("blank title", "blank\ttitle", str_replace("\" ", "\"\t", $subsub[1]))))))) . "\n";
    } else {
      $retct.=$bits[$ir] . "\n";
    }
  }
  return $retct; //str_replace("/./", "/", str_replace("a target", "a\ttarget", str_replace("blank title", "blank\ttitle", str_replace("\" ", "\"\t", $ct))));
}

function mappath($inp) {
   if (substr(($inp . " "),0,1) == '.') {
     if (PHP_OS=='WINNT' || PHP_OS=='WIN32' || PHP_OS=='Windows') {
      return dirname(__FILE__) . substr("\\",0,1) . $inp;
     } else {
      return dirname(__FILE__) . "/" . $inp;
     }
   }
   return $inp;
}

$sortbit="";
if (isset($_GET['wsort'])) { $sortbit=str_replace("+"," ",urldecode($_GET['wsort'])); } //" | sort ";
if (isset($_GET['hsort'])) { if ($sortbit == "") { $sortbit=str_replace("+"," ",urldecode($_GET['hsort'])); } else { $sortbit.=str_replace("+"," ",urldecode($_GET['hsort']));  } } //" | sort ";
if (isset($_GET['fsort'])) { if ($sortbit == "") { $sortbit=str_replace("+"," ",urldecode($_GET['fsort'])); } else { $sortbit.=str_replace("+"," ",urldecode($_GET['fsort']));  } } //" | sort ";
$scriptis="";
$formis="";
$datais="<tr><th><input type=text name=path value='" . valit('path','.') . "'></input></th><th><input type=text name=filespec value='" . valit('filespec','*.*g*') . "'></input></th><th><select id=wo name=wo><option value='" . valit('wo','==') . "'>=</option><option value='<'>&lt;</option><option value='>'>&gt;</option><option value='<='>&lt;=</option><option value='>='>&gt;=</option><option value='!='>!=</option></select></th><th><input name=wv value='" . valit('wv','500') . "'></input></th><th><select id=ho name=ho><option value='" . valit('ho','==') . "'>=</option><option value='<'>&lt;</option><option value='>'>&gt;</option><option value='<='>&lt;=</option><option value='>='>&gt;=</option><option value='!='>!=</option></select></th><th><input name=hv value='" . valit('hv','500') . "'></input></th></tr>";
$formis.="<form onsubmit=\"document.getElementById('myh1').style.cursor='progress'; document.getElementById('mysub').style.cursor='progress'; this.style.cursor='progress'; return true;\" method=GET action=./find_images_via_size.php><table id=mytable border=50><tr><th>Relative Path</th><th>Filespec <select style=display:inline-block; id=fsort name=fsort><option value='" . valit('fsort','') . "'>Value</option><option value=' | sort -k 4 '>Ascending Value</option><option value=' | sort -r -k 4 '>Descending Value</option></select></th><th>Width Operator</th><th>Width <select style=display:inline-block; id=wsort name=wsort><option value='" . valit('wsort','') . "'>Value (px)</option><option value=' | sort -n -k 1 '>Ascending Value</option><option value=' | sort -r -n -k 1 '>Descending Value</option></select></th><th>Height Operator</th><th>Height <select style=display:inline-block; id=hsort name=hsort><option value='" . valit('hsort','') . "'>Value (px)</option><option value=' | sort -n -k 3 '>Ascending Value</option><option value=' | sort -r -n -k 3 '>Descending Value</option></select></th></tr>" . $datais . "</table><br><br><input style='background-color:yellow;font-size:20px;' id=mysub type=submit value=List></input><input style='position:absolute;top:-200px;left:-200px;' type=text value=''></input></form>";
$formis.=$scriptis; 
$htmlis="<!doctype html><html><head><title>Find Images Via Size - RJM Programming - October, 2019</title></head><body><h1 id=myh1>Find Images Via Size <a onclick=\"location.href='#mytable';\" style=\"text-decoration:none;cursor:pointer;\">&#11015;</a> RJM Programming - October, 2019</h1>" . $formis . "<p></p></body></html>";
$lastsed="";
     if (PHP_OS=='WINNT' || PHP_OS=='WIN32' || PHP_OS=='Windows') {
     $lastsed=" | sed 's/\\(\\ \\)\\(\\.[^\\<]*\\)/\\ \\<a target=_blank title=\"" . str_replace("/","\\/",dirname(__FILE__) . substr("\\",0,1)) . "\\2\" href=\"\\2\"\\>\\<img style=height:60px; src=\"\\2\"\\>\\<\\/img\\>\\2\\<\\/a\\>/g' ";
     } else {
     $lastsed=" | sed 's/\\(\\ \\)\\(\\.[^\\<]*\\)/\\ \\<a target=_blank title=\"" . str_replace("/","\\/",dirname(__FILE__) . "/") . "\\2\" href=\"\\2\"\\>\\<img style=height:60px; src=\"\\2\"\\>\\<\\/img\\>\\2\\<\\/a\\>/g' ";
     }

// | sed '/\\\\ \\\\.[^\\\\<]*/s//<a target=_blank title=\"" . dirname(__FILE__) . substr("\\\\",0,1) . "\\\\1\" href=\"\\\\1\">\\\\1</a>/g' 
// | sed '/\\\\(\\\\ \\\\)\\\\(\\\\.[^\\\\<]*\\\\)/s// <a target=_blank title=\"" . dirname(__FILE__) . substr("\\\\",0,1) . "\\\\2\" href=\"\\\\2\"\\\\>\\\\2\\\\<\\\\/a\\\\>/g' 
// ksh -c "echo '123 x 567 ./x.x' | sed '/\\(\\ \\)\\(\\.[^\\<]*\\)/s// <a target=_blank title=\"" . dirname(__FILE__) . substr("\\",0,1) . "\\2\" href=\"\\2\"\\>\\2\\<\\/a\\>/g' "
 
if (isset($_GET['filespec'])) {
                                        //$cf="find " . (str_replace("+"," ",$_GET['path'])) . " -name '" . str_replace("+"," ",$_GET['filespec']) . "' -exec file {} \; | grep ' image data' | sed 's/JFIF standard 1.01, aspect ratio, //g' | sed 's/\(." . explode(".",str_replace("+"," ",$_GET['filespec']))[1] . "\): .* \([0-9]* x [0-9]*\).*/\\2 \\1/' | awk 'int($1) " . str_replace("+"," ",$_GET['wo']) . " " . str_replace("+"," ",$_GET['wv']) . " && int($3) " . str_replace("+"," ",$_GET['ho']) . " " . str_replace("+"," ",$_GET['hv']) . " {print}' | sed 's/\\(\\ \\)\\(\\.[^\\<]*\\)/\\ \\<a target=_blank title=\"" . str_replace("/","\\/",dirname(__FILE__) . "/") . "\\2\" href=\"\\2\"\\>\\2\\<\\/a\\>/g'";
   //echo 'cd ' . dirname(__FILE__) . '; ' . $cf . '';
     if (PHP_OS=='WINNT' || PHP_OS=='WIN32' || PHP_OS=='Windows') {
                                        $cf="find " . (str_replace("+"," ",$_GET['path'])) . " -name '" . str_replace("+"," ",$_GET['filespec']) . "' -exec file {} \;  | sed '/\ precision\ [0-9]*\, /s//`/g' | grep ' image data' | sed 's/\(JFIF [^\`]*\)`\([0-9]*\)x\([0-9]*\)/\\2 x \\3/g' | sed 's/\(." . explode(".",str_replace("+"," ",$_GET['filespec']))[1] . "\): .* \([0-9]* x [0-9]*\).*/\\2 \\1/' | awk 'int($1) " . str_replace("+"," ",$_GET['wo']) . " " . valit('wv',str_replace("+"," ",$_GET['wv'])) . " && int($3) " . str_replace("+"," ",$_GET['ho']) . " " . valit('hv',str_replace("+"," ",$_GET['hv'])) . " {print}' " . $sortbit . $lastsed . " > " . dirname(__FILE__) . substr("\\",0,1) . "find_images_via_size.txt 2> " . dirname(__FILE__) . substr("\\",0,1) . "find_images_via_size.err";
  exec('cd ' . dirname(__FILE__) . '; ' . $cf . ''); //find ' . (str_replace("+"," ",$_GET['path'])) . " -name '" . str_replace("+"," ",$_GET['filespec']) . "' -exec file {} \\; | grep ' image data' | sed 's/\\(." . explode(".",str_replace("+"," ",$_GET['filespec']))[1] . "\\): .* \\([0-9]* x [0-9]*\\).*/\\2 \\1/' | awk 'int($1) " . str_replace("+"," ",$_GET['wo']) . " " . str_replace("+"," ",$_GET['wv']) . " && int($3) " . str_replace("+"," ",$_GET['ho']) . " " . str_replace("+"," ",$_GET['hv']) . " {print}' | sed 's/\\(\\ \\)\\(\\.[^\\<]*\\)/\\ \\<a target=_blank title=\"" . str_replace("/","\\/",dirname(__FILE__) . substr("\\",0,1)) . "\\2\" href=\"\\2\"\\>\\2\\<\\/a\\>/g'  > " . dirname(__FILE__) . substr("\\",0,1) . "find_images_via_size.txt 2> " . dirname(__FILE__) . substr("\\",0,1) . "find_images_via_size.err");
     } else {
     //  | sed '/\\\\ \\\\.[^\\\\<]*/s/<a target=_blank title=\"" . dirname(__FILE__) . "/" . "\\\\1\" href=\"\\\\1\">\\\\1</a>/g'
                                        $cf="find " . (str_replace("+"," ",$_GET['path'])) . " -name '" . str_replace("+"," ",$_GET['filespec']) . "' -exec file {} \;  | sed '/\ precision\ [0-9]*\, /s//`/g' | grep ' image data' | sed 's/\(JFIF [^\`]*\)`\([0-9]*\)x\([0-9]*\)/\\2 x \\3/g' | sed 's/\(." . explode(".",str_replace("+"," ",$_GET['filespec']))[1] . "\): .* \([0-9]* x [0-9]*\).*/\\2 \\1/' | awk 'int($1) " . str_replace("+"," ",$_GET['wo']) . " " . valit('wv',str_replace("+"," ",$_GET['wv'])) . " && int($3) " . str_replace("+"," ",$_GET['ho']) . " " . valit('hv',str_replace("+"," ",$_GET['hv'])) . " {print}' " . $sortbit . $lastsed . " > " . dirname(__FILE__) . "/" . "find_images_via_size.txt 2> " . dirname(__FILE__) . "/" . "find_images_via_size.err";
   //echo 'cd ' . dirname(__FILE__) . '; ' . str_replace("<","&lt;",str_replace(">","&gt;",$cf)) . '';
   //exit;
  exec('cd ' . dirname(__FILE__) . '; ' . $cf . ''); //find ' . (str_replace("+"," ",$_GET['path'])) . " -name '" . str_replace("+"," ",$_GET['filespec']) . "' -exec file {} \\; | grep ' image data' | sed 's/JFIF standard 1.01, aspect ratio, //g' | sed 's/\\(." . explode(".",str_replace("+"," ",$_GET['filespec']))[1] . "\\): .* \\([0-9]* x [0-9]*\\).*/\\2 \\1/' | awk 'int($1) " . str_replace("+"," ",$_GET['wo']) . " " . str_replace("+"," ",$_GET['wv']) . " && int($3) " . str_replace("+"," ",$_GET['ho']) . " " . str_replace("+"," ",$_GET['hv']) . " {print}' | sed 's/\\(\\ \\)\\(\\.[^\\<]*\\)/\\ \\<a target=_blank title=\"" . str_replace("/","\\/",dirname(__FILE__) . "/") . "\\2\" href=\"\\2\"\\>\\2\\<\\/a\\>/g'  > " . dirname(__FILE__) . "/" . "find_images_via_size.txt 2> " . dirname(__FILE__) . "/" . "find_images_via_size.err");
     }
  // //passthru("find " . str_replace("+"," ",$_GET['path']) . " -name '" . str_replace("+"," ",$_GET['filespec']) . "' -exec file {} \; | sed 's/\(." . explode(".",str_replace("+"," ",$_GET['filespec']))[1] . "\): .* \([0-9]* x [0-9]*\).*/\2 \1/' | awk 'int($1) " . str_replace("+"," ",$_GET['wo']) . " " . str_replace("+"," ",$_GET['wv']) . " && int($3) " . str_replace("+"," ",$_GET['ho']) . " " . str_replace("+"," ",$_GET['hv']) . " {print}'");
  echo explode("</p>", $htmlis)[0] . "<table border=2><tr><th>Width (px)</th><th></th><th>Height (px)</th><th></th><th>Image</th></tr><tr><td>" . str_replace(" ","</td><td>",str_replace("\n","</TD></tr><tr><td>",huhit(file_get_contents("find_images_via_size.txt")))) . "</td><td colspan=4></td></tr></table>" . "</p>" . explode("</p>", $htmlis)[1];
  // //echo file_get_contents("find_images_via_size.txt");
} else {
  echo $htmlis;
}
exit;