<?php
include_once("../classes/users.class.php");

// Setting up some basic classes
$db = new database();

$action = filter_input(INPUT_POST, 'action');
$name = filter_input(INPUT_POST, 'name');
$currentDirectory = filter_input(INPUT_POST, 'directory');
switch($action){

    case 'folder':
       //search through the main directory for folders
        $rootPath = "../droply/demo/uploads/" . $_COOKIE['uid'];
        if($currentDirectory != "") {
            $rootPath = $currentDirectory;
        }
        //$rootPath = "../uploads/4";
       // $iter = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootPath, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST , RecursiveIteratorIterator::CATCH_GET_CHILD );
        $iter = new DirectoryIterator($rootPath);
        $directory  = array();
      /*  foreach($iter as $path => $dir){
            if($dir->isDir()){
                $directory[] = $path;
            } */
            foreach($iter as $key) {
                if($key->isDot() || !$key->isDir()){ continue;}
              $directory[] = $rootPath . '/' . $key->getFilename();
        }
        echo json_encode($directory);
        break;

    case 'back':
        $rootPath = $currentDirectory;
        //need to strip the last /
        $iter = new DirectoryIterator($rootPath);
        $directory = array();
        foreach($iter as $key){
            if($key->isDot() || !$key->isDir()) { continue;}
            $directory[] = $rootPath . '/' . $key->getFilename();
        }
        echo json_encode($directory);
        break;

    case 'file':
        //search through the main directory for files
        $rootPath = "../droply/demo/uploads/" . $_COOKIE['uid'];
        if($currentDirectory != ""){
            $rootPath =  $currentDirectory;
        }
        $fileSystemIterator = new FilesystemIterator($rootPath);
        $files = array();
        foreach($fileSystemIterator as $fileInfo){
            if(!$fileInfo->isDir())
            $files[] = $fileInfo->getFilename();
        }
        echo json_encode($files);
        break;
}

?>