diff --git a/adminer/edit.inc.php b/adminer/edit.inc.php index fc5c8820..c91dcbc6 100644 --- a/adminer/edit.inc.php +++ b/adminer/edit.inc.php @@ -3,7 +3,7 @@ $where = (isset($_GET["select"]) ? (count($_POST["check"]) == 1 ? where_check($_ $update = ($where && !$_POST["clone"]); $fields = fields($_GET["edit"]); foreach ($fields as $name => $field) { - if ((isset($_GET["default"]) ? $field["auto_increment"] || ereg('text|blob', $field["type"]) : !isset($field["privileges"][$update ? "update" : "insert"])) || !strlen(adminer_field_name($field))) { + if ((isset($_GET["default"]) ? $field["auto_increment"] || ereg('text|blob', $field["type"]) : !isset($field["privileges"][$update ? "update" : "insert"])) || !strlen($adminer->fieldName($field))) { unset($fields[$name]); } } @@ -36,7 +36,7 @@ if ($_POST && !$error && !isset($_GET["select"])) { } } -$table_name = adminer_table_name(table_status($_GET["edit"])); +$table_name = $adminer->tableName(table_status($_GET["edit"])); page_header( (isset($_GET["default"]) ? lang('Default values') : ($_GET["where"] || (isset($_GET["select"]) && !$_POST["clone"]) ? lang('Edit') : lang('Insert'))), $error, @@ -69,7 +69,7 @@ if ($fields) { unset($create); echo "\n"; foreach ($fields as $name => $field) { - echo "
" . adminer_field_name($field); + echo "
" . $adminer->fieldName($field); $value = (isset($row) ? (strlen($row[$name]) && ($field["type"] == "enum" || $field["type"] == "set") ? intval($row[$name]) : $row[$name]) : ($_POST["clone"] && $field["auto_increment"] ? "" : (isset($_GET["select"]) ? false : $field["default"])) diff --git a/adminer/include/adminer.inc.php b/adminer/include/adminer.inc.php index bfb1263f..3d562b40 100644 --- a/adminer/include/adminer.inc.php +++ b/adminer/include/adminer.inc.php @@ -1,32 +1,33 @@
"> @@ -35,196 +36,194 @@ function adminer_login_form($username) {
' . htmlspecialchars($field["field"]) . '', $field); -} - -/** Links after select heading -* @param array result of SHOW TABLE STATUS -* @return string -*/ -function adminer_select_links($table_status) { - global $SELF; - return call_adminer('select_links', '' . lang('Table structure') . '', $table_status); -} - -/** Find backward keys for table -* @param string -* @return array $return[$target_table][$key_name][$target_column] = $source_column; -*/ -function adminer_backward_keys($table) { - return call_adminer('backward_keys', array(), $table); -} - -/** Query printed in select before execution -* @param string query to be executed -* @return string -*/ -function adminer_select_query($query) { - global $SELF; - // it would be nice if $query can be passed by reference and printed value would be returned but call_user() doesn't allow reference parameters - return call_adminer('select_query', "

" . htmlspecialchars($query) . " " . lang('Edit') . "\n", $query); -} - -/** Description of a row in a table -* @param string -* @return string SQL expression, empty string for no description -*/ -function adminer_row_description($table) { - return call_adminer('row_description', "", $table); -} - -/** Get descriptions of selected data -* @param array all data to print -* @param array -* @return array -*/ -function adminer_row_descriptions($rows, $foreign_keys) { - return call_adminer('row_descriptions', $rows, $rows, $foreign_keys); -} - -/** Value printed in select table -* @param string escaped value to print -* @param string link to foreign key -* @param array single field returned from fields() -* @return string -*/ -function adminer_select_val($val, $link, $field) { - $return = ($field["type"] == "char" ? "$val" : $val); - if (ereg('blob|binary', $field["type"]) && !is_utf8($val)) { - $return = lang('%d byte(s)', strlen($val)); + + /** Authorize the user + * @param string + * @param string + * @return bool + */ + function login($login, $password) { + return true; } - return call_adminer('select_val', ($link ? "$return" : $return), $val, $link); -} - -/** Print extra text in the end of a select form -* @param array fields holding e-mails -* @return bool whether to print default extra -*/ -function adminer_select_extra_display($email_fields) { - call_adminer('select_extra_display', false, $email_fields); -} - -/** Process extras in select form -* @param array AND conditions -* @return bool true if processed, false to process other parts of form -*/ -function adminer_select_extra_process($where) { - return call_adminer('select_extra_process', false, $where); -} - -/** Query printed after execution in the message -* @param string executed query -* @return string -*/ -function adminer_message_query($query) { - global $SELF; - $id = "sql-" . count($_SESSION["messages"]); - $_SESSION["history"][$_GET["server"]][$_GET["db"]][] = $query; - return call_adminer('message_query', " " . lang('SQL command') . "

', $query); -} - -/** Functions displayed in edit form -* @param array single field from fields() -* @return array -*/ -function adminer_edit_functions($field) { - $return = array(""); - if (!isset($_GET["default"])) { - if (ereg('char|date|time', $field["type"])) { - $return = (ereg('char', $field["type"]) ? array("", "md5", "sha1", "password", "uuid") : array("", "now")); //! JavaScript for disabling maxlength + + /** Table caption used in navigation and headings + * @param array result of SHOW TABLE STATUS + * @return string + */ + function tableName($tableStatus) { + return htmlspecialchars($tableStatus["Name"]); + } + + /** Field caption used in select and edit + * @param array single field returned from fields() + * @return string + */ + function fieldName($field) { + return '' . htmlspecialchars($field["field"]) . ''; + } + + /** Links after select heading + * @param array result of SHOW TABLE STATUS + * @return string + */ + function selectLinks($tableStatus) { + global $SELF; + return '' . lang('Table structure') . ''; + } + + /** Find backward keys for table + * @param string + * @return array $return[$target_table][$key_name][$target_column] = $source_column; + */ + function backwardKeys($table) { + return array(); + } + + /** Query printed in select before execution + * @param string query to be executed + * @return string + */ + function selectQuery($query) { + global $SELF; + // it would be nice if $query can be passed by reference and printed value would be returned but call_user() doesn't allow reference parameters + return "

" . htmlspecialchars($query) . " " . lang('Edit') . "\n"; + } + + /** Description of a row in a table + * @param string + * @return string SQL expression, empty string for no description + */ + function rowDescription($table) { + return ""; + } + + /** Get descriptions of selected data + * @param array all data to print + * @param array + * @return array + */ + function rowDescriptions($rows, $foreignKeys) { + return $rows; + } + + /** Value printed in select table + * @param string escaped value to print + * @param string link to foreign key + * @param array single field returned from fields() + * @return string + */ + function selectVal($val, $link, $field) { + $return = ($field["type"] == "char" ? "$val" : $val); + if (ereg('blob|binary', $field["type"]) && !is_utf8($val)) { + $return = lang('%d byte(s)', strlen($val)); } - if (!isset($_GET["call"]) && (isset($_GET["select"]) || where($_GET))) { - // relative functions - if (ereg('int|float|double|decimal', $field["type"])) { - $return = array("", "+", "-"); + return ($link ? "$return" : $return); + } + + /** Print extra text in the end of a select form + * @param array fields holding e-mails + * @return null + */ + function selectExtraDisplay($emailFields) { + } + + /** Process extras in select form + * @param array AND conditions + * @return bool true if processed, false to process other parts of form + */ + function selectExtraProcess($where) { + return false; + } + + /** Query printed after execution in the message + * @param string executed query + * @return string + */ + function messageQuery($query) { + global $SELF; + $id = "sql-" . count($_SESSION["messages"]); + $_SESSION["history"][$_GET["server"]][$_GET["db"]][] = $query; + return " " . lang('SQL command') . "

'; + } + + /** Functions displayed in edit form + * @param array single field from fields() + * @return array + */ + function editFunctions($field) { + $return = array(""); + if (!isset($_GET["default"])) { + if (ereg('char|date|time', $field["type"])) { + $return = (ereg('char', $field["type"]) ? array("", "md5", "sha1", "password", "uuid") : array("", "now")); //! JavaScript for disabling maxlength } - if (ereg('date', $field["type"])) { - $return[] = "+ interval"; - $return[] = "- interval"; - } - if (ereg('time', $field["type"])) { - $return[] = "addtime"; - $return[] = "subtime"; + if (!isset($_GET["call"]) && (isset($_GET["select"]) || where($_GET))) { + // relative functions + if (ereg('int|float|double|decimal', $field["type"])) { + $return = array("", "+", "-"); + } + if (ereg('date', $field["type"])) { + $return[] = "+ interval"; + $return[] = "- interval"; + } + if (ereg('time', $field["type"])) { + $return[] = "addtime"; + $return[] = "subtime"; + } } } + if ($field["null"] || isset($_GET["default"])) { + array_unshift($return, "NULL"); + } + return (isset($_GET["select"]) ? array("orig" => lang('original')) : array()) + $return; } - if ($field["null"] || isset($_GET["default"])) { - array_unshift($return, "NULL"); + + /** Get options to display edit field + * @param string table name + * @param array single field from fields() + * @return array options for + */ + function editInput($table, $field) { + return false; } - return call_adminer('edit_functions', (isset($_GET["select"]) ? array("orig" => lang('original')) : array()) + $return, $field); -} - -/** Get options to display edit field -* @param string table name -* @param array single field from fields() -* @return array options for -*/ -function adminer_edit_input($table, $field) { - return call_adminer('edit_input', false, $table, $field); -} - -/** Process sent input -* @param string field name -* @param array single field from fields() -* @return string expression to use in a query -*/ -function adminer_process_input($name, $field) { - global $dbh; - $idf = bracket_escape($name); - $function = $_POST["function"][$idf]; - $value = $_POST["fields"][$idf]; - $return = $dbh->quote($value); - if (ereg('^(now|uuid)$', $function)) { - $return = "$function()"; - } elseif (ereg('^[+-]$', $function)) { - $return = idf_escape($name) . " $function $return"; - } elseif (ereg('^[+-] interval$', $function)) { - $return = idf_escape($name) . " $function " . (preg_match("~^([0-9]+|'[0-9.: -]') [A-Z_]+$~i", $value) ? $value : $return); - } elseif (ereg('^(addtime|subtime)$', $function)) { - $return = "$function(" . idf_escape($name) . ", $return)"; - } elseif (ereg('^(md5|sha1|password)$', $function)) { - $return = "$function($return)"; - } elseif (ereg('date|time', $field["type"]) && $value == "CURRENT_TIMESTAMP") { - $return = $value; + + /** Process sent input + * @param string field name + * @param array single field from fields() + * @return string expression to use in a query + */ + function processInput($name, $field) { + global $dbh; + $idf = bracket_escape($name); + $function = $_POST["function"][$idf]; + $value = $_POST["fields"][$idf]; + $return = $dbh->quote($value); + if (ereg('^(now|uuid)$', $function)) { + $return = "$function()"; + } elseif (ereg('^[+-]$', $function)) { + $return = idf_escape($name) . " $function $return"; + } elseif (ereg('^[+-] interval$', $function)) { + $return = idf_escape($name) . " $function " . (preg_match("~^([0-9]+|'[0-9.: -]') [A-Z_]+$~i", $value) ? $value : $return); + } elseif (ereg('^(addtime|subtime)$', $function)) { + $return = "$function(" . idf_escape($name) . ", $return)"; + } elseif (ereg('^(md5|sha1|password)$', $function)) { + $return = "$function($return)"; + } elseif (ereg('date|time', $field["type"]) && $value == "CURRENT_TIMESTAMP") { + $return = $value; + } + return $return; } - return call_adminer('process_input', $return, $name, $field); -} - -/** Prints navigation after Adminer title -* @param string can be "auth" if there is no database connection or "db" if there is no database selected -* @return bool true if default navigation should be printed -*/ -function adminer_navigation($missing) { - global $SELF, $dbh; - if (call_adminer('navigation', true, $missing) && $missing != "auth") { - ob_flush(); - flush(); - $databases = get_databases(); - ?> + + /** Prints navigation after Adminer title + * @param string can be "auth" if there is no database connection or "db" if there is no database selected + * @return null + */ + function navigation($missing) { + global $SELF, $dbh; + if ($missing != "auth") { + ob_flush(); + flush(); + $databases = get_databases(); + ?>

@@ -247,19 +246,21 @@ function adminer_navigation($missing) {

query("SHOW TABLES"); - if (!$result->num_rows) { - echo "

" . lang('No tables.') . "\n"; - } else { - echo "

\n"; - while ($row = $result->fetch_row()) { - echo '' . lang('select') . ' '; - echo '' . adminer_table_name(array("Name" => $row[0])) . "
\n"; //! Adminer::table_name may work with full table status + if ($missing != "db" && strlen($_GET["db"])) { + $result = $dbh->query("SHOW TABLES"); + if (!$result->num_rows) { + echo "

" . lang('No tables.') . "\n"; + } else { + echo "

\n"; + while ($row = $result->fetch_row()) { + echo '' . lang('select') . ' '; + echo '' . $this->tableName(array("Name" => $row[0])) . "
\n"; //! Adminer::table_name may work with full table status + } } + $result->free(); + echo '

' . lang('Create new table') . "\n"; } - $result->free(); - echo '

' . lang('Create new table') . "\n"; } } + } diff --git a/adminer/include/auth.inc.php b/adminer/include/auth.inc.php index eb5c1487..fffa9970 100644 --- a/adminer/include/auth.inc.php +++ b/adminer/include/auth.inc.php @@ -37,12 +37,12 @@ if (isset($_POST["server"])) { } function auth_error($exception = null) { - global $ignore, $dbh; + global $ignore, $dbh, $adminer; $username = $_SESSION["usernames"][$_GET["server"]]; unset($_SESSION["usernames"][$_GET["server"]]); page_header(lang('Login'), (isset($username) ? htmlspecialchars($exception ? $exception->getMessage() : (is_string($dbh) ? $dbh : lang('Invalid credentials.'))) : (isset($_POST["server"]) ? lang('Sessions must be enabled.') : ($_POST ? lang('Session expired, please login again.') : ""))), null); echo "

\n"; - adminer_login_form($login); + $adminer->loginForm($login); echo "

\n"; hidden_fields($_POST, $ignore); // expired session foreach ($_FILES as $key => $val) { @@ -57,7 +57,7 @@ if (!isset($username)) { $username = $_GET["username"]; // default username can be passed in URL } $dbh = (isset($username) ? connect() : ''); -if (is_string($dbh) || !adminer_login($username, $_SESSION["passwords"][$_GET["server"]])) { +if (is_string($dbh) || !$adminer->login($username, $_SESSION["passwords"][$_GET["server"]])) { auth_error(); exit; } diff --git a/adminer/include/bootstrap.inc.php b/adminer/include/bootstrap.inc.php index d042fe5a..5aa4a932 100644 --- a/adminer/include/bootstrap.inc.php +++ b/adminer/include/bootstrap.inc.php @@ -72,6 +72,7 @@ include "../adminer/include/functions.inc.php"; include "../adminer/include/lang.inc.php"; include "../adminer/lang/$LANG.inc.php"; include "./include/adminer.inc.php"; +$adminer = (function_exists('adminer_object') ? adminer_object() : new Adminer); include "../adminer/include/design.inc.php"; include "../adminer/include/pdo.inc.php"; include "../adminer/include/mysql.inc.php"; diff --git a/adminer/include/design.inc.php b/adminer/include/design.inc.php index 0174d1b7..07cd3547 100644 --- a/adminer/include/design.inc.php +++ b/adminer/include/design.inc.php @@ -1,6 +1,6 @@ @@ -8,7 +8,7 @@ function page_header($title, $error = "", $breadcrumb = array(), $title2 = "") { -<?php echo $title . (strlen($title2) ? ": " . htmlspecialchars($title2) : "") . (strlen($_GET["server"]) && $_GET["server"] != "localhost" ? htmlspecialchars("- $_GET[server]") : "") . " - " . adminer_name(); ?> +<?php echo $title . (strlen($title2) ? ": " . htmlspecialchars($title2) : "") . (strlen($_GET["server"]) && $_GET["server"] != "localhost" ? htmlspecialchars("- $_GET[server]") : "") . " - " . $adminer->name(); ?> @@ -56,16 +56,16 @@ function page_header($title, $error = "", $breadcrumb = array(), $title2 = "") { } function page_footer($missing = false) { - global $VERSION; + global $VERSION, $adminer; ?>

messageQuery($query); } if ($execute) { $failed = !$dbh->query($query); @@ -276,7 +282,7 @@ function column_foreign_keys($table) { } function input($field, $value, $function) { - global $types; + global $types, $adminer; $name = htmlspecialchars(bracket_escape($field["field"])); echo "
"; if ($field["type"] == "enum") { @@ -294,11 +300,11 @@ function input($field, $value, $function) { echo ' '; } } else { - $functions = adminer_edit_functions($field); + $functions = $adminer->editFunctions($field); $first = array_search("", $functions); $onchange = ($first ? ' onchange="var f = this.form[\'function[' . addcslashes($name, "\r\n'\\") . ']\']; if (' . $first . ' > f.selectedIndex) f.selectedIndex = ' . $first . ';"' : ''); echo (count($functions) > 1 ? '' : " ") . ''; - $options = adminer_edit_input($_GET["edit"], $field); // usage in call is without a table + $options = $adminer->editInput($_GET["edit"], $field); // usage in call is without a table if (is_array($options)) { echo ''; } elseif ($field["type"] == "set") { //! 64 bits @@ -321,7 +327,7 @@ function input($field, $value, $function) { } function process_input($name, $field) { - global $dbh; + global $dbh, $adminer; $idf = bracket_escape($name); $function = $_POST["function"][$idf]; $value = $_POST["fields"][$idf]; @@ -340,7 +346,7 @@ function process_input($name, $field) { } return "_binary" . $dbh->quote($file); } else { - return adminer_process_input($name, $field); + return $adminer->processInput($name, $field); } } @@ -363,11 +369,3 @@ function email_header($header) { // iconv_mime_encode requires PHP 5, imap_8bit requires IMAP extension return "=?UTF-8?B?" . base64_encode($header) . "?="; //! split long lines } - -function call_adminer($method, $default, $arg1 = null, $arg2 = null, $arg3 = null) { - if (method_exists('Adminer', $method)) { // user defined class - // can use func_get_args() and call_user_func_array() - return Adminer::$method($arg1, $arg2, $arg3); - } - return $default; //! $default is evaluated even if not neccessary -} diff --git a/adminer/include/mysql.inc.php b/adminer/include/mysql.inc.php index 37d7724d..a4862d94 100644 --- a/adminer/include/mysql.inc.php +++ b/adminer/include/mysql.inc.php @@ -141,8 +141,9 @@ if (extension_loaded("mysqli")) { } function connect() { + global $adminer; $dbh = new Min_DB; - $credentials = adminer_credentials(); + $credentials = $adminer->credentials(); if ($dbh->connect($credentials[0], $credentials[1], $credentials[2])) { $dbh->query("SET SQL_QUOTE_SHOW_CREATE=1"); $dbh->query("SET NAMES utf8"); diff --git a/adminer/select.inc.php b/adminer/select.inc.php index 7d396f61..688625f3 100644 --- a/adminer/select.inc.php +++ b/adminer/select.inc.php @@ -18,7 +18,7 @@ $rights = array(); // privilege => 0 $columns = array(); // selectable columns unset($text_length); foreach ($fields as $key => $field) { - $name = adminer_field_name($field); + $name = $adminer->fieldName($field); if (isset($field["privileges"]["select"]) && strlen($name)) { $columns[$key] = html_entity_decode(strip_tags($name)); if (ereg('text|blob', $field["type"])) { @@ -109,7 +109,7 @@ if ($_POST && !$error) { } exit; } - if (!adminer_select_extra_process($where)) { + if (!$adminer->selectExtraProcess($where)) { if (!$_POST["import"]) { // edit $result = true; $affected = 0; @@ -169,14 +169,14 @@ if ($_POST && !$error) { } } -page_header(lang('Select') . ": " . adminer_table_name($table_status), $error); +page_header(lang('Select') . ": " . $adminer->tableName($table_status), $error); echo "

"; if (isset($rights["insert"])) { //! pass search values forth and back echo '' . lang('New item') . ' '; } -echo adminer_select_links($table_status); +echo $adminer->selectLinks($table_status); if (!$columns) { echo "

" . lang('Unable to select the table') . ($fields ? "" : ": " . htmlspecialchars($dbh->error)) . ".\n"; @@ -253,7 +253,7 @@ if (!$columns) { echo "\n"; $query = "SELECT " . (intval($limit) && count($group) < count($select) ? "SQL_CALC_FOUND_ROWS " : "") . $from . $group_by . (strlen($limit) ? " LIMIT " . intval($limit) . (intval($_GET["page"]) ? " OFFSET " . ($limit * $_GET["page"]) : "") : ""); - echo adminer_select_query($query); + echo $adminer->selectQuery($query); $result = $dbh->query($query); if (!$result) { @@ -276,12 +276,12 @@ if (!$columns) { ); $foreign_keys = column_foreign_keys($_GET["select"]); - $descriptions = adminer_row_descriptions($rows, $foreign_keys); + $descriptions = $adminer->rowDescriptions($rows, $foreign_keys); - $backward_keys = adminer_backward_keys($_GET["select"]); + $backward_keys = $adminer->backwardKeys($_GET["select"]); $table_names = array_keys($backward_keys); if ($table_names) { - $table_names = array_combine($table_names, array_map('adminer_table_name', array_map('table_status', $table_names))); + $table_names = array_combine($table_names, array_map(array($adminer, 'tableName'), array_map('table_status', $table_names))); } echo "\n"; @@ -291,7 +291,7 @@ if (!$columns) { foreach ($rows[0] as $key => $val) { $val = $_GET["columns"][key($select)]; $field = $fields[$select ? $val["col"] : $key]; - $name = ($field ? adminer_field_name($field) : "*"); + $name = ($field ? $adminer->fieldName($field) : "*"); if (strlen($name)) { $names[$key] = $name; echo '
' . apply_sql_function($val["fun"], $name) . ""; @@ -336,7 +336,7 @@ if (!$columns) { if (!$link && is_email($val)) { $link = "mailto:$val"; } - $val = adminer_select_val($val, $link, $fields[$key]); + $val = $adminer->selectVal($val, $link, $fields[$key]); echo "$val"; } } @@ -381,7 +381,7 @@ if (!$columns) { } echo "
" . lang('CSV Import') . "
\n"; - adminer_select_extra_display(array_filter($email_fields, 'strlen')); + $adminer->selectExtraDisplay(array_filter($email_fields, 'strlen')); echo "\n"; } diff --git a/editor/example.php b/editor/example.php index 52c1b3d4..7b22dd9d 100644 --- a/editor/example.php +++ b/editor/example.php @@ -1,36 +1,41 @@
@@ -21,197 +22,195 @@ function adminer_login_form($username) {
query("SELECT TABLE_NAME, CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_COLUMN_NAME + + function login($login, $password) { + return true; + } + + function tableName($tableStatus) { + table_comment($tableStatus); + return htmlspecialchars(strlen($tableStatus["Comment"]) ? $tableStatus["Comment"] : $tableStatus["Name"]); + } + + function fieldName($field) { + return htmlspecialchars(strlen($field["comment"]) ? $field["comment"] : $field["field"]); + } + + function selectLinks($tableStatus) { + return ""; + } + + function backwardKeys($table) { + global $dbh; + $return = array(); + $result = $dbh->query("SELECT TABLE_NAME, CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_COLUMN_NAME FROM information_schema.KEY_COLUMN_USAGE -WHERE TABLE_SCHEMA = " . $dbh->quote(adminer_database()) . " -AND REFERENCED_TABLE_SCHEMA = " . $dbh->quote(adminer_database()) . " +WHERE TABLE_SCHEMA = " . $dbh->quote($this->database()) . " +AND REFERENCED_TABLE_SCHEMA = " . $dbh->quote($this->database()) . " AND REFERENCED_TABLE_NAME = " . $dbh->quote($table) . " ORDER BY ORDINAL_POSITION"); //! requires MySQL 5 - if ($result) { - while ($row = $result->fetch_assoc()) { - $return[$row["TABLE_NAME"]][$row["CONSTRAINT_NAME"]][$row["COLUMN_NAME"]] = $row["REFERENCED_COLUMN_NAME"]; + if ($result) { + while ($row = $result->fetch_assoc()) { + $return[$row["TABLE_NAME"]][$row["CONSTRAINT_NAME"]][$row["COLUMN_NAME"]] = $row["REFERENCED_COLUMN_NAME"]; + } + $result->free(); } - $result->free(); + return $return; } - return call_adminer('backward_keys', $return, $table); -} - -function adminer_select_query($query) { - return call_adminer('select_query', "\n", $query); -} - -function adminer_row_description($table) { - $return = ""; - // first varchar column - foreach (fields($table) as $field) { - if ($field["type"] == "varchar") { - $return = idf_escape($field["field"]); - break; + + function selectQuery($query) { + return "\n"; + } + + function rowDescription($table) { + $return = ""; + // first varchar column + foreach (fields($table) as $field) { + if ($field["type"] == "varchar") { + $return = idf_escape($field["field"]); + break; + } + } + return $return; + } + + function rowDescriptions($rows, $foreignKeys) { + global $dbh; + $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 (strlen($name)) { + // find all used ids + $ids = array(); + foreach ($rows as $row) { + $ids[$row[$key]] = $dbh->quote($row[$key]); + } + // uses constant number of queries to get the descriptions, join would be complex, multiple queries would be slow + $descriptions = array(); + $result = $dbh->query("SELECT $id, $name FROM " . idf_escape($foreignKey["table"]) . " WHERE $id IN (" . implode(", ", $ids) . ")"); + while ($row = $result->fetch_row()) { + $descriptions[$row[0]] = $row[1]; + } + $result->free(); + // use the descriptions + foreach ($rows as $n => $row) { + $return[$n][$key] = $descriptions[$row[$key]]; + } + break; + } + } + } + } + return $return; + } + + function selectVal($val, $link, $field) { + $return = ($val == "NULL" ? " " : $val); + if (ereg('blob|binary', $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"; + } + } + return ($link ? "$return" : $return); + } + + function selectExtraDisplay($emailFields) { + global $confirm; + if ($emailFields) { + echo '
' . lang('E-mail') . "
\n"; } } - return call_adminer('row_description', $return, $table); -} - -function adminer_row_descriptions($rows, $foreign_keys) { - global $dbh; - $return = $rows; - foreach ($rows[0] as $key => $val) { - foreach ((array) $foreign_keys[$key] as $foreign_key) { + + function selectExtraProcess($where) { + global $dbh; + if ($_POST["email"]) { + $sent = 0; + if ($_POST["all"] || $_POST["check"]) { + $field = idf_escape($_POST["email_field"]); + $result = $dbh->query("SELECT DISTINCT $field 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"])) . "))") + ); + while ($row = $result->fetch_row()) { + if (is_email($row[0]) && mail($row[0], email_header($_POST["email_subject"]), $_POST["email_message"], + "MIME-Version: 1.0\nContent-Type: text/plain; charset=utf-8\nContent-Transfer-Encoding: 8bit" + . (is_email($_POST["email_from"]) ? "\nFrom: $_POST[email_from]" : "") //! should allow address with a name but simple application of email_header() adds the default server domain + )) { + $sent++; + } + } + $result->free(); + } + redirect(remove_from_uri(), lang('%d e-mail(s) have been sent.', $sent)); + } + return false; + } + + function messageQuery($query) { + return ""; + } + + function editFunctions($field) { + return (isset($_GET["select"]) ? array("orig" => lang('original')) : array()) + array(""); + } + + function editInput($table, $field) { + global $dbh; + $return = null; + $foreign_keys = column_foreign_keys($table); + foreach ((array) $foreign_keys[$field["field"]] as $foreign_key) { if (count($foreign_key["source"]) == 1) { $id = idf_escape($foreign_key["target"][0]); - $name = adminer_row_description($foreign_key["table"]); - if (strlen($name)) { - // find all used ids - $ids = array(); - foreach ($rows as $row) { - $ids[$row[$key]] = $dbh->quote($row[$key]); + $name = $this->rowDescription($foreign_key["table"]); + if (strlen($name) && $dbh->result($dbh->query("SELECT COUNT(*) FROM " . idf_escape($foreign_key["table"]))) <= 1000) { // optionlist with more than 1000 options would be too big + if ($field["null"]) { + $return[""] = ""; } - // uses constant number of queries to get the descriptions, join would be complex, multiple queries would be slow - $descriptions = array(); - $result = $dbh->query("SELECT $id, $name FROM " . idf_escape($foreign_key["table"]) . " WHERE $id IN (" . implode(", ", $ids) . ")"); + $result = $dbh->query("SELECT $id, $name FROM " . idf_escape($foreign_key["table"]) . " ORDER BY 2"); while ($row = $result->fetch_row()) { - $descriptions[$row[0]] = $row[1]; + $return[$row[0]] = $row[1]; } $result->free(); - // use the descriptions - foreach ($rows as $n => $row) { - $return[$n][$key] = $descriptions[$row[$key]]; - } break; } } } + return $return; } - return call_adminer('row_descriptions', $return, $rows, $foreign_keys); -} - -function adminer_select_val($val, $link, $field) { - $return = ($val == "NULL" ? " " : $val); - if (ereg('blob|binary', $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"; + + /** Process sent input + * @param string field name + * @param array single field from fields() + * @return string expression to use in a query + */ + function processInput($name, $field) { + global $dbh; + $idf = bracket_escape($name); + $value = $_POST["fields"][$idf]; + $return = $dbh->quote($value); + if (!ereg('varchar|text', $field["type"]) && !strlen($value)) { + $return = "NULL"; + } elseif (ereg('date|time', $field["type"]) && $value == "CURRENT_TIMESTAMP") { + $return = $value; } + return $return; } - return call_adminer('select_val', ($link ? "$return" : $return), $val, $link); -} - -function adminer_select_extra_display($email_fields) { - global $confirm; - if (call_adminer('select_extra_display', true, $email_fields) && $email_fields) { - echo '
' . lang('E-mail') . "
\n"; - } -} - -function adminer_select_extra_process($where) { - global $dbh; - if ($_POST["email"]) { - $sent = 0; - if ($_POST["all"] || $_POST["check"]) { - $field = idf_escape($_POST["email_field"]); - $result = $dbh->query("SELECT DISTINCT $field 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"])) . "))") - ); - while ($row = $result->fetch_row()) { - if (is_email($row[0]) && mail($row[0], email_header($_POST["email_subject"]), $_POST["email_message"], - "MIME-Version: 1.0\nContent-Type: text/plain; charset=utf-8\nContent-Transfer-Encoding: 8bit" - . (is_email($_POST["email_from"]) ? "\nFrom: $_POST[email_from]" : "") //! should allow address with a name but simple application of email_header() adds the default server domain - )) { - $sent++; - } - } - $result->free(); - } - redirect(remove_from_uri(), lang('%d e-mail(s) have been sent.', $sent)); - } - return call_adminer('select_extra_process', false, $where); -} - -function adminer_message_query($query) { - return call_adminer('message_query', "", $query); -} - -function adminer_edit_functions($field) { - return call_adminer('edit_functions', (isset($_GET["select"]) ? array("orig" => lang('original')) : array()) + array(""), $field); -} - -function adminer_edit_input($table, $field) { - global $dbh; - $return = null; - $foreign_keys = column_foreign_keys($table); - foreach ((array) $foreign_keys[$field["field"]] as $foreign_key) { - if (count($foreign_key["source"]) == 1) { - $id = idf_escape($foreign_key["target"][0]); - $name = adminer_row_description($foreign_key["table"]); - if (strlen($name) && $dbh->result($dbh->query("SELECT COUNT(*) FROM " . idf_escape($foreign_key["table"]))) <= 1000) { // optionlist with more than 1000 options would be too big - if ($field["null"]) { - $return[""] = ""; - } - $result = $dbh->query("SELECT $id, $name FROM " . idf_escape($foreign_key["table"]) . " ORDER BY 2"); - while ($row = $result->fetch_row()) { - $return[$row[0]] = $row[1]; - } - $result->free(); - break; - } - } - } - return call_adminer('edit_input', $return, $table, $field); -} - -/** Process sent input -* @param string field name -* @param array single field from fields() -* @return string expression to use in a query -*/ -function adminer_process_input($name, $field) { - global $dbh; - $idf = bracket_escape($name); - $function = $_POST["function"][$idf]; - $value = $_POST["fields"][$idf]; - $return = $dbh->quote($value); - if (!ereg('varchar|text', $field["type"]) && !strlen($value)) { - $return = "NULL"; - } elseif (ereg('date|time', $field["type"]) && $value == "CURRENT_TIMESTAMP") { - $return = $value; - } - return call_adminer('process_input', $return, $name, $field); -} - -function adminer_navigation($missing) { - global $SELF; - if (call_adminer('navigation', true, $missing) && $missing != "auth") { - ?> + + function navigation($missing) { + global $SELF; + if ($missing != "auth") { + ?>

"> @@ -219,19 +218,21 @@ function adminer_navigation($missing) {

" . lang('No tables.') . "\n"; - } else { - echo "

\n"; - foreach ($table_status as $row) { - $name = adminer_table_name($row); - if (isset($row["Engine"]) && strlen($name)) { // ignore views and tables without name - echo '$name
\n"; + if ($missing != "db") { + $table_status = table_status(); + if (!$table_status) { + echo "

" . lang('No tables.') . "\n"; + } else { + echo "

\n"; + foreach ($table_status as $row) { + $name = $this->tableName($row); + if (isset($row["Engine"]) && strlen($name)) { // ignore views and tables without name + echo '$name
\n"; + } } } } } } + } diff --git a/editor/include/connect.inc.php b/editor/include/connect.inc.php index f1d3f89d..dcff166d 100644 --- a/editor/include/connect.inc.php +++ b/editor/include/connect.inc.php @@ -1,3 +1,3 @@ select_db(adminer_database()); +$dbh->select_db($adminer->database());