<?php

	/*
	*	Gekko - Open Source Web Development Framework
	*	------------------------------------------------------------------------
	*	Copyright (C) 2004-2006, J. Carlos Nieto <xiam@users.sourceforge.net>
	*	This program is Free Software.
	*
	*	@package	Gekko
	*	@subpackage	/lib
	*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
	*	@author		J. Carlos Nieto <xiam@users.sourceforge.net>
	*	@link		http://www.gekkoware.org
	*/

	if (!defined("IN-GEKKO")) die("Get a life!");

	Class GekkoPluginSystem {
		var $plugins = Array();
		var $events = Array();

		function LoadPlugins() {

			// plugins
			$plugins = listdir(GEKKO_SOURCE_DIR."lib/plugins");
			foreach ($plugins as $plugin) {
				$file = GEKKO_SOURCE_DIR."lib/plugins/$plugin";
				if (file_exists($file) && !is_dir($file)) {
					include_once $file;

					// initializing plugin class
					$className = strtolower("plugin_".preg_replace("/\..*/", "", $plugin));
					$this->plugins[$className] = new $className;
				}
			}

			// module plugins
			$modules = gekkoModule::getList();
			foreach ($modules as $module) {
				$file = GEKKO_SOURCE_DIR."modules/$module/plugin.inc.php";
				if (file_exists($file)) {
					include_once $file;
					// initializing plugin class
					$className = strtolower("plugin_mod$module");
					$this->plugins[$className] = new $className;
				}
			}

		}
		function Register($plugin, $event) {
			if (!isset($this->events[$event]))
				$this->events[$event] = Array();

			$this->events[$event][] = strtolower($plugin);
		}
		function triggerEvent($event, &$arg1) {
			if (isset($this->events[$event])) {
				foreach ($this->events[$event] as $plugin) {
					if (!defined("IN-INSTALL") && !conf::getkey("core", "plugins.".strtolower($plugin).".disable"))
						$arg1 = call_user_func(array($this->plugins[$plugin], "Invoke"), $event, $arg1);
				}
			}
		}
	}

	$GLOBALS["PluginCore"] = new GekkoPluginSystem();

	$GLOBALS["PluginCore"]->LoadPlugins();
?>