PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/docroot/pgadmin/libraries/lib.inc.php

https://github.com/mterenzio/FollowThis
PHP | 263 lines | 161 code | 48 blank | 54 comment | 64 complexity | d333ce6b5c8052f45c2c5964f7bd214f MD5 | raw file
  1. <?php
  2. /**
  3. * Function library read in upon startup
  4. *
  5. * $Id: lib.inc.php,v 1.123 2008/04/06 01:10:35 xzilla Exp $
  6. */
  7. include_once('./libraries/decorator.inc.php');
  8. include_once('./lang/translations.php');
  9. // Set error reporting level to max
  10. error_reporting(E_ALL);
  11. // Application name
  12. $appName = 'phpPgAdmin';
  13. // Application version
  14. $appVersion = '5.0.1';
  15. // PostgreSQL and PHP minimum version
  16. $postgresqlMinVer = '7.4';
  17. $phpMinVer = '5.0';
  18. // Check the version of PHP
  19. if (version_compare(phpversion(), $phpMinVer, '<'))
  20. exit(sprintf('Version of PHP not supported. Please upgrade to version %s or later.', $phpMinVer));
  21. // Check to see if the configuration file exists, if not, explain
  22. if (file_exists('conf/config.inc.php')) {
  23. $conf = array();
  24. include('./conf/config.inc.php');
  25. }
  26. else {
  27. echo 'Configuration error: Copy conf/config.inc.php-dist to conf/config.inc.php and edit appropriately.';
  28. exit;
  29. }
  30. // Configuration file version. If this is greater than that in config.inc.php, then
  31. // the app will refuse to run. This and $conf['version'] should be incremented whenever
  32. // backwards incompatible changes are made to config.inc.php-dist.
  33. $conf['base_version'] = 16;
  34. // Always include english.php, since it's the master language file
  35. if (!isset($conf['default_lang'])) $conf['default_lang'] = 'english';
  36. $lang = array();
  37. require_once('./lang/recoded/english.php');
  38. // Create Misc class references
  39. require_once('./classes/Misc.php');
  40. $misc = new Misc();
  41. // Start session (if not auto-started)
  42. if (!ini_get('session.auto_start')) {
  43. session_name('PPA_ID');
  44. session_start();
  45. }
  46. // Do basic PHP configuration checks
  47. if (ini_get('magic_quotes_gpc')) {
  48. $misc->stripVar($_GET);
  49. $misc->stripVar($_POST);
  50. $misc->stripVar($_COOKIE);
  51. $misc->stripVar($_REQUEST);
  52. }
  53. // This has to be deferred until after stripVar above
  54. $misc->setHREF();
  55. $misc->setForm();
  56. // Enforce PHP environment
  57. ini_set('magic_quotes_runtime', 0);
  58. ini_set('magic_quotes_sybase', 0);
  59. ini_set('arg_separator.output', '&amp;');
  60. // If login action is set, then set session variables
  61. if (isset($_POST['loginServer']) && isset($_POST['loginUsername']) &&
  62. isset($_POST['loginPassword_'.md5($_POST['loginServer'])])) {
  63. $_server_info = $misc->getServerInfo($_POST['loginServer']);
  64. $_server_info['username'] = $_POST['loginUsername'];
  65. $_server_info['password'] = $_POST['loginPassword_'.md5($_POST['loginServer'])];
  66. $misc->setServerInfo(null, $_server_info, $_POST['loginServer']);
  67. // Check for shared credentials
  68. if (isset($_POST['loginShared'])) {
  69. $_SESSION['sharedUsername'] = $_POST['loginUsername'];
  70. $_SESSION['sharedPassword'] = $_POST['loginPassword_'.md5($_POST['loginServer'])];
  71. }
  72. $_reload_browser = true;
  73. }
  74. /* select the theme */
  75. unset($_theme);
  76. $conf['theme'] = 'default';
  77. // 1. Check for the theme from a request var
  78. if (isset($_REQUEST['theme']) && is_file("./themes/{$_REQUEST['theme']}/global.css")) {
  79. /* save the selected theme in cookie for a year */
  80. setcookie('ppaTheme', $_REQUEST['theme'], time()+31536000);
  81. $_theme = $_SESSION['ppaTheme'] = $conf['theme'] = $_REQUEST['theme'];
  82. }
  83. // 2. Check for theme session var
  84. if (!isset($_theme) && isset($_SESSION['ppaTheme']) && is_file("./themes/{$_SESSION['ppaTheme']}/global.css")) {
  85. $conf['theme'] = $_SESSION['ppaTheme'];
  86. }
  87. // 3. Check for theme in cookie var
  88. if (!isset($_theme) && isset($_COOKIE['ppaTheme']) && is_file("./themes/{$_COOKIE['ppaTheme']}/global.css")) {
  89. $conf['theme'] = $_COOKIE['ppaTheme'];
  90. }
  91. // Determine language file to import:
  92. unset($_language);
  93. // 1. Check for the language from a request var
  94. if (isset($_REQUEST['language']) && isset($appLangFiles[$_REQUEST['language']])) {
  95. /* save the selected language in cookie for a year */
  96. setcookie('webdbLanguage', $_REQUEST['language'], time()+31536000);
  97. $_language = $_REQUEST['language'];
  98. }
  99. // 2. Check for language session var
  100. if (!isset($_language) && isset($_SESSION['webdbLanguage']) && isset($appLangFiles[$_SESSION['webdbLanguage']])) {
  101. $_language = $_SESSION['webdbLanguage'];
  102. }
  103. // 3. Check for language in cookie var
  104. if (!isset($_language) && isset($_COOKIE['webdbLanguage']) && isset($appLangFiles[$_COOKIE['webdbLanguage']])) {
  105. $_language = $_COOKIE['webdbLanguage'];
  106. }
  107. // 4. Check for acceptable languages in HTTP_ACCEPT_LANGUAGE var
  108. if (!isset($_language) && $conf['default_lang'] == 'auto' && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  109. // extract acceptable language tags
  110. // (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4)
  111. 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);
  112. foreach($_m as $_l) { // $_l[1] = language tag, [2] = quality
  113. if (!isset($_l[2])) $_l[2] = 1; // Default quality to 1
  114. if ($_l[2] > 0 && $_l[2] <= 1 && isset($availableLanguages[$_l[1]])) {
  115. // Build up array of (quality => language_file)
  116. $_acceptLang[$_l[2]] = $availableLanguages[$_l[1]];
  117. }
  118. }
  119. unset($_m);
  120. unset($_l);
  121. if (isset($_acceptLang)) {
  122. // Sort acceptable languages by quality
  123. krsort($_acceptLang, SORT_NUMERIC);
  124. $_language = reset($_acceptLang);
  125. unset($_acceptLang);
  126. }
  127. }
  128. // 5. Otherwise resort to the default set in the config file
  129. if (!isset($_language) && $conf['default_lang'] != 'auto' && isset($appLangFiles[$conf['default_lang']])) {
  130. $_language = $conf['default_lang'];
  131. }
  132. // Import the language file
  133. if (isset($_language)) {
  134. include("./lang/recoded/{$_language}.php");
  135. $_SESSION['webdbLanguage'] = $_language;
  136. }
  137. // Check for config file version mismatch
  138. if (!isset($conf['version']) || $conf['base_version'] > $conf['version']) {
  139. echo $lang['strbadconfig'];
  140. exit;
  141. }
  142. // Check database support is properly compiled in
  143. if (!function_exists('pg_connect')) {
  144. echo $lang['strnotloaded'];
  145. exit;
  146. }
  147. // Create data accessor object, if necessary
  148. if (!isset($_no_db_connection)) {
  149. if (!isset($_REQUEST['server'])) {
  150. echo $lang['strnoserversupplied'];
  151. exit;
  152. }
  153. $_server_info = $misc->getServerInfo();
  154. /* starting with PostgreSQL 9.0, we can set the application name */
  155. if(isset($_server_info['pgVersion']) && $_server_info['pgVersion'] >= 9)
  156. putenv("PGOPTIONS=--application_name={$appName}_{$appVersion}");
  157. // Redirect to the login form if not logged in
  158. if (!isset($_server_info['username'])) {
  159. include('./login.php');
  160. exit;
  161. }
  162. // Connect to the current database, or if one is not specified
  163. // then connect to the default database.
  164. if (isset($_REQUEST['database']))
  165. $_curr_db = $_REQUEST['database'];
  166. else
  167. $_curr_db = $_server_info['defaultdb'];
  168. include_once('./classes/database/Connection.php');
  169. // Connect to database and set the global $data variable
  170. $data = $misc->getDatabaseAccessor($_curr_db);
  171. // If schema is defined and database supports schemas, then set the
  172. // schema explicitly.
  173. if (isset($_REQUEST['database']) && isset($_REQUEST['schema'])) {
  174. $status = $data->setSchema($_REQUEST['schema']);
  175. if ($status != 0) {
  176. echo $lang['strbadschema'];
  177. exit;
  178. }
  179. }
  180. // Get database encoding
  181. $dbEncoding = $data->getDatabaseEncoding();
  182. // Set client encoding to database encoding
  183. if ($dbEncoding != '') {
  184. // Explicitly change client encoding if it's different to server encoding.
  185. if (function_exists('pg_client_encoding'))
  186. $currEncoding = pg_client_encoding($data->conn->_connectionID);
  187. elseif (function_exists('pg_clientencoding'))
  188. $currEncoding = pg_clientencoding($data->conn->_connectionID);
  189. else
  190. $currEncoding = null;
  191. if ($currEncoding != $dbEncoding) {
  192. $status = $data->setClientEncoding($dbEncoding);
  193. if ($status != 0 && $status != -99) {
  194. echo $lang['strbadencoding'];
  195. exit;
  196. }
  197. }
  198. // Override $lang['appcharset']
  199. if (isset($data->codemap[$dbEncoding]))
  200. $lang['appcharset'] = $data->codemap[$dbEncoding];
  201. else
  202. $lang['appcharset'] = $dbEncoding;
  203. }
  204. // Load Slony if required
  205. if ($_server_info['slony_support']) {
  206. include('./classes/plugins/Slony.php');
  207. $slony = new Slony();
  208. }
  209. }
  210. if (!function_exists("htmlspecialchars_decode")) {
  211. function htmlspecialchars_decode($string, $quote_style = ENT_COMPAT) {
  212. return strtr($string, array_flip(get_html_translation_table(HTML_SPECIALCHARS, $quote_style)));
  213. }
  214. }
  215. ?>