2007-07-16 22:40:55 +00:00
|
|
|
<?php
|
|
|
|
function input($name, $field, $value) {
|
|
|
|
global $types;
|
|
|
|
$name = htmlspecialchars(bracket_escape($name));
|
2007-08-30 12:42:35 +00:00
|
|
|
$onchange = ($field["null"] ? ' onchange="this.form[\'null[' . addcslashes($name, "\r\n'\\") . ']\'].checked = false;"' : '');
|
2007-07-16 22:40:55 +00:00
|
|
|
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 = stripcslashes(str_replace("''", "'", $val));
|
|
|
|
$id = "field-$name-" . ($i+1);
|
|
|
|
$checked = (is_int($value) ? $value == $i+1 : $value === $val); //! '' collide with NULL in $_GET["default"]
|
|
|
|
echo ' <label for="' . $id . '"><input type="radio" name="fields[' . $name . ']" id="' . $id . '" value="' . (isset($_GET["default"]) ? htmlspecialchars($val) : $i+1) . '"' . ($checked ? ' checked="checked"' : '') . ' />' . htmlspecialchars($val) . '</label>';
|
|
|
|
}
|
|
|
|
if ($field["null"]) {
|
|
|
|
$id = "field-$name-";
|
|
|
|
echo ' <label for="' . $id . '"><input type="radio" name="fields[' . $name . ']" id="' . $id . '" value=""' . (strlen($value) ? '' : ' checked="checked"') . ' />' . lang('NULL') . '</label>';
|
|
|
|
}
|
|
|
|
} elseif ($field["type"] == "set") { //! 64 bits
|
|
|
|
preg_match_all("~'((?:[^']+|'')*)'~", $field["length"], $matches);
|
|
|
|
foreach ($matches[1] as $i => $val) {
|
|
|
|
$val = stripcslashes(str_replace("''", "'", $val));
|
|
|
|
$id = "field-$name-" . ($i+1);
|
|
|
|
$checked = (is_int($value) ? ($value >> $i) & 1 : in_array($val, explode(",", $value), true));
|
2007-08-30 12:42:35 +00:00
|
|
|
echo ' <input type="checkbox" name="fields[' . $name . '][' . $i . ']" id="' . $id . '" value="' . (isset($_GET["default"]) ? htmlspecialchars($val) : 1 << $i) . '"' . ($checked ? ' checked="checked"' : '') . $onchange . ' /><label for="' . $id . '">' . htmlspecialchars($val) . '</label>';
|
2007-07-16 22:40:55 +00:00
|
|
|
}
|
|
|
|
} elseif (strpos($field["type"], "text") !== false) {
|
2007-08-30 12:42:35 +00:00
|
|
|
echo '<textarea name="fields[' . $name . ']" cols="50" rows="12"' . $onchange . '>' . htmlspecialchars($value) . '</textarea>';
|
2007-07-16 22:40:55 +00:00
|
|
|
} elseif (preg_match('~binary|blob~', $field["type"])) {
|
2007-08-30 12:42:35 +00:00
|
|
|
echo (ini_get("file_uploads") ? '<input type="file" name="' . $name . '"' . $onchange . ' />' : lang('File uploads are disabled.') . ' ');
|
2007-07-16 22:40:55 +00:00
|
|
|
} else {
|
2008-03-20 12:50:35 +00:00
|
|
|
echo '<input name="fields[' . $name . ']" value="' . htmlspecialchars($value) . '"' . (preg_match('~^([0-9]+)(,([0-9]+))?$~', $field["length"], $match) ? " maxlength='" . ($match[1] + ($match[3] ? 1 : 0) + ($match[2] && !$field["unsigned"] ? 1 : 0)) . "'" : ($types[$field["type"]] ? " maxlength='" . $types[$field["type"]] . "'" : '')) . $onchange . ' />';
|
2007-07-16 22:40:55 +00:00
|
|
|
}
|
2007-07-26 08:35:01 +00:00
|
|
|
if ($field["null"] && $field["type"] != "enum") {
|
2007-07-16 22:40:55 +00:00
|
|
|
$id = "null-$name";
|
|
|
|
echo '<label for="' . $id . '"><input type="checkbox" name="null[' . $name . ']" value="1" id="' . $id . '"' . (isset($value) ? '' : ' checked="checked"') . ' />' . lang('NULL') . '</label>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function process_input($name, $field) {
|
|
|
|
global $mysql;
|
|
|
|
$name = bracket_escape($name);
|
|
|
|
$value = $_POST["fields"][$name];
|
2007-08-07 12:48:07 +00:00
|
|
|
if ($field["type"] != "enum" && !$field["auto_increment"] ? $_POST["null"][$name] : !strlen($value)) {
|
2007-07-16 22:40:55 +00:00
|
|
|
return "NULL";
|
|
|
|
} elseif ($field["type"] == "enum") {
|
|
|
|
return (isset($_GET["default"]) ? "'" . $mysql->escape_string($value) . "'" : intval($value));
|
|
|
|
} elseif ($field["type"] == "set") {
|
|
|
|
return (isset($_GET["default"]) ? "'" . implode(",", array_map(array($mysql, 'escape_string'), (array) $value)) . "'" : array_sum((array) $value));
|
|
|
|
} elseif (preg_match('~binary|blob~', $field["type"])) {
|
|
|
|
$file = get_file($name);
|
2008-03-18 10:29:20 +00:00
|
|
|
if (!is_string($file) && ($file != UPLOAD_ERR_NO_FILE || !$field["null"])) {
|
|
|
|
return false; //! report errors
|
2007-07-16 22:40:55 +00:00
|
|
|
}
|
|
|
|
return "_binary'" . (is_string($file) ? $mysql->escape_string($file) : "") . "'";
|
2007-07-25 14:40:35 +00:00
|
|
|
} elseif ($field["type"] == "timestamp" && $value == "CURRENT_TIMESTAMP") {
|
|
|
|
return $value;
|
2007-07-16 22:40:55 +00:00
|
|
|
} else {
|
|
|
|
return "'" . $mysql->escape_string($value) . "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function edit_type($key, $field, $collations) {
|
|
|
|
global $types, $unsigned, $inout;
|
2008-03-20 16:17:37 +00:00
|
|
|
?>
|
2007-07-16 22:40:55 +00:00
|
|
|
<td><select name="<?php echo $key; ?>[type]" onchange="type_change(this);"><?php echo optionlist(array_keys($types), $field["type"]); ?></select></td>
|
|
|
|
<td><input name="<?php echo $key; ?>[length]" value="<?php echo htmlspecialchars($field["length"]); ?>" size="3" /></td>
|
|
|
|
<td><select name="<?php echo $key; ?>[collation]"><option value="">(<?php echo lang('collation'); ?>)</option><?php echo optionlist($collations, $field["collation"]); ?></select> <select name="<?php echo $key; ?>[unsigned]"><?php echo optionlist($unsigned, $field["unsigned"]); ?></select></td>
|
|
|
|
<?php
|
|
|
|
}
|
|
|
|
|
2007-07-17 05:51:09 +00:00
|
|
|
function process_type($field, $collate = "COLLATE") {
|
2007-07-16 22:40:55 +00:00
|
|
|
global $mysql, $enum_length, $unsigned;
|
|
|
|
return " $field[type]"
|
2008-01-08 15:54:34 +00:00
|
|
|
. ($field["length"] && !preg_match('~^date|time$~', $field["type"]) ? "(" . process_length($field["length"]) . ")" : "")
|
2007-07-16 22:40:55 +00:00
|
|
|
. (preg_match('~int|float|double|decimal~', $field["type"]) && in_array($field["unsigned"], $unsigned) ? " $field[unsigned]" : "")
|
2007-07-17 05:51:09 +00:00
|
|
|
. (preg_match('~char|text|enum|set~', $field["type"]) && $field["collation"] ? " $collate '" . $mysql->escape_string($field["collation"]) . "'" : "")
|
2007-07-16 22:40:55 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
function edit_fields($fields, $collations, $type = "TABLE") {
|
|
|
|
global $inout;
|
2008-03-20 16:17:37 +00:00
|
|
|
?>
|
2007-07-16 22:40:55 +00:00
|
|
|
<thead><tr>
|
2007-07-17 09:12:15 +00:00
|
|
|
<?php if ($type == "PROCEDURE") { ?><td><?php echo lang('IN-OUT'); ?></td><?php } ?>
|
2007-07-17 06:01:52 +00:00
|
|
|
<th><?php echo ($type == "TABLE" ? lang('Column name') : lang('Parameter name')); ?></th>
|
2007-07-16 22:40:55 +00:00
|
|
|
<td><?php echo lang('Type'); ?></td>
|
|
|
|
<td><?php echo lang('Length'); ?></td>
|
|
|
|
<td><?php echo lang('Options'); ?></td>
|
|
|
|
<?php if ($type == "TABLE") { ?>
|
|
|
|
<td><?php echo lang('NULL'); ?></td>
|
2007-07-19 14:33:24 +00:00
|
|
|
<td><input type="radio" name="auto_increment_col" value="" /><?php echo lang('Auto Increment'); ?></td>
|
2007-07-17 16:31:42 +00:00
|
|
|
<td><?php echo lang('Comment'); ?></td>
|
2007-07-16 22:40:55 +00:00
|
|
|
<?php } ?>
|
2008-05-07 13:51:10 +00:00
|
|
|
<td><input type="image" name="add[0]" src="plus.gif" title="<?php echo lang('Add next'); ?>" /></td>
|
2007-07-16 22:40:55 +00:00
|
|
|
</tr></thead>
|
|
|
|
<?php
|
2007-07-16 23:08:55 +00:00
|
|
|
$column_comments = false;
|
|
|
|
foreach ($fields as $i => $field) {
|
|
|
|
$i++;
|
2008-03-20 16:17:37 +00:00
|
|
|
$display = ($_POST["add"][$i-1] || (isset($field["field"]) && !$_POST["drop_col"][$i]));
|
2007-07-16 23:08:55 +00:00
|
|
|
?>
|
2008-03-20 16:17:37 +00:00
|
|
|
<tr<?php echo ($display ? "" : " style='display: none;'"); ?>>
|
2007-07-17 09:12:15 +00:00
|
|
|
<?php if ($type == "PROCEDURE") { ?><td><select name="fields[<?php echo $i; ?>][inout]"><?php echo optionlist($inout, $field["inout"]); ?></select></td><?php } ?>
|
2008-03-20 16:17:37 +00:00
|
|
|
<th><?php if ($display) { ?><input name="fields[<?php echo $i; ?>][field]" value="<?php echo htmlspecialchars($field["field"]); ?>" maxlength="64" /><?php } ?><input type="hidden" name="fields[<?php echo $i; ?>][orig]" value="<?php echo htmlspecialchars($field[($_POST ? "orig" : "field")]); ?>" /></th>
|
2007-07-16 22:40:55 +00:00
|
|
|
<?php edit_type("fields[$i]", $field, $collations); ?>
|
|
|
|
<?php if ($type == "TABLE") { ?>
|
|
|
|
<td><input type="checkbox" name="fields[<?php echo $i; ?>][null]" value="1"<?php if ($field["null"]) { ?> checked="checked"<?php } ?> /></td>
|
2007-07-19 14:33:24 +00:00
|
|
|
<td><input type="radio" name="auto_increment_col" value="<?php echo $i; ?>"<?php if ($field["auto_increment"]) { ?> checked="checked"<?php } ?> /></td>
|
2007-07-17 16:31:42 +00:00
|
|
|
<td><input name="fields[<?php echo $i; ?>][comment]" value="<?php echo htmlspecialchars($field["comment"]); ?>" maxlength="255" /></td>
|
2007-07-16 22:40:55 +00:00
|
|
|
<?php } ?>
|
2008-05-07 13:51:10 +00:00
|
|
|
<td style="white-space: nowrap;">
|
|
|
|
<input type="image" name="add[<?php echo $i; ?>]" src="plus.gif" title="<?php echo lang('Add next'); ?>" onclick="return !add_row(this);" />
|
|
|
|
<input type="image" name="drop_col[<?php echo $i; ?>]" src="minus.gif" title="<?php echo lang('Remove'); ?>" onclick="return !remove_row(this);" />
|
|
|
|
<input type="image" name="up[<?php echo $i; ?>]" src="up.gif" title="<?php echo lang('Move up'); ?>" />
|
|
|
|
<input type="image" name="down[<?php echo $i; ?>]" src="down.gif" title="<?php echo lang('Move down'); ?>" />
|
|
|
|
</td>
|
2007-07-16 22:40:55 +00:00
|
|
|
</tr>
|
|
|
|
<?php
|
2007-07-16 23:08:55 +00:00
|
|
|
if (strlen($field["comment"])) {
|
|
|
|
$column_comments = true;
|
|
|
|
}
|
2007-07-16 22:40:55 +00:00
|
|
|
}
|
2007-07-16 23:08:55 +00:00
|
|
|
return $column_comments;
|
2007-07-16 22:40:55 +00:00
|
|
|
}
|
2007-07-16 23:08:55 +00:00
|
|
|
|
2008-05-07 11:22:08 +00:00
|
|
|
function process_fields(&$fields) {
|
|
|
|
ksort($fields);
|
|
|
|
$offset = 0;
|
|
|
|
if ($_POST["up"]) {
|
|
|
|
$last = 0;
|
|
|
|
foreach ($fields as $key => $field) {
|
|
|
|
if (key($_POST["up"]) == $key) {
|
|
|
|
unset($fields[$key]);
|
|
|
|
array_splice($fields, $last, 0, array($field));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (isset($field["field"])) {
|
|
|
|
$last = $offset;
|
|
|
|
}
|
|
|
|
$offset++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($_POST["down"]) {
|
|
|
|
$found = false;
|
|
|
|
foreach ($fields as $key => $field) {
|
|
|
|
if (isset($field["field"]) && $found) {
|
|
|
|
unset($fields[key($_POST["down"])]);
|
|
|
|
array_splice($fields, $offset, 0, array($found));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (key($_POST["down"]) == $key) {
|
|
|
|
$found = $field;
|
|
|
|
}
|
|
|
|
$offset++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$fields = array_values($fields);
|
|
|
|
if ($_POST["add"]) {
|
|
|
|
array_splice($fields, key($_POST["add"]), 0, array(array()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-16 23:08:55 +00:00
|
|
|
function type_change($count) {
|
2008-03-20 16:17:37 +00:00
|
|
|
?>
|
2007-07-16 22:40:55 +00:00
|
|
|
<script type="text/javascript">
|
2007-07-17 16:31:42 +00:00
|
|
|
var added = '.';
|
2007-07-17 20:56:08 +00:00
|
|
|
function add_row(button) {
|
|
|
|
var match = /([0-9]+)(\.[0-9]+)?/.exec(button.name)
|
|
|
|
var x = match[0] + (match[2] ? added.substr(match[2].length) : added) + '1';
|
|
|
|
var row = button.parentNode.parentNode;
|
2007-07-24 12:30:53 +00:00
|
|
|
var row2 = row.cloneNode(true);
|
2007-08-23 15:34:26 +00:00
|
|
|
var tags = row.getElementsByTagName('select');
|
|
|
|
var tags2 = row2.getElementsByTagName('select');
|
2007-07-24 12:30:53 +00:00
|
|
|
for (var i=0; i < tags.length; i++) {
|
|
|
|
tags[i].name = tags[i].name.replace(/([0-9.]+)/, x);
|
|
|
|
tags2[i].selectedIndex = tags[i].selectedIndex;
|
|
|
|
}
|
2007-08-23 15:34:26 +00:00
|
|
|
tags = row.getElementsByTagName('input');
|
2007-07-17 16:31:42 +00:00
|
|
|
for (var i=0; i < tags.length; i++) {
|
2007-07-19 14:33:24 +00:00
|
|
|
if (tags[i].name == 'auto_increment_col') {
|
2007-07-17 16:31:42 +00:00
|
|
|
tags[i].value = x;
|
2008-01-17 10:21:36 +00:00
|
|
|
tags[i].checked = false;
|
2007-07-24 12:30:53 +00:00
|
|
|
}
|
|
|
|
tags[i].name = tags[i].name.replace(/([0-9.]+)/, x);
|
|
|
|
if (/\[(orig|field|comment)/.test(tags[i].name)) {
|
|
|
|
tags[i].value = '';
|
2007-07-17 16:31:42 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-24 12:30:53 +00:00
|
|
|
row.parentNode.insertBefore(row2, row);
|
2007-08-04 19:36:41 +00:00
|
|
|
tags[0].focus();
|
2007-07-17 16:31:42 +00:00
|
|
|
added += '0';
|
|
|
|
return true;
|
|
|
|
}
|
2007-07-26 09:18:58 +00:00
|
|
|
function remove_row(button) {
|
2008-03-20 16:17:37 +00:00
|
|
|
var field = button.form[button.name.replace(/drop_col(.+)/, 'fields$1[field]')];
|
|
|
|
field.parentNode.removeChild(field);
|
2007-07-26 09:18:58 +00:00
|
|
|
button.parentNode.parentNode.style.display = 'none';
|
|
|
|
return true;
|
|
|
|
}
|
2007-07-16 22:40:55 +00:00
|
|
|
function type_change(type) {
|
|
|
|
var name = type.name.substr(0, type.name.length - 6);
|
2007-07-24 12:30:53 +00:00
|
|
|
type.form[name + '[collation]'].style.display = (/char|text|enum|set/.test(type.options[type.selectedIndex].text) ? '' : 'none');
|
|
|
|
type.form[name + '[unsigned]'].style.display = (/int|float|double|decimal/.test(type.options[type.selectedIndex].text) ? '' : 'none');
|
2007-07-16 22:40:55 +00:00
|
|
|
}
|
2007-07-16 23:08:55 +00:00
|
|
|
for (var i=1; <?php echo $count; ?> >= i; i++) {
|
2007-07-16 22:40:55 +00:00
|
|
|
document.getElementById('form')['fields[' + i + '][type]'].onchange();
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
<?php
|
|
|
|
}
|
|
|
|
|
2007-07-17 09:12:15 +00:00
|
|
|
function normalize_enum($match) {
|
|
|
|
return "'" . str_replace("'", "''", addcslashes(stripcslashes(str_replace($match[0]{0} . $match[0]{0}, $match[0]{0}, substr($match[0], 1, -1))), '\\')) . "'";
|
|
|
|
}
|
|
|
|
|
2007-07-16 22:40:55 +00:00
|
|
|
function routine($name, $type) {
|
|
|
|
global $mysql, $enum_length, $inout;
|
2007-08-16 08:32:11 +00:00
|
|
|
$aliases = array("bit" => "tinyint", "bool" => "tinyint", "boolean" => "tinyint", "integer" => "int", "double precision" => "float", "real" => "float", "dec" => "decimal", "numeric" => "decimal", "fixed" => "decimal", "national char" => "char", "national varchar" => "varchar");
|
2008-03-20 16:38:21 +00:00
|
|
|
$type_pattern = "([a-z]+)(?:\\s*\\(((?:[^'\")]*|$enum_length)+)\\))?\\s*(zerofill\\s*)?(unsigned(?:\\s+zerofill)?)?(?:\\s*(?:CHARSET|CHARACTER\\s+SET)\\s*['\"]?([^'\"\\s]+)['\"]?)?";
|
2007-07-16 22:40:55 +00:00
|
|
|
$pattern = "\\s*(" . ($type == "FUNCTION" ? "" : implode("|", $inout)) . ")?\\s*(?:`((?:[^`]+|``)*)`\\s*|\\b(\\S+)\\s+)$type_pattern";
|
|
|
|
$create = $mysql->result($mysql->query("SHOW CREATE $type " . idf_escape($name)), 2);
|
2007-07-17 09:12:15 +00:00
|
|
|
preg_match("~\\(((?:$pattern\\s*,?)*)\\)" . ($type == "FUNCTION" ? "\\s*RETURNS\\s+$type_pattern" : "") . "\\s*(.*)~is", $create, $match);
|
2007-07-16 22:40:55 +00:00
|
|
|
$fields = array();
|
2007-07-17 09:12:15 +00:00
|
|
|
preg_match_all("~$pattern\\s*,?~is", $match[1], $matches, PREG_SET_ORDER);
|
2007-07-16 22:40:55 +00:00
|
|
|
foreach ($matches as $i => $param) {
|
2007-08-16 08:32:11 +00:00
|
|
|
$data_type = strtolower($param[4]);
|
2007-07-16 22:40:55 +00:00
|
|
|
$fields[$i] = array(
|
|
|
|
"field" => str_replace("``", "`", $param[2]) . $param[3],
|
2007-08-16 08:32:11 +00:00
|
|
|
"type" => (isset($aliases[$data_type]) ? $aliases[$data_type] : $data_type),
|
2007-07-16 22:40:55 +00:00
|
|
|
"length" => preg_replace_callback("~$enum_length~s", 'normalize_enum', $param[5]),
|
|
|
|
"unsigned" => strtolower(preg_replace('~\\s+~', ' ', trim("$param[7] $param[6]"))),
|
|
|
|
"inout" => strtoupper($param[1]),
|
2007-07-17 09:12:15 +00:00
|
|
|
"collation" => strtolower($param[8]),
|
2007-07-16 22:40:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if ($type != "FUNCTION") {
|
2007-07-17 09:12:15 +00:00
|
|
|
return array("fields" => $fields, "definition" => $match[10]);
|
2007-07-16 22:40:55 +00:00
|
|
|
}
|
2007-07-17 09:12:15 +00:00
|
|
|
$returns = array("type" => $match[10], "length" => $match[11], "unsigned" => $match[13], "collation" => $match[14]);
|
|
|
|
return array("fields" => $fields, "returns" => $returns, "definition" => $match[15]);
|
2007-07-16 22:40:55 +00:00
|
|
|
}
|