<?php

// doviadir.php 
// Traverse directory tree
// RJM Programming http://www.rjmprogramming.com.au
// November, 2014

if (!isset($_GET['dir']) && !isset($_GET['fix']) && !isset($_POST['dir']) && !$argv) { // web browsing initially
   echo '<!doctype HTML>
         <html>
         <body style="background-color:pink;">
         <div align="center">
         <h1>Traverse Directory Tree</h1><br><br>
         <form method="GET" action="' . $_SERVER['REQUEST_URI'] . '">' . "\n";
   if (strpos(strtolower($_SERVER['SERVER_NAME']), 'rjmprogramming.com.au') !== false || strpos(strtolower($_SERVER['SERVER_PORT']), '8888') !== false) {
         echo 'Domain: <p name="domain" id="domain">' . "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/" . '</p><br>' . "\n";
         echo '<input size=120 type="hidden" name="dir" id="dir" value="' . "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/" . '"><br>' . "\n";
         echo 'Subdirectory (end with ' . DIRECTORY_SEPARATOR . '): <input size=120 onblur=" if (this.value.substring(0,1) == String.fromCharCode(46)) { this.value=' . "'" . '' . "'" . ';  } document.getElementById(' . "'" . 'dir' . "'" . ').value=document.getElementById(' . "'" . 'domain' . "'" . ').innerHTML + this.value.replace((String.fromCharCode(46) + String.fromCharCode(46)), String.fromCharCode(46)); " type="text" name="subdirectory" id="subdirectory" value=""><br><br>';
   } else {
         echo 'Domain: <input size=120 onblur=" if (this.value.substring(0,1) == String.fromCharCode(46)) { this.value=' . "'" . '/' . "'" . ';  document.getElementById(' . "'" . 'dir' . "'" . ').value=this.value.replace((String.fromCharCode(46) + String.fromCharCode(46)), String.fromCharCode(46)) + document.getElementById(' . "'" . 'subdirectory' . "'" . ').value.replace((String.fromCharCode(46) + String.fromCharCode(46)), String.fromCharCode(46));"  type="text" name="domain" id="domain" value="' . DIRECTORY_SEPARATOR . '"><br>' . "\n";
         echo '<input type="hidden" name="dir" id="dir" value="' . DIRECTORY_SEPARATOR . '"><br>' . "\n";
         echo 'Subdirectory (end with ' . DIRECTORY_SEPARATOR . '): <input size=120 onblur=" if (this.value.substring(0,1) == String.fromCharCode(46)) { this.value=' . "'" . '' . "'" . ';  document.getElementById(' . "'" . 'dir' . "'" . ').value=document.getElementById(' . "'" . 'domain' . "'" . ').value.replace((String.fromCharCode(46) + String.fromCharCode(46)), String.fromCharCode(46)) + this.value.replace((String.fromCharCode(46) + String.fromCharCode(46)), String.fromCharCode(46)); " type="text" name="subdirectory" id="subdirectory" value=""><br><br>';
   }
   echo '
         Find 1 of 2 (optional): <input size=120 type="text" name="inspec1" id="inspec1" value=""><br>
         Find 2 of 2 (optional): <input size=120 type="text" name="inspec2" id="inspec2" value=""><br><br>
         <input type="submit" name="submit" id="submit" value="Submit">      
         </form>
         </div>
         </body>
         </html>';
} else if (isset($_GET['fix'])) {
     $hdr = "";
     $onhdr = "";
     if (1 == 3 && (strpos(strtolower($_SERVER['SERVER_NAME']), 'rjmprogramming.com.au') !== false || strpos(strtolower($_SERVER['SERVER_PORT']), '8888') !== false)) {
      $hdr = "<head><script type='text/javascript'> function onloadf() { parent.document.getElementById('myfiles').innerHTML = parent.document.getElementById('myfiles').innerHTML.replace((String.fromCharCode(46) + String.fromCharCode(46)), '.').replace(/" . str_replace("/", "\\/", str_replace("PHP/", "/", dirname(__FILE__) . "/")) . "/gi, 'http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/');  parent.document.getElementById('mydirs').innerHTML = parent.document.getElementById('mydirs').innerHTML.innerHTML.replace((String.fromCharCode(46) + String.fromCharCode(46)), '.').replace(/" . str_replace("/", "\\/", str_replace("PHP/", "/", dirname(__FILE__) . "/")) . "/gi, 'http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/'); location.href='http://www.rjmprogramming.com.au'; } </script></head>";
      $onhdr = ' onload=" onloadf(); " ';
     }
     echo '<!doctype HTML>
           <html>' . $hdr . '
           <body style="background-color:pink;" ' . $onhdr . '>
           </body>
           </html>';
     exit;
} else {

   set_time_limit(0);

   $suffix = "";
   $dirs = array();
   $dir    = dirname(__FILE__) . "/"; 
   $fdir = "";
   $inspec1 = ''; 
   $inspec2 = '';
   $icnt = 0; 

   if ($argv) {
   foreach ($argv as $arg) {   // PHP command line
    if ($icnt == 0) {
      if (strpos(urldecode($arg), ".php") === false) $dir = urldecode($arg);
      if (strpos(urldecode($arg), ".php") === false) $fdir = $dir;
    } else if ($icnt == 1) {
      $inspec1 = urldecode($arg);
    } else if ($icnt == 2) {
      $inspec2 = urldecode($arg);
    } 
    if (strpos(urldecode($arg), ".php") === false) $icnt = $icnt + 1;
   }
   }


   if (isset($_GET['subdirectory']) || isset($_POST['subdirectory'])) {     // web browsing
     $hdr = "";
     $onhdr = "";
     if (1 == 2 && (strpos(strtolower($_SERVER['SERVER_NAME']), 'rjmprogramming.com.au') !== false || strpos(strtolower($_SERVER['SERVER_PORT']), '8888') !== false)) {
      $hdr = "<head><script type='text/javascript'> function onloadf() { document.getElementById('myfiles').innerHTML = document.getElementById('myfiles').innerHTML.replace(/" . str_replace("/", "\\/", str_replace("PHP/", "/", dirname(__FILE__) . "/")) . "/gi, 'http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/');  document.getElementById('mydirs').innerHTML = document.getElementById('mydirs').innerHTML.replace(/" . str_replace("/", "\\/", str_replace("PHP/", "/", dirname(__FILE__) . "/")) . "/gi, 'http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/'); } </script></head>";
      $onhdr = ' onload=" onloadf(); " ';
     }
     echo '<!doctype HTML>
           <html>' . $hdr . '
           <body style="background-color:pink;" ' . $onhdr . '>
           <div align="center">
           <h1>Traverse Directory Tree <a href="' . str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']) . '" title="Back">Results</a></h1><br><br>';
   }                                                                        // business logic


   if (isset($_GET['dir'])) {     // curl (parameterising idea) or web browsing later
     $swstuff = explode("@", urldecode($_GET['dir']));
     if (sizeof($swstuff) == 1) {
       $dir = urldecode($_GET['dir']);
     } else {
       $dir = urldecode($swstuff[0]);
       if (sizeof($swstuff) >= 2) $inspec1 = urldecode($swstuff[1]);
       if (sizeof($swstuff) >= 3) $inspec2 = urldecode($swstuff[3]);
     }
     $fdir = $dir;
   }

   if (isset($_GET['inspec1'])) {
     $inspec1 = urldecode($_GET['inspec1']);
   } 
   if (isset($_GET['inspec2'])) {
     $inspec2 = urldecode($_GET['inspec2']);
   } 

   if (isset($_POST['dir'])) {           // curl parameterising idea
     $swstuff = explode("@", urldecode($_POST['dir']));
     if (sizeof($swstuff) == 1) {
       $dir = urldecode($_POST['dir']);
     } else {
       $dir = urldecode($swstuff[0]);
       if (sizeof($swstuff) >= 2) $inspec1 = urldecode($swstuff[1]);
       if (sizeof($swstuff) >= 3) $inspec2 = urldecode($swstuff[3]);
     }
     $fdir = $dir;
   }

   if (isset($_POST['inspec1'])) {
     $inspec1 = urldecode($_POST['inspec1']);
   } 
   if (isset($_POST['inspec2'])) {
     $inspec2 = urldecode($_POST['inspec2']);
   } 
   
   if (strpos(strtolower($_SERVER['SERVER_NAME']), 'rjmprogramming.com.au') !== false || strpos(strtolower($_SERVER['SERVER_PORT']), '8888') !== false) {
     $dir = str_replace("http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/", str_replace("PHP/", "/", dirname(__FILE__) . "/"), $dir);
   }
   
   $result = array();
   
   function moredirs($inidea) {
     global $dirs;
     $idirs = 0;
     $dirs[sizeof($dirs)] = str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $inidea);
     $cdir = scandir($inidea);
     foreach ($cdir as $key => $value) {
      if (!in_array($value,array(".",".."))) {
         if (is_dir($inidea . DIRECTORY_SEPARATOR . $value)) {
            moredirs($inidea . DIRECTORY_SEPARATOR . $value);
            return 1;
         }
      }
     }
     return $idirs;
   }
   
   $dirs[sizeof($dirs)] = $dir;
   
   if (trim($fdir) == "") {
     
     $cdir = scandir($dir);
     foreach ($cdir as $key => $value) {
      if (!in_array($value,array(".",".."))) {
         if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) {
            moredirs($dir . DIRECTORY_SEPARATOR . $value);  
         }
      }
     }
   
   } 
   
   
   if (sizeof($dirs) >= 1) {
    for ($j=0; $j<sizeof($dirs); $j++) {
       foreach (glob($dirs[$j] . "*") as $filename) {
        if (($inspec1 == "" || strpos(str_replace($dirs[$j],"",$filename), $inspec1) !== false) && ($inspec2 == "" || strpos(str_replace($dirs[$j],"",$filename), $inspec2) !== false)) {
         if (!is_dir($filename)) {
          if (1 == 1 && (strpos(strtolower($_SERVER['SERVER_NAME']), 'rjmprogramming.com.au') !== false || strpos(strtolower($_SERVER['SERVER_PORT']), '8888') !== false)) {
            $result[sizeof($result)] = str_replace(str_replace("PHP/", "/", dirname(__FILE__) . "/"), "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/", $filename);  
          } else {
            $result[sizeof($result)] = $filename;  
          }
         }
        }
       }
    if (1 == 1 && (strpos(strtolower($_SERVER['SERVER_NAME']), 'rjmprogramming.com.au') !== false || strpos(strtolower($_SERVER['SERVER_PORT']), '8888') !== false)) {
            $dirs[$j] = str_replace(str_replace("PHP/", "/", dirname(__FILE__) . "/"), "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/", $dirs[$j]);  
    }
    } 
   }
   
   $fis = "Files";
   if (isset($_GET['inspec1']) && isset($_GET['inspec2'])) {    
     $fis = str_replace(" and  ", "", "Files containing " . $inspec1 . " and " . $inspec2 . " ");
   } else if (isset($_GET['inspec1'])) {    
     $fis = "Files containing " . $inspec1;
   } else if (isset($_POST['inspec1']) && isset($_POST['inspec2'])) {    
     $fis = str_replace(" and  ", "", "Files containing " . $inspec1 . " and " . $inspec2 . " ");
   } else if (isset($_POST['inspec1'])) {    
     $fis = "Files containing " . $inspec1;
   } 
   
   

   if (isset($_GET['subdirectory']) || isset($_POST['subdirectory'])) {     // web browsing 
   
     if (1 == 1 && (strpos(strtolower($_SERVER['SERVER_NAME']), 'rjmprogramming.com.au') !== false || strpos(strtolower($_SERVER['SERVER_PORT']), '8888') !== false)) {
     if (sizeof($dirs) >= 1) {
      for ($j=0; $j<sizeof($dirs); $j++) {
       $dirs[$j] = str_replace(str_replace("PHP/", "/", dirname(__FILE__) . "/"), "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/", $dirs[$j]);
 	  }
     }
     }
     echo "<br><br><p>Directories ...<br><textarea id='mydirs' cols=100 rows=5>";
     $r = str_replace(str_replace("PHP/", "/", dirname(__FILE__) . "/"), "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/", print_r($dirs));
     echo $r . "</textarea><br><br>" . $fis . " ...<br><textarea id='myfiles' cols=100 rows=50>";
     $r2 = str_replace(str_replace("PHP/", "/", dirname(__FILE__) . "/"), "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . "/", print_r($result));
	 echo $r2 . '</textarea></p>
         </div>
         <iframe src="' . str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']) . '?fix=it" width=1 height=1 style="display:none;"></iframe>
         </body>
         </html>';
   } else {
    echo "\n\nDirectories ...\n";
    print_r($dirs);
    echo "\n\n" . $fis . " ...\n";
    print_r($result);
   }                                                                       // business logic

}
?>
