File: Config.php

package info (click to toggle)
irm 1.5.3.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,436 kB
  • ctags: 3,308
  • sloc: php: 16,796; sh: 127; perl: 97; pascal: 56; makefile: 48
file content (325 lines) | stat: -rw-r--r-- 7,215 bytes parent folder | download | duplicates (2)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php

// Default system configuration.  Also defines the possible configuration
// variables.
$DEFAULT_CONFIG = array(
		'dbver' => NULL,
		'notifyassignedbyemail' => true,
		'notifynewtrackingbyemail' => false,
		'newtrackingemail' => 'user@host.com',
		'groups' => true,
		'usenamesearch' => true,
		'userupdates' => true,
		'sendexpire' => false,
		'showjobsonlogin' => true,
		'minloglevel' => 5,
		'logo' => 'irm-jr1.jpg',
		'snmp' => false,
		'snmp_rcommunity' => 'public',
		'snmp_ping' => false,
		'knowledgebase' => true,
		'fasttrack' => true,
		'anonymous' => false,
		'anon_faq' => false,
		'anon_tt' => false
		);

class Config
{
	// The current IRM version
	function Version()
	{
		return '1.5.3.1';
	}

	/** Return an "absolute" web location for $file (given relative to
	 * the root of the IRM installation.
	 *
	 * Assumes:
	 *   # That the PHP script file is located within the IRM
	 *	installation tree; and
	 *   # That the file that this method exists in is one level
	 *	down from the root of the IRM installation.
	 *
	 * I swear this code made sense to me when I wrote it.
	 *
	 * You can also give AbsLoc a set of arguments to be passed to the
	 * file specified, and they'll be appended in the usual URL manner. 
	 * Make $args an associative array of name => value pairs.
	 *
	 * Warning: Do *not* pass any arguments as part of the filename if
	 * you want to give an array of arguments -- Bad Things will happen.
	 */
	function AbsLoc($file, $args = NULL)
	{
		$sloc = $_SERVER['SCRIPT_NAME'];
		$sfile = @$_SERVER['SCRIPT_FILENAME'];
		if (!$sfile)
		{
			$sfile = @$_SERVER['PATH_TRANSLATED'];
		}
		$sfile = realpath($sfile);
		if (Config::onWindows())
		{
			$sfile = str_replace('\\', '/', $sfile);
		}
		
		// First, work out the filesystem location of the root of
		// the IRM installation
		$instroot = dirname(dirname(__FILE__));
		if (Config::onWindows())
		{
			$instroot = str_replace('\\', '/', $instroot);
		}
		
		// Next, get the name of the script file relative to the root
		// of the IRM installation
		$relativescript = ereg_replace("^$instroot", '', $sfile);

		// Now, we can get the web location of the root of the IRM
		// installation by stripping out the script-file specific
		// portion from the web location of the script
		$webroot = ereg_replace("$relativescript\$", '', $sloc);

		if ($args !== NULL)
		{
			$arglist = array();
			foreach ($args as $k => $v)
			{
				$arglist[] = urlencode($k) . "=" . urlencode($v);
			}
			
			$file = "$file?" . join('&', $arglist);
		}
		
		return "$webroot/$file";
	}

	function &Database()
	{
		require_once 'lib/IRMDB.php';

		global $DB;
		
		$dbcfg = Config::CurrentSection('database');

		if (!@$dbcfg['DSN'])
		{
			trigger_error(sprintf(_("No DSN found for section %s!"), $_SESSION['_sess_database']), E_USER_ERROR);
		}

		if (!$DB)
		{
			$DB = new IRMDB($dbcfg['DSN'], @$dbcfg['dbsocket']);
		}
		
		if ($dbcfg['DSN'] !== @$DB->dsn)
		{
			unset($DB);
			$DB = new IRMDB($dbcfg['DSN'], @$dbcfg['dbsocket']);
		}

		return $DB;
	}

	function onWindows()
	{
		return preg_match('/^WIN/i', PHP_OS);
	}

	function PathSeparator()
	{
		return Config::onWindows() ? ';' : ':';
	}
	
	function ReadConfig($type)
	{
		switch($type)
		{
			case 'database':
				$file = 'database.ini';
				$errlevel = E_USER_ERROR;
				break;
		
			case 'ldap':
				$file = 'ldap.ini';
				$errlevel = E_USER_NOTICE;
				break;
			
			default:
				printf(_("Unknown type of config file: %s\n"), $type);
				echo "Backtrace:\n";
				print_r(debug_backtrace());
				exit(1);
		}
		
		$basedir = dirname(dirname(__FILE__));
		
		$cfgfile = "$basedir/config/$file";
		if (file_exists($cfgfile))
		{
			return parse_ini_file($cfgfile, true);
		}
		else
		{
			trigger_error(sprintf(_("config file %s not found"), "config/${type}.ini"), $errlevel);
			return false;
		}
	}

	function CurrentSection($type)
	{
		$cfg = Config::ReadConfig($type);
		
		$dataset = @$_SESSION['_sess_database'];
		if (!$dataset)
		{
			$dataset = '_default';
		}
		
		return @$cfg[$dataset] ? $cfg[$dataset] : array();
	}

	/** Retrieve the current value of the specified system config variable.
	 */
	function Get($var)
	{
		global $DEFAULT_CONFIG;
		
		if (!array_intersect(array_keys($DEFAULT_CONFIG), array($var)))
		{
			return NULL;
		}
		
		$DB = Config::Database();
		$qvar = $DB->getTextValue($var);
		$val = $DB->getOne("SELECT value FROM config WHERE variable=$qvar");
		
		if ($val === NULL)
		{
			return $DEFAULT_CONFIG[$var];
		}
		else
		{
			return $val;
		}
	}

	/** Set the value of the specified system config variable.
	 */
	function Set($variable, $value)
	{
		global $DEFAULT_CONFIG;
		
		if (!array_intersect(array_keys($DEFAULT_CONFIG), array($variable)))
		{
			return NULL;
		}
		
		$DB = Config::Database();
		$qvar = $DB->getTextValue($variable);
		$qval = $DB->getTextValue($value);
		
		// Why all this stuff, you ask?  Because we can't rely on
		// MySQL's REPLACE statement (damned database independence)
		// and we can't use UPDATE because it's not guaranteed that
		// the relevant config variable exists in the database.
		$DB->autoCommit(false);
		$DB->query("DELETE FROM config WHERE variable=$qvar");
		$DB->query("INSERT INTO config (variable, value) VALUES ($qvar, $qval)");
		$DB->commit();
		$DB->autoCommit(true);
	}

	/** Retrieve the complete set of config values.
	 * Get all of the current config values from the database and
	 * return them to the calling function as an associative array.
	 */
	function All()
	{
		$DB = Config::Database();
		
		$cfg = $DB->getAll("SELECT variable,value FROM config");
		
		global $DEFAULT_CONFIG;
		$valid = array_keys($DEFAULT_CONFIG);

		$all = $DEFAULT_CONFIG;
		foreach ($cfg as $c)
		{
			if (array_intersect($valid, array($c['variable'])))
			{
				$var = $c['variable'];
				$value = $c['value'];
				if (is_bool($DEFAULT_CONFIG[$var]))
				{
					// Quick boolean conversion
					if ($value == 0)
					{
						$value = false;
					}
					else
					{
						$value = true;
					}
				}

				$all[$var] = $value;
			}
		}
		
		return $all;
	}

	/** Return whether LDAP is in use or not for the currently selected database.
	 * Returns true if there is an LDAP configuration section defined for the
	 * database in use by the current user, or false otherwise.
	 */
	function UseLDAP()
	{
		return (boolean)Config::LDAP();
	}

	/** Return the LDAP config for the current database, if it exists
	 * Returns an associative array containing all of the LDAP config
	 * parameters for the database that the currently logged-in user
	 * is is using, or false if the database does not do LDAP.
	 */
	function LDAP()
	{
		$defaults = array('server' => 'localhost',
				'protocol' => '3',
				'fullnamefield' => 'cn',
				'emailfield' => 'mail',
				'locationfield' => 'roomNumber',
				'phonefield' => 'telephoneField'
				);

		$db = @$_SESSION['_sess_database'];
		$cfg = Config::ReadConfig('ldap');

		if (!$cfg)
		{
			return false;
		}

		if (array_key_exists($db, $cfg))
		{
			$cfg = $cfg[$db];
			
			foreach ($defaults as $k => $v)
			{
				if (!array_key_exists($k, $cfg))
				{
					$cfg[$k] = $v;
				}
			}

			return $cfg;
		}
		else
		{
			return false;
		}
	}
}