<?php

class module_base extends opml_base {
    var $submod;
    var $class;
    var $dir;

    function load_submod($dir) {
        global $conf_nomask, $conf_basedir, $modules;
        $nomask = array('.','..','CVS');

        $this->dir = $dir;
        //$this->class = get_parent_class($this);
        $name = get_class($this);
        $modules[$name] =& $this;

        // load the attributes first
        if (is_dir($dir) && $dh = opendir($dir)) {
            while ($file = readdir($dh)) {
                if (in_array($file,$nomask)) continue;
                if (preg_match("/^func/",$file)) continue;

                $filename = $conf_basedir . $dir . $file;
                if (is_dir($filename)) continue;
        
                include_once $filename;

                $name = substr($file,0,strrpos($file,'.'));
                $this->submod[$name] = new $name;
                $this->submod[$name]->module_invoke("","load_submod",
                    $dir . $name . "/");
            }
        }

        // then load the functions
        if (is_dir($dir) && $dh = opendir($dir)) {
            while ($file = readdir($dh)) {
                if (in_array($file,$nomask)) continue;
                if (!preg_match("/^func/",$file)) continue;

                $filename = $dir . $file;
                if (is_dir($filename)) continue;
        
                include_once $filename;

                $name = substr($file,0,strrpos($file,'.'));
                $this->submod[$name] = new $name;
                $this->submod[$name]->module_invoke("","load_submod",
                    $dir . $name . "/");
            }
        }
    }

    function module_invoke_sub($class,$function,
        $a1 = NULL, $a2 = NULL, $a3 = NULL, $a4 = NULL, $a5 = NULL, $a6 = NULL){
        if (!isset($this->submod)) return 0;
        foreach ($this->submod as $att => $m) {
            $this->submod[$att]->module_invoke($class,$function,
                $a1,$a2,$a3,$a4,$a5,$a6);
        }
        return 1;
    }

    function module_invoke($class,$function,
        $a1 = NULL, $a2 = NULL, $a3 = NULL, $a4 = NULL, $a5 = NULL, $a6 = NULL){
        if ($class != "" && get_parent_class($this) != $class) return;
        if (method_exists($this,$function))
            return $this->$function($a1,$a2,$a3,$a4,$a5,$a6);
        return 0;
    }

    function has_module($type) {
        if (get_class($this) == $type) return 1;
        if (!isset($this->submod)) return 0;
        foreach ($this->submod as $sub => $v) {
            $ans = $this->submod[$sub]->has_module($type);
            if ($ans) return 1;
        }
        return 0;
    }
}

function has_module($type) {
    global $module_tree;
    return $module_tree->has_module($type);
}

?>
