<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created:	11/17/2014	ELL
// Modified: 9/12/2016	ELL
// Added ability to drill down thru file tree.
// 

include ("check_session.php");

$folder = $_POST['folder'];
$sub_folder = $_POST['sub_folder'];
$show_hidden = $_POST['show_hidden'];
$source = $_REQUEST['source'];
if (!isset($show_hidden))
	$show_hidden = 0;

$document_root = $_SERVER['DOCUMENT_ROOT'];

if ($sub_folder == 'root')
	$sub_folder = "";
	
$path = $document_root . "/" . $folder;
if ($sub_folder != "")
	$path .= "/" . $sub_folder;

if ((strpos($folder, BWB_SCRIPTS_DIR) === 0) || (strpos($folder, BWB_SCRIPTS_DIR) === 1))
{
	$last_file_edited = $_SESSION['last_script_edited'];
	$default_ext = ".js";
}
elseif ((strpos($folder, BWB_LIB_DIR) === 0) || (strpos($folder, BWB_LIB_DIR) === 1))
{
	$default_ext = ".php";
	$last_file_edited = $_SESSION['last_php_edited'];
}
else
{
	$last_file_edited = $_SESSION['last_file_edited'];
	$default_ext = ".html";
}

echo "<div class='button_sub_level_1 btn_new_file' onClick=\"window.location='edit_file.php?file_name=untitled$default_ext&folder=$folder$sub_folder&source=" . $source . "';\"><span class='button_text_sub_level_1'>&nbsp;*** " . NEW_FILE . " ***</span></div>\n";
echo "<div class='button_sub_level_1 btn_new_folder' onClick=\"CreateFolderMsg('$folder', '$sub_folder');\"><span class='button_text_sub_level_1'>&nbsp;*** " . NEW_FOLDER . " ***</span></div>\n";

$file_arr = array();
$dir_arr = array();
$bad_dirs = array(".", "..", "manage", "ckeditor", "kfm", "connect", BWB_SCRIPTS_DIR, BWB_LIB_DIR, BWB_FONTS_DIR, "backup", "wizard", "admin", "catalog", "cgi-bin", "error_log", "shoppingcart", "system", "template_admin", "manage_inv", "uploads", "_notes", ".files-sqlite-pdo");
			   
if ($handle = opendir($path)) 
{
	while (false !== ($file = readdir($handle)))
	{
		if (!is_dir($path . "/" . $file))
		{
			if ((strpos($file, ".", 0) !== 0) || ($show_hidden == 1))
				$file_arr[] = $file;
		}
		elseif (is_dir($path . "/" . $file) && !in_array($file, $bad_dirs))
		{
			if (!(($sub_folder == "") && ($file == "..")))
				$dir_arr[] = $file;
		}
	}
	closedir($handle);
	
	sort($file_arr);
	sort($dir_arr);
	foreach($dir_arr as $dir)
	{	
		echo "<div class='button_sub_level_1 btn_folder_select' onClick=\"ShowFiles('$folder', '$sub_folder/$dir', this);\"><span class='button_text_sub_level_1'>$dir</span></div>\n";
	}
	
	$url_sub_folder = str_replace("/", "%2F", $sub_folder);
	$url_folder = str_replace("/", "%2F", $folder);
	foreach($file_arr as $file)
	{
		if ($last_file_edited == $file)
			$selected = " down_sub";
		else
			$selected = "";
	
		echo "<div class='button_sub_level_1" . $selected . "' onClick=\"window.location='edit_file.php?file_name=" . $url_sub_folder . "%2F" . $file . "&folder=" . $url_folder . "&source=" . $source . "';\"><span class='button_text_sub_level_1'>" . $file . "</span></div>\n";
	}
}
?>