From a63fadd5034a8300ecd0573f4e1fe4c07c383064 Mon Sep 17 00:00:00 2001 From: Peter Knut Date: Mon, 7 Oct 2024 22:20:32 +0200 Subject: [PATCH] Refactor working with a locked file --- adminer/include/auth.inc.php | 8 ++--- adminer/include/bootstrap.inc.php | 6 ++-- adminer/include/functions.inc.php | 57 ++++++++++++++++++------------- 3 files changed, 40 insertions(+), 31 deletions(-) diff --git a/adminer/include/auth.inc.php b/adminer/include/auth.inc.php index 099843bc..603bd470 100644 --- a/adminer/include/auth.inc.php +++ b/adminer/include/auth.inc.php @@ -82,11 +82,11 @@ function build_http_url($server, $username, $password, $defaultServer, $defaultP function add_invalid_login() { global $adminer; - $fp = file_open_lock(get_temp_dir() . "/adminer.invalid"); - if (!$fp) { + $file = open_file_with_lock(get_temp_dir() . "/adminer.invalid"); + if (!$file) { return; } - $invalids = unserialize(stream_get_contents($fp)); + $invalids = unserialize(stream_get_contents($file)); $time = time(); if ($invalids) { foreach ($invalids as $ip => $val) { @@ -100,7 +100,7 @@ function add_invalid_login() { $invalid = array($time + 30*60, 0); // active for 30 minutes } $invalid[1]++; - file_write_unlock($fp, serialize($invalids)); + write_and_unlock_file($file, serialize($invalids)); } function check_invalid_login() { diff --git a/adminer/include/bootstrap.inc.php b/adminer/include/bootstrap.inc.php index 6951da57..b884ee33 100644 --- a/adminer/include/bootstrap.inc.php +++ b/adminer/include/bootstrap.inc.php @@ -32,9 +32,9 @@ if (isset($_GET["file"])) { } if ($_GET["script"] == "version") { - $fp = file_open_lock(get_temp_dir() . "/adminer.version"); - if ($fp) { - file_write_unlock($fp, serialize(array("signature" => $_POST["signature"], "version" => $_POST["version"]))); + $file = open_file_with_lock(get_temp_dir() . "/adminer.version"); + if ($file) { + write_and_unlock_file($file, serialize(["signature" => $_POST["signature"], "version" => $_POST["version"]])); } exit; } diff --git a/adminer/include/functions.inc.php b/adminer/include/functions.inc.php index be585333..67e5e583 100644 --- a/adminer/include/functions.inc.php +++ b/adminer/include/functions.inc.php @@ -1215,33 +1215,42 @@ function get_temp_dir() { return $return; } -/** Open and exclusively lock a file -* @param string -* @return resource or null for error -*/ -function file_open_lock($filename) { - $fp = @fopen($filename, "r+"); // @ - may not exist - if (!$fp) { // c+ is available since PHP 5.2.6 - $fp = @fopen($filename, "w"); // @ - may not be writable - if (!$fp) { - return; - } - chmod($filename, 0660); +/** + * Opens and exclusively lock a file. + * + * @param string $filename + * @return resource|null + */ +function open_file_with_lock($filename) +{ + $file = fopen($filename, "c+"); + if (!$file) { + return null; } - flock($fp, LOCK_EX); - return $fp; + + chmod($filename, 0660); + + if (!flock($file, LOCK_EX)) { + fclose($file); + return null; + } + + return $file; } -/** Write and unlock a file -* @param resource -* @param string -*/ -function file_write_unlock($fp, $data) { - rewind($fp); - fwrite($fp, $data); - ftruncate($fp, strlen($data)); - flock($fp, LOCK_UN); - fclose($fp); +/** + * Writes and unlocks a file. + * + * @param resource $file + * @param string $data + */ +function write_and_unlock_file($file, $data) +{ + rewind($file); + fwrite($file, $data); + ftruncate($file, strlen($data)); + flock($file, LOCK_UN); + fclose($file); } /**