<?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.

Page: class.plugins.php
Last Change: 20 June 2011
*/

class fieme_plugins
{
	//What hooks will we use?
	public $hooks;
	
	//The hooks we are currently running
	public $currently_running;

	//load up the installed, and activated plugins.
	function load()
	{
		global $cache, $plugin, $core;
		
		if ($cache->read("plugins", 0) === FALSE)
		{
			if (!file_exists(FIEME_ROOT . "cache//" . "plugins"))
			{
				$core->update_plugins();
			}
			else
			{
				$core->run_background(FIEME_ROOT . "cache\cache_files\\refresh_plugins.php");
			}
		}
		
		require_once FIEME_ROOT . 'cache/plugins';
		if(@is_array($p_c['active'])) //p_c = plugin_cache - Defined in the plugin!
		{
			foreach($p_c['active'] as $p=>$v)
			{
				if($p != "" && file_exists(FIEME_ROOT."includes/plugins/".$p.".php"))
				{
					require_once FIEME_ROOT."includes/plugins/".$p.".php";
					if (FIEME_DEV == 1)
					{
						global $dev_content;
						$dev_content .= "included plugin: $v!<br />";
					}
				}
			}
		}
	}

	//Run by the plugins, telling us what plugins will be run, and when.
	function add_hook($where, $function, $priority=50)
	{
		// Stops the defining, if the hook is already installed.
		if(@$this->hooks[$where][$priority][$function] == 1)
		{
			return true;
		}

		// Add the hook
		$this->hooks[$where][$priority][$function] = $function;
		if (FIEME_DEV == 1)
		{
			global $dev_content;
			$dev_content .= "defined $function@$where<br />";
		}
		return true;
	}

	//Let's run all my hooks.
	function run_hooks($where, $arguments="")
	{
		if(!is_array(@$this->hooks[$where]))
		{
			return $arguments;
		}
		$this->current_hook = $where;
		ksort($this->hooks[$where]);
		foreach($this->hooks[$where] as $priority => $function)
		{
			if(is_array($function))
			{
				foreach($function as $functions)
				{
					$returnargs = call_user_func_array($functions, array(&$arguments));
					
					
					if($returnargs)
					{
						$arguments = $returnargs;
					}
				}
			}
		}
		$this->current_hook = '';
		return $arguments;
	}
	
	//Remove the hook
	function remove_hook($where, $function, $priority=10)
	{
		// Check to see if we don't already have this hook running at this priority
		if(!isset($this->hooks[$where][$priority][$function]))
		{
			return true;
		}
		unset($this->hooks[$where][$priority][$function]);
	}
}

?>