<?php session_start();; ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script>
function open_file(f1)
{
   var key = '<?php echo $_SERVER['DOCUMENT_ROOT']; ?>';
   var kL = key.length;
   var url = f1.substring(kL).replace(/\\/g, '/');
   window.open( encodeURI(url), 'browser' ); 
}

function view_source(f1)
{
   var url = 'go.php?SOURCE=' + encodeURIComponent(f1);
   window.open( url, 'browser' ); 
} 
</script>
</head>
<body>
<?php
include('../lib/generic.inc.php');
 
if (!empty($_REQUEST['USERID']))
   try_login();   
else if (!empty($_SESSION['USERID']))
   go_logged_in();
else
   ask_credentials();
   
//------------------------------------------------
function try_login()
{                                                         
   if ($_REQUEST['USERID'] === 'inca@incahellas.com' && @$_REQUEST['PWD'] === '$anasa$') {
      $_SESSION['USERID'] = 'inca';
      return go_logged_in(); 
   }
   else {
      return ask_credentials();
   }
}   
//------------------------------------------------
function ask_credentials()
{
   if (empty($_REQUEST['inca'])) {
      echo 'Hello world';
      return;
   }
?>   
   <form method=POST action="go.php">
   <table>
   <tr>
      <td>User Id</td>
      <td><input type=text name="USERID"></td>
   </tr>
   <tr>
      <td>Password</td>
      <td><input type=password name="PWD"></td>
   </tr>
   <tr>
      <td></td>
      <td><input type=submit value="Login"></td>
   </tr>
   </table>
   </form>   
<?php   
}
//------------------------------------------------
function view_source()
{
   $src = file_get_contents($_REQUEST['SOURCE']);
   echo '<pre>'.myhtml($src).'</pre>';
}
//------------------------------------------------
function go_logged_in()
{
   $sources = array('bat', 'css', 'htc', 'php', 'wsf');
   $exclude = array('bmp', 'css', 'gif', 'jpg', 'jpeg', 'js', 'htm', 'html', 'pdf', 'png', 'wav');
   
   if (!empty($_REQUEST['SOURCE']))
      return view_source();
      
   if ( empty($_REQUEST['FOLDER']) ) {
      $search = $_SERVER['DOCUMENT_ROOT'].'/*'; 
   }
   else {
      $search = $_REQUEST['FOLDER'].'*';
      
      $ar1 = explode('/', strtr($_REQUEST['FOLDER'], '\\', '/'));
      while (empty(array_pop($ar1)));
      $master = implode('/', $ar1).'/';
      echo '<h2><a href="go.php?FOLDER='.urlencode($master).'">'.$_REQUEST['FOLDER'].'</a></h2>'; 
   }      
        
   $ar1 = glob($search, GLOB_MARK );
   echo '<table>';
   foreach ($ar1 as $f1) {
      $l = strlen($f1);
      if ($f1{$l-1} === '/'  || $f1{$l-1} === '\\') { // folder
         echo '<tr><td><a href="go.php?FOLDER='.urlencode($f1).'">'.myhtml($f1).'</a></td></tr>';
      }
      else { // file
         $ext = strtolower(pathinfo($f1, PATHINFO_EXTENSION));
         if (in_array($ext, $exclude))
            continue;
            
         echo '<tr><td>'.myhtml($f1).'</td><td>';
         if (in_array($ext, $sources))
         {
            echo '<a href="javascript:view_source(\''.myjs($f1).'\')">View source</a>'; 
         }
         else 
         {
            echo '<a href="javascript:open_file(\''.myjs($f1).'\')">Open in browser</a>';
         }
         echo '</td></tr>';
      }
   }
   echo '</table>';
}
//------------------------------------------------
?>
</body>
</html>