<?php
    $admin  = getCONFIG('admin');
    $semi   = getCONFIG('semi');

    $hn     = $_GET['hn'];
    $dir    = '../store/'.$hn;
    function getDirContents($dir,$hn, &$results = array()) {
        $files = scandir($dir);

        foreach ($files as $key => $value) {
            $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
            if (!is_dir($path)) {
                $hnstep01   = strtoupper($hn);
                $pathstep01 = str_replace($hn,$hnstep01,$path);
                $hnstep02   = strtolower($hnstep01);
                $pathstep02 = str_replace($hnstep01,$hnstep02,$pathstep01);
                $ex         = explode("\\$hnstep02\\",$pathstep02);
                // dd($ex);
                $results['file'][] = $ex[1];
                $results['size'][] = filesize($path);
            } else if ($value != "." && $value != "..") {
                $hnstep01   = strtoupper($hn);
                $pathstep01 = str_replace($hn,$hnstep01,$path);
                $hnstep02   = strtolower($hnstep01);
                $pathstep02 = str_replace($hnstep01,$hnstep02,$pathstep01);

                getDirContents($pathstep02,$hnstep02, $results);
                $ex = explode($hnstep02,$pathstep02);
                $results['dir'][] = $ex[1];
            }
        }
        return $results;
    }

    $data = getDirContents($dir,$hn);
    $arr = array();
    if(isset($data['dir'])){
        foreach($data['dir'] as $d){
            $arr['dir'][] = $d;
        }
    }

    if(isset($data['file'])){
        foreach($data['file'] as $key=>$val){
            $syn = true;
            $lastFourChars = mb_substr($val, -4, null, 'utf-8');
            if ($lastFourChars === ".mp4") {
                if(@$admin->com_type!="server"){
                    if(@$semi->vdotoserver){
                        $arr['file'][$key]['path'] = $data['file'][$key];
                        $arr['file'][$key]['size'] = $data['size'][$key];
                    }
                }
            }else{
                $arr['file'][$key]['path'] = $data['file'][$key];
                $arr['file'][$key]['size'] = $data['size'][$key];
            }
        }
    }

    printJSON($arr);





















?>
