="); var $_values = array(); function name() { return lang('Editor'); } //! driver, ns function credentials() { return array(SERVER, $_GET["username"], get_session("passwords")); } function permanentLogin() { return password_file(); } function database() { global $connection; $dbs = get_databases(false); return (!$dbs ? $connection->result("SELECT SUBSTRING_INDEX(CURRENT_USER, '@', 1)") // username without the database list : $dbs[(information_schema($dbs[0]) ? 1 : 0)] // first available database ); } function loginForm() { ?>
">
\n"; echo checkbox("permanent", 1, $_COOKIE["adminer_permanent"], lang('Permanent login')) . "\n"; } function login($login, $password) { return true; } function tableName($tableStatus) { return h($tableStatus["Comment"] != "" ? $tableStatus["Comment"] : $tableStatus["Name"]); } function fieldName($field, $order = 0) { return h($field["comment"] != "" ? $field["comment"] : $field["field"]); } function selectLinks($tableStatus, $set = "") { $TABLE = $tableStatus["Name"]; if (isset($set)) { echo '

' . lang('New item') . "\n"; } echo ">>\n"; } function backwardKeys($table, $tableName) { global $connection; $return = array(); foreach (get_rows("SELECT TABLE_NAME, CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_COLUMN_NAME FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = " . $connection->quote($this->database()) . " AND REFERENCED_TABLE_SCHEMA = " . $connection->quote($this->database()) . " AND REFERENCED_TABLE_NAME = " . $connection->quote($table) . " ORDER BY ORDINAL_POSITION") as $row) { //! requires MySQL 5 $return[$row["TABLE_NAME"]]["keys"][$row["CONSTRAINT_NAME"]][$row["COLUMN_NAME"]] = $row["REFERENCED_COLUMN_NAME"]; } foreach ($return as $key => $val) { $name = $this->tableName(table_status($key)); if ($name != "") { $search = preg_quote($tableName); $separator = "(:|\\s*-)?\\s+"; $return[$key]["name"] = (preg_match("(^$search$separator(.+)|^(.+?)$separator$search\$)", $name, $match) ? $match[2] . $match[3] : $name); } else { unset($return[$key]); } } return $return; } function backwardKeysPrint($backwardKeys, $row) { if ($backwardKeys) { echo ""; foreach ($backwardKeys as $table => $backwardKey) { foreach ($backwardKey["keys"] as $cols) { $link = ME . 'select=' . urlencode($table); $i = 0; foreach ($cols as $column => $val) { $link .= where_link($i++, $column, $row[$val]); } echo "" . h($backwardKey["name"]) . ""; $link = ME . 'edit=' . urlencode($table); foreach ($cols as $column => $val) { $link .= "&set" . urlencode("[" . bracket_escape($column) . "]") . "=" . urlencode($row[$val]); } echo "+ "; } } } } function selectQuery($query) { return "\n"; } function rowDescription($table) { // first varchar column foreach (fields($table) as $field) { if ($field["type"] == "varchar") { return idf_escape($field["field"]); } } return ""; } function rowDescriptions($rows, $foreignKeys) { $return = $rows; foreach ($rows[0] as $key => $val) { foreach ((array) $foreignKeys[$key] as $foreignKey) { if (count($foreignKey["source"]) == 1) { $id = idf_escape($foreignKey["target"][0]); $name = $this->rowDescription($foreignKey["table"]); if ($name != "") { // find all used ids $ids = array(); foreach ($rows as $row) { $ids[$row[$key]] = exact_value($row[$key]); } // uses constant number of queries to get the descriptions, join would be complex, multiple queries would be slow $descriptions = $this->_values[$foreignKey["table"]]; if (!$descriptions) { $descriptions = get_key_vals("SELECT $id, $name FROM " . idf_escape($foreignKey["table"]) . " WHERE $id IN (" . implode(", ", $ids) . ")"); } // use the descriptions foreach ($rows as $n => $row) { if (isset($row[$key])) { $return[$n][$key] = (string) $descriptions[$row[$key]]; } } break; } } } } return $return; } function selectVal($val, $link, $field) { $return = ($val == "NULL" ? " " : $val); if (ereg('blob|bytea', $field["type"]) && !is_utf8($val)) { $return = lang('%d byte(s)', strlen($val)); if (ereg("^(GIF|\xFF\xD8\xFF|\x89\x50\x4E\x47\x0D\x0A\x1A\x0A)", $val)) { // GIF|JPG|PNG, getimagetype() works with filename $return = "$return"; } } if ($field["full_type"] == "tinyint(1)" && $return != " ") { // bool $return = '' . h($val) . ''; } if ($link) { $return = "$return"; } if (!$link && $field["full_type"] != "tinyint(1)" && ereg('int|float|double|decimal', $field["type"])) { $return = "

$return
"; // Firefox doesn't support } elseif (ereg('date', $field["type"])) { $return = "
$return
"; } return $return; } function editVal($val, $field) { if (ereg('date|timestamp', $field["type"]) && isset($val)) { return preg_replace('~^([0-9]{2}([0-9]+))-(0?([0-9]+))-(0?([0-9]+))~', lang('$1-$3-$5'), $val); } return (ereg("binary", $field["type"]) ? reset(unpack("H*", $val)) : $val); } function selectColumnsPrint($select, $columns) { // can allow grouping functions by indexes } function selectSearchPrint($where, $columns, $indexes) { $where = (array) $_GET["where"]; echo '
' . lang('Search') . "
\n"; $keys = array(); foreach ($where as $key => $val) { $keys[$val["col"]] = $key; } $i = 0; foreach (fields($_GET["select"]) as $name => $field) { if (ereg("enum", $field["type"])) { //! set - uses 1 << $i and FIND_IN_SET() $desc = $columns[$name]; $key = $keys[$name]; $i--; echo "
" . h($desc) . ":"; enum_input("checkbox", " name='where[$i][val][]'", $field, (array) $where[$key]["val"]); //! impossible to search for NULL echo "
\n"; unset($columns[$name]); } } foreach ($columns as $name => $desc) { $options = $this->_foreignKeyOptions($_GET["select"], $name); if ($options) { $key = $keys[$name]; $i--; echo "
" . h($desc) . ":
\n"; unset($columns[$name]); } } $i = 0; foreach ($where as $val) { if (($val["col"] == "" || $columns[$val["col"]]) && "$val[col]$val[val]" != "") { echo "
"; echo html_select("where[$i][op]", array(-1 => "") + $this->operators, $val["op"]); echo "
\n"; $i++; } } echo "
"; echo html_select("where[$i][op]", array(-1 => "") + $this->operators); echo "
\n"; echo "
\n"; } function selectOrderPrint($order, $columns, $indexes) { //! desc $orders = array(); foreach ($indexes as $key => $index) { $order = array(); foreach ($index["columns"] as $val) { $order[] = $this->fieldName(array("field" => $val, "comment" => $columns[$val])); } if (count(array_filter($order, 'strlen')) > 1 && $key != "PRIMARY") { $orders[$key] = implode(", ", $order); } } if ($orders) { echo '
' . lang('Sort') . "
"; echo ""; echo "
\n"; } } function selectLimitPrint($limit) { echo "
" . lang('Limit') . "
"; //
for easy styling echo html_select("limit", array("", "30", "100"), $limit); echo "
\n"; } function selectLengthPrint($text_length) { } function selectActionPrint() { echo "
" . lang('Action') . "
"; echo ""; echo "
\n"; } function selectEmailPrint($emailFields, $columns) { if ($emailFields) { echo '
' . lang('E-mail') . "
\n"; } } function selectColumnsProcess($columns, $indexes) { return array(array(), array()); } function selectSearchProcess($fields, $indexes) { $return = array(); foreach ((array) $_GET["where"] as $key => $where) { $col = $where["col"]; $op = $where["op"]; $val = $where["val"]; if (($key < 0 ? "" : $col) . $val != "") { $conds = array(); foreach (($col != "" ? array($col => $fields[$col]) : $fields) as $name => $field) { if ($col != "" || is_numeric($val) || !ereg('int|float|double|decimal', $field["type"])) { if ($col != "" && $field["type"] == "enum") { $conds[] = idf_escape($name) . " IN (" . implode(", ", array_map('intval', $val)) . ")"; } else { $text_type = ereg('char|text|enum|set', $field["type"]); $value = $this->processInput($field, ($text_type && ereg('^[^%]+$', $val) ? "%$val%" : $val)); $conds[] = idf_escape($name) . ($value == "NULL" ? " IS" . ($op == ">=" ? " NOT" : "") : (in_array($op, $this->operators) ? " $op" : ($op != "=" && $text_type ? " LIKE" : " ="))) . " $value"; //! can issue "Illegal mix of collations" for columns in other character sets - solve by CONVERT($name using utf8) } } } $return[] = ($conds ? "(" . implode(" OR ", $conds) . ")" : "0"); } } return $return; } function selectOrderProcess($fields, $indexes) { if ($_GET["order"]) { return array(idf_escape($_GET["order"][0]) . (isset($_GET["desc"][0]) ? " DESC" : "")); } $index_order = $_GET["index_order"]; foreach (($index_order != "" ? array($indexes[$index_order]) : $indexes) as $index) { if ($index_order != "" || $index["type"] == "INDEX") { $desc = false; foreach ($index["columns"] as $val) { if (ereg('date|timestamp', $fields[$val]["type"])) { $desc = true; break; } } $return = array(); foreach ($index["columns"] as $val) { $return[] = idf_escape($val) . ($desc ? " DESC" : ""); } return $return; } } return array(); } function selectLimitProcess() { return (isset($_GET["limit"]) ? $_GET["limit"] : "30"); } function selectLengthProcess() { return "100"; } function selectEmailProcess($where, $foreignKeys) { if ($_POST["email_append"]) { return true; } if ($_POST["email"]) { $sent = 0; if ($_POST["all"] || $_POST["check"]) { $field = idf_escape($_POST["email_field"]); $subject = $_POST["email_subject"]; $message = $_POST["email_message"]; preg_match_all('~\\{\\$([a-z0-9_]+)\\}~i', "$subject.$message", $matches); // allows {$name} in subject or message $rows = get_rows("SELECT DISTINCT $field" . ($matches[1] ? ", " . implode(", ", array_map('idf_escape', array_unique($matches[1]))) : "") . " FROM " . idf_escape($_GET["select"]) . " WHERE $field IS NOT NULL AND $field != ''" . ($where ? " AND " . implode(" AND ", $where) : "") . ($_POST["all"] ? "" : " AND ((" . implode(") OR (", array_map('where_check', (array) $_POST["check"])) . "))") ); $fields = fields($_GET["select"]); foreach ($this->rowDescriptions($rows, $foreignKeys) as $row) { $replace = array('{\\' => '{'); // allow literal {$name} foreach ($matches[1] as $val) { $replace['{$' . "$val}"] = $this->editVal($row[$val], $fields[$val]); } $email = $row[$_POST["email_field"]]; if (is_email($email) && send_email($email, strtr($subject, $replace), strtr($message, $replace), $_POST["email_from"], $_FILES["email_files"])) { $sent++; } } } cookie("adminer_email", $_POST["email_from"]); redirect(remove_from_uri(), lang('%d e-mail(s) have been sent.', $sent)); } return false; } function messageQuery($query) { return ""; } function editFunctions($field) { $return = array("" => ($field["null"] || $field["auto_increment"] || $field["full_type"] == "tinyint(1)" ? "" : "*")); //! respect driver if (ereg('date|time', $field["type"])) { $return["now"] = lang('now'); } if (eregi('_(md5|sha1)$', $field["field"], $match)) { $return[] = strtolower($match[1]); } return $return; } function editInput($table, $field, $attrs, $value) { if ($field["type"] == "enum") { return ($field["null"] ? "" : ""); } $options = $this->_foreignKeyOptions($table, $field["field"]); if ($options) { return "" . optionlist($options, $value, true) . ""; } if ($field["full_type"] == "tinyint(1)") { // bool return '"; } if (ereg('date|timestamp', $field["type"])) { return " (" . lang('[yyyy]-mm-dd') . ")"; //! maxlength } return ''; } function processInput($field, $value, $function = "") { global $connection; if ($function == "now") { return "$function()"; } $return = $value; if (ereg('date|timestamp', $field["type"]) && preg_match('(^' . str_replace('\\$1', '(?P[0-9]*)', preg_replace('~(\\\\\\$([2-6]))~', '(?P[0-9]{1,2})', preg_quote(lang('$1-$3-$5')))) . '(.*))', $value, $match)) { $return = ($match["p1"] != "" ? $match["p1"] : ($match["p2"] != "" ? ($match["p2"] < 70 ? 20 : 19) . $match["p2"] : gmdate("Y"))) . "-$match[p3]$match[p4]-$match[p5]$match[p6]" . end($match); } $return = $connection->quote($return); if (!ereg('varchar|text', $field["type"]) && $field["full_type"] != "tinyint(1)" && $value == "") { $return = "NULL"; } elseif (ereg('^(md5|sha1)$', $function)) { $return = "$function($return)"; } if (ereg("binary", $field["type"])) { $return = "unhex($return)"; } return $return; } function dumpOutput($select, $value = "") { return ""; } function dumpFormat($select, $value = "") { return html_select("format", array('csv' => 'CSV,', 'csv;' => 'CSV;'), $value, $select); } function navigation($missing) { global $VERSION, $token; ?>

name(); ?>

$password) { if (isset($password)) { if ($first) { echo "

\n"; $first = false; } echo "" . ($username != "" ? h($username) : "" . lang('empty') . "") . "
\n"; } } } else { ?>

" . lang('No tables.') . "\n"; } else { $this->tablesPrint($table_status); } } } } function tablesPrint($tables) { echo "

\n"; foreach ($tables as $row) { $name = $this->tableName($row); if (isset($row["Engine"]) && $name != "") { // ignore views and tables without name echo "" . bold($name, $_GET["select"] == $row["Name"]) . "
\n"; } } } function _foreignKeyOptions($table, $column) { $foreignKeys = column_foreign_keys($table); foreach ((array) $foreignKeys[$column] as $foreignKey) { if (count($foreignKey["source"]) == 1) { $id = idf_escape($foreignKey["target"][0]); $name = $this->rowDescription($foreignKey["table"]); if ($name != "") { $return = &$this->_values[$foreignKey["table"]]; if (!isset($return)) { $table_status = table_status($foreignKey["table"]); $return = ($table_status["Rows"] > 1000 ? array() : array("" => "") + get_key_vals("SELECT $id, $name FROM " . idf_escape($foreignKey["table"]) . " ORDER BY 2")); } return $return; } } } } } $adminer = (function_exists('adminer_object') ? adminer_object() : new Adminer);