diff --git a/libraries/import/csv.php b/libraries/import/csv.php index ad5c40673..fb50cb767 100644 --- a/libraries/import/csv.php +++ b/libraries/import/csv.php @@ -10,300 +10,303 @@ /** * */ -if ($plugin_param == 'table') { - if (isset($plugin_list)) { - $plugin_list['csv'] = array( - 'text' => 'strCSV', - 'extension' => 'csv', - 'options' => array( - array('type' => 'bool', 'name' => 'replace', 'text' => 'strReplaceTable'), - array('type' => 'bool', 'name' => 'ignore', 'text' => 'strIgnoreDuplicates'), - array('type' => 'text', 'name' => 'terminated', 'text' => 'strFieldsTerminatedBy', 'size' => 2, 'len' => 2), - array('type' => 'text', 'name' => 'enclosed', 'text' => 'strFieldsEnclosedBy', 'size' => 2, 'len' => 2), - array('type' => 'text', 'name' => 'escaped', 'text' => 'strFieldsEscapedBy', 'size' => 2, 'len' => 2), - array('type' => 'text', 'name' => 'new_line', 'text' => 'strLinesTerminatedBy', 'size' => 2), - array('type' => 'text', 'name' => 'columns', 'text' => 'strColumnNames'), - ), - 'options_text' => 'strCSVImportOptions', - ); - } else { +if ($plugin_param !== 'table') { + return; +} + +if (isset($plugin_list)) { + $plugin_list['csv'] = array( + 'text' => 'strCSV', + 'extension' => 'csv', + 'options' => array( + array('type' => 'bool', 'name' => 'replace', 'text' => 'strReplaceTable'), + array('type' => 'bool', 'name' => 'ignore', 'text' => 'strIgnoreDuplicates'), + array('type' => 'text', 'name' => 'terminated', 'text' => 'strFieldsTerminatedBy', 'size' => 2, 'len' => 2), + array('type' => 'text', 'name' => 'enclosed', 'text' => 'strFieldsEnclosedBy', 'size' => 2, 'len' => 2), + array('type' => 'text', 'name' => 'escaped', 'text' => 'strFieldsEscapedBy', 'size' => 2, 'len' => 2), + array('type' => 'text', 'name' => 'new_line', 'text' => 'strLinesTerminatedBy', 'size' => 2), + array('type' => 'text', 'name' => 'columns', 'text' => 'strColumnNames'), + ), + 'options_text' => 'strCSVImportOptions', + ); /* We do not define function when plugin is just queried for information above */ - $replacements = array( - '\\n' => "\n", - '\\t' => "\t", - '\\r' => "\r", - ); - $csv_terminated = strtr($csv_terminated, $replacements); - $csv_enclosed = strtr($csv_enclosed, $replacements); - $csv_escaped = strtr($csv_escaped, $replacements); - $csv_new_line = strtr($csv_new_line, $replacements); + return; +} - if (strlen($csv_terminated) != 1) { - $message = sprintf($strInvalidCSVParameter, $strFieldsTerminatedBy); - $show_error_header = TRUE; - $error = TRUE; - } elseif (strlen($csv_enclosed) != 1) { - $message = sprintf($strInvalidCSVParameter, $strFieldsEnclosedBy); - $show_error_header = TRUE; - $error = TRUE; - } elseif (strlen($csv_escaped) != 1) { - $message = sprintf($strInvalidCSVParameter, $strFieldsEscapedBy); - $show_error_header = TRUE; - $error = TRUE; - } elseif (strlen($csv_new_line) != 1 && $csv_new_line != 'auto') { - $message = sprintf($strInvalidCSVParameter, $strLinesTerminatedBy); - $show_error_header = TRUE; - $error = TRUE; +$replacements = array( + '\\n' => "\n", + '\\t' => "\t", + '\\r' => "\r", + ); +$csv_terminated = strtr($csv_terminated, $replacements); +$csv_enclosed = strtr($csv_enclosed, $replacements); +$csv_escaped = strtr($csv_escaped, $replacements); +$csv_new_line = strtr($csv_new_line, $replacements); + +if (strlen($csv_terminated) != 1) { + $message = sprintf($strInvalidCSVParameter, $strFieldsTerminatedBy); + $show_error_header = TRUE; + $error = TRUE; +} elseif (strlen($csv_enclosed) != 1) { + $message = sprintf($strInvalidCSVParameter, $strFieldsEnclosedBy); + $show_error_header = TRUE; + $error = TRUE; +} elseif (strlen($csv_escaped) != 1) { + $message = sprintf($strInvalidCSVParameter, $strFieldsEscapedBy); + $show_error_header = TRUE; + $error = TRUE; +} elseif (strlen($csv_new_line) != 1 && $csv_new_line != 'auto') { + $message = sprintf($strInvalidCSVParameter, $strLinesTerminatedBy); + $show_error_header = TRUE; + $error = TRUE; +} + +$buffer = ''; +if (isset($csv_replace)) { + $sql_template = 'REPLACE'; +} else { + $sql_template = 'INSERT'; + if (isset($csv_ignore)) { + $sql_template .= ' IGNORE'; + } +} +$sql_template .= ' INTO ' . PMA_backquote($table); + +$tmp_fields = PMA_DBI_get_fields($db, $table); + +if (empty($csv_columns)) { + $fields = $tmp_fields; +} else { + $sql_template .= ' ('; + $fields = array(); + $tmp = split(',( ?)', $csv_columns); + foreach ($tmp as $key => $val) { + if (count($fields) > 0) { + $sql_template .= ', '; } - - $buffer = ''; - if (isset($csv_replace)) { - $sql_template = 'REPLACE'; - } else { - $sql_template = 'INSERT'; - if (isset($csv_ignore)) { - $sql_template .= ' IGNORE'; - } - } - $sql_template .= ' INTO ' . PMA_backquote($table); - - $tmp_fields = PMA_DBI_get_fields($db, $table); - - if (empty($csv_columns)) { - $fields = $tmp_fields; - } else { - $sql_template .= ' ('; - $fields = array(); - $tmp = split(',( ?)', $csv_columns); - foreach ($tmp as $key => $val) { - if (count($fields) > 0) { - $sql_template .= ', '; - } - $val = trim($val); - $found = FALSE; - foreach ($tmp_fields as $id => $field) { - if ($field['Field'] == $val) { - $found = TRUE; - break; - } - } - if (!$found) { - $message = sprintf($strInvalidColumn, $val); - $show_error_header = TRUE; - $error = TRUE; - break; - } - $fields[] = $field; - $sql_template .= PMA_backquote($val); - } - $sql_template .= ') '; - } - - $required_fields = count($fields); - - $sql_template .= ' VALUES ('; - - // Defaults for parser - $i = 0; - $len = 0; - $line = 1; - $lasti = -1; - $values = array(); - $csv_finish = FALSE; - - while (!($finished && $i >= $len) && !$error && !$timeout_passed) { - $data = PMA_importGetNextChunk(); - if ($data === FALSE) { - // subtract data we didn't handle yet and stop processing - $offset -= strlen($buffer); + $val = trim($val); + $found = FALSE; + foreach ($tmp_fields as $id => $field) { + if ($field['Field'] == $val) { + $found = TRUE; break; - } elseif ($data === TRUE) { - // Handle rest of buffer - } else { - // Append new data to buffer - $buffer .= $data; - unset($data); - // Do not parse string when we're not at the end and don't have new line inside - if (($csv_new_line == 'auto' && strpos($buffer, "\r") === FALSE && strpos($buffer, "\n") === FALSE) - || ($csv_new_line != 'auto' && strpos($buffer, $csv_new_line) === FALSE)) { - continue; - } } + } + if (!$found) { + $message = sprintf($strInvalidColumn, $val); + $show_error_header = TRUE; + $error = TRUE; + break; + } + $fields[] = $field; + $sql_template .= PMA_backquote($val); + } + $sql_template .= ') '; +} - // Current length of our buffer - $len = strlen($buffer); - // Currently parsed char - $ch = $buffer[$i]; - while ($i < $len) { - // Deadlock protection - if ($lasti == $i && $lastlen == $len) { - $message = sprintf($strInvalidCSVFormat, $line); - $show_error_header = TRUE; - $error = TRUE; - break; - } - $lasti = $i; - $lastlen = $len; +$required_fields = count($fields); - // This can happen with auto EOL and \r at the end of buffer - if (!$csv_finish) { - // Grab empty field - if ($ch == $csv_terminated) { - if ($i == $len - 1) { - break; - } - $values[] = ''; - $i++; - $ch = $buffer[$i]; - continue; - } +$sql_template .= ' VALUES ('; - // Grab one field - $fallbacki = $i; - if ($ch == $csv_enclosed) { - if ($i == $len - 1) { - break; - } - $need_end = TRUE; - $i++; - $ch = $buffer[$i]; - } else { - $need_end = FALSE; - } - $fail = FALSE; - $value = ''; - while (($need_end && $ch != $csv_enclosed) - || (!$need_end && !($ch == $csv_terminated - || $ch == $csv_new_line || ($csv_new_line == 'auto' - && ($ch == "\r" || $ch == "\n"))))) { - if ($ch == $csv_escaped) { - if ($i == $len - 1) { - $fail = TRUE; - break; - } - $i++; - $ch = $buffer[$i]; - } - $value .= $ch; - if ($i == $len - 1) { - if (!$finished) { - $fail = TRUE; - } - break; - } - $i++; - $ch = $buffer[$i]; - } +// Defaults for parser +$i = 0; +$len = 0; +$line = 1; +$lasti = -1; +$values = array(); +$csv_finish = FALSE; - // unquoted NULL string - if (false === $need_end && $value === 'NULL') { - $value = null; - } +while (!($finished && $i >= $len) && !$error && !$timeout_passed) { + $data = PMA_importGetNextChunk(); + if ($data === FALSE) { + // subtract data we didn't handle yet and stop processing + $offset -= strlen($buffer); + break; + } elseif ($data === TRUE) { + // Handle rest of buffer + } else { + // Append new data to buffer + $buffer .= $data; + unset($data); + // Do not parse string when we're not at the end and don't have new line inside + if (($csv_new_line == 'auto' && strpos($buffer, "\r") === FALSE && strpos($buffer, "\n") === FALSE) + || ($csv_new_line != 'auto' && strpos($buffer, $csv_new_line) === FALSE)) { + continue; + } + } - if ($fail) { - $i = $fallbacki; - $ch = $buffer[$i]; - break; - } - // Need to strip trailing enclosing char? - if ($need_end && $ch == $csv_enclosed) { - if ($finished && $i == $len - 1) { - $ch = NULL; - } elseif ($i == $len - 1) { - $i = $fallbacki; - $ch = $buffer[$i]; - break; - } else { - $i++; - $ch = $buffer[$i]; - } - } - // Are we at the end? - if ($ch == $csv_new_line || ($csv_new_line == 'auto' && ($ch == "\r" || $ch == "\n")) || ($finished && $i == $len - 1)) { - $csv_finish = TRUE; - } - // Go to next char - if ($ch == $csv_terminated) { - if ($i == $len - 1) { - $i = $fallbacki; - $ch = $buffer[$i]; - break; - } - $i++; - $ch = $buffer[$i]; - } - // If everything went okay, store value - $values[] = $value; - } - - // End of line - if ($csv_finish || $ch == $csv_new_line || ($csv_new_line == 'auto' && ($ch == "\r" || $ch == "\n"))) { - if ($csv_new_line == 'auto' && $ch == "\r") { // Handle "\r\n" - if ($i >= ($len - 2) && !$finished) { - break; // We need more data to decide new line - } - if ($buffer[$i + 1] == "\n") { - $i++; - } - } - // We didn't parse value till the end of line, so there was empty one - if (!$csv_finish) { - $values[] = ''; - } - // Do we have correct count of values? - if (count($values) != $required_fields) { - - // Hack for excel - if ($values[count($values) - 1] == ';') { - unset($values[count($values) - 1]); - } else { - $message = sprintf($strInvalidCSVFieldCount, $line); - $show_error_header = TRUE; - $error = TRUE; - break; - } - } - - $first = TRUE; - $sql = $sql_template; - foreach ($values as $key => $val) { - if (!$first) { - $sql .= ', '; - } - if ($val === null) { - $sql .= 'NULL'; - } else { - $sql .= '\'' . addslashes($val) . '\''; - } - - $first = FALSE; - } - $sql .= ')'; - - /** - * @todo maybe we could add original line to verbose SQL in comment - */ - PMA_importRunQuery($sql, $sql); - $line++; - $csv_finish = FALSE; - $values = array(); - $buffer = substr($buffer, $i + 1); - $len = strlen($buffer); - $i = 0; - $lasti = -1; - $ch = $buffer[0]; - } - } // End of parser loop - } // End of import loop - - // Commit any possible data in buffers - PMA_importRunQuery(); - - if (count($values) != 0 && !$error) { + // Current length of our buffer + $len = strlen($buffer); + // Currently parsed char + $ch = $buffer[$i]; + while ($i < $len) { + // Deadlock protection + if ($lasti == $i && $lastlen == $len) { $message = sprintf($strInvalidCSVFormat, $line); $show_error_header = TRUE; $error = TRUE; + break; } - } + $lasti = $i; + $lastlen = $len; + + // This can happen with auto EOL and \r at the end of buffer + if (!$csv_finish) { + // Grab empty field + if ($ch == $csv_terminated) { + if ($i == $len - 1) { + break; + } + $values[] = ''; + $i++; + $ch = $buffer[$i]; + continue; + } + + // Grab one field + $fallbacki = $i; + if ($ch == $csv_enclosed) { + if ($i == $len - 1) { + break; + } + $need_end = TRUE; + $i++; + $ch = $buffer[$i]; + } else { + $need_end = FALSE; + } + $fail = FALSE; + $value = ''; + while (($need_end && $ch != $csv_enclosed) + || (!$need_end && !($ch == $csv_terminated + || $ch == $csv_new_line || ($csv_new_line == 'auto' + && ($ch == "\r" || $ch == "\n"))))) { + if ($ch == $csv_escaped) { + if ($i == $len - 1) { + $fail = TRUE; + break; + } + $i++; + $ch = $buffer[$i]; + } + $value .= $ch; + if ($i == $len - 1) { + if (!$finished) { + $fail = TRUE; + } + break; + } + $i++; + $ch = $buffer[$i]; + } + + // unquoted NULL string + if (false === $need_end && $value === 'NULL') { + $value = null; + } + + if ($fail) { + $i = $fallbacki; + $ch = $buffer[$i]; + break; + } + // Need to strip trailing enclosing char? + if ($need_end && $ch == $csv_enclosed) { + if ($finished && $i == $len - 1) { + $ch = NULL; + } elseif ($i == $len - 1) { + $i = $fallbacki; + $ch = $buffer[$i]; + break; + } else { + $i++; + $ch = $buffer[$i]; + } + } + // Are we at the end? + if ($ch == $csv_new_line || ($csv_new_line == 'auto' && ($ch == "\r" || $ch == "\n")) || ($finished && $i == $len - 1)) { + $csv_finish = TRUE; + } + // Go to next char + if ($ch == $csv_terminated) { + if ($i == $len - 1) { + $i = $fallbacki; + $ch = $buffer[$i]; + break; + } + $i++; + $ch = $buffer[$i]; + } + // If everything went okay, store value + $values[] = $value; + } + + // End of line + if ($csv_finish || $ch == $csv_new_line || ($csv_new_line == 'auto' && ($ch == "\r" || $ch == "\n"))) { + if ($csv_new_line == 'auto' && $ch == "\r") { // Handle "\r\n" + if ($i >= ($len - 2) && !$finished) { + break; // We need more data to decide new line + } + if ($buffer[$i + 1] == "\n") { + $i++; + } + } + // We didn't parse value till the end of line, so there was empty one + if (!$csv_finish) { + $values[] = ''; + } + // Do we have correct count of values? + if (count($values) != $required_fields) { + + // Hack for excel + if ($values[count($values) - 1] == ';') { + unset($values[count($values) - 1]); + } else { + $message = sprintf($strInvalidCSVFieldCount, $line); + $show_error_header = TRUE; + $error = TRUE; + break; + } + } + + $first = TRUE; + $sql = $sql_template; + foreach ($values as $key => $val) { + if (!$first) { + $sql .= ', '; + } + if ($val === null) { + $sql .= 'NULL'; + } else { + $sql .= '\'' . addslashes($val) . '\''; + } + + $first = FALSE; + } + $sql .= ')'; + + /** + * @todo maybe we could add original line to verbose SQL in comment + */ + PMA_importRunQuery($sql, $sql); + $line++; + $csv_finish = FALSE; + $values = array(); + $buffer = substr($buffer, $i + 1); + $len = strlen($buffer); + $i = 0; + $lasti = -1; + $ch = $buffer[0]; + } + } // End of parser loop +} // End of import loop + +// Commit any possible data in buffers +PMA_importRunQuery(); + +if (count($values) != 0 && !$error) { + $message = sprintf($strInvalidCSVFormat, $line); + $show_error_header = TRUE; + $error = TRUE; } ?> diff --git a/libraries/import/docsql.php b/libraries/import/docsql.php index de91d0360..c12556ec2 100644 --- a/libraries/import/docsql.php +++ b/libraries/import/docsql.php @@ -6,76 +6,82 @@ * @version $Id$ */ +$cfgRelation = PMA_getRelationsParam(); + /** * We need relations enabled and we work only on database */ -if ($plugin_param == 'database' && $GLOBALS['num_tables'] > 0 && $GLOBALS['cfgRelation']['relwork'] && $GLOBALS['cfgRelation']['commwork']) { - if (isset($plugin_list)) { - $plugin_list['docsql'] = array( // set name of your plugin - 'text' => 'strDocSQL', // text to be displayed as choice - 'extension' => '', // extension this plugin can handle - 'options' => array( // array of options for your plugin (optional) - array('type' => 'text', 'name' => 'table', 'text' => 'strTableName'), - ), - 'options_text' => 'strDocSQLOptions', // text to describe plugin options (must be set if options are used) - ); - } else { - /* We do not define function when plugin is just queried for information above */ - $tab = $_POST['docsql_table']; - $buffer = ''; - /* Read whole buffer, we except it is small enough */ - while (!$finished && !$error && !$timeout_passed) { - $data = PMA_importGetNextChunk(); - if ($data === FALSE) { - // subtract data we didn't handle yet and stop processing - break; - } elseif ($data === TRUE) { - // nothing to read - break; - } else { - // Append new data to buffer - $buffer .= $data; - } - } // End of import loop - /* Process the data */ - if ($data === TRUE && !$error && !$timeout_passed) { - $buffer = str_replace("\r\n", "\n", $buffer); - $buffer = str_replace("\r", "\n", $buffer); - $lines = explode("\n", $buffer); - foreach ($lines AS $lkey => $line) { - //echo '
' . $line . '
'; - $inf = explode('|', $line); - if (!empty($inf[1]) && strlen(trim($inf[1])) > 0) { - $qry = ' - INSERT INTO - ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['column_info']) . ' - ( db_name, table_name, column_name, ' . PMA_backquote('comment') . ' ) - VALUES ( - \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\', - \'' . PMA_sqlAddslashes(trim($tab)) . '\', - \'' . PMA_sqlAddslashes(trim($inf[0])) . '\', - \'' . PMA_sqlAddslashes(trim($inf[1])) . '\')'; - PMA_importRunQuery($qry, $qry . '-- ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]), true); - } // end inf[1] exists - if (!empty($inf[2]) && strlen(trim($inf[2])) > 0) { - $for = explode('->', $inf[2]); - $qry = ' - INSERT INTO - ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation']) . ' - ( master_db, master_table, master_field, foreign_db, foreign_table, foreign_field) - VALUES ( - \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\', - \'' . PMA_sqlAddslashes(trim($tab)) . '\', - \'' . PMA_sqlAddslashes(trim($inf[0])) . '\', - \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\', - \'' . PMA_sqlAddslashes(trim($for[0])) . '\', - \'' . PMA_sqlAddslashes(trim($for[1])) . '\')'; - PMA_importRunQuery($qry, $qry . '-- ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]) . '(' . htmlspecialchars($inf[2]) . ')', true); - } // end inf[2] exists - } // End lines loop - } // End import - // Commit any possible data in buffers - PMA_importRunQuery(); - } +if ($plugin_param !== 'database' || $GLOBALS['num_tables'] < 1 + || ! $cfgRelation['relwork'] || ! $cfgRelation['commwork']) { + return; } + +if (isset($plugin_list)) { + $plugin_list['docsql'] = array( // set name of your plugin + 'text' => 'strDocSQL', // text to be displayed as choice + 'extension' => '', // extension this plugin can handle + 'options' => array( // array of options for your plugin (optional) + array('type' => 'text', 'name' => 'table', 'text' => 'strTableName'), + ), + 'options_text' => 'strDocSQLOptions', // text to describe plugin options (must be set if options are used) + ); + /* We do not define function when plugin is just queried for information above */ + return; +} + +$tab = $_POST['docsql_table']; +$buffer = ''; +/* Read whole buffer, we except it is small enough */ +while (!$finished && !$error && !$timeout_passed) { + $data = PMA_importGetNextChunk(); + if ($data === FALSE) { + // subtract data we didn't handle yet and stop processing + break; + } elseif ($data === TRUE) { + // nothing to read + break; + } else { + // Append new data to buffer + $buffer .= $data; + } +} // End of import loop +/* Process the data */ +if ($data === TRUE && !$error && !$timeout_passed) { + $buffer = str_replace("\r\n", "\n", $buffer); + $buffer = str_replace("\r", "\n", $buffer); + $lines = explode("\n", $buffer); + foreach ($lines AS $lkey => $line) { + //echo '' . $line . '
'; + $inf = explode('|', $line); + if (!empty($inf[1]) && strlen(trim($inf[1])) > 0) { + $qry = ' + INSERT INTO + ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . ' + ( db_name, table_name, column_name, ' . PMA_backquote('comment') . ' ) + VALUES ( + \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\', + \'' . PMA_sqlAddslashes(trim($tab)) . '\', + \'' . PMA_sqlAddslashes(trim($inf[0])) . '\', + \'' . PMA_sqlAddslashes(trim($inf[1])) . '\')'; + PMA_importRunQuery($qry, $qry . '-- ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]), true); + } // end inf[1] exists + if (!empty($inf[2]) && strlen(trim($inf[2])) > 0) { + $for = explode('->', $inf[2]); + $qry = ' + INSERT INTO + ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation']) . ' + ( master_db, master_table, master_field, foreign_db, foreign_table, foreign_field) + VALUES ( + \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\', + \'' . PMA_sqlAddslashes(trim($tab)) . '\', + \'' . PMA_sqlAddslashes(trim($inf[0])) . '\', + \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\', + \'' . PMA_sqlAddslashes(trim($for[0])) . '\', + \'' . PMA_sqlAddslashes(trim($for[1])) . '\')'; + PMA_importRunQuery($qry, $qry . '-- ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]) . '(' . htmlspecialchars($inf[2]) . ')', true); + } // end inf[2] exists + } // End lines loop +} // End import +// Commit any possible data in buffers +PMA_importRunQuery(); ?> diff --git a/libraries/import/ldi.php b/libraries/import/ldi.php index e9db16106..169fbb77f 100644 --- a/libraries/import/ldi.php +++ b/libraries/import/ldi.php @@ -9,99 +9,102 @@ /** * */ -if ($plugin_param == 'table') { - if (isset($plugin_list)) { - if ($GLOBALS['cfg']['Import']['ldi_local_option'] == 'auto') { - $GLOBALS['cfg']['Import']['ldi_local_option'] = FALSE; - - if (PMA_MYSQL_INT_VERSION < 32349) { - $GLOBALS['cfg']['Import']['ldi_local_option'] = TRUE; - } - - if (PMA_MYSQL_INT_VERSION > 40003) { - $result = PMA_DBI_try_query('SHOW VARIABLES LIKE \'local\\_infile\';'); - if ($result != FALSE && PMA_DBI_num_rows($result) > 0) { - $tmp = PMA_DBI_fetch_row($result); - if ($tmp[1] == 'ON') { - $GLOBALS['cfg']['Import']['ldi_local_option'] = TRUE; - } - } - PMA_DBI_free_result($result); - unset($result); - } - } - $plugin_list['ldi'] = array( - 'text' => 'strLDI', - 'extension' => 'ldi', // This is nonsense, however we want to default to our parser for csv - 'options' => array( - array('type' => 'bool', 'name' => 'replace', 'text' => 'strReplaceTable'), - array('type' => 'bool', 'name' => 'ignore', 'text' => 'strIgnoreDuplicates'), - array('type' => 'text', 'name' => 'terminated', 'text' => 'strFieldsTerminatedBy', 'size' => 2, 'len' => 2), - array('type' => 'text', 'name' => 'enclosed', 'text' => 'strFieldsEnclosedBy', 'size' => 2, 'len' => 2), - array('type' => 'text', 'name' => 'escaped', 'text' => 'strFieldsEscapedBy', 'size' => 2, 'len' => 2), - array('type' => 'text', 'name' => 'new_line', 'text' => 'strLinesTerminatedBy', 'size' => 2), - array('type' => 'text', 'name' => 'columns', 'text' => 'strColumnNames'), - array('type' => 'bool', 'name' => 'local_option', 'text' => 'strLDILocal'), - ), - 'options_text' => 'strLDIImportOptions', - ); - } else { - /* We do not define function when plugin is just queried for information above */ - if ($import_file == 'none' || $compression != 'none' || $charset_conversion) { - // We handle only some kind of data! - $message = $strInvalidLDIImport; - $show_error_header = TRUE; - $error = TRUE; - } else { - $sql = 'LOAD DATA'; - if (isset($ldi_local_option)) { - $sql .= ' LOCAL'; - } - $sql .= ' INFILE \'' . PMA_sqlAddslashes($import_file) . '\''; - if (isset($ldi_replace)) { - $sql .= ' REPLACE'; - } elseif (isset($ldi_ignore)) { - $sql .= ' IGNORE'; - } - $sql .= ' INTO TABLE ' . PMA_backquote($table); - - if (strlen($ldi_terminated) > 0) { - $sql .= ' FIELDS TERMINATED BY \'' . $ldi_terminated . '\''; - } - if (strlen($ldi_enclosed) > 0) { - $sql .= ' ENCLOSED BY \'' . PMA_sqlAddslashes($ldi_enclosed) . '\''; - } - if (strlen($ldi_escaped) > 0) { - $sql .= ' ESCAPED BY \'' . PMA_sqlAddslashes($ldi_escaped) . '\''; - } - if (strlen($ldi_new_line) > 0){ - if ($ldi_new_line == 'auto') { - $ldi_new_line = PMA_whichCrlf() == "\n" ? '\n' : '\r\n'; - } - $sql .= ' LINES TERMINATED BY \'' . $ldi_new_line . '\''; - } - if ($skip_queries > 0) { - $sql .= ' IGNORE ' . $skip_queries . ' LINES'; - $skip_queries = 0; - } - if (strlen($ldi_columns) > 0) { - $sql .= ' ('; - $tmp = split(',( ?)', $ldi_columns); - $cnt_tmp = count($tmp); - for ($i = 0; $i < $cnt_tmp; $i++) { - if ($i > 0) { - $sql .= ', '; - } - $sql .= PMA_backquote(trim($tmp[$i])); - } // end for - $sql .= ')'; - } - - PMA_importRunQuery($sql, $sql); - PMA_importRunQuery(); - $finished = TRUE; - } - - } +if ($plugin_param !== 'table') { + return; } + +if (isset($plugin_list)) { + if ($GLOBALS['cfg']['Import']['ldi_local_option'] == 'auto') { + $GLOBALS['cfg']['Import']['ldi_local_option'] = FALSE; + + if (PMA_MYSQL_INT_VERSION < 32349) { + $GLOBALS['cfg']['Import']['ldi_local_option'] = TRUE; + } + + if (PMA_MYSQL_INT_VERSION > 40003) { + $result = PMA_DBI_try_query('SHOW VARIABLES LIKE \'local\\_infile\';'); + if ($result != FALSE && PMA_DBI_num_rows($result) > 0) { + $tmp = PMA_DBI_fetch_row($result); + if ($tmp[1] == 'ON') { + $GLOBALS['cfg']['Import']['ldi_local_option'] = TRUE; + } + } + PMA_DBI_free_result($result); + unset($result); + } + } + $plugin_list['ldi'] = array( + 'text' => 'strLDI', + 'extension' => 'ldi', // This is nonsense, however we want to default to our parser for csv + 'options' => array( + array('type' => 'bool', 'name' => 'replace', 'text' => 'strReplaceTable'), + array('type' => 'bool', 'name' => 'ignore', 'text' => 'strIgnoreDuplicates'), + array('type' => 'text', 'name' => 'terminated', 'text' => 'strFieldsTerminatedBy', 'size' => 2, 'len' => 2), + array('type' => 'text', 'name' => 'enclosed', 'text' => 'strFieldsEnclosedBy', 'size' => 2, 'len' => 2), + array('type' => 'text', 'name' => 'escaped', 'text' => 'strFieldsEscapedBy', 'size' => 2, 'len' => 2), + array('type' => 'text', 'name' => 'new_line', 'text' => 'strLinesTerminatedBy', 'size' => 2), + array('type' => 'text', 'name' => 'columns', 'text' => 'strColumnNames'), + array('type' => 'bool', 'name' => 'local_option', 'text' => 'strLDILocal'), + ), + 'options_text' => 'strLDIImportOptions', + ); + /* We do not define function when plugin is just queried for information above */ + return; +} + +if ($import_file == 'none' || $compression != 'none' || $charset_conversion) { + // We handle only some kind of data! + $message = $strInvalidLDIImport; + $show_error_header = TRUE; + $error = TRUE; + return; +} + +$sql = 'LOAD DATA'; +if (isset($ldi_local_option)) { + $sql .= ' LOCAL'; +} +$sql .= ' INFILE \'' . PMA_sqlAddslashes($import_file) . '\''; +if (isset($ldi_replace)) { + $sql .= ' REPLACE'; +} elseif (isset($ldi_ignore)) { + $sql .= ' IGNORE'; +} +$sql .= ' INTO TABLE ' . PMA_backquote($table); + +if (strlen($ldi_terminated) > 0) { + $sql .= ' FIELDS TERMINATED BY \'' . $ldi_terminated . '\''; +} +if (strlen($ldi_enclosed) > 0) { + $sql .= ' ENCLOSED BY \'' . PMA_sqlAddslashes($ldi_enclosed) . '\''; +} +if (strlen($ldi_escaped) > 0) { + $sql .= ' ESCAPED BY \'' . PMA_sqlAddslashes($ldi_escaped) . '\''; +} +if (strlen($ldi_new_line) > 0){ + if ($ldi_new_line == 'auto') { + $ldi_new_line = PMA_whichCrlf() == "\n" ? '\n' : '\r\n'; + } + $sql .= ' LINES TERMINATED BY \'' . $ldi_new_line . '\''; +} +if ($skip_queries > 0) { + $sql .= ' IGNORE ' . $skip_queries . ' LINES'; + $skip_queries = 0; +} +if (strlen($ldi_columns) > 0) { + $sql .= ' ('; + $tmp = split(',( ?)', $ldi_columns); + $cnt_tmp = count($tmp); + for ($i = 0; $i < $cnt_tmp; $i++) { + if ($i > 0) { + $sql .= ', '; + } + $sql .= PMA_backquote(trim($tmp[$i])); + } // end for + $sql .= ')'; +} + +PMA_importRunQuery($sql, $sql); +PMA_importRunQuery(); +$finished = TRUE; ?> diff --git a/libraries/import/sql.php b/libraries/import/sql.php index 3c33b9860..2ee1b66f9 100644 --- a/libraries/import/sql.php +++ b/libraries/import/sql.php @@ -25,240 +25,242 @@ if (isset($plugin_list)) { array('type' => 'select', 'name' => 'compatibility', 'text' => 'strSQLCompatibility', 'values' => $values, 'doc' => array('manual_MySQL_Database_Administration', 'Server_SQL_mode')) ); } -} else { -/* We do not define function when plugin is just queried for information above */ - $buffer = ''; - // Defaults for parser - $sql = ''; - $start_pos = 0; - $i = 0; - $len= 0; - if (isset($_POST['sql_delimiter'])) { - $sql_delimiter = $_POST['sql_delimiter']; - } else { - $sql_delimiter = ';'; - } - // Handle compatibility option - if (isset($_REQUEST['sql_compatibility'])) { - PMA_DBI_try_query('SET SQL_MODE="' . $_REQUEST['sql_compatibility'] . '"'); + /* We do not define function when plugin is just queried for information above */ + return; +} + +$buffer = ''; +// Defaults for parser +$sql = ''; +$start_pos = 0; +$i = 0; +$len= 0; +if (isset($_POST['sql_delimiter'])) { + $sql_delimiter = $_POST['sql_delimiter']; +} else { + $sql_delimiter = ';'; +} + +// Handle compatibility option +if (isset($_REQUEST['sql_compatibility'])) { + PMA_DBI_try_query('SET SQL_MODE="' . $_REQUEST['sql_compatibility'] . '"'); +} +while (!($finished && $i >= $len) && !$error && !$timeout_passed) { + $data = PMA_importGetNextChunk(); + if ($data === FALSE) { + // subtract data we didn't handle yet and stop processing + $offset -= strlen($buffer); + break; + } elseif ($data === TRUE) { + // Handle rest of buffer + } else { + // Append new data to buffer + $buffer .= $data; + // free memory + unset($data); + // Do not parse string when we're not at the end and don't have ; inside + if ((strpos($buffer, $sql_delimiter, $i) === FALSE) && !$finished) { + continue; + } } - while (!($finished && $i >= $len) && !$error && !$timeout_passed) { - $data = PMA_importGetNextChunk(); - if ($data === FALSE) { - // subtract data we didn't handle yet and stop processing - $offset -= strlen($buffer); - break; - } elseif ($data === TRUE) { - // Handle rest of buffer + // Current length of our buffer + $len = strlen($buffer); + // Grab some SQL queries out of it + while ($i < $len) { + $found_delimiter = false; + // Find first interesting character, several strpos seem to be faster than simple loop in php: + //while (($i < $len) && (strpos('\'";#-/', $buffer[$i]) === FALSE)) $i++; + //if ($i == $len) break; + $oi = $i; + $p1 = strpos($buffer, '\'', $i); + if ($p1 === FALSE) { + $p1 = 2147483647; + } + $p2 = strpos($buffer, '"', $i); + if ($p2 === FALSE) { + $p2 = 2147483647; + } + $p3 = strpos($buffer, $sql_delimiter, $i); + if ($p3 === FALSE) { + $p3 = 2147483647; } else { - // Append new data to buffer - $buffer .= $data; - // free memory - unset($data); - // Do not parse string when we're not at the end and don't have ; inside - if ((strpos($buffer, $sql_delimiter, $i) === FALSE) && !$finished) { + $found_delimiter = true; + } + $p4 = strpos($buffer, '#', $i); + if ($p4 === FALSE) { + $p4 = 2147483647; + } + $p5 = strpos($buffer, '--', $i); + if ($p5 === FALSE || $p5 >= ($len - 2) || $buffer[$p5 + 2] > ' ') { + $p5 = 2147483647; + } + $p6 = strpos($buffer, '/*', $i); + if ($p6 === FALSE) { + $p6 = 2147483647; + } + $p7 = strpos($buffer, '`', $i); + if ($p7 === FALSE) { + $p7 = 2147483647; + } + $p8 = strpos($buffer, 'DELIMITER', $i); + if ($p8 === FALSE || $p8 >= ($len - 11) || $buffer[$p8 + 9] > ' ') { + $p8 = 2147483647; + } + $i = min ($p1, $p2, $p3, $p4, $p5, $p6, $p7, $p8); + unset($p1, $p2, $p3, $p4, $p5, $p6, $p7, $p8); + if ($i == 2147483647) { + $i = $oi; + if (!$finished) { + break; + } + // at the end there might be some whitespace... + if (trim($buffer) == '') { + $buffer = ''; + $len = 0; + break; + } + // We hit end of query, go there! + $i = strlen($buffer) - 1; + } + + // Grab current character + $ch = $buffer[$i]; + + // Quotes + if (strpos('\'"`', $ch) !== FALSE) { + $quote = $ch; + $endq = FALSE; + while (!$endq) { + // Find next quote + $pos = strpos($buffer, $quote, $i + 1); + // No quote? Too short string + if ($pos === FALSE) { + // We hit end of string => unclosed quote, but we handle it as end of query + if ($finished) { + $endq = TRUE; + $i = $len - 1; + } + break; + } + // Was not the quote escaped? + $j = $pos - 1; + while ($buffer[$j] == '\\') $j--; + // Even count means it was not escaped + $endq = (((($pos - 1) - $j) % 2) == 0); + // Skip the string + $i = $pos; + } + if (!$endq) { + break; + } + $i++; + // Aren't we at the end? + if ($finished && $i == $len) { + $i--; + } else { continue; } } - // Current length of our buffer - $len = strlen($buffer); - // Grab some SQL queries out of it - while ($i < $len) { - $found_delimiter = false; - // Find first interesting character, several strpos seem to be faster than simple loop in php: - //while (($i < $len) && (strpos('\'";#-/', $buffer[$i]) === FALSE)) $i++; - //if ($i == $len) break; - $oi = $i; - $p1 = strpos($buffer, '\'', $i); - if ($p1 === FALSE) { - $p1 = 2147483647; - } - $p2 = strpos($buffer, '"', $i); - if ($p2 === FALSE) { - $p2 = 2147483647; - } - $p3 = strpos($buffer, $sql_delimiter, $i); - if ($p3 === FALSE) { - $p3 = 2147483647; - } else { - $found_delimiter = true; - } - $p4 = strpos($buffer, '#', $i); - if ($p4 === FALSE) { - $p4 = 2147483647; - } - $p5 = strpos($buffer, '--', $i); - if ($p5 === FALSE || $p5 >= ($len - 2) || $buffer[$p5 + 2] > ' ') { - $p5 = 2147483647; - } - $p6 = strpos($buffer, '/*', $i); - if ($p6 === FALSE) { - $p6 = 2147483647; - } - $p7 = strpos($buffer, '`', $i); - if ($p7 === FALSE) { - $p7 = 2147483647; - } - $p8 = strpos($buffer, 'DELIMITER', $i); - if ($p8 === FALSE || $p8 >= ($len - 11) || $buffer[$p8 + 9] > ' ') { - $p8 = 2147483647; - } - $i = min ($p1, $p2, $p3, $p4, $p5, $p6, $p7, $p8); - unset($p1, $p2, $p3, $p4, $p5, $p6, $p7, $p8); - if ($i == 2147483647) { - $i = $oi; - if (!$finished) { - break; - } - // at the end there might be some whitespace... - if (trim($buffer) == '') { - $buffer = ''; - $len = 0; - break; - } - // We hit end of query, go there! - $i = strlen($buffer) - 1; - } - // Grab current character - $ch = $buffer[$i]; + // Not enough data to decide + if ((($i == ($len - 1) && ($ch == '-' || $ch == '/')) + || ($i == ($len - 2) && (($ch == '-' && $buffer[$i + 1] == '-') + || ($ch == '/' && $buffer[$i + 1] == '*')))) && !$finished) { + break; + } - // Quotes - if (strpos('\'"`', $ch) !== FALSE) { - $quote = $ch; - $endq = FALSE; - while (!$endq) { - // Find next quote - $pos = strpos($buffer, $quote, $i + 1); - // No quote? Too short string - if ($pos === FALSE) { - // We hit end of string => unclosed quote, but we handle it as end of query - if ($finished) { - $endq = TRUE; - $i = $len - 1; - } - break; - } - // Was not the quote escaped? - $j = $pos - 1; - while ($buffer[$j] == '\\') $j--; - // Even count means it was not escaped - $endq = (((($pos - 1) - $j) % 2) == 0); - // Skip the string - $i = $pos; - } - if (!$endq) { - break; - } - $i++; - // Aren't we at the end? - if ($finished && $i == $len) { - $i--; + // Comments + if ($ch == '#' + || ($i < ($len - 1) && $ch == '-' && $buffer[$i + 1] == '-' && (($i < ($len - 2) && $buffer[$i + 2] <= ' ') || ($i == ($len - 1) && $finished))) + || ($i < ($len - 1) && $ch == '/' && $buffer[$i + 1] == '*') + ) { + // Copy current string to SQL + if ($start_pos != $i) { + $sql .= substr($buffer, $start_pos, $i - $start_pos); + } + // Skip the rest + $j = $i; + $i = strpos($buffer, $ch == '/' ? '*/' : "\n", $i); + // didn't we hit end of string? + if ($i === FALSE) { + if ($finished) { + $i = $len - 1; } else { - continue; + break; } } - - // Not enough data to decide - if ((($i == ($len - 1) && ($ch == '-' || $ch == '/')) - || ($i == ($len - 2) && (($ch == '-' && $buffer[$i + 1] == '-') - || ($ch == '/' && $buffer[$i + 1] == '*')))) && !$finished) { - break; - } - - // Comments - if ($ch == '#' - || ($i < ($len - 1) && $ch == '-' && $buffer[$i + 1] == '-' && (($i < ($len - 2) && $buffer[$i + 2] <= ' ') || ($i == ($len - 1) && $finished))) - || ($i < ($len - 1) && $ch == '/' && $buffer[$i + 1] == '*') - ) { - // Copy current string to SQL - if ($start_pos != $i) { - $sql .= substr($buffer, $start_pos, $i - $start_pos); - } - // Skip the rest - $j = $i; - $i = strpos($buffer, $ch == '/' ? '*/' : "\n", $i); - // didn't we hit end of string? - if ($i === FALSE) { - if ($finished) { - $i = $len - 1; + // Skip * + if ($ch == '/') { + // Check for MySQL conditional comments and include them as-is + if ($buffer[$j + 2] == '!') { + $comment = substr($buffer, $j + 3, $i - $j - 3); + if (preg_match('/^[0-9]{5}/', $comment, $version)) { + if ($version[0] <= PMA_MYSQL_INT_VERSION) { + $sql .= substr($comment, 5); + } } else { - break; + $sql .= $comment; } } - // Skip * - if ($ch == '/') { - // Check for MySQL conditional comments and include them as-is - if ($buffer[$j + 2] == '!') { - $comment = substr($buffer, $j + 3, $i - $j - 3); - if (preg_match('/^[0-9]{5}/', $comment, $version)) { - if ($version[0] <= PMA_MYSQL_INT_VERSION) { - $sql .= substr($comment, 5); - } - } else { - $sql .= $comment; - } - } - $i++; - } - // Skip last char $i++; - // Next query part will start here + } + // Skip last char + $i++; + // Next query part will start here + $start_pos = $i; + // Aren't we at the end? + if ($i == $len) { + $i--; + } else { + continue; + } + } + // Change delimiter, if redefined, and skip it (don't send to server!) + if ((substr($buffer, $i, 9) == "DELIMITER") && ($buffer[$i + 9] <= ' ') && ($i<$len-11) && (!(strpos($buffer,"\n",$i+11)===FALSE) )) { + $new_line_pos = strpos($buffer, "\n", $i + 10); + $sql_delimiter = substr($buffer, $i+10, $new_line_pos - $i -10); + $i= $new_line_pos + 1; + // Next query part will start here + $start_pos = $i; + continue; + } + + // End of SQL + if ($found_delimiter || ($finished && ($i == $len - 1))) { + $tmp_sql = $sql; + if ($start_pos < $len) { + $length_to_grab = $i - $start_pos; + if (!$found_delimiter) { + $length_to_grab++; + } + $tmp_sql .= substr($buffer, $start_pos, $length_to_grab); + unset($length_to_grab); + } + // Do not try to execute empty SQL + if (!preg_match('/^([\s]*;)*$/', trim($tmp_sql))) { + $sql = $tmp_sql; + PMA_importRunQuery($sql, substr($buffer, 0, $i + strlen($sql_delimiter))); + $buffer = substr($buffer, $i + strlen($sql_delimiter)); + // Reset parser: + $len = strlen($buffer); + $sql = ''; + $i = 0; + $start_pos = 0; + // Any chance we will get a complete query? + //if ((strpos($buffer, ';') === FALSE) && !$finished) { + if ((strpos($buffer, $sql_delimiter) === FALSE) && !$finished) { + break; + } + } else { + $i++; $start_pos = $i; - // Aren't we at the end? - if ($i == $len) { - $i--; - } else { - continue; - } - } - // Change delimiter, if redefined, and skip it (don't send to server!) - if ((substr($buffer, $i, 9) == "DELIMITER") && ($buffer[$i + 9] <= ' ') && ($i<$len-11) && (!(strpos($buffer,"\n",$i+11)===FALSE) )) { - $new_line_pos = strpos($buffer, "\n", $i + 10); - $sql_delimiter = substr($buffer, $i+10, $new_line_pos - $i -10); - $i= $new_line_pos + 1; - // Next query part will start here - $start_pos = $i; - continue; } + } - // End of SQL - if ($found_delimiter || ($finished && ($i == $len - 1))) { - $tmp_sql = $sql; - if ($start_pos < $len) { - $length_to_grab = $i - $start_pos; - if (!$found_delimiter) { - $length_to_grab++; - } - $tmp_sql .= substr($buffer, $start_pos, $length_to_grab); - unset($length_to_grab); - } - // Do not try to execute empty SQL - if (!preg_match('/^([\s]*;)*$/', trim($tmp_sql))) { - $sql = $tmp_sql; - PMA_importRunQuery($sql, substr($buffer, 0, $i + strlen($sql_delimiter))); - $buffer = substr($buffer, $i + strlen($sql_delimiter)); - // Reset parser: - $len = strlen($buffer); - $sql = ''; - $i = 0; - $start_pos = 0; - // Any chance we will get a complete query? - //if ((strpos($buffer, ';') === FALSE) && !$finished) { - if ((strpos($buffer, $sql_delimiter) === FALSE) && !$finished) { - break; - } - } else { - $i++; - $start_pos = $i; - } - } - - } // End of parser loop - } // End of import loop - // Commit any possible data in buffers - PMA_importRunQuery('', substr($buffer, 0, $len)); - PMA_importRunQuery(); -} + } // End of parser loop +} // End of import loop +// Commit any possible data in buffers +PMA_importRunQuery('', substr($buffer, 0, $len)); +PMA_importRunQuery(); ?>