SqlQueryForm.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * functions for displaying the sql query form
  5. *
  6. * @usedby server_sql.php
  7. * @usedby db_sql.php
  8. * @usedby tbl_sql.php
  9. * @usedby tbl_structure.php
  10. * @usedby tbl_tracking.php
  11. * @package PhpMyAdmin
  12. */
  13. namespace PhpMyAdmin;
  14. use PhpMyAdmin\Bookmark;
  15. use PhpMyAdmin\Encoding;
  16. use PhpMyAdmin\Url;
  17. use PhpMyAdmin\Util;
  18. /**
  19. * PhpMyAdmin\SqlQueryForm class
  20. *
  21. * @package PhpMyAdmin
  22. */
  23. class SqlQueryForm
  24. {
  25. /**
  26. * return HTML for the sql query boxes
  27. *
  28. * @param boolean|string $query query to display in the textarea
  29. * or true to display last executed
  30. * @param boolean|string $display_tab sql|full|false
  31. * what part to display
  32. * false if not inside querywindow
  33. * @param string $delimiter delimiter
  34. *
  35. * @return string
  36. *
  37. * @usedby server_sql.php
  38. * @usedby db_sql.php
  39. * @usedby tbl_sql.php
  40. * @usedby tbl_structure.php
  41. * @usedby tbl_tracking.php
  42. */
  43. public static function getHtml(
  44. $query = true, $display_tab = false, $delimiter = ';'
  45. ) {
  46. $html = '';
  47. if (! $display_tab) {
  48. $display_tab = 'full';
  49. }
  50. // query to show
  51. if (true === $query) {
  52. $query = $GLOBALS['sql_query'];
  53. }
  54. // set enctype to multipart for file uploads
  55. if ($GLOBALS['is_upload']) {
  56. $enctype = ' enctype="multipart/form-data"';
  57. } else {
  58. $enctype = '';
  59. }
  60. $table = '';
  61. $db = '';
  62. if (strlen($GLOBALS['db']) === 0) {
  63. // prepare for server related
  64. $goto = empty($GLOBALS['goto']) ?
  65. 'server_sql.php' : $GLOBALS['goto'];
  66. } elseif (strlen($GLOBALS['table']) === 0) {
  67. // prepare for db related
  68. $db = $GLOBALS['db'];
  69. $goto = empty($GLOBALS['goto']) ?
  70. 'db_sql.php' : $GLOBALS['goto'];
  71. } else {
  72. $table = $GLOBALS['table'];
  73. $db = $GLOBALS['db'];
  74. $goto = empty($GLOBALS['goto']) ?
  75. 'tbl_sql.php' : $GLOBALS['goto'];
  76. }
  77. // start output
  78. $html .= '<form method="post" action="import.php" ' . $enctype;
  79. $html .= ' class="ajax lock-page"';
  80. $html .= ' id="sqlqueryform" name="sqlform">' . "\n";
  81. $html .= '<input type="hidden" name="is_js_confirmed" value="0" />'
  82. . "\n" . Url::getHiddenInputs($db, $table) . "\n"
  83. . '<input type="hidden" name="pos" value="0" />' . "\n"
  84. . '<input type="hidden" name="goto" value="'
  85. . htmlspecialchars($goto) . '" />' . "\n"
  86. . '<input type="hidden" name="message_to_show" value="'
  87. . __('Your SQL query has been executed successfully.') . '" />'
  88. . "\n" . '<input type="hidden" name="prev_sql_query" value="'
  89. . htmlspecialchars($query) . '" />' . "\n";
  90. // display querybox
  91. if ($display_tab === 'full' || $display_tab === 'sql') {
  92. $html .= self::getHtmlForInsert(
  93. $query, $delimiter
  94. );
  95. }
  96. // Bookmark Support
  97. if ($display_tab === 'full') {
  98. $cfgBookmark = Bookmark::getParams($GLOBALS['cfg']['Server']['user']);
  99. if ($cfgBookmark) {
  100. $html .= self::getHtmlForBookmark();
  101. }
  102. }
  103. // Japanese encoding setting
  104. if (Encoding::canConvertKanji()) {
  105. $html .= Encoding::kanjiEncodingForm();
  106. }
  107. $html .= '</form>' . "\n";
  108. // print an empty div, which will be later filled with
  109. // the sql query results by ajax
  110. $html .= '<div id="sqlqueryresultsouter"></div>';
  111. return $html;
  112. }
  113. /**
  114. * Get initial values for Sql Query Form Insert
  115. *
  116. * @param string $query query to display in the textarea
  117. *
  118. * @return array ($legend, $query, $columns_list)
  119. *
  120. * @usedby self::getHtmlForInsert()
  121. */
  122. public static function init($query)
  123. {
  124. $columns_list = array();
  125. if (strlen($GLOBALS['db']) === 0) {
  126. // prepare for server related
  127. $legend = sprintf(
  128. __('Run SQL query/queries on server “%s”'),
  129. htmlspecialchars(
  130. ! empty($GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose'])
  131. ? $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose']
  132. : $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['host']
  133. )
  134. );
  135. } elseif (strlen($GLOBALS['table']) === 0) {
  136. // prepare for db related
  137. $db = $GLOBALS['db'];
  138. // if you want navigation:
  139. $tmp_db_link = '<a href="' . Util::getScriptNameForOption(
  140. $GLOBALS['cfg']['DefaultTabDatabase'], 'database'
  141. )
  142. . Url::getCommon(array('db' => $db)) . '"';
  143. $tmp_db_link .= '>'
  144. . htmlspecialchars($db) . '</a>';
  145. $legend = sprintf(__('Run SQL query/queries on database %s'), $tmp_db_link);
  146. if (empty($query)) {
  147. $query = Util::expandUserString(
  148. $GLOBALS['cfg']['DefaultQueryDatabase'], 'backquote'
  149. );
  150. }
  151. } else {
  152. $db = $GLOBALS['db'];
  153. $table = $GLOBALS['table'];
  154. // Get the list and number of fields
  155. // we do a try_query here, because we could be in the query window,
  156. // trying to synchronize and the table has not yet been created
  157. $columns_list = $GLOBALS['dbi']->getColumns(
  158. $db, $GLOBALS['table'], null, true
  159. );
  160. $tmp_tbl_link = '<a href="' . Util::getScriptNameForOption(
  161. $GLOBALS['cfg']['DefaultTabTable'], 'table'
  162. ) . Url::getCommon(array('db' => $db, 'table' => $table)) . '" >';
  163. $tmp_tbl_link .= htmlspecialchars($db)
  164. . '.' . htmlspecialchars($table) . '</a>';
  165. $legend = sprintf(__('Run SQL query/queries on table %s'), $tmp_tbl_link);
  166. if (empty($query)) {
  167. $query = Util::expandUserString(
  168. $GLOBALS['cfg']['DefaultQueryTable'], 'backquote'
  169. );
  170. }
  171. }
  172. $legend .= ': ' . Util::showMySQLDocu('SELECT');
  173. return array($legend, $query, $columns_list);
  174. }
  175. /**
  176. * return HTML for Sql Query Form Insert
  177. *
  178. * @param string $query query to display in the textarea
  179. * @param string $delimiter default delimiter to use
  180. *
  181. * @return string
  182. *
  183. * @usedby self::getHtml()
  184. */
  185. public static function getHtmlForInsert(
  186. $query = '', $delimiter = ';'
  187. ) {
  188. // enable auto select text in textarea
  189. if ($GLOBALS['cfg']['TextareaAutoSelect']) {
  190. $auto_sel = ' onclick="selectContent(this, sql_box_locked, true);"';
  191. } else {
  192. $auto_sel = '';
  193. }
  194. $locking = '';
  195. $height = $GLOBALS['cfg']['TextareaRows'] * 2;
  196. list($legend, $query, $columns_list) = self::init($query);
  197. if (! empty($columns_list)) {
  198. $sqlquerycontainer_id = 'sqlquerycontainer';
  199. } else {
  200. $sqlquerycontainer_id = 'sqlquerycontainerfull';
  201. }
  202. $html = '<a id="querybox"></a>'
  203. . '<div id="queryboxcontainer">'
  204. . '<fieldset id="queryboxf">';
  205. $html .= '<legend>' . $legend . '</legend>';
  206. $html .= '<div id="queryfieldscontainer">';
  207. $html .= '<div id="' . $sqlquerycontainer_id . '">'
  208. . '<textarea tabindex="100" name="sql_query" id="sqlquery"'
  209. . ' cols="' . $GLOBALS['cfg']['TextareaCols'] . '"'
  210. . ' rows="' . $height . '"'
  211. . $auto_sel . $locking . '>'
  212. . htmlspecialchars($query)
  213. . '</textarea>';
  214. $html .= '<div id="querymessage"></div>';
  215. // Add buttons to generate query easily for
  216. // select all, single select, insert, update and delete
  217. if (! empty($columns_list)) {
  218. $html .= '<input type="button" value="SELECT *" id="selectall"'
  219. . ' class="button sqlbutton" />';
  220. $html .= '<input type="button" value="SELECT" id="select"'
  221. . ' class="button sqlbutton" />';
  222. $html .= '<input type="button" value="INSERT" id="insert"'
  223. . ' class="button sqlbutton" />';
  224. $html .= '<input type="button" value="UPDATE" id="update"'
  225. . ' class="button sqlbutton" />';
  226. $html .= '<input type="button" value="DELETE" id="delete"'
  227. . ' class="button sqlbutton" />';
  228. }
  229. $html .= '<input type="button" value="' . __('Clear') . '" id="clear"'
  230. . ' class="button sqlbutton" />';
  231. if ($GLOBALS['cfg']['CodemirrorEnable']) {
  232. $html .= '<input type="button" value="' . __('Format') . '" id="format"'
  233. . ' class="button sqlbutton" />';
  234. }
  235. $html .= '<input type="button" value="' . __('Get auto-saved query')
  236. . '" id="saved" class="button sqlbutton" />';
  237. // parameter binding
  238. $html .= '<div>';
  239. $html .= '<input type="checkbox" name="parameterized" id="parameterized" />';
  240. $html .= '<label for="parameterized">' . __('Bind parameters') . '</label>';
  241. $html .= Util::showDocu('faq', 'faq6-40');
  242. $html .= '<div id="parametersDiv"></div>';
  243. $html .= '</div>';
  244. $html .= '</div>' . "\n";
  245. if (! empty($columns_list)) {
  246. $html .= '<div id="tablefieldscontainer">'
  247. . '<label>' . __('Columns') . '</label>'
  248. . '<select id="tablefields" name="dummy" '
  249. . 'size="' . ($GLOBALS['cfg']['TextareaRows'] - 2) . '" '
  250. . 'multiple="multiple" ondblclick="insertValueQuery()">';
  251. foreach ($columns_list as $field) {
  252. $html .= '<option value="'
  253. . Util::backquote(htmlspecialchars($field['Field']))
  254. . '"';
  255. if (isset($field['Field'])
  256. && strlen($field['Field']) > 0
  257. && isset($field['Comment'])
  258. ) {
  259. $html .= ' title="' . htmlspecialchars($field['Comment']) . '"';
  260. }
  261. $html .= '>' . htmlspecialchars($field['Field']) . '</option>' . "\n";
  262. }
  263. $html .= '</select>'
  264. . '<div id="tablefieldinsertbuttoncontainer">';
  265. if (Util::showIcons('ActionLinksMode')) {
  266. $html .= '<input type="button" class="button" name="insert"'
  267. . ' value="&lt;&lt;" onclick="insertValueQuery()"'
  268. . ' title="' . __('Insert') . '" />';
  269. } else {
  270. $html .= '<input type="button" class="button" name="insert"'
  271. . ' value="' . __('Insert') . '"'
  272. . ' onclick="insertValueQuery()" />';
  273. }
  274. $html .= '</div>' . "\n"
  275. . '</div>' . "\n";
  276. }
  277. $html .= '<div class="clearfloat"></div>' . "\n";
  278. $html .= '</div>' . "\n";
  279. $cfgBookmark = Bookmark::getParams($GLOBALS['cfg']['Server']['user']);
  280. if ($cfgBookmark) {
  281. $html .= '<div id="bookmarkoptions">';
  282. $html .= '<div class="formelement">';
  283. $html .= '<label for="bkm_label">'
  284. . __('Bookmark this SQL query:') . '</label>';
  285. $html .= '<input type="text" name="bkm_label" id="bkm_label"'
  286. . ' tabindex="110" value="" />';
  287. $html .= '</div>';
  288. $html .= '<div class="formelement">';
  289. $html .= '<input type="checkbox" name="bkm_all_users" tabindex="111"'
  290. . ' id="id_bkm_all_users" value="true" />';
  291. $html .= '<label for="id_bkm_all_users">'
  292. . __('Let every user access this bookmark') . '</label>';
  293. $html .= '</div>';
  294. $html .= '<div class="formelement">';
  295. $html .= '<input type="checkbox" name="bkm_replace" tabindex="112"'
  296. . ' id="id_bkm_replace" value="true" />';
  297. $html .= '<label for="id_bkm_replace">'
  298. . __('Replace existing bookmark of same name') . '</label>';
  299. $html .= '</div>';
  300. $html .= '</div>';
  301. }
  302. $html .= '<div class="clearfloat"></div>' . "\n";
  303. $html .= '</fieldset>' . "\n"
  304. . '</div>' . "\n";
  305. $html .= '<fieldset id="queryboxfooter" class="tblFooters">' . "\n";
  306. $html .= '<div class="formelement">' . "\n";
  307. $html .= '</div>' . "\n";
  308. $html .= '<div class="formelement">';
  309. $html .= '<label for="id_sql_delimiter">[ ' . __('Delimiter')
  310. . '</label>' . "\n";
  311. $html .= '<input type="text" name="sql_delimiter" tabindex="131" size="3" '
  312. . 'value="' . $delimiter . '" '
  313. . 'id="id_sql_delimiter" /> ]';
  314. $html .= '</div>';
  315. $html .= '<div class="formelement">';
  316. $html .= '<input type="checkbox" name="show_query" value="1" '
  317. . 'id="checkbox_show_query" tabindex="132" checked="checked" />'
  318. . '<label for="checkbox_show_query">' . __('Show this query here again')
  319. . '</label>';
  320. $html .= '</div>';
  321. $html .= '<div class="formelement">';
  322. $html .= '<input type="checkbox" name="retain_query_box" value="1" '
  323. . 'id="retain_query_box" tabindex="133" '
  324. . ($GLOBALS['cfg']['RetainQueryBox'] === false
  325. ? '' : ' checked="checked"')
  326. . ' />'
  327. . '<label for="retain_query_box">' . __('Retain query box')
  328. . '</label>';
  329. $html .= '</div>';
  330. $html .= '<div class="formelement">';
  331. $html .= '<input type="checkbox" name="rollback_query" value="1" '
  332. . 'id="rollback_query" tabindex="134" />'
  333. . '<label for="rollback_query">' . __('Rollback when finished')
  334. . '</label>';
  335. $html .= '</div>';
  336. // Disable/Enable foreign key checks
  337. $html .= '<div class="formelement">';
  338. $html .= Util::getFKCheckbox();
  339. $html .= '</div>';
  340. $html .= '<input type="submit" id="button_submit_query" name="SQL"';
  341. $html .= ' tabindex="200" value="' . __('Go') . '" />' . "\n";
  342. $html .= '<div class="clearfloat"></div>' . "\n";
  343. $html .= '</fieldset>' . "\n";
  344. return $html;
  345. }
  346. /**
  347. * return HTML for sql Query Form Bookmark
  348. *
  349. * @return string|null
  350. *
  351. * @usedby self::getHtml()
  352. */
  353. public static function getHtmlForBookmark()
  354. {
  355. $bookmark_list = Bookmark::getList(
  356. $GLOBALS['dbi'],
  357. $GLOBALS['cfg']['Server']['user'],
  358. $GLOBALS['db']
  359. );
  360. if (empty($bookmark_list) || count($bookmark_list) < 1) {
  361. return null;
  362. }
  363. $html = '<fieldset id="fieldsetBookmarkOptions">';
  364. $html .= '<legend>';
  365. $html .= __('Bookmarked SQL query') . '</legend>' . "\n";
  366. $html .= '<div class="formelement">';
  367. $html .= '<select name="id_bookmark" id="id_bookmark">' . "\n";
  368. $html .= '<option value="">&nbsp;</option>' . "\n";
  369. foreach ($bookmark_list as $bookmark) {
  370. $html .= '<option value="' . htmlspecialchars($bookmark->getId()) . '"'
  371. . ' data-varcount="' . $bookmark->getVariableCount()
  372. . '">'
  373. . htmlspecialchars($bookmark->getLabel())
  374. . (empty($bookmark->getUser()) ? (' (' . __('shared') . ')') : '')
  375. . '</option>' . "\n";
  376. }
  377. // &nbsp; is required for correct display with styles/line height
  378. $html .= '</select>&nbsp;' . "\n";
  379. $html .= '</div>' . "\n";
  380. $html .= '<div class="formelement">' . "\n";
  381. $html .= '<input type="radio" name="action_bookmark" value="0"'
  382. . ' id="radio_bookmark_exe" checked="checked" />'
  383. . '<label for="radio_bookmark_exe">' . __('Submit')
  384. . '</label>' . "\n";
  385. $html .= '<input type="radio" name="action_bookmark" value="1"'
  386. . ' id="radio_bookmark_view" />'
  387. . '<label for="radio_bookmark_view">' . __('View only')
  388. . '</label>' . "\n";
  389. $html .= '<input type="radio" name="action_bookmark" value="2"'
  390. . ' id="radio_bookmark_del" />'
  391. . '<label for="radio_bookmark_del">' . __('Delete')
  392. . '</label>' . "\n";
  393. $html .= '</div>' . "\n";
  394. $html .= '<div class="clearfloat"></div>' . "\n";
  395. $html .= '<div class="formelement hide">' . "\n";
  396. $html .= __('Variables');
  397. $html .= Util::showDocu('faq', 'faqbookmark');
  398. $html .= '<div id="bookmark_variables"></div>';
  399. $html .= '</div>' . "\n";
  400. $html .= '</fieldset>' . "\n";
  401. $html .= '<fieldset id="fieldsetBookmarkOptionsFooter" class="tblFooters">';
  402. $html .= '<input type="submit" name="SQL" id="button_submit_bookmark" value="'
  403. . __('Go') . '" />';
  404. $html .= '<div class="clearfloat"></div>' . "\n";
  405. $html .= '</fieldset>' . "\n";
  406. return $html;
  407. }
  408. }