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

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] . "<a" . str_replace(" ","\t",str_replace("/./", "/", str_replace("a target", "a\ttarget", str_replace("blank title", "blank\ttitle", str_replace("\" ", "\"\t", $subsub[0]))))) . "href=" .  str_replace(" ","%20",str_replace("/./", "/", str_replace("a target", "a\ttarget", 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;
}

$datais="<tr><th><input type=text name=path value='.'></input></th><th><input type=text name=filespec value='*.*g'></input></th><th><select name=wo><option value='='>=</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=500></input></th><th><select name=ho><option value='='>=</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=500></input></th></tr>";
$formis="<form method=GET action=./find_images_via_size.php><table id=mytable border=50><tr><th>Relative Path</th><th>Filespec</th><th>Width Operator</th><th>Width Value</th><th>Height Operator</th><th>Height Value</th></tr>" . $datais . "</table><br><br><input style='background-color:yellow;font-size:20px;' type=submit value=List></input></form>"; 
$htmlis="<!doctype html><html><head><title>Find Images Via Size - RJM Programming - October, 2019</title></head><body><h1>Find Images Via Size <a onclick=\"location.href='#mytable';\" style=\"text-decoration:none;cursor:pointer;\">-</a> RJM Programming - October, 2019</h1><p></p>" . $formis . "</body></html>";


// | 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 {} \; | 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 '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";
  exec('cd ' . dirname(__FILE__) . '; ' . $cf . ''); //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}' | 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 '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";
   //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 {} \\; | 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>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=3></td></tr></table>" . "</p>" . explode("</p>", $htmlis)[1];
  // //echo file_get_contents("find_images_via_size.txt");
} else {
  echo $htmlis;
}
exit;