diff options
author | Erich Eckner <git@eckner.net> | 2019-03-30 20:42:33 +0100 |
---|---|---|
committer | Erich Eckner <git@eckner.net> | 2019-03-30 20:42:33 +0100 |
commit | c047b6d28b0cbe26d3e86befde90353d321042ff (patch) | |
tree | 8fdb4758956a0938833bd932dd82f98a3a5423bc | |
parent | 0fa37daae3efe20c57665bd59f33768733b200be (diff) | |
download | ddns-c047b6d28b0cbe26d3e86befde90353d321042ff.tar.xz |
ddns.php: style
-rw-r--r-- | ddns.php | 82 |
1 files changed, 38 insertions, 44 deletions
@@ -1,89 +1,83 @@ <?php function lifetime($addr) { - $addrTeile=explode(".",$addr); - if (($addrTeile[0]=="10") || - (($addrTeile[0]=="192") && ($addrTeile[1]=="168")) || - (($addrTeile[0]=="172") && ($addrTeile[1]>=16) && ($addrTeile[1]<32))) - return "7200"; - return "120"; + $addrTeile = explode('.', $addr); + if (($addrTeile[0] == '10') || + (($addrTeile[0] == '192') && ($addrTeile[1] == '168')) || + (($addrTeile[0] == '172') && ($addrTeile[1] >= 16) && ($addrTeile[1] < 32))) + return '7200'; + return '120'; }; // invalid syntax? - if ((!array_key_exists('i',$_GET)) || - (strlen($_GET["i"])!=64) || - (preg_match("@^[0-9A-Za-z]{64}$@",$_GET["i"])!=1)) + if ((!array_key_exists('i', $_GET)) || + (strlen($_GET['i']) != 64) || + (preg_match('@^[0-9A-Za-z]{64}$@', $_GET['i']) != 1)) die(); - $line=preg_grep("@^".$_GET["i"]." @", file("/srv/http/vhosts/eckner.net/ddns/tokens")); + $line = preg_grep('@^' . $_GET['i'] . ' @', file('/srv/http/vhosts/eckner.net/ddns/tokens')); // unknown token? - if (count($line)!=1) + if (count($line) != 1) die(); - $domain=substr(explode(" ",implode("",$line))[1],0,-1); - if (isset($_GET["address"])) - $ips=explode(",",$_GET["address"]); + $domain = substr(explode(' ', implode('', $line))[1], 0, -1); + if (array_key_exists('address', $_GET)) + $ips = explode(',', $_GET['address']); else - $ips[0]="auto"; + $ips[0] = 'auto'; foreach ($ips as $i => $ip) - if ($ip == "auto") - $ips[$i]=$_SERVER["REMOTE_ADDR"]; + if ($ip == 'auto') + $ips[$i] = $_SERVER['REMOTE_ADDR']; - $ips=array_unique($ips); + $ips = array_unique($ips); unset($hasRrType); foreach ($ips as $i => $ip) { - if (filter_var($ip,FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { - $rrTypes[$i]="A"; - $hasRrType["A"]=1; + if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { + $rrTypes[$i] = 'A'; + $hasRrType['A'] = 1; } - elseif (filter_var($ip,FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { - $rrTypes[$i]="AAAA"; - $hasRrType["AAAA"]=1; + elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { + $rrTypes[$i] = 'AAAA'; + $hasRrType['AAAA'] = 1; } else die(); } - $updateCommand=""; - $logContent=""; + $updateCommand = ''; + $logContent = ''; foreach ($hasRrType as $rrType => $dummy) { // delete old record if existing - $updateCommand= - $updateCommand. + $updateCommand .= "zone ddns.eckner.net.\n". - "prereq yxrrset ".$domain.".ddns.eckner.net IN ".$rrType."\n". - "update delete ".$domain.".ddns.eckner.net IN ".$rrType."\n". + "prereq yxrrset " . $domain . ".ddns.eckner.net IN " . $rrType . "\n". + "update delete " . $domain . ".ddns.eckner.net IN " . $rrType . "\n". "send\n". "zone ddns.eckner.net.\n"; // set new records no matter what foreach ($ips as $i => $ip) if ($rrTypes[$i] == $rrType) { - $updateCommand= - $updateCommand. - "update add ".$domain.".ddns.eckner.net. ".lifetime($ip)." IN ".$rrType." ".$ip."\n"; - $logContent= - $logContent. - date("Y-m-d H:i:s")." ".$domain." ".$ip."\n"; + $updateCommand .= + "update add " . $domain . ".ddns.eckner.net. " . lifetime($ip) . " IN " . $rrType . " " . $ip . "\n"; + $logContent .= + date('Y-m-d H:i:s') . ' ' . $domain . ' ' . $ip . "\n"; } - $updateCommand= - $updateCommand. - "send\n"; + $updateCommand .= + "send\n"; } // actually do something - $pin=popen("nsupdate -l","w"); + $pin = popen('nsupdate -l', 'w'); if ($pin === FALSE) die(); - fwrite($pin,$updateCommand); + fwrite($pin, $updateCommand); pclose($pin); - file_put_contents("/srv/http/vhosts/eckner.net/ddns/log", $logContent, FILE_APPEND | LOCK_EX); - -?> + file_put_contents('/srv/http/vhosts/eckner.net/ddns/log', $logContent, FILE_APPEND | LOCK_EX); |