Example #1
0
 public function index()
 {
     $params = $this->input->post();
     if (empty($params) || !isset($params['id']) || !isset($params['host']) || empty($params['host']) || !isset($params['login']) || empty($params['login']) || !isset($params['password']) || empty($params['password'])) {
         echo json_encode(array(array('text' => '/', 'children' => true)));
     } elseif ($params['id'] && $params['host'] && $params['login'] && $params['password']) {
         if ($params['id'] == '#') {
             if (isset($params['rootFolder']) && !empty($params['rootFolder'])) {
                 $rootFolder = $params['rootFolder'];
                 $rootFolderId = str_replace('/', '_SEP_', $rootFolder);
                 echo json_encode(array(array('id' => $rootFolderId, 'text' => $rootFolder, 'type' => 'folder', 'icon' => '/public/img/ic_folder_open_black_18dp.png', 'children' => true)));
             } else {
                 echo json_encode(array(array('id' => '_SEP_', 'text' => '/', 'type' => 'folder', 'icon' => '/public/img/ic_folder_open_black_18dp.png', 'children' => true)));
             }
             return;
         }
         $folderToRead = str_replace('_SEP_', '/', $params['id']);
         $ssh = new ssh($params['host'], $params['login'], $params['password']);
         $files = $ssh->ls($folderToRead);
         $outputArray = array();
         foreach ($files as $fileData) {
             $fullFilePath = rtrim($folderToRead, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $fileData['name'];
             $icon = $fileData['type'] == 'dir' ? '/public/img/ic_folder_open_black_18dp.png' : '/public/img/ic_description_black_18dp.png';
             if ($fileData['type'] == 'file' && preg_match('%\\.(' . implode('|', $this->knownCodeFileExtensions) . ')$%i', $fileData['name'])) {
                 $icon = '/public/img/ic_code_black_18dp.png';
             }
             $outputArray[] = array('id' => str_replace(DIRECTORY_SEPARATOR, '_SEP_', $fullFilePath), 'text' => $fileData['name'], 'type' => $fileData['type'] == 'dir' ? 'folder' : 'file', 'icon' => $icon, 'children' => $fileData['type'] == 'dir');
         }
         // sort - first folders A-Z, than files A-Z
         $this->sortFiles($outputArray);
         //$result = $ssh("ls -la ".escapeshellarg($params['id']));
         // load the children of `id`
         header('Content-Type: application/json');
         echo json_encode($outputArray);
     }
 }