{<? 
//print_r($_GET);
error_reporting(0);
if ($_GET['logout'] || ($_SESSION['logged_in']==0)) {
    $_SESSION['logged_in']=0;
    header("Location: /index.cgi"); 
    die();     
}
$base_path=$_GLOBALS['BASE_PATH'];
if (!$base_path) $base_path="";
include($base_path.'/lang/lang.inc');
if ($_GET['dir']) {
    
  chdir($_GET['dir']);
  $dir=$_GET['dir'];
  if (substr($dir,0,strlen($docroot))==$docroot) {
      $dir=substr($dir,strlen($docroot));
      $download_ok=1;
  }
} else {
    if ($docroot=='/') {
        
        $download_ok=1;
        $dir='/';
    }
    $_GET['dir']='/';
    chdir("\\");
}
$mounted=0;

if (substr($_GET['dir'],1,1)==':') {
    $drive=substr($_GET['dir'],0,1);
    
    $mounted=disk_status(intval($drive));
    //print("mounted = ".$drive." ".$mounted);
    if ($mounted>0 && $mounted < 4) {
            print('"error":1,"status":"Disk error '+$mounted+'",');
        
    }
    
    
}
if ($_GET['cmd']=="mkdir") {
    if (!mkdir($_GET['dir'].$_GET['newdir'])) {
        print('"status":"'+lang('FOLDER_CREATED_SUCCESSFULLY','dir',$_GET['newdir'])+'",');
    } else {
        print('"error":1,status":"'.lang('DIR_CREATE_ERROR','dir').' '+$_GET['newdir']+'",');
    }
} 
if ($_GET['cmd']=="del") {
    if ($_GET['fn']) {
        if (!unlink($_GET['fn'])) {
            print('"status":"'.lang('DELETED_FILE','dir',$_GET['fn']).'",');
        } else {
            print('"error":1,"status":"'.lang('ERROR_DELETE_DIR','dir').' '+$_GET['fn']+'",');
        }
    }
}

if ($_GET['cmd']=="rename") {
    if ($_POST['fn'] && $_POST['fnto']) {
        //if (file_exists($_GET['fn'])) {
        if (!file_exists($_POST['fnto'])) {
            $res=rename($_POST['fn'],$_POST['fnto']);
            if (!$res) {
                print('"status":"'.lang('RENAMED_FILE','dir',$_POST['fn']).'",');
            } else {
                print('"error":1,"status":"'.lang('ERROR_RENAME_FILE','dir').' '+$_POST['fn']+' - '.$res.'",');
            }
        } else {
            print('"error":1,"status":"'.lang('ERROR_RENAME_FILE','dir').' '+$_POST['fn']+' - '.$res.'",');
        }
    }
}

if ($_GET['cmd']=="untar") 
{
    if ($_GET['fn']) {
           max_execution_time(600);
        print('"filelist":"');
        if (untar($_GET['fn'])) {
            print('","status":"'.lang('UNTAR_FILE','dir').' '+$_GET['fn']+' successfully",');
        } else {
            print('","error":1,"status":"'.lang('ERROR_UNTAR_FILE','dir').' '+$_GET['fn']+'",');
        }
    }
}
if ($_GET['cmd']=="rmdir") {
    if ($_GET['param']) {
        if (!rmdir($_GET['param'],1)) {
            print('"status":"'.lang('FOLDER_DELETE_SUCCESS','dir',$_GET['param']).'",');                
        } else {
            print('"error":1,"status":"'.lang('FOLDER_DELETE_ERROR','dir',$_GET['param']).'",');        
        }
    }
}
 if ($_SERVER['HW_PLATFORM']==11  || $_SERVER['HW_PLATFORM']>=20) {
    $cur_drive=get_drive(); 
    if ((substr($_GET['dir'],0,2)=="0:")) $cur_drive=0;
    if ((substr($_GET['dir'],0,2)=="1:")) $cur_drive=1;
    if ((substr($_GET['dir'],0,2)=="2:")) $cur_drive=2;
    
    if (disk_status($cur_drive)==1 || disk_status($cur_drive)==2) {
        print('"wprotect":1,"info":"Not mounted",');
        print('"directory":"'+$_GET['dir']+'","items":[]}');
        die();
    } else if (disk_status($cur_drive)==0){
        print('"wprotect":0,"info":"Free: '.disk_free_space($cur_drive)."KB of ".disk_total_space($cur_drive).'KB",');
    } else if (disk_status($cur_drive)==4){
        print('"wprotect":1,"info":"Write Protected,Free: '.disk_free_space($cur_drive)."KB of ".disk_total_space($cur_drive).'KB",');
    }
    
    
} else print('"wprotect":0,"info":"Free: '.disk_free_space($cur_drive)."KB of ".disk_total_space($cur_drive).'KB",');

print('"directory":"'+$_GET['dir']+'","items":[');
if (!$mounted || $mounted==4) {
$arr=findfirst("*.*",255-8);
$has_parent=0;
$cnt=0;
$pattern="^[\ \%()a-zA-Z0-9_.-~\-]*$";
while ($arr) {
    $cnt++;
    print("{");
    if (($arr['attributes'] & 16)) {
        print('"type":"directory","name":');
        $candel=1;
        $class="dir";
        if ($arr['filename']=="..") {
            
            $up = substr($_GET['dir'],0,strlen($_GET['dir'])-1);
            $newdir=substr($_GET['dir'],0,strrpos($up,"/"))+"/";
            //print("newdir=".$newdir);
            if ($_SERVER['HW_PLATFORM']==11  || $_SERVER['HW_PLATFORM']>=20) { 
                if (substr($_GET['dir'],1,1)==':' && $newdir=='/') {
                   // print("match parent");
                    $newdir=substr($_GET['dir'],0,2)."/";
                }
            }

            $arr["filename"]=lang('PARENT_DIRECTORY','dir');
            $class="parent";
            $candel=0;
        } else {
             if ($arr['filename']==".") {
                $has_parent=1;
                if ($_SERVER['HW_PLATFORM']==11  || $_SERVER['HW_PLATFORM']>=20) { 
                    if (substr($_GET['dir'],1,1)==':')
                        $newdir=substr($_GET['dir'],0,2)."/";
                }
                else $newdir="/";
                $arr["filename"]=lang('TOP_DIRECTORY','dir');
                $candel=0;
                $class="parent";
            } else {
                $val=ereg($pattern,$arr["filename"]);
                if (!$val) {
                    $arr['filename']='___invaliddir'.$cnt." ";
                }

                $newdir = $_GET['dir'].$arr['filename']."/";
            }  
        }
        print('"'+$arr['filename']+'","path":"'+$newdir+'",');
        print('"class":"'+$class+'","attributes":"'.lang('FOLDER','dir').'",');
        print('"timestamp":"0"');
    } else {
        $val=$arr["filename"];
        $ok=ereg($pattern,$arr["filename"]);
        if (!$ok) {
            $arr['filename']='___invalid'.$cnt;
            $arr['timestamp']=0;
            $arr['size']=0;
        }
        print('"type":"file","name":"'+$arr["filename"]+'",');
        $ext = substr($arr["filename"],strrpos($arr['filename'],".")+1);
        $canedit=0;
        $class="unknown";
        if (($ext=="js")  || ($ext=="php") || ($ext=="ini") || ($ext=="txt") || ($ext=="css") || ($ext=="inc")) {
            $class="text";
            $canedit=1;
        } else {
            if ($ext=="cgi") {
                $class="script";
            }
            if ($ext=="cgc") {
                $class="compiled";
            }
            if ($ext=="hex") {
                $class="hex";
            }
            if (($ext=="tar") || ($ext=="gz")) {
                $class="compressed";
            }
            if (($ext=="png") || ($ext=="gif") || ($ext=="jpg")) {
                $class="image";
            }
            if ($ext=="pdf") {
                $class="pdf";
            }
        }
        print('"class":"'+$class+'",');
        print('"ext":"'+$ext+'",');
        $ts=timefromfat($arr['timestamp']);
        print('"size" : '+$arr['filesize']+",");
        
        $res="";//$arr['attributes'];
        $attr=intval($arr['attributes']);
        if ($attr & 1) $res=$res+lang('READ_ONLY','dir')+" ";
        if ($attr & 2) $res=$res+lang('HIDDEN','dir')." ";
        if ($attr & 4) $res=$res+lang('SYSTEM','dir')." ";
        if ($attr & 32) $res=$res+lang('ARCHIVE','dir')." ";
        print('"attributes":"'+$res+'",');
        $str=strftime("%Y-%m-%d %H:%M",$ts);
        print('"timestamp":"'+$ts+'",');
        print('"time":"'+$str+'"');
    }

    print("}");
    $arr=findnext();
    if ($arr) print(",");
}
    // on Ultra, readdir does not return .. info
    if (!$has_parent) {
        
        if ($cnt) print(",");
        print('{"type":"directory","name":');
        $candel=1;
        $class="dir";
        $up = substr($_GET['dir'],0,strlen($_GET['dir'])-1);
        $newdir=substr($_GET['dir'],0,strrpos($up,"/"))+"/";
        $class="parent";
        $candel=0;
            if ($_SERVER['HW_PLATFORM']==11  || $_SERVER['HW_PLATFORM']>=20) { 
                if (substr($_GET['dir'],1,1)==':' && $newdir=='/') {
                   // print("match parent");
                    $newdir=substr($_GET['dir'],0,2)."/";
                }
            }
        
        print('"'+lang('PARENT_DIRECTORY','dir')+'","path":"'+$newdir+'",');
        print('"class":"'+$class+'","attributes":"'.lang('FOLDER','dir').'",');
        print('"timestamp":"0"');
        print("}");
        
        
         print(',{"type":"directory","name":');
        $candel=1;
        $class="dir";
        $up = substr($_GET['dir'],0,strlen($_GET['dir'])-1);
        $newdir=substr($_GET['dir'],0,strrpos($up,"/"))+"/";
            if ($_SERVER['HW_PLATFORM']==11  || $_SERVER['HW_PLATFORM']>=20) { 
                if (substr($_GET['dir'],1,1)==':' && $newdir=='/') {
                   // print("match parent");
                    $newdir=substr($_GET['dir'],0,2)."/";
                }
            }
        
        $class="parent";
        $candel=0;
        print('"'+lang('TOP_DIRECTORY','dir')+'","path":"'+$newdir+'",');
        print('"class":"'+$class+'","attributes":"'.lang('FOLDER','dir').'",');
        print('"timestamp":"0"');
        print("}");
    }        
        
       
}
        
?>]
}