<?php

/**------------------------------------------------
*
* Rooty, 2018 <rooty@rooty.me>
*
*
* This software is protected by copyright, please
* read the file COPYRIGHT.
* This program is distributed in the hope that it
* will be useful, but without any warranty; without
* even the implied warranty of merchantability or
* fitness for a particular purpose. Please
* read the file LICENCE.
*
*Fichier PluginManager.php
*
*Ce fichier contient la classe PluginManager
*
* @author Rooty <www.rooty.me|rooty@rooty.me>
* @link www.rooty.me
* @since 2004/04/14
* @version 0.3
* @package system_plugin
* @copyright Copyright &copy; 2009-2018, Rooty
*/

####################################################################################################
################################# Parametrage
#####################################################################################################
/**
* declaration du system... si cette variable n'est pas définie, les fichier inclus ne marche pas...
* cette variable est déclarée sur la page d'execution du script.
*<p> if ( !defined('SYSTEM_IN') )
* {
* die("Hacking attempt");
* }</p>
*/
if ( !defined('SYSTEM_IN') )
{
 die("Hacking attempt");
}

/**
* Appel à la classe Plugin...
*/
require ($CONF_LIB_PATH."lib/system/system_plugin/plugin.php");

/**
* Classe PluginManager
*
* <p>Classe de gestion des instances de Plugins <br>
*   (permet de fabriquer, detruire, mettre à jour des objet Plugins)<br>
* Cette classe se sert des ressources à sa disposition (dans lib/plugins)<br><br>
* </p>
* @package system_plugin
*/
class PluginManager
{


/**
* @access private
* @var chaine
* @desc chaine
*/
var $sourceDir		= null;

var $tabPlugs		=null;

	//
	// Constructor
	//

/**
*Constructeur de la classe PluginManager
*
*<p>Initialise le bon objet (ressource) ... <br>chacun de ces objets doit avoir les mêmes methodes (equivalente aux methodes sourceXXX)</p>
*<p>L'objet PluginManager ne doit servir qu'a la récuperation des Plugin</p>
* @access public
*/
	function __construct()
	{
		global $CONF_LIB_PATH;
		$this->setSourceDir($CONF_LIB_PATH."lib/object_plugin/");
	}

/**
* getSourceDir : récupere le repertoire des plugin à insérer
* @access public
* @return chaine nom du repertoire d'inclusion
*/
	function getSourceDir()
	{
		return $this->sourceDir;
	}

/**
* setSourceDir : affecte le repertoire des plugin à insérer
* @access public
* @return booleen
*/
	function setSourceDir($path)
	{
		$this->sourceDir=$path;
		return true;
	}

/**
* doListPlugin : récupere le nom de tous les plugin disponibles
* la liste est éventuellment stovké ds tabPlugs
* @access public
* @return booleen
*/
	function doListPlugin()
	{
		$i=0;
		$tab=array();
		if ($this->tabPlugs!=null)
			return $this->tabPlugs;

		if (is_dir($this->sourceDir)) {
		   if ($dh = opendir($this->sourceDir)) {
			while (($obj = readdir($dh)) !== false) {
				if (is_dir($this->sourceDir.$obj))
					if (file_exists($this->sourceDir.$obj."/".$obj.".php")){
						$tab[$i]=$obj;
						$i++;
					}
			}
			closedir($dh);
		   }
		}
		// trie par ordre alpha en comptant les a. et en les supprimant
		asort($tab);
		reset($tab);
		// TODO : mb_string, supprimer les a*.
		foreach($tab as $key=>$val){
// 			$tab[$key]=eregi_replace("^[[:space:]]*[a-z]+[.)".chr(176)."][[:space:]]+", "", $val);
            $tab[$key]=mb_eregi_replace("^[\s]*[a-z]+[.)°][\s]+", "", $val);
//             $tab[$key]=mb_eregi_replace("^[/\s/]+", "", $val);
//             $tab[$key]=$val;
		}
		$this->tabPlugs=$tab;
		return $tab;
	}

/**
* doLoadPlugin : charge pour tous les plugin sous la forme $GLOBALS['SYSTEM_PLUGIN']['nomduplugin']=new Plugin();
* @access public
* @return booleen
*/
	function doLoadPlugin()
	{
		$GLOBALS['SYSTEM_PLUGIN']=array();	/* Déclaration de la variable globale des plugin */
		$tab=$this->doListPlugin();
		for ($i=0; $i<count($tab); $i++)
		{
			/* On suppose que le plugin existent (test dans doListPlugin) */
			include_once($this->sourceDir.$tab[$i]."/".$tab[$i].".php");
			/* On créer une instance pour le plugin sous SYSTEM_PLUGIN */
			$GLOBALS["SYSTEM_PLUGIN"][$tab[$i]]=new $tab[$i]();
		}
		return true;
	}
/**
* doBeforeSessionStart : appel pour tous les plugin leur methode __beforeSessionStart()
* @access public
* @return booleen
*/
	function doBeforeSessionStart()
	{
		if (!Isset($GLOBALS['SYSTEM_PLUGIN']))
			$this->doLoadPlugin();

		$tab=$this->doListPlugin();

		for ($i=0; $i<count($tab); $i++)
		{
			$GLOBALS["SYSTEM_PLUGIN"][$tab[$i]]->__beforeSessionStart();
		}
		return true;
	}
/**
* doBeforeProcess : appel pour tous les plugin leur methode __beforeProcess()
* @access public
* @return booleen
*/
	function doBeforeProcess()
	{
		if (!Isset($GLOBALS['SYSTEM_PLUGIN']))
			$this->doLoadPlugin();
		$tab=$this->doListPlugin();

		for ($i=0; $i<count($tab); $i++)
		{
			$GLOBALS["SYSTEM_PLUGIN"][$tab[$i]]->__beforeProcess();
		}
		return true;
	}

/**
* doBeforeSessionStart : appel pour tous les plugin leur methode __beforeSessionStart()
* @access public
* @return booleen
*/
	function doBeforeExec()
	{
		if (!Isset($GLOBALS['SYSTEM_PLUGIN']))
			$this->doLoadPlugin();

		$tab=$this->doListPlugin();

		for ($i=0; $i<count($tab); $i++)
		{
			$GLOBALS["SYSTEM_PLUGIN"][$tab[$i]]->__beforeExec();
		}
		return true;
	}

/**
* doBeforeSessionStart : appel pour tous les plugin leur methode __beforeSessionStart()
* @access public
* @return booleen
*/
	function doAfterExec()
	{
		if (!Isset($GLOBALS['SYSTEM_PLUGIN']))
			$this->doLoadPlugin();

		$tab=$this->doListPlugin();

		for ($i=0; $i<count($tab); $i++)
		{
			$GLOBALS["SYSTEM_PLUGIN"][$tab[$i]]->__afterExec();
		}
		return true;
	}

/**
* doAfterProcess : appel pour tous les plugin leur methode __afterProcess()
* @access public
* @return booleen
*/
	function doAfterProcess()
	{
		if (!Isset($GLOBALS['SYSTEM_PLUGIN']))
			$this->doLoadPlugin();
		$tab=$this->doListPlugin();

		for ($i=0; $i<count($tab); $i++)
		{
			$GLOBALS["SYSTEM_PLUGIN"][$tab[$i]]->__afterProcess();
		}
		return true;
	}
/**
* doAfterProcess : appel pour tous les plugin leur methode __afterProcess()
* @access public
* @return booleen
*/
	function doBeforeSessionClose()
	{
		if (!Isset($GLOBALS['SYSTEM_PLUGIN']))
			$this->doLoadPlugin();
		$tab=$this->doListPlugin();

		for ($i=0; $i<count($tab); $i++)
		{
			$GLOBALS["SYSTEM_PLUGIN"][$tab[$i]]->__beforeSessionClose();
		}
		return true;
	}
}
?>