From 61640ee6d786e51ccf2f37a60194c39e1a68104f Mon Sep 17 00:00:00 2001 From: jakubvrana Date: Fri, 3 Jul 2009 09:16:50 +0000 Subject: [PATCH] Move common functions git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@789 7c3ca157-0c34-0410-bff1-cbf682f78f5c --- adminer/include/editing.inc.php | 100 --------------------------- adminer/include/export.inc.php | 9 --- adminer/include/functions.inc.php | 109 ++++++++++++++++++++++++++++++ editor/include/export.inc.php | 27 ++++++++ editor/index.php | 3 +- 5 files changed, 137 insertions(+), 111 deletions(-) create mode 100644 editor/include/export.inc.php diff --git a/adminer/include/editing.inc.php b/adminer/include/editing.inc.php index 4d992787..2c71c0c0 100644 --- a/adminer/include/editing.inc.php +++ b/adminer/include/editing.inc.php @@ -1,104 +1,4 @@ "; - if ($field["type"] == "enum") { - echo " " . (isset($_GET["select"]) ? ' ' : ""); - if ($field["null"] || isset($_GET["default"])) { - echo ' '; - } - if (!isset($_GET["default"])) { - echo ''; - } - preg_match_all("~'((?:[^']+|'')*)'~", $field["length"], $matches); - foreach ($matches[1] as $i => $val) { - $val = stripcslashes(str_replace("''", "'", $val)); - $checked = (is_int($value) ? $value == $i+1 : $value === $val); - echo ' '; - } - } else { - $first = ($field["null"] || isset($_GET["default"])) + isset($_GET["select"]); - $onchange = ($first ? ' onchange="var f = this.form[\'function[' . addcslashes($name, "\r\n'\\") . ']\']; if (' . $first . ' > f.selectedIndex) f.selectedIndex = ' . $first . ';"' : ''); - $options = array(""); - if (!isset($_GET["default"])) { - if (preg_match('~char|date|time~', $field["type"])) { - $options = (preg_match('~char~', $field["type"]) ? array("", "md5", "sha1", "password", "uuid") : array("", "now")); //! JavaScript for disabling maxlength - } - if (!isset($_GET["call"]) && (isset($_GET["select"]) || where($_GET))) { - // relative functions - if (preg_match('~int|float|double|decimal~', $field["type"])) { - $options = array("", "+", "-"); - } - if (preg_match('~date~', $field["type"])) { - $options[] = "+ interval"; - $options[] = "- interval"; - } - if (preg_match('~time~', $field["type"])) { - $options[] = "addtime"; - $options[] = "subtime"; - } - } - } - if ($field["null"] || isset($_GET["default"])) { - array_unshift($options, "NULL"); - } - echo (count($options) > 1 || isset($_GET["select"]) ? '' : " ") . ''; - if ($field["type"] == "set") { //! 64 bits - preg_match_all("~'((?:[^']+|'')*)'~", $field["length"], $matches); - foreach ($matches[1] as $i => $val) { - $val = stripcslashes(str_replace("''", "'", $val)); - $checked = (is_int($value) ? ($value >> $i) & 1 : in_array($val, explode(",", $value), true)); - echo ' '; - } - } elseif (strpos($field["type"], "text") !== false) { - echo ''; - } elseif (preg_match('~binary|blob~', $field["type"])) { - echo (ini_get("file_uploads") ? '' : lang('File uploads are disabled.') . ' '); - } else { - // int(3) is only a display hint - $maxlength = (!ereg('int', $field["type"]) && preg_match('~^([0-9]+)(,([0-9]+))?$~', $field["length"], $match) ? ($match[1] + ($match[3] ? 1 : 0) + ($match[2] && !$field["unsigned"] ? 1 : 0)) : ($types[$field["type"]] ? $types[$field["type"]] + ($field["unsigned"] ? 0 : 1) : 0)); - echo ''; - } - } -} - -function process_input($name, $field) { - global $dbh; - $idf = bracket_escape($name); - $function = $_POST["function"][$idf]; - $value = $_POST["fields"][$idf]; - if ($field["type"] == "enum" ? $value == -1 : $function == "orig") { - return false; - } elseif ($field["type"] == "enum" || $field["auto_increment"] ? !strlen($value) : $function == "NULL") { - return "NULL"; - } elseif ($field["type"] == "enum") { - return (isset($_GET["default"]) ? $dbh->quote($value) : intval($value)); - } elseif ($field["type"] == "set") { - return (isset($_GET["default"]) ? "'" . implode(",", array_map('escape_string', (array) $value)) . "'" : array_sum((array) $value)); - } elseif (preg_match('~binary|blob~', $field["type"])) { - $file = get_file($idf); - if (!is_string($file)) { - return false; //! report errors - } - return "_binary" . (is_string($file) ? $dbh->quote($file) : ""); - } elseif ($field["type"] == "timestamp" && $value == "CURRENT_TIMESTAMP") { - return $value; - } elseif (preg_match('~^(now|uuid)$~', $function)) { - return "$function()"; - } elseif (preg_match('~^[+-]$~', $function)) { - return idf_escape($name) . " $function " . $dbh->quote($value); - } elseif (preg_match('~^[+-] interval$~', $function)) { - return idf_escape($name) . " $function " . (preg_match("~^([0-9]+|'[0-9.: -]') [A-Z_]+$~i", $value) ? $value : $dbh->quote($value)); - } elseif (preg_match('~^(addtime|subtime)$~', $function)) { - return "$function(" . idf_escape($name) . ", " . $dbh->quote($value) . ")"; - } elseif (preg_match('~^(md5|sha1|password)$~', $function)) { - return "$function(" . $dbh->quote($value) . ")"; - } else { - return $dbh->quote($value); - } -} - function referencable_primary($self) { $return = array(); // table_name => field foreach (table_status_referencable() as $table_name => $table) { diff --git a/adminer/include/export.inc.php b/adminer/include/export.inc.php index ffaf7293..6042de32 100644 --- a/adminer/include/export.inc.php +++ b/adminer/include/export.inc.php @@ -1,13 +1,4 @@ $val) { - if (preg_match("~[\"\n,]~", $val) || (isset($val) && !strlen($val))) { - $row[$key] = '"' . str_replace('"', '""', $val) . '"'; - } - } - echo implode(",", $row) . "\n"; -} - function dump_table($table, $style, $is_view = false) { global $dbh; if ($_POST["format"] == "csv") { diff --git a/adminer/include/functions.inc.php b/adminer/include/functions.inc.php index 580a2ba4..d95ee7ed 100644 --- a/adminer/include/functions.inc.php +++ b/adminer/include/functions.inc.php @@ -258,3 +258,112 @@ function hidden_fields($process, $ignore = array()) { } } } + +function input($name, $field, $value) { + global $types; + $name = htmlspecialchars(bracket_escape($name)); + echo ""; + if ($field["type"] == "enum") { + echo " " . (isset($_GET["select"]) ? ' ' : ""); + if ($field["null"] || isset($_GET["default"])) { + echo ' '; + } + if (!isset($_GET["default"])) { + echo ''; + } + preg_match_all("~'((?:[^']+|'')*)'~", $field["length"], $matches); + foreach ($matches[1] as $i => $val) { + $val = stripcslashes(str_replace("''", "'", $val)); + $checked = (is_int($value) ? $value == $i+1 : $value === $val); + echo ' '; + } + } else { + $first = ($field["null"] || isset($_GET["default"])) + isset($_GET["select"]); + $onchange = ($first ? ' onchange="var f = this.form[\'function[' . addcslashes($name, "\r\n'\\") . ']\']; if (' . $first . ' > f.selectedIndex) f.selectedIndex = ' . $first . ';"' : ''); + $options = array(""); + if (!isset($_GET["default"])) { + if (preg_match('~char|date|time~', $field["type"])) { + $options = (preg_match('~char~', $field["type"]) ? array("", "md5", "sha1", "password", "uuid") : array("", "now")); //! JavaScript for disabling maxlength + } + if (!isset($_GET["call"]) && (isset($_GET["select"]) || where($_GET))) { + // relative functions + if (preg_match('~int|float|double|decimal~', $field["type"])) { + $options = array("", "+", "-"); + } + if (preg_match('~date~', $field["type"])) { + $options[] = "+ interval"; + $options[] = "- interval"; + } + if (preg_match('~time~', $field["type"])) { + $options[] = "addtime"; + $options[] = "subtime"; + } + } + } + if ($field["null"] || isset($_GET["default"])) { + array_unshift($options, "NULL"); + } + echo (count($options) > 1 || isset($_GET["select"]) ? '' : " ") . ''; + if ($field["type"] == "set") { //! 64 bits + preg_match_all("~'((?:[^']+|'')*)'~", $field["length"], $matches); + foreach ($matches[1] as $i => $val) { + $val = stripcslashes(str_replace("''", "'", $val)); + $checked = (is_int($value) ? ($value >> $i) & 1 : in_array($val, explode(",", $value), true)); + echo ' '; + } + } elseif (strpos($field["type"], "text") !== false) { + echo ''; + } elseif (preg_match('~binary|blob~', $field["type"])) { + echo (ini_get("file_uploads") ? '' : lang('File uploads are disabled.') . ' '); + } else { + // int(3) is only a display hint + $maxlength = (!ereg('int', $field["type"]) && preg_match('~^([0-9]+)(,([0-9]+))?$~', $field["length"], $match) ? ($match[1] + ($match[3] ? 1 : 0) + ($match[2] && !$field["unsigned"] ? 1 : 0)) : ($types[$field["type"]] ? $types[$field["type"]] + ($field["unsigned"] ? 0 : 1) : 0)); + echo ''; + } + } +} + +function process_input($name, $field) { + global $dbh; + $idf = bracket_escape($name); + $function = $_POST["function"][$idf]; + $value = $_POST["fields"][$idf]; + if ($field["type"] == "enum" ? $value == -1 : $function == "orig") { + return false; + } elseif ($field["type"] == "enum" || $field["auto_increment"] ? !strlen($value) : $function == "NULL") { + return "NULL"; + } elseif ($field["type"] == "enum") { + return (isset($_GET["default"]) ? $dbh->quote($value) : intval($value)); + } elseif ($field["type"] == "set") { + return (isset($_GET["default"]) ? "'" . implode(",", array_map('escape_string', (array) $value)) . "'" : array_sum((array) $value)); + } elseif (preg_match('~binary|blob~', $field["type"])) { + $file = get_file($idf); + if (!is_string($file)) { + return false; //! report errors + } + return "_binary" . (is_string($file) ? $dbh->quote($file) : ""); + } elseif ($field["type"] == "timestamp" && $value == "CURRENT_TIMESTAMP") { + return $value; + } elseif (preg_match('~^(now|uuid)$~', $function)) { + return "$function()"; + } elseif (preg_match('~^[+-]$~', $function)) { + return idf_escape($name) . " $function " . $dbh->quote($value); + } elseif (preg_match('~^[+-] interval$~', $function)) { + return idf_escape($name) . " $function " . (preg_match("~^([0-9]+|'[0-9.: -]') [A-Z_]+$~i", $value) ? $value : $dbh->quote($value)); + } elseif (preg_match('~^(addtime|subtime)$~', $function)) { + return "$function(" . idf_escape($name) . ", " . $dbh->quote($value) . ")"; + } elseif (preg_match('~^(md5|sha1|password)$~', $function)) { + return "$function(" . $dbh->quote($value) . ")"; + } else { + return $dbh->quote($value); + } +} + +function dump_csv($row) { + foreach ($row as $key => $val) { + if (preg_match("~[\"\n,]~", $val) || (isset($val) && !strlen($val))) { + $row[$key] = '"' . str_replace('"', '""', $val) . '"'; + } + } + echo implode(",", $row) . "\n"; +} diff --git a/editor/include/export.inc.php b/editor/include/export.inc.php new file mode 100644 index 00000000..53c7b369 --- /dev/null +++ b/editor/include/export.inc.php @@ -0,0 +1,27 @@ +query(($select ? $select : "SELECT * FROM " . idf_escape($table))); + if ($result) { + while ($row = $result->fetch_assoc()) { + dump_csv($row); + } + $result->free(); + } +} + +function dump_headers($identifier, $multi_table = false) { + $filename = (strlen($identifier) ? friendly_url($identifier) : "dump"); + $ext = "csv"; + header("Content-Type: text/csv; charset=utf-8"); + header("Content-Disposition: attachment; filename=$filename.$ext"); + return $ext; +} + +$dump_output = ""; +$dump_format = ""; diff --git a/editor/index.php b/editor/index.php index dcc5809f..33acb869 100644 --- a/editor/index.php +++ b/editor/index.php @@ -20,8 +20,7 @@ include "../adminer/include/pdo.inc.php"; include "../adminer/include/mysql.inc.php"; include "./include/auth.inc.php"; include "./include/connect.inc.php"; -include "../adminer/include/editing.inc.php"; -include "../adminer/include/export.inc.php"; +include "./include/export.inc.php"; $confirm = " onclick=\"return confirm('" . lang('Are you sure?') . "');\""; $error = "";