1<?php
2
3	/**
4	 * Function library read in upon startup
5	 *
6	 * $Id: lib.inc.php,v 1.123 2008/04/06 01:10:35 xzilla Exp $
7	 */
8
9	include_once('./libraries/decorator.inc.php');
10	include_once('./lang/translations.php');
11
12	// Set error reporting level to max
13	error_reporting(E_ALL);
14
15	// Application name
16	$appName = 'phpPgAdmin';
17
18	// Application version
19	$appVersion = '7.13.0';
20
21	// PostgreSQL and PHP minimum version
22	$postgresqlMinVer = '7.4';
23	$phpMinVer = '7.2';
24
25	// Check the version of PHP
26	if (version_compare(phpversion(), $phpMinVer, '<'))
27		exit(sprintf('Version of PHP not supported. Please upgrade to version %s or later.', $phpMinVer));
28
29	// Check to see if the configuration file exists, if not, explain
30	if (file_exists('conf/config.inc.php')) {
31		$conf = array();
32		include('./conf/config.inc.php');
33	}
34	else {
35		echo 'Configuration error: Copy conf/config.inc.php-dist to conf/config.inc.php and edit appropriately.';
36		exit;
37	}
38
39	// Configuration file version.  If this is greater than that in config.inc.php, then
40	// the app will refuse to run.  This and $conf['version'] should be incremented whenever
41	// backwards incompatible changes are made to config.inc.php-dist.
42	$conf['base_version'] = 16;
43
44	// Always include english.php, since it's the master language file
45	if (!isset($conf['default_lang'])) $conf['default_lang'] = 'english';
46	$lang = array();
47	require_once('./lang/english.php');
48
49	// Create Misc class references
50	require_once('./classes/Misc.php');
51	$misc = new Misc();
52
53	// Start session (if not auto-started)
54	if (!ini_get('session.auto_start')) {
55		session_name('PPA_ID');
56		session_start();
57	}
58
59	// Do basic PHP configuration checks
60	if (ini_get('magic_quotes_gpc')) {
61		$misc->stripVar($_GET);
62		$misc->stripVar($_POST);
63		$misc->stripVar($_COOKIE);
64		$misc->stripVar($_REQUEST);
65	}
66
67	// This has to be deferred until after stripVar above
68	$misc->setHREF();
69	$misc->setForm();
70
71	// Enforce PHP environment
72	ini_set('magic_quotes_runtime', 0);
73	ini_set('magic_quotes_sybase', 0);
74	ini_set('arg_separator.output', '&amp;');
75
76	// If login action is set, then set session variables
77	if (isset($_POST['loginServer']) && isset($_POST['loginUsername']) &&
78		isset($_POST['loginPassword_'.md5($_POST['loginServer'])])) {
79
80		$_server_info = $misc->getServerInfo($_POST['loginServer']);
81
82		$_server_info['username'] = $_POST['loginUsername'];
83		$_server_info['password'] = $_POST['loginPassword_'.md5($_POST['loginServer'])];
84
85		$misc->setServerInfo(null, $_server_info, $_POST['loginServer']);
86
87		// Check for shared credentials
88		if (isset($_POST['loginShared'])) {
89			$_SESSION['sharedUsername'] = $_POST['loginUsername'];
90			$_SESSION['sharedPassword'] = $_POST['loginPassword_'.md5($_POST['loginServer'])];
91		}
92
93		$_reload_browser = true;
94	}
95
96	/* select the theme */
97	unset($_theme);
98	if (!isset($conf['theme']))
99		$conf['theme'] = 'default';
100
101	// 1. Check for the theme from a request var
102	if (isset($_REQUEST['theme']) && is_file("./themes/{$_REQUEST['theme']}/global.css")) {
103		/* save the selected theme in cookie for a year */
104		setcookie('ppaTheme', $_REQUEST['theme'], time()+31536000);
105		$_theme = $_SESSION['ppaTheme'] = $conf['theme'] = $_REQUEST['theme'];
106	}
107
108	// 2. Check for theme session var
109	if (!isset($_theme) && isset($_SESSION['ppaTheme']) && is_file("./themes/{$_SESSION['ppaTheme']}/global.css")) {
110		$conf['theme']  = $_SESSION['ppaTheme'];
111	}
112
113	// 3. Check for theme in cookie var
114	if (!isset($_theme) && isset($_COOKIE['ppaTheme']) && is_file("./themes/{$_COOKIE['ppaTheme']}/global.css")) {
115		$conf['theme']  = $_COOKIE['ppaTheme'];
116	}
117
118	// 4. Check for theme by server/db/user
119	$info = $misc->getServerInfo();
120
121	if (!is_null($info)) {
122		$_theme = '';
123
124		if ( (isset($info['theme']['default']))
125			and is_file("./themes/{$info['theme']['default']}/global.css")
126		)
127			$_theme = $info['theme']['default'];
128
129		if ( isset($_REQUEST['database'])
130			and isset($info['theme']['db'][$_REQUEST['database']])
131			and is_file("./themes/{$info['theme']['db'][$_REQUEST['database']]}/global.css")
132		)
133			$_theme = $info['theme']['db'][$_REQUEST['database']];
134
135		if ( isset($info['username'])
136			and isset($info['theme']['user'][$info['username']])
137			and is_file("./themes/{$info['theme']['user'][$info['username']]}/global.css")
138		)
139			$_theme = $info['theme']['user'][$info['username']];
140
141		if ($_theme !== '') {
142			setcookie('ppaTheme', $_theme, time()+31536000);
143			$conf['theme'] = $_theme;
144		}
145	}
146
147	// Determine language file to import:
148	unset($_language);
149
150	// 1. Check for the language from a request var
151	if (isset($_REQUEST['language']) && isset($appLangFiles[$_REQUEST['language']])) {
152		/* save the selected language in cookie for a year */
153		setcookie('webdbLanguage', $_REQUEST['language'], time()+31536000);
154		$_language = $_REQUEST['language'];
155	}
156
157	// 2. Check for language session var
158	if (!isset($_language) && isset($_SESSION['webdbLanguage']) && isset($appLangFiles[$_SESSION['webdbLanguage']])) {
159		$_language = $_SESSION['webdbLanguage'];
160	}
161
162	// 3. Check for language in cookie var
163	if (!isset($_language) && isset($_COOKIE['webdbLanguage']) && isset($appLangFiles[$_COOKIE['webdbLanguage']])) {
164		$_language  = $_COOKIE['webdbLanguage'];
165	}
166
167	// 4. Check for acceptable languages in HTTP_ACCEPT_LANGUAGE var
168	if (!isset($_language) && $conf['default_lang'] == 'auto' && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
169		// extract acceptable language tags
170		// (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4)
171		preg_match_all('/\s*([a-z]{1,8}(?:-[a-z]{1,8})*)(?:;q=([01](?:.[0-9]{0,3})?))?\s*(?:,|$)/', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']), $_m, PREG_SET_ORDER);
172		foreach($_m as $_l) {  // $_l[1] = language tag, [2] = quality
173			if (!isset($_l[2])) $_l[2] = 1;  // Default quality to 1
174			if ($_l[2] > 0 && $_l[2] <= 1 && isset($availableLanguages[$_l[1]])) {
175				// Build up array of (quality => language_file)
176				$_acceptLang[$_l[2]] = $availableLanguages[$_l[1]];
177			}
178		}
179		unset($_m);
180		unset($_l);
181		if (isset($_acceptLang)) {
182			// Sort acceptable languages by quality
183			krsort($_acceptLang, SORT_NUMERIC);
184			$_language = reset($_acceptLang);
185			unset($_acceptLang);
186		}
187	}
188
189	// 5. Otherwise resort to the default set in the config file
190	if (!isset($_language) && $conf['default_lang'] != 'auto' && isset($appLangFiles[$conf['default_lang']])) {
191		$_language = $conf['default_lang'];
192	}
193
194	// 6. Otherwise, default to english.
195	if (!isset($_language))
196		$_language = 'english';
197
198
199	// Import the language file
200	if (isset($_language)) {
201		include("./lang/{$_language}.php");
202		$_SESSION['webdbLanguage'] = $_language;
203	}
204
205	// Check for config file version mismatch
206	if (!isset($conf['version']) || $conf['base_version'] > $conf['version']) {
207		echo $lang['strbadconfig'];
208		exit;
209	}
210
211	// Check php libraries
212	$php_libraries_requirements = [
213		// required_function => name_of_the_php_library
214		'pg_connect' => 'pgsql',
215		'mb_strlen' => 'mbstring'];
216	$missing_libraries = [];
217	foreach($php_libraries_requirements as $funcname => $lib)
218		if (!function_exists($funcname))
219			$missing_libraries[] = $lib;
220	if ($missing_libraries) {
221		$missing_list = implode(', ', $missing_libraries);
222		$error_missing_template_string = count($missing_libraries) <= 1 ? $lang['strlibnotfound'] : $lang['strlibnotfound_plural'];
223		printf($error_missing_template_string, $missing_list);
224		exit;
225	}
226
227	// Manage the plugins
228	require_once('./classes/PluginManager.php');
229
230	// Create data accessor object, if necessary
231	if (!isset($_no_db_connection)) {
232		if (!isset($_REQUEST['server'])) {
233			echo $lang['strnoserversupplied'];
234			exit;
235	    }
236		$_server_info = $misc->getServerInfo();
237
238		/* starting with PostgreSQL 9.0, we can set the application name */
239		if(isset($_server_info['pgVersion']) && $_server_info['pgVersion'] >= 9)
240			putenv("PGAPPNAME={$appName}_{$appVersion}");
241
242		// Redirect to the login form if not logged in
243		if (!isset($_server_info['username'])) {
244			include('./login.php');
245			exit;
246		}
247
248		// Connect to the current database, or if one is not specified
249		// then connect to the default database.
250		if (isset($_REQUEST['database']))
251			$_curr_db = $_REQUEST['database'];
252		else
253			$_curr_db = $_server_info['defaultdb'];
254
255		include_once('./classes/database/Connection.php');
256
257		// Connect to database and set the global $data variable
258		$data = $misc->getDatabaseAccessor($_curr_db);
259
260		// If schema is defined and database supports schemas, then set the
261		// schema explicitly.
262		if (isset($_REQUEST['database']) && isset($_REQUEST['schema'])) {
263			$status = $data->setSchema($_REQUEST['schema']);
264			if ($status != 0) {
265				echo $lang['strbadschema'];
266				exit;
267			}
268		}
269	}
270
271	if (!function_exists("htmlspecialchars_decode")) {
272		function htmlspecialchars_decode($string, $quote_style = ENT_COMPAT) {
273			return strtr($string, array_flip(get_html_translation_table(HTML_SPECIALCHARS, $quote_style)));
274		}
275	}
276
277	$plugin_manager = new PluginManager($_language);
278?>
279