PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyadmin/libraries/common.inc.php

https://github.com/md-tech/openemr
PHP | 924 lines | 406 code | 108 blank | 410 comment | 120 complexity | 4f21ab9c5199e252969c08ecd668ab4e MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Misc stuff and REQUIRED by ALL the scripts.
  5. * MUST be included by every script
  6. *
  7. * Among other things, it contains the advanced authentication work.
  8. *
  9. * Order of sections for common.inc.php:
  10. *
  11. * LABEL_variables_init
  12. * - initialize some variables always needed
  13. * LABEL_parsing_config_file
  14. * - parsing of the configuration file
  15. * LABEL_loading_language_file
  16. * - loading language file
  17. * LABEL_theme_setup
  18. * - setting up themes
  19. *
  20. * - load of MySQL extension (if necessary)
  21. * - loading of an authentication library
  22. * - db connection
  23. * - authentication work
  24. *
  25. * @version $Id$
  26. */
  27. /**
  28. * For now, avoid warnings of E_STRICT mode
  29. * (this must be done before function definitions)
  30. */
  31. if (defined('E_STRICT')) {
  32. $old_error_reporting = error_reporting(0);
  33. if ($old_error_reporting & E_STRICT) {
  34. error_reporting($old_error_reporting ^ E_STRICT);
  35. } else {
  36. error_reporting($old_error_reporting);
  37. }
  38. unset($old_error_reporting);
  39. }
  40. // at this point PMA_PHP_INT_VERSION is not yet defined
  41. if (version_compare(phpversion(), '6', 'lt')) {
  42. /**
  43. * Avoid object cloning errors
  44. */
  45. @ini_set('zend.ze1_compatibility_mode', false);
  46. /**
  47. * Avoid problems with magic_quotes_runtime
  48. */
  49. @ini_set('magic_quotes_runtime', false);
  50. }
  51. /**
  52. * for verification in all procedural scripts under libraries
  53. */
  54. define('PHPMYADMIN', true);
  55. /**
  56. * core functions
  57. */
  58. require_once './libraries/core.lib.php';
  59. /**
  60. * Input sanitizing
  61. */
  62. require_once './libraries/sanitizing.lib.php';
  63. /**
  64. * the PMA_Theme class
  65. */
  66. require_once './libraries/Theme.class.php';
  67. /**
  68. * the PMA_Theme_Manager class
  69. */
  70. require_once './libraries/Theme_Manager.class.php';
  71. /**
  72. * the PMA_Config class
  73. */
  74. require_once './libraries/Config.class.php';
  75. /**
  76. * the PMA_Table class
  77. */
  78. require_once './libraries/Table.class.php';
  79. if (!defined('PMA_MINIMUM_COMMON')) {
  80. /**
  81. * common functions
  82. */
  83. require_once './libraries/common.lib.php';
  84. /**
  85. * Java script escaping.
  86. */
  87. require_once './libraries/js_escape.lib.php';
  88. /**
  89. * Include URL/hidden inputs generating.
  90. */
  91. require_once './libraries/url_generating.lib.php';
  92. }
  93. /******************************************************************************/
  94. /* start procedural code label_start_procedural */
  95. /**
  96. * protect against older PHP versions' bug about GLOBALS overwrite
  97. * (no need to localize this message :))
  98. * but what if script.php?GLOBALS[admin]=1&GLOBALS[_REQUEST]=1 ???
  99. */
  100. if (isset($_REQUEST['GLOBALS']) || isset($_FILES['GLOBALS'])
  101. || isset($_SERVER['GLOBALS']) || isset($_COOKIE['GLOBALS'])
  102. || isset($_ENV['GLOBALS'])) {
  103. die('GLOBALS overwrite attempt');
  104. }
  105. /**
  106. * protect against possible exploits - there is no need to have so much variables
  107. */
  108. if (count($_REQUEST) > 1000) {
  109. die('possible exploit');
  110. }
  111. /**
  112. * Check for numeric keys
  113. * (if register_globals is on, numeric key can be found in $GLOBALS)
  114. */
  115. foreach ($GLOBALS as $key => $dummy) {
  116. if (is_numeric($key)) {
  117. die('numeric key detected');
  118. }
  119. }
  120. unset($dummy);
  121. /**
  122. * PATH_INFO could be compromised if set, so remove it from PHP_SELF
  123. * and provide a clean PHP_SELF here
  124. */
  125. $PMA_PHP_SELF = PMA_getenv('PHP_SELF');
  126. $_PATH_INFO = PMA_getenv('PATH_INFO');
  127. if (! empty($_PATH_INFO) && ! empty($PMA_PHP_SELF)) {
  128. $path_info_pos = strrpos($PMA_PHP_SELF, $_PATH_INFO);
  129. if ($path_info_pos + strlen($_PATH_INFO) === strlen($PMA_PHP_SELF)) {
  130. $PMA_PHP_SELF = substr($PMA_PHP_SELF, 0, $path_info_pos);
  131. }
  132. }
  133. $PMA_PHP_SELF = htmlspecialchars($PMA_PHP_SELF);
  134. /**
  135. * just to be sure there was no import (registering) before here
  136. * we empty the global space (but avoid unsetting $variables_list
  137. * and $key in the foreach(), we still need them!)
  138. */
  139. $variables_whitelist = array (
  140. 'GLOBALS',
  141. '_SERVER',
  142. '_GET',
  143. '_POST',
  144. '_REQUEST',
  145. '_FILES',
  146. '_ENV',
  147. '_COOKIE',
  148. '_SESSION',
  149. 'PMA_PHP_SELF',
  150. 'variables_whitelist',
  151. 'key'
  152. );
  153. foreach (get_defined_vars() as $key => $value) {
  154. if (! in_array($key, $variables_whitelist)) {
  155. unset($$key);
  156. }
  157. }
  158. unset($key, $value, $variables_whitelist);
  159. /**
  160. * Subforms - some functions need to be called by form, cause of the limited URL
  161. * length, but if this functions inside another form you cannot just open a new
  162. * form - so phpMyAdmin uses 'arrays' inside this form
  163. *
  164. * <code>
  165. * <form ...>
  166. * ... main form elments ...
  167. * <input type="hidden" name="subform[action1][id]" value="1" />
  168. * ... other subform data ...
  169. * <input type="submit" name="usesubform[action1]" value="do action1" />
  170. * ... other subforms ...
  171. * <input type="hidden" name="subform[actionX][id]" value="X" />
  172. * ... other subform data ...
  173. * <input type="submit" name="usesubform[actionX]" value="do actionX" />
  174. * ... main form elments ...
  175. * <input type="submit" name="main_action" value="submit form" />
  176. * </form>
  177. * </code
  178. *
  179. * so we now check if a subform is submitted
  180. */
  181. $__redirect = null;
  182. if (isset($_POST['usesubform'])) {
  183. // if a subform is present and should be used
  184. // the rest of the form is deprecated
  185. $subform_id = key($_POST['usesubform']);
  186. $subform = $_POST['subform'][$subform_id];
  187. $_POST = $subform;
  188. $_REQUEST = $subform;
  189. /**
  190. * some subforms need another page than the main form, so we will just
  191. * include this page at the end of this script - we use $__redirect to
  192. * track this
  193. */
  194. if (isset($_POST['redirect'])
  195. && $_POST['redirect'] != basename($PMA_PHP_SELF)) {
  196. $__redirect = $_POST['redirect'];
  197. unset($_POST['redirect']);
  198. }
  199. unset($subform_id, $subform);
  200. } else {
  201. // Note: here we overwrite $_REQUEST so that it does not contain cookies,
  202. // because another application for the same domain could have set
  203. // a cookie (with a compatible path) that overrides a variable
  204. // we expect from GET or POST.
  205. // We'll refer to cookies explicitly with the $_COOKIE syntax.
  206. $_REQUEST = array_merge($_GET, $_POST);
  207. }
  208. // end check if a subform is submitted
  209. // remove quotes added by php
  210. // (get_magic_quotes_gpc() is deprecated in PHP 5.3, but compare with 5.2.99
  211. // to be able to test with 5.3.0-dev)
  212. if (function_exists('get_magic_quotes_gpc') && -1 == version_compare(PHP_VERSION, '5.2.99') && get_magic_quotes_gpc()) {
  213. PMA_arrayWalkRecursive($_GET, 'stripslashes', true);
  214. PMA_arrayWalkRecursive($_POST, 'stripslashes', true);
  215. PMA_arrayWalkRecursive($_COOKIE, 'stripslashes', true);
  216. PMA_arrayWalkRecursive($_REQUEST, 'stripslashes', true);
  217. }
  218. /**
  219. * clean cookies on new install or upgrade
  220. * when changing something with increment the cookie version
  221. */
  222. /*
  223. $pma_cookie_version = 4;
  224. if (isset($_COOKIE)
  225. && (! isset($_COOKIE['pmaCookieVer'])
  226. || $_COOKIE['pmaCookieVer'] < $pma_cookie_version)) {
  227. // delete all cookies
  228. foreach($_COOKIE as $cookie_name => $tmp) {
  229. PMA_removeCookie($cookie_name);
  230. }
  231. $_COOKIE = array();
  232. PMA_setCookie('pmaCookieVer', $pma_cookie_version);
  233. }
  234. */
  235. /**
  236. * include deprecated grab_globals only if required
  237. */
  238. if (empty($__redirect) && !defined('PMA_NO_VARIABLES_IMPORT')) {
  239. require './libraries/grab_globals.lib.php';
  240. }
  241. /**
  242. * include session handling after the globals, to prevent overwriting
  243. */
  244. require_once './libraries/session.inc.php';
  245. /**
  246. * init some variables LABEL_variables_init
  247. */
  248. /**
  249. * holds errors
  250. * @global array $GLOBALS['PMA_errors']
  251. */
  252. $GLOBALS['PMA_errors'] = array();
  253. /**
  254. * holds parameters to be passed to next page
  255. * @global array $GLOBALS['url_params']
  256. */
  257. $GLOBALS['url_params'] = array();
  258. /**
  259. * the whitelist for $GLOBALS['goto']
  260. * @global array $goto_whitelist
  261. */
  262. $goto_whitelist = array(
  263. //'browse_foreigners.php',
  264. //'calendar.php',
  265. //'changelog.php',
  266. //'chk_rel.php',
  267. 'db_create.php',
  268. 'db_datadict.php',
  269. 'db_sql.php',
  270. 'db_export.php',
  271. 'db_importdocsql.php',
  272. 'db_qbe.php',
  273. 'db_structure.php',
  274. 'db_import.php',
  275. 'db_operations.php',
  276. 'db_printview.php',
  277. 'db_search.php',
  278. //'Documentation.html',
  279. //'error.php',
  280. 'export.php',
  281. 'import.php',
  282. //'index.php',
  283. //'navigation.php',
  284. //'license.php',
  285. 'main.php',
  286. 'pdf_pages.php',
  287. 'pdf_schema.php',
  288. //'phpinfo.php',
  289. 'querywindow.php',
  290. //'readme.php',
  291. 'server_binlog.php',
  292. 'server_collations.php',
  293. 'server_databases.php',
  294. 'server_engines.php',
  295. 'server_export.php',
  296. 'server_import.php',
  297. 'server_privileges.php',
  298. 'server_processlist.php',
  299. 'server_sql.php',
  300. 'server_status.php',
  301. 'server_variables.php',
  302. 'sql.php',
  303. 'tbl_addfield.php',
  304. 'tbl_alter.php',
  305. 'tbl_change.php',
  306. 'tbl_create.php',
  307. 'tbl_import.php',
  308. 'tbl_indexes.php',
  309. 'tbl_move_copy.php',
  310. 'tbl_printview.php',
  311. 'tbl_sql.php',
  312. 'tbl_export.php',
  313. 'tbl_operations.php',
  314. 'tbl_structure.php',
  315. 'tbl_relation.php',
  316. 'tbl_replace.php',
  317. 'tbl_row_action.php',
  318. 'tbl_select.php',
  319. //'themes.php',
  320. 'transformation_overview.php',
  321. 'transformation_wrapper.php',
  322. 'translators.html',
  323. 'user_password.php',
  324. );
  325. /**
  326. * check $__redirect against whitelist
  327. */
  328. if (! PMA_checkPageValidity($__redirect, $goto_whitelist)) {
  329. $__redirect = null;
  330. }
  331. /**
  332. * holds page that should be displayed
  333. * @global string $GLOBALS['goto']
  334. */
  335. $GLOBALS['goto'] = '';
  336. // Security fix: disallow accessing serious server files via "?goto="
  337. if (PMA_checkPageValidity($_REQUEST['goto'], $goto_whitelist)) {
  338. $GLOBALS['goto'] = $_REQUEST['goto'];
  339. $GLOBALS['url_params']['goto'] = $_REQUEST['goto'];
  340. } else {
  341. unset($_REQUEST['goto'], $_GET['goto'], $_POST['goto'], $_COOKIE['goto']);
  342. }
  343. /**
  344. * returning page
  345. * @global string $GLOBALS['back']
  346. */
  347. if (PMA_checkPageValidity($_REQUEST['back'], $goto_whitelist)) {
  348. $GLOBALS['back'] = $_REQUEST['back'];
  349. } else {
  350. unset($_REQUEST['back'], $_GET['back'], $_POST['back'], $_COOKIE['back']);
  351. }
  352. /**
  353. * Check whether user supplied token is valid, if not remove any possibly
  354. * dangerous stuff from request.
  355. *
  356. * remember that some objects in the session with session_start and __wakeup()
  357. * could access this variables before we reach this point
  358. * f.e. PMA_Config: fontsize
  359. *
  360. * @todo variables should be handled by their respective owners (objects)
  361. * f.e. lang, server, convcharset, collation_connection in PMA_Config
  362. */
  363. if (! PMA_isValid($_REQUEST['token']) || $_SESSION[' PMA_token '] != $_REQUEST['token']) {
  364. /**
  365. * List of parameters which are allowed from unsafe source
  366. */
  367. $allow_list = array(
  368. /* needed for direct access, see FAQ 1.34
  369. * also, server needed for cookie login screen (multi-server)
  370. */
  371. 'server', 'db', 'table', 'target',
  372. /* to change the language on login screen or main page */
  373. 'lang',
  374. /* Session ID */
  375. 'phpMyAdmin',
  376. /* Cookie preferences */
  377. 'pma_lang', 'pma_charset', 'pma_collation_connection',
  378. /* Possible login form */
  379. 'pma_servername', 'pma_username', 'pma_password',
  380. );
  381. /**
  382. * Require cleanup functions
  383. */
  384. require_once './libraries/cleanup.lib.php';
  385. /**
  386. * Do actual cleanup
  387. */
  388. PMA_remove_request_vars($allow_list);
  389. }
  390. /**
  391. * @global string $convcharset
  392. * @see select_lang.lib.php
  393. */
  394. if (isset($_REQUEST['convcharset'])) {
  395. $convcharset = strip_tags($_REQUEST['convcharset']);
  396. }
  397. /**
  398. * current selected database
  399. * @global string $GLOBALS['db']
  400. */
  401. $GLOBALS['db'] = '';
  402. if (PMA_isValid($_REQUEST['db'])) {
  403. // can we strip tags from this?
  404. // only \ and / is not allowed in db names for MySQL
  405. $GLOBALS['db'] = $_REQUEST['db'];
  406. $GLOBALS['url_params']['db'] = $GLOBALS['db'];
  407. }
  408. /**
  409. * current selected table
  410. * @global string $GLOBALS['table']
  411. */
  412. $GLOBALS['table'] = '';
  413. if (PMA_isValid($_REQUEST['table'])) {
  414. // can we strip tags from this?
  415. // only \ and / is not allowed in table names for MySQL
  416. $GLOBALS['table'] = $_REQUEST['table'];
  417. $GLOBALS['url_params']['table'] = $GLOBALS['table'];
  418. }
  419. /**
  420. * SQL query to be executed
  421. * @global string $GLOBALS['sql_query']
  422. */
  423. $GLOBALS['sql_query'] = '';
  424. if (PMA_isValid($_REQUEST['sql_query'])) {
  425. $GLOBALS['sql_query'] = $_REQUEST['sql_query'];
  426. }
  427. /**
  428. * avoid problems in phpmyadmin.css.php in some cases
  429. * @global string $js_frame
  430. */
  431. $_REQUEST['js_frame'] = PMA_ifSetOr($_REQUEST['js_frame'], '');
  432. //$_REQUEST['set_theme'] // checked later in this file LABEL_theme_setup
  433. //$_REQUEST['server']; // checked later in this file
  434. //$_REQUEST['lang']; // checked by LABEL_loading_language_file
  435. /******************************************************************************/
  436. /* parsing configuration file LABEL_parsing_config_file */
  437. /**
  438. * We really need this one!
  439. */
  440. if (! function_exists('preg_replace')) {
  441. PMA_fatalError('strCantLoad', 'pcre');
  442. }
  443. /**
  444. * @global PMA_Config $_SESSION['PMA_Config']
  445. * force reading of config file, because we removed sensitive values
  446. * in the previous iteration
  447. */
  448. $_SESSION['PMA_Config'] = new PMA_Config('./config.inc.php');
  449. if (!defined('PMA_MINIMUM_COMMON')) {
  450. $_SESSION['PMA_Config']->checkPmaAbsoluteUri();
  451. }
  452. /**
  453. * BC - enable backward compatibility
  454. * exports all configuration settings into $GLOBALS ($GLOBALS['cfg'])
  455. */
  456. $_SESSION['PMA_Config']->enableBc();
  457. /**
  458. * check HTTPS connection
  459. */
  460. if ($_SESSION['PMA_Config']->get('ForceSSL')
  461. && !$_SESSION['PMA_Config']->get('is_https')) {
  462. PMA_sendHeaderLocation(
  463. preg_replace('/^http/', 'https',
  464. $_SESSION['PMA_Config']->get('PmaAbsoluteUri'))
  465. . PMA_generate_common_url($_GET));
  466. exit;
  467. }
  468. /******************************************************************************/
  469. /* loading language file LABEL_loading_language_file */
  470. /**
  471. * Added messages while developing:
  472. */
  473. if (file_exists('./lang/added_messages.php')) {
  474. include './lang/added_messages.php';
  475. }
  476. /**
  477. * Includes the language file if it hasn't been included yet
  478. */
  479. require './libraries/language.lib.php';
  480. /**
  481. * check for errors occurred while loading configuration
  482. * this check is done here after loading language files to present errors in locale
  483. */
  484. if ($_SESSION['PMA_Config']->error_config_file) {
  485. $GLOBALS['PMA_errors'][] = $strConfigFileError
  486. . '<br /><br />'
  487. . ($_SESSION['PMA_Config']->getSource() == './config.inc.php' ?
  488. '<a href="show_config_errors.php"'
  489. .' target="_blank">' . $_SESSION['PMA_Config']->getSource() . '</a>'
  490. :
  491. '<a href="' . $_SESSION['PMA_Config']->getSource() . '"'
  492. .' target="_blank">' . $_SESSION['PMA_Config']->getSource() . '</a>');
  493. }
  494. if ($_SESSION['PMA_Config']->error_config_default_file) {
  495. $GLOBALS['PMA_errors'][] = sprintf($strConfigDefaultFileError,
  496. $_SESSION['PMA_Config']->default_source);
  497. }
  498. if ($_SESSION['PMA_Config']->error_pma_uri) {
  499. $GLOBALS['PMA_errors'][] = sprintf($strPmaUriError);
  500. }
  501. /**
  502. * current server
  503. * @global integer $GLOBALS['server']
  504. */
  505. $GLOBALS['server'] = 0;
  506. /**
  507. * Servers array fixups.
  508. * $default_server comes from PMA_Config::enableBc()
  509. * @todo merge into PMA_Config
  510. */
  511. // Do we have some server?
  512. if (!isset($cfg['Servers']) || count($cfg['Servers']) == 0) {
  513. // No server => create one with defaults
  514. $cfg['Servers'] = array(1 => $default_server);
  515. } else {
  516. // We have server(s) => apply default configuration
  517. $new_servers = array();
  518. foreach ($cfg['Servers'] as $server_index => $each_server) {
  519. // Detect wrong configuration
  520. if (!is_int($server_index) || $server_index < 1) {
  521. $GLOBALS['PMA_errors'][] = sprintf($strInvalidServerIndex, $server_index);
  522. }
  523. $each_server = array_merge($default_server, $each_server);
  524. // Don't use servers with no hostname
  525. if ($each_server['connect_type'] == 'tcp' && empty($each_server['host'])) {
  526. $GLOBALS['PMA_errors'][] = sprintf($strInvalidServerHostname, $server_index);
  527. }
  528. // Final solution to bug #582890
  529. // If we are using a socket connection
  530. // and there is nothing in the verbose server name
  531. // or the host field, then generate a name for the server
  532. // in the form of "Server 2", localized of course!
  533. if ($each_server['connect_type'] == 'socket' && empty($each_server['host']) && empty($each_server['verbose'])) {
  534. $each_server['verbose'] = $GLOBALS['strServer'] . $server_index;
  535. }
  536. $new_servers[$server_index] = $each_server;
  537. }
  538. $cfg['Servers'] = $new_servers;
  539. unset($new_servers, $server_index, $each_server);
  540. }
  541. // Cleanup
  542. unset($default_server);
  543. /******************************************************************************/
  544. /* setup themes LABEL_theme_setup */
  545. /**
  546. * @global PMA_Theme_Manager $_SESSION['PMA_Theme_Manager']
  547. */
  548. if (! isset($_SESSION['PMA_Theme_Manager'])) {
  549. $_SESSION['PMA_Theme_Manager'] = new PMA_Theme_Manager;
  550. } else {
  551. /**
  552. * @todo move all __wakeup() functionality into session.inc.php
  553. */
  554. $_SESSION['PMA_Theme_Manager']->checkConfig();
  555. }
  556. // for the theme per server feature
  557. if (isset($_REQUEST['server']) && !isset($_REQUEST['set_theme'])) {
  558. $GLOBALS['server'] = $_REQUEST['server'];
  559. $tmp = $_SESSION['PMA_Theme_Manager']->getThemeCookie();
  560. if (empty($tmp)) {
  561. $tmp = $_SESSION['PMA_Theme_Manager']->theme_default;
  562. }
  563. $_SESSION['PMA_Theme_Manager']->setActiveTheme($tmp);
  564. unset($tmp);
  565. }
  566. /**
  567. * @todo move into PMA_Theme_Manager::__wakeup()
  568. */
  569. if (isset($_REQUEST['set_theme'])) {
  570. // if user selected a theme
  571. $_SESSION['PMA_Theme_Manager']->setActiveTheme($_REQUEST['set_theme']);
  572. }
  573. /**
  574. * the theme object
  575. * @global PMA_Theme $_SESSION['PMA_Theme']
  576. */
  577. $_SESSION['PMA_Theme'] = $_SESSION['PMA_Theme_Manager']->theme;
  578. // BC
  579. /**
  580. * the active theme
  581. * @global string $GLOBALS['theme']
  582. */
  583. $GLOBALS['theme'] = $_SESSION['PMA_Theme']->getName();
  584. /**
  585. * the theme path
  586. * @global string $GLOBALS['pmaThemePath']
  587. */
  588. $GLOBALS['pmaThemePath'] = $_SESSION['PMA_Theme']->getPath();
  589. /**
  590. * the theme image path
  591. * @global string $GLOBALS['pmaThemeImage']
  592. */
  593. $GLOBALS['pmaThemeImage'] = $_SESSION['PMA_Theme']->getImgPath();
  594. /**
  595. * load layout file if exists
  596. */
  597. if (@file_exists($_SESSION['PMA_Theme']->getLayoutFile())) {
  598. include $_SESSION['PMA_Theme']->getLayoutFile();
  599. /**
  600. * @todo remove if all themes are update use Navi instead of Left as frame name
  601. */
  602. if (! isset($GLOBALS['cfg']['NaviWidth'])
  603. && isset($GLOBALS['cfg']['LeftWidth'])) {
  604. $GLOBALS['cfg']['NaviWidth'] = $GLOBALS['cfg']['LeftWidth'];
  605. }
  606. }
  607. if (! defined('PMA_MINIMUM_COMMON')) {
  608. /**
  609. * Character set conversion.
  610. */
  611. require_once './libraries/charset_conversion.lib.php';
  612. /**
  613. * String handling
  614. */
  615. require_once './libraries/string.lib.php';
  616. /**
  617. * Lookup server by name
  618. * by Arnold - Helder Hosting
  619. * (see FAQ 4.8)
  620. */
  621. if (! empty($_REQUEST['server']) && is_string($_REQUEST['server'])
  622. && ! is_numeric($_REQUEST['server'])) {
  623. foreach ($cfg['Servers'] as $i => $server) {
  624. if ($server['host'] == $_REQUEST['server']) {
  625. $_REQUEST['server'] = $i;
  626. break;
  627. }
  628. }
  629. if (is_string($_REQUEST['server'])) {
  630. unset($_REQUEST['server']);
  631. }
  632. unset($i);
  633. }
  634. /**
  635. * If no server is selected, make sure that $cfg['Server'] is empty (so
  636. * that nothing will work), and skip server authentication.
  637. * We do NOT exit here, but continue on without logging into any server.
  638. * This way, the welcome page will still come up (with no server info) and
  639. * present a choice of servers in the case that there are multiple servers
  640. * and '$cfg['ServerDefault'] = 0' is set.
  641. */
  642. if (isset($_REQUEST['server']) && (is_string($_REQUEST['server']) || is_numeric($_REQUEST['server'])) && ! empty($_REQUEST['server']) && ! empty($cfg['Servers'][$_REQUEST['server']])) {
  643. $GLOBALS['server'] = $_REQUEST['server'];
  644. $cfg['Server'] = $cfg['Servers'][$GLOBALS['server']];
  645. } else {
  646. if (!empty($cfg['Servers'][$cfg['ServerDefault']])) {
  647. $GLOBALS['server'] = $cfg['ServerDefault'];
  648. $cfg['Server'] = $cfg['Servers'][$GLOBALS['server']];
  649. } else {
  650. $GLOBALS['server'] = 0;
  651. $cfg['Server'] = array();
  652. }
  653. }
  654. $GLOBALS['url_params']['server'] = $GLOBALS['server'];
  655. if (! empty($cfg['Server'])) {
  656. /**
  657. * Loads the proper database interface for this server
  658. */
  659. require_once './libraries/database_interface.lib.php';
  660. // Gets the authentication library that fits the $cfg['Server'] settings
  661. // and run authentication
  662. // to allow HTTP or http
  663. $cfg['Server']['auth_type'] = strtolower($cfg['Server']['auth_type']);
  664. if (! file_exists('./libraries/auth/' . $cfg['Server']['auth_type'] . '.auth.lib.php')) {
  665. PMA_fatalError($strInvalidAuthMethod . ' ' . $cfg['Server']['auth_type']);
  666. }
  667. /**
  668. * the required auth type plugin
  669. */
  670. require_once './libraries/auth/' . $cfg['Server']['auth_type'] . '.auth.lib.php';
  671. if (!PMA_auth_check()) {
  672. PMA_auth();
  673. } else {
  674. PMA_auth_set_user();
  675. }
  676. // Check IP-based Allow/Deny rules as soon as possible to reject the
  677. // user
  678. // Based on mod_access in Apache:
  679. // http://cvs.apache.org/viewcvs.cgi/httpd-2.0/modules/aaa/mod_access.c?rev=1.37&content-type=text/vnd.viewcvs-markup
  680. // Look at: "static int check_dir_access(request_rec *r)"
  681. // Robbat2 - May 10, 2002
  682. if (isset($cfg['Server']['AllowDeny'])
  683. && isset($cfg['Server']['AllowDeny']['order'])) {
  684. /**
  685. * ip based access library
  686. */
  687. require_once './libraries/ip_allow_deny.lib.php';
  688. $allowDeny_forbidden = false; // default
  689. if ($cfg['Server']['AllowDeny']['order'] == 'allow,deny') {
  690. $allowDeny_forbidden = true;
  691. if (PMA_allowDeny('allow')) {
  692. $allowDeny_forbidden = false;
  693. }
  694. if (PMA_allowDeny('deny')) {
  695. $allowDeny_forbidden = true;
  696. }
  697. } elseif ($cfg['Server']['AllowDeny']['order'] == 'deny,allow') {
  698. if (PMA_allowDeny('deny')) {
  699. $allowDeny_forbidden = true;
  700. }
  701. if (PMA_allowDeny('allow')) {
  702. $allowDeny_forbidden = false;
  703. }
  704. } elseif ($cfg['Server']['AllowDeny']['order'] == 'explicit') {
  705. if (PMA_allowDeny('allow')
  706. && !PMA_allowDeny('deny')) {
  707. $allowDeny_forbidden = false;
  708. } else {
  709. $allowDeny_forbidden = true;
  710. }
  711. } // end if ... elseif ... elseif
  712. // Ejects the user if banished
  713. if ($allowDeny_forbidden) {
  714. PMA_auth_fails();
  715. }
  716. unset($allowDeny_forbidden); //Clean up after you!
  717. } // end if
  718. // is root allowed?
  719. if (!$cfg['Server']['AllowRoot'] && $cfg['Server']['user'] == 'root') {
  720. $allowDeny_forbidden = true;
  721. PMA_auth_fails();
  722. unset($allowDeny_forbidden); //Clean up after you!
  723. }
  724. $bkp_track_err = @ini_set('track_errors', 1);
  725. // Try to connect MySQL with the control user profile (will be used to
  726. // get the privileges list for the current user but the true user link
  727. // must be open after this one so it would be default one for all the
  728. // scripts)
  729. $controllink = false;
  730. if ($cfg['Server']['controluser'] != '') {
  731. $controllink = PMA_DBI_connect($cfg['Server']['controluser'],
  732. $cfg['Server']['controlpass'], true);
  733. }
  734. if (! $controllink) {
  735. $controllink = PMA_DBI_connect($cfg['Server']['user'],
  736. $cfg['Server']['password'], true);
  737. } // end if ... else
  738. // Pass #1 of DB-Config to read in master level DB-Config will go here
  739. // Robbat2 - May 11, 2002
  740. // Connects to the server (validates user's login)
  741. $userlink = PMA_DBI_connect($cfg['Server']['user'],
  742. $cfg['Server']['password'], false);
  743. // Pass #2 of DB-Config to read in user level DB-Config will go here
  744. // Robbat2 - May 11, 2002
  745. @ini_set('track_errors', $bkp_track_err);
  746. unset($bkp_track_err);
  747. /**
  748. * If we auto switched to utf-8 we need to reread messages here
  749. */
  750. if (defined('PMA_LANG_RELOAD')) {
  751. require './libraries/language.lib.php';
  752. }
  753. /**
  754. * SQL Parser code
  755. */
  756. require_once './libraries/sqlparser.lib.php';
  757. /**
  758. * SQL Validator interface code
  759. */
  760. require_once './libraries/sqlvalidator.lib.php';
  761. /**
  762. * the PMA_List_Database class
  763. */
  764. require_once './libraries/List_Database.class.php';
  765. $PMA_List_Database = new PMA_List_Database($userlink, $controllink);
  766. /**
  767. * some resetting has to be done when switching servers
  768. */
  769. if (isset($_SESSION['userconf']['previous_server']) && $_SESSION['userconf']['previous_server'] != $GLOBALS['server']) {
  770. unset($_SESSION['userconf']['navi_limit_offset']);
  771. }
  772. $_SESSION['userconf']['previous_server'] = $GLOBALS['server'];
  773. } // end server connecting
  774. /**
  775. * Kanji encoding convert feature appended by Y.Kawada (2002/2/20)
  776. */
  777. if (@function_exists('mb_convert_encoding')
  778. && strpos(' ' . $lang, 'ja-')
  779. && file_exists('./libraries/kanji-encoding.lib.php')) {
  780. require_once './libraries/kanji-encoding.lib.php';
  781. /**
  782. * enable multibyte string support
  783. */
  784. define('PMA_MULTIBYTE_ENCODING', 1);
  785. } // end if
  786. /**
  787. * save some settings in cookies
  788. * @todo should be done in PMA_Config
  789. */
  790. PMA_setCookie('pma_lang', $GLOBALS['lang']);
  791. PMA_setCookie('pma_charset', $GLOBALS['convcharset']);
  792. PMA_setCookie('pma_collation_connection', $GLOBALS['collation_connection']);
  793. $_SESSION['PMA_Theme_Manager']->setThemeCookie();
  794. /**
  795. * check if profiling was requested and remember it
  796. * (note: when $cfg['ServerDefault'] = 0, constant is not defined)
  797. */
  798. if (PMA_profilingSupported() && isset($_REQUEST['profiling'])) {
  799. $_SESSION['profiling'] = true;
  800. } elseif (isset($_REQUEST['profiling_form'])) {
  801. // the checkbox was unchecked
  802. unset($_SESSION['profiling']);
  803. }
  804. } // end if !defined('PMA_MINIMUM_COMMON')
  805. // remove sensitive values from session
  806. $_SESSION['PMA_Config']->set('blowfish_secret', '');
  807. $_SESSION['PMA_Config']->set('Servers', '');
  808. $_SESSION['PMA_Config']->set('default_server', '');
  809. if (!empty($__redirect) && in_array($__redirect, $goto_whitelist)) {
  810. /**
  811. * include subform target page
  812. */
  813. require $__redirect;
  814. exit();
  815. }
  816. ?>