/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* general function, usally for data manipulation pages
*
*/
/**
* @var sql_box_locked lock for the sqlbox textarea in the querybox/querywindow
*/
var sql_box_locked = false;
/**
* @var array holds elements which content should only selected once
*/
var only_once_elements = new Array();
/**
* @var ajax_message_init boolean boolean that stores status of
* notification for PMA_ajaxShowNotification
*/
var ajax_message_init = false;
/**
* Generate a new password and copy it to the password input areas
*
* @param object the form that holds the password fields
*
* @return boolean always true
*/
function suggestPassword(passwd_form) {
// restrict the password to just letters and numbers to avoid problems:
// "editors and viewers regard the password as multiple words and
// things like double click no longer work"
var pwchars = "abcdefhjmnpqrstuvwxyz23456789ABCDEFGHJKLMNPQRSTUVWYXZ";
var passwordlength = 16; // do we want that to be dynamic? no, keep it simple :)
var passwd = passwd_form.generated_pw;
passwd.value = '';
for ( i = 0; i < passwordlength; i++ ) {
passwd.value += pwchars.charAt( Math.floor( Math.random() * pwchars.length ) )
}
passwd_form.text_pma_pw.value = passwd.value;
passwd_form.text_pma_pw2.value = passwd.value;
return true;
}
/**
* for libraries/display_change_password.lib.php
* libraries/user_password.php
*
*/
function displayPasswordGenerateButton() {
$('#tr_element_before_generate_password').parent().append('
');
}
/**
* selects the content of a given object, f.e. a textarea
*
* @param object element element of which the content will be selected
* @param var lock variable which holds the lock for this element
* or true, if no lock exists
* @param boolean only_once if true this is only done once
* f.e. only on first focus
*/
function selectContent( element, lock, only_once ) {
if ( only_once && only_once_elements[element.name] ) {
return;
}
only_once_elements[element.name] = true;
if ( lock ) {
return;
}
element.select();
}
/**
* Displays an confirmation box before to submit a "DROP DATABASE" query.
* This function is called while clicking links
*
* @param object the link
* @param object the sql query to submit
*
* @return boolean whether to run the query or not
*/
function confirmLinkDropDB(theLink, theSqlQuery)
{
// Confirmation is not required in the configuration file
// or browser is Opera (crappy js implementation)
if (PMA_messages['strDoYouReally'] == '' || typeof(window.opera) != 'undefined') {
return true;
}
var is_confirmed = confirm(PMA_messages['strDropDatabaseStrongWarning'] + '\n' + PMA_messages['strDoYouReally'] + ' :\n' + theSqlQuery);
if (is_confirmed) {
theLink.href += '&is_js_confirmed=1';
}
return is_confirmed;
} // end of the 'confirmLinkDropDB()' function
/**
* Displays an confirmation box before to submit a "DROP/DELETE/ALTER" query.
* This function is called while clicking links
*
* @param object the link
* @param object the sql query to submit
*
* @return boolean whether to run the query or not
*/
function confirmLink(theLink, theSqlQuery)
{
// Confirmation is not required in the configuration file
// or browser is Opera (crappy js implementation)
if (PMA_messages['strDoYouReally'] == '' || typeof(window.opera) != 'undefined') {
return true;
}
var is_confirmed = confirm(PMA_messages['strDoYouReally'] + ' :\n' + theSqlQuery);
if (is_confirmed) {
if ( typeof(theLink.href) != 'undefined' ) {
theLink.href += '&is_js_confirmed=1';
} else if ( typeof(theLink.form) != 'undefined' ) {
theLink.form.action += '?is_js_confirmed=1';
}
}
return is_confirmed;
} // end of the 'confirmLink()' function
/**
* Displays an confirmation box before doing some action
*
* @param object the message to display
*
* @return boolean whether to run the query or not
*
* @todo used only by libraries/display_tbl.lib.php. figure out how it is used
* and replace with a jQuery equivalent
*/
function confirmAction(theMessage)
{
// TODO: Confirmation is not required in the configuration file
// or browser is Opera (crappy js implementation)
if (typeof(window.opera) != 'undefined') {
return true;
}
var is_confirmed = confirm(theMessage);
return is_confirmed;
} // end of the 'confirmAction()' function
/**
* Displays an error message if a "DROP DATABASE" statement is submitted
* while it isn't allowed, else confirms a "DROP/DELETE/ALTER" query before
* sumitting it if required.
* This function is called by the 'checkSqlQuery()' js function.
*
* @param object the form
* @param object the sql query textarea
*
* @return boolean whether to run the query or not
*
* @see checkSqlQuery()
*/
function confirmQuery(theForm1, sqlQuery1)
{
// Confirmation is not required in the configuration file
if (PMA_messages['strDoYouReally'] == '') {
return true;
}
// The replace function (js1.2) isn't supported
else if (typeof(sqlQuery1.value.replace) == 'undefined') {
return true;
}
// js1.2+ -> validation with regular expressions
else {
// "DROP DATABASE" statement isn't allowed
if (PMA_messages['strNoDropDatabases'] != '') {
var drop_re = new RegExp('(^|;)\\s*DROP\\s+(IF EXISTS\\s+)?DATABASE\\s', 'i');
if (drop_re.test(sqlQuery1.value)) {
alert(PMA_messages['strNoDropDatabases']);
theForm1.reset();
sqlQuery1.focus();
return false;
} // end if
} // end if
// Confirms a "DROP/DELETE/ALTER/TRUNCATE" statement
//
// TODO: find a way (if possible) to use the parser-analyser
// for this kind of verification
// For now, I just added a ^ to check for the statement at
// beginning of expression
var do_confirm_re_0 = new RegExp('^\\s*DROP\\s+(IF EXISTS\\s+)?(TABLE|DATABASE|PROCEDURE)\\s', 'i');
var do_confirm_re_1 = new RegExp('^\\s*ALTER\\s+TABLE\\s+((`[^`]+`)|([A-Za-z0-9_$]+))\\s+DROP\\s', 'i');
var do_confirm_re_2 = new RegExp('^\\s*DELETE\\s+FROM\\s', 'i');
var do_confirm_re_3 = new RegExp('^\\s*TRUNCATE\\s', 'i');
if (do_confirm_re_0.test(sqlQuery1.value)
|| do_confirm_re_1.test(sqlQuery1.value)
|| do_confirm_re_2.test(sqlQuery1.value)
|| do_confirm_re_3.test(sqlQuery1.value)) {
var message = (sqlQuery1.value.length > 100)
? sqlQuery1.value.substr(0, 100) + '\n ...'
: sqlQuery1.value;
var is_confirmed = confirm(PMA_messages['strDoYouReally'] + ' :\n' + message);
// statement is confirmed -> update the
// "is_js_confirmed" form field so the confirm test won't be
// run on the server side and allows to submit the form
if (is_confirmed) {
theForm1.elements['is_js_confirmed'].value = 1;
return true;
}
// statement is rejected -> do not submit the form
else {
window.focus();
sqlQuery1.focus();
return false;
} // end if (handle confirm box result)
} // end if (display confirm box)
} // end confirmation stuff
return true;
} // end of the 'confirmQuery()' function
/**
* Displays a confirmation box before disabling the BLOB repository for a given database.
* This function is called while clicking links
*
* @param object the database
*
* @return boolean whether to disable the repository or not
*/
function confirmDisableRepository(theDB)
{
// Confirmation is not required in the configuration file
// or browser is Opera (crappy js implementation)
if (PMA_messages['strDoYouReally'] == '' || typeof(window.opera) != 'undefined') {
return true;
}
var is_confirmed = confirm(PMA_messages['strBLOBRepositoryDisableStrongWarning'] + '\n' + PMA_messages['strBLOBRepositoryDisableAreYouSure']);
return is_confirmed;
} // end of the 'confirmDisableBLOBRepository()' function
/**
* Displays an error message if the user submitted the sql query form with no
* sql query, else checks for "DROP/DELETE/ALTER" statements
*
* @param object the form
*
* @return boolean always false
*
* @see confirmQuery()
*/
function checkSqlQuery(theForm)
{
var sqlQuery = theForm.elements['sql_query'];
var isEmpty = 1;
// The replace function (js1.2) isn't supported -> basic tests
if (typeof(sqlQuery.value.replace) == 'undefined') {
isEmpty = (sqlQuery.value == '') ? 1 : 0;
if (isEmpty && typeof(theForm.elements['sql_file']) != 'undefined') {
isEmpty = (theForm.elements['sql_file'].value == '') ? 1 : 0;
}
if (isEmpty && typeof(theForm.elements['sql_localfile']) != 'undefined') {
isEmpty = (theForm.elements['sql_localfile'].value == '') ? 1 : 0;
}
if (isEmpty && typeof(theForm.elements['id_bookmark']) != 'undefined') {
isEmpty = (theForm.elements['id_bookmark'].value == null || theForm.elements['id_bookmark'].value == '');
}
}
// js1.2+ -> validation with regular expressions
else {
var space_re = new RegExp('\\s+');
if (typeof(theForm.elements['sql_file']) != 'undefined' &&
theForm.elements['sql_file'].value.replace(space_re, '') != '') {
return true;
}
if (typeof(theForm.elements['sql_localfile']) != 'undefined' &&
theForm.elements['sql_localfile'].value.replace(space_re, '') != '') {
return true;
}
if (isEmpty && typeof(theForm.elements['id_bookmark']) != 'undefined' &&
(theForm.elements['id_bookmark'].value != null || theForm.elements['id_bookmark'].value != '') &&
theForm.elements['id_bookmark'].selectedIndex != 0
) {
return true;
}
// Checks for "DROP/DELETE/ALTER" statements
if (sqlQuery.value.replace(space_re, '') != '') {
if (confirmQuery(theForm, sqlQuery)) {
return true;
} else {
return false;
}
}
theForm.reset();
isEmpty = 1;
}
if (isEmpty) {
sqlQuery.select();
alert(PMA_messages['strFormEmpty']);
sqlQuery.focus();
return false;
}
return true;
} // end of the 'checkSqlQuery()' function
// Global variable row_class is set to even
var row_class = 'even';
/**
* Generates a row dynamically in the differences table displaying
* the complete statistics of difference in table like number of
* rows to be updated, number of rows to be inserted, number of
* columns to be added, number of columns to be removed, etc.
*
* @param index index of matching table
* @param update_size number of rows/column to be updated
* @param insert_size number of rows/coulmns to be inserted
* @param remove_size number of columns to be removed
* @param insert_index number of indexes to be inserted
* @param remove_index number of indexes to be removed
* @param img_obj image object
* @param table_name name of the table
*/
function showDetails(i, update_size, insert_size, remove_size, insert_index, remove_index, img_obj, table_name)
{
// The path of the image is split to facilitate comparison
var relative_path = (img_obj.src).split("themes/");
// The image source is changed when the showDetails function is called.
if (relative_path[1] == 'original/img/new_data_hovered.jpg') {
img_obj.src = "./themes/original/img/new_data_selected_hovered.jpg";
img_obj.alt = PMA_messages['strClickToUnselect']; //only for IE browser
} else if (relative_path[1] == 'original/img/new_struct_hovered.jpg') {
img_obj.src = "./themes/original/img/new_struct_selected_hovered.jpg";
img_obj.alt = PMA_messages['strClickToUnselect'];
} else if (relative_path[1] == 'original/img/new_struct_selected_hovered.jpg') {
img_obj.src = "./themes/original/img/new_struct_hovered.jpg";
img_obj.alt = PMA_messages['strClickToSelect'];
} else if (relative_path[1] == 'original/img/new_data_selected_hovered.jpg') {
img_obj.src = "./themes/original/img/new_data_hovered.jpg";
img_obj.alt = PMA_messages['strClickToSelect'];
}
var div = document.getElementById("list");
var table = div.getElementsByTagName("table")[0];
var table_body = table.getElementsByTagName("tbody")[0];
//Global variable row_class is being used
if (row_class == 'even') {
row_class = 'odd';
} else {
row_class = 'even';
}
// If the red or green button against a table name is pressed then append a new row to show the details of differences of this table.
if ((relative_path[1] != 'original/img/new_struct_selected_hovered.jpg') && (relative_path[1] != 'original/img/new_data_selected_hovered.jpg')) {
var newRow = document.createElement("tr");
newRow.setAttribute("class", row_class);
newRow.className = row_class;
// Id assigned to this row element is same as the index of this table name in the matching_tables/source_tables_uncommon array
newRow.setAttribute("id" , i);
var table_name_cell = document.createElement("td");
table_name_cell.align = "center";
table_name_cell.innerHTML = table_name ;
newRow.appendChild(table_name_cell);
var create_table = document.createElement("td");
create_table.align = "center";
var add_cols = document.createElement("td");
add_cols.align = "center";
var remove_cols = document.createElement("td");
remove_cols.align = "center";
var alter_cols = document.createElement("td");
alter_cols.align = "center";
var add_index = document.createElement("td");
add_index.align = "center";
var delete_index = document.createElement("td");
delete_index.align = "center";
var update_rows = document.createElement("td");
update_rows.align = "center";
var insert_rows = document.createElement("td");
insert_rows.align = "center";
var tick_image = document.createElement("img");
tick_image.src = "./themes/original/img/s_success.png";
if (update_size == '' && insert_size == '' && remove_size == '') {
/**
This is the case when the table needs to be created in target database.
*/
create_table.appendChild(tick_image);
add_cols.innerHTML = "--";
remove_cols.innerHTML = "--";
alter_cols.innerHTML = "--";
delete_index.innerHTML = "--";
add_index.innerHTML = "--";
update_rows.innerHTML = "--";
insert_rows.innerHTML = "--";
newRow.appendChild(create_table);
newRow.appendChild(add_cols);
newRow.appendChild(remove_cols);
newRow.appendChild(alter_cols);
newRow.appendChild(delete_index);
newRow.appendChild(add_index);
newRow.appendChild(update_rows);
newRow.appendChild(insert_rows);
} else if (update_size == '' && remove_size == '') {
/**
This is the case when data difference is displayed in the
table which is present in source but absent from target database
*/
create_table.innerHTML = "--";
add_cols.innerHTML = "--";
remove_cols.innerHTML = "--";
alter_cols.innerHTML = "--";
add_index.innerHTML = "--";
delete_index.innerHTML = "--";
update_rows.innerHTML = "--";
insert_rows.innerHTML = insert_size;
newRow.appendChild(create_table);
newRow.appendChild(add_cols);
newRow.appendChild(remove_cols);
newRow.appendChild(alter_cols);
newRow.appendChild(delete_index);
newRow.appendChild(add_index);
newRow.appendChild(update_rows);
newRow.appendChild(insert_rows);
} else if (remove_size == '') {
/**
This is the case when data difference between matching_tables is displayed.
*/
create_table.innerHTML = "--";
add_cols.innerHTML = "--";
remove_cols.innerHTML = "--";
alter_cols.innerHTML = "--";
add_index.innerHTML = "--";
delete_index.innerHTML = "--";
update_rows.innerHTML = update_size;
insert_rows.innerHTML = insert_size;
newRow.appendChild(create_table);
newRow.appendChild(add_cols);
newRow.appendChild(remove_cols);
newRow.appendChild(alter_cols);
newRow.appendChild(delete_index);
newRow.appendChild(add_index);
newRow.appendChild(update_rows);
newRow.appendChild(insert_rows);
} else {
/**
This is the case when structure difference between matching_tables id displayed
*/
create_table.innerHTML = "--";
add_cols.innerHTML = insert_size;
remove_cols.innerHTML = remove_size;
alter_cols.innerHTML = update_size;
delete_index.innerHTML = remove_index;
add_index.innerHTML = insert_index;
update_rows.innerHTML = "--";
insert_rows.innerHTML = "--";
newRow.appendChild(create_table);
newRow.appendChild(add_cols);
newRow.appendChild(remove_cols);
newRow.appendChild(alter_cols);
newRow.appendChild(delete_index);
newRow.appendChild(add_index);
newRow.appendChild(update_rows);
newRow.appendChild(insert_rows);
}
table_body.appendChild(newRow);
} else if ((relative_path[1] != 'original/img/new_struct_hovered.jpg') && (relative_path[1] != 'original/img/new_data_hovered.jpg')) {
//The case when the row showing the details need to be removed from the table i.e. the difference button is deselected now.
var table_rows = table_body.getElementsByTagName("tr");
var j;
var index = 0;
for (j=0; j < table_rows.length; j++)
{
if (table_rows[j].id == i) {
index = j;
table_rows[j].parentNode.removeChild(table_rows[j]);
}
}
//The table row css is being adjusted. Class "odd" for odd rows and "even" for even rows should be maintained.
for(index; index < table_rows.length; index++)
{
row_class_element = table_rows[index].getAttribute('class');
if (row_class_element == "even") {
table_rows[index].setAttribute("class","odd"); // for Mozilla firefox
table_rows[index].className = "odd"; // for IE browser
} else {
table_rows[index].setAttribute("class","even"); // for Mozilla firefox
table_rows[index].className = "even"; // for IE browser
}
}
}
}
/**
* Changes the image on hover effects
*
* @param img_obj the image object whose source needs to be changed
*
*/
function change_Image(img_obj)
{
var relative_path = (img_obj.src).split("themes/");
if (relative_path[1] == 'original/img/new_data.jpg') {
img_obj.src = "./themes/original/img/new_data_hovered.jpg";
} else if (relative_path[1] == 'original/img/new_struct.jpg') {
img_obj.src = "./themes/original/img/new_struct_hovered.jpg";
} else if (relative_path[1] == 'original/img/new_struct_hovered.jpg') {
img_obj.src = "./themes/original/img/new_struct.jpg";
} else if (relative_path[1] == 'original/img/new_data_hovered.jpg') {
img_obj.src = "./themes/original/img/new_data.jpg";
} else if (relative_path[1] == 'original/img/new_data_selected.jpg') {
img_obj.src = "./themes/original/img/new_data_selected_hovered.jpg";
} else if(relative_path[1] == 'original/img/new_struct_selected.jpg') {
img_obj.src = "./themes/original/img/new_struct_selected_hovered.jpg";
} else if (relative_path[1] == 'original/img/new_struct_selected_hovered.jpg') {
img_obj.src = "./themes/original/img/new_struct_selected.jpg";
} else if (relative_path[1] == 'original/img/new_data_selected_hovered.jpg') {
img_obj.src = "./themes/original/img/new_data_selected.jpg";
}
}
/**
* Generates the URL containing the list of selected table ids for synchronization and
* a variable checked for confirmation of deleting previous rows from target tables
*
* @param token the token generated for each PMA form
*
*/
function ApplySelectedChanges(token)
{
var div = document.getElementById("list");
var table = div.getElementsByTagName('table')[0];
var table_body = table.getElementsByTagName('tbody')[0];
// Get all the rows from the details table
var table_rows = table_body.getElementsByTagName('tr');
var x = table_rows.length;
var i;
/**
Append the token at the beginning of the query string followed by
Table_ids that shows that "Apply Selected Changes" button is pressed
*/
var append_string = "?token="+token+"&Table_ids="+1;
for(i=0; i max) {
theField.select();
alert(message.replace('%d', val));
theField.focus();
return false;
}
// It's a valid number
else {
theField.value = val;
}
return true;
} // end of the 'checkFormElementInRange()' function
function checkTableEditForm(theForm, fieldsCnt)
{
// TODO: avoid sending a message if user just wants to add a line
// on the form but has not completed at least one field name
var atLeastOneField = 0;
var i, elm, elm2, elm3, val, id;
for (i=0; i exits
if ((thePointerClass == '' && theMarkClass == '')
|| typeof(theRow.style) == 'undefined') {
return false;
}
var tagSwitch = null;
// 2. Gets the current row and exits if the browser can't get it
if (typeof(document.getElementsByTagName) != 'undefined') {
tagSwitch = 'tag';
} else if (typeof(document.getElementById('table_results')) != 'undefined') {
tagSwitch = 'cells';
} else {
return false;
}
var theCells = null;
if (tagSwitch == 'tag') {
theRows = document.getElementById('table_results').getElementsByTagName('tr');
theCells = theRows[1].getElementsByTagName('td');
} else if (tagSwitch == 'cells') {
theRows = document.getElementById('table_results').rows;
theCells = theRows[1].cells;
}
// 3. Gets the current Class...
var currentClass = null;
var newClass = null;
// 3.1 ... with DOM compatible browsers except Opera that does not return
// valid values with "getAttribute"
if (typeof(window.opera) == 'undefined'
&& typeof(theCells[theColNum].getAttribute) != 'undefined') {
currentClass = theCells[theColNum].className;
} // end 3
// 4. Defines the new Class
// 4.1 Current Class is the default one
if (currentClass == ''
|| currentClass.toLowerCase() == theDefaultClass1.toLowerCase()
|| currentClass.toLowerCase() == theDefaultClass2.toLowerCase()) {
if (theAction == 'over' && thePointerClass != '') {
newClass = thePointerClass;
} else if (theAction == 'click' && theMarkClass != '') {
newClass = theMarkClass;
marked_row[theColNum] = true;
}
}
// 4.1.2 Current Class is the pointer one
else if (currentClass.toLowerCase() == thePointerClass.toLowerCase() &&
(typeof(marked_row[theColNum]) == 'undefined' || !marked_row[theColNum]) || marked_row[theColNum] == false) {
if (theAction == 'out') {
if (theColNum % 2) {
newClass = theDefaultClass1;
} else {
newClass = theDefaultClass2;
}
}
else if (theAction == 'click' && theMarkClass != '') {
newClass = theMarkClass;
marked_row[theColNum] = true;
}
}
// 4.1.3 Current Class is the marker one
else if (currentClass.toLowerCase() == theMarkClass.toLowerCase()) {
if (theAction == 'click') {
newClass = (thePointerClass != '')
? thePointerClass
: ((theColNum % 2) ? theDefaultClass2 : theDefaultClass1);
marked_row[theColNum] = false;
}
} // end 4
// 5 ... with DOM compatible browsers except Opera
if (newClass) {
var c = null;
var rowCnt = theRows.length;
for (c = 0; c < rowCnt; c++) {
if (tagSwitch == 'tag') {
Cells = theRows[c].getElementsByTagName('td');
} else if (tagSwitch == 'cells') {
Cells = theRows[c].cells;
}
Cell = Cells[theColNum];
// 5.1 Sets the new Class...
Cell.className = Cell.className.replace(currentClass, newClass);
} // end for
} // end 5
return true;
} // end of the 'setVerticalPointer()' function
/**
* Checks/unchecks all checkbox in given conainer (f.e. a form, fieldset or div)
*
* @param string container_id the container id
* @param boolean state new value for checkbox (true or false)
* @return boolean always true
*/
function setCheckboxes( container_id, state ) {
if(state) {
$("#"+container_id).find("input:checkbox").attr('checked', 'checked');
}
else {
$("#"+container_id).find("input:checkbox").removeAttr('checked');
}
return true;
} // end of the 'setCheckboxes()' function
// - this was directly written to each td, so why not a function ;)
// setCheckboxColumn(\'id_rows_to_delete' . $row_no . ''\');
function setCheckboxColumn(theCheckbox){
if (document.getElementById(theCheckbox)) {
document.getElementById(theCheckbox).checked = (document.getElementById(theCheckbox).checked ? false : true);
if (document.getElementById(theCheckbox + 'r')) {
document.getElementById(theCheckbox + 'r').checked = document.getElementById(theCheckbox).checked;
}
} else {
if (document.getElementById(theCheckbox + 'r')) {
document.getElementById(theCheckbox + 'r').checked = (document.getElementById(theCheckbox +'r').checked ? false : true);
if (document.getElementById(theCheckbox)) {
document.getElementById(theCheckbox).checked = document.getElementById(theCheckbox + 'r').checked;
}
}
}
}
/**
* Checks/unchecks all options of a