coax / hmailserver-webadmin

hMailServer PHPWebAdmin redesign project
https://www.hmailserver.com
77 stars 19 forks source link

Enhancement Suggestion hm_securityranges #52

Closed gotspatel closed 3 hours ago

gotspatel commented 5 hours ago

Country and Flag is better outside in hm_securityranges instead of hm_securityrange, that way we don't have to open each IP to know where it is from, what do you say?

hm_securityranges.php changes as below if you approve

<thead>
          <tr>
            <th data-sort="string"><?php EchoTranslation("Name")?></th>
            <th style="width:20%;" data-sort="ipaddress"><?php EchoTranslation("IP address") ?></th>
            <th style="width:10%;" data-sort="int"><?php EchoTranslation("Priority") ?></th>
            <th style="width:20%;" data-sort="int"><?php EchoTranslation("Expires") ?></th>
            <th style="width:20%;" data-sort="int"><?php EchoTranslation("Country") ?></th>
            <th style="width:32px;" class="no-sort">&nbsp;</th>
          </tr>
        </thead>

....

if ($Count>0) {
    for ($i = 0; $i < $Count; $i++) {
        $obSecurityRange = $obSecurityRanges->Item($i);
        $securityrangename = $obSecurityRange->Name;
        $securityrangeid = $obSecurityRange->ID;

        $securityrangename = PreprocessOutput($securityrangename);
        $securityrangepriority = $obSecurityRange->Priority;
        $ExpiresTime = $obSecurityRange->Expires ? humanTiming(strtotime(makeIsoDate($obSecurityRange->ExpiresTime))) : Translate("Never");
        $GeoCountry = GeoIp($obSecurityRange->LowerIP);
        $LowerIp = $obSecurityRange->LowerIP;

        echo '          <tr>
            <td><a href="?page=securityrange&action=edit&securityrangeid=' . $securityrangeid . '"' . ($obSecurityRange->Expires!==false?' class="red"':'') . '>' . $securityrangename . '</a></td>
            <td>' . $LowerIp . '</td>
            <td>' . $securityrangepriority . '</td>
            <td data-sort-value="' . strtotime($obSecurityRange->ExpiresTime) . '">' . $ExpiresTime . '</td>
            <td>' . $GeoCountry . '</td>
            <td><a href="#" onclick="return Confirm(\'' . $str_confirm . ' <b>' . $securityrangename . '</b>:\',\'' . $str_yes . '\',\'' . $str_no . '\',\'?page=background_securityrange_save&csrftoken=' . $csrftoken . '&action=delete&securityrangeid=' . $securityrangeid . '\');" class="delete" title="' . $str_delete . '">' . $str_delete . '</a></td>
          </tr>' . PHP_EOL;
    }

Firefox_Screenshot_2024-10-19T10-13-43 283Z

coax commented 4 hours ago

Unfortunately, this would result in your host's IP being blocked from ip-api.com due to hammering their API.

gotspatel commented 4 hours ago

You are correct, i am trying to updating it to use localhosted maxmind data https://github.com/palinkas-jo-reggelt/GeoIP_Server

coax commented 4 hours ago

That certainly is an option anyone can do on their own, but will not be implemented into hMailAdmin as it requires constant db updates with new data.

gotspatel commented 3 hours ago

yes and I am posting here the function (for If anyone wants to use it)

USE WITH CAUTION it requires https://github.com/palinkas-jo-reggelt/GeoIP_Server

ALSO CHANGE the YOUR_GEOIP_URL in here ---> http://YOUR_GEOIP_URL/api.php?geoip=

function GeoIp($ip) {
    global $obLanguage;
    if ($ip === '0.0.0.0') return Translate('Unknown');
    $regex = '/(127\.0\.0\.1)|^(10\.)|^(192\.168\.)|^(169\.254\.)|^(172\.(1[6-9]|2[0-9]|3[0-1]))/';
    if (preg_match($regex, $ip)) return Translate('Local IP range');

    set_error_handler(function() { /* Ignore errors */ });

    try {
    // Fetch the JSON data from the LOCAL API
    $json = file_get_contents('http://YOUR_GEOIP_URL/api.php?geoip=' . $ip);
    $parsed = json_decode($json);
    // Access country_code and country_name directly
    if (isset($parsed->data->country_code)) {
        return '<p><img src="flags/' . $parsed->data->country_code . '.gif" style="margin-right:5px;">' . $parsed->data->country_name . '</p>';
    } else {
        return Translate('Unknown');
    }
    } catch (Exception $e) {
        // Handle the error if necessary
    }
    restore_error_handler();
}