<?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_delete.php - version 1.0
*/

$nAdminOnlyPg = 1;

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

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

// config
$nPageName = "plugin_delete.php";
$nRefreshCall = "runDelete();";
$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){
	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"];
		}
		if($nPluginID){
			nDeletePlugin($nPluginID);
			// Optmization
			$nSQL = "OPTIMIZE TABLE nPlugins";
			mysql_select_db($DBName);
			mysql_query($nSQL, $Link);
			
			// Now that record no longer exists, replace all instances of this ID from system log with name
			$nSQL = "SELECT nID,nEventDesc FROM nSystemEventLog";
			mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
			while($Row = mysql_fetch_array($Result)){
				$nEventID = $Row["nID"];
				$nEventDesc = $Row["nEventDesc"];
				$nNewEventDesc = str_replace("[nPluginID:$nRecID]","<strong>$nName</strong>",$nEventDesc);
				if($nNewEventDesc!=$nEventDesc){ nUpdateEventDesc($nEventID,$nNewEventDesc); }
			}
			
			// log system event
			$nSystemEventDesc = _("Plugin deleted:")." <strong>$nName</strong>";
			$Query = "INSERT INTO nSystemEventLog (nEventTime,nEventDesc,nEditorUser) VALUES ('".date("Y-m-d H:i:s")."','$nSystemEventDesc','$ValidUser')";
			mysql_select_db($DBName); mysql_query($Query, $Link);
		}
	}
	echo "<script>Modalbox.hide();</script>";
	exit();
}
?>
<script>
runDelete = function(){
	try{ nLoadPluginsTable(); }catch(e){}
	try{ showNotify('The plugin <strong><?php echo addslashes($nName)?></strong> was successfully removed.','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?>">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
	<td height="80" align="center" style="padding:10px;">
		
		<table border="0" cellspacing="0" cellpadding="0">
		  <tr>
			<td valign="top" style="padding-right:10px;"><img src="assets/images/icons/question.png" alt="" width="32" height="32" border="0"></td>
			<td class="blueBold" style="padding-right:10px;"><?php echo _("Are you sure you want to delete"); ?> <strong><?php echo $nName?></strong>?</td>
		  </tr>
		</table>
		
	</td>
  </tr>
  <tr>
	<td style="padding:10px; text-align:center;">
	<input type="submit" name="nYes" value="<?php echo _("Delete"); ?>" class="button black"> 
	<input type="button" name="nCancel" value="<?php echo _("Cancel"); ?>" class="button" onClick="dialogDeleteClose();">
	</td>
  </tr>
</table>
</form>
<?php
function nDeletePlugin($nPluginID){
	global $DBName,$Link;
	global $nPluginPath;
	if(!$nPluginID){ return; }
	$nMasterPluginPath = "$nPluginPath$nPluginID/";
	// Remove folder
	if(is_dir("$nMasterPluginPath")){ nRecursiveDelete("$nMasterPluginPath"); }
	// Remove record
	$Query = "DELETE FROM nPlugins WHERE nPluginID='$nPluginID' LIMIT 1";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
	// Remove all associated databases
	mysql_select_db($DBName);
	$Query = "SHOW TABLES FROM $DBName";
	$result = mysql_query($Query);
	while ($Row = mysql_fetch_row($result)) {
	    $nTableName = $Row["0"];
		$pos = strpos($nTableName, $nPluginID);
		if($pos !== false){
			// Remove record
			$Query = "DROP TABLE IF EXISTS $nTableName";
			mysql_select_db($DBName);
			mysql_query($Query, $Link);
		}
	}
	// REMOVE all associated navigation items
	$Query = "DELETE FROM nPluginNav WHERE nPluginID='$nPluginID'";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
	// REMOVE all associated inclusion points
	$Query = "DELETE FROM nPluginInclusionPoint WHERE nPluginID='$nPluginID'";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
}
function nRecursiveDelete($str){
	if(is_file($str)){
		return @unlink($str);
	} elseif(is_dir($str)){
		$scan = glob(rtrim($str,'/').'/*');
		foreach($scan as $index=>$path){
			nRecursiveDelete($path);
		}
		return @rmdir($str);
	}
}
?>
<? require("../inc/db_close.php"); ?>
