PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/classes/Controllers/SqlController.php

http://github.com/phpmyadmin/phpmyadmin
PHP | 324 lines | 228 code | 43 blank | 53 comment | 33 complexity | 9ba0707262f09b5c3d1088eec134b73b 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\CheckUserPrivileges;
  5. use PhpMyAdmin\Config\PageSettings;
  6. use PhpMyAdmin\Core;
  7. use PhpMyAdmin\DatabaseInterface;
  8. use PhpMyAdmin\Html\Generator;
  9. use PhpMyAdmin\Message;
  10. use PhpMyAdmin\ParseAnalyze;
  11. use PhpMyAdmin\Response;
  12. use PhpMyAdmin\Sql;
  13. use PhpMyAdmin\Template;
  14. use PhpMyAdmin\Url;
  15. use PhpMyAdmin\Util;
  16. use function mb_strpos;
  17. use function strlen;
  18. use function strpos;
  19. use function urlencode;
  20. class SqlController extends AbstractController
  21. {
  22. /** @var Sql */
  23. private $sql;
  24. /** @var CheckUserPrivileges */
  25. private $checkUserPrivileges;
  26. /**
  27. * @param Response $response A Response instance.
  28. * @param DatabaseInterface $dbi A DatabaseInterface instance.
  29. * @param Template $template A Template instance.
  30. * @param Sql $sql An Sql instance.
  31. * @param CheckUserPrivileges $checkUserPrivileges A CheckUserPrivileges instance.
  32. */
  33. public function __construct(
  34. $response,
  35. $dbi,
  36. Template $template,
  37. Sql $sql,
  38. CheckUserPrivileges $checkUserPrivileges
  39. ) {
  40. parent::__construct($response, $dbi, $template);
  41. $this->sql = $sql;
  42. $this->checkUserPrivileges = $checkUserPrivileges;
  43. }
  44. public function index(): void
  45. {
  46. global $cfg, $db, $display_query, $pmaThemeImage, $sql_query, $table, $message;
  47. global $ajax_reload, $goto, $err_url, $find_real_end, $unlim_num_rows, $import_text, $disp_query;
  48. global $extra_data, $message_to_show, $sql_data, $disp_message, $query_type, $selected, $complete_query;
  49. global $is_gotofile, $back, $table_from_sql;
  50. $this->checkUserPrivileges->getPrivileges();
  51. PageSettings::showGroup('Browse');
  52. $header = $this->response->getHeader();
  53. $scripts = $header->getScripts();
  54. $scripts->addFile('vendor/jquery/jquery.uitablefilter.js');
  55. $scripts->addFile('table/change.js');
  56. $scripts->addFile('indexes.js');
  57. $scripts->addFile('gis_data_editor.js');
  58. $scripts->addFile('multi_column_sort.js');
  59. /**
  60. * Set ajax_reload in the response if it was already set
  61. */
  62. if (isset($ajax_reload) && $ajax_reload['reload'] === true) {
  63. $this->response->addJSON('ajax_reload', $ajax_reload);
  64. }
  65. /**
  66. * Defines the url to return to in case of error in a sql statement
  67. */
  68. $is_gotofile = true;
  69. if (empty($goto)) {
  70. if (empty($table)) {
  71. $goto = Util::getScriptNameForOption(
  72. $cfg['DefaultTabDatabase'],
  73. 'database'
  74. );
  75. } else {
  76. $goto = Util::getScriptNameForOption(
  77. $cfg['DefaultTabTable'],
  78. 'table'
  79. );
  80. }
  81. }
  82. if (! isset($err_url)) {
  83. $err_url = ! empty($back) ? $back : $goto;
  84. $err_url .= Url::getCommon(
  85. ['db' => $GLOBALS['db']],
  86. strpos($err_url, '?') === false ? '?' : '&'
  87. );
  88. if ((mb_strpos(' ' . $err_url, 'db_') !== 1 || mb_strpos($err_url, '?route=/database/') === false)
  89. && strlen($table) > 0
  90. ) {
  91. $err_url .= '&amp;table=' . urlencode($table);
  92. }
  93. }
  94. // Coming from a bookmark dialog
  95. if (isset($_POST['bkm_fields']['bkm_sql_query'])) {
  96. $sql_query = $_POST['bkm_fields']['bkm_sql_query'];
  97. } elseif (isset($_POST['sql_query'])) {
  98. $sql_query = $_POST['sql_query'];
  99. } elseif (isset($_GET['sql_query'], $_GET['sql_signature'])) {
  100. if (Core::checkSqlQuerySignature($_GET['sql_query'], $_GET['sql_signature'])) {
  101. $sql_query = $_GET['sql_query'];
  102. }
  103. }
  104. // This one is just to fill $db
  105. if (isset($_POST['bkm_fields']['bkm_database'])) {
  106. $db = $_POST['bkm_fields']['bkm_database'];
  107. }
  108. // Default to browse if no query set and we have table
  109. // (needed for browsing from DefaultTabTable)
  110. if (empty($sql_query) && strlen($table) > 0 && strlen($db) > 0) {
  111. $sql_query = $this->sql->getDefaultSqlQueryForBrowse($db, $table);
  112. // set $goto to what will be displayed if query returns 0 rows
  113. $goto = '';
  114. } else {
  115. // Now we can check the parameters
  116. Util::checkParameters(['sql_query']);
  117. }
  118. /**
  119. * Parse and analyze the query
  120. */
  121. [
  122. $analyzed_sql_results,
  123. $db,
  124. $table_from_sql,
  125. ] = ParseAnalyze::sqlQuery($sql_query, $db);
  126. if ($table != $table_from_sql && ! empty($table_from_sql)) {
  127. $table = $table_from_sql;
  128. }
  129. /**
  130. * Check rights in case of DROP DATABASE
  131. *
  132. * This test may be bypassed if $is_js_confirmed = 1 (already checked with js)
  133. * but since a malicious user may pass this variable by url/form, we don't take
  134. * into account this case.
  135. */
  136. if ($this->sql->hasNoRightsToDropDatabase(
  137. $analyzed_sql_results,
  138. $cfg['AllowUserDropDatabase'],
  139. $this->dbi->isSuperuser()
  140. )) {
  141. Generator::mysqlDie(
  142. __('"DROP DATABASE" statements are disabled.'),
  143. '',
  144. false,
  145. $err_url
  146. );
  147. }
  148. /**
  149. * Need to find the real end of rows?
  150. */
  151. if (isset($find_real_end) && $find_real_end) {
  152. $unlim_num_rows = $this->sql->findRealEndOfRows($db, $table);
  153. }
  154. /**
  155. * Bookmark add
  156. */
  157. if (isset($_POST['store_bkm'])) {
  158. $this->sql->addBookmark($goto);
  159. return;
  160. }
  161. /**
  162. * Sets or modifies the $goto variable if required
  163. */
  164. if ($goto === Url::getFromRoute('/sql')) {
  165. $is_gotofile = false;
  166. $goto = Url::getFromRoute('/sql', [
  167. 'db' => $db,
  168. 'table' => $table,
  169. 'sql_query' => $sql_query,
  170. ]);
  171. }
  172. $this->sql->executeQueryAndSendQueryResponse(
  173. $analyzed_sql_results,
  174. $is_gotofile,
  175. $db,
  176. $table,
  177. $find_real_end ?? null,
  178. $import_text ?? null,
  179. $extra_data ?? null,
  180. $message_to_show ?? null,
  181. $message ?? null,
  182. $sql_data ?? null,
  183. $goto,
  184. $pmaThemeImage,
  185. isset($disp_query) ? $display_query : null,
  186. $disp_message ?? null,
  187. $query_type ?? null,
  188. $sql_query,
  189. $selected ?? null,
  190. $complete_query ?? null
  191. );
  192. }
  193. /**
  194. * Get values for the relational columns
  195. *
  196. * During grid edit, if we have a relational field, show the dropdown for it.
  197. */
  198. public function getRelationalValues(): void
  199. {
  200. global $db, $table;
  201. $this->checkUserPrivileges->getPrivileges();
  202. $column = $_POST['column'];
  203. if ($_SESSION['tmpval']['relational_display'] == 'D'
  204. && isset($_POST['relation_key_or_display_column'])
  205. && $_POST['relation_key_or_display_column']
  206. ) {
  207. $curr_value = $_POST['relation_key_or_display_column'];
  208. } else {
  209. $curr_value = $_POST['curr_value'];
  210. }
  211. $dropdown = $this->sql->getHtmlForRelationalColumnDropdown(
  212. $db,
  213. $table,
  214. $column,
  215. $curr_value
  216. );
  217. $this->response->addJSON('dropdown', $dropdown);
  218. }
  219. /**
  220. * Get possible values for enum fields during grid edit.
  221. */
  222. public function getEnumValues(): void
  223. {
  224. global $db, $table;
  225. $this->checkUserPrivileges->getPrivileges();
  226. $column = $_POST['column'];
  227. $curr_value = $_POST['curr_value'];
  228. $values = $this->sql->getValuesForColumn($db, $table, $column);
  229. $dropdown = $this->template->render('sql/enum_column_dropdown', [
  230. 'values' => $values,
  231. 'selected_values' => [$curr_value],
  232. ]);
  233. $this->response->addJSON('dropdown', $dropdown);
  234. }
  235. /**
  236. * Get possible values for SET fields during grid edit.
  237. */
  238. public function getSetValues(): void
  239. {
  240. global $db, $table;
  241. $this->checkUserPrivileges->getPrivileges();
  242. $column = $_POST['column'];
  243. $curr_value = $_POST['curr_value'];
  244. $select = $this->sql->getHtmlForSetColumn(
  245. $db,
  246. $table,
  247. $column,
  248. $curr_value
  249. );
  250. $this->response->addJSON('select', $select);
  251. }
  252. public function getDefaultForeignKeyCheckValue(): void
  253. {
  254. $this->checkUserPrivileges->getPrivileges();
  255. $this->response->addJSON(
  256. 'default_fk_check_value',
  257. Util::isForeignKeyCheck()
  258. );
  259. }
  260. public function setColumnOrderOrVisibility(): void
  261. {
  262. global $db, $table;
  263. $this->checkUserPrivileges->getPrivileges();
  264. $tableObject = $this->dbi->getTable($db, $table);
  265. $status = false;
  266. // set column order
  267. if (isset($_POST['col_order'])) {
  268. $status = $this->sql->setColumnProperty($tableObject, 'col_order');
  269. }
  270. // set column visibility
  271. if ($status === true && isset($_POST['col_visib'])) {
  272. $status = $this->sql->setColumnProperty($tableObject, 'col_visib');
  273. }
  274. if ($status instanceof Message) {
  275. $this->response->setRequestStatus(false);
  276. $this->response->addJSON('message', $status->getString());
  277. return;
  278. }
  279. $this->response->setRequestStatus($status === true);
  280. }
  281. }