common.inc.php 34 KB

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