db_common.inc.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Common includes for the database level views
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. use PhpMyAdmin\Core;
  9. use PhpMyAdmin\Message;
  10. use PhpMyAdmin\Response;
  11. use PhpMyAdmin\Url;
  12. use PhpMyAdmin\Util;
  13. use PhpMyAdmin\Operations;
  14. if (! defined('PHPMYADMIN')) {
  15. exit;
  16. }
  17. PhpMyAdmin\Util::checkParameters(array('db'));
  18. global $cfg;
  19. global $db;
  20. $response = Response::getInstance();
  21. $is_show_stats = $cfg['ShowStats'];
  22. $db_is_system_schema = $GLOBALS['dbi']->isSystemSchema($db);
  23. if ($db_is_system_schema) {
  24. $is_show_stats = false;
  25. }
  26. /**
  27. * Defines the urls to return to in case of error in a sql statement
  28. */
  29. $err_url_0 = 'index.php' . Url::getCommon();
  30. $err_url = PhpMyAdmin\Util::getScriptNameForOption(
  31. $GLOBALS['cfg']['DefaultTabDatabase'], 'database'
  32. )
  33. . Url::getCommon(array('db' => $db));
  34. /**
  35. * Ensures the database exists (else move to the "parent" script) and displays
  36. * headers
  37. */
  38. if (! isset($is_db) || ! $is_db) {
  39. if (strlen($db) > 0) {
  40. $is_db = $GLOBALS['dbi']->selectDb($db);
  41. // This "Command out of sync" 2014 error may happen, for example
  42. // after calling a MySQL procedure; at this point we can't select
  43. // the db but it's not necessarily wrong
  44. if ($GLOBALS['dbi']->getError() && $GLOBALS['errno'] == 2014) {
  45. $is_db = true;
  46. unset($GLOBALS['errno']);
  47. }
  48. } else {
  49. $is_db = false;
  50. }
  51. // Not a valid db name -> back to the welcome page
  52. $params = array('reload' => '1');
  53. if (isset($message)) {
  54. $params['message'] = $message;
  55. }
  56. $uri = './index.php' . Url::getCommonRaw($params);
  57. if (strlen($db) === 0 || ! $is_db) {
  58. $response = Response::getInstance();
  59. if ($response->isAjax()) {
  60. $response->setRequestStatus(false);
  61. $response->addJSON(
  62. 'message',
  63. Message::error(__('No databases selected.'))
  64. );
  65. } else {
  66. Core::sendHeaderLocation($uri);
  67. }
  68. exit;
  69. }
  70. } // end if (ensures db exists)
  71. /**
  72. * Changes database charset if requested by the user
  73. */
  74. if (isset($_POST['submitcollation'])
  75. && isset($_POST['db_collation'])
  76. && ! empty($_POST['db_collation'])
  77. ) {
  78. list($db_charset) = explode('_', $_POST['db_collation']);
  79. $sql_query = 'ALTER DATABASE '
  80. . PhpMyAdmin\Util::backquote($db)
  81. . ' DEFAULT' . Util::getCharsetQueryPart($_POST['db_collation']);
  82. $result = $GLOBALS['dbi']->query($sql_query);
  83. $message = Message::success();
  84. /**
  85. * Changes tables charset if requested by the user
  86. */
  87. if (
  88. isset($_POST['change_all_tables_collations']) &&
  89. $_POST['change_all_tables_collations'] === 'on'
  90. ) {
  91. list($tables, , , , , , , ,) = PhpMyAdmin\Util::getDbInfo($db, null);
  92. foreach($tables as $tableName => $data) {
  93. if ($GLOBALS['dbi']->getTable($db, $tableName)->isView()) {
  94. // Skip views, we can not change the collation of a view.
  95. // issue #15283
  96. continue;
  97. }
  98. $sql_query = 'ALTER TABLE '
  99. . PhpMyAdmin\Util::backquote($db)
  100. . '.'
  101. . PhpMyAdmin\Util::backquote($tableName)
  102. . ' DEFAULT '
  103. . Util::getCharsetQueryPart($_POST['db_collation']);
  104. $GLOBALS['dbi']->query($sql_query);
  105. /**
  106. * Changes columns charset if requested by the user
  107. */
  108. if (
  109. isset($_POST['change_all_tables_columns_collations']) &&
  110. $_POST['change_all_tables_columns_collations'] === 'on'
  111. ) {
  112. $operations = new Operations();
  113. $operations->changeAllColumnsCollation($db, $tableName, $_POST['db_collation']);
  114. }
  115. }
  116. }
  117. unset($db_charset);
  118. /**
  119. * If we are in an Ajax request, let us stop the execution here. Necessary for
  120. * db charset change action on db_operations.php. If this causes a bug on
  121. * other pages, we might have to move this to a different location.
  122. */
  123. if ($response->isAjax()) {
  124. $response->setRequestStatus($message->isSuccess());
  125. $response->addJSON('message', $message);
  126. exit;
  127. }
  128. } elseif (isset($_POST['submitcollation'])
  129. && isset($_POST['db_collation'])
  130. && empty($_POST['db_collation'])
  131. ) {
  132. $response = Response::getInstance();
  133. if ($response->isAjax()) {
  134. $response->setRequestStatus(false);
  135. $response->addJSON(
  136. 'message',
  137. Message::error(__('No collation provided.'))
  138. );
  139. }
  140. }
  141. /**
  142. * Set parameters for links
  143. */
  144. $url_query = Url::getCommon(array('db' => $db));