From 560be744124308080677e556af6c6a8e27875c67 Mon Sep 17 00:00:00 2001 From: Felix Kretschmer Date: Thu, 21 Jun 2018 02:15:32 +0200 Subject: [PATCH] Update update.php Added domain support Store content to data.js Multi-tenant planned to support multiple domains Broadcast to DNS API planned --- update.php | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/update.php b/update.php index 4526ff0..e487a5c 100644 --- a/update.php +++ b/update.php @@ -4,6 +4,7 @@ * https://github.com/mattes/ * * Copyright 2012 Matthias Kadenbach + * Update 2018 Felix Kretschmer * Released under the MIT license * * Examples: @@ -42,10 +43,10 @@ define("IP_HTML_PAGE", ROOT . "ip.html"); // set an username -define("USERNAME", "aladin"); +define("USERNAME", "dyndns"); // set a secure password -define("PASSWORD", "magic"); +define("PASSWORD", "magicKey"); // lock user after number of failed login attempts // if user got locked empty data.json manually @@ -87,7 +88,7 @@ function send_status_and_exit($code, $text) { // load and restore values from data file $data = json_decode(@file_get_contents(DATA_FILE_PATH), true); if(!$data) { - $data = array("current_ip4addr" => null, "current_ip6addr" => null, "last_update_timestamp" => null, "failed_logins" => 0); + $data = array("domain" => null, "current_ip4addr" => null, "current_ip6addr" => null, "last_update_timestamp" => null, "failed_logins" => 0); } // check failed logins and if username or password are not correct @@ -108,7 +109,10 @@ function send_status_and_exit($code, $text) { $_GET["ip4addr"] = "0.0.0.0"; $_GET["ip6addr"] = "0:0:0:0:0:0:0:0"; } - + +// get the domain +$domain = $_GET["domain"]; + // get ip4 and ip6 address $ip4addr = filter_var((!empty($_GET["ip4addr"]) ? $_GET["ip4addr"] : null), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); $ip6addr = filter_var((!empty($_GET["ip6addr"]) ? $_GET["ip6addr"] : null), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); @@ -121,21 +125,32 @@ function send_status_and_exit($code, $text) { if($ip4addr == $data["current_ip4addr"] && ipv(4)) send_status_and_exit(200, "nochg " . $ip4addr); if($ip6addr == $data["current_ip6addr"] && ipv(6)) send_status_and_exit(200, "nochg " . $ip6addr); -// fritzbox sends $_GET["domain"], too. ignore it. - // write changes to data file $data["last_update_timestamp"] = time(); $data["current_ip4addr"] = $ip4addr; $data["current_ip6addr"] = $ip6addr; +$data["domain"] = $domain; + if(!@file_put_contents(DATA_FILE_PATH, json_encode($data))) send_status_and_exit(500, "911 server is unable to write data file"); // create ip html page if(IP_HTML_PAGE) { $html = @file_get_contents(ROOT . "ip.template.html"); if($html === false) send_status_and_exit(500, "911 server is unable to read ip.template.html"); + + $html = str_replace(array("%DOMAIN%"), array($domain), $html); + $html = str_replace(array("%TIME%"), array(date("Y-m-d H:i:s",$data["last_update_timestamp"])), $html); - if(ipv(4)) $html = str_replace(array("%IP%"), array($ip4addr), $html); - if(ipv(6)) $html = str_replace(array("%IP%"), array($ip6addr), $html); + if(ipv(4)) { + $html = str_replace(array("%IPv4%"), array($ip4addr), $html); + } else { + $html = str_replace(array("%IPv4%"), array("no IPv4"), $html); + } + if(ipv(6)) { + $html = str_replace(array("%IPv6%"), array($ip6addr), $html); + } else { + $html = str_replace(array("%IPv6%"), array("no IPv6"), $html); + } if(!@file_put_contents(IP_HTML_PAGE, $html)) send_status_and_exit(500, "911 server is unable to create ip html page"); } @@ -143,8 +158,10 @@ function send_status_and_exit($code, $text) { // ready to broadcast the ip somewhere else ... // @todo +// multi-tenant +// fix v6 support // success! if(ipv(4)) send_status_and_exit(200, "good " . $ip4addr); -if(ipv(6)) send_status_and_exit(200, "good " . $ip6addr); \ No newline at end of file +if(ipv(6)) send_status_and_exit(200, "good " . $ip6addr);