From ab2dca6c27ab05b78d58c653b6b3e53aad5a3b05 Mon Sep 17 00:00:00 2001 From: Ramon Caballero Date: Sat, 4 Apr 2026 13:56:26 +0100 Subject: [PATCH] Fix #1: handle invalid JSON and missing fields in Porkbun ping response --- pb-dydns.php | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/pb-dydns.php b/pb-dydns.php index c400e6d..f8d1e59 100755 --- a/pb-dydns.php +++ b/pb-dydns.php @@ -23,7 +23,7 @@ // // Append this line as many times as domains you want to automatically update: // -// */10 * * * * php /path/to/pb-dydns.php domain_name > /dev/null +// */10 * * * * php /path/to/pb-dydns.php domain_name > /dev/null // // And restart cron (I'm not sure if this is necessary): // @@ -41,7 +41,7 @@ $config_filename = __DIR__ . "/pb-dydns.json"; if (!file_exists($config_filename)) { - echo "The config file $config_filename does not exist." . PHP_EOL; + echo "The config file $config_filename does not exist." . PHP_EOL; $config = json_encode(array ( @@ -75,15 +75,35 @@ $myDomain = $argv[1]; $pbapi = new PorkbunAPI($config_filename); // Test connection to Porkbun API in order to get the public IP: -$result = json_decode($pbapi->ping()); -if ($result->status == "SUCCESS") $myIp = $result->yourIp; -else +$raw = $pbapi->ping(); +$result = json_decode($raw); + +if ($result === null) { - echo_to_cli("There was an error trying to ping Porkbun." . PHP_EOL); - var_dump($result); + echo_to_cli("Invalid JSON returned by Porkbun API:\n$raw\n"); exit(3); } +if (!isset($result->status)) +{ + echo_to_cli("Porkbun API response missing 'status' field:\n$raw\n"); + exit(3); +} + +if ($result->status !== "SUCCESS") +{ + echo_to_cli("Porkbun API returned an error:\n$raw\n"); + exit(3); +} + +if (!isset($result->yourIp)) +{ + echo_to_cli("Porkbun API did not return your public IP.\n"); + exit(3); +} + +$myIp = $result->yourIp; + echo_to_cli("Your public IP address is $myIp" . PHP_EOL); // Retrieve all DNS records associated with user's domain: