sql_query_form.lib.php 18 KB

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