<?php
/**
 * @version $Id: plugin.inc.php v2.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

if(!defined('IN_ADMIN') || !isset($_PBDEV['PHP_SELF']) || !preg_match('/[\/\\\\]admincp\.php$/i', $_PBDEV['PHP_SELF']))
{
	exit('Access Denied');
}
define('PB_CSCRIPT', 'plugin');
allowdo('setplugins');

$actionurl = 'admincp.php?action=plugin';
$navlink = '<strong>&nbsp;&raquo;&nbsp;</strong><a href="admincp.php?action=plugin">'.$cp_message['plugin'].'</a><strong>&nbsp;&raquo;&nbsp;</strong>';

$m_plugin = '';
$plugins = $plugin_lang = array();

!file_exists(PBDIGG_ROOT.'data/cache/cache_pluginsmenu.php') && $Cache->pluginsmenu();
include_once(PBDIGG_ROOT.'data/cache/cache_pluginsmenu.php');

if ($plugin)
{
	if (!preg_match('/^[a-zA-Z]+?$/i', $plugin))
	{
		showMsg($a_message['parameter_error'], $actionurl);
	}
	if (is_dir(PBDIGG_ROOT.'plugins/'.$plugin))
	{
		require_once(PBDIGG_ROOT.'plugins/'.$plugin.'/'.$plugin.'.admin.php');
	}
	$navlink .= $plugin_lang['plugin_title'];
	$actionurl .= '&amp;plugin='.$plugin;
	$TPL->assign('plugin_tpl', '{#include file="'.PBDIGG_ROOT.'plugins/'.$plugin.'/template/'.$plugin.'.admin.html"#}');
	$TPL->assign('showplugin', '1');
	$TPL->assign('plugin', $plugin);
}
else
{
	if ($step == 'del')
	{
		$pid = intval($pid);
		$rs = $DB->db_fetch_one_array("SELECT * FROM {$db_prefix}plugins WHERE pid = $pid");
		if ($DB->db_num())
		{
			if ($rs['dbtables'])
			{
				$dbtables = array_filter(explode(',', $rs['dbtables']));
				foreach ($dbtables as $dbtable)
				{
					if (preg_match("/^pbplugin_/i", $dbtable))
					{
						$DB->db_query("DROP TABLE IF EXISTS `$dbtable`");
					}
				}
			}
			$DB->db_query("DELETE FROM {$db_prefix}plugins WHERE pid = $pid LIMIT 1");
			Deldir(PBDIGG_ROOT.'plugins/'.$rs['pmark']);
			$Cache->plugins();
			$Cache->pluginsmenu();
		}
		showMsg($a_message['plugin_delsuccess'], $actionurl);
	}
	elseif ($step == 'status')
	{
		$pid = intval($pid);
		$status = $status ? 0 : 1;
		$DB->db_query("UPDATE {$db_prefix}plugins SET status = $status WHERE pid = $pid");
		$Cache->plugins();
		showMsg($a_message['plugin_modsuccess'], $actionurl);
	}
	else
	{
		$DB->db_query("SELECT pid, status, pname, pmark FROM {$db_prefix}plugins");
		while ($rs = $DB->db_fetch_array())
		{
			if ($rs['status'])
			{
				$rs['opptagstatus'] = $a_message['close'];
			}
			else
			{
				$rs['opptagstatus'] = $a_message['open'];
			}
			$plugins[] = $rs;
		}
		$TPL->assign('plugins', $plugins);
		$plugin_lang['plugin_title'] = $cp_message['plugin'];
	}
}

$TPL->assign('navlink', $navlink);
$TPL->assign('menu', $m_plugin);
$TPL->assign('plugin_lang', $plugin_lang);
?>