summaryrefslogtreecommitdiffstats
path: root/pts-core/functions/pts-functions_modules.php
blob: 95422ccc2fefb370414cb5bac7a09e08fce72d6c (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php

/*
	Phoronix Test Suite
	URLs: http://www.phoronix.com, http://www.phoronix-test-suite.com/
	Copyright (C) 2008, Phoronix Media
	Copyright (C) 2008, Michael Larabel
	pts-functions_modules.php: Functions related to PTS module loading/management.
	Modules are optional add-ons that don't fit the requirements for entrance into pts-core but provide added functionality.

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	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. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

function pts_module_start_process()
{
	$GLOBALS["PTS_MODULES"] = array();
	$GLOBALS["PTS_MODULE_CURRENT"] = FALSE;
	$GLOBALS["PTS_MODULE_VAR_STORE"] = array();

	// Enable the toggling of the system screensaver by default.
	// To disable w/o code modification, set HALT_SCREENSAVER=NO environmental variable
	array_push($GLOBALS["PTS_MODULES"], "toggle_screensaver");

	pts_load_modules();
	pts_module_process("__startup");
	register_shutdown_function("pts_module_process", "__shutdown");
}
function pts_auto_detect_modules($load_here = FALSE)
{
	$module_variables_file = @file_get_contents(MODULE_DIR . "module-variables.txt");
	$module_variables = explode("\n", $module_variables_file);

	foreach($module_variables as $module_var)
	{
		$module_var = explode("=", $module_var);

		if(count($module_var) == 2)
		{
			$env_var = trim($module_var[0]);
			$module = trim($module_var[1]);

			if(!in_array($module, $GLOBALS["PTS_MODULES"]) && ($e = getenv($env_var)) != FALSE && !empty($e))
			{
				if(defined("PTS_DEBUG_MODE"))
					echo "Attempting To Add Module: " . $module . "\n";

				array_push($GLOBALS["PTS_MODULES"], $module);

				if($load_here)
					pts_load_module($module);
			}
		}
	}
}
function pts_load_modules()
{
	// Check for modules to auto-load from the configuration file
	if(strlen(($load_modules = pts_read_user_config(P_OPTION_LOAD_MODULES, ""))) > 0)
		foreach(explode(",", $load_modules) as $module)
		{
			$module_r = explode("=", $module);

			if(count($module_r) == 2)
				pts_module_set_environment_variable(trim($module_r[0]), trim($module_r[1]));
			else
				array_push($GLOBALS["PTS_MODULES"], trim($module));
		}

	// Check for modules to load manually in PTS_MODULES
	if(($load_modules = getenv("PTS_MODULES")) !== FALSE)
		foreach(explode(",", $load_modules) as $module)
		{
			$module = trim($module);

			if(!in_array($module, $GLOBALS["PTS_MODULES"]))
				array_push($GLOBALS["PTS_MODULES"], trim($module));
		}

	// Detect modules to load automatically
	pts_auto_detect_modules();

	// Clean-up modules list
	array_unique($GLOBALS["PTS_MODULES"]);
	for($i = 0; $i < count($GLOBALS["PTS_MODULES"]); $i++)
		if(!is_file(MODULE_DIR . $GLOBALS["PTS_MODULES"][$i] . ".php"))
			unset($GLOBALS["PTS_MODULES"][$i]);

	// Reset counter
	$GLOBALS["PTS_MODULE_CURRENT"] = FALSE;

	// Load the modules
	$module_store_list = array();
	foreach($GLOBALS["PTS_MODULES"] as $module)
	{
		pts_load_module($module);

		eval("\$module_store_vars = " . $module . "::\$module_store_vars;");

		if(is_array($module_store_vars) && count($module_store_vars) > 0)
			foreach($module_store_vars as $store_var)
				if(!in_array($store_var, $module_store_list))
					array_push($module_store_list, $store_var);
	}

	// Should any of the module options be saved to the results?
	foreach($module_store_list as $var)
	{
		$var_value = getenv($var);

		if($var_value != FALSE && !empty($var_value))
			array_push($GLOBALS["PTS_MODULE_VAR_STORE"], $var . "=" . $var_value);
	}
}
function pts_load_module($module)
{
	@include(MODULE_DIR . $module . ".php");
}
function pts_module_process($process)
{
	foreach($GLOBALS["PTS_MODULES"] as $module)
	{
		$GLOBALS["PTS_MODULE_CURRENT"] = $module;
		eval($module . "::" . $process . "();"); // TODO: This can be cleaned up once PHP 5.3.0+ is out there and adopted
	}
	$GLOBALS["PTS_MODULE_CURRENT"] = FALSE;
}
function pts_module_process_extensions($extensions)
{
	if(!empty($extensions))
	{
		$GLOBALS["MODULE_STORE"] = $extensions;
		$extensions = explode(";", $extensions);

		foreach($extensions as $ev)
		{
			$ev_r = explode("=", $ev);
			pts_module_set_environment_variable($ev_r[1], $ev_r[2]);
		}

		pts_auto_detect_modules(true);
	}
}
function pts_module_set_environment_variable($name, $value)
{
	if(getenv($name) == FALSE)
		putenv($name . "=" . $value);
}

?>