get_valid_module()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 6
rs 10
1
<?php
2
/**
3
 * Pre-Config Functions.   This stuff is called almost instantly
4
 * so it needs to be defined before any of the rest of the program can go.
5
 * @author Joe Huss <[email protected]>
6
 * @copyright 2019
7
 * @package MyAdmin
8
 * @category Config
9
 */
10
11
/**
12
 * register_module()
13
 * @param string     $module
14
 * @param array|bool $settings
15
 * @return void
16
 */
17
function register_module($module, $settings = false)
18
{
19
	if ($settings === false) {
20
		$settings = [];
21
	}
22
	if (!isset($GLOBALS['modules'])) {
23
		$GLOBALS['modules'] = [];
24
	}
25
	$modules = $GLOBALS['modules'];
26
	$modules[$module] = $settings;
27
	$GLOBALS['modules'] = $modules;
28
}
29
30
/**
31
 * get_module_stuff()
32
 * @param string $module
33
 * @return array
34
 */
35
function get_module_stuff($module = 'default')
36
{
37
	$module = get_module_name($module);
38
	return [
39
		get_module_db($module),
40
		$module,
41
		get_module_settings($module)
42
	];
43
}
44
45
/**
46
 * get_module_name()
47
 * gets the name of a module, or makes sure that the given module exists, if not returns default
48
 *
49
 * @param string $module the module name your attempting to validate / get the name of
50
 * @return string the name of the module
51
 */
52
function get_module_name($module = 'default')
53
{
54
	if ($module != 'default') {
55
		if (isset($GLOBALS[$module.'_dbh'])) {
56
			return $module;
57
		}
58
		if (isset($GLOBALS['modules'][$module])) {
59
			return $module;
60
		} elseif (isset($_REQUEST['module']) && isset($GLOBALS['modules'][$_REQUEST['module']])) {
61
			return $_REQUEST['module'];
62
		}
63
	}
64
	$tkeys = array_keys($GLOBALS['modules']);
65
	if (count($tkeys) > 0) {
66
		foreach ($tkeys as $idx => $key) {
67
			if ($key != 'default') {
68
				return $key;
69
			}
70
		}
71
	}
72
	return 'default';
73
}
74
75
/**
76
 * get_module_settings()
77
 * gets the array of settings for a given module, or a specific setting for that module
78
 *
79
 * @param string $module
80
 * @param bool|string $setting optional parameter, false to return all settings, or a specific setting name to return that setting
81
 * @return array|false array of settings or false if no setting
82
 */
83
function get_module_settings($module = 'default', $setting = false)
84
{
85
	if (!isset($GLOBALS['modules'][$module])) {
86
		$keys = array_keys($GLOBALS['modules']);
87
		$module = $keys[0];
88
	}
89
	if ($setting !== false) {
90
		if (isset($GLOBALS['modules'][$module][$setting])) {
91
			return $GLOBALS['modules'][$module][$setting];
92
		} else {
93
			return false;
94
		}
95
	} else {
96
		return $GLOBALS['modules'][$module];
97
	}
98
}
99
100
/**
101
 * @param $service
102
 * @return mixed
103
 */
104
function get_service_define($service)
105
{
106
	return $GLOBALS['tf']->get_service_define($service);
107
}
108
109
/**
110
 * @param $module
111
 * @return bool
112
 */
113
function has_module_db($module)
114
{
115
	return isset($GLOBALS[$module.'_dbh']);
116
}
117
118
/**
119
 * gets the database handler for a given module
120
 *
121
 * @param string $module the name of the module to get the dbh for
122
 * @return Db the database handler resource
0 ignored issues
show
Bug introduced by
The type Db was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
123
 */
124
function get_module_db($module)
125
{
126
	if ($module == 'powerdns') {
127
		if (!isset($GLOBALS['powerdns_dbh'])) {
128
			$GLOBALS['powerdns_dbh'] = new \MyDb\Mdb2\Db(POWERDNS_DB, POWERDNS_USER, POWERDNS_PASSWORD, POWERDNS_HOST);
0 ignored issues
show
Bug introduced by
The constant POWERDNS_HOST was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant POWERDNS_DB was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant POWERDNS_USER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant POWERDNS_PASSWORD was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The type MyDb\Mdb2\Db was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
129
			$GLOBALS['powerdns_dbh']->Type = 'mysqli';
130
		}
131
		return clone $GLOBALS['powerdns_dbh'];
132
	} elseif ($module == 'zonemta') {
133
		if (!isset($GLOBALS['zonemta_dbh'])) {
134
			$GLOBALS['zonemta_dbh'] = new \MyDb\Mysqli\Db(ZONEMTA_MYSQL_DB, ZONEMTA_MYSQL_USERNAME, ZONEMTA_MYSQL_PASSWORD, ZONEMTA_MYSQL_HOST);
0 ignored issues
show
Bug introduced by
The type MyDb\Mysqli\Db was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The constant ZONEMTA_MYSQL_PASSWORD was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant ZONEMTA_MYSQL_DB was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant ZONEMTA_MYSQL_USERNAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant ZONEMTA_MYSQL_HOST was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
135
			$GLOBALS['zonemta_dbh']->Type = 'mysqli';
136
		}
137
		return clone $GLOBALS['zonemta_dbh'];
138
	} else {
139
		if (isset($GLOBALS[$module.'_dbh'])) {
140
			return clone $GLOBALS[$module.'_dbh'];
141
		} else {
142
			if ($module != '' && $module != 'default') {
143
				myadmin_log('myadmin', 'info', "Tried to get_module_db(${module}) and GLOBALS[${module}_dbh] does not exist, falling back to GLOBALS['tf']->db", __LINE__, __FILE__, $module);
0 ignored issues
show
Bug introduced by
The function myadmin_log was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

143
				/** @scrutinizer ignore-call */ 
144
    myadmin_log('myadmin', 'info', "Tried to get_module_db(${module}) and GLOBALS[${module}_dbh] does not exist, falling back to GLOBALS['tf']->db", __LINE__, __FILE__, $module);
Loading history...
144
			}
145
			return clone $GLOBALS['tf']->db;
146
		}
147
	}
148
}
149
150
/**
151
 * get_valid_module()
152
 * returns the module name if a valid module or default if not
153
 *
154
 * @param string $module
155
 * @return string the/a validated module name
156
 */
157
function get_valid_module($module = 'default')
158
{
159
	if (isset($GLOBALS['modules'][$module])) {
160
		return $module;
161
	} else {
162
		return 'default';
163
	}
164
}
165