common.inc.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /**
  3. * Misc stuff and REQUIRED by ALL the scripts.
  4. * MUST be included by every script
  5. *
  6. * Among other things, it contains the advanced authentication work.
  7. *
  8. * Order of sections for common.inc.php:
  9. *
  10. * the authentication libraries must be before the connection to db
  11. *
  12. * ... so the required order is:
  13. *
  14. * LABEL_variables_init
  15. * - initialize some variables always needed
  16. * LABEL_parsing_config_file
  17. * - parsing of the configuration file
  18. * LABEL_loading_language_file
  19. * - loading language file
  20. * LABEL_setup_servers
  21. * - check and setup configured servers
  22. * LABEL_theme_setup
  23. * - setting up themes
  24. *
  25. * - load of MySQL extension (if necessary)
  26. * - loading of an authentication library
  27. * - db connection
  28. * - authentication work
  29. */
  30. declare(strict_types=1);
  31. use PhpMyAdmin\Config;
  32. use PhpMyAdmin\Core;
  33. use PhpMyAdmin\DatabaseInterface;
  34. use PhpMyAdmin\ErrorHandler;
  35. use PhpMyAdmin\LanguageManager;
  36. use PhpMyAdmin\Logging;
  37. use PhpMyAdmin\Message;
  38. use PhpMyAdmin\MoTranslator\Loader;
  39. use PhpMyAdmin\Plugins;
  40. use PhpMyAdmin\Profiling;
  41. use PhpMyAdmin\Response;
  42. use PhpMyAdmin\Routing;
  43. use PhpMyAdmin\Session;
  44. use PhpMyAdmin\SqlParser\Lexer;
  45. use PhpMyAdmin\ThemeManager;
  46. use PhpMyAdmin\Tracker;
  47. global $containerBuilder, $error_handler, $PMA_Config, $server, $dbi;
  48. global $lang, $cfg, $isConfigLoading, $auth_plugin, $route, $PMA_Theme;
  49. global $url_params, $goto, $back, $db, $table, $sql_query, $token_mismatch;
  50. /**
  51. * block attempts to directly run this script
  52. */
  53. if (getcwd() == __DIR__) {
  54. die('Attack stopped');
  55. }
  56. /**
  57. * Minimum PHP version; can't call Core::fatalError() which uses a
  58. * PHP 5 function, so cannot easily localize this message.
  59. */
  60. if (PHP_VERSION_ID < 70103) {
  61. die(
  62. '<p>PHP 7.1.3+ is required.</p>'
  63. . '<p>Currently installed version is: ' . PHP_VERSION . '</p>'
  64. );
  65. }
  66. // phpcs:disable PSR1.Files.SideEffects
  67. /**
  68. * for verification in all procedural scripts under libraries
  69. */
  70. define('PHPMYADMIN', true);
  71. // phpcs:enable
  72. /**
  73. * Load vendor configuration.
  74. */
  75. require_once ROOT_PATH . 'libraries/vendor_config.php';
  76. /**
  77. * Activate autoloader
  78. */
  79. if (! @is_readable(AUTOLOAD_FILE)) {
  80. die(
  81. '<p>File <samp>' . AUTOLOAD_FILE . '</samp> missing or not readable.</p>'
  82. . '<p>Most likely you did not run Composer to '
  83. . '<a href="https://docs.phpmyadmin.net/en/latest/setup.html#installing-from-git">'
  84. . 'install library files</a>.</p>'
  85. );
  86. }
  87. require_once AUTOLOAD_FILE;
  88. /**
  89. * (TCPDF workaround)
  90. * Avoid referring to nonexistent files (causes warnings when open_basedir is used)
  91. * This is defined to avoid the tcpdf code to search for a directory outside of open_basedir
  92. * See: https://github.com/phpmyadmin/phpmyadmin/issues/16709
  93. * This value if not used but is usefull, no header logic is used for PDF exports
  94. */
  95. if (! defined('K_PATH_IMAGES')) {
  96. // phpcs:disable PSR1.Files.SideEffects
  97. define('K_PATH_IMAGES', ROOT_PATH);
  98. // phpcs:enable
  99. }
  100. $route = Routing::getCurrentRoute();
  101. if ($route === '/import-status') {
  102. // phpcs:disable PSR1.Files.SideEffects
  103. define('PMA_MINIMUM_COMMON', true);
  104. // phpcs:enable
  105. }
  106. $containerBuilder = Core::getContainerBuilder();
  107. /**
  108. * Load gettext functions.
  109. */
  110. Loader::loadFunctions();
  111. /** @var ErrorHandler $error_handler */
  112. $error_handler = $containerBuilder->get('error_handler');
  113. /**
  114. * Warning about missing PHP extensions.
  115. */
  116. Core::checkExtensions();
  117. /**
  118. * Configure required PHP settings.
  119. */
  120. Core::configure();
  121. /* start procedural code label_start_procedural */
  122. Core::cleanupPathInfo();
  123. /* parsing configuration file LABEL_parsing_config_file */
  124. /** @var bool $isConfigLoading Indication for the error handler */
  125. $isConfigLoading = false;
  126. /**
  127. * Force reading of config file, because we removed sensitive values
  128. * in the previous iteration.
  129. *
  130. * @var Config $PMA_Config
  131. */
  132. $PMA_Config = $containerBuilder->get('config');
  133. register_shutdown_function([Config::class, 'fatalErrorHandler']);
  134. /**
  135. * include session handling after the globals, to prevent overwriting
  136. */
  137. if (! defined('PMA_NO_SESSION')) {
  138. Session::setUp($PMA_Config, $error_handler);
  139. }
  140. /**
  141. * init some variables LABEL_variables_init
  142. */
  143. /**
  144. * holds parameters to be passed to next page
  145. *
  146. * @global array $url_params
  147. */
  148. $url_params = [];
  149. $containerBuilder->setParameter('url_params', $url_params);
  150. Core::setGotoAndBackGlobals($containerBuilder, $PMA_Config);
  151. Core::checkTokenRequestParam();
  152. Core::setDatabaseAndTableFromRequest($containerBuilder);
  153. /**
  154. * SQL query to be executed
  155. *
  156. * @global string $sql_query
  157. */
  158. $sql_query = '';
  159. if (Core::isValid($_POST['sql_query'])) {
  160. $sql_query = $_POST['sql_query'];
  161. }
  162. $containerBuilder->setParameter('sql_query', $sql_query);
  163. //$_REQUEST['set_theme'] // checked later in this file LABEL_theme_setup
  164. //$_REQUEST['server']; // checked later in this file
  165. //$_REQUEST['lang']; // checked by LABEL_loading_language_file
  166. /* loading language file LABEL_loading_language_file */
  167. /**
  168. * lang detection is done here
  169. */
  170. $language = LanguageManager::getInstance()->selectLanguage();
  171. $language->activate();
  172. /**
  173. * check for errors occurred while loading configuration
  174. * this check is done here after loading language files to present errors in locale
  175. */
  176. $PMA_Config->checkPermissions();
  177. $PMA_Config->checkErrors();
  178. /* Check server configuration */
  179. Core::checkConfiguration();
  180. /* Check request for possible attacks */
  181. Core::checkRequest();
  182. /* setup servers LABEL_setup_servers */
  183. $PMA_Config->checkServers();
  184. /**
  185. * current server
  186. *
  187. * @global integer $server
  188. */
  189. $server = $PMA_Config->selectServer();
  190. $url_params['server'] = $server;
  191. $containerBuilder->setParameter('server', $server);
  192. $containerBuilder->setParameter('url_params', $url_params);
  193. /**
  194. * BC - enable backward compatibility
  195. * exports all configuration settings into globals ($cfg global)
  196. */
  197. $PMA_Config->enableBc();
  198. /* setup themes LABEL_theme_setup */
  199. $PMA_Theme = ThemeManager::initializeTheme();
  200. /** @var DatabaseInterface $dbi */
  201. $dbi = null;
  202. if (! defined('PMA_MINIMUM_COMMON')) {
  203. /**
  204. * save some settings in cookies
  205. *
  206. * @todo should be done in PhpMyAdmin\Config
  207. */
  208. $PMA_Config->setCookie('pma_lang', (string) $lang);
  209. ThemeManager::getInstance()->setThemeCookie();
  210. $dbi = DatabaseInterface::load();
  211. $containerBuilder->set(DatabaseInterface::class, $dbi);
  212. $containerBuilder->setAlias('dbi', DatabaseInterface::class);
  213. if (! empty($cfg['Server'])) {
  214. $PMA_Config->getLoginCookieValidityFromCache($server);
  215. $auth_plugin = Plugins::getAuthPlugin();
  216. $auth_plugin->authenticate();
  217. Core::connectToDatabaseServer($dbi, $auth_plugin);
  218. $auth_plugin->rememberCredentials();
  219. $auth_plugin->checkTwoFactor();
  220. /* Log success */
  221. Logging::logUser($cfg['Server']['user']);
  222. if ($dbi->getVersion() < $cfg['MysqlMinVersion']['internal']) {
  223. Core::fatalError(
  224. __('You should upgrade to %s %s or later.'),
  225. [
  226. 'MySQL',
  227. $cfg['MysqlMinVersion']['human'],
  228. ]
  229. );
  230. }
  231. // Sets the default delimiter (if specified).
  232. if (! empty($_REQUEST['sql_delimiter'])) {
  233. Lexer::$DEFAULT_DELIMITER = $_REQUEST['sql_delimiter'];
  234. }
  235. // TODO: Set SQL modes too.
  236. } else { // end server connecting
  237. $response = Response::getInstance();
  238. $response->getHeader()->disableMenuAndConsole();
  239. $response->getFooter()->setMinimal();
  240. }
  241. $response = Response::getInstance();
  242. Profiling::check($dbi, $response);
  243. /*
  244. * There is no point in even attempting to process
  245. * an ajax request if there is a token mismatch
  246. */
  247. if ($response->isAjax() && $_SERVER['REQUEST_METHOD'] === 'POST' && $token_mismatch) {
  248. $response->setRequestStatus(false);
  249. $response->addJSON(
  250. 'message',
  251. Message::error(__('Error: Token mismatch'))
  252. );
  253. exit;
  254. }
  255. $containerBuilder->set('response', Response::getInstance());
  256. }
  257. // load user preferences
  258. $PMA_Config->loadUserPreferences();
  259. $containerBuilder->set('theme_manager', ThemeManager::getInstance());
  260. /* Tell tracker that it can actually work */
  261. Tracker::enable();
  262. if (! defined('PMA_MINIMUM_COMMON')
  263. && ! empty($server)
  264. && isset($cfg['ZeroConf'])
  265. && $cfg['ZeroConf'] == true
  266. ) {
  267. $dbi->postConnectControl();
  268. }