SqlQueryForm.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * functions for displaying the sql query form
  4. *
  5. * @usedby /server/sql
  6. * @usedby /database/sql
  7. * @usedby /table/sql
  8. * @usedby /table/structure
  9. * @usedby /table/tracking
  10. */
  11. declare(strict_types=1);
  12. namespace PhpMyAdmin;
  13. use PhpMyAdmin\Html\MySQLDocumentation;
  14. use function htmlspecialchars;
  15. use function sprintf;
  16. use function strlen;
  17. use function strpos;
  18. /**
  19. * PhpMyAdmin\SqlQueryForm class
  20. */
  21. class SqlQueryForm
  22. {
  23. /** @var Template */
  24. private $template;
  25. /**
  26. * @param Template $template Template object
  27. */
  28. public function __construct(Template $template)
  29. {
  30. $this->template = $template;
  31. }
  32. /**
  33. * return HTML for the sql query boxes
  34. *
  35. * @param bool|string $query query to display in the textarea
  36. * or true to display last executed
  37. * @param bool|string $display_tab sql|full|false
  38. * what part to display
  39. * false if not inside querywindow
  40. * @param string $delimiter delimiter
  41. *
  42. * @return string
  43. *
  44. * @usedby /server/sql
  45. * @usedby /database/sql
  46. * @usedby /table/sql
  47. * @usedby /table/structure
  48. * @usedby /table/tracking
  49. */
  50. public function getHtml(
  51. $query = true,
  52. $display_tab = false,
  53. $delimiter = ';'
  54. ) {
  55. global $dbi;
  56. if (! $display_tab) {
  57. $display_tab = 'full';
  58. }
  59. // query to show
  60. if ($query === true) {
  61. $query = $GLOBALS['sql_query'];
  62. if (empty($query) && (isset($_GET['show_query']) || isset($_POST['show_query']))) {
  63. $query = $_GET['sql_query'] ?? $_POST['sql_query'] ?? '';
  64. }
  65. }
  66. $table = '';
  67. $db = '';
  68. if (strlen($GLOBALS['db']) === 0) {
  69. // prepare for server related
  70. $goto = empty($GLOBALS['goto']) ? Url::getFromRoute('/server/sql') : $GLOBALS['goto'];
  71. } elseif (strlen($GLOBALS['table']) === 0) {
  72. // prepare for db related
  73. $db = $GLOBALS['db'];
  74. $goto = empty($GLOBALS['goto']) ? Url::getFromRoute('/database/sql') : $GLOBALS['goto'];
  75. } else {
  76. $table = $GLOBALS['table'];
  77. $db = $GLOBALS['db'];
  78. $goto = empty($GLOBALS['goto']) ? Url::getFromRoute('/table/sql') : $GLOBALS['goto'];
  79. }
  80. if ($display_tab === 'full' || $display_tab === 'sql') {
  81. [$legend, $query, $columns_list] = $this->init($query);
  82. }
  83. $cfgBookmark = Bookmark::getParams($GLOBALS['cfg']['Server']['user']);
  84. $bookmarks = [];
  85. if ($display_tab === 'full') {
  86. if ($cfgBookmark) {
  87. $bookmark_list = Bookmark::getList(
  88. $dbi,
  89. $GLOBALS['cfg']['Server']['user'],
  90. $GLOBALS['db']
  91. );
  92. foreach ($bookmark_list as $bookmarkItem) {
  93. $bookmarks[] = [
  94. 'id' => $bookmarkItem->getId(),
  95. 'variable_count' => $bookmarkItem->getVariableCount(),
  96. 'label' => $bookmarkItem->getLabel(),
  97. 'is_shared' => empty($bookmarkItem->getUser()),
  98. ];
  99. }
  100. }
  101. }
  102. return $this->template->render('sql/query', [
  103. 'legend' => $legend ?? '',
  104. 'textarea_cols' => $GLOBALS['cfg']['TextareaCols'],
  105. 'textarea_rows' => $GLOBALS['cfg']['TextareaRows'],
  106. 'textarea_auto_select' => $GLOBALS['cfg']['TextareaAutoSelect'],
  107. 'columns_list' => $columns_list ?? [],
  108. 'codemirror_enable' => $GLOBALS['cfg']['CodemirrorEnable'],
  109. 'has_bookmark' => $cfgBookmark,
  110. 'delimiter' => $delimiter,
  111. 'retain_query_box' => $GLOBALS['cfg']['RetainQueryBox'] !== false,
  112. 'is_upload' => $GLOBALS['is_upload'],
  113. 'db' => $db,
  114. 'table' => $table,
  115. 'goto' => $goto,
  116. 'query' => $query,
  117. 'display_tab' => $display_tab,
  118. 'bookmarks' => $bookmarks,
  119. 'can_convert_kanji' => Encoding::canConvertKanji(),
  120. 'is_foreign_key_check' => Util::isForeignKeyCheck(),
  121. ]);
  122. }
  123. /**
  124. * Get initial values for Sql Query Form Insert
  125. *
  126. * @param string $query query to display in the textarea
  127. *
  128. * @return array ($legend, $query, $columns_list)
  129. */
  130. public function init($query)
  131. {
  132. global $dbi;
  133. $columns_list = [];
  134. if (strlen($GLOBALS['db']) === 0) {
  135. // prepare for server related
  136. $legend = sprintf(
  137. __('Run SQL query/queries on server “%s”'),
  138. htmlspecialchars(
  139. ! empty($GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose'])
  140. ? $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose']
  141. : $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['host']
  142. )
  143. );
  144. } elseif (strlen($GLOBALS['table']) === 0) {
  145. // prepare for db related
  146. $db = $GLOBALS['db'];
  147. // if you want navigation:
  148. $scriptName = Util::getScriptNameForOption(
  149. $GLOBALS['cfg']['DefaultTabDatabase'],
  150. 'database'
  151. );
  152. $tmp_db_link = '<a href="' . $scriptName
  153. . Url::getCommon(['db' => $db], strpos($scriptName, '?') === false ? '?' : '&')
  154. . '">';
  155. $tmp_db_link .= htmlspecialchars($db) . '</a>';
  156. $legend = sprintf(__('Run SQL query/queries on database %s'), $tmp_db_link);
  157. if (empty($query)) {
  158. $query = Util::expandUserString(
  159. $GLOBALS['cfg']['DefaultQueryDatabase'],
  160. 'backquote'
  161. );
  162. }
  163. } else {
  164. $db = $GLOBALS['db'];
  165. $table = $GLOBALS['table'];
  166. // Get the list and number of fields
  167. // we do a try_query here, because we could be in the query window,
  168. // trying to synchronize and the table has not yet been created
  169. $columns_list = $dbi->getColumns(
  170. $db,
  171. $GLOBALS['table'],
  172. null,
  173. true
  174. );
  175. $scriptName = Util::getScriptNameForOption(
  176. $GLOBALS['cfg']['DefaultTabTable'],
  177. 'table'
  178. );
  179. $tmp_tbl_link = '<a href="' . $scriptName . Url::getCommon(['db' => $db, 'table' => $table], '&') . '">';
  180. $tmp_tbl_link .= htmlspecialchars($db) . '.' . htmlspecialchars($table) . '</a>';
  181. $legend = sprintf(__('Run SQL query/queries on table %s'), $tmp_tbl_link);
  182. if (empty($query)) {
  183. $query = Util::expandUserString(
  184. $GLOBALS['cfg']['DefaultQueryTable'],
  185. 'backquote'
  186. );
  187. }
  188. }
  189. $legend .= ': ' . MySQLDocumentation::show('SELECT');
  190. return [
  191. $legend,
  192. $query,
  193. $columns_list,
  194. ];
  195. }
  196. }