<?

/**
 * config.mod.php
 *
 * This file contains a module to load in extra modules and check a
 * configurationfile
 * @package   Modules
 */

/**
 * Name of the module in this file
 */
define("MOD_CONFIG",$name);

/**
 * Name for option modules
 */
define("EXTRA_MODULES",0);
/**
 * Name for option configfile
 */
define("CONFIGFILE",1);

$modules[$name]= new ConfigModule();

/**
 * This class takes some options to add extra modules or to add config
 * options via a configfile
 *
 * This module is used to dynamically insert modules that are needed for a
 * specific form. There are 2 ways of including these modules:
 * 1. AUTO_CONFIGURATION: all modules are scanned (*.def.xml) for options
 * that belong to a specific module. If the option is used in the form the
 * module is loaded. The routine for this auto-configuration is pretty
 * slow, so it is possible to turn it off in configfile.mod.php
 * 2. options 'modules' Every module that is added to the modules-option, is
 * loaded (multiple options can be seperated with :)
 *
 * @author    Herman Suijs (<a href="mailto:php@suijsmail.com">php@suijsmail.com</a>)
 * @package   Modules
 */

class ConfigModule extends Module {
  
/**
   * Name of the module
   */
  
var $name "ConfigModule";
  
/**
   * Id of the module
   */
  
var $id MOD_CONFIG;
  
/**
   * Execution level of the module
   */
  
var $execlevel 1;
  
/**
   * Variable to contain all modules that need to be loaded
   */
  
var $modules "";

  
/**
   * Constructor
   *
   * Create the options and call the superConstructor
   */
  
function ConfigModule() {
    
$this->options[EXTRA_MODULES] = new Option("config:modules:extra_modules","",false,false);
    
$this->options[CONFIGFILE] = new Option("configfile:config_file");
    
$this->Module();
   
/* function ConfigModule() */}
  
  
/**
   * Before action
   * 
   * Check all options, load in a configfile if necessary and set all
   * options.
   */
  
function before_action() {
    global 
$formHandler,$debug;
    
$this->check_options();
    
// If a configfile is given, load it in
    
if ($this->options[CONFIGFILE]) {
      
$configfile file(CONFIG_DIR."/".$this->options[CONFIGFILE]->value);
      unset(
$this->options[CONFIGFILE]);
      while (list(,
$line)=each($configfile)) {
    
$line ereg_replace("\n","",$line);
    
$line ereg_replace("\r","",$line);
    
$line_info ereg_replace(COMMENT_LINE,"",$line);
    if (!
strpos($line_info,"=")===false) {
      list(
$config_name,$config_value)=explode("=",$line_info);
      
// Make sure the debug-options also work
      
if ($config_name=="config"||$config_name=="modules"||$config_name=="extra_modules") {
        
$this->modules $config_value;
      } elseif (
$config_name=="debug") {
        if (
is_true($config_value)) {
          
$debug->enable();
         }
        
// this will override //options set in the form
        //$debug->enable(is_true($config_value));

        //$debug->add_message("Option: " . $config_name . " ". $config_value,$this->name);
      
} elseif ($config_name=="noop") {
        if (
is_true($config_value)) {
          
$debug->noop();
        } 
/* if (is_true($config_value)) */
        //$debug->set_noop(is_true($config_value));
        //$debug->add_message("Option: " . $config_name . " ". $config_value,$this->name);
      
} elseif (!strpos($config_name,"level")===false) {
        if (
$debug->get_level()==1) {
          
$debug->set_level($config_value);
        } 
/* if ($debug->get_level()==1) */
      
}
        
$debug->add(5,$this->name,"Option: %s %s",$config_name,$config_value);
      
$config[$config_name] = $config_value;
    } else { 
/* while (list(,$line)=each($conf... */
      
$debug->add(7,$this->name,"Comment or ... in configfile: %s",  $line);
   } 
/* while (list(,$line)=each($conf... */
      
/* if ($this->options[CONFIGFILE]... */
      
unset($configfile);
      
// add the values of the configfile to the vars-array
      //if (!empty($config)) $formHandler->set_default_vars($config);
    
$formHandler->addto_vars($config,CONFIGFILE_OVERRIDE);
    } 
/* function before_action() */
  
/* class ConfigModule extends Mod... */

  /**
   * Routine for automatic loading of modules
   *
   * When predefined options from an xml-configfile are here, then the
   * module is added to the list of modules to be loaded in.
   *
   * Because the file xml.inc.php is quite large and may downgrade the
   * performance of the whole script, you can turn of auto-configuration in
   * the configfile.mod.php (must become xml config one day)
   */
  
function auto_config() {
  
// return an array with modules to add
    
global $debug,$formHandler;
    
// Use all vars for configuration
    
$vars $formHandler->get_vars();
    
//$vars = $formHandler->get_showvars();
    //echo fh_serialize($vars);
    
$load_modules = array();
    
// Include class-file for reading xml-config files
    
include_once(INCLUDE_DIR."/xml.inc.php");

    
//$localdir = opendir(LOCAL_DIR);
    
$localdir opendir(MODULES_DIR);
    while (
$xmlfile readdir($localdir)) {
      if (
ereg(DEF_EXT ."$",$xmlfile)) {
      
//echo "XML-file: %s".getcwd().MODULES_DIR."/$xmlfile";
    
$xml = new XML(getcwd()."/".MODULES_DIR."/$xmlfile");
    
$load_in false;
    
//$debug->add_message("XML-file: $xmlfile",$this->name."(AUTO_CONFIG)");
    
$debug->add(3,$this->name."(AUTO_CONFIG)","XML-file: %s",$xmlfile);
    
$module_path "/module[1]";
    
$mod_name $xml->get_content($module_path "/name[1]");
    
$mod_file $xml->get_content($module_path "/file[1]");
    
$options $xml->evaluate($module_path "/options/optionname");
    
//$debug->add_mesage(fh_serialize($options) ."<br>",$this->name);
    
foreach ($options as $option) {
      if (
$load_in) break;
      
$optionname $xml->get_content($option);
      
$attributes $xml->get_attributes($option);
      if (
$attributes["aliases"] != "")
        
$aliases explode(":",$attributes["aliases"]);
      
$aliases[] = $optionname;
      
//echo fh_serialize($aliases) ."<br>";
      
while (list(,$alias) = each($aliases)) {
        if (
$vars[$alias] !="") {
          
$load_modules[$mod_name] = $mod_file;
          
//$debug->add_message("Module $mod_name ($optionname-$alias): $mod_file <br>",$this->name);
          
$debug->add(6,$this->name,"Module %s (%s): %s"$mod_name,"$optionname-$alias",$mod_file,$this->name);
          
$load_in=true;
          break;
        } 
/* if ($vars[$alias] !="") */
      
/* while (list(,$alias) = each($a... */
    
/* foreach ($options as $option) */
    
unset($xml);
      } 
/* if (ereg(DEF_EXT ."$",$xmlfile... */
    
/* while ($xmlfile = readdir($loc... */
    
return $load_modules;
  } 
/* function auto_config() */

  /**
   * Do action
   *
   * All modules that were selected will be loaded in
   */
  
function do_action() {
    global 
$formHandler,$modules,$debug,$name;
    
$this->action();

    
reset($this->options);
    
//echo fh_serialize($this->options);
    //list($name,$config) = each($this->options);
    
$config "";
    
$sep false;
    if (
$this->options[EXTRA_MODULES]) {
      if (
$sep$config.= CONFIG_SEP;
      
$config .= $this->options[EXTRA_MODULES]->value;
      
$sep true;
    } 
/* if ($this->options[EXTRA_MODUL... */
    
if (!empty($this->modules)) {
      if (
$sep$config.= CONFIG_SEP;
      
$config .= $this->modules;
      
$sep true;
    } 
/* if (!empty($this->modules)) */
    //echo "addModule $name " .fh_serialize($config) . "<br>";
    
if (!strpos($config,CONFIG_SEP)===false) {
      
$configs explode(CONFIG_SEP,$config);
      while (list(,
$name) = each($configs)) {
    
$name trim($name);
    if (!
$added[$name]) {
      
$message addModule($name,$name);
    } 
/* if (!$added[$name]) */
    
$added[$name] = true;
    
//$debug->add_message($message,$this->name);
    
$debug->add(6,$this->name,$message);
      } 
/* while (list(,$name) = each($co... */
    
} else { /* if (!strpos($config,CONFIG_SEP... */
      
$message addModule($config,$config);
      
//$debug->add_message($message,$this->name);
      
$debug->add(6,$this->name,$message);
   
/* if (!strpos($config,CONFIG_SEP... */  }

    if (
$formHandler->config(CFG_AUTO_CONFIG)) {
      
//$debug->add_message("AUTO_CONFIG",$this->name);
      
$debug->add_message(6,$this->name,"AUTO_CONFIG");
      
$load_modules $this->auto_config();
      while (list(
$name,$file) = each($load_modules)) {
    
$message addModule($file,$name);
    
//$debug->add_message($message,$this->name);
    
$debug->add(6,$this->name,$message);
      } 
/* while (list($name,$file) = eac... */
    
/* if ($formHandler->config(CFG_A... */

  
/* function do_action() */

  /**
   * After action
   *
   * Cleanup
   */
  
function after_action() {
    
$this->clean_up();
  } 
/* function after_action() */
}