<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 10/3/2011	ELL
// Revised: 2/9/2012	ELL
// Added table prefix handling.
// Revised: 4/9/2013	ELL
// Modified to be generic SQL file running tool such that can be used by plugin installer as well.

	include_once ("check_session.php");
	
	$enable_file_manager = $_SESSION['enable_file_manager'];
	if ($enable_file_manager != "100")
		header("Location: /index.php");
	
	// Open SQL file and read in all lines.
	$document_root = getenv('DOCUMENT_ROOT');
	if (!isset($tmp_folder) || ($tmp_folder == ""))
		$tmp_folder = "tmp";
	
	if (isset($sql_file) && ($sql_file != ""))	// If installing plugin or some other SQL file, then use that file.
	{
		$sql_file_parts = explode(".", $sql_file);
		$sql_file_name = $sql_file_parts[0];
		echo READING_WORD . " $sql_file_name " . UPLOAD_TEMPLATE_SQL_FILE_MSG . "<br><br>\n";
		$sql_file = $document_root . "/" . $tmp_folder . "/" . $sql_file;
	}
	else
	{
		echo UPLOAD_TEMPLATE_SQL_FILE_NAME_NOT_DEFINED_ERR;
		exit;
	}

	$sql_string = "";
	$handle = @fopen($sql_file, "r");
	if ($handle) {
		$sql_string = fread($handle, filesize($sql_file));
		fclose($handle);
	}
//	echo "SQL: $sql_string<br><br>\n";
	
	echo PARSING_WORD . " $sql_file_name " . UPLOAD_TEMPLATE_SQL_MSG . "<br><br>\n";
	
	include_once("lib/run_sql.php");
	
	$result = RunSQL($sql_string, $bwb_dbh, BWB_TABLE_PREFIX, true, UPLOAD_TEMPLATE_ROWS_AFFECTED_MSG);
?>