HomeController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers;
  4. use PhpMyAdmin\Charsets;
  5. use PhpMyAdmin\Charsets\Charset;
  6. use PhpMyAdmin\Charsets\Collation;
  7. use PhpMyAdmin\CheckUserPrivileges;
  8. use PhpMyAdmin\Config;
  9. use PhpMyAdmin\DatabaseInterface;
  10. use PhpMyAdmin\Git;
  11. use PhpMyAdmin\Html\Generator;
  12. use PhpMyAdmin\LanguageManager;
  13. use PhpMyAdmin\Message;
  14. use PhpMyAdmin\RecentFavoriteTable;
  15. use PhpMyAdmin\Relation;
  16. use PhpMyAdmin\Response;
  17. use PhpMyAdmin\Server\Select;
  18. use PhpMyAdmin\Template;
  19. use PhpMyAdmin\ThemeManager;
  20. use PhpMyAdmin\Url;
  21. use PhpMyAdmin\UserPreferences;
  22. use PhpMyAdmin\Util;
  23. use const E_USER_NOTICE;
  24. use const E_USER_WARNING;
  25. use const PHP_VERSION;
  26. use function count;
  27. use function extension_loaded;
  28. use function file_exists;
  29. use function ini_get;
  30. use function preg_match;
  31. use function sprintf;
  32. use function strlen;
  33. use function strtotime;
  34. use function trigger_error;
  35. class HomeController extends AbstractController
  36. {
  37. /** @var Config */
  38. private $config;
  39. /** @var ThemeManager */
  40. private $themeManager;
  41. /** @var DatabaseInterface */
  42. private $dbi;
  43. /**
  44. * @param Response $response
  45. * @param Config $config
  46. * @param DatabaseInterface $dbi
  47. */
  48. public function __construct($response, Template $template, $config, ThemeManager $themeManager, $dbi)
  49. {
  50. parent::__construct($response, $template);
  51. $this->config = $config;
  52. $this->themeManager = $themeManager;
  53. $this->dbi = $dbi;
  54. }
  55. public function index(): void
  56. {
  57. global $cfg, $server, $collation_connection, $message, $show_query, $db, $table, $err_url;
  58. if ($this->response->isAjax() && ! empty($_REQUEST['access_time'])) {
  59. return;
  60. }
  61. // This is for $cfg['ShowDatabasesNavigationAsTree'] = false;
  62. // See: https://github.com/phpmyadmin/phpmyadmin/issues/16520
  63. // The DB is defined here and sent to the JS front-end to refresh the DB tree
  64. $db = $_POST['db'] ?? '';
  65. $table = '';
  66. $show_query = '1';
  67. $err_url = Url::getFromRoute('/');
  68. if ($server > 0 && $this->dbi->isSuperUser()) {
  69. $this->dbi->selectDb('mysql');
  70. }
  71. $languageManager = LanguageManager::getInstance();
  72. if (! empty($message)) {
  73. $displayMessage = Generator::getMessage($message);
  74. unset($message);
  75. }
  76. if (isset($_SESSION['partial_logout'])) {
  77. $partialLogout = Message::success(__(
  78. 'You were logged out from one server, to logout completely '
  79. . 'from phpMyAdmin, you need to logout from all servers.'
  80. ))->getDisplay();
  81. unset($_SESSION['partial_logout']);
  82. }
  83. $syncFavoriteTables = RecentFavoriteTable::getInstance('favorite')
  84. ->getHtmlSyncFavoriteTables();
  85. $hasServer = $server > 0 || count($cfg['Servers']) > 1;
  86. if ($hasServer) {
  87. $hasServerSelection = $cfg['ServerDefault'] == 0
  88. || (! $cfg['NavigationDisplayServers']
  89. && (count($cfg['Servers']) > 1
  90. || ($server == 0 && count($cfg['Servers']) === 1)));
  91. if ($hasServerSelection) {
  92. $serverSelection = Select::render(true, true);
  93. }
  94. if ($server > 0) {
  95. $checkUserPrivileges = new CheckUserPrivileges($this->dbi);
  96. $checkUserPrivileges->getPrivileges();
  97. $charsets = Charsets::getCharsets($this->dbi, $cfg['Server']['DisableIS']);
  98. $collations = Charsets::getCollations($this->dbi, $cfg['Server']['DisableIS']);
  99. $charsetsList = [];
  100. /** @var Charset $charset */
  101. foreach ($charsets as $charset) {
  102. $collationsList = [];
  103. /** @var Collation $collation */
  104. foreach ($collations[$charset->getName()] as $collation) {
  105. $collationsList[] = [
  106. 'name' => $collation->getName(),
  107. 'description' => $collation->getDescription(),
  108. 'is_selected' => $collation_connection === $collation->getName(),
  109. ];
  110. }
  111. $charsetsList[] = [
  112. 'name' => $charset->getName(),
  113. 'description' => $charset->getDescription(),
  114. 'collations' => $collationsList,
  115. ];
  116. }
  117. }
  118. }
  119. $languageSelector = '';
  120. if (empty($cfg['Lang']) && $languageManager->hasChoice()) {
  121. $languageSelector = $languageManager->getSelectorDisplay($this->template);
  122. }
  123. $themeSelection = '';
  124. if ($cfg['ThemeManager']) {
  125. $themeSelection = $this->themeManager->getHtmlSelectBox();
  126. }
  127. $databaseServer = [];
  128. if ($server > 0 && $cfg['ShowServerInfo']) {
  129. $hostInfo = '';
  130. if (! empty($cfg['Server']['verbose'])) {
  131. $hostInfo .= $cfg['Server']['verbose'];
  132. if ($cfg['ShowServerInfo']) {
  133. $hostInfo .= ' (';
  134. }
  135. }
  136. if ($cfg['ShowServerInfo'] || empty($cfg['Server']['verbose'])) {
  137. $hostInfo .= $this->dbi->getHostInfo();
  138. }
  139. if (! empty($cfg['Server']['verbose']) && $cfg['ShowServerInfo']) {
  140. $hostInfo .= ')';
  141. }
  142. $serverCharset = Charsets::getServerCharset($this->dbi, $cfg['Server']['DisableIS']);
  143. $databaseServer = [
  144. 'host' => $hostInfo,
  145. 'type' => Util::getServerType(),
  146. 'connection' => Generator::getServerSSL(),
  147. 'version' => $this->dbi->getVersionString() . ' - ' . $this->dbi->getVersionComment(),
  148. 'protocol' => $this->dbi->getProtoInfo(),
  149. 'user' => $this->dbi->fetchValue('SELECT USER();'),
  150. 'charset' => $serverCharset->getDescription() . ' (' . $serverCharset->getName() . ')',
  151. ];
  152. }
  153. $webServer = [];
  154. if ($cfg['ShowServerInfo']) {
  155. $webServer['software'] = $_SERVER['SERVER_SOFTWARE'] ?? null;
  156. if ($server > 0) {
  157. $clientVersion = $this->dbi->getClientInfo();
  158. if (preg_match('#\d+\.\d+\.\d+#', $clientVersion)) {
  159. $clientVersion = 'libmysql - ' . $clientVersion;
  160. }
  161. $webServer['database'] = $clientVersion;
  162. $webServer['php_extensions'] = Util::listPHPExtensions();
  163. $webServer['php_version'] = PHP_VERSION;
  164. }
  165. }
  166. $relation = new Relation($this->dbi);
  167. if ($server > 0) {
  168. $cfgRelation = $relation->getRelationsParam();
  169. if (! $cfgRelation['allworks']
  170. && $cfg['PmaNoRelation_DisableWarning'] == false
  171. ) {
  172. $messageText = __(
  173. 'The phpMyAdmin configuration storage is not completely '
  174. . 'configured, some extended features have been deactivated. '
  175. . '%sFind out why%s. '
  176. );
  177. if ($cfg['ZeroConf'] == true) {
  178. $messageText .= '<br>' .
  179. __(
  180. 'Or alternately go to \'Operations\' tab of any database '
  181. . 'to set it up there.'
  182. );
  183. }
  184. $messageInstance = Message::notice($messageText);
  185. $messageInstance->addParamHtml(
  186. '<a href="' . Url::getFromRoute('/check-relations')
  187. . '" data-post="' . Url::getCommon() . '">'
  188. );
  189. $messageInstance->addParamHtml('</a>');
  190. /* Show error if user has configured something, notice elsewhere */
  191. if (! empty($cfg['Servers'][$server]['pmadb'])) {
  192. $messageInstance->isError(true);
  193. }
  194. $configStorageMessage = $messageInstance->getDisplay();
  195. }
  196. }
  197. $this->checkRequirements();
  198. $git = new Git($this->config);
  199. $this->render('home/index', [
  200. 'message' => $displayMessage ?? '',
  201. 'partial_logout' => $partialLogout ?? '',
  202. 'is_git_revision' => $git->isGitRevision(),
  203. 'server' => $server,
  204. 'sync_favorite_tables' => $syncFavoriteTables,
  205. 'has_server' => $hasServer,
  206. 'is_demo' => $cfg['DBG']['demo'],
  207. 'has_server_selection' => $hasServerSelection ?? false,
  208. 'server_selection' => $serverSelection ?? '',
  209. 'has_change_password_link' => $cfg['Server']['auth_type'] !== 'config' && $cfg['ShowChgPassword'],
  210. 'charsets' => $charsetsList ?? [],
  211. 'language_selector' => $languageSelector,
  212. 'theme_selection' => $themeSelection,
  213. 'database_server' => $databaseServer,
  214. 'web_server' => $webServer,
  215. 'show_php_info' => $cfg['ShowPhpInfo'],
  216. 'is_version_checked' => $cfg['VersionCheck'],
  217. 'phpmyadmin_version' => PMA_VERSION,
  218. 'config_storage_message' => $configStorageMessage ?? '',
  219. ]);
  220. }
  221. public function setTheme(): void
  222. {
  223. $this->themeManager->setActiveTheme($_POST['set_theme']);
  224. $this->themeManager->setThemeCookie();
  225. $userPreferences = new UserPreferences();
  226. $preferences = $userPreferences->load();
  227. $preferences['config_data']['ThemeDefault'] = $_POST['set_theme'];
  228. $userPreferences->save($preferences['config_data']);
  229. $this->response->header('Location: index.php?route=/' . Url::getCommonRaw([], '&'));
  230. }
  231. public function setCollationConnection(): void
  232. {
  233. $this->config->setUserValue(
  234. null,
  235. 'DefaultConnectionCollation',
  236. $_POST['collation_connection'],
  237. 'utf8mb4_unicode_ci'
  238. );
  239. $this->response->header('Location: index.php?route=/' . Url::getCommonRaw([], '&'));
  240. }
  241. public function reloadRecentTablesList(): void
  242. {
  243. if (! $this->response->isAjax()) {
  244. return;
  245. }
  246. $this->response->addJSON([
  247. 'list' => RecentFavoriteTable::getInstance('recent')->getHtmlList(),
  248. ]);
  249. }
  250. public function gitRevision(): void
  251. {
  252. if (! $this->response->isAjax()) {
  253. return;
  254. }
  255. $git = new Git($this->config);
  256. if (! $git->isGitRevision()) {
  257. return;
  258. }
  259. $commit = $git->checkGitRevision();
  260. if (! $this->config->get('PMA_VERSION_GIT') || $commit === null) {
  261. $this->response->setRequestStatus(false);
  262. return;
  263. }
  264. $commit['author']['date'] = Util::localisedDate(strtotime($commit['author']['date']));
  265. $commit['committer']['date'] = Util::localisedDate(strtotime($commit['committer']['date']));
  266. $this->render('home/git_info', $commit);
  267. }
  268. private function checkRequirements(): void
  269. {
  270. global $cfg, $server, $lang;
  271. /**
  272. * mbstring is used for handling multibytes inside parser, so it is good
  273. * to tell user something might be broken without it, see bug #1063149.
  274. */
  275. if (! extension_loaded('mbstring')) {
  276. trigger_error(
  277. __(
  278. 'The mbstring PHP extension was not found and you seem to be using'
  279. . ' a multibyte charset. Without the mbstring extension phpMyAdmin'
  280. . ' is unable to split strings correctly and it may result in'
  281. . ' unexpected results.'
  282. ),
  283. E_USER_WARNING
  284. );
  285. }
  286. /**
  287. * Missing functionality
  288. */
  289. if (! extension_loaded('curl') && ! ini_get('allow_url_fopen')) {
  290. trigger_error(
  291. __(
  292. 'The curl extension was not found and allow_url_fopen is '
  293. . 'disabled. Due to this some features such as error reporting '
  294. . 'or version check are disabled.'
  295. )
  296. );
  297. }
  298. if ($cfg['LoginCookieValidityDisableWarning'] == false) {
  299. /**
  300. * Check whether session.gc_maxlifetime limits session validity.
  301. */
  302. $gc_time = (int) ini_get('session.gc_maxlifetime');
  303. if ($gc_time < $cfg['LoginCookieValidity']) {
  304. trigger_error(
  305. __(
  306. 'Your PHP parameter [a@https://www.php.net/manual/en/session.' .
  307. 'configuration.php#ini.session.gc-maxlifetime@_blank]session.' .
  308. 'gc_maxlifetime[/a] is lower than cookie validity configured ' .
  309. 'in phpMyAdmin, because of this, your login might expire sooner ' .
  310. 'than configured in phpMyAdmin.'
  311. ),
  312. E_USER_WARNING
  313. );
  314. }
  315. }
  316. /**
  317. * Check whether LoginCookieValidity is limited by LoginCookieStore.
  318. */
  319. if ($cfg['LoginCookieStore'] != 0
  320. && $cfg['LoginCookieStore'] < $cfg['LoginCookieValidity']
  321. ) {
  322. trigger_error(
  323. __(
  324. 'Login cookie store is lower than cookie validity configured in ' .
  325. 'phpMyAdmin, because of this, your login will expire sooner than ' .
  326. 'configured in phpMyAdmin.'
  327. ),
  328. E_USER_WARNING
  329. );
  330. }
  331. /**
  332. * Warning if using the default MySQL controluser account
  333. */
  334. if (isset($cfg['Server']['controluser'], $cfg['Server']['controlpass'])
  335. && $server != 0
  336. && $cfg['Server']['controluser'] === 'pma'
  337. && $cfg['Server']['controlpass'] === 'pmapass'
  338. ) {
  339. trigger_error(
  340. __(
  341. 'Your server is running with default values for the ' .
  342. 'controluser and password (controlpass) and is open to ' .
  343. 'intrusion; you really should fix this security weakness' .
  344. ' by changing the password for controluser \'pma\'.'
  345. ),
  346. E_USER_WARNING
  347. );
  348. }
  349. /**
  350. * Check if user does not have defined blowfish secret and it is being used.
  351. */
  352. if (! empty($_SESSION['encryption_key'])) {
  353. if (empty($cfg['blowfish_secret'])) {
  354. trigger_error(
  355. __(
  356. 'The configuration file now needs a secret passphrase (blowfish_secret).'
  357. ),
  358. E_USER_WARNING
  359. );
  360. } elseif (strlen($cfg['blowfish_secret']) < 32) {
  361. trigger_error(
  362. __(
  363. 'The secret passphrase in configuration (blowfish_secret) is too short.'
  364. ),
  365. E_USER_WARNING
  366. );
  367. }
  368. }
  369. /**
  370. * Check for existence of config directory which should not exist in
  371. * production environment.
  372. */
  373. if (@file_exists(ROOT_PATH . 'config')) {
  374. trigger_error(
  375. __(
  376. 'Directory [code]config[/code], which is used by the setup script, ' .
  377. 'still exists in your phpMyAdmin directory. It is strongly ' .
  378. 'recommended to remove it once phpMyAdmin has been configured. ' .
  379. 'Otherwise the security of your server may be compromised by ' .
  380. 'unauthorized people downloading your configuration.'
  381. ),
  382. E_USER_WARNING
  383. );
  384. }
  385. /**
  386. * Warning about Suhosin only if its simulation mode is not enabled
  387. */
  388. if ($cfg['SuhosinDisableWarning'] == false
  389. && ini_get('suhosin.request.max_value_length')
  390. && ini_get('suhosin.simulation') == '0'
  391. ) {
  392. trigger_error(
  393. sprintf(
  394. __(
  395. 'Server running with Suhosin. Please refer ' .
  396. 'to %sdocumentation%s for possible issues.'
  397. ),
  398. '[doc@faq1-38]',
  399. '[/doc]'
  400. ),
  401. E_USER_WARNING
  402. );
  403. }
  404. /* Missing template cache */
  405. if ($this->config->getTempDir('twig') === null) {
  406. trigger_error(
  407. sprintf(
  408. __(
  409. 'The $cfg[\'TempDir\'] (%s) is not accessible. ' .
  410. 'phpMyAdmin is not able to cache templates and will ' .
  411. 'be slow because of this.'
  412. ),
  413. $this->config->get('TempDir')
  414. ),
  415. E_USER_WARNING
  416. );
  417. }
  418. /**
  419. * Warning about incomplete translations.
  420. *
  421. * The data file is created while creating release by ./scripts/remove-incomplete-mo
  422. */
  423. if (! @file_exists(ROOT_PATH . 'libraries/language_stats.inc.php')) {
  424. return;
  425. }
  426. include ROOT_PATH . 'libraries/language_stats.inc.php';
  427. /*
  428. * This message is intentionally not translated, because we're
  429. * handling incomplete translations here and focus on english
  430. * speaking users.
  431. */
  432. if (! isset($GLOBALS['language_stats'][$lang])
  433. || $GLOBALS['language_stats'][$lang] >= $cfg['TranslationWarningThreshold']
  434. ) {
  435. return;
  436. }
  437. trigger_error(
  438. 'You are using an incomplete translation, please help to make it '
  439. . 'better by [a@https://www.phpmyadmin.net/translate/'
  440. . '@_blank]contributing[/a].',
  441. E_USER_NOTICE
  442. );
  443. }
  444. }