common.inc.php 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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. * the authentication libraries must be before the connection to db
  12. *
  13. * ... so the required order is:
  14. *
  15. * LABEL_variables_init
  16. * - initialize some variables always needed
  17. * LABEL_parsing_config_file
  18. * - parsing of the configuration file
  19. * LABEL_loading_language_file
  20. * - loading language file
  21. * LABEL_setup_servers
  22. * - check and setup configured servers
  23. * LABEL_theme_setup
  24. * - setting up themes
  25. *
  26. * - load of MySQL extension (if necessary)
  27. * - loading of an authentication library
  28. * - db connection
  29. * - authentication work
  30. *
  31. * @package PhpMyAdmin
  32. */
  33. /**
  34. * block attempts to directly run this script
  35. */
  36. if (getcwd() == dirname(__FILE__)) {
  37. die('Attack stopped');
  38. }
  39. /**
  40. * Minimum PHP version; can't call PMA_fatalError() which uses a
  41. * PHP 5 function, so cannot easily localize this message.
  42. */
  43. if (version_compare(PHP_VERSION, '5.2.0', 'lt')) {
  44. die('PHP 5.2+ is required');
  45. }
  46. /**
  47. * Backward compatibility for PHP 5.2
  48. */
  49. if (!defined('E_DEPRECATED')) {
  50. define('E_DEPRECATED', 8192);
  51. }
  52. /**
  53. * for verification in all procedural scripts under libraries
  54. */
  55. define('PHPMYADMIN', true);
  56. /**
  57. * the error handler
  58. */
  59. require './libraries/Error_Handler.class.php';
  60. /**
  61. * initialize the error handler
  62. */
  63. $GLOBALS['error_handler'] = new PMA_Error_Handler();
  64. $cfg['Error_Handler']['display'] = true;
  65. /*
  66. * This setting was removed in PHP 5.3. But at this point PMA_PHP_INT_VERSION
  67. * is not yet defined so we use another way to find out the PHP version.
  68. */
  69. if (version_compare(phpversion(), '5.3', 'lt')) {
  70. /**
  71. * Avoid object cloning errors
  72. */
  73. @ini_set('zend.ze1_compatibility_mode', false);
  74. }
  75. /**
  76. * This setting was removed in PHP 5.4. But at this point PMA_PHP_INT_VERSION
  77. * is not yet defined so we use another way to find out the PHP version.
  78. */
  79. if (version_compare(phpversion(), '5.4', 'lt')) {
  80. /**
  81. * Avoid problems with magic_quotes_runtime
  82. */
  83. @ini_set('magic_quotes_runtime', false);
  84. }
  85. /**
  86. * core functions
  87. */
  88. require './libraries/core.lib.php';
  89. /**
  90. * Input sanitizing
  91. */
  92. require './libraries/sanitizing.lib.php';
  93. /**
  94. * Warning about mbstring.
  95. */
  96. if (! function_exists('mb_detect_encoding')) {
  97. PMA_warnMissingExtension('mbstring', $fatal = true);
  98. }
  99. /**
  100. * the PMA_Theme class
  101. */
  102. require './libraries/Theme.class.php';
  103. /**
  104. * the PMA_Theme_Manager class
  105. */
  106. require './libraries/Theme_Manager.class.php';
  107. /**
  108. * the PMA_Config class
  109. */
  110. require './libraries/Config.class.php';
  111. /**
  112. * the relation lib, tracker needs it
  113. */
  114. require './libraries/relation.lib.php';
  115. /**
  116. * the PMA_Tracker class
  117. */
  118. require './libraries/Tracker.class.php';
  119. /**
  120. * the PMA_Table class
  121. */
  122. require './libraries/Table.class.php';
  123. /**
  124. * the PMA_Types class
  125. */
  126. require './libraries/Types.class.php';
  127. if (! defined('PMA_MINIMUM_COMMON') || defined('PMA_SETUP')) {
  128. /**
  129. * common functions
  130. */
  131. include_once './libraries/Util.class.php';
  132. /**
  133. * JavaScript escaping.
  134. */
  135. include_once './libraries/js_escape.lib.php';
  136. /**
  137. * Include URL/hidden inputs generating.
  138. */
  139. include_once './libraries/url_generating.lib.php';
  140. /**
  141. * Used to generate the page
  142. */
  143. include_once 'libraries/Response.class.php';
  144. }
  145. /******************************************************************************/
  146. /* start procedural code label_start_procedural */
  147. /**
  148. * PATH_INFO could be compromised if set, so remove it from PHP_SELF
  149. * and provide a clean PHP_SELF here
  150. */
  151. $PMA_PHP_SELF = PMA_getenv('PHP_SELF');
  152. $_PATH_INFO = PMA_getenv('PATH_INFO');
  153. if (! empty($_PATH_INFO) && ! empty($PMA_PHP_SELF)) {
  154. $path_info_pos = strrpos($PMA_PHP_SELF, $_PATH_INFO);
  155. if ($path_info_pos + strlen($_PATH_INFO) === strlen($PMA_PHP_SELF)) {
  156. $PMA_PHP_SELF = substr($PMA_PHP_SELF, 0, $path_info_pos);
  157. }
  158. }
  159. $PMA_PHP_SELF = htmlspecialchars($PMA_PHP_SELF);
  160. /**
  161. * just to be sure there was no import (registering) before here
  162. * we empty the global space (but avoid unsetting $variables_list
  163. * and $key in the foreach (), we still need them!)
  164. */
  165. $variables_whitelist = array (
  166. 'GLOBALS',
  167. '_SERVER',
  168. '_GET',
  169. '_POST',
  170. '_REQUEST',
  171. '_FILES',
  172. '_ENV',
  173. '_COOKIE',
  174. '_SESSION',
  175. 'error_handler',
  176. 'PMA_PHP_SELF',
  177. 'variables_whitelist',
  178. 'key'
  179. );
  180. foreach (get_defined_vars() as $key => $value) {
  181. if (! in_array($key, $variables_whitelist)) {
  182. unset($$key);
  183. }
  184. }
  185. unset($key, $value, $variables_whitelist);
  186. /**
  187. * Subforms - some functions need to be called by form, cause of the limited URL
  188. * length, but if this functions inside another form you cannot just open a new
  189. * form - so phpMyAdmin uses 'arrays' inside this form
  190. *
  191. * <code>
  192. * <form ...>
  193. * ... main form elments ...
  194. * <input type="hidden" name="subform[action1][id]" value="1" />
  195. * ... other subform data ...
  196. * <input type="submit" name="usesubform[action1]" value="do action1" />
  197. * ... other subforms ...
  198. * <input type="hidden" name="subform[actionX][id]" value="X" />
  199. * ... other subform data ...
  200. * <input type="submit" name="usesubform[actionX]" value="do actionX" />
  201. * ... main form elments ...
  202. * <input type="submit" name="main_action" value="submit form" />
  203. * </form>
  204. * </code>
  205. *
  206. * so we now check if a subform is submitted
  207. */
  208. $__redirect = null;
  209. if (isset($_POST['usesubform']) && ! defined('PMA_MINIMUM_COMMON')) {
  210. // if a subform is present and should be used
  211. // the rest of the form is deprecated
  212. $subform_id = key($_POST['usesubform']);
  213. $subform = $_POST['subform'][$subform_id];
  214. $_POST = $subform;
  215. $_REQUEST = $subform;
  216. /**
  217. * some subforms need another page than the main form, so we will just
  218. * include this page at the end of this script - we use $__redirect to
  219. * track this
  220. */
  221. if (isset($_POST['redirect'])
  222. && $_POST['redirect'] != basename($PMA_PHP_SELF)
  223. ) {
  224. $__redirect = $_POST['redirect'];
  225. unset($_POST['redirect']);
  226. }
  227. unset($subform_id, $subform);
  228. } else {
  229. // Note: here we overwrite $_REQUEST so that it does not contain cookies,
  230. // because another application for the same domain could have set
  231. // a cookie (with a compatible path) that overrides a variable
  232. // we expect from GET or POST.
  233. // We'll refer to cookies explicitly with the $_COOKIE syntax.
  234. $_REQUEST = array_merge($_GET, $_POST);
  235. }
  236. // end check if a subform is submitted
  237. /**
  238. * This setting was removed in PHP 5.4. But at this point PMA_PHP_INT_VERSION
  239. * is not yet defined so we use another way to find out the PHP version.
  240. */
  241. if (version_compare(phpversion(), '5.4', 'lt')) {
  242. // remove quotes added by PHP
  243. if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
  244. PMA_arrayWalkRecursive($_GET, 'stripslashes', true);
  245. PMA_arrayWalkRecursive($_POST, 'stripslashes', true);
  246. PMA_arrayWalkRecursive($_COOKIE, 'stripslashes', true);
  247. PMA_arrayWalkRecursive($_REQUEST, 'stripslashes', true);
  248. }
  249. }
  250. /**
  251. * check timezone setting
  252. * this could produce an E_STRICT - but only once,
  253. * if not done here it will produce E_STRICT on every date/time function
  254. * (starting with PHP 5.3, this code can produce E_WARNING rather than
  255. * E_STRICT)
  256. *
  257. */
  258. date_default_timezone_set(@date_default_timezone_get());
  259. /******************************************************************************/
  260. /* parsing configuration file LABEL_parsing_config_file */
  261. /**
  262. * We really need this one!
  263. */
  264. if (! function_exists('preg_replace')) {
  265. PMA_warnMissingExtension('pcre', true);
  266. }
  267. /**
  268. * JSON is required in several places.
  269. */
  270. if (! function_exists('json_encode')) {
  271. PMA_warnMissingExtension('json', true);
  272. }
  273. /**
  274. * @global PMA_Config $GLOBALS['PMA_Config']
  275. * force reading of config file, because we removed sensitive values
  276. * in the previous iteration
  277. */
  278. $GLOBALS['PMA_Config'] = new PMA_Config(CONFIG_FILE);
  279. if (!defined('PMA_MINIMUM_COMMON')) {
  280. $GLOBALS['PMA_Config']->checkPmaAbsoluteUri();
  281. }
  282. /**
  283. * BC - enable backward compatibility
  284. * exports all configuration settings into $GLOBALS ($GLOBALS['cfg'])
  285. */
  286. $GLOBALS['PMA_Config']->enableBc();
  287. /**
  288. * clean cookies on upgrade
  289. * when changing something related to PMA cookies, increment the cookie version
  290. */
  291. $pma_cookie_version = 4;
  292. if (isset($_COOKIE)
  293. && (isset($_COOKIE['pmaCookieVer'])
  294. && $_COOKIE['pmaCookieVer'] < $pma_cookie_version)
  295. ) {
  296. // delete all cookies
  297. foreach ($_COOKIE as $cookie_name => $tmp) {
  298. $GLOBALS['PMA_Config']->removeCookie($cookie_name);
  299. }
  300. $_COOKIE = array();
  301. $GLOBALS['PMA_Config']->setCookie('pmaCookieVer', $pma_cookie_version);
  302. }
  303. /**
  304. * check HTTPS connection
  305. */
  306. if ($GLOBALS['PMA_Config']->get('ForceSSL')
  307. && ! $GLOBALS['PMA_Config']->get('is_https')
  308. ) {
  309. require './libraries/select_lang.lib.php';
  310. // grab SSL URL
  311. $url = $GLOBALS['PMA_Config']->getSSLUri();
  312. // Actually redirect
  313. PMA_sendHeaderLocation($url . PMA_generate_common_url($_GET, 'text'));
  314. // delete the current session, otherwise we get problems (see bug #2397877)
  315. $GLOBALS['PMA_Config']->removeCookie($GLOBALS['session_name']);
  316. exit;
  317. }
  318. /**
  319. * include session handling after the globals, to prevent overwriting
  320. */
  321. require './libraries/session.inc.php';
  322. /**
  323. * init some variables LABEL_variables_init
  324. */
  325. /**
  326. * holds parameters to be passed to next page
  327. * @global array $GLOBALS['url_params']
  328. */
  329. $GLOBALS['url_params'] = array();
  330. /**
  331. * the whitelist for $GLOBALS['goto']
  332. * @global array $goto_whitelist
  333. */
  334. $goto_whitelist = array(
  335. //'browse_foreigners.php',
  336. //'changelog.php',
  337. //'chk_rel.php',
  338. 'db_create.php',
  339. 'db_datadict.php',
  340. 'db_sql.php',
  341. 'db_events.php',
  342. 'db_export.php',
  343. 'db_importdocsql.php',
  344. 'db_qbe.php',
  345. 'db_structure.php',
  346. 'db_import.php',
  347. 'db_operations.php',
  348. 'db_printview.php',
  349. 'db_search.php',
  350. 'db_routines.php',
  351. 'export.php',
  352. 'import.php',
  353. //'index.php',
  354. //'navigation.php',
  355. //'license.php',
  356. 'index.php',
  357. 'pdf_pages.php',
  358. 'pdf_schema.php',
  359. 'querywindow.php',
  360. 'server_binlog.php',
  361. 'server_collations.php',
  362. 'server_databases.php',
  363. 'server_engines.php',
  364. 'server_export.php',
  365. 'server_import.php',
  366. 'server_privileges.php',
  367. 'server_sql.php',
  368. 'server_status.php',
  369. 'server_status_advisor.php',
  370. 'server_status_monitor.php',
  371. 'server_status_queries.php',
  372. 'server_status_variables.php',
  373. 'server_variables.php',
  374. 'sql.php',
  375. 'tbl_addfield.php',
  376. 'tbl_change.php',
  377. 'tbl_create.php',
  378. 'tbl_import.php',
  379. 'tbl_indexes.php',
  380. 'tbl_move_copy.php',
  381. 'tbl_printview.php',
  382. 'tbl_sql.php',
  383. 'tbl_export.php',
  384. 'tbl_operations.php',
  385. 'tbl_structure.php',
  386. 'tbl_relation.php',
  387. 'tbl_replace.php',
  388. 'tbl_row_action.php',
  389. 'tbl_select.php',
  390. 'tbl_zoom_select.php',
  391. //'themes.php',
  392. 'transformation_overview.php',
  393. 'transformation_wrapper.php',
  394. 'user_password.php',
  395. );
  396. /**
  397. * check $__redirect against whitelist
  398. */
  399. if (! PMA_checkPageValidity($__redirect, $goto_whitelist)) {
  400. $__redirect = null;
  401. }
  402. /**
  403. * holds page that should be displayed
  404. * @global string $GLOBALS['goto']
  405. */
  406. $GLOBALS['goto'] = '';
  407. // Security fix: disallow accessing serious server files via "?goto="
  408. if (PMA_checkPageValidity($_REQUEST['goto'], $goto_whitelist)) {
  409. $GLOBALS['goto'] = $_REQUEST['goto'];
  410. $GLOBALS['url_params']['goto'] = $_REQUEST['goto'];
  411. } else {
  412. unset($_REQUEST['goto'], $_GET['goto'], $_POST['goto'], $_COOKIE['goto']);
  413. }
  414. /**
  415. * returning page
  416. * @global string $GLOBALS['back']
  417. */
  418. if (PMA_checkPageValidity($_REQUEST['back'], $goto_whitelist)) {
  419. $GLOBALS['back'] = $_REQUEST['back'];
  420. } else {
  421. unset($_REQUEST['back'], $_GET['back'], $_POST['back'], $_COOKIE['back']);
  422. }
  423. /**
  424. * Check whether user supplied token is valid, if not remove any possibly
  425. * dangerous stuff from request.
  426. *
  427. * remember that some objects in the session with session_start and __wakeup()
  428. * could access this variables before we reach this point
  429. * f.e. PMA_Config: fontsize
  430. *
  431. * @todo variables should be handled by their respective owners (objects)
  432. * f.e. lang, server, collation_connection in PMA_Config
  433. */
  434. $token_mismatch = true;
  435. if (PMA_isValid($_REQUEST['token'])) {
  436. $token_mismatch = ! hash_equals($_SESSION[' PMA_token '], $_REQUEST['token']);
  437. }
  438. if ($token_mismatch) {
  439. /**
  440. * List of parameters which are allowed from unsafe source
  441. */
  442. $allow_list = array(
  443. /* needed for direct access, see FAQ 1.34
  444. * also, server needed for cookie login screen (multi-server)
  445. */
  446. 'server', 'db', 'table', 'target', 'lang',
  447. /* Session ID */
  448. 'phpMyAdmin',
  449. /* Cookie preferences */
  450. 'pma_lang', 'pma_collation_connection',
  451. /* Possible login form */
  452. 'pma_servername', 'pma_username', 'pma_password',
  453. /* Needed to send the correct reply */
  454. 'ajax_request',
  455. /* Permit to log out even if there is a token mismatch */
  456. 'old_usr',
  457. /* url.php */
  458. 'url',
  459. );
  460. /**
  461. * Allow changing themes in test/theme.php
  462. */
  463. if (defined('PMA_TEST_THEME')) {
  464. $allow_list[] = 'set_theme';
  465. }
  466. /**
  467. * Require cleanup functions
  468. */
  469. include './libraries/cleanup.lib.php';
  470. /**
  471. * Do actual cleanup
  472. */
  473. PMA_remove_request_vars($allow_list);
  474. }
  475. /**
  476. * current selected database
  477. * @global string $GLOBALS['db']
  478. */
  479. $GLOBALS['db'] = '';
  480. if (PMA_isValid($_REQUEST['db'])) {
  481. // can we strip tags from this?
  482. // only \ and / is not allowed in db names for MySQL
  483. $GLOBALS['db'] = $_REQUEST['db'];
  484. $GLOBALS['url_params']['db'] = $GLOBALS['db'];
  485. }
  486. /**
  487. * current selected table
  488. * @global string $GLOBALS['table']
  489. */
  490. $GLOBALS['table'] = '';
  491. if (PMA_isValid($_REQUEST['table'])) {
  492. // can we strip tags from this?
  493. // only \ and / is not allowed in table names for MySQL
  494. $GLOBALS['table'] = $_REQUEST['table'];
  495. $GLOBALS['url_params']['table'] = $GLOBALS['table'];
  496. }
  497. /**
  498. * Store currently selected recent table.
  499. * Affect $GLOBALS['db'] and $GLOBALS['table']
  500. */
  501. if (PMA_isValid($_REQUEST['selected_recent_table'])) {
  502. $recent_table = json_decode($_REQUEST['selected_recent_table'], true);
  503. $GLOBALS['db'] = $recent_table['db'];
  504. $GLOBALS['url_params']['db'] = $GLOBALS['db'];
  505. $GLOBALS['table'] = $recent_table['table'];
  506. $GLOBALS['url_params']['table'] = $GLOBALS['table'];
  507. }
  508. /**
  509. * SQL query to be executed
  510. * @global string $GLOBALS['sql_query']
  511. */
  512. $GLOBALS['sql_query'] = '';
  513. if (PMA_isValid($_REQUEST['sql_query'])) {
  514. $GLOBALS['sql_query'] = $_REQUEST['sql_query'];
  515. }
  516. //$_REQUEST['set_theme'] // checked later in this file LABEL_theme_setup
  517. //$_REQUEST['server']; // checked later in this file
  518. //$_REQUEST['lang']; // checked by LABEL_loading_language_file
  519. /******************************************************************************/
  520. /* loading language file LABEL_loading_language_file */
  521. /**
  522. * lang detection is done here
  523. */
  524. require './libraries/select_lang.lib.php';
  525. // Defines the cell alignment values depending on text direction
  526. if ($GLOBALS['text_dir'] == 'ltr') {
  527. $GLOBALS['cell_align_left'] = 'left';
  528. $GLOBALS['cell_align_right'] = 'right';
  529. } else {
  530. $GLOBALS['cell_align_left'] = 'right';
  531. $GLOBALS['cell_align_right'] = 'left';
  532. }
  533. /**
  534. * check for errors occurred while loading configuration
  535. * this check is done here after loading language files to present errors in locale
  536. */
  537. $GLOBALS['PMA_Config']->checkPermissions();
  538. if ($GLOBALS['PMA_Config']->error_config_file) {
  539. $error = '[strong]' . __('Failed to read configuration file') . '[/strong]'
  540. . '[br][br]'
  541. . __('This usually means there is a syntax error in it, please check any errors shown below.')
  542. . '[br][br]'
  543. . '[conferr]';
  544. trigger_error($error, E_USER_ERROR);
  545. }
  546. if ($GLOBALS['PMA_Config']->error_config_default_file) {
  547. $error = sprintf(
  548. __('Could not load default configuration from: %1$s'),
  549. $GLOBALS['PMA_Config']->default_source
  550. );
  551. trigger_error($error, E_USER_ERROR);
  552. }
  553. if ($GLOBALS['PMA_Config']->error_pma_uri) {
  554. trigger_error(
  555. __('The [code]$cfg[\'PmaAbsoluteUri\'][/code] directive MUST be set in your configuration file!'),
  556. E_USER_ERROR
  557. );
  558. }
  559. /******************************************************************************/
  560. /* setup servers LABEL_setup_servers */
  561. /**
  562. * current server
  563. * @global integer $GLOBALS['server']
  564. */
  565. $GLOBALS['server'] = 0;
  566. /**
  567. * Servers array fixups.
  568. * $default_server comes from PMA_Config::enableBc()
  569. * @todo merge into PMA_Config
  570. */
  571. // Do we have some server?
  572. if (! isset($cfg['Servers']) || count($cfg['Servers']) == 0) {
  573. // No server => create one with defaults
  574. $cfg['Servers'] = array(1 => $default_server);
  575. } else {
  576. // We have server(s) => apply default configuration
  577. $new_servers = array();
  578. foreach ($cfg['Servers'] as $server_index => $each_server) {
  579. // Detect wrong configuration
  580. if (!is_int($server_index) || $server_index < 1) {
  581. trigger_error(
  582. sprintf(__('Invalid server index: %s'), $server_index),
  583. E_USER_ERROR
  584. );
  585. }
  586. $each_server = array_merge($default_server, $each_server);
  587. // Don't use servers with no hostname
  588. if ($each_server['connect_type'] == 'tcp' && empty($each_server['host'])) {
  589. trigger_error(
  590. sprintf(
  591. __('Invalid hostname for server %1$s. Please review your configuration.'),
  592. $server_index
  593. ),
  594. E_USER_ERROR
  595. );
  596. }
  597. // Final solution to bug #582890
  598. // If we are using a socket connection
  599. // and there is nothing in the verbose server name
  600. // or the host field, then generate a name for the server
  601. // in the form of "Server 2", localized of course!
  602. if ($each_server['connect_type'] == 'socket'
  603. && empty($each_server['host'])
  604. && empty($each_server['verbose'])
  605. ) {
  606. $each_server['verbose'] = __('Server') . $server_index;
  607. }
  608. $new_servers[$server_index] = $each_server;
  609. }
  610. $cfg['Servers'] = $new_servers;
  611. unset($new_servers, $server_index, $each_server);
  612. }
  613. // Cleanup
  614. unset($default_server);
  615. /******************************************************************************/
  616. /* setup themes LABEL_theme_setup */
  617. /**
  618. * @global PMA_Theme_Manager $_SESSION['PMA_Theme_Manager']
  619. */
  620. if (! isset($_SESSION['PMA_Theme_Manager'])) {
  621. $_SESSION['PMA_Theme_Manager'] = new PMA_Theme_Manager;
  622. } else {
  623. /**
  624. * @todo move all __wakeup() functionality into session.inc.php
  625. */
  626. $_SESSION['PMA_Theme_Manager']->checkConfig();
  627. }
  628. // for the theme per server feature
  629. if (isset($_REQUEST['server']) && ! isset($_REQUEST['set_theme'])) {
  630. $GLOBALS['server'] = $_REQUEST['server'];
  631. $tmp = $_SESSION['PMA_Theme_Manager']->getThemeCookie();
  632. if (empty($tmp)) {
  633. $tmp = $_SESSION['PMA_Theme_Manager']->theme_default;
  634. }
  635. $_SESSION['PMA_Theme_Manager']->setActiveTheme($tmp);
  636. unset($tmp);
  637. }
  638. /**
  639. * @todo move into PMA_Theme_Manager::__wakeup()
  640. */
  641. if (isset($_REQUEST['set_theme'])) {
  642. // if user selected a theme
  643. $_SESSION['PMA_Theme_Manager']->setActiveTheme($_REQUEST['set_theme']);
  644. }
  645. /**
  646. * the theme object
  647. * @global PMA_Theme $_SESSION['PMA_Theme']
  648. */
  649. $_SESSION['PMA_Theme'] = $_SESSION['PMA_Theme_Manager']->theme;
  650. // BC
  651. /**
  652. * the active theme
  653. * @global string $GLOBALS['theme']
  654. */
  655. $GLOBALS['theme'] = $_SESSION['PMA_Theme']->getName();
  656. /**
  657. * the theme path
  658. * @global string $GLOBALS['pmaThemePath']
  659. */
  660. $GLOBALS['pmaThemePath'] = $_SESSION['PMA_Theme']->getPath();
  661. /**
  662. * the theme image path
  663. * @global string $GLOBALS['pmaThemeImage']
  664. */
  665. $GLOBALS['pmaThemeImage'] = $_SESSION['PMA_Theme']->getImgPath();
  666. /**
  667. * load layout file if exists
  668. */
  669. if (@file_exists($_SESSION['PMA_Theme']->getLayoutFile())) {
  670. include $_SESSION['PMA_Theme']->getLayoutFile();
  671. }
  672. if (! defined('PMA_MINIMUM_COMMON')) {
  673. /**
  674. * Character set conversion.
  675. */
  676. include_once './libraries/charset_conversion.lib.php';
  677. /**
  678. * String handling
  679. */
  680. include_once './libraries/string.lib.php';
  681. /**
  682. * Lookup server by name
  683. * (see FAQ 4.8)
  684. */
  685. if (! empty($_REQUEST['server'])
  686. && is_string($_REQUEST['server'])
  687. && ! is_numeric($_REQUEST['server'])
  688. ) {
  689. foreach ($cfg['Servers'] as $i => $server) {
  690. if ($server['host'] == $_REQUEST['server']) {
  691. $_REQUEST['server'] = $i;
  692. break;
  693. }
  694. }
  695. if (is_string($_REQUEST['server'])) {
  696. unset($_REQUEST['server']);
  697. }
  698. unset($i);
  699. }
  700. /**
  701. * If no server is selected, make sure that $cfg['Server'] is empty (so
  702. * that nothing will work), and skip server authentication.
  703. * We do NOT exit here, but continue on without logging into any server.
  704. * This way, the welcome page will still come up (with no server info) and
  705. * present a choice of servers in the case that there are multiple servers
  706. * and '$cfg['ServerDefault'] = 0' is set.
  707. */
  708. if (isset($_REQUEST['server'])
  709. && (is_string($_REQUEST['server']) || is_numeric($_REQUEST['server']))
  710. && ! empty($_REQUEST['server'])
  711. && ! empty($cfg['Servers'][$_REQUEST['server']])
  712. ) {
  713. $GLOBALS['server'] = $_REQUEST['server'];
  714. $cfg['Server'] = $cfg['Servers'][$GLOBALS['server']];
  715. } else {
  716. if (!empty($cfg['Servers'][$cfg['ServerDefault']])) {
  717. $GLOBALS['server'] = $cfg['ServerDefault'];
  718. $cfg['Server'] = $cfg['Servers'][$GLOBALS['server']];
  719. } else {
  720. $GLOBALS['server'] = 0;
  721. $cfg['Server'] = array();
  722. }
  723. }
  724. $GLOBALS['url_params']['server'] = $GLOBALS['server'];
  725. /**
  726. * Kanji encoding convert feature appended by Y.Kawada (2002/2/20)
  727. */
  728. if (function_exists('mb_convert_encoding')
  729. && $lang == 'ja'
  730. ) {
  731. include_once './libraries/kanji-encoding.lib.php';
  732. } // end if
  733. /**
  734. * save some settings in cookies
  735. * @todo should be done in PMA_Config
  736. */
  737. $GLOBALS['PMA_Config']->setCookie('pma_lang', $GLOBALS['lang']);
  738. if (isset($GLOBALS['collation_connection'])) {
  739. $GLOBALS['PMA_Config']->setCookie(
  740. 'pma_collation_connection',
  741. $GLOBALS['collation_connection']
  742. );
  743. }
  744. $_SESSION['PMA_Theme_Manager']->setThemeCookie();
  745. if (! empty($cfg['Server'])) {
  746. /**
  747. * Loads the proper database interface for this server
  748. */
  749. include_once './libraries/database_interface.lib.php';
  750. include_once './libraries/logging.lib.php';
  751. // get LoginCookieValidity from preferences cache
  752. // no generic solution for loading preferences from cache as some settings
  753. // need to be kept for processing in PMA_Config::loadUserPreferences()
  754. $cache_key = 'server_' . $GLOBALS['server'];
  755. if (isset($_SESSION['cache'][$cache_key]['userprefs']['LoginCookieValidity'])) {
  756. $value = $_SESSION['cache'][$cache_key]['userprefs']['LoginCookieValidity'];
  757. $GLOBALS['PMA_Config']->set('LoginCookieValidity', $value);
  758. $GLOBALS['cfg']['LoginCookieValidity'] = $value;
  759. unset($value);
  760. }
  761. unset($cache_key);
  762. // Gets the authentication library that fits the $cfg['Server'] settings
  763. // and run authentication
  764. // to allow HTTP or http
  765. $cfg['Server']['auth_type'] = strtolower($cfg['Server']['auth_type']);
  766. /**
  767. * the required auth type plugin
  768. */
  769. $auth_class = "Authentication" . ucfirst($cfg['Server']['auth_type']);
  770. if (! file_exists('./libraries/plugins/auth/' . $auth_class . '.class.php')) {
  771. PMA_fatalError(
  772. __('Invalid authentication method set in configuration:')
  773. . ' ' . $cfg['Server']['auth_type']
  774. );
  775. }
  776. if (isset($_REQUEST['pma_password']) && strlen($_REQUEST['pma_password']) > 256) {
  777. $_REQUEST['pma_password'] = substr($_REQUEST['pma_password'], 0, 256);
  778. }
  779. include_once './libraries/plugins/auth/' . $auth_class . '.class.php';
  780. // todo: add plugin manager
  781. $plugin_manager = null;
  782. $auth_plugin = new $auth_class($plugin_manager);
  783. if (! $auth_plugin->authCheck()) {
  784. /* Force generating of new session on login */
  785. PMA_secureSession();
  786. $auth_plugin->auth();
  787. } else {
  788. $auth_plugin->authSetUser();
  789. }
  790. // Check IP-based Allow/Deny rules as soon as possible to reject the
  791. // user
  792. // Based on mod_access in Apache:
  793. // http://cvs.apache.org/viewcvs.cgi/httpd-2.0/modules/aaa/mod_access.c?rev=1.37&content-type=text/vnd.viewcvs-markup
  794. // Look at: "static int check_dir_access(request_rec *r)"
  795. if (isset($cfg['Server']['AllowDeny'])
  796. && isset($cfg['Server']['AllowDeny']['order'])
  797. ) {
  798. /**
  799. * ip based access library
  800. */
  801. include_once './libraries/ip_allow_deny.lib.php';
  802. $allowDeny_forbidden = false; // default
  803. if ($cfg['Server']['AllowDeny']['order'] == 'allow,deny') {
  804. $allowDeny_forbidden = true;
  805. if (PMA_allowDeny('allow')) {
  806. $allowDeny_forbidden = false;
  807. }
  808. if (PMA_allowDeny('deny')) {
  809. $allowDeny_forbidden = true;
  810. }
  811. } elseif ($cfg['Server']['AllowDeny']['order'] == 'deny,allow') {
  812. if (PMA_allowDeny('deny')) {
  813. $allowDeny_forbidden = true;
  814. }
  815. if (PMA_allowDeny('allow')) {
  816. $allowDeny_forbidden = false;
  817. }
  818. } elseif ($cfg['Server']['AllowDeny']['order'] == 'explicit') {
  819. if (PMA_allowDeny('allow') && ! PMA_allowDeny('deny')) {
  820. $allowDeny_forbidden = false;
  821. } else {
  822. $allowDeny_forbidden = true;
  823. }
  824. } // end if ... elseif ... elseif
  825. // Ejects the user if banished
  826. if ($allowDeny_forbidden) {
  827. PMA_log_user($cfg['Server']['user'], 'allow-denied');
  828. $auth_plugin->authFails();
  829. }
  830. } // end if
  831. // is root allowed?
  832. if (!$cfg['Server']['AllowRoot'] && $cfg['Server']['user'] == 'root') {
  833. $allowDeny_forbidden = true;
  834. PMA_log_user($cfg['Server']['user'], 'root-denied');
  835. $auth_plugin->authFails();
  836. }
  837. // is a login without password allowed?
  838. if (!$cfg['Server']['AllowNoPassword'] && $cfg['Server']['password'] == '') {
  839. $login_without_password_is_forbidden = true;
  840. PMA_log_user($cfg['Server']['user'], 'empty-denied');
  841. $auth_plugin->authFails();
  842. }
  843. // if using TCP socket is not needed
  844. if (strtolower($cfg['Server']['connect_type']) == 'tcp') {
  845. $cfg['Server']['socket'] = '';
  846. }
  847. // Try to connect MySQL with the control user profile (will be used to
  848. // get the privileges list for the current user but the true user link
  849. // must be open after this one so it would be default one for all the
  850. // scripts)
  851. $controllink = false;
  852. if ($cfg['Server']['controluser'] != '') {
  853. if (! empty($cfg['Server']['controlhost'])) {
  854. $controllink = PMA_DBI_connect(
  855. $cfg['Server']['controluser'],
  856. $cfg['Server']['controlpass'],
  857. true,
  858. array('host' => $cfg['Server']['controlhost'])
  859. );
  860. } else {
  861. $controllink = PMA_DBI_connect(
  862. $cfg['Server']['controluser'],
  863. $cfg['Server']['controlpass'],
  864. true
  865. );
  866. }
  867. }
  868. // Connects to the server (validates user's login)
  869. $userlink = PMA_DBI_connect(
  870. $cfg['Server']['user'], $cfg['Server']['password'], false
  871. );
  872. if (! $controllink) {
  873. $controllink = $userlink;
  874. }
  875. $auth_plugin->storeUserCredentials();
  876. /* Log success */
  877. PMA_log_user($cfg['Server']['user']);
  878. /**
  879. * with phpMyAdmin 3 we support MySQL >=5
  880. * but only production releases:
  881. * - > 5.0.15
  882. */
  883. if (PMA_MYSQL_INT_VERSION < 50015) {
  884. PMA_fatalError(
  885. __('You should upgrade to %s %s or later.'),
  886. array('MySQL', '5.0.15')
  887. );
  888. }
  889. /**
  890. * Type handling object.
  891. */
  892. if (PMA_DRIZZLE) {
  893. $GLOBALS['PMA_Types'] = new PMA_Types_Drizzle();
  894. } else {
  895. $GLOBALS['PMA_Types'] = new PMA_Types_MySQL();
  896. }
  897. if (PMA_DRIZZLE) {
  898. // DisableIS must be set to false for Drizzle, it maps SHOW commands
  899. // to INFORMATION_SCHEMA queries anyway so it's fast on large servers
  900. $cfg['Server']['DisableIS'] = false;
  901. // SHOW OPEN TABLES is not supported by Drizzle
  902. $cfg['SkipLockedTables'] = false;
  903. }
  904. /**
  905. * SQL Parser code
  906. */
  907. include_once './libraries/sqlparser.lib.php';
  908. /**
  909. * SQL Validator interface code
  910. */
  911. include_once './libraries/sqlvalidator.lib.php';
  912. /**
  913. * the PMA_List_Database class
  914. */
  915. include_once './libraries/PMA.php';
  916. $pma = new PMA;
  917. $pma->userlink = $userlink;
  918. $pma->controllink = $controllink;
  919. /**
  920. * some resetting has to be done when switching servers
  921. */
  922. if (isset($_SESSION['tmp_user_values']['previous_server'])
  923. && $_SESSION['tmp_user_values']['previous_server'] != $GLOBALS['server']
  924. ) {
  925. unset($_SESSION['tmp_user_values']['navi_limit_offset']);
  926. }
  927. $_SESSION['tmp_user_values']['previous_server'] = $GLOBALS['server'];
  928. } // end server connecting
  929. /**
  930. * check if profiling was requested and remember it
  931. * (note: when $cfg['ServerDefault'] = 0, constant is not defined)
  932. */
  933. if (isset($_REQUEST['profiling'])
  934. && PMA_Util::profilingSupported()
  935. ) {
  936. $_SESSION['profiling'] = true;
  937. } elseif (isset($_REQUEST['profiling_form'])) {
  938. // the checkbox was unchecked
  939. unset($_SESSION['profiling']);
  940. }
  941. /**
  942. * Inclusion of profiling scripts is needed on various
  943. * pages like sql, tbl_sql, db_sql, tbl_select
  944. */
  945. if (! defined('PMA_BYPASS_GET_INSTANCE')) {
  946. $response = PMA_Response::getInstance();
  947. }
  948. if (isset($_SESSION['profiling'])) {
  949. $header = $response->getHeader();
  950. $scripts = $header->getScripts();
  951. /* < IE 9 doesn't support canvas natively */
  952. if (PMA_USR_BROWSER_AGENT == 'IE' && PMA_USR_BROWSER_VER < 9) {
  953. $scripts->addFile('canvg/flashcanvas.js');
  954. }
  955. $scripts->addFile('jqplot/jquery.jqplot.js');
  956. $scripts->addFile('jqplot/plugins/jqplot.pieRenderer.js');
  957. $scripts->addFile('canvg/canvg.js');
  958. }
  959. /*
  960. * There is no point in even attempting to process
  961. * an ajax request if there is a token mismatch
  962. */
  963. if (isset($response) && $response->isAjax() && $token_mismatch) {
  964. $response->isSuccess(false);
  965. $response->addJSON(
  966. 'message',
  967. PMA_Message::error(__('Error: Token mismatch'))
  968. );
  969. exit;
  970. }
  971. } // end if !defined('PMA_MINIMUM_COMMON')
  972. // load user preferences
  973. $GLOBALS['PMA_Config']->loadUserPreferences();
  974. // remove sensitive values from session
  975. $GLOBALS['PMA_Config']->set('blowfish_secret', '');
  976. $GLOBALS['PMA_Config']->set('Servers', '');
  977. $GLOBALS['PMA_Config']->set('default_server', '');
  978. /* Tell tracker that it can actually work */
  979. PMA_Tracker::enable();
  980. /**
  981. * @global boolean $GLOBALS['is_ajax_request']
  982. * @todo should this be moved to the variables init section above?
  983. *
  984. * Check if the current request is an AJAX request, and set is_ajax_request
  985. * accordingly. Suppress headers, footers and unnecessary output if set to
  986. * true
  987. */
  988. if (isset($_REQUEST['ajax_request']) && $_REQUEST['ajax_request'] == true) {
  989. $GLOBALS['is_ajax_request'] = true;
  990. } else {
  991. $GLOBALS['is_ajax_request'] = false;
  992. }
  993. /**
  994. * @global boolean $GLOBALS['grid_edit']
  995. *
  996. * Set to true if this is a request made during an grid edit process. This
  997. * request is made to retrieve the non-truncated/transformed values.
  998. */
  999. if (isset($_REQUEST['grid_edit']) && $_REQUEST['grid_edit'] == true) {
  1000. $GLOBALS['grid_edit'] = true;
  1001. } else {
  1002. $GLOBALS['grid_edit'] = false;
  1003. }
  1004. if (isset($_REQUEST['GLOBALS']) || isset($_FILES['GLOBALS'])) {
  1005. PMA_fatalError(__("GLOBALS overwrite attempt"));
  1006. }
  1007. /**
  1008. * protect against possible exploits - there is no need to have so much variables
  1009. */
  1010. if (count($_REQUEST) > 1000) {
  1011. PMA_fatalError(__('possible exploit'));
  1012. }
  1013. /**
  1014. * Check for numeric keys
  1015. * (if register_globals is on, numeric key can be found in $GLOBALS)
  1016. */
  1017. foreach ($GLOBALS as $key => $dummy) {
  1018. if (is_numeric($key)) {
  1019. PMA_fatalError(__('numeric key detected'));
  1020. }
  1021. }
  1022. unset($dummy);
  1023. // here, the function does not exist with this configuration:
  1024. // $cfg['ServerDefault'] = 0;
  1025. $GLOBALS['is_superuser'] = function_exists('PMA_isSuperuser') && PMA_isSuperuser();
  1026. if (!empty($__redirect) && in_array($__redirect, $goto_whitelist)) {
  1027. /**
  1028. * include subform target page
  1029. */
  1030. include $__redirect;
  1031. exit();
  1032. }
  1033. ?>