sql_query_form.lib.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. * @usedby querywindow.php
  12. * @package PhpMyAdmin
  13. */
  14. if (! defined('PHPMYADMIN')) {
  15. exit;
  16. }
  17. /**
  18. *
  19. */
  20. require_once './libraries/file_listing.lib.php'; // used for file listing
  21. require_once './libraries/bookmark.lib.php'; // used for bookmarks
  22. /**
  23. * return HTML for the sql query boxes
  24. *
  25. * @param boolean|string $query query to display in the textarea
  26. * or true to display last executed
  27. * @param boolean|string $display_tab sql|files|history|full|false
  28. * what part to display
  29. * false if not inside querywindow
  30. * @param string $delimiter delimeter
  31. *
  32. * @return string
  33. *
  34. * @usedby server_sql.php
  35. * @usedby db_sql.php
  36. * @usedby tbl_sql.php
  37. * @usedby tbl_structure.php
  38. * @usedby tbl_tracking.php
  39. * @usedby querywindow.php
  40. */
  41. function PMA_getHtmlForSqlQueryForm(
  42. $query = true, $display_tab = false, $delimiter = ';'
  43. ) {
  44. $html = '';
  45. // check tab to display if inside querywindow
  46. if (! $display_tab) {
  47. $display_tab = 'full';
  48. $is_querywindow = false;
  49. } else {
  50. $is_querywindow = true;
  51. }
  52. // query to show
  53. if (true === $query) {
  54. $query = $GLOBALS['sql_query'];
  55. }
  56. // set enctype to multipart for file uploads
  57. if ($GLOBALS['is_upload']) {
  58. $enctype = ' enctype="multipart/form-data"';
  59. } else {
  60. $enctype = '';
  61. }
  62. $table = '';
  63. $db = '';
  64. if (! strlen($GLOBALS['db'])) {
  65. // prepare for server related
  66. $goto = empty($GLOBALS['goto']) ?
  67. 'server_sql.php' : $GLOBALS['goto'];
  68. } elseif (! strlen($GLOBALS['table'])) {
  69. // prepare for db related
  70. $db = $GLOBALS['db'];
  71. $goto = empty($GLOBALS['goto']) ?
  72. 'db_sql.php' : $GLOBALS['goto'];
  73. } else {
  74. $table = $GLOBALS['table'];
  75. $db = $GLOBALS['db'];
  76. $goto = empty($GLOBALS['goto']) ?
  77. 'tbl_sql.php' : $GLOBALS['goto'];
  78. }
  79. // start output
  80. if ($is_querywindow) {
  81. $html .= '<form method="post" id="sqlqueryform"';
  82. $html .= ' action="import.php" ' . $enctype . ' name="sqlform">';
  83. } else {
  84. $html .= '<form method="post" action="import.php" ' . $enctype;
  85. $html .= ' class="ajax"';
  86. $html .= ' id="sqlqueryform" name="sqlform">' . "\n";
  87. }
  88. if ($is_querywindow) {
  89. $html .= '<input type="hidden" name="focus_querywindow"'
  90. . ' value="true" />' . "\n";
  91. if ($display_tab != 'sql' && $display_tab != 'full') {
  92. $html .= '<input type="hidden" name="sql_query"'
  93. . ' value="" />' . "\n";
  94. $html .= '<input type="hidden" name="show_query"'
  95. . ' value="1" />' . "\n";
  96. }
  97. }
  98. $html .= '<input type="hidden" name="is_js_confirmed" value="0" />'
  99. . "\n" . PMA_URL_getHiddenInputs($db, $table) . "\n"
  100. . '<input type="hidden" name="pos" value="0" />' . "\n"
  101. . '<input type="hidden" name="goto" value="'
  102. . htmlspecialchars($goto) . '" />' . "\n"
  103. . '<input type="hidden" name="message_to_show" value="'
  104. . __('Your SQL query has been executed successfully.') . '" />'
  105. . "\n" . '<input type="hidden" name="prev_sql_query" value="'
  106. . htmlspecialchars($query) . '" />' . "\n";
  107. // display querybox
  108. if ($display_tab === 'full' || $display_tab === 'sql') {
  109. $html .= PMA_getHtmlForSqlQueryFormInsert(
  110. $query, $is_querywindow, $delimiter
  111. );
  112. }
  113. // display uploads
  114. if ($display_tab === 'files' && $GLOBALS['is_upload']) {
  115. $html .= PMA_getHtmlForSqlQueryFormUpload();
  116. }
  117. // Bookmark Support
  118. if ($display_tab === 'full' || $display_tab === 'history') {
  119. if (! empty($GLOBALS['cfg']['Bookmark'])) {
  120. $html .= PMA_getHtmlForSqlQueryFormBookmark();
  121. }
  122. }
  123. // Encoding setting form appended by Y.Kawada
  124. if (function_exists('PMA_Kanji_encodingForm')) {
  125. $html .= PMA_Kanji_encodingForm();
  126. }
  127. $html .= '</form>' . "\n";
  128. // print an empty div, which will be later filled with
  129. // the sql query results by ajax
  130. $html .= '<div id="sqlqueryresults"></div>';
  131. return $html;
  132. }
  133. /**
  134. * return HTML for Sql Query Form Insert
  135. *
  136. * @param string $query query to display in the textarea
  137. * @param boolean $is_querywindow if inside querywindow or not
  138. * @param string $delimiter default delimiter to use
  139. *
  140. * @return string
  141. *
  142. * @usedby PMA_getHtmlForSqlQueryForm()
  143. */
  144. function PMA_getHtmlForSqlQueryFormInsert(
  145. $query = '', $is_querywindow = false, $delimiter = ';'
  146. ) {
  147. // enable auto select text in textarea
  148. if ($GLOBALS['cfg']['TextareaAutoSelect']) {
  149. $auto_sel = ' onclick="selectContent(this, sql_box_locked, true);"';
  150. } else {
  151. $auto_sel = '';
  152. }
  153. // enable locking if inside query window
  154. if ($is_querywindow) {
  155. $locking = ' onkeypress="document.sqlform.elements[\'LockFromUpdate\'].'
  156. . 'checked = true;"';
  157. $height = $GLOBALS['cfg']['TextareaRows'] * 1.25;
  158. } else {
  159. $locking = '';
  160. $height = $GLOBALS['cfg']['TextareaRows'] * 2;
  161. }
  162. $table = '';
  163. $db = '';
  164. $fields_list = array();
  165. if (! strlen($GLOBALS['db'])) {
  166. // prepare for server related
  167. $legend = sprintf(
  168. __('Run SQL query/queries on server %s'),
  169. '&quot;' . htmlspecialchars(
  170. ! empty($GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose'])
  171. ? $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose']
  172. : $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['host']
  173. ) . '&quot;'
  174. );
  175. } elseif (! strlen($GLOBALS['table'])) {
  176. // prepare for db related
  177. $db = $GLOBALS['db'];
  178. // if you want navigation:
  179. $tmp_db_link = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
  180. . '?' . PMA_URL_getCommon($db) . '"';
  181. if ($is_querywindow) {
  182. $tmp_db_link .= ' target="_self"'
  183. . ' onclick="this.target=window.opener.frame_content.name"';
  184. }
  185. $tmp_db_link .= '>'
  186. . htmlspecialchars($db) . '</a>';
  187. // else use
  188. // $tmp_db_link = htmlspecialchars($db);
  189. $legend = sprintf(__('Run SQL query/queries on database %s'), $tmp_db_link);
  190. if (empty($query)) {
  191. $query = PMA_Util::expandUserString(
  192. $GLOBALS['cfg']['DefaultQueryDatabase'], 'backquote'
  193. );
  194. }
  195. } else {
  196. $table = $GLOBALS['table'];
  197. $db = $GLOBALS['db'];
  198. // Get the list and number of fields
  199. // we do a try_query here, because we could be in the query window,
  200. // trying to synchonize and the table has not yet been created
  201. $fields_list = $GLOBALS['dbi']->getColumns(
  202. $db, $GLOBALS['table'], null, true
  203. );
  204. $tmp_db_link = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
  205. . '?' . PMA_URL_getCommon($db) . '"';
  206. if ($is_querywindow) {
  207. $tmp_db_link .= 'target="_parent" '
  208. . 'onclick="window.opener.location.href = \''
  209. . $GLOBALS['cfg']['DefaultTabDatabase']
  210. . '?' . PMA_URL_getCommon($db) . '\';return false;"';
  211. }
  212. $tmp_db_link .= '>'
  213. . htmlspecialchars($db) . '</a>';
  214. // else use
  215. // $tmp_db_link = htmlspecialchars($db);
  216. $legend = sprintf(__('Run SQL query/queries on database %s'), $tmp_db_link);
  217. if (empty($query)) {
  218. $query = PMA_Util::expandUserString(
  219. $GLOBALS['cfg']['DefaultQueryTable'], 'backquote'
  220. );
  221. }
  222. }
  223. $legend .= ': ' . PMA_Util::showMySQLDocu('SELECT');
  224. if (count($fields_list)) {
  225. $sqlquerycontainer_id = 'sqlquerycontainer';
  226. } else {
  227. $sqlquerycontainer_id = 'sqlquerycontainerfull';
  228. }
  229. $html = '<a id="querybox"></a>'
  230. . '<div id="queryboxcontainer">'
  231. . '<fieldset id="queryboxf">';
  232. $html .= '<legend>' . $legend . '</legend>';
  233. $html .= '<div id="queryfieldscontainer">';
  234. $html .= '<div id="' . $sqlquerycontainer_id . '">'
  235. . '<textarea tabindex="100" name="sql_query" id="sqlquery"'
  236. . ' cols="' . $GLOBALS['cfg']['TextareaCols'] . '"'
  237. . ' rows="' . $height . '"'
  238. . ' dir="' . $GLOBALS['text_dir'] . '"'
  239. . $auto_sel . $locking . '>'
  240. . htmlspecialchars($query)
  241. . '</textarea>';
  242. // Add buttons to generate query easily for
  243. // select all, single select, insert, update and delete
  244. if (count($fields_list)) {
  245. $html .= '<input type="button" value="SELECT *" id="selectall"'
  246. . ' class="button sqlbutton" />';
  247. $html .= '<input type="button" value="SELECT" id="select"'
  248. . ' class="button sqlbutton" />';
  249. $html .= '<input type="button" value="INSERT" id="insert"'
  250. . ' class="button sqlbutton" />';
  251. $html .= '<input type="button" value="UPDATE" id="update"'
  252. . ' class="button sqlbutton" />';
  253. $html .= '<input type="button" value="DELETE" id="delete"'
  254. . ' class="button sqlbutton" />';
  255. }
  256. $html .= '<input type="button" value="' . __('Clear') . '" id="clear"'
  257. . ' class="button sqlbutton" />';
  258. $html .= '</div>' . "\n";
  259. if (count($fields_list)) {
  260. $html .= '<div id="tablefieldscontainer">'
  261. . '<label>' . __('Columns') . '</label>'
  262. . '<select id="tablefields" name="dummy" '
  263. . 'size="' . ($GLOBALS['cfg']['TextareaRows'] - 2) . '" '
  264. . 'multiple="multiple" ondblclick="insertValueQuery()">';
  265. foreach ($fields_list as $field) {
  266. $html .= '<option value="'
  267. . PMA_Util::backquote(htmlspecialchars($field['Field'])) . '"';
  268. if (isset($field['Field'])
  269. && strlen($field['Field'])
  270. && isset($field['Comment'])
  271. ) {
  272. $html .= ' title="' . htmlspecialchars($field['Comment']) . '"';
  273. }
  274. $html .= '>' . htmlspecialchars($field['Field']) . '</option>' . "\n";
  275. }
  276. $html .= '</select>'
  277. . '<div id="tablefieldinsertbuttoncontainer">';
  278. if (PMA_Util::showIcons('ActionLinksMode')) {
  279. $html .= '<input type="button" class="button" name="insert"'
  280. . ' value="&lt;&lt;" onclick="insertValueQuery()"'
  281. . ' title="' . __('Insert') . '" />';
  282. } else {
  283. $html .= '<input type="button" class="button" name="insert"'
  284. . ' value="' . __('Insert') . '"'
  285. . ' onclick="insertValueQuery()" />';
  286. }
  287. $html .= '</div>' . "\n"
  288. . '</div>' . "\n";
  289. }
  290. $html .= '<div class="clearfloat"></div>' . "\n";
  291. $html .= '</div>' . "\n";
  292. if (! empty($GLOBALS['cfg']['Bookmark'])) {
  293. $html .= '<div id="bookmarkoptions">';
  294. $html .= '<div class="formelement">';
  295. $html .= '<label for="bkm_label">'
  296. . __('Bookmark this SQL query:') . '</label>';
  297. $html .= '<input type="text" name="bkm_label" id="bkm_label"'
  298. . ' tabindex="110" value="" />';
  299. $html .= '</div>';
  300. $html .= '<div class="formelement">';
  301. $html .= '<input type="checkbox" name="bkm_all_users" tabindex="111"'
  302. . ' id="id_bkm_all_users" value="true" />';
  303. $html .= '<label for="id_bkm_all_users">'
  304. . __('Let every user access this bookmark') . '</label>';
  305. $html .= '</div>';
  306. $html .= '<div class="formelement">';
  307. $html .= '<input type="checkbox" name="bkm_replace" tabindex="112"'
  308. . ' id="id_bkm_replace" value="true" />';
  309. $html .= '<label for="id_bkm_replace">'
  310. . __('Replace existing bookmark of same name') . '</label>';
  311. $html .= '</div>';
  312. $html .= '</div>';
  313. }
  314. $html .= '<div class="clearfloat"></div>' . "\n";
  315. $html .= '</fieldset>' . "\n"
  316. . '</div>' . "\n";
  317. $html .= '<fieldset id="queryboxfooter" class="tblFooters">' . "\n";
  318. $html .= '<div class="formelement">' . "\n";
  319. if ($is_querywindow) {
  320. $html .= '<input type="checkbox" '
  321. . 'name="LockFromUpdate" checked="checked" tabindex="120" '
  322. . 'id="checkbox_lock" /> <label for="checkbox_lock">'
  323. . __('Do not overwrite this query from outside the window')
  324. . '</label>';
  325. }
  326. $html .= '</div>' . "\n";
  327. $html .= '<div class="formelement">' . "\n";
  328. $html .= '<label for="id_sql_delimiter">[ ' . __('Delimiter')
  329. . '</label>' . "\n";
  330. $html .= '<input type="text" name="sql_delimiter" tabindex="131" size="3" '
  331. . 'value="' . $delimiter . '" '
  332. . 'id="id_sql_delimiter" /> ]';
  333. $html .= '<input type="checkbox" name="show_query" value="1" '
  334. . 'id="checkbox_show_query" tabindex="132" checked="checked" />'
  335. . '<label for="checkbox_show_query">' . __('Show this query here again')
  336. . '</label>';
  337. if (! $is_querywindow) {
  338. $html .= '<input type="checkbox" name="retain_query_box" value="1" '
  339. . 'id="retain_query_box" tabindex="133" '
  340. . ($GLOBALS['cfg']['RetainQueryBox'] === false
  341. ? '' : ' checked="checked"')
  342. . ' />'
  343. . '<label for="retain_query_box">' . __('Retain query box')
  344. . '</label>';
  345. }
  346. $html .= '</div>' . "\n";
  347. $html .= '<input type="submit" id="button_submit_query" name="SQL"';
  348. if ($is_querywindow) {
  349. $html .= 'onclick="var form = this.parentNode.parentNode;'
  350. . ' window.opener.name = \'sqlParentWindow\';'
  351. . ' form.target = \'sqlParentWindow\';'
  352. . ' return checkSqlQuery(form);"';
  353. }
  354. $html .= ' tabindex="200" value="' . __('Go') . '" />' . "\n";
  355. $html .= '<div class="clearfloat"></div>' . "\n";
  356. $html .= '</fieldset>' . "\n";
  357. return $html;
  358. }
  359. /**
  360. * return HTML for sql Query Form Bookmark
  361. *
  362. * @return string|void
  363. *
  364. * @usedby PMA_getHtmlForSqlQueryForm()
  365. */
  366. function PMA_getHtmlForSqlQueryFormBookmark()
  367. {
  368. $bookmark_list = PMA_Bookmark_getList($GLOBALS['db']);
  369. if (! $bookmark_list || count($bookmark_list) < 1) {
  370. return;
  371. }
  372. $html = '<fieldset id="fieldsetBookmarkOptions">';
  373. $html .= '<legend>';
  374. $html .= __('Bookmarked SQL query') . '</legend>' . "\n";
  375. $html .= '<div class="formelement">';
  376. $html .= '<select name="id_bookmark" id="id_bookmark">' . "\n";
  377. $html .= '<option value="">&nbsp;</option>' . "\n";
  378. foreach ($bookmark_list as $key => $value) {
  379. $html .= '<option value="' . htmlspecialchars($key) . '">'
  380. . htmlspecialchars($value) . '</option>' . "\n";
  381. }
  382. // &nbsp; is required for correct display with styles/line height
  383. $html .= '</select>&nbsp;' . "\n";
  384. $html .= '</div>' . "\n";
  385. $html .= '<div class="formelement">' . "\n";
  386. $html .= __('Variable');
  387. $html .= PMA_Util::showDocu('faq', 'faqbookmark');
  388. $html .= '<input type="text" name="bookmark_variable" class="textfield"'
  389. . ' size="10" />' . "\n";
  390. $html .= '</div>' . "\n";
  391. $html .= '<div class="formelement">' . "\n";
  392. $html .= '<input type="radio" name="action_bookmark" value="0"'
  393. . ' id="radio_bookmark_exe" checked="checked" />'
  394. . '<label for="radio_bookmark_exe">' . __('Submit')
  395. . '</label>' . "\n";
  396. $html .= '<input type="radio" name="action_bookmark" value="1"'
  397. . ' id="radio_bookmark_view" />'
  398. . '<label for="radio_bookmark_view">' . __('View only')
  399. . '</label>' . "\n";
  400. $html .= '<input type="radio" name="action_bookmark" value="2"'
  401. . ' id="radio_bookmark_del" />'
  402. . '<label for="radio_bookmark_del">' . __('Delete')
  403. . '</label>' . "\n";
  404. $html .= '</div>' . "\n";
  405. $html .= '<div class="clearfloat"></div>' . "\n";
  406. $html .= '</fieldset>' . "\n";
  407. $html .= '<fieldset id="fieldsetBookmarkOptionsFooter" class="tblFooters">';
  408. $html .= '<input type="submit" name="SQL" id="button_submit_bookmark" value="'
  409. . __('Go') . '" />';
  410. $html .= '<div class="clearfloat"></div>' . "\n";
  411. $html .= '</fieldset>' . "\n";
  412. return $html;
  413. }
  414. /**
  415. * return HTML for Sql Query Form Upload
  416. *
  417. * @return string
  418. *
  419. * @usedby PMA_getHtmlForSqlQueryForm()
  420. */
  421. function PMA_getHtmlForSqlQueryFormUpload()
  422. {
  423. global $timeout_passed, $local_import_file;
  424. $errors = array();
  425. // we allow only SQL here
  426. $matcher = '@\.sql(\.(' . PMA_supportedDecompressions() . '))?$@';
  427. if (!empty($GLOBALS['cfg']['UploadDir'])) {
  428. $files = PMA_getFileSelectOptions(
  429. PMA_Util::userDir($GLOBALS['cfg']['UploadDir']), $matcher,
  430. (isset($timeout_passed) && $timeout_passed && isset($local_import_file))
  431. ? $local_import_file
  432. : ''
  433. );
  434. } else {
  435. $files = '';
  436. }
  437. // start output
  438. $html = '<fieldset id="">';
  439. $html .= '<legend>';
  440. $html .= __('Browse your computer:') . '</legend>';
  441. $html .= '<div class="formelement">';
  442. $html .= '<input type="file" name="sql_file" class="textfield" /> ';
  443. $html .= PMA_Util::getFormattedMaximumUploadSize($GLOBALS['max_upload_size']);
  444. // some browsers should respect this :)
  445. $html .= PMA_Util::generateHiddenMaxFileSize($GLOBALS['max_upload_size']) . "\n";
  446. $html .= '</div>';
  447. if ($files === false) {
  448. $errors[] = PMA_Message::error(
  449. __('The directory you set for upload work cannot be reached.')
  450. );
  451. } elseif (!empty($files)) {
  452. $html .= '<div class="formelement">';
  453. $html .= '<strong>' . __('web server upload directory:') . '</strong>' . "\n";
  454. $html .= '<select size="1" name="sql_localfile">' . "\n";
  455. $html .= '<option value="" selected="selected"></option>' . "\n";
  456. $html .= $files;
  457. $html .= '</select>' . "\n";
  458. $html .= '</div>';
  459. }
  460. $html .= '<div class="clearfloat"></div>' . "\n";
  461. $html .= '</fieldset>';
  462. $html .= '<fieldset id="" class="tblFooters">';
  463. $html .= __('Character set of the file:') . "\n";
  464. $html .= PMA_generateCharsetDropdownBox(
  465. PMA_CSDROPDOWN_CHARSET,
  466. 'charset_of_file', null, 'utf8', false
  467. );
  468. $html .= '<input type="submit" name="SQL" value="' . __('Go')
  469. . '" />' . "\n";
  470. $html .= '<div class="clearfloat"></div>' . "\n";
  471. $html .= '</fieldset>';
  472. foreach ($errors as $error) {
  473. $html .= $error->getDisplay();
  474. }
  475. return $html;
  476. }
  477. ?>