2007-07-02 05:51:26 +00:00
|
|
|
<?php
|
|
|
|
function idf_escape($idf) {
|
|
|
|
return "`" . str_replace("`", "``", $idf) . "`";
|
|
|
|
}
|
|
|
|
|
|
|
|
function idf_unescape($idf) {
|
|
|
|
return str_replace("``", "`", $idf);
|
|
|
|
}
|
|
|
|
|
|
|
|
function bracket_escape($idf, $back = false) {
|
2007-07-09 14:45:11 +00:00
|
|
|
static $trans = array(':' => ':1', ']' => ':2', '[' => ':3');
|
2007-07-02 05:51:26 +00:00
|
|
|
return strtr($idf, ($back ? array_flip($trans) : $trans));
|
|
|
|
}
|
|
|
|
|
|
|
|
function optionlist($options, $selected = array(), $not_vals = false) {
|
|
|
|
$return = "";
|
|
|
|
foreach ($options as $k => $v) {
|
|
|
|
if (is_array($v)) {
|
|
|
|
$return .= '<optgroup label="' . htmlspecialchars($k) . '">';
|
|
|
|
}
|
|
|
|
foreach ((is_array($v) ? $v : array($k => $v)) as $key => $val) {
|
2007-07-05 05:09:01 +00:00
|
|
|
$checked = in_array(($not_vals ? $val : $key), (array) $selected, true);
|
2007-07-02 05:51:26 +00:00
|
|
|
$return .= '<option' . ($not_vals ? '' : ' value="' . htmlspecialchars($key) . '"') . ($checked ? ' selected="selected"' : '') . '>' . htmlspecialchars($val) . '</option>';
|
|
|
|
}
|
|
|
|
if (is_array($v)) {
|
|
|
|
$return .= '</optgroup>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fields($table) {
|
2007-07-10 13:30:42 +00:00
|
|
|
global $mysql;
|
2007-07-02 05:51:26 +00:00
|
|
|
$return = array();
|
2007-07-10 13:30:42 +00:00
|
|
|
$result = $mysql->query("SHOW FULL COLUMNS FROM " . idf_escape($table));
|
2007-07-06 13:03:13 +00:00
|
|
|
if ($result) {
|
2007-07-10 13:30:42 +00:00
|
|
|
while ($row = $result->fetch_assoc()) {
|
2007-07-06 13:03:13 +00:00
|
|
|
preg_match('~^([^(]+)(?:\\((.+)\\))?( unsigned)?( zerofill)?$~', $row["Type"], $match);
|
|
|
|
$return[$row["Field"]] = array(
|
|
|
|
"field" => $row["Field"],
|
|
|
|
"type" => $match[1],
|
|
|
|
"length" => $match[2],
|
|
|
|
"unsigned" => ltrim($match[3] . $match[4]),
|
|
|
|
"default" => $row["Default"],
|
2007-07-06 15:48:57 +00:00
|
|
|
"null" => ($row["Null"] == "YES"),
|
2007-07-09 06:12:22 +00:00
|
|
|
"auto_increment" => ($row["Extra"] == "auto_increment"),
|
2007-07-06 13:03:13 +00:00
|
|
|
"collation" => $row["Collation"],
|
2007-07-09 06:12:22 +00:00
|
|
|
"privileges" => array_flip(explode(",", $row["Privileges"])),
|
|
|
|
"comment" => $row["Comment"],
|
2007-07-06 13:03:13 +00:00
|
|
|
);
|
|
|
|
}
|
2007-07-10 13:30:42 +00:00
|
|
|
$result->free();
|
2007-07-02 05:51:26 +00:00
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function indexes($table) {
|
2007-07-10 13:30:42 +00:00
|
|
|
global $mysql;
|
2007-07-02 05:51:26 +00:00
|
|
|
$return = array();
|
2007-07-10 13:30:42 +00:00
|
|
|
$result = $mysql->query("SHOW INDEX FROM " . idf_escape($table));
|
2007-07-10 20:51:14 +00:00
|
|
|
if ($result) {
|
|
|
|
while ($row = $result->fetch_assoc()) {
|
|
|
|
$return[$row["Key_name"]]["type"] = ($row["Key_name"] == "PRIMARY" ? "PRIMARY" : ($row["Index_type"] == "FULLTEXT" ? "FULLTEXT" : ($row["Non_unique"] ? "INDEX" : "UNIQUE")));
|
|
|
|
$return[$row["Key_name"]]["columns"][$row["Seq_in_index"]] = $row["Column_name"];
|
|
|
|
}
|
|
|
|
$result->free();
|
2007-07-02 05:51:26 +00:00
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function foreign_keys($table) {
|
2007-07-10 13:30:42 +00:00
|
|
|
global $mysql;
|
2007-07-11 15:44:10 +00:00
|
|
|
static $pattern = '(?:[^`]+|``)+';
|
2007-07-02 05:51:26 +00:00
|
|
|
$return = array();
|
2007-07-10 13:30:42 +00:00
|
|
|
$result = $mysql->query("SHOW CREATE TABLE " . idf_escape($table));
|
2007-07-06 13:03:13 +00:00
|
|
|
if ($result) {
|
2007-07-11 08:24:03 +00:00
|
|
|
$create_table = $mysql->result($result, 1);
|
2007-07-10 13:30:42 +00:00
|
|
|
$result->free();
|
2007-07-11 15:44:10 +00:00
|
|
|
preg_match_all("~FOREIGN KEY \\(((?:`$pattern`,? ?)+)\\) REFERENCES `($pattern)`(?:\\.`($pattern)`)? \\(((?:`$pattern`,? ?)+)\\)~", $create_table, $matches, PREG_SET_ORDER);
|
2007-07-06 13:03:13 +00:00
|
|
|
foreach ($matches as $match) {
|
2007-07-11 15:44:10 +00:00
|
|
|
$db = idf_unescape(strlen($match[3]) ? $match[2] : $match[3]);
|
|
|
|
$table = idf_unescape(strlen($match[3]) ? $match[3] : $match[2]);
|
|
|
|
preg_match_all("~`($pattern)`~", $match[1], $source);
|
|
|
|
preg_match_all("~`($pattern)`~", $match[4], $target);
|
|
|
|
$return[] = array($db, $table, array_map('idf_unescape', $source[1]), array_map('idf_unescape', $target[1]));
|
2007-07-06 13:03:13 +00:00
|
|
|
}
|
2007-07-02 05:51:26 +00:00
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function unique_idf($row, $indexes) {
|
2007-07-05 04:39:15 +00:00
|
|
|
foreach ($indexes as $index) {
|
|
|
|
if ($index["type"] == "PRIMARY" || $index["type"] == "UNIQUE") {
|
|
|
|
$return = array();
|
|
|
|
foreach ($index["columns"] as $key) {
|
|
|
|
if (!isset($row[$key])) {
|
|
|
|
continue 2;
|
2007-07-02 05:51:26 +00:00
|
|
|
}
|
2007-07-09 06:12:22 +00:00
|
|
|
$return[] = urlencode("where[" . bracket_escape($key) . "]") . "=" . urlencode($row[$key]);
|
2007-07-02 05:51:26 +00:00
|
|
|
}
|
2007-07-05 04:39:15 +00:00
|
|
|
return $return;
|
2007-07-02 05:51:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$return = array();
|
|
|
|
foreach ($row as $key => $val) {
|
2007-07-09 06:12:22 +00:00
|
|
|
$return[] = (isset($val) ? urlencode("where[" . bracket_escape($key) . "]") . "=" . urlencode($val) : "null%5B%5D=" . urlencode($key));
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function where() {
|
2007-07-10 13:30:42 +00:00
|
|
|
global $mysql;
|
2007-07-09 06:12:22 +00:00
|
|
|
$return = array();
|
|
|
|
foreach ((array) $_GET["where"] as $key => $val) {
|
2007-07-10 14:16:44 +00:00
|
|
|
$return[] = idf_escape(bracket_escape($key, "back")) . " = BINARY '" . $mysql->escape_string($val) . "'"; //! enum and set
|
2007-07-09 06:12:22 +00:00
|
|
|
}
|
|
|
|
foreach ((array) $_GET["null"] as $key) {
|
|
|
|
$return[] = idf_escape(bracket_escape($key, "back")) . " IS NULL";
|
2007-07-02 05:51:26 +00:00
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function collations() {
|
2007-07-10 13:30:42 +00:00
|
|
|
global $mysql;
|
2007-07-02 05:51:26 +00:00
|
|
|
$return = array();
|
2007-07-10 13:30:42 +00:00
|
|
|
$result = $mysql->query("SHOW COLLATION");
|
|
|
|
while ($row = $result->fetch_assoc()) {
|
2007-07-02 05:51:26 +00:00
|
|
|
$return[$row["Charset"]][] = $row["Collation"];
|
|
|
|
}
|
2007-07-10 13:30:42 +00:00
|
|
|
$result->free();
|
2007-07-02 05:51:26 +00:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function engines() {
|
2007-07-10 13:30:42 +00:00
|
|
|
global $mysql;
|
2007-07-02 05:51:26 +00:00
|
|
|
$return = array();
|
2007-07-10 13:30:42 +00:00
|
|
|
$result = $mysql->query("SHOW ENGINES");
|
|
|
|
while ($row = $result->fetch_assoc()) {
|
2007-07-02 05:51:26 +00:00
|
|
|
if ($row["Support"] == "YES" || $row["Support"] == "DEFAULT") {
|
|
|
|
$return[] = $row["Engine"];
|
|
|
|
}
|
|
|
|
}
|
2007-07-10 13:30:42 +00:00
|
|
|
$result->free();
|
2007-07-02 05:51:26 +00:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2007-07-04 14:33:52 +00:00
|
|
|
function types() {
|
|
|
|
return array(
|
|
|
|
"tinyint" => 3, "smallint" => 5, "mediumint" => 8, "int" => 10, "bigint" => 20,
|
|
|
|
"float" => 12, "double" => 21, "decimal" => 66,
|
|
|
|
"date" => 10, "datetime" => 19, "timestamp" => 19, "time" => 10, "year" => 4,
|
|
|
|
"char" => 255, "varchar" => 65535,
|
|
|
|
"binary" => 255, "varbinary" => 65535,
|
|
|
|
"tinytext" => 255, "text" => 65535, "mediumtext" => 16777215, "longtext" => 4294967295,
|
|
|
|
"tinyblob" => 255, "blob" => 65535, "mediumblob" => 16777215, "longblob" => 4294967295,
|
|
|
|
"enum" => 65535, "set" => 64,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-07-09 06:12:22 +00:00
|
|
|
function token() {
|
|
|
|
return ($GLOBALS["TOKENS"][] = rand(1, 1e6));
|
|
|
|
}
|
|
|
|
|
|
|
|
function token_delete() {
|
|
|
|
if ($_POST["token"] && ($pos = array_search($_POST["token"], (array) $GLOBALS["TOKENS"])) !== false) {
|
|
|
|
unset($GLOBALS["TOKENS"][$pos]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function redirect($location, $message = null) {
|
|
|
|
if (isset($message)) {
|
|
|
|
$_SESSION["message"] = $message;
|
|
|
|
}
|
|
|
|
token_delete();
|
|
|
|
if (strlen(SID)) {
|
|
|
|
$location .= (strpos($location, "?") === false ? "?" : "&") . SID;
|
|
|
|
}
|
|
|
|
header("Location: " . (strlen($location) ? $location : "."));
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_file($key) {
|
|
|
|
if (isset($_POST["files"][$key])) {
|
|
|
|
$length = strlen($_POST["files"][$key]);
|
|
|
|
return ($length & $length < 4 ? intval($_POST["files"][$key]) : base64_decode($_POST["files"][$key]));
|
|
|
|
}
|
|
|
|
return (!$_FILES[$key] || $_FILES[$key]["error"] ? $_FILES[$key]["error"] : file_get_contents($_FILES[$key]["tmp_name"]));
|
|
|
|
}
|
|
|
|
|
|
|
|
function select($result) {
|
2007-07-10 20:51:14 +00:00
|
|
|
global $SELF;
|
2007-07-10 13:30:42 +00:00
|
|
|
if (!$result->num_rows) {
|
2007-07-09 06:12:22 +00:00
|
|
|
echo "<p class='message'>" . lang('No rows.') . "</p>\n";
|
|
|
|
} else {
|
|
|
|
echo "<table border='1' cellspacing='0' cellpadding='2'>\n";
|
2007-07-10 13:30:42 +00:00
|
|
|
for ($i=0; $row = $result->fetch_row(); $i++) {
|
2007-07-09 06:12:22 +00:00
|
|
|
if (!$i) {
|
|
|
|
echo "<thead><tr>";
|
|
|
|
$links = array();
|
|
|
|
$indexes = array();
|
|
|
|
$columns = array();
|
|
|
|
$blobs = array();
|
|
|
|
for ($j=0; $j < count($row); $j++) {
|
2007-07-10 13:30:42 +00:00
|
|
|
$field = $result->fetch_field();
|
2007-07-10 20:51:14 +00:00
|
|
|
if (strlen($field->orgtable) && $field->flags & 2) {
|
2007-07-10 13:30:42 +00:00
|
|
|
if (!isset($indexes[$field->orgtable])) {
|
|
|
|
$indexes[$field->orgtable] = array();
|
|
|
|
foreach (indexes($field->orgtable) as $index) {
|
2007-07-09 06:12:22 +00:00
|
|
|
if ($index["type"] == "PRIMARY") {
|
2007-07-10 13:30:42 +00:00
|
|
|
$indexes[$field->orgtable] = array_flip($index["columns"]);
|
2007-07-09 06:12:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-07-10 13:30:42 +00:00
|
|
|
$columns[$field->orgtable] = $indexes[$field->orgtable];
|
2007-07-09 06:12:22 +00:00
|
|
|
}
|
2007-07-10 20:51:14 +00:00
|
|
|
if (isset($columns[$field->orgtable][$field->orgname])) {
|
|
|
|
unset($columns[$field->orgtable][$field->orgname]);
|
|
|
|
$indexes[$field->orgtable][$field->orgname] = $j;
|
|
|
|
$links[$j] = $field->orgtable;
|
|
|
|
}
|
2007-07-09 06:12:22 +00:00
|
|
|
}
|
2007-07-10 13:30:42 +00:00
|
|
|
if ($field->charsetnr == 63) {
|
2007-07-09 06:12:22 +00:00
|
|
|
$blobs[$j] = true;
|
|
|
|
}
|
|
|
|
echo "<th>" . htmlspecialchars($field->name) . "</th>";
|
|
|
|
}
|
|
|
|
echo "</tr></thead>\n";
|
|
|
|
}
|
|
|
|
echo "<tr>";
|
|
|
|
foreach ($row as $key => $val) {
|
|
|
|
if (!isset($val)) {
|
|
|
|
$val = "<i>NULL</i>";
|
|
|
|
} else {
|
|
|
|
$val = ($blobs[$key] && preg_match('~[\\x80-\\xFF]~', $val) ? "<i>" . lang('%d byte(s)', strlen($val)) . "</i>" : (trim($val) ? nl2br(htmlspecialchars($val)) : " "));
|
|
|
|
if (isset($links[$key]) && !$columns[$links[$key]]) {
|
|
|
|
$link = "edit=" . urlencode($links[$key]);
|
|
|
|
foreach ($indexes[$links[$key]] as $col => $j) {
|
|
|
|
$link .= "&where" . urlencode("[" . bracket_escape($col) . "]") . "=" . urlencode($row[$j]);
|
|
|
|
}
|
|
|
|
$val = '<a href="' . htmlspecialchars($SELF) . $link . '">' . $val . '</a>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
echo "<td>$val</td>";
|
|
|
|
}
|
|
|
|
echo "</tr>\n";
|
|
|
|
}
|
|
|
|
echo "</table>\n";
|
|
|
|
}
|
2007-07-10 13:30:42 +00:00
|
|
|
$result->free();
|
2007-07-09 14:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"' : '') . ' />';
|
|
|
|
}
|
2007-07-10 18:53:35 +00:00
|
|
|
preg_match_all("~'((?:[^']+|'')*)'~", $field["length"], $matches);
|
2007-07-09 14:45:11 +00:00
|
|
|
foreach ($matches[1] as $i => $val) {
|
2007-07-09 22:55:50 +00:00
|
|
|
$val = stripcslashes(str_replace("''", "'", $val));
|
2007-07-09 14:45:11 +00:00
|
|
|
$id = "field-$name-" . ($i+1);
|
|
|
|
$checked = (is_int($value) ? $value == $i+1 : $value === $val); //! '' collide with NULL in $_GET["default"]
|
2007-07-09 22:55:50 +00:00
|
|
|
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>';
|
2007-07-09 14:45:11 +00:00
|
|
|
}
|
|
|
|
if ($field["null"]) {
|
|
|
|
$id = "field-$name-";
|
2007-07-09 22:55:50 +00:00
|
|
|
echo ' <label for="' . $id . '"><input type="radio" name="fields[' . $name . ']" id="' . $id . '" value=""' . (strlen($value) ? '' : ' checked="checked"') . ' />' . lang('NULL') . '</label>';
|
2007-07-09 14:45:11 +00:00
|
|
|
}
|
|
|
|
} elseif ($field["type"] == "set") { //! 64 bits
|
2007-07-10 18:53:35 +00:00
|
|
|
preg_match_all("~'((?:[^']+|'')*)'~", $field["length"], $matches);
|
2007-07-09 14:45:11 +00:00
|
|
|
foreach ($matches[1] as $i => $val) {
|
2007-07-09 22:55:50 +00:00
|
|
|
$val = stripcslashes(str_replace("''", "'", $val));
|
2007-07-09 14:45:11 +00:00
|
|
|
$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";
|
2007-07-09 22:55:50 +00:00
|
|
|
echo '<label for="' . $id . '"><input type="checkbox" name="null[' . $name . ']" value="1" id="' . $id . '"' . (isset($value) ? '' : ' checked="checked"') . ' />' . lang('NULL') . '</label>';
|
2007-07-09 14:45:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function process_input($name, $field) {
|
2007-07-10 13:30:42 +00:00
|
|
|
global $mysql;
|
2007-07-09 14:45:11 +00:00
|
|
|
$name = bracket_escape($name);
|
2007-07-11 11:00:49 +00:00
|
|
|
$value = $_POST["fields"][$name];
|
|
|
|
if (preg_match('~char|text|set|binary|blob~', $field["type"]) ? $_POST["null"][$name] : !strlen($value)) {
|
|
|
|
return "NULL";
|
2007-07-09 14:45:11 +00:00
|
|
|
} elseif ($field["type"] == "enum") {
|
2007-07-11 11:00:49 +00:00
|
|
|
return (isset($_GET["default"]) ? "'" . $mysql->escape_string($value) . "'" : intval($value));
|
2007-07-09 14:45:11 +00:00
|
|
|
} elseif ($field["type"] == "set") {
|
2007-07-11 11:36:09 +00:00
|
|
|
return (isset($_GET["default"]) ? "'" . implode(",", array_map(array($mysql, 'escape_string'), (array) $value)) . "'" : array_sum((array) $value));
|
2007-07-09 14:45:11 +00:00
|
|
|
} 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)
|
|
|
|
}
|
2007-07-11 11:00:49 +00:00
|
|
|
return "_binary'" . (is_string($file) ? $mysql->escape_string($file) : "") . "'";
|
2007-07-09 14:45:11 +00:00
|
|
|
} else {
|
2007-07-11 11:00:49 +00:00
|
|
|
return "'" . $mysql->escape_string($value) . "'";
|
2007-07-09 14:45:11 +00:00
|
|
|
}
|
2007-07-09 06:12:22 +00:00
|
|
|
}
|
|
|
|
|
2007-07-02 05:51:26 +00:00
|
|
|
if (get_magic_quotes_gpc()) {
|
|
|
|
$process = array(&$_GET, &$_POST);
|
|
|
|
while (list($key, $val) = each($process)) {
|
|
|
|
foreach ($val as $k => $v) {
|
|
|
|
unset($process[$key][$k]);
|
|
|
|
if (is_array($v)) {
|
|
|
|
$process[$key][stripslashes($k)] = $v;
|
|
|
|
$process[] = &$process[$key][stripslashes($k)];
|
|
|
|
} else {
|
|
|
|
$process[$key][stripslashes($k)] = stripslashes($v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($process);
|
|
|
|
}
|