<?php
/*
fiemeCP - BETA.
Written by, and managed by xDario.

The script may be altered at wish, but redistributing any part of it, even with credets, is prohibited. Currently, only myself and my website (http://www.dario-ml.com/) along with kryptodev are aloud to host my script.

©2011 xDario (created for Ohai Online)

You must provide a thanks on the page from which this is called, unless you have donated. If I find my class used without copyright, or a mention of appreciation, I will pursude you. Fiesta servers aren't thousands, so you're not hard to spot out.

ADMIN SECTION!
Page: settings.php
Last Change: 30 June 2011
*/

define('IN_FIEME', 1);

require_once "admin_global.php";

$settings_page = @$_GET['page'];

if ($settings_page == "patches")
{
	if (@$_POST['do'] == "add_patch")
	{
		sqlsrv_query($fiemecp->dbacc, "INSERT INTO [Account].[dbo].[fPatches] ([title], [notes], [patch_name], [postedby], [time], [ip_poser], [deleted], [only_admin]) VALUES (?,?,?,?,?,?,'0','0')", array($_POST['title'], $_POST['content'], $_POST['patch_name'], $_SESSION['fieme_a_uname'], time(), $_SERVER['REMOTE_ADDR']), array( "Scrollable" => SQLSRV_CURSOR_KEYSET));
	}
	
	$current_patches_content = "";
	
	$query = sqlsrv_query($fiemecp->dbacc, "SELECT TOP 50 * FROM dbo.fPatches WHERE deleted = 0", array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET));
	while ($row = sqlsrv_fetch_array( $query, SQLSRV_FETCH_ASSOC))
	{
		$row['time'] = date("jS M Y \a\\t H:i", $row['time']);
		eval("\$current_patches_content .= \"" . $template->get("admin.settings.patches.patches.repeat") . "\";");
	}
	
	eval("\$current_patches = \"" . $template->get("admin.settings.patches.patches") . "\";");
	
	
	eval("\$add_patch_form = \"" . $template->get("admin.settings.patches.add") . "\";");
	
	eval("\$content = \"" . $template->get("admin.settings.patches.main") . "\";");
}
elseif ($settings_page == "configuration")
{
	$content = "";
	if (isset($_GET['config']))
	{
		if (!empty($_POST))
		{
			foreach ($_POST as $key=>$value)
			{
				if (preg_match("/^s_/i", $key))
				{
					if ($_POST["d$key"] != $value)//If it's changed...
					{
						$option_name = substr($key, 2);
						sqlsrv_query($fiemecp->dbacc, "UPDATE dbo.fSettings SET setting_value = ? WHERE setting_group = ? AND setting_save_name = ?", array($value, $_POST['_save_settings'], $option_name), array( "Scrollable" => SQLSRV_CURSOR_KEYSET));
					}
				}
			}
			
		}
		
		$core->refresh_settings($_GET['config']);
		
		$query = sqlsrv_query($fiemecp->dbacc, "SELECT * FROM dbo.fSettings WHERE setting_group = ?", array($_GET['config']), array( "Scrollable" => SQLSRV_CURSOR_KEYSET));
		if (sqlsrv_num_rows($query) > 0)
		{
			$i = 1;
			$content = "<h2>Editing <u>$_GET[config]</u> settings</h2>
			<form method=\"POST\" action=\"settings.php?page=configuration&config=$_GET[config]\">
			<input type=\"hidden\" name=\"_save_settings\" value=\"$_GET[config]\">";
			while ($row = sqlsrv_fetch_array($query))
			{
				if ($row['setting_form_type'] == "text")
				{
					$row['setting_form'] = "<input type=\"text\" size=\"50\" name=\"s_$row[setting_save_name]\" value=\"$row[setting_value]\">";
					$row['setting_form'] .="<input type=\"hidden\" name=\"ds_$row[setting_save_name]\" value=\"$row[setting_value]\">";
				}
				elseif ($row['setting_form_type'] == "textarea")
				{
					$row['setting_form'] = "<textarea name=\"s_$row[setting_save_name]\" cols=\"65\" rows=\"8\">$row[setting_value]</textarea>";
					$row['setting_form'] .="<input type=\"hidden\" name=\"ds_$row[setting_save_name]\" value=\"$row[setting_value]\">";
				}
				elseif ($row['setting_form_type'] == "radio")
				{
					
					eval("\$test = $row[setting_form];");
					
					
					$row['setting_form'] = "";
					foreach ($test as $option=>$value)
					{
						if ($option == $row['setting_value'])
						{
							$row['setting_form'] .="<input type=\"radio\" name=\"s_$row[setting_save_name]\" value=\"$option\" checked=\"checked\">$value";
						}
						else
						{
							$row['setting_form'] .="<input type=\"radio\" name=\"s_$row[setting_save_name]\" value=\"$option\">$value";
						}
					}
					$row['setting_form'] .="<input type=\"hidden\" name=\"ds_$row[setting_save_name]\" value=\"$row[setting_value]\">";
					
				}
				else
				{
					continue;
				}
				
				$content .= "<div class=\"options$i\"><b><u>$row[setting_name]</u></b><br />
				<div style=\"font-size: 85%; color: #333;\">$row[setting_desc]</div><br />
				$row[setting_form]</div>";
				if ($i == 1)
				{
					$i = 2;
				}
				else
				{
					$i = 1;
				}
			}
			$content .= "<input type=\"submit\" value=\"Save Settings\"></form>";
		}
		else
		{
			$content = "Invalid";
		}
	}
	else
	{
		//get categories
		$query = sqlsrv_query($fiemecp->dbacc, "SELECT setting_group FROM fSettings GROUP BY setting_group", array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET));
		$content = "<h2>Settings:</h2>";
		while ($row = sqlsrv_fetch_array($query))
		{
			$content .= "<a href=\"settings.php?page=configuration&config=$row[setting_group]\">$row[setting_group]</a><br />";
		}
	}
}
elseif ($settings_page == "plugins")
{
	//////////////////////////////////////////////////////////
	///////////ACTIVATE AND INSTALL///////////////////////////
	//////////////////////////////////////////////////////////
	if (@$_GET['do'] == 'ac')
	{
		include FIEME_ROOT . 'cache/plugins';
		
		$plugin_name = @$_GET['what'];
		//Check that it's all present!!
		if (file_exists(FIEME_ROOT . "includes/plugins/".$plugin_name.".php"))
		{
			require_once FIEME_ROOT . "includes/plugins/".$plugin_name.".php";
			if (function_exists($plugin_name."_information") && function_exists($plugin_name."_activate") && function_exists($plugin_name."_deactivate"))
			{
				//check compatibility
				$info = call_user_func($plugin_name . "_information");
				if (is_array($info['fieme_rev']))
				{
					foreach($info['fieme_rev'] as $key=>$compat)
					{
						$compat = str_replace(".", "\.", str_replace("*", ".*", $compat));
						if (preg_match("/^".$compat."/", FIEME_REV))
						{
							$foo = 1;
						}
					}
				}
				else
				{
					$info['fieme_rev'] = str_replace(".", "\.", str_replace("*", ".*", $info['fieme_rev']));
					if (preg_match("/^".$info['fieme_rev']."/", FIEME_REV))
					{
						$foo = 1;
					}
				}
				
				if (isset($foo)) // if it's compatible...
				{
					//VALID PLUGIN! Time to add it to the server
					//Check it's not already added...
					
					$query = sqlsrv_query($fiemecp->dbacc, "SELECT * FROM dbo.fPlugins WHERE [plugin_name] = ?", array($plugin_name), array( "Scrollable" => SQLSRV_CURSOR_KEYSET));
					if (sqlsrv_num_rows($query) == 0)
					{
						sqlsrv_query($fiemecp->dbacc, "INSERT INTO fPlugins ([plugin_name] ,[active]) VALUES (?, '1')", array($plugin_name));
					}
					else
					{
						sqlsrv_query($fiemecp->dbacc, "UPDATE fPlugins SET active='1' WHERE plugin_name=?", array($plugin_name));
					}
					
					call_user_func($plugin_name . "_activate");
					
					header("Location: settings.php?page=plugins");
				}
				else
				{
					$error = "The plugin is not compatible with your version of FiemeCP.";
				}
				
			}
			else
			{
				//NOT VALID :(.
				$error = "The plugin is invalid! Contact the developer!<br /><br />";		  
			}
		}
		else
		{
			$error = "Plugin does not exist!<br /><br />";
		}
	}
	elseif (@$_GET['do'] == 'install')
	{
		include FIEME_ROOT . 'cache/plugins';
		
		$plugin_name = @$_GET['what'];
		//Check that it's all present!!
		if (file_exists(FIEME_ROOT . "includes/plugins/".$plugin_name.".php"))
		{
			require_once FIEME_ROOT . "includes/plugins/".$plugin_name.".php";
			if (function_exists($plugin_name."_information") && function_exists($plugin_name."_activate") && function_exists($plugin_name."_deactivate") && function_exists($plugin_name."_install") && function_exists($plugin_name."_uninstall"))
			{
				//check compatibility
				$info = call_user_func($plugin_name . "_information");
				if (is_array($info['fieme_rev']))
				{
					foreach($info['fieme_rev'] as $key=>$compat)
					{
						$compat = str_replace(".", "\.", str_replace("*", ".*", $compat));
						if (preg_match("/^".$compat."/", FIEME_REV))
						{
							$foo = 1;
						}
					}
				}
				else
				{
					$info['fieme_rev'] = str_replace(".", "\.", str_replace("*", ".*", $info['fieme_rev']));
					if (preg_match("/^".$info['fieme_rev']."/", FIEME_REV))
					{
						$foo = 1;
					}
				}
				
				if (isset($foo)) //if compatible...
				{
					//VALID PLUGIN! Time to add it to the server
					//Check it's not already added...
					
					$query = sqlsrv_query($fiemecp->dbacc, "SELECT * FROM dbo.fPlugins WHERE [plugin_name] = ?", array($plugin_name), array( "Scrollable" => SQLSRV_CURSOR_KEYSET));
					if (sqlsrv_num_rows($query) == 0)
					{
						sqlsrv_query($fiemecp->dbacc, "INSERT INTO fPlugins ([plugin_name] ,[active]) VALUES (?, '1')", array($plugin_name));
					}
					else
					{
						sqlsrv_query($fiemecp->dbacc, "UPDATE fPlugins SET active='1' WHERE plugin_name=?", array($plugin_name));
					}
					
					call_user_func($plugin_name . "_install");
					
					call_user_func($plugin_name . "_activate");
					
					header("Location: settings.php?page=plugins");
				}
				else
				{
					$error = "The plugin is not compatible with your version of FiemeCP.";
				}
			}
			else
			{
				//NOT VALID :(.
				$error = "The plugin is invalid! Contact the developer!<br /><br />";		  
			}
		}
		else
		{
			$error = "Plugin does not exist!<br /><br />";
		}
	}
	//////////////////////////////////////////////////////////
	///////////DEACTIVATE AND UNINSTALL///////////////////////
	//////////////////////////////////////////////////////////
	elseif (@$_GET['do'] == 'deac')
	{
		include FIEME_ROOT . 'cache/plugins';
		
		$plugin_name = @$_GET['what'];
		//Check that it's all present!!
		if (file_exists(FIEME_ROOT . "includes/plugins/".$plugin_name.".php"))
		{
			require_once FIEME_ROOT . "includes/plugins/".$plugin_name.".php";
			if (function_exists($plugin_name."_information") && function_exists($plugin_name."_activate") && function_exists($plugin_name."_deactivate"))
			{
				//VALID PLUGIN! Time to add it to the server
				//Check it's not already added...
				
				if (function_exists($plugin_name."_install") && function_exists($plugin_name."_uninstall")) //So, it's been installed...
				{
					$towhat = "2";
				}
				else
				{
					$towhat = "0";
				}
				
				$query = sqlsrv_query($fiemecp->dbacc, "SELECT * FROM dbo.fPlugins WHERE [plugin_name] = ?", array($plugin_name), array( "Scrollable" => SQLSRV_CURSOR_KEYSET));
				if (sqlsrv_num_rows($query) > 0)
				{
					sqlsrv_query($fiemecp->dbacc, "UPDATE fPlugins SET active='$towhat' WHERE plugin_name=?", array($plugin_name));
				}
				
				call_user_func($plugin_name . "_deactivate");
				
				header("Location: settings.php?page=plugins");
			}
			else
			{
				//NOT VALID :(.
				$error = "The plugin is invalid! Contact the developer!<br /><br />";		  
			}
		}
		else
		{
			$error = "Plugin does not exist!<br /><br />";
		}
	}
	elseif (@$_GET['do'] == 'uninstall')
	{
		include FIEME_ROOT . 'cache/plugins';
		
		$plugin_name = @$_GET['what'];
		//Check that it's all present!!
		if (file_exists(FIEME_ROOT . "includes/plugins/".$plugin_name.".php"))
		{
			require_once FIEME_ROOT . "includes/plugins/".$plugin_name.".php";
			if (function_exists($plugin_name."_information") && function_exists($plugin_name."_activate") && function_exists($plugin_name."_deactivate") && function_exists($plugin_name."_uninstall"))
			{
				$query = sqlsrv_query($fiemecp->dbacc, "SELECT * FROM dbo.fPlugins WHERE [plugin_name] = ?", array($plugin_name), array( "Scrollable" => SQLSRV_CURSOR_KEYSET));
				if (sqlsrv_num_rows($query) > 0)
				{
					$row = sqlsrv_fetch_array($query);
					sqlsrv_query($fiemecp->dbacc, "UPDATE fPlugins SET active='0' WHERE plugin_name=?", array($plugin_name));
				}
				
				if ($row['active'] != 2)
				{
					call_user_func($plugin_name . "_deactivate");
				}
				
				call_user_func($plugin_name . "_uninstall");
				
				header("Location: settings.php?page=plugins");
			}
			else
			{
				//NOT VALID :(.
				$error = "The plugin is invalid! Contact the developer!<br /><br />";		  
			}
		}
		else
		{
			$error = "Plugin does not exist!<br /><br />";
		}
	}
	//////////////////////////////////////////////////////////
	///////////GENERAL ///////////////////////////////////////
	//////////////////////////////////////////////////////////
	elseif (@$_GET['do'] == 'ask_uninstall')
	{
		$error = "Are you sure you want to deinstall $_GET[what]? <a href='settings.php?page=plugins&do=uninstall&what=$_GET[what]'>Yes</a> || <a href='settings.php?page=plugins'>No</a><br /><br />";
	}
	//after edits up there..
	$core->update_plugins();
	
	$content = @$error . "<table width=\"100%\" rules=\"all\" cellspacing=\"5\" cellpadding=\"5\" style=\"text-align: center\">";
	
	if ($handle = opendir(FIEME_ROOT.'includes/plugins/'))
	{
		while (false !== ($file = readdir($handle)))
		{
			if ($file != "." && $file != ".." && preg_match('/\.php$/i', $file))
			{
				$plugin_name = substr($file, 0, strlen($file) - 4);
				
				include FIEME_ROOT . 'cache/plugins';
				
				if (isset($p_c['active']["$plugin_name"]) || isset($p_c['deactive']["$plugin_name"])) 
				{
					if (!function_exists($plugin_name . "_information"))
					{
						$na_bi = 1; //not active, but installed
						require_once FIEME_ROOT.'includes/plugins/'.$file;
					}
					//the plugin is activated, because it's already been loaded!
					$info = call_user_func($plugin_name . "_information");
					
					//check if needs to be installed, deinstalled, or what..
					if (function_exists($plugin_name . "_install"))
					{
						$a_d = "<td><a href='settings.php?page=plugins&do=ask_uninstall&what=$plugin_name'>Uninstall</a></td>";
						if (isset($na_bi))
						{
							$a_d .= "<td><a href='settings.php?page=plugins&do=ac&what=$plugin_name'>Activate</a></td>";
							$a_c = "#CCCCFF";
						}
						else
						{
							$a_d .= "<td><a href='settings.php?page=plugins&do=deac&what=$plugin_name'>Deactivate</a></td>";
							$a_c = "#CCFFCC";
						}
					}
					else
					{
						$a_d = "<td colspan=\"2\"><a href='settings.php?page=plugins&do=deac&what=$plugin_name'>Deactivate</a></td>";
						$a_c = "#CCFFCC";
					}
					
					if (is_array($info['fieme_rev']))
					{
						$required_fieme = "";
						foreach ($info['fieme_rev'] as $rev)
						{
							if ($required_fieme != "")
							{
								$required_fieme .= " // ";
							}
							$required_fieme .= $rev;
						}
					}
					else
					{
						$required_fieme = $info['fieme_rev'];
					}
					
					$content .= "<tr BGCOLOR=\"$a_c\"><td width='70%'><strong><u>$info[name] $info[version]</u></strong> <span style=\"font-siz:90%;\">Active (from: $file)</font><br />
					Created by: $info[created_by] | Fieme Required: $required_fieme<br />
					$info[about]</td>$a_d<br />";
				}
				else
				{
					//since the plugin name information function is not present, the function is not yet installed!
					
					require_once FIEME_ROOT.'includes/plugins/'.$file;
					
					$info = call_user_func($plugin_name . "_information");
					
					//check if needs to be installed, deinstalled, or what..
					if (function_exists($plugin_name . "_install"))
					{
						$a_d = "<td colspan=\"2\"><a href='settings.php?page=plugins&do=install&what=$plugin_name'>Install & Activate</a></td>";
					}
					else
					{
						$a_d = "<td colspan=\"2\"><a href='settings.php?page=plugins&do=ac&what=$plugin_name'>Activate</a></td>";
					}
					
					if (is_array($info['fieme_rev']))
					{
						$required_fieme = "";
						foreach ($info['fieme_rev'] as $rev)
						{
							if ($required_fieme != "")
							{
								$required_fieme .= " // ";
							}
							$required_fieme .= $rev;
						}
					}
					else
					{
						$required_fieme = $info['fieme_rev'];
					}
					
					$content .= "<tr BGCOLOR=\"#FFCCCC\"><td width='70%'><strong><u>$info[name] $info[version]</u></strong> <span style=\"font-siz:90%;\">Not running (from: $file)</font><br />
					Created by: $info[created_by] | Fieme Required: $required_fieme<br />
					$info[about]</td>$a_d<br />";
				}
				
			}
		}
		closedir($handle);
	}
	
	$content .= "</table>";
}
else
{
	//default action
	eval("\$content = \"" . $template->get("admin.settings.basic.main") . "\";");
	$plugin->run_hooks("settings_extra");
}

eval("\$links = \"" . $template->get("admin.settings.links") . "\";");
eval("\$page_settings = \"" . $template->get("admin.settings") . "\";");
output_page($page_settings, $settings->dev_mode_admin);