<?php
/*
 * This file is part of a larger software package. Please read the License file 
 * in the root of this installation for terms of usage and distribution.
 * 
 * @author The Web Pro Miami, Inc. <info@thewebpro.com>
 * @copyright The Web Pro Miami, Inc. <info@thewebpro.com>, http://www.thewebpro.com/
 * 
 * CORE: manage/dialog/plugin_activate.php - version 1.0
*/

$nAdminOnlyPg = 1;

require("../inc/stop_cache.php");

// Secure and connect
require("../../inc/dbconnect.php");
require("../inc/secure_page.php");

// bring in other required files
require("../inc/pluginutil.php");

// config
$nPageName = "plugin_activate.php";
$nRefreshCall = "runActivate();";
$nPluginPath = "../../plugins/";

// Get passed-in vars
$nUpdateDB = (isset($_POST["nUpdateDB"]) ? $_POST["nUpdateDB"] : NULL);
$nRecID = (isset($_GET["id"]) ? $_GET["id"] : NULL);
if(!$nRecID){ $nRecID = (isset($_POST["nRecID"]) ? $_POST["nRecID"] : NULL); }

if($nRecID){
	$nSQL = "SELECT nName FROM nPlugins WHERE nID='$nRecID' LIMIT 1";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	if($Row = mysql_fetch_array($Result)){
		$nName = $Row["nName"];
	} else { echo "<div align=\"center\" style=\"padding:20px;\">No record found.</div>"; }
} else { echo "<div align=\"center\" style=\"padding:20px;\">No record available. Invalid ID.</div>"; }

if(!$nUpdateDB){
	$nErrorARRAY = array();
	if($nRecID){
		// get Plugin ID
		$nSQL = "SELECT nPluginID FROM nPlugins WHERE nID='$nRecID' LIMIT 1";
		mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
		if($Row = mysql_fetch_array($Result)){
			$nPluginID = $Row["nPluginID"];
		}
		// run initialization functions from the plugin (createComponent, createSettingsForm, etc...)
		// also checks for file/function errors, or if onActivate function itself returns false (error)
		$nMasterFilePath = "$nPluginPath$nPluginID/$nPluginID.php";
		$nPluginActivateErrors = "";
		if(file_exists("$nMasterFilePath")){
			$nIncludeBH = include "$nMasterFilePath"; // send include into variable so it's output isn't viewable, but it's functions and vars are available
			if(function_exists($nPluginID."_onActivate")){ eval("\$nPluginActivateErrors = ".$nPluginID."_onActivate();"); } else { $nErrorARRAY[] = "This plugin is missing a critical function (".$nPluginID."_onActivate) and cannot be activated."; }
		} else { $nErrorARRAY[] = "This plugin is missing a critical file: $nPluginID.php"; }
		if($nPluginActivateErrors!=TRUE || $nPluginActivateErrors!=1){ $nErrorARRAY[] = $nPluginActivateErrors; }
	} else {
		$nErrorARRAY[] = "This plugin does not have a valid ID.  Please delete and re-install the plugin.";
	}
} else {
	if($nRecID){
		// on repost (without errors), set plugin to active
		$nSQL = "UPDATE nPlugins SET nActive=1 WHERE nID='$nRecID' LIMIT 1";
		mysql_select_db($DBName);
		mysql_query($nSQL, $Link);
	}
	exit();
}
if($nErrorARRAY){?>
	
	<div align="center">
		<strong>This component was unable to be activated for the following reasons:</strong><BR>
		<ul><? foreach($nErrorARRAY as $nErrorMsg){ echo "<li>$nErrorMsg</li>"; } ?></ul>
	</div>

<? } else { ?>

<script>
runActivate = function(){
	try{ nLoadPluginsTable(); }catch(e){}
	try{ showNotify('The plugin <strong><?php echo addslashes($nName)?></strong> is now activated.','done') }catch(e){};
	dialogDeleteClose();
	dialogClose();
}
</script>

	<form action="<?=$_SERVER['PHP_SELF']?>" method="post" name="dialogForm" id="dialogForm" onSubmit="return submitFormModal('dialog/<?=$nPageName;?>?id=<?=$nRecID?>','dialogForm','<?=$nRefreshCall;?>');return false;">
		<input type="hidden" name="nUpdateDB" value="1">
		<input type="hidden" name="nRecID" value="<?=$nRecID?>">
		
			<div align="center" style="padding:60px 100px;">
				
				<div><img src="img/loading_l.gif" alt="" width="32" height="32" border="0"></div><BR>
				<strong>Activating plugin:<BR><BR>
				<?php echo $nName?></strong><BR><BR>
				Please wait...
				
			</div>
		
	</form>
<script>
setTimeout("submitActivateForm()", 2000);
submitActivateForm = function(){
	$('#dialogForm').submit();
}
</script>
<? } // end ErrorARRAY ?>
<?php
function nDeletePlugin($nPluginID){
	global $DBName,$Link;
	global $nPluginPath;
	if(!$nPluginID){ return; }
	$nMasterPluginPath = "$nPluginPath$nPluginID/";
	// Remove folder
	if(is_dir("$nMasterPluginPath")){ nRecursiveDelete("$nMasterPluginPath"); }
	// Delete all component records
	$Query = "DELETE FROM nPluginComp WHERE nPluginID='$nPluginID'";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
	// Remove record
	$Query = "DELETE FROM nPlugins WHERE nPluginID='$nPluginID' LIMIT 1";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
}
function createFileComponent($nComponentName,$nComponentFile){
	global $DBName,$Link;
	global $nPluginPath,$nPluginID;
	if(!$nComponentName || !$nComponentFile){ return 0; }
	if(!$nPluginID){ return 0; }
	// Check if it exists first
	$nSQL = "SELECT nPluginID FROM nPluginComp WHERE nPluginID='$nPluginID' AND nCompCode='$nComponentName' AND nFilePath='$nComponentFile' LIMIT 1";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	if($Row = mysql_fetch_array($Result)){
		return 1; // record already exists, just return
	}
	// Verify file exists first
	$nMasterFilePath = "$nPluginPath$nPluginID/$nComponentFile";
	if(!file_exists("$nMasterFilePath")){ return 0; }
	// Save new component to database
	$Query = "INSERT INTO nPluginComp VALUES ('0','$nPluginID','$nComponentName','$nComponentFile','')";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
	return 1; // true
}
function createFunctionComponent($nComponentName,$nComponentFunction){
	global $DBName,$Link;
	global $nPluginID;
	if(!$nComponentName || !$nComponentFunction){ return "Error(createFunctionComponent): nComponentName or nComponentFunction not defined."; }
	if(!$nPluginID){ return "Error(createFunctionComponent): nPluginID not defined."; }
	if(!function_exists($nPluginID."_".$nComponentFunction)){ return "Error(createFunctionComponent): function does not exist (".$nPluginID."_".$nComponentFunction.")."; }
	// Check if it exists first
	$nSQL = "SELECT nPluginID FROM nPluginComp WHERE nPluginID='$nPluginID' AND nCompCode='$nComponentName' AND nFunction='$nComponentFunction' LIMIT 1";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	if($Row = mysql_fetch_array($Result)){
		return 1; // record already exists, just return
	}
	// Save new component to database
	$Query = "INSERT INTO nPluginComp VALUES ('0','$nPluginID','$nComponentName','','$nComponentFunction')";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
	return 1; // true
}
function addToPluginNav($nNavName,$nNavPage){
	global $DBName,$Link;
	global $nPluginPath,$nPluginID;
	if(!$nNavName || !$nNavPage){ return "Error(addToPluginNav): nNavName or nNavPage not defined."; }
	if(!$nPluginID){ return "Error(addToPluginNav): nPluginID not defined."; }
	// make sure nav page exists first
	$nMasterFilePath = "$nPluginPath$nPluginID/$nNavPage";
	if(!file_exists("$nMasterFilePath")){ return "Error(addToPluginNav): file ($nMasterFilePath) does not exist."; }
	// Remove all existing nav pages matching this name for this plugin first (only one allowed)
	$Query = "DELETE FROM nPluginNav WHERE nPluginID='$nPluginID' AND nNavName='$nNavName' AND nNavType='plugins'";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
	// Save new item to database
	$Query = "INSERT INTO nPluginNav VALUES ('0','$nPluginID','$nNavName','$nNavPage','plugins')";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
	return 1; // true
}

// list name, position:place after which column name, column title, function name to call for this column (will be sent ID of item as only parameter)
function addToList($nListName,$nPosition,$nColumnTitle,$nComponentFunction){
	global $DBName,$Link;
	global $nPluginID;
	if(!$nListName || !$nComponentFunction){ return "Error(addToList): nListName or nComponentFunction not defined."; }
	if(!$nPluginID){ return "Error(addToList): nPluginID($nPluginID) not defined."; }
	if(!function_exists($nPluginID."_".$nComponentFunction)){ return "Error(addToList): function does not exist (".$nPluginID."_".$nComponentFunction.")."; }
	// change to real list inclusion name
	switch(strtolower($nListName)){
		case "orders": $nListName="Orders"; break;
		case "products": $nListName="Products"; break;
		case "categories": $nListName="Categories"; break;
		case "groups": $nListName="Groups"; break;
		case "customers": $nListName="Customers"; break;
		case "discounts": $nListName="Discounts"; break;
		default: return "Error(addToList): nListName not valid.";
	}
	// Remove all existing inclusion points matching this one
	$Query = "DELETE FROM nPluginInclusionPoint WHERE nPluginID='$nPluginID' AND nInclusionType='List' AND nInclusionPoint='$nListName' AND nSetting1='$nPosition' AND nSetting2='$nColumnTitle' AND nSetting3='$nComponentFunction'";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
	// Save new item to database
	$Query = "INSERT INTO nPluginInclusionPoint VALUES ('0','$nPluginID','List','$nListName','$nPosition','$nColumnTitle','$nComponentFunction','','')";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
	return 1; // true
}
// page name, position:place after which section name, section title, function name to call for this column (will be sent ID of item as only parameter)
function addToBackOfficePage($nPageName,$nPosition,$nSectionTitle,$nComponentFunction){
	global $DBName,$Link;
	global $nPluginID;
	if(!$nPageName || !$nComponentFunction){ return "Error(addToBackOfficePage): nPageName or nComponentFunction not defined."; }
	if(!$nPluginID){ return "Error(addToBackOfficePage): nPluginID($nPluginID) not defined."; }
	if(!function_exists($nPluginID."_".$nComponentFunction)){ return "Error(addToBackOfficePage): function does not exist (".$nPluginID."_".$nComponentFunction.")."; }
	// change to real list inclusion name
	switch(strtolower($nPageName)){
		case "modifycustomer": $nPageName="ModifyCustomer"; break;
		case "modifydiscount": $nPageName="ModifyDiscount"; break;
		default: return "Error(addToBackOfficePage): nPageName not valid.";
	}
	// Remove all existing inclusion points matching this one
	$Query = "DELETE FROM nPluginInclusionPoint WHERE nPluginID='$nPluginID' AND nInclusionType='BackOfficePage' AND nInclusionPoint='$nPageName' AND nSetting1='$nPosition' AND nSetting2='$nSectionTitle' AND nSetting3='$nComponentFunction'";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
	// Save new item to database
	$Query = "INSERT INTO nPluginInclusionPoint VALUES ('0','$nPluginID','BackOfficePage','$nPageName','$nPosition','$nSectionTitle','$nComponentFunction','','')";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
	return 1; // true
}
// page name, location on page, column title, function name to call for this column (will be sent ID of item as only parameter)
function addToStorePage($nPageName,$nLocation,$nComponentFunction){
	global $DBName,$Link;
	global $nPluginID;
	if(!$nPageName || !$nComponentFunction){ return "Error(addToStorePage): nPageName or nComponentFunction not defined."; }
	if(!$nPluginID){ return "Error(addToStorePage): nPluginID($nPluginID) not defined."; }
	if(!function_exists($nPluginID."_".$nComponentFunction)){ return "Error(addToStorePage): function does not exist (".$nPluginID."_".$nComponentFunction.")."; }
	// change to real page inclusion name
	switch(strtolower($nPageName)){
		case "all": $nPageName="AllPages"; break;
		case "ordercomplete": $nPageName="OrderComplete"; break;
		case "myaccount": $nPageName="MyAccount"; break;
		case "cart": $nPageName="Cart"; break;
		case "checkoutshipping": $nPageName="CheckoutShipping"; break;
		case "checkoutpayment": $nPageName="CheckoutPayment"; break;
		case "checkoutfinal": $nPageName="CheckoutFinal"; break;
		default: return "Error(addToStorePage): nPageName not valid.";
	}
	if($nLocation){ $nLocation = strtolower($nLocation); }
	// Remove all existing inclusion points matching this one
	$Query = "DELETE FROM nPluginInclusionPoint WHERE nPluginID='$nPluginID' AND nInclusionType='StorePage' AND nInclusionPoint='$nPageName' AND nSetting1='$nLocation' AND nSetting2='$nComponentFunction'";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
	// Save new item to database
	$Query = "INSERT INTO nPluginInclusionPoint VALUES ('0','$nPluginID','StorePage','$nPageName','$nLocation','$nComponentFunction','','','')";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
	return 1; // true
}
function createSettingsForm($nSettingsFunction){
	global $DBName,$Link;
	global $nPluginID;
	if(!$nSettingsFunction){ return 0; }
	if(!$nPluginID){ return 0; }
	if(!function_exists($nPluginID."_".$nSettingsFunction)){ return 0; }
	// Remove all existing forms for this plugin first (only one allowed)
	$Query = "DELETE FROM nPluginSettingsForm WHERE nPluginID='$nPluginID'";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
	// Save new component to database
	$Query = "INSERT INTO nPluginSettingsForm VALUES ('0','$nPluginID','$nSettingsFunction')";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
	return 1; // true
}
?>
<? require("../inc/db_close.php"); ?>
