From dd122a1056604214b21688f6a6951423b34c9c49 Mon Sep 17 00:00:00 2001 From: Peter Knut Date: Wed, 9 Oct 2024 19:14:22 +0200 Subject: [PATCH] Clean up the code for PHP < 5.6 --- adminer/drivers/mssql.inc.php | 2 +- adminer/drivers/mysql.inc.php | 8 +++--- adminer/drivers/pgsql.inc.php | 2 +- adminer/include/functions.inc.php | 10 +------- editor/include/editing.inc.php | 2 +- plugins/drivers/simpledb.php | 20 ++------------- plugins/plugin.php | 41 ++++++++++++++----------------- 7 files changed, 28 insertions(+), 57 deletions(-) diff --git a/adminer/drivers/mssql.inc.php b/adminer/drivers/mssql.inc.php index e2b9755c..e7ce1b69 100644 --- a/adminer/drivers/mssql.inc.php +++ b/adminer/drivers/mssql.inc.php @@ -432,7 +432,7 @@ WHERE OBJECT_NAME(i.object_id) = " . q($table) function error() { global $connection; - return nl_br(h(preg_replace('~^(\[[^]]*])+~m', '', $connection->error))); + return nl2br(h(preg_replace('~^(\[[^]]*])+~m', '', $connection->error))); } function create_database($db, $collation) { diff --git a/adminer/drivers/mysql.inc.php b/adminer/drivers/mysql.inc.php index 3cce3888..3a2e1dbc 100644 --- a/adminer/drivers/mysql.inc.php +++ b/adminer/drivers/mysql.inc.php @@ -14,7 +14,7 @@ if (!defined("DRIVER")) { function connect($server = "", $username = "", $password = "", $database = null, $port = null, $socket = null) { global $adminer; - mysqli_report(MYSQLI_REPORT_OFF); // stays between requests, not required since PHP 5.3.4 + mysqli_report(MYSQLI_REPORT_OFF); list($host, $port) = explode(":", $server, 2); // part after : is used for port or socket $ssl = $adminer->connectSsl(); @@ -34,7 +34,7 @@ if (!defined("DRIVER")) { $database, (is_numeric($port) ? $port : ini_get("mysqli.default_port")), (!is_numeric($port) ? $port : $socket), - ($ssl ? 64 : 0) // 64 - MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT (not available before PHP 5.6.16) + ($ssl ? MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT : 0) ); $this->options(MYSQLI_OPT_LOCAL_INFILE, false); return $return; @@ -262,7 +262,7 @@ if (!defined("DRIVER")) { } function set_charset($charset) { - $this->query("SET NAMES $charset"); // charset in DSN is ignored before PHP 5.3.6 + $this->query("SET NAMES $charset"); } function select_db($database) { @@ -375,7 +375,7 @@ if (!defined("DRIVER")) { $connection = new Min_DB; $credentials = $adminer->credentials(); if ($connection->connect($credentials[0], $credentials[1], $credentials[2])) { - $connection->set_charset(charset($connection)); // available in MySQLi since PHP 5.0.5 + $connection->set_charset(charset($connection)); $connection->query("SET sql_quote_show_create = 1, autocommit = 1"); if (min_version('5.7.8', 10.2, $connection)) { $structured_types[lang('Strings')][] = "json"; diff --git a/adminer/drivers/pgsql.inc.php b/adminer/drivers/pgsql.inc.php index 4f22d95a..36af2f3c 100644 --- a/adminer/drivers/pgsql.inc.php +++ b/adminer/drivers/pgsql.inc.php @@ -488,7 +488,7 @@ ORDER BY connamespace, conname") as $row) { if (preg_match('~^(.*\n)?([^\n]*)\n( *)\^(\n.*)?$~s', $return, $match)) { $return = $match[1] . preg_replace('~((?:[^&]|&[^;]*;){' . strlen($match[3]) . '})(.*)~', '\1\2', $match[2]) . $match[4]; } - return nl_br($return); + return nl2br($return); } function create_database($db, $collation) { diff --git a/adminer/include/functions.inc.php b/adminer/include/functions.inc.php index 44ab7e0b..d24ce841 100644 --- a/adminer/include/functions.inc.php +++ b/adminer/include/functions.inc.php @@ -158,14 +158,6 @@ function h($string) { return str_replace("\0", "�", htmlspecialchars($string, ENT_QUOTES, 'utf-8')); } -/** Convert \n to
-* @param string -* @return string -*/ -function nl_br($string) { - return str_replace("\n", "
", $string); // nl2br() uses XHTML before PHP 5.3 -} - /** Generate HTML checkbox * @param string * @param string @@ -956,7 +948,7 @@ function input($field, $value, $function) { if (version_compare(PHP_VERSION, 5.4) >= 0) { $args[] = JSON_PRETTY_PRINT; } - $value = call_user_func_array('json_encode', $args); //! requires PHP 5.2 + $value = call_user_func_array('json_encode', $args); $function = "json"; } $reset = ($jush == "mssql" && $field["auto_increment"]); diff --git a/editor/include/editing.inc.php b/editor/include/editing.inc.php index f11a9162..7d8d580c 100644 --- a/editor/include/editing.inc.php +++ b/editor/include/editing.inc.php @@ -17,7 +17,7 @@ function email_header($header) { * @return bool */ function send_mail($email, $subject, $message, $from = "", $files = array()) { - $eol = (DIRECTORY_SEPARATOR == "/" ? "\n" : "\r\n"); // PHP_EOL available since PHP 5.0.2 + $eol = "\r\n"; $message = str_replace("\n", $eol, wordwrap(str_replace("\r", "", "$message\n"))); $boundary = uniqid("boundary"); $attachments = ""; diff --git a/plugins/drivers/simpledb.php b/plugins/drivers/simpledb.php index 6a2cde57..75a408a4 100644 --- a/plugins/drivers/simpledb.php +++ b/plugins/drivers/simpledb.php @@ -272,7 +272,7 @@ if (isset($_GET["simpledb"])) { function rollback() { return false; } - + function slowQuery($query, $timeout) { $this->_conn->timeout = $timeout; return $query; @@ -437,22 +437,6 @@ if (isset($_GET["simpledb"])) { function last_id() { } - function hmac($algo, $data, $key, $raw_output = false) { - // can use hash_hmac() since PHP 5.1.2 - $blocksize = 64; - if (strlen($key) > $blocksize) { - $key = pack("H*", $algo($key)); - } - $key = str_pad($key, $blocksize, "\0"); - $k_ipad = $key ^ str_repeat("\x36", $blocksize); - $k_opad = $key ^ str_repeat("\x5C", $blocksize); - $return = $algo($k_opad . pack("H*", $algo($k_ipad . $data))); - if ($raw_output) { - $return = pack("H*", $return); - } - return $return; - } - function sdb_request($action, $params = array()) { global $adminer, $connection; list($host, $params['AWSAccessKeyId'], $secret) = $adminer->credentials(); @@ -467,7 +451,7 @@ if (isset($_GET["simpledb"])) { $query .= '&' . rawurlencode($key) . '=' . rawurlencode($val); } $query = str_replace('%7E', '~', substr($query, 1)); - $query .= "&Signature=" . urlencode(base64_encode(hmac('sha1', "POST\n" . preg_replace('~^https?://~', '', $host) . "\n/\n$query", $secret, true))); + $query .= "&Signature=" . urlencode(base64_encode(hash_hmac('sha1', "POST\n" . preg_replace('~^https?://~', '', $host) . "\n/\n$query", $secret, true))); @ini_set('track_errors', 1); // @ - may be disabled $file = @file_get_contents($connection->_url, false, stream_context_create(array('http' => array( diff --git a/plugins/plugin.php b/plugins/plugin.php index 065b0af9..6defead8 100644 --- a/plugins/plugin.php +++ b/plugins/plugin.php @@ -7,36 +7,31 @@ * @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other) */ class AdminerPlugin extends Adminer { - /** @access protected */ - var $plugins; - - function _findRootClass($class) { // is_subclass_of(string, string) is available since PHP 5.0.3 - do { - $return = $class; - } while ($class = get_parent_class($class)); - return $return; - } - - /** Register plugins - * @param array object instances or null to register all classes starting by 'Adminer' - */ - function __construct($plugins) { + protected $plugins; + + /** + * Registers plugins. + * @param array $plugins Object instances or null to register all classes starting by 'Adminer'. + */ + function __construct(array $plugins = null) + { if ($plugins === null) { - $plugins = array(); + $plugins = []; foreach (get_declared_classes() as $class) { - if (preg_match('~^Adminer.~i', $class) && strcasecmp($this->_findRootClass($class), 'Adminer')) { //! can use interface + if (preg_match('~^Adminer.~i', $class) && !is_subclass_of($class, 'Adminer')) { //! can use interface $plugins[$class] = new $class; } } } + $this->plugins = $plugins; //! it is possible to use ReflectionObject to find out which plugins defines which methods at once } - + function _callParent($function, $args) { return call_user_func_array(array('parent', $function), $args); } - + function _applyPlugin($function, $args) { foreach ($this->plugins as $plugin) { if (method_exists($plugin, $function)) { @@ -57,7 +52,7 @@ class AdminerPlugin extends Adminer { } return $this->_callParent($function, $args); } - + function _appendPlugin($function, $args) { $return = $this->_callParent($function, $args); foreach ($this->plugins as $plugin) { @@ -70,14 +65,14 @@ class AdminerPlugin extends Adminer { } return $return; } - + // appendPlugin - + function dumpFormat() { $args = func_get_args(); return $this->_appendPlugin(__FUNCTION__, $args); } - + function dumpOutput() { $args = func_get_args(); return $this->_appendPlugin(__FUNCTION__, $args); @@ -94,7 +89,7 @@ class AdminerPlugin extends Adminer { } // applyPlugin - + function name() { $args = func_get_args(); return $this->_applyPlugin(__FUNCTION__, $args);