1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-06 06:37:33 +02:00

Notices: Avoid accessing offset on null

Thanks to @peterpp at 62017e3.
This commit is contained in:
Jakub Vrana
2025-03-26 04:16:17 +01:00
parent d3be21e000
commit 1b8a428d2f
16 changed files with 37 additions and 27 deletions

View File

@@ -71,13 +71,13 @@ if ($in) {
$field = $routine["fields"][$key];
$name = $field["field"];
echo "<tr><th>" . $adminer->fieldName($field);
$value = $_POST["fields"][$name];
$value = idx($_POST["fields"], $name);
if ($value != "") {
if ($field["type"] == "set") {
$value = implode(",", $value);
}
}
input($field, $value, (string) $_POST["function"][$name]); // param name can be empty
input($field, $value, idx($_POST["function"], $name, "")); // param name can be empty
echo "\n";
}
echo "</table>\n";

View File

@@ -95,7 +95,7 @@ $j = 0;
foreach ($row["source"] as $key => $val) {
echo "<tr>";
echo "<td>" . html_select("source[" . (+$key) . "]", array(-1 => "") + $source, $val, ($j == count($row["source"]) - 1 ? "foreignAddRow.call(this);" : ""), "label-source");
echo "<td>" . html_select("target[" . (+$key) . "]", $target, $row["target"][$key], "", "label-target");
echo "<td>" . html_select("target[" . (+$key) . "]", $target, idx($row["target"], $key), "", "label-target");
$j++;
}
?>

View File

@@ -667,7 +667,7 @@ class Adminer {
global $driver;
restart_session();
$history = &get_session("queries");
if (!$history[$_GET["db"]]) {
if (!idx($history, $_GET["db"])) {
$history[$_GET["db"]] = array();
}
if (strlen($query) > 1e6) {

View File

@@ -62,7 +62,7 @@ function check_invalid_login() {
}
}
$invalid = ($invalids ? $invalids[$adminer->bruteForceKey()] : array());
$next_attempt = ($invalid[1] > 29 ? $invalid[0] - time() : 0); // allow 30 invalid attempts
$next_attempt = (idx($invalid, 1) > 29 ? $invalid[0] - time() : 0); // allow 30 invalid attempts
if ($next_attempt > 0) { //! do the same with permanent login
auth_error(lang('Too many unsuccessful logins, try again in %d minute(s).', ceil($next_attempt / 60)));
}

View File

@@ -8,7 +8,7 @@ if (extension_loaded("xdebug") && file_exists(sys_get_temp_dir() . "/adminer.cov
$coverage = unserialize(file_get_contents($coverage_filename));
foreach (xdebug_get_code_coverage() as $filename => $lines) {
foreach ($lines as $l => $val) {
if (!$coverage[$filename][$l] || $val > 0) {
if (!idx($coverage[$filename], $l) || $val > 0) {
$coverage[$filename][$l] = $val;
}
}

View File

@@ -182,7 +182,7 @@ function get_nonce() {
function page_messages($error) {
global $adminer;
$uri = preg_replace('~^[^?]*~', '', $_SERVER["REQUEST_URI"]);
$messages = $_SESSION["messages"][$uri];
$messages = idx($_SESSION["messages"], $uri);
if ($messages) {
echo "<div class='message'>" . implode("</div>\n<div class='message'>", $messages) . "</div>" . script("messagesPrint();");
unset($_SESSION["messages"][$uri]);

View File

@@ -353,7 +353,7 @@ function edit_fields($fields, $collations, $type = "TABLE", $foreign_keys = arra
foreach ($fields as $i => $field) {
$i++;
$orig = $field[($_POST ? "orig" : "field")];
$display = (isset($_POST["add"][$i-1]) || (isset($field["field"]) && !$_POST["drop_col"][$i])) && (support("drop_col") || $orig == "");
$display = (isset($_POST["add"][$i-1]) || (isset($field["field"]) && !idx($_POST["drop_col"], $i))) && (support("drop_col") || $orig == "");
echo "<tr" . ($display ? "" : " style='display: none;'") . ">\n";
echo ($type == "PROCEDURE" ? "<td>" . html_select("fields[$i][inout]", explode("|", $driver->inout), $field["inout"]) : "") . "<th>";
if ($display) {

View File

@@ -3,8 +3,7 @@ namespace Adminer;
error_reporting(24575); // all but E_DEPRECATED (overriding mysqli methods without types is deprecated)
set_error_handler(function ($errno, $errstr) {
// "offset on null" mutes $_GET["fields"][0] if there's no ?fields[]= (62017e3 is a wrong fix for this)
// "Undefined array key" mutes $_GET["q"] if there's no ?q=
// "Undefined offset" and "Undefined index" are older messages for the same thing
return !!preg_match('~^(Trying to access array offset on( value of type)? null|Undefined (array key|offset|index))~', $errstr);
return !!preg_match('~^(Undefined (array key|offset|index))~', $errstr);
}, E_WARNING | E_NOTICE); // warning since PHP 8.0

View File

@@ -65,6 +65,16 @@ function escape_string($val) {
return substr(q($val), 1, -1);
}
/** Get a possibly missing item from a possibly missing array
* @param array|null
* @param string|int
* @param mixed
* @return mixed
*/
function idx($array, $key, $default = null) {
return ($array && array_key_exists($key, $array) ? $array[$key] : $default);
}
/** Remove non-digits from a string
* @param string
* @return string
@@ -302,12 +312,13 @@ function where($where, $fields = array()) {
foreach ((array) $where["where"] as $key => $val) {
$key = bracket_escape($key, 1); // 1 - back
$column = escape_key($key);
$field_type = $fields[$key]["type"];
$field = ($fields ? $fields[$key] : array());
$field_type = $field["type"];
$return[] = $column
. (JUSH == "sql" && $field_type == "json" ? " = CAST(" . q($val) . " AS JSON)"
: (JUSH == "sql" && is_numeric($val) && preg_match('~\.~', $val) ? " LIKE " . q($val) // LIKE because of floats but slow with ints
: (JUSH == "mssql" && strpos($field_type, "datetime") === false ? " LIKE " . q(preg_replace('~[_%[]~', '[\0]', $val)) // LIKE because of text but it does not work with datetime
: " = " . unconvert_field($fields[$key], q($val)))))
: " = " . unconvert_field($field, q($val)))))
; //! enum and set
if (JUSH == "sql" && preg_match('~char|text~', $field_type) && preg_match("~[^ -@]~", $val)) { // not just [a-z] to catch non-ASCII characters
$return[] = "$column = " . q($val) . " COLLATE " . charset($connection) . "_bin";

View File

@@ -352,7 +352,7 @@ function process_input($field) {
return null;
}
$idf = bracket_escape($field["field"]);
$function = $_POST["function"][$idf];
$function = idx($_POST["function"], $idf);
$value = $_POST["fields"][$idf];
if ($field["type"] == "enum" || $driver->enumLength($field)) {
if ($value == -1) {
@@ -453,7 +453,7 @@ function edit_form($table, $fields, $row, $update) {
$autofocus = !$_POST;
foreach ($fields as $name => $field) {
echo "<tr><th>" . $adminer->fieldName($field);
$default = $_GET["set"][bracket_escape($name)];
$default = idx($_GET["set"], bracket_escape($name));
if ($default === null) {
$default = $field["default"];
if ($field["type"] == "bit" && preg_match("~^b'([01]*)'\$~", $default, $regs)) {
@@ -477,7 +477,7 @@ function edit_form($table, $fields, $row, $update) {
$value = $adminer->editVal($value, $field);
}
$function = ($_POST["save"]
? (string) $_POST["function"][$name]
? idx($_POST["function"], $name, "")
: ($update && preg_match('~^CURRENT_TIMESTAMP~i', $field["on_update"])
? "now"
: ($value === false ? null : ($value !== null ? '' : 'NULL'))

View File

@@ -33,8 +33,8 @@ if ($_POST && !$error && !$_POST["add"] && !$_POST["drop_col"]) {
ksort($index["columns"]);
foreach ($index["columns"] as $key => $column) {
if ($column != "") {
$length = $index["lengths"][$key];
$desc = $index["descs"][$key];
$length = idx($index["lengths"], $key);
$desc = idx($index["descs"], $key);
$set[] = idf_escape($column) . ($length ? "(" . (+$length) . ")" : "") . ($desc ? " DESC" : "");
$columns[] = $column;
$lengths[] = ($length ?: null);

View File

@@ -32,8 +32,8 @@ foreach (table_status('', true) as $table => $table_status) {
foreach ($adminer->foreignKeys($table) as $val) {
if (!$val["db"]) {
$left = $base_left;
if ($table_pos[$table][1] || $table_pos[$val["table"]][1]) {
$left = min(floatval($table_pos[$table][1]), floatval($table_pos[$val["table"]][1])) - 1;
if (idx($table_pos[$table], 1) || idx($table_pos[$val["table"]], 1)) {
$left = min(idx($table_pos[$table], 1, 0), idx($table_pos[$val["table"]], 1, 0)) - 1;
} else {
$base_left -= .1;
}
@@ -71,7 +71,7 @@ foreach ($schema as $name => $table) {
foreach ((array) $table["references"] as $target_name => $refs) {
foreach ($refs as $left => $ref) {
$left1 = $left - $table_pos[$name][1];
$left1 = $left - idx($table_pos[$name], 1);
$i = 0;
foreach ($ref[0] as $source) {
echo "\n<div class='references' title='" . h($target_name) . "' id='refs$left-" . ($i++) . "' style='left: $left1" . "em; top: " . $table["fields"][$source]["pos"] . "em; padding-top: .5em;'>"
@@ -83,7 +83,7 @@ foreach ($schema as $name => $table) {
foreach ((array) $referenced[$name] as $target_name => $refs) {
foreach ($refs as $left => $columns) {
$left1 = $left - $table_pos[$name][1];
$left1 = $left - idx($table_pos[$name], 1);
$i = 0;
foreach ($columns as $target) {
echo "\n<div class='references arrow' title='" . h($target_name) . "' id='refd$left-" . ($i++) . "' style='left: $left1" . "em; top: " . $table["fields"][$target]["pos"] . "em;'>"

View File

@@ -339,7 +339,7 @@ if (!$columns && support("table")) {
$rank = 1;
foreach ($rows[0] as $key => $val) {
if (!isset($unselected[$key])) {
$val = $_GET["columns"][key($select)];
$val = idx($_GET["columns"], key($select)) ?: array();
$field = $fields[$select ? ($val ? $val["col"] : current($select)) : $key];
$name = ($field ? $adminer->fieldName($field, $rank) : ($val["fun"] ? "*" : h($key)));
if ($name != "") {
@@ -450,7 +450,7 @@ if (!$columns && support("table")) {
$val = select_value($val, $link, $field, $text_length);
$id = h("val[$unique_idf][" . bracket_escape($key) . "]");
$value = $_POST["val"][$unique_idf][bracket_escape($key)];
$value = idx(idx($_POST["val"], $unique_idf), bracket_escape($key));
$editable = !is_array($row[$key]) && is_utf8($val) && $rows[$n][$key] == $row[$key] && !$functions[$key] && !$field["generated"];
$text = preg_match('~text|json|lob~', $field["type"]);
echo "<td id='$id'" . (preg_match(number_type(), $field["type"]) && is_numeric(strip_tags($val)) ? " class='number'" : "");

View File

@@ -40,7 +40,7 @@ if (!extension_loaded("xdebug")) {
for ($l=0; $l <= count($file); $l++) {
$line = $file[$l];
$color = "#C0FFC0"; // tested
switch ($coverage[realpath($filename)][$l+1]) {
switch ($coverage[realpath($filename)][$l+1] ?? null) {
case -1: // untested
$color = "#FFC0C0";
break;

View File

@@ -259,7 +259,7 @@ ORDER BY ORDINAL_POSITION", null, "") as $row
}
$key = $keys[$name];
$i--;
echo "<div>" . h($desc) . input_hidden("where[$i][col]", $name) . input_hidden("where[$i][op]", "=") . ": <select name='where[$i][val]'>" . optionlist($options, $where[$key]["val"], true) . "</select></div>\n";
echo "<div>" . h($desc) . input_hidden("where[$i][col]", $name) . input_hidden("where[$i][op]", "=") . ": <select name='where[$i][val]'>" . optionlist($options, idx($where[$key], "val"), true) . "</select></div>\n";
unset($columns[$name]);
}
}
@@ -294,7 +294,7 @@ ORDER BY ORDINAL_POSITION", null, "") as $row
}
if ($orders) {
echo '<fieldset><legend>' . lang('Sort') . "</legend><div>";
echo "<select name='index_order'>" . optionlist(array("" => "") + $orders, ($_GET["order"][0] != "" ? "" : $_GET["index_order"]), true) . "</select>";
echo "<select name='index_order'>" . optionlist(array("" => "") + $orders, (idx($_GET["order"], 0) != "" ? "" : $_GET["index_order"]), true) . "</select>";
echo "</div></fieldset>\n";
}
if ($_GET["order"]) {

View File

@@ -274,7 +274,7 @@ if (isset($_GET["elastic"])) {
function delete($table, $queryWhere, $limit = 0) {
//! use $limit
$ids = array();
if (isset($_GET["where"]["_id"]) && $_GET["where"]["_id"]) {
if (idx($_GET["where"], "_id")) {
$ids[] = $_GET["where"]["_id"];
}
if (isset($_POST['check'])) {