]> git.joonet.de Git - adminer.git/commitdiff
Move stuff to functions.inc.php
authorjakubvrana <jakubvrana@7c3ca157-0c34-0410-bff1-cbf682f78f5c>
Mon, 9 Jul 2007 14:45:11 +0000 (14:45 +0000)
committerjakubvrana <jakubvrana@7c3ca157-0c34-0410-bff1-cbf682f78f5c>
Mon, 9 Jul 2007 14:45:11 +0000 (14:45 +0000)
git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@80 7c3ca157-0c34-0410-bff1-cbf682f78f5c

edit.inc.php
functions.inc.php
sql.inc.php

index df229ef33f9953746ede9a5579f6b4ffb5b1e70b..621c0da261e3e5d17ff14bf9e40a9bcf79fc7882 100644 (file)
@@ -14,33 +14,10 @@ if ($_POST && !$error) {
        } else {
                $set = array();
                foreach ($fields as $name => $field) {
-                       $key = bracket_escape($name);
-                       $val = $_POST["fields"][$key];
-                       if (preg_match('~char|text|set|binary|blob~', $field["type"]) ? $_POST["null"][$key] : !strlen($val)) {
-                               $val = "NULL";
-                       } elseif ($field["type"] == "enum") {
-                               $val = (isset($_GET["default"]) && preg_match_all("~'((?:[^']*|'')+)'~", $field["length"], $matches) ? "'" . $matches[1][$val-1] . "'" : intval($val));
-                       } elseif ($field["type"] == "set") {
-                               if (!isset($_GET["default"])) {
-                                       $val = array_sum((array) $val);
-                               } else {
-                                       preg_match_all("~'((?:[^']*|'')+)'~", $field["length"], $matches);
-                                       $value = array();
-                                       foreach ((array) $val as $key => $v) {
-                                               $value[] = $matches[1][$key];
-                                       }
-                                       $val = "'" . implode(",", $value) . "'";
-                               }
-                       } elseif (preg_match('~binary|blob~', $field["type"])) {
-                               $file = get_file($key);
-                               if (!is_string($file) && !$field["null"]) {
-                                       continue; //! report errors, also empty $_POST - not only because of file upload
-                               }
-                               $val = "_binary'" . (is_string($file) ? mysql_real_escape_string($file) : "") . "'";
-                       } else {
-                               $val = "'" . mysql_real_escape_string($val) . "'";
+                       $val = process_input($name, $field);
+                       if ($val !== false) {
+                               $set[] = idf_escape($name) . (isset($_GET["default"]) ? ($val == "NULL" ? " DROP DEFAULT" : " SET DEFAULT $val") : " = $val");
                        }
-                       $set[] = idf_escape($name) . (isset($_GET["default"]) ? ($val == "NULL" ? " DROP DEFAULT" : " SET DEFAULT $val") : " = $val");
                }
                if (isset($_GET["default"])) {
                        $query = "ALTER TABLE " . idf_escape($_GET["edit"]) . " ALTER " . implode(", ALTER ", $set);
@@ -78,54 +55,27 @@ if ($_POST) {
        unset($data);
 }
 ?>
+
 <form action="" method="post" enctype="multipart/form-data">
-<table border="0" cellspacing="0" cellpadding="2">
 <?php
-$types = types();
-$save_possible = false;
+echo ($fields ? "<table border='0' cellspacing='0' cellpadding='2'>\n" : "");
 foreach ($fields as $name => $field) {
-       $save_possible = true;
        echo "<tr><th>" . htmlspecialchars($name) . "</th><td>";
-       $value = (isset($data) ? $data[$name] : $field["default"]);
-       $name = htmlspecialchars($_POST ? $name : bracket_escape($name));
-       if ($field["type"] == "enum") {
-               if (!isset($_GET["default"])) {
-                       echo '<input type="radio" name="fields[' . $name . ']" value="0"' . ($value == "0" ? ' checked="checked"' : '') . ' />';
-               }
-               preg_match_all("~'((?:[^']*|'')+)'~", $field["length"], $matches);
-               foreach ($matches[1] as $i => $val) {
-                       $id = "field-$name-" . ($i+1);
-                       $checked = (isset($data) ? $value == $i+1 : $val === $field["default"]);
-                       echo ' <input type="radio" name="fields[' . $name . ']" id="' . $id . '" value="' . ($i+1) . '"' . ($checked ? ' checked="checked"' : '') . ' /><label for="' . $id . '">' . htmlspecialchars(str_replace("''", "'", $val)) . '</label>';
-               }
-               if ($field["null"]) {
-                       $id = "field-$name-";
-                       echo '<input type="radio" name="fields[' . $name . ']" id="' . $id . '" value=""' . (strlen($value) ? '' : ' checked="checked"') . ' /><label for="' . $id . '">' . lang('NULL') . '</label> ';
-               }
-       } elseif ($field["type"] == "set") { //! 64 bits
-               preg_match_all("~'((?:[^']*|'')+)'~", $field["length"], $matches);
-               foreach ($matches[1] as $i => $val) {
-                       $id = "$name-" . ($i+1);
-                       $checked = (isset($data) ? ($value >> $i) & 1 : in_array(str_replace("''", "'", $val), explode(",", $field["default"]), true));
-                       echo ' <input type="checkbox" name="fields[' . $name . '][' . $i . ']" id="' . $id . '" value="' . (1 << $i) . '"' . ($checked ? ' checked="checked"' : '') . ' /><label for="' . $id . '">' . htmlspecialchars(str_replace("''", "'", $val)) . '</label>';
-               }
-       } elseif (strpos($field["type"], "text") !== false) {
-               echo '<textarea name="fields[' . $name . ']" cols="50" rows="12">' . htmlspecialchars($value) . '</textarea>';
-       } elseif (preg_match('~binary|blob~', $field["type"])) {
-               echo (ini_get("file_uploads") ? '<input type="file" name="' . $name . '" />' : lang('File uploads are disabled.') . ' ');
-       } else { //! binary
-               echo '<input name="fields[' . $name . ']" value="' . htmlspecialchars($value) . '"' . (strlen($field["length"]) ? " maxlength='$field[length]'" : ($types[$field["type"]] ? " maxlength='" . $types[$field["type"]] . "'" : '')) . ' />';
-       }
-       if ($field["null"] && preg_match('~char|text|set|binary|blob~', $field["type"])) {
-               echo '<input type="checkbox" name="null[' . $name . ']" value="1" id="null-' . $name . '"' . (isset($value) ? '' : ' checked="checked"') . ' /><label for="null-' . $name . '">' . lang('NULL') . '</label>';
+       if (!isset($data)) {
+               $value = $field["default"];
+       } elseif (strlen($data[$name]) && ($field["type"] == "enum" || $field["type"] == "set")) {
+               $value = intval($data[$name]);
+       } else {
+               $value = $data[$name];
        }
+       input($name, $field, $value);
        echo "</td></tr>\n";
 }
+echo ($fields ? "</table>\n" : "");
 ?>
-</table>
 <p>
 <input type="hidden" name="token" value="<?php echo $token; ?>" />
-<?php if ($save_possible) { ?>
+<?php if ($fields) { ?>
 <input type="submit" value="<?php echo lang('Save'); ?>" />
 <?php if (!isset($_GET["default"])) { ?><input type="submit" name="insert" value="<?php echo lang('Save and insert'); ?>" /><?php } ?>
 <?php } ?>
index cb58821347682e507076682c82d5ab4f8b7175a9..0520fc24bf44003dc168c3588f044ee1812e505d 100644 (file)
@@ -8,7 +8,7 @@ function idf_unescape($idf) {
 }
 
 function bracket_escape($idf, $back = false) {
-       static $trans = array(':' => ':1', ']' => ':2');
+       static $trans = array(':' => ':1', ']' => ':2', '[' => ':3');
        return strtr($idf, ($back ? array_flip($trans) : $trans));
 }
 
@@ -236,6 +236,70 @@ function select($result) {
                }
                echo "</table>\n";
        }
+       mysql_free_result($result);
+}
+
+function input($name, $field, $value) {
+       static $types;
+       if (!isset($types)) {
+               $types = types();
+       }
+       $name = htmlspecialchars(bracket_escape($name));
+       if ($field["type"] == "enum") {
+               if (!isset($_GET["default"])) {
+                       echo '<input type="radio" name="fields[' . $name . ']" value="0"' . ($value === 0 ? ' checked="checked"' : '') . ' />';
+               }
+               preg_match_all("~'((?:[^']*|'')+)'~", $field["length"], $matches);
+               foreach ($matches[1] as $i => $val) {
+                       $val = str_replace("''", "'", $val);
+                       $id = "field-$name-" . ($i+1);
+                       $checked = (is_int($value) ? $value == $i+1 : $value === $val); //! '' collide with NULL in $_GET["default"]
+                       echo ' <input type="radio" name="fields[' . $name . ']" id="' . $id . '" value="' . (isset($_GET["default"]) ? htmlspecialchars($val) : $i+1) . '"' . ($checked ? ' checked="checked"' : '') . ' /><label for="' . $id . '">' . htmlspecialchars($val) . '</label>';
+               }
+               if ($field["null"]) {
+                       $id = "field-$name-";
+                       echo '<input type="radio" name="fields[' . $name . ']" id="' . $id . '" value=""' . (strlen($value) ? '' : ' checked="checked"') . ' /><label for="' . $id . '">' . lang('NULL') . '</label> ';
+               }
+       } elseif ($field["type"] == "set") { //! 64 bits
+               preg_match_all("~'((?:[^']*|'')+)'~", $field["length"], $matches);
+               foreach ($matches[1] as $i => $val) {
+                       $val = str_replace("''", "'", $val);
+                       $id = "field-$name-" . ($i+1);
+                       $checked = (is_int($value) ? ($value >> $i) & 1 : in_array($val, explode(",", $value), true));
+                       echo ' <input type="checkbox" name="fields[' . $name . '][' . $i . ']" id="' . $id . '" value="' . (isset($_GET["default"]) ? htmlspecialchars($val) : 1 << $i) . '"' . ($checked ? ' checked="checked"' : '') . ' /><label for="' . $id . '">' . htmlspecialchars($val) . '</label>';
+               }
+       } elseif (strpos($field["type"], "text") !== false) {
+               echo '<textarea name="fields[' . $name . ']" cols="50" rows="12">' . htmlspecialchars($value) . '</textarea>';
+       } elseif (preg_match('~binary|blob~', $field["type"])) {
+               echo (ini_get("file_uploads") ? '<input type="file" name="' . $name . '" />' : lang('File uploads are disabled.') . ' ');
+       } else {
+               echo '<input name="fields[' . $name . ']" value="' . htmlspecialchars($value) . '"' . (strlen($field["length"]) ? " maxlength='$field[length]'" : ($types[$field["type"]] ? " maxlength='" . $types[$field["type"]] . "'" : '')) . ' />';
+       }
+       if ($field["null"] && preg_match('~char|text|set|binary|blob~', $field["type"])) {
+               $id = "null-$name";
+               echo '<input type="checkbox" name="null[' . $name . ']" value="1" id="' . $id . '"' . (isset($value) ? '' : ' checked="checked"') . ' /><label for="' . $id . '">' . lang('NULL') . '</label>';
+       }
+}
+
+function process_input($name, $field) {
+       $name = bracket_escape($name);
+       $return = $_POST["fields"][$name];
+       if (preg_match('~char|text|set|binary|blob~', $field["type"]) ? $_POST["null"][$name] : !strlen($return)) {
+               $return = "NULL";
+       } elseif ($field["type"] == "enum") {
+               $return = (isset($_GET["default"]) ? "'" . mysql_real_escape_string($return) . "'" : intval($return));
+       } elseif ($field["type"] == "set") {
+               $return = (isset($_GET["default"]) ? "'" . implode(",", array_map('mysql_real_escape_string', (array) $return)) . "'" : array_sum((array) $return));
+       } elseif (preg_match('~binary|blob~', $field["type"])) {
+               $file = get_file($name);
+               if (!is_string($file) && !$field["null"]) {
+                       return false; //! report errors, also empty $_POST (too big POST data, not only FILES)
+               }
+               $return = "_binary'" . (is_string($file) ? mysql_real_escape_string($file) : "") . "'";
+       } else {
+               $return = "'" . mysql_real_escape_string($return) . "'";
+       }
+       return $return;
 }
 
 if (get_magic_quotes_gpc()) {
index 2fb600fcfd9ee1edf7f0b47e1568ec5ab27fa94d..3d6d88a1e88cde8a5a5230db03d1ee007553ae87 100644 (file)
@@ -24,13 +24,14 @@ if ($_POST && $error) {
                                if (!$result) {
                                        echo "<p class='error'>" . lang('Error in query') . ": " . htmlspecialchars(mysql_error()) . "</p>\n";
                                } elseif ($result === true) {
-                                       //~ if (token_delete()) {
-                                               //~ $token = token();
-                                       //~ }
+                                       /* more secure but less user-friendly
+                                       if (token_delete()) {
+                                               $token = token();
+                                       }
+                                       */
                                        echo "<p class='message'>" . lang('Query executed OK, %d row(s) affected.', mysql_affected_rows()) . "</p>\n";
                                } else {
                                        select($result);
-                                       mysql_free_result($result);
                                }
                        }
                }
@@ -42,6 +43,7 @@ if ($_POST && $error) {
        echo "<p class='error'>" . lang('Unable to upload a file.') . "</p>\n";
 }
 ?>
+
 <form action="<?php echo htmlspecialchars($SELF); ?>sql=" method="post">
 <p><textarea name="query" rows="20" cols="80"><?php echo htmlspecialchars($_POST["query"]); ?></textarea></p>
 <p><input type="hidden" name="token" value="<?php echo $token; ?>" /><input type="submit" value="<?php echo lang('Execute'); ?>" /></p>