public function Dialog(Application $app, Request $request)
 {
     $config = $app['FileManager'];
     $config['ext'] = array_merge($config['ext_img'], $config['ext_file'], $config['ext_misc'], $config['ext_video'], $config['ext_music']);
     //handle sessions
     $session = new SessionHandler($app);
     $util = new Utility();
     //handle additional parameters to set views ect
     if (isset($_GET['view'])) {
         $session->setViewType($util->fix_get_params($_GET['view']));
     }
     if (isset($_GET["filter"])) {
         $session->setFilter($util->fix_get_params($_GET['filter']));
     }
     if (isset($_GET["sort_by"])) {
         $session->setSortBy($util->fix_get_params($_GET["sort_by"]));
     }
     if (isset($_GET["descending"])) {
         $session->setDescending($util->fix_get_params($_GET["descending"]));
     }
     $subdir = '';
     if (isset($_GET['fldr']) && !empty($_GET['fldr']) && strpos($_GET['fldr'], '../') === FALSE && strpos($_GET['fldr'], './') === FALSE) {
         $subdir = urldecode(trim(strip_tags($_GET['fldr']), "/") . "/");
         $session->setFilter('');
     }
     // If hidden folders are specified
     if (count($config['hidden_folders'])) {
         // If hidden folder appears in the path specified in URL parameter "fldr"
         $dirs = explode('/', $subdir);
         foreach ($dirs as $dir) {
             if ($dir !== '' && in_array($dir, $hidden_folders)) {
                 // Ignore the path
                 $subdir = "";
                 break;
             }
         }
     }
     /***
      *SUB-DIR CODE
      ***/
     if (!isset($_SESSION['RF']["subfolder"])) {
         $_SESSION['RF']["subfolder"] = '';
     }
     $rfm_subfolder = '';
     if (!empty($_SESSION['RF']["subfolder"]) && strpos($_SESSION['RF']["subfolder"], '../') === FALSE && strpos($_SESSION['RF']["subfolder"], './') === FALSE && strpos($_SESSION['RF']["subfolder"], "/") !== 0 && strpos($_SESSION['RF']["subfolder"], '.') === FALSE) {
         $rfm_subfolder = $_SESSION['RF']['subfolder'];
     }
     if ($rfm_subfolder != "" && $rfm_subfolder[strlen($rfm_subfolder) - 1] != "/") {
         $rfm_subfolder .= "/";
     }
     if (!file_exists($config['current_path'] . $rfm_subfolder . $subdir)) {
         $subdir = '';
         if (!file_exists($config['current_path'] . $rfm_subfolder . $subdir)) {
             $rfm_subfolder = "";
         }
     }
     if (trim($rfm_subfolder) == "") {
         $this->cur_dir = $config['upload_dir'] . $subdir;
         $cur_path = $config['current_path'] . $subdir;
         $thumbs_path = $config['thumbs_base_path'];
         $parent = $subdir;
     } else {
         $this->cur_dir = $config['upload_dir'] . $rfm_subfolder . $subdir;
         $cur_path = $config['current_path'] . $rfm_subfolder . $subdir;
         $thumbs_path = $config['thumbs_base_path'] . $rfm_subfolder;
         $parent = $rfm_subfolder . $subdir;
     }
     $cycle = TRUE;
     $max_cycles = 50;
     $i = 0;
     while ($cycle && $i < $max_cycles) {
         $i++;
         if ($parent == "./") {
             $parent = "";
         }
         if (file_exists($config['current_path'] . $parent . "config.php")) {
             require_once $config['current_path'] . $parent . "config.php";
             $cycle = FALSE;
         }
         if ($parent == "") {
             $cycle = FALSE;
         } else {
             $parent = $util->fix_dirname($parent) . "/";
         }
     }
     if (!is_dir($thumbs_path . $subdir)) {
         $util->create_folder(FALSE, $thumbs_path . $subdir);
     }
     if (isset($_GET['popup'])) {
         $popup = strip_tags($_GET['popup']);
     } else {
         $popup = 0;
     }
     //Sanitize popup
     $popup = !!$popup;
     if (isset($_GET['crossdomain'])) {
         $crossdomain = strip_tags($_GET['crossdomain']);
     } else {
         $crossdomain = 0;
     }
     //Sanitize crossdomain
     $crossdomain = !!$crossdomain;
     //view type
     $view = $session->getViewType();
     //filter
     $filter = $session->getFilter();
     //sorting method
     $sort_by = $session->getSortBy();
     //sorting order
     $descending = $session->getDescending();
     $boolarray = array(false => 'false', true => 'true');
     $return_relative_url = isset($_GET['relative_url']) && $_GET['relative_url'] == "1" ? true : false;
     if (!isset($_GET['type'])) {
         $_GET['type'] = 0;
     }
     if (isset($_GET['editor'])) {
         $editor = strip_tags($_GET['editor']);
     } else {
         if ($_GET['type'] == 0) {
             $editor = false;
         } else {
             $editor = 'tinymce';
         }
     }
     if (!isset($_GET['field_id'])) {
         $_GET['field_id'] = '';
     }
     $field_id = isset($_GET['field_id']) ? $util->fix_get_params($_GET['field_id']) : '';
     $type_param = $util->fix_get_params($_GET['type']);
     if ($type_param == 1) {
         $apply = 'apply_img';
     } elseif ($type_param == 2) {
         $apply = 'apply_link';
     } elseif ($type_param == 0 && $_GET['field_id'] == '') {
         $apply = 'apply_none';
     } elseif ($type_param == 3) {
         $apply = 'apply_video';
     } else {
         $apply = 'apply';
     }
     $get_params = http_build_query(array('editor' => $editor, 'type' => $type_param, 'lang' => $config['default_language'], 'popup' => $popup, 'crossdomain' => $crossdomain, 'field_id' => $field_id, 'relative_url' => $return_relative_url, 'akey' => isset($_GET['akey']) && $_GET['akey'] != '' ? $_GET['akey'] : 'key', 'fldr' => ''));
     //get base config options
     $twigArr = $app['FileManager'];
     //overwrite specific's
     $twigArr['lang'] = $app['FileManager']['default_language'];
     $twigArr['ext'] = array_merge($config['ext_img'], $config['ext_file'], $config['ext_misc'], $config['ext_video'], $config['ext_music']);
     $twigArr['apply'] = $apply;
     $twigArr['field_id'] = $field_id;
     $twigArr['popup'] = $popup;
     $twigArr['crossdomain'] = $crossdomain;
     $twigArr['editor'] = $editor;
     $twigArr['view'] = $view;
     $twigArr['filter'] = $filter;
     $twigArr['sort_by'] = $sort_by;
     $twigArr['descending'] = $descending;
     $twigArr['subdir'] = $subdir;
     $twigArr['field_id'] = $field_id;
     $twigArr['type_param'] = $type_param;
     $twigArr['cur_dir'] = $config['upload_dir'] . $subdir;
     $twigArr['cur_path'] = $config['current_path'] . $subdir;
     $twigArr['thumbs_path'] = $config['thumbs_base_path'];
     $twigArr['cur_dir_thumb'] = $twigArr['thumbs_path'] . $twigArr['subdir'];
     $twigArr['parent'] = $subdir;
     $twigArr['duplicate_files'] = 0;
     $twigArr['rfm_subfolder'] = "";
     $twigArr['base_url_func'] = $util->base_url();
     $twigArr['current_url'] = str_replace(array('&filter=' . $filter, '&sort_by=' . $sort_by, '&descending=' . intval($descending)), array(''), $twigArr['base_url'] . $_SERVER['REQUEST_URI']);
     $twigArr['get_type'] = $_GET['type'];
     $twigArr['home_link'] = $_GET['type'];
     $twigArr['get_params'] = $get_params;
     $twigArr['return_relative_url'] = 0;
     if ($return_relative_url == true) {
         $twigArr['return_relative_url'] = 1;
     }
     if ($twigArr['duplicate_files'] === true) {
         $twigArr['duplicate_files'] = 1;
     }
     //        array(
     //            'ext_img' => $config['ext_img'],
     //            'ext' => array_merge($config['ext_img'], $config['ext_file'], $config['ext_misc'], $config['ext_video'], $config['ext_music']),
     //            'aviary_active' => $config['aviary_active'],
     //            'Error_extension'
     //            'MaxSizeUpload' => (int)$config['MaxSizeUpload'],
     //        );
     $template = 'FileManager/view.html.twig';
     $class_ext = '';
     $src = '';
     $files = scandir($config['current_path'] . $rfm_subfolder . $subdir);
     $n_files = count($files);
     //php sorting
     $sorted = array();
     $current_folder = array();
     $prev_folder = array();
     foreach ($files as $k => $file) {
         if ($file == ".") {
             $current_folder = array('file' => $file);
         } elseif ($file == "..") {
             $prev_folder = array('file' => $file);
         } elseif (is_dir($config['current_path'] . $rfm_subfolder . $subdir . $file)) {
             $date = filemtime($config['current_path'] . $rfm_subfolder . $subdir . $file);
             if ($config['show_folder_size']) {
                 $size = $util->foldersize($config['current_path'] . $rfm_subfolder . $subdir . $file);
             } else {
                 $size = 0;
             }
             $file_ext = 'dir';
             $sorted[$k] = array('file' => $file, 'file_lcase' => strtolower($file), 'date' => $date, 'size' => $size, 'extension' => $file_ext, 'extension_lcase' => strtolower($file_ext));
         } else {
             $file_path = $config['current_path'] . $rfm_subfolder . $subdir . $file;
             $date = filemtime($file_path);
             $size = filesize($file_path);
             $file_ext = substr(strrchr($file, '.'), 1);
             $sorted[$k] = array('file' => $file, 'file_lcase' => strtolower($file), 'date' => $date, 'size' => $size, 'extension' => $file_ext, 'extension_lcase' => strtolower($file_ext));
         }
     }
     // Should lazy loading be enabled
     $lazy_loading_enabled = $config['lazy_loading_file_number_threshold'] == 0 || $config['lazy_loading_file_number_threshold'] != -1 && $n_files > $config['lazy_loading_file_number_threshold'] ? true : false;
     $twigArr['lazy_loading_enabled'] = $lazy_loading_enabled;
     switch ($sort_by) {
         case 'date':
             usort($sorted, array($this, 'dateSort'));
             break;
         case 'size':
             usort($sorted, array($this, 'sizeSort'));
             break;
         case 'extension':
             usort($sorted, array($this, 'extensionSort'));
             break;
         default:
             usort($sorted, array($this, 'filenameSort'));
             break;
     }
     if (!$descending) {
         $sorted = array_reverse($sorted);
     }
     $files = array_merge(array($prev_folder), array($current_folder), $sorted);
     //Add file / folder stuff to array
     $twigArr['n_files'] = $n_files;
     $twigArr['uniqid'] = uniqid();
     //can open current dir?
     $open_dir = false;
     if (@opendir($config['current_path'] . $rfm_subfolder . $subdir)) {
         $open_dir = true;
     }
     $twigArr['clipboard'] = 0;
     $clipboard_path = $session->getClipboardPath();
     //var_dump($clipboard_path);
     if (isset($clipboard_path) && trim($clipboard_path) != null) {
         $twigArr['clipboard'] = 1;
     }
     //$twigArr['open_dir'] = uniqid();
     $twigArr['open_dir'] = $open_dir;
     $twigArr['render_need_name'] = $this->render_need_name($app, $files, $twigArr, $config, $subdir, $filter, $config['transliteration'], $thumbs_path, $get_params, $rfm_subfolder);
     $twigArr['render_need_name_2'] = $this->two;
     $twigArr['files_prevent_duplicate'] = $this->files_prevent_duplicate;
     return $app['twig']->render($template, $twigArr);
 }