devshort

private self-hosted shortlink service
git clone https://git.clttr.info/devshort.git
Log (Feed) | Files | Refs (Tags) | README | LICENSE

commit 2a20e1605ae8014610bd4b4f02dc9ea6e2ba256e
parent 9b04447d02800d60ff1b65361fd1f1aa9b4512da
Author: René Wagner <rwagner@rw-net.de>
Date:   Sun, 22 Mar 2020 21:01:44 +0100

add file locking and shorten time distance between read & write of stats.json

Diffstat:
Madmin.php | 4++--
Mindex.php | 2+-
Mredirect.php | 7++++---
3 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/admin.php b/admin.php @@ -42,8 +42,8 @@ if (isset($_GET["delete"]) || isset($_GET["add"])) { $config_content["shortlinks"][$filtered["name"]] = $filtered["url"]; $stats_content[$filtered["name"]] = array(); } - file_put_contents($config_path, json_encode($config_content, JSON_PRETTY_PRINT)); - file_put_contents($stats_path, json_encode($stats_content, JSON_PRETTY_PRINT)); + file_put_contents($config_path, json_encode($config_content, JSON_PRETTY_PRINT), LOCK_EX); + file_put_contents($stats_path, json_encode($stats_content, JSON_PRETTY_PRINT), LOCK_EX); header("Content-Type: application/json"); echo "{\"status\": \"successful\"}"; exit; diff --git a/index.php b/index.php @@ -9,7 +9,7 @@ $stats_content = json_decode(file_get_contents($stats_path), true); // Count the access $stats_content["index"][date("Y-m-d")] += 1; -file_put_contents($stats_path, json_encode($stats_content, JSON_PRETTY_PRINT)); +file_put_contents($stats_path, json_encode($stats_content, JSON_PRETTY_PRINT), LOCK_EX); // Generate custom buttons for the footer $links_string = ""; diff --git a/redirect.php b/redirect.php @@ -13,13 +13,14 @@ if (in_array($short, $return_404)) { $config_path = implode(DIRECTORY_SEPARATOR, array(__DIR__, "data", "config.json")); $config_content = json_decode(file_get_contents($config_path), true); $stats_path = implode(DIRECTORY_SEPARATOR, array(__DIR__, "data", "stats.json")); -$stats_content = json_decode(file_get_contents($stats_path), true); // Count the access to the given $name function count_access($name) { - global $stats_path, $stats_content; + global $stats_path; + + $stats_content = json_decode(file_get_contents($stats_path), true); $stats_content[$name][date("Y-m-d")] += 1; - file_put_contents($stats_path, json_encode($stats_content, JSON_PRETTY_PRINT)); + file_put_contents($stats_path, json_encode($stats_content, JSON_PRETTY_PRINT), LOCK_EX); } if (array_key_exists($short, $config_content["shortlinks"])) {