mirror of
https://github.com/vrana/adminer.git
synced 2025-08-30 09:39:51 +02:00
Refactor working with a locked file
This commit is contained in:
@@ -82,11 +82,11 @@ function build_http_url($server, $username, $password, $defaultServer, $defaultP
|
|||||||
|
|
||||||
function add_invalid_login() {
|
function add_invalid_login() {
|
||||||
global $adminer;
|
global $adminer;
|
||||||
$fp = file_open_lock(get_temp_dir() . "/adminer.invalid");
|
$file = open_file_with_lock(get_temp_dir() . "/adminer.invalid");
|
||||||
if (!$fp) {
|
if (!$file) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$invalids = unserialize(stream_get_contents($fp));
|
$invalids = unserialize(stream_get_contents($file));
|
||||||
$time = time();
|
$time = time();
|
||||||
if ($invalids) {
|
if ($invalids) {
|
||||||
foreach ($invalids as $ip => $val) {
|
foreach ($invalids as $ip => $val) {
|
||||||
@@ -100,7 +100,7 @@ function add_invalid_login() {
|
|||||||
$invalid = array($time + 30*60, 0); // active for 30 minutes
|
$invalid = array($time + 30*60, 0); // active for 30 minutes
|
||||||
}
|
}
|
||||||
$invalid[1]++;
|
$invalid[1]++;
|
||||||
file_write_unlock($fp, serialize($invalids));
|
write_and_unlock_file($file, serialize($invalids));
|
||||||
}
|
}
|
||||||
|
|
||||||
function check_invalid_login() {
|
function check_invalid_login() {
|
||||||
|
@@ -32,9 +32,9 @@ if (isset($_GET["file"])) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($_GET["script"] == "version") {
|
if ($_GET["script"] == "version") {
|
||||||
$fp = file_open_lock(get_temp_dir() . "/adminer.version");
|
$file = open_file_with_lock(get_temp_dir() . "/adminer.version");
|
||||||
if ($fp) {
|
if ($file) {
|
||||||
file_write_unlock($fp, serialize(array("signature" => $_POST["signature"], "version" => $_POST["version"])));
|
write_and_unlock_file($file, serialize(["signature" => $_POST["signature"], "version" => $_POST["version"]]));
|
||||||
}
|
}
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
@@ -1215,33 +1215,42 @@ function get_temp_dir() {
|
|||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Open and exclusively lock a file
|
/**
|
||||||
* @param string
|
* Opens and exclusively lock a file.
|
||||||
* @return resource or null for error
|
*
|
||||||
*/
|
* @param string $filename
|
||||||
function file_open_lock($filename) {
|
* @return resource|null
|
||||||
$fp = @fopen($filename, "r+"); // @ - may not exist
|
*/
|
||||||
if (!$fp) { // c+ is available since PHP 5.2.6
|
function open_file_with_lock($filename)
|
||||||
$fp = @fopen($filename, "w"); // @ - may not be writable
|
{
|
||||||
if (!$fp) {
|
$file = fopen($filename, "c+");
|
||||||
return;
|
if (!$file) {
|
||||||
}
|
return null;
|
||||||
chmod($filename, 0660);
|
|
||||||
}
|
}
|
||||||
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
|
* Writes and unlocks a file.
|
||||||
* @param string
|
*
|
||||||
*/
|
* @param resource $file
|
||||||
function file_write_unlock($fp, $data) {
|
* @param string $data
|
||||||
rewind($fp);
|
*/
|
||||||
fwrite($fp, $data);
|
function write_and_unlock_file($file, $data)
|
||||||
ftruncate($fp, strlen($data));
|
{
|
||||||
flock($fp, LOCK_UN);
|
rewind($file);
|
||||||
fclose($fp);
|
fwrite($file, $data);
|
||||||
|
ftruncate($file, strlen($data));
|
||||||
|
flock($file, LOCK_UN);
|
||||||
|
fclose($file);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user