From 08d666985f237e1aee80a42ff4478767e36a7f66 Mon Sep 17 00:00:00 2001 From: andryyy Date: Sun, 6 Jun 2021 21:00:48 +0200 Subject: [PATCH] [Web] Improve last logins, switch to country name (workaround, fix in progress) --- data/web/inc/functions.inc.php | 11 +++++++---- data/web/js/site/user.js | 29 ++++++----------------------- data/web/user.php | 2 +- 3 files changed, 14 insertions(+), 28 deletions(-) diff --git a/data/web/inc/functions.inc.php b/data/web/inc/functions.inc.php index 8a605b03..45be1dd8 100644 --- a/data/web/inc/functions.inc.php +++ b/data/web/inc/functions.inc.php @@ -258,10 +258,11 @@ function last_login($action, $username, $sasl_limit = 10) { switch ($action) { case 'get': if (filter_var($username, FILTER_VALIDATE_EMAIL) && hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) { - $stmt = $pdo->prepare('SELECT `real_rip`, MAX(`datetime`) as `datetime`, `service` FROM `sasl_logs` + $stmt = $pdo->prepare('SELECT `real_rip`, MAX(`datetime`) as `datetime`, `service`, `app_password` FROM `sasl_logs` + LEFT OUTER JOIN `app_passwd` on `sasl_logs`.`app_password` = `app_passwd`.`id` WHERE `username` = :username AND `success` = 1 - GROUP BY `real_rip`, `service` + GROUP BY `real_rip`, `service`, `app_password` ORDER BY `datetime` DESC LIMIT :sasl_limit;'); $stmt->execute(array(':username' => $username, ':sasl_limit' => $sasl_limit)); @@ -286,11 +287,13 @@ function last_login($action, $username, $sasl_limit = 10) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL,"https://dfdata.bella.network/lookup/" . $sasl[$k]['real_rip']); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_USERAGENT, 'Moocow'); + curl_setopt($curl, CURLOPT_TIMEOUT, 5); $ip_data = curl_exec($curl); if (!curl_errno($curl)) { $ip_data_array = json_decode($ip_data, true); - if ($ip_data_array !== false and !empty($ip_data_array['location']['shortcountry'])) { - $sasl[$k]['location'] = $ip_data_array['location']['shortcountry']; + if ($ip_data_array !== false and !empty($ip_data_array['location']['country'])) { + $sasl[$k]['location'] = implode(', ', array_filter(array($ip_data_array['location']['country'], $ip_data_array['location']['city']))); try { $redis->hSet('IP_LOCATIONS', $sasl[$k]['real_rip'], $sasl[$k]['location']); } diff --git a/data/web/js/site/user.js b/data/web/js/site/user.js index 1a28c468..65728c85 100644 --- a/data/web/js/site/user.js +++ b/data/web/js/site/user.js @@ -78,7 +78,7 @@ jQuery(function($){ } }) - function last_logins(action, lines = 5) { + function last_logins(action, lines = 10) { if (action == 'get') { $.ajax({ dataType: 'json', @@ -99,28 +99,11 @@ jQuery(function($){ $.each(data.sasl, function (i, item) { var datetime = new Date(item.datetime.replace(/-/g, "/")); var local_datetime = datetime.toLocaleDateString(undefined, {year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit"}); - if (item.location) { - ip_location = ''; - } else { - ip_location = ''; - } - if (item.service == "smtp") { - service = '
' + item.service.toUpperCase() + '
'; - } else if (item.service == "imap") { - service = '
' + item.service.toUpperCase() + '
'; - } else { - service = '
' + item.service.toUpperCase() + '
'; - } - if (item.real_rip.startsWith("Web")) { - real_rip = item.real_rip; - } else { - real_rip = '' + item.real_rip + ' '; - } - real_rip = real_rip + ' ' + ip_location; - $('.last-login').append('
  • ' + - local_datetime + ' ' + service + ' ' + lang.from + ' ' + - real_rip + - '
  • '); + item.app_password?app_password=', via App':app_password="",item.location?ip_location=", "+item.location:ip_location=""; + "smtp"==item.service?service='
    '+item.service.toUpperCase()+'
    ':"imap"==item.service?service='
    '+item.service.toUpperCase()+"
    ":service='
    '+item.service.toUpperCase()+"
    "; + item.real_rip.startsWith("Web")?real_rip=item.real_rip:real_rip=''+item.real_rip+""; + ip_data = real_rip + ip_location + app_password; + $(".last-login").append('
  • '+local_datetime+" "+service+" "+lang.from+" "+ip_data+"
  • "); }) $('.last-login').append(''); } diff --git a/data/web/user.php b/data/web/user.php index 3f829e33..fa11d718 100644 --- a/data/web/user.php +++ b/data/web/user.php @@ -176,7 +176,7 @@ elseif (isset($_SESSION['mailcow_cc_role']) && $_SESSION['mailcow_cc_role'] == '

    []


    -
    +