<?php
/*************************************************************************************************

    ddnsshowhostname.php

    Show DDNS hostname record

        2013.07.27 Ver.1.0 Masashi.org
        2019.10.04 Ver.1.1 Masashi.org (mysqli etc)

***************************************************************************************************/
include "ddnscommon.php";

//
include "del_nday_file.php";
if(
del_nday_file(1)){die();}
//
/**** Settings ****/
/* List of Domain names */
$link mysqli_connect($db_host $db_user $db_pass $db_name);
if (
mysqli_connect_errno() > 0) {
  die(
"DB Connection error: " mysqli_connect_error());
}
$sql "SELECT domain FROM domain";
$rs mysqli_query($link,$sql);
$ct mysqli_num_rows($rs);
for(
$i=0;$i<$ct;$i++){
        
$row mysqli_fetch_row($rs);
        
$domains[$i] = $row[0];
}

mysqli_close($link);

/**** Initialize POST data into array ****/
$array = array("username","password","confirm");
$sizeofarray sizeof($array);
for(
$i=0;$i<$sizeofarray;$i++){
    $
$array[$i] = $_POST["{$array[$i]}"];
}

/******************************  Output HTML headers  **********************************************/
include "ddnsheader.php";
echo<<<EOH
<TITLE>Show Hostname Records for DDNS -{$org_name}-</TITLE>
</HEAD>
EOH;
echo<<<EOH
<BODY>
<h1>Show Hostname Records for DDNS</h1>
<br />
EOH;

/*************************************************************************************************

    If there is no POST data, display Input Form.

        -If any of them is missing, go to input form.
        -If any one of them is sent, warn items missing.
        -If IP address is not provided by POST method, get ip address from the remote host address.

***************************************************************************************************/
if(!($username && $password)){
    if(
$username || $password){
        if(!
$username){
            
$msg .= "User ID is missing.<br />";
        }
        if(!
$password){
            
$msg .= "Password is missing.<br />";
        }
        echo 
"<font color=\"red\">$msg</font>";
    }
echo <<<EOF
<form action="{$_SERVER['SCRIPT_NAME']}" method="post">
    <table border=0>
        <tr>
            <td>User ID</td>
            <td>:</td>
            <td><input type="text" name="username" size=20 maxlength=20 value="
$username"></td>
        </tr>
        <tr>
            <td>Password</td>
            <td>:</td>
            <td><input type="password" name="password" size=20 maxlength=20></td>
        </tr>
    </table>
    Show All Records:<input type="checkbox" name="confirm" value="1"><font color="red">(Privilege is required)</font><br />
    <br />
    <input type="submit" value="Show Records">
</form>
EOF;

/*************************************************************************************************

    If POST data is ready, process them
       compare with data in the database.

***************************************************************************************************/
}else{
    
$link mysqli_connect($db_host $db_user $db_pass $db_name);
    if (
mysqli_connect_errno() > 0) {
      die(
"DB Connection error: " mysqli_connect_error());
    }
    
$pw_md5 md5($password);
    
$sql sprintf("SELECT * FROM userdata WHERE username='%s' AND password='%s'",
        
mysqli_real_escape_string($link,$username),
        
mysqli_real_escape_string($link,$pw_md5));
    
$rs mysqli_query($link,$sql);
    
$item mysqli_fetch_array($rs);
    if(!
$item){
        echo 
"Username or Password is not correct.";
    }else{
        if(
$confirm && $item['su']){
            
$sql "SELECT * FROM hostdata";
        }else{
            
$sql sprintf("SELECT * FROM hostdata WHERE username='%s'",
                
mysqli_real_escape_string($link,$username));
        }
        
$rs mysqli_query($link,$sql);
        
$ct mysqli_num_rows($rs);
echo <<<EOF
    <table border=1>
    <tr>
        <th width="100">User ID</th>
        <th width="150">Hostname</th>
        <th width="120">Current IP</th>
        <th width="150">Last Access</th>
        <th width="150">Last Update</th>
        <th width="120">Registration IP</th>
        <th width="150">Registration Date</th>
    </tr>
EOF;
        for(
$i=0;$i<$ct;$i++){
                
$item mysqli_fetch_array($rs);
            echo 
"<tr>\n";
            echo 
"<td>",$item['username'],"</td>\n";
            echo 
"<td>",$item['hostname'],"</td>\n";
            echo 
"<td>",$item['current_ip'],"</td>\n";
            echo 
"<td>",$item['last_access'],"</td>\n";
            echo 
"<td>",$item['last_update'],"</td>\n";
            echo 
"<td>",$item['registration_ip'],"</td>\n";
            echo 
"<td>",$item['registration_date'],"</td>\n";
            echo 
"</tr>\n";
        }
        
mysqli_close($link);
echo <<<EOF
    </table>
    <br />
        <form action="
{$_SERVER['SCRIPT_NAME']}" method="post">
            <input type="hidden" name="username" value=
$username>
            <input type="hidden" name="password" value=
$password>
            <input type="hidden" name="confirm" value=
$confirm>
            <input type="submit" value="Reload">
        </form>
EOF;
    }
}
?>
<hr />
<a href="./">Return to home</a> -<?php echo $org_name?>-
</BODY>
</HTML>