PageRenderTime 51ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/classes/Controllers/HomeController.php

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