<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created:	11/17/2014	ELL
// 

include ("check_session.php");

$folder = $_POST['folder'];

$document_root = $_SERVER['DOCUMENT_ROOT'];

if ($folder == "pages")
{
	$last_file_edited = $_SESSION['last_file_edited'];
	$default_ext = ".html";
	if (BWB_BASE_DIR == '')
		$path = $document_root . "/" . BWB_WEBPAGE_DIR;
	else
		$path = $document_root . "/" . BWB_BASE_DIR . "/" . BWB_WEBPAGE_DIR;
}
elseif ($folder == "scripts")
{
	$last_file_edited = $_SESSION['last_script_edited'];
	$default_ext = ".js";
	if (BWB_BASE_DIR == '')
		$path = $document_root . "/" . BWB_SCRIPTS_DIR;
	else
		$path = $document_root . "/" . BWB_BASE_DIR . "/" . BWB_SCRIPTS_DIR;
}
elseif ($folder == "php")
{
	$default_ext = ".php";
	$last_file_edited = $_SESSION['last_php_edited'];
	$path = $document_root . "/php";
}
else
{
	echo EDIT_FILE_SELECT_NO_FOLDER_ERR;
	exit;
}

echo "<div class='button_sub_level_1' onClick=\"window.location='edit_file.php?file_name=untitled$default_ext&folder=$folder';\"><span class='button_text_sub_level_1'>*** " . NEW_WORD . " ***</span></div>\n";

$file_arr = array();
			   
if ($handle = opendir($path)) 
{
	while (false !== ($file = readdir($handle)))
	{
		if (($file != ".") && ($file != "..") && !is_dir($path . "/" . $file) && (strpos($file, ".", 0) != 0))
		{
			$file_arr[] = $file;
		}
	}
	closedir($handle);
	
	sort($file_arr);
	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=$file&folder=$folder';\"><span class='button_text_sub_level_1'>$file</span></div>\n";
	}
}
?>