summaryrefslogtreecommitdiff
path: root/utils/modules.php
blob: 4718f956abb1cdb7d61fe777d6d9a3ea9b54af12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php

$modules = array();

function getModuleList()
{
	global $MODULES_DIR;

	$mlist = array();
	
	$d = opendir($MODULES_DIR);
	while(false !== ($f = readdir($d))) {
	    	if(is_dir($f)) continue;
		if(substr($f, strlen($f) - 4, 4) != ".php") continue;
		array_push($mlist, basename($f, ".php"));
	}
	closedir($d);

	sort($mlist);

	return $mlist;
}

function loadModule($modulename)
{
	global $MODULES_DIR;
	global $modules;

	if(!$modules[$modulename]) {
		$modulefile = $MODULES_DIR . "/" . $modulename . ".php";
		if(file_exists($modulefile)) {
			include_once($modulefile);
			$module = call_user_func($modulename . "_init");
			$modules[$modulename] = $module;
		} else {
			// Module does not exist.
			return;
		}
	}
	return $modules[$modulename];
}

function loadAllModules()
{
	foreach(getModuleList() as $m) {
		loadModule($m);
	}
}

function getModuleParams($code)
{
	$params = array();
	$ps = explode(",", $code);
	foreach($ps as $p) {
		$v = explode("=", $p);
		$params[$v[0]] = $v[1];
	}
	return $params;
}

function runModule($code)
{
	global $modules;

	$str = "";

	$m = explode("?", $code);
	$module = $m[0];
	$params = getModuleParams($m[1]);

	loadModule($module);

	if($modules[$module]) {
		$str .= $modules[$module]->run($params);
	} else {
		$str .= "<p><strong>CMS ERROR: Could not find module:";
		$str .= " [[" . $modulecode . "]]</strong></p>\n";
	}
	
	return $str;
}

?>