From 54000aa117fb505e04ac4d58de55eb7258fc5042 Mon Sep 17 00:00:00 2001 From: Edward Cheng Date: Wed, 2 Jul 2014 13:59:51 +0800 Subject: [PATCH] [Console]Second commit Console changes: + Ajax global callback + Support disable Codemirror + Global AJAX callback + XSS issues slove + JSON response bug fix + Selection query Explain and Profiling + Translation improvement + Bookmark creation and deletion + Codemirror wrong font-size under Firefox + Options card input elements value bug + Etc Removal: - Rollback query execution support Other changes: + Unify query history behavior Signed-off-by: Edward Cheng --- import.php | 29 ++- js/ajax.js | 3 - js/console.js | 297 ++++++++++++++++++++++++----- js/messages.php | 1 + js/sql.js | 3 - libraries/Console.class.php | 54 ++++-- libraries/Response.class.php | 8 +- libraries/bookmark.lib.php | 4 +- libraries/relation.lib.php | 8 +- themes/original/css/common.css.php | 24 +++ themes/pmahomme/css/common.css.php | 37 +++- 11 files changed, 376 insertions(+), 92 deletions(-) diff --git a/import.php b/import.php index 9d11397a54..cad37f8607 100644 --- a/import.php +++ b/import.php @@ -29,11 +29,38 @@ if (isset($_REQUEST['simulate_dml'])) { } // If it's a refresh console bookmarks request -if (isset($_GET['console_bookmark_refresh'])) { +if (isset($_REQUEST['console_bookmark_refresh'])) { $response = PMA_Response::getInstance(); $response->addJSON('console_message_bookmark', PMA_Console::getBookmarkContent()); exit; } +// If it's a console bookmark add request +if (isset($_REQUEST['console_bookmark_add'])) { + $response = PMA_Response::getInstance(); + if(isset($_REQUEST['label']) && isset($_REQUEST['db']) + && isset($_REQUEST['bookmark_query']) && isset($_REQUEST['shared'])) + { + $cfgBookmark = PMA_Bookmark_getParams(); + $bookmarkFields = array( + 'bkm_database' => $_REQUEST['db'], + 'bkm_user' => $cfgBookmark['user'], + 'bkm_sql_query' => urlencode($_REQUEST['bookmark_query']), + 'bkm_label' => $_REQUEST['label'] + ); + $isShared = ($_REQUEST['shared'] == 'true' ? true : false); + if(PMA_Bookmark_save($bookmarkFields, $isShared)) { + $response->addJSON('message', __('Successed')); + $response->addJSON('data', $bookmarkFields); + $response->addJSON('isShared', $isShared); + } else { + $response->addJSON('message', __('Failed')); + } + die(); + } else { + $response->addJSON('message', __('Incomplete params')); + die(); + } +} /** * Sets globals from $_POST diff --git a/js/ajax.js b/js/ajax.js index 4c441ec8bd..3a290a001a 100644 --- a/js/ajax.js +++ b/js/ajax.js @@ -458,9 +458,6 @@ var AJAX = { $('#'+data.fieldWithError).addClass("error"); } } - if(PMA_console && PMA_console.isEnabled) { - PMA_console.ajaxCallback(data); - } }, /** * This object is in charge of downloading scripts, diff --git a/js/console.js b/js/console.js index bdb3c025a5..97018d1e13 100644 --- a/js/console.js +++ b/js/console.js @@ -104,16 +104,16 @@ var PMA_console = { var tempConfig = JSON.parse($.cookie('pma_console_config')); if(tempConfig) { if(tempConfig.alwaysExpand === true) { - $('#pma_console_options input[name=always_expand]').attr('checked', true); + $('#pma_console_options input[name=always_expand]').prop('checked', true); } if(tempConfig.startHistory === true) { - $('#pma_console_options input[name=start_history]').attr('checked', true); + $('#pma_console_options input[name=start_history]').prop('checked', true); } if(tempConfig.currentQuery === true) { - $('#pma_console_options input[name=current_query]').attr('checked', true); + $('#pma_console_options input[name=current_query]').prop('checked', true); } } else { - $('#pma_console_options input[name=current_query]').attr('checked', true); + $('#pma_console_options input[name=current_query]').prop('checked', true); } PMA_console.updateConfig(); @@ -166,6 +166,11 @@ var PMA_console = { PMA_console.updateConfig(); }); + $(document).ajaxComplete(function (event, xhr) { + var data = $.parseJSON(xhr.responseText); + PMA_console.ajaxCallback(data); + }); + PMA_console.isInitialized = true; } @@ -192,15 +197,15 @@ var PMA_console = { * * @return void */ - execute: function(queryString, target) { + execute: function(queryString, options) { if(typeof(queryString) != 'string' || ! /[a-z]|[A-Z]/.test(queryString)){ return; } PMA_console.$requestForm.children('textarea').val(queryString); - if(target && target.db) { - PMA_console.$requestForm.children('[name=db]').attr('value', target.db); - if(target.table) { - PMA_console.$requestForm.children('[name=table]').attr('value', target.table); + if(options && options.db) { + PMA_console.$requestForm.children('[name=db]').attr('value', options.db); + if(options.table) { + PMA_console.$requestForm.children('[name=table]').attr('value', options.table); } else { PMA_console.$requestForm.children('[name=table]').attr('value', ''); } @@ -208,21 +213,26 @@ var PMA_console = { PMA_console.$requestForm.children('[name=db]').attr('value', (PMA_commonParams.get('db').length > 0 ? PMA_commonParams.get('db') : '')); } + PMA_console.$requestForm.find('[name=profiling]').remove(); + if(options && options.profiling === true) { + PMA_console.$requestForm.append(''); + } if (! confirmQuery(PMA_console.$requestForm[0], PMA_console.$requestForm.children('textarea')[0])) { return; } PMA_console.$requestForm.children('[name=console_message_id]') - .attr('value', PMA_consoleMessages.appendQuery({sql_query: queryString})); + .attr('value', PMA_consoleMessages.appendQuery({sql_query: queryString}).message_id); PMA_console.$requestForm.trigger('submit'); PMA_consoleInput.clear(); }, ajaxCallback: function(data) { - if(data.console_message_id) { + if(data && data.console_message_id) { PMA_consoleMessages.updateQuery(data.console_message_id, data.success, (data._reloadQuerywindow ? data._reloadQuerywindow : false)); - } else if(data._reloadQuerywindow) { + } else if( data && data._reloadQuerywindow) { if(data._reloadQuerywindow.sql_query.length > 0) { - PMA_consoleMessages.appendQuery(data._reloadQuerywindow, 'successed'); + PMA_consoleMessages.appendQuery(data._reloadQuerywindow, 'successed') + .$message.addClass(PMA_console.config.currentQuery ? '' : 'hide'); } } }, @@ -373,6 +383,10 @@ var PMA_console = { currentQuery: $('#pma_console_options input[name=current_query]').prop('checked') }; $.cookie('pma_console_config', JSON.stringify(PMA_console.config)); + }, + isSelect: function (queryString) { + var reg_exp = /^SELECT\s+/i; + return reg_exp.test(queryString); } }; @@ -453,10 +467,15 @@ var PMA_consoleResizer = { */ var PMA_consoleInput = { /** - * @var CodeMirror object + * @var array, contains Codemirror objects or input jQuery objects * @access private */ - _cm: null, + _inputs: null, + /** + * @var bool, if codemirror enabled + * @access private + */ + _codemirror: false, /** * Used for console input initialize * @@ -464,15 +483,35 @@ var PMA_consoleInput = { */ initialize: function() { // _cm object can't be reinitialize - if(PMA_consoleInput._cm !== null) { + if(PMA_consoleInput._inputs !== null) { return; } - PMA_consoleInput._cm = CodeMirror($('#query_input')[0], { - theme: 'pma', - mode: 'text/x-sql', - lineWrapping: true - }); - $('#pma_console .CodeMirror.cm-s-pma').keydown(PMA_consoleInput._keydown); + if(typeof CodeMirror !== 'undefined') { + PMA_consoleInput._codemirror = true; + } + PMA_consoleInput._inputs = []; + if (PMA_consoleInput._codemirror) { + PMA_consoleInput._inputs.console = CodeMirror($('#pma_console .console_query_input')[0], { + theme: 'pma', + mode: 'text/x-sql', + lineWrapping: true + }); + if ($('#pma_bookmarks').length !== 0) { + PMA_consoleInput._inputs.bookmark = CodeMirror($('#pma_console .bookmark_add_input')[0], { + theme: 'pma', + mode: 'text/x-sql', + lineWrapping: true + }); + } + } else { + PMA_consoleInput._inputs.console = + $('