Procházet zdrojové kódy

added support empty date to frontend

root před 3 měsíci
rodič
revize
9de87f420a

+ 1 - 1
html/admin/devices/index-passive.php

@@ -155,7 +155,7 @@ $params[]=$displayed;
 $params[]=$start;
 $users = get_records_sql($db_link,$sSQL, $params);
 foreach ($users as $user) {
-    if ($user['last_found'] == '0000-00-00 00:00:00') { $user['last_found'] = ''; }
+    if (is_empty_datetime($user['last_found'])) { $user['last_found'] = ''; }
     print "<tr align=center>\n";
     $cl = "data";
     print "<td class='".$cl."' style='padding:0'><input type=checkbox name=fid[] value=".$user['dev_id']."></td>\n";

+ 3 - 3
html/admin/iplist/deleted.php

@@ -94,9 +94,9 @@ $params[]=$start;
 
 $users = get_records_sql($db_link,$sSQL, $params);
 foreach ($users as $user) {
-    if (empty($user['last_found']) or $user['last_found'] === '0000-00-00 00:00:00') { $user['last_found'] = ''; }
-    if (empty($user['ts']) or $user['ts'] === '0000-00-00 00:00:00') { $user['ts'] = ''; }
-    if (empty($user['changed_time']) or $user['changed_time'] === '0000-00-00 00:00:00') { $user['changed_time'] = ''; }
+    if (empty($user['last_found']) || is_empty_datetime($user['last_found'])) { $user['last_found'] = ''; }
+    if (empty($user['ts']) || is_empty_datetime($user['ts'])) { $user['ts'] = ''; }
+    if (empty($user['changed_time']) || is_empty_datetime($user['changed_time'])) { $user['changed_time'] = ''; }
     print "<tr align=center>\n";
     $cl = "data";
     print "<td class=\"$cl\" ><a href=/admin/users/editauth.php?id=".$user['id'].">" . $user['ip'] . "</a></td>\n";

+ 6 - 6
html/admin/iplist/doubles.php

@@ -88,9 +88,9 @@ foreach ($users as $row) {
 
             $f_index++;
             $user = get_record_sql($db_link,"SELECT * FROM user_auth WHERE id=?", [$f_id]);
-            if (empty($user['arp_found']) or $user['arp_found'] === '0000-00-00 00:00:00') { $user['arp_found'] = ''; }
-            if (empty($user['ts']) or $user['ts'] === '0000-00-00 00:00:00') { $user['ts'] = ''; }
-            if (empty($user['changed_time']) or $user['changed_time'] === '0000-00-00 00:00:00') { $user['changed_time'] = ''; }
+            if (empty($user['arp_found']) || is_empty_datetime($user['arp_found'])) { $user['arp_found'] = ''; }
+            if (empty($user['ts']) || is_empty_datetime($user['ts'])) { $user['ts'] = ''; }
+            if (empty($user['changed_time']) || is_empty_datetime($user['changed_time'])) { $user['changed_time'] = ''; }
             print "<tr align=center>\n";
             $cl = "data";
             print "<td class=\"data\" style='padding:0'><input type=checkbox name=f_auth_id[] value=".$user["id"];
@@ -114,9 +114,9 @@ foreach ($users as $row) {
         if (!isset($printed[$row['id']])) {
             $f_index++;
             $user = get_record_sql($db_link,"SELECT * FROM user_auth WHERE id=?", [$row['id']]);
-            if (empty($user['arp_found']) or $user['arp_found'] === '0000-00-00 00:00:00') { $user['arp_found'] = ''; }
-            if (empty($user['ts']) or $user['ts'] === '0000-00-00 00:00:00') { $user['ts'] = ''; }
-            if (empty($user['changed_time']) or $user['changed_time'] === '0000-00-00 00:00:00') { $user['changed_time'] = ''; }
+            if (empty($user['arp_found']) || is_empty_datetime($user['arp_found'])) { $user['arp_found'] = ''; }
+            if (empty($user['ts']) || is_empty_datetime($user['ts'])) { $user['ts'] = ''; }
+            if (empty($user['changed_time']) || is_empty_datetime($user['changed_time'])) { $user['changed_time'] = ''; }
             print "<tr align=center>\n";
             $cl = "data";
             print "<td class=\"data\" style='padding:0'><input type=checkbox name=f_auth_id[] value=".$user["id"];

+ 4 - 4
html/admin/iplist/index.php

@@ -244,14 +244,14 @@ $params[]=$start;
 
 $users = get_records_sql($db_link,$sSQL, $params);
 foreach ($users as $user) {
-    if ($user['dhcp_time'] == '0000-00-00 00:00:00') {
+    if (is_empty_datetime($user['dhcp_time'])) {
         $dhcp_str = '';
     } else {
         $dhcp_str = $user['dhcp_time'] . " (" . $user['dhcp_action'] . ")";
     }
-    if ($user['last_found'] == '0000-00-00 00:00:00') { $user['last_found'] = ''; }
-    if ($user['arp_found'] == '0000-00-00 00:00:00') { $user['arp_found'] = ''; }
-    if ($user['mac_found'] == '0000-00-00 00:00:00') { $user['mac_found'] = ''; }
+    if (is_empty_datetime($user['last_found'])) { $user['last_found'] = ''; }
+    if (is_empty_datetime($user['arp_found'])) { $user['arp_found'] = ''; }
+    if (is_empty_datetime($user['mac_found'])) { $user['mac_found'] = ''; }
     print "<tr align=center>\n";
     $cl = "data";
     if (!$user['enabled']) { $cl = "warn"; }

+ 2 - 2
html/admin/iplist/nagios.php

@@ -106,12 +106,12 @@ $params[]=$start;
 
 $users = get_records_sql($db_link,$sSQL, $params);
 foreach ($users as $user) {
-    if ($user['dhcp_time'] == '0000-00-00 00:00:00') {
+    if (is_empty_datetime($user['dhcp_time'])) {
         $dhcp_str = '';
     } else {
         $dhcp_str = $user['dhcp_time'] . " (" . $user['dhcp_action'] . ")";
     }
-    if ($user['last_found'] == '0000-00-00 00:00:00') { $user['last_found'] = ''; }
+    if (is_empty_datetime($user['last_found'])) { $user['last_found'] = ''; }
     print "<tr align=center>\n";
     $cl = "data";
     if ($user['nagios_status'] == "UP") { $cl = "up"; }

+ 5 - 5
html/admin/users/editauth.php

@@ -322,19 +322,19 @@ $device = get_record_sql($db_link, "SELECT * FROM devices WHERE user_id=?", [ $a
 $parent_name = get_login($db_link, $auth_info['user_id']);
 if (empty($parent_name)) { $parent_name=$auth_info['user_id']; }
 
-if ($auth_info['dhcp_time'] == '0000-00-00 00:00:00') {
+if (is_empty_datetime($auth_info['dhcp_time'])) {
     $dhcp_str = '';
 } else {
     $dhcp_str = $auth_info['dhcp_time'] . " (" . $auth_info['dhcp_action'] . ")";
 }
-if ($auth_info['last_found'] == '0000-00-00 00:00:00') { $auth_info['last_found'] = ''; }
-if ($auth_info['mac_found'] == '0000-00-00 00:00:00') { $auth_info['mac_found'] = ''; }
-if ($auth_info['arp_found'] == '0000-00-00 00:00:00') { $auth_info['arp_found'] = ''; }
+if (is_empty_datetime($auth_info['last_found'])) { $auth_info['last_found'] = ''; }
+if (is_empty_datetime($auth_info['mac_found'])) { $auth_info['mac_found'] = ''; }
+if (is_empty_datetime($auth_info['arp_found'])) { $auth_info['arp_found'] = ''; }
 
 $now = DateTime::createFromFormat("Y-m-d H:i:s",date('Y-m-d H:i:s'));
 $created = new DateTime($auth_info['ts']);
 
-if (empty($auth_info['end_life']) or $auth_info['end_life'] == '0000-00-00 00:00:00') { 
+if (empty($auth_info['end_life']) || is_empty_datetime($auth_info['end_life'])) { 
     $now->modify('+1 day');
     $auth_info['end_life'] = $now->format('Y-m-d H:i:s');
     }

+ 4 - 4
html/admin/users/edituser.php

@@ -445,16 +445,16 @@ require_once($_SERVER["DOCUMENT_ROOT"] . "/inc/header.php");
             if (!empty($flist)) {
                 foreach ($flist as $row) {
                     $dhcp_str = '';
-                    if ($row["dhcp_time"] !== '0000-00-00 00:00:00') {
+                    if (!is_empty_datetime($row["dhcp_time"])) {
                         if (!empty($row["dhcp_action"])) { $dhcp_str = FormatDateStr('Y.m.d H:m', $row["dhcp_time"]) . " (" . $row["dhcp_action"] . ")"; }
                     }
-                    if ($row["last_found"] == '0000-00-00 00:00:00') {
+                    if (is_empty_datetime($row["last_found"])) {
                         $row["last_found"] = '';
                     }
-                    if ($row["mac_found"] == '0000-00-00 00:00:00') {
+                    if (is_empty_datetime($row["mac_found"])) {
                         $row["mac_found"] = '';
                     }
-                    if ($row["arp_found"] == '0000-00-00 00:00:00') {
+                    if (is_empty_datetime($row["arp_found"])) {
                         $row["arp_found"] = '';
                     }
                     print "<tr align=center>";

+ 41 - 1
html/inc/common.php

@@ -251,6 +251,46 @@ if (!function_exists('mb_ucfirst')) {
     }
 }
 
+function is_empty_datetime($datetime) {
+    if (empty($datetime)) {
+        return true;
+    }
+    
+    // Проверяем формат даты и "нулевые" значения
+    if ($datetime === '0000-00-00 00:00:00') {
+        return true;
+    }
+    
+    // Регулярное выражение для даты в формате YYYY-MM-DD HH:MM:SS
+    if (!preg_match('/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/', $datetime, $matches)) {
+        return false; // Неверный формат — считаем валидным
+    }
+    
+    $year = (int)$matches[1];
+    $month = (int)$matches[2];
+    $day = (int)$matches[3];
+    $hour = (int)$matches[4];
+    $minute = (int)$matches[5];
+    $second = (int)$matches[6];
+    
+    // Проверяем, что дата в пределах "нулевого" диапазона
+    // Unix epoch начинается с 1970-01-01 00:00:00 UTC
+    // Но из-за часовых поясов могут быть варианты вроде 1970-01-01 03:00:00
+    if ($year == 1970 && $month == 1 && $day == 1) {
+        // Допускаем небольшой диапазон часов (0-12) как "нулевую" дату
+        if ($hour >= 0 && $hour <= 12 && $minute == 0 && $second == 0) {
+            return true;
+        }
+    }
+    
+    // Также проверяем даты до 1970 года (иногда бывают)
+    if ($year < 1970) {
+        return true;
+    }
+    
+    return false;
+}
+
 function get_user_ip()
 {
     $auth_ip = getenv("HTTP_CLIENT_IP");
@@ -3618,7 +3658,7 @@ function apply_device_lock($db, $device_id, $iteration = 0)
     );
     
     // Проверяем, есть ли запись и валидна ли временная метка
-    if (empty($dev) || empty($dev['locked_timestamp']) || $dev['locked_timestamp'] === '0000-00-00 00:00:00') {
+    if (empty($dev) || is_empty_datetime($dev['locked_timestamp'])) {
         LOG_DEBUG($db, "Snmp discovery lock not found. Set and discovery.");
         return set_lock_discovery($db, (int)$device_id);
     }