diff --git a/front/css/pialert.css b/front/css/pialert.css new file mode 100644 index 00000000..75e68839 --- /dev/null +++ b/front/css/pialert.css @@ -0,0 +1,373 @@ +/******************************************************************************* +* Pi.alert CSS +*******************************************************************************/ + +/* ----------------------------------------------------------------------------- + Global Variables +----------------------------------------------------------------------------- */ +:root { + --color-aqua: #00c0ef; + --color-green: #00a65a; + --color-yellow: #f39c12; + --color-red: #dd4b39; +} + + +/* ----------------------------------------------------------------------------- + Text Classes +----------------------------------------------------------------------------- */ +.text-center { + text-align: center; +} + +.text-right { + text-align: right; +} + +.text-white { + color: white; +} + +.text-gray50 { + color: #808080; +} + +.text-aqua-20 { + color: rgba(0,192,239,20%); +} + +.text-green-20 { + color: rgba(0,166,90,20%); +} + +.text-yellow-20 { + color: rgba(243,156,18,20%); +} + +.text-red-20 { + color: rgba(221,75,57,20%); +} + +.no-border { + border: none; +} + +/* ----------------------------------------------------------------------------- + Main Sections +----------------------------------------------------------------------------- */ +.content-header { + padding-top: 5px; +} + +.content-header > .breadcrumb { + background: transparent; +} + +.content { + padding-bottom: 0px; +} + +.box-body { + padding-top: 0px; + padding-bottom: 0px; +} + +.main-footer { + padding: 5px; + color: gray; +} + + +/* ----------------------------------------------------------------------------- + Customized Main Menu +----------------------------------------------------------------------------- */ +.main-header .logo { + width: 150px; +} + +.main-header > .navbar { + margin-left: 150px; +} + +.main-sidebar, .left-side { + width: 150px; +} + +.content-wrapper, .right-side, .main-footer { + margin-left: 150px; +} + +@media (max-width: 767px) { + .main-header .logo { + width: 100%; + } + + .main-header .navbar { + margin: 0; + } + + .content-wrapper, .main-footer { + margin-left: 0px; + } + +} + +.sidebar-open .content-wrapper, .sidebar-open .main-footer { + -webkit-transform: translate(150px, 0); + -ms-transform: translate(150px, 0); + -o-transform: translate(150px, 0); + transform: translate(150px, 0) +} + + +.skin-yellow-light .sidebar-menu > li > a:hover { + background: #f0f0f0; + border-left-color: rgb(243, 156, 18); +} + +.skin-yellow-light .sidebar-menu > li.active > a { + background: #e0e0e0; + border-left-color: rgb(243, 156, 18); +} + + + +/* ----------------------------------------------------------------------------- + Customized Boxes +----------------------------------------------------------------------------- */ +.box-transparent { + position: relative; + margin-bottom: 20px; + width: 100%; +} + +.pa-small-box-2 .inner h3 { + margin-left: 0em; + margin-bottom: 1.3em; +} + +.pa-small-box-aqua { + border-top: 3px solid #00c0ef; + box-shadow: 0 5px 5px rgba(0, 0, 0, 0.1); +} + +.pa-small-box-aqua .inner { + color: #00c0ef; + background-color:#FFFFFF; +} + +.pa-small-box-aqua .inner h3 { + margin-left: 0.5em; +} + +.pa-small-box-aqua .icon { + color: #00c0ef; +} + + + +/* -------------------------------------------------------------------------- */ +.pa-small-box-green { + border-top: 3px solid #00a65a; + box-shadow: 0 5px 5px rgba(0, 0, 0, 0.1); +} + +.pa-small-box-green .inner { + color: #00a65a; + background-color:#FFFFFF; +} + +.pa-small-box-green .inner h3 { + margin-left: 0.5em; +} + +.pa-small-box-green .icon { + color: #00a65a; +} + +/* -------------------------------------------------------------------------- */ +.pa-small-box-yellow { + border-top: 3px solid #f39c12; + box-shadow: 0 5px 5px rgba(0, 0, 0, 0.1); +} + +.pa-small-box-yellow .inner { + color: #f39c12; + background-color:#FFFFFF; +} + +.pa-small-box-yellow .inner h3 { + margin-left: 0.5em; +} + +.pa-small-box-yellow .icon { + color: #f39c12; +} + +/* -------------------------------------------------------------------------- */ +.pa-small-box-red { + border-top: 3px solid #dd4b39; + box-shadow: 0 5px 5px rgba(0, 0, 0, 0.1); +} + +.pa-small-box-red .inner { + color: #dd4b39; + background-color:#FFFFFF; +} + +.pa-small-box-red .inner h3 { + margin-left: 0.5em; +} + +.pa-small-box-red .icon { + color: #dd4b39; +} + + +/* ----------------------------------------------------------------------------- + Customized Box Borders +----------------------------------------------------------------------------- */ +.box.box-aqua { + border-top-color: #00c0ef; +} + +.box.box-green { + border-top-color: #00a65a; +} + +.box.box-yellow { + border-top-color: #f39c12; +} + +.box.box-red { + border-top-color: #dd4b39; +} + + +/* ----------------------------------------------------------------------------- + Custom Border +----------------------------------------------------------------------------- */ +.bottom-border-aqua { + border-bottom-color: #00c0ef; + border-bottom-style: solid; + border-bottom-width: 3px +} + +.bottom-border-primary { + border-bottom-color: #3c8dbc; + border-bottom-style: solid; + border-bottom-width: 3px +} + + +/* ----------------------------------------------------------------------------- + Customized Tabs +----------------------------------------------------------------------------- */ +.nav-tabs-custom { + background: transparent; +} + +.nav > li > a { + position: relative; + display: block; + padding: 10px 10px; +} + + +/* ----------------------------------------------------------------------------- + Customized Menu dropdown +----------------------------------------------------------------------------- */ +.dropdown-menu { + max-height: 250px; + overflow-x: hidden; + box-shadow: 0px 3px 20px rgba(0,0,0,0.2); +} + + +/* ----------------------------------------------------------------------------- + Default Table config +----------------------------------------------------------------------------- */ +.table > tbody > tr > td { + padding:4px; +} + +.table-hover tbody tr:hover td, .table-hover tbody tr:hover th { + background-color: #FFFFD0; +} + +.dataTables_info, .dataTables_paginate, .dataTables_length, .dataTables_filter { + color: #B0B0B0; +} + + + +/* ----------------------------------------------------------------------------- + Customized Full Calendar +----------------------------------------------------------------------------- */ +.fc h2 { + font-size: 20px; +} + +.fc-weekend { + background-color: #FFF0E0; +} + +.fc-normaldays { + background-color: #FF0000; +} + +.fc-sat { + background-color: #FFF0E0; +} + +.fc-sun { + background-color: #FFF0E0; +} + +.fc-resized-row { height: 26px !important; } + +.fc-transparent-border { + border-Color: transparent !important; +} + +.tooltip-inner { + background-color: #606060; + color: White; +} + +.tooltip.right .tooltip-arrow { + border-right: 5px solid #606060; +} + + + +/* ----------------------------------------------------------------------------- + Spin +----------------------------------------------------------------------------- */ +.pa_semitransparent-panel { + position: absolute; + width: 100%; //calc (100% -40px); + height: 100%; + left: 0; + top: 0; + display: block; + + opacity: 0.8; + background-color: #fff; + z-index: 99; +} + +.pa_spinner { + position: absolute; + left: 0; + right: 0; + top: 20px; + margin-left: auto; + margin-right: auto; + padding: 15px; + width: 200px; + + background-color: #fff; + z-index: 100; +} + diff --git a/front/deviceDetails.php b/front/deviceDetails.php new file mode 100644 index 00000000..f15bf508 --- /dev/null +++ b/front/deviceDetails.php @@ -0,0 +1,957 @@ + + + + +
+ + +
+

+  Quering device info... +

+ + + + Sessions, Presence & Alerts period: + + + + +
+ + + +
+ + + + + +
+
+ + + + + + +
+ + +
+ + + +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/front/devices.php b/front/devices.php new file mode 100644 index 00000000..58937cdc --- /dev/null +++ b/front/devices.php @@ -0,0 +1,339 @@ + + + + +
+ + +
+

+ Devices +

+ + + + New Devices period: + + + +
+ + +
+ + + + +
+
+
+
+

Devices

+
+ +
+ + + + + + + + + + + + + + + + +
NameOwnerDevice typeFavoriteGroupFirst SessionLast SessionLast IPStatusMACLast IP Order
+
+ +
+ +
+ +
+ + + +
+ +
+ + + + + + + + + + + + + + + + diff --git a/front/events.php b/front/events.php new file mode 100644 index 00000000..55f6fd75 --- /dev/null +++ b/front/events.php @@ -0,0 +1,366 @@ + + + + +
+ + +
+

+ Events +

+ + + + Events period: + + + +
+ + +
+ + + + +
+
+
+
+

Events

+
+ +
+ + + + + + + + + + + + + + + + + +
OrderDeviceOwnerDateEvent TypeConnectionDisconnectionDurationDuration OrderIPIP OrderAdditional Info
+
+ +
+ +
+ +
+ + + +
+ +
+ + + + + + + + + + + + + + + + diff --git a/front/img/backgroud.png b/front/img/backgroud.png new file mode 100644 index 00000000..03bd6a7d Binary files /dev/null and b/front/img/backgroud.png differ diff --git a/front/img/pialertLogoBlack.png b/front/img/pialertLogoBlack.png new file mode 100644 index 00000000..fe20f244 Binary files /dev/null and b/front/img/pialertLogoBlack.png differ diff --git a/front/img/pialertLogoGray50.png b/front/img/pialertLogoGray50.png new file mode 100644 index 00000000..ab4563e0 Binary files /dev/null and b/front/img/pialertLogoGray50.png differ diff --git a/front/img/pialertLogoGray80.png b/front/img/pialertLogoGray80.png new file mode 100644 index 00000000..3c347c27 Binary files /dev/null and b/front/img/pialertLogoGray80.png differ diff --git a/front/img/pialertLogoOrange.png b/front/img/pialertLogoOrange.png new file mode 100644 index 00000000..00bf5dad Binary files /dev/null and b/front/img/pialertLogoOrange.png differ diff --git a/front/img/pialertLogoWhite.png b/front/img/pialertLogoWhite.png new file mode 100644 index 00000000..ae372da9 Binary files /dev/null and b/front/img/pialertLogoWhite.png differ diff --git a/front/index.php b/front/index.php new file mode 100644 index 00000000..58937cdc --- /dev/null +++ b/front/index.php @@ -0,0 +1,339 @@ + + + + +
+ + +
+

+ Devices +

+ + + + New Devices period: + + + +
+ + +
+ + + + +
+
+
+
+

Devices

+
+ +
+ + + + + + + + + + + + + + + + +
NameOwnerDevice typeFavoriteGroupFirst SessionLast SessionLast IPStatusMACLast IP Order
+
+ +
+ +
+ +
+ + + +
+ +
+ + + + + + + + + + + + + + + + diff --git a/front/js/pialert_common.js b/front/js/pialert_common.js new file mode 100644 index 00000000..5778f71f --- /dev/null +++ b/front/js/pialert_common.js @@ -0,0 +1,39 @@ +/* ----------------------------------------------------------------------------- + Pi.Alert Common Javascript functions +----------------------------------------------------------------------------- */ + +// ----------------------------------------------------------------------------- +var timerRefreshData = '' + + +// ----------------------------------------------------------------------------- +function translateHTMLcodes (text) { + if (text == null) { + return null; + } + var text2 = text.replace(new RegExp(' ', 'g'), " "); + text2 = text2.replace(new RegExp('<', 'g'), "<"); + return text2; +} + + +// ----------------------------------------------------------------------------- +function stopTimerRefreshData () { + try { + clearTimeout (timerRefreshData); + } catch (e) {} +} + + +// ----------------------------------------------------------------------------- +function newTimerRefreshData (refeshFunction) { + timerRefreshData = setTimeout (function() { + refeshFunction(); + }, 5000); +} + + +// ----------------------------------------------------------------------------- +function debugTimer () { + document.getElementById ('pageTitle').innerHTML = (new Date().getSeconds()); +} diff --git a/front/php/server/db.php b/front/php/server/db.php new file mode 100644 index 00000000..102dc884 --- /dev/null +++ b/front/php/server/db.php @@ -0,0 +1,52 @@ + diff --git a/front/php/server/devices.php b/front/php/server/devices.php new file mode 100644 index 00000000..aa76c287 --- /dev/null +++ b/front/php/server/devices.php @@ -0,0 +1,377 @@ +query('SELECT COUNT(*) FROM Devices '); + $row = $result -> fetchArray (SQLITE3_NUM); + $devices = $row[0]; + + // Connected + $result = $db->query('SELECT COUNT(*) FROM Devices ' . getDeviceCondition ('connected') ); + $row = $result -> fetchArray (SQLITE3_NUM); + $connected = $row[0]; + + // New + $result = $db->query('SELECT COUNT(*) FROM Devices ' . getDeviceCondition ('new') ); + $row = $result -> fetchArray (SQLITE3_NUM); + $newDevices = $row[0]; + + // Down Alerts + $result = $db->query('SELECT COUNT(*) FROM Devices ' . getDeviceCondition ('down')); + $row = $result -> fetchArray (SQLITE3_NUM); + $devicesDownAlert = $row[0]; + + echo (json_encode (array ($devices, $connected, $newDevices, $devicesDownAlert))); +} + + +//------------------------------------------------------------------------------ +// Query the List of devices in a determined Status +//------------------------------------------------------------------------------ +function queryList() { + global $db; + + // Request Parameters + $periodDate = getDateFromPeriod(); + + // SQL + $condition = getDeviceCondition ($_REQUEST['status']); + + $result = $db->query('SELECT *, + CASE WHEN dev_AlertDeviceDown=1 AND dev_PresentLastScan=0 THEN "Down" + WHEN dev_FirstConnection >= ' . $periodDate . ' THEN "New" + WHEN dev_PresentLastScan=1 THEN "On-line" + ELSE "Off-line" + END AS dev_Status + FROM Devices ' . $condition); + + // arrays of rows + $tableData = array(); + while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { + $tableData['data'][] = array ($row['dev_Name'], + $row['dev_Owner'], + $row['dev_DeviceType'], + $row['dev_Favorite'], + $row['dev_Group'], + formatDate ($row['dev_FirstConnection']), + formatDate ($row['dev_LastConnection']), + $row['dev_LastIP'], + $row['dev_Status'], + $row['dev_MAC'], // MAC (hidden) + formatIPlong ($row['dev_LastIP']) // IP orderable + ); + } + + // Control no rows + if (empty($tableData['data'])) { + $tableData['data'] = ''; + } + + // Return json + echo (json_encode ($tableData)); +} + +//------------------------------------------------------------------------------ +// Query the List of Owners +//------------------------------------------------------------------------------ +function queryOwners() { + global $db; + + // SQL + $result = $db->query('SELECT DISTINCT 1 as dev_Order, dev_Owner + FROM Devices + WHERE dev_Owner <> "(unknown)" AND dev_Owner <> "" + AND dev_Favorite = 1 + UNION + SELECT DISTINCT 2 as dev_Order, dev_Owner + FROM Devices + WHERE dev_Owner <> "(unknown)" AND dev_Owner <> "" + AND dev_Favorite = 0 + AND dev_Owner NOT IN (SELECT dev_Owner FROM Devices WHERE dev_Favorite = 1) + ORDER BY 1,2 '); + + // arrays of rows + $tableData = array(); + while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { + $tableData[] = array ('order' => $row['dev_Order'], + 'name' => $row['dev_Owner']); + } + + // Return json + echo (json_encode ($tableData)); +} + + +//------------------------------------------------------------------------------ +// Query the List of types +//------------------------------------------------------------------------------ +function queryDeviceTypes() { + global $db; + + // SQL + $result = $db->query('SELECT DISTINCT 9 as dev_Order, dev_DeviceType + FROM Devices + WHERE dev_DeviceType NOT IN ("", + "Smartphone", "Tablet", + "Laptop", "Mini PC", "PC", "Printer", "Server", + "Game Console", "SmartTV", "TV Decoder", "Virtual Assistance", + "Clock", "House Appliance", "Phone", "Radio", + "AP", "NAS", "PLC", "Router") + + UNION SELECT 1 as dev_Order, "Smartphone" + UNION SELECT 1 as dev_Order, "Tablet" + + UNION SELECT 2 as dev_Order, "Laptop" + UNION SELECT 2 as dev_Order, "Mini PC" + UNION SELECT 2 as dev_Order, "PC" + UNION SELECT 2 as dev_Order, "Printer" + UNION SELECT 2 as dev_Order, "Server" + + UNION SELECT 3 as dev_Order, "Game Console" + UNION SELECT 3 as dev_Order, "SmartTV" + UNION SELECT 3 as dev_Order, "TV Decoder" + UNION SELECT 3 as dev_Order, "Virtual Assistance" + + UNION SELECT 4 as dev_Order, "Clock" + UNION SELECT 4 as dev_Order, "House Appliance" + UNION SELECT 4 as dev_Order, "Phone" + UNION SELECT 4 as dev_Order, "Radio" + + UNION SELECT 5 as dev_Order, "AP" + UNION SELECT 5 as dev_Order, "NAS" + UNION SELECT 5 as dev_Order, "PLC" + UNION SELECT 5 as dev_Order, "Router" + + UNION SELECT 10 as dev_Order, "Other" + + ORDER BY 1,2 '); + + // arrays of rows + $tableData = array(); + while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { + $tableData[] = array ('order' => $row['dev_Order'], + 'name' => $row['dev_DeviceType']); + } + + // Return json + echo (json_encode ($tableData)); +} + + +//------------------------------------------------------------------------------ +// Query the List of groups +//------------------------------------------------------------------------------ +function queryGroups() { + global $db; + + // SQL + $result = $db->query('SELECT DISTINCT 1 as dev_Order, dev_Group + FROM Devices + WHERE dev_Group <> "(unknown)" AND dev_Group <> "Others" AND dev_Group <> "" + UNION SELECT 1 as dev_Order, "Always on" + UNION SELECT 1 as dev_Order, "Friends" + UNION SELECT 1 as dev_Order, "Personal" + UNION SELECT 2 as dev_Order, "Others" + ORDER BY 1,2 '); + + // arrays of rows + $tableData = array(); + while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { + $tableData[] = array ('order' => $row['dev_Order'], + 'name' => $row['dev_Group']); + } + + // Return json + echo (json_encode ($tableData)); +} + + +//------------------------------------------------------------------------------ +// Query the List of devices for calendar +//------------------------------------------------------------------------------ +function queryCalendarList() { + global $db; + + // Request Parameters + $periodDate = getDateFromPeriod(); + + // SQL + $condition = getDeviceCondition ($_REQUEST['status']); + $result = $db->query('SELECT * FROM Devices ' . $condition); + + // arrays of rows + $tableData = array(); + while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { + if ($row['dev_Favorite'] == 1) { + $row['dev_Name'] = ' '. $row['dev_Name']; + } + + $tableData[] = array ('id' => $row['dev_MAC'], + 'title' => $row['dev_Name'], + 'favorite' => $row['dev_Favorite']); + } + + // Return json + echo (json_encode ($tableData)); +} + + +//------------------------------------------------------------------------------ +// Query Device Data +//------------------------------------------------------------------------------ +function queryDeviceData() { + global $db; + + // Request Parameters + $periodDate = getDateFromPeriod(); + $mac = $_REQUEST['mac']; + + // Device Data + $result = $db->query('SELECT *, + CASE WHEN dev_AlertDeviceDown=1 AND dev_PresentLastScan=0 THEN "Down" + WHEN dev_PresentLastScan=1 THEN "On-line" + ELSE "Off-line" END as dev_Status + FROM Devices + WHERE dev_MAC="' . $mac .'"'); + + $row = $result -> fetchArray (SQLITE3_ASSOC); + $deviceData = $row; + $deviceData['dev_FirstConnection'] = formatDate ($row['dev_FirstConnection']); // Date formated + $deviceData['dev_LastConnection'] = formatDate ($row['dev_LastConnection']); // Date formated + + // Count Totals + $condicion = ' WHERE eve_MAC="' . $mac .'" AND eve_DateTime >= ' . $periodDate; + + // Connections + $result = $db->query('SELECT COUNT(*) FROM Sessions + WHERE ses_MAC="' . $mac .'" + AND ( ses_DateTimeConnection >= ' . $periodDate . ' + OR ses_DateTimeDisconnection >= ' . $periodDate . ' + OR ses_StillConnected = 1 ) '); + $row = $result -> fetchArray (SQLITE3_NUM); + $deviceData['dev_Sessions'] = $row[0]; + + // Events + $result = $db->query('SELECT COUNT(*) FROM Events ' . $condicion . ' AND eve_EventType <> "Connected" AND eve_EventType <> "Disconnected" '); + $row = $result -> fetchArray (SQLITE3_NUM); + $deviceData['dev_Events'] = $row[0]; + + // Donw Alerts + $result = $db->query('SELECT COUNT(*) FROM Events ' . $condicion . ' AND eve_EventType = "Device Down"'); + $row = $result -> fetchArray (SQLITE3_NUM); + $deviceData['dev_DownAlerts'] = $row[0]; + + // Presence hours + $result = $db->query('SELECT SUM (julianday (IFNULL (ses_DateTimeDisconnection, DATETIME("now"))) + - julianday (CASE WHEN ses_DateTimeConnection < ' . $periodDate . ' THEN ' . $periodDate . ' + ELSE ses_DateTimeConnection END)) *24 + FROM Sessions + WHERE ses_MAC="' . $mac .'" + AND ses_DateTimeConnection IS NOT NULL + AND (ses_DateTimeDisconnection IS NOT NULL OR ses_StillConnected = 1 ) + AND ( ses_DateTimeConnection >= ' . $periodDate . ' + OR ses_DateTimeDisconnection >= ' . $periodDate . ' + OR ses_StillConnected = 1 ) '); + $row = $result -> fetchArray (SQLITE3_NUM); + $deviceData['dev_PresenceHours'] = round ($row[0]); + + + // Return json + echo (json_encode ($deviceData)); +} + + +//------------------------------------------------------------------------------ +// Status Where conditions +//------------------------------------------------------------------------------ +function getDeviceCondition ($deviceStatus) { + // Request Parameters + $periodDate = getDateFromPeriod(); + + switch ($deviceStatus) { + case 'all': + return ''; + case 'connected': + return 'WHERE dev_PresentLastScan=1'; + case 'new': + return 'WHERE dev_FirstConnection >= ' . $periodDate; + case 'down': + return 'WHERE dev_AlertDeviceDown=1 AND dev_PresentLastScan=0'; + case 'favorites': + return 'WHERE dev_Favorite=1'; + default: + return 'WHERE 1=0'; + } +} + + +//------------------------------------------------------------------------------ +// Update Device Data +//------------------------------------------------------------------------------ +function updateDeviceData() { + global $db; + + // sql + $sql = 'UPDATE Devices SET + dev_Name = "'. $_REQUEST['name'] .'", + dev_Owner = "'. $_REQUEST['owner'] .'", + dev_DeviceType = "'. $_REQUEST['type'] .'", + dev_Vendor = "'. $_REQUEST['vendor'] .'", + dev_Favorite = "'. $_REQUEST['favorite'] .'", + dev_Group = "'. $_REQUEST['group'] .'", + dev_Comments = "'. $_REQUEST['comments'] .'", + dev_StaticIP = "'. $_REQUEST['staticIP'] .'", + dev_ScanCycle = "'. $_REQUEST['scancycle'] .'", + dev_AlertEvents = "'. $_REQUEST['alertevents'] .'", + dev_AlertDeviceDown = "'. $_REQUEST['alertdown'] .'", + dev_SkipRepeated = "'. $_REQUEST['skiprepeated'] .'" + WHERE dev_MAC="' . $_REQUEST['mac'] .'"'; + // update Data + $result = $db->query($sql); + + // check result + if ($result == TRUE) { + echo "Device updated successfully"; + } else { + echo "Error updating device\n\n". $sql .'\n\n' . $db->lastErrorMsg(); + } + +} + + +?> diff --git a/front/php/server/events.php b/front/php/server/events.php new file mode 100644 index 00000000..8e073973 --- /dev/null +++ b/front/php/server/events.php @@ -0,0 +1,429 @@ += '. $periodDate; + + $SQL2 = 'SELECT Count(*) + FROM Sessions '; + + // All + $result = $db->query($SQL1); + $row = $result -> fetchArray (SQLITE3_NUM); + $eventsAll = $row[0]; + + // Sessions + $result = $db->query($SQL2. ' WHERE ( ses_DateTimeConnection >= '. $periodDate .' + OR ses_DateTimeDisconnection >= '. $periodDate .' + OR ses_StillConnected = 1 ) '); + $row = $result -> fetchArray (SQLITE3_NUM); + $eventsSessions = $row[0]; + + // Missing + $result = $db->query($SQL2. ' WHERE (ses_DateTimeConnection IS NULL AND ses_DateTimeDisconnection >= '. $periodDate .' ) + OR (ses_DateTimeDisconnection IS NULL AND ses_StillConnected = 0 AND ses_DateTimeConnection >= '. $periodDate .' )' ); + $row = $result -> fetchArray (SQLITE3_NUM); + $eventsMissing = $row[0]; + + // Voided + $result = $db->query($SQL1. ' AND eve_EventType LIKE "VOIDED%" '); + $row = $result -> fetchArray (SQLITE3_NUM); + $eventsVoided = $row[0]; + + // New + $result = $db->query($SQL1. ' AND eve_EventType LIKE "New Device" '); + $row = $result -> fetchArray (SQLITE3_NUM); + $eventsNew = $row[0]; + + // Down + $result = $db->query($SQL1. ' AND eve_EventType LIKE "Device Down" '); + $row = $result -> fetchArray (SQLITE3_NUM); + $eventsDown = $row[0]; + + // Return json + echo (json_encode (array ($eventsAll, $eventsSessions, $eventsMissing, $eventsVoided, $eventsNew, $eventsDown))); +} + + +//------------------------------------------------------------------------------ +// Query the List of events +//------------------------------------------------------------------------------ +function queryList() { + global $db; + + // Request Parameters + $type = $_REQUEST ['type']; + $periodDate = getDateFromPeriod(); + + // SQL + $SQL1 = 'SELECT eve_DateTime AS eve_DateTimeOrder, dev_name, dev_owner, eve_DateTime, eve_EventType, NULL, NULL, NULL, NULL, eve_IP, NULL, eve_AdditionalInfo, NULL, Dev_MAC + FROM Events_Devices + WHERE eve_DateTime >= '. $periodDate; + + $SQL2 = 'SELECT IFNULL (ses_DateTimeConnection, ses_DateTimeDisconnection) ses_DateTimeOrder, + dev_name, dev_owner, Null, Null, ses_DateTimeConnection, ses_DateTimeDisconnection, NULL, NULL, ses_IP, NULL, ses_AdditionalInfo, ses_StillConnected, Dev_MAC + FROM Sessions_Devices '; + + // SQL Variations for status + switch ($type) { + case 'all': + $SQL = $SQL1; + break; + case 'sessions': + $SQL = $SQL2 . ' WHERE ( ses_DateTimeConnection >= '. $periodDate .' + OR ses_DateTimeDisconnection >= '. $periodDate .' + OR ses_StillConnected = 1 ) '; + break; + case 'missing': + $SQL = $SQL2 . ' WHERE (ses_DateTimeConnection IS NULL AND ses_DateTimeDisconnection >= '. $periodDate .' ) + OR (ses_DateTimeDisconnection IS NULL AND ses_StillConnected = 0 AND ses_DateTimeConnection >= '. $periodDate .' )'; + break; + case 'voided': + $SQL = $SQL1 .' AND eve_EventType LIKE "VOIDED%" '; + break; + case 'new': + $SQL = $SQL1 .' AND eve_EventType = "New Device" '; + break; + case 'down': + $SQL = $SQL1 .' AND eve_EventType = "Device Down" '; + break; + default: + $SQL = $SQL1 .' AND 1==0 '; + break; + } + + // Query + $result = $db->query($SQL); + + $tableData = array(); + while ($row = $result -> fetchArray (SQLITE3_NUM)) { + if ($type == 'sessions' || $type == 'missing' ) { + // Duration + if (!empty ($row[5]) && !empty($row[6]) ) { + $row[7] = formatDateDiff ($row[5], $row[6]); + $row[8] = abs(strtotime($row[6]) - strtotime($row[5])); + } elseif ($row[12] == 1) { + $row[7] = formatDateDiff ($row[5], ''); + $row[8] = abs(strtotime("now") - strtotime($row[5])); + } else { + $row[7] = '...'; + $row[8] = 0; + } + + // Connection + if (!empty ($row[5]) ) { + $row[5] = formatDate ($row[5]); + } else { + $row[5] = ''; + } + + // Disconnection + if (!empty ($row[6]) ) { + $row[6] = formatDate ($row[6]); + } elseif ($row[12] == 0) { + $row[6] = ''; + } else { + $row[6] = '...'; + } + + } else { + // Event Date + $row[3] = formatDate ($row[3]); + } + + // IP Order + $row[10] = formatIPlong ($row[9]); + $tableData['data'][] = $row; + } + + // Control no rows + if (empty($tableData['data'])) { + $tableData['data'] = ''; + } + + // Return json + echo (json_encode ($tableData)); +} + + +//------------------------------------------------------------------------------ +// Query Device Sessions +//------------------------------------------------------------------------------ +function queryDeviceSessions() { + global $db; + + // Request Parameters + $mac = $_REQUEST['mac']; + $periodDate = getDateFromPeriod(); + + // SQL + $result = $db->query('SELECT IFNULL (ses_DateTimeConnection, ses_DateTimeDisconnection) ses_DateTimeOrder, + ses_EventTypeConnection, ses_DateTimeConnection, + ses_EventTypeDisconnection, ses_DateTimeDisconnection, ses_StillConnected, + ses_IP, ses_AdditionalInfo + FROM Sessions + WHERE ses_MAC="' . $mac .'" + AND ( ses_DateTimeConnection >= '. $periodDate .' + OR ses_DateTimeDisconnection >= '. $periodDate .' + OR ses_StillConnected = 1 ) '); + + // arrays of rows + $tableData = array(); + while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { + // Connection DateTime + if ($row['ses_EventTypeConnection'] == '') { + $ini = $row['ses_EventTypeConnection']; + } else { + $ini = formatDate ($row['ses_DateTimeConnection']); + } + + // Disconnection DateTime + if ($row['ses_StillConnected'] == true) { + $end = '...'; + } elseif ($row['ses_EventTypeDisconnection'] == '') { + $end = $row['ses_EventTypeDisconnection']; + } else { + $end = formatDate ($row['ses_DateTimeDisconnection']); + } + + // Duration + if ($row['ses_EventTypeConnection'] == '' || $row['ses_EventTypeDisconnection'] == '') { + $dur = '...'; + } elseif ($row['ses_StillConnected'] == true) { + $dur = formatDateDiff ($row['ses_DateTimeConnection'], ''); //******************************************************************************************* + } else { + $dur = formatDateDiff ($row['ses_DateTimeConnection'], $row['ses_DateTimeDisconnection']); + } + + // Additional Info + $info = $row['ses_AdditionalInfo']; + if ($row['ses_EventTypeConnection'] == 'New Device' ) { + $info = $row['ses_EventTypeConnection'] .': '. $info; + } + + // Push row data + $tableData['data'][] = array($row['ses_DateTimeOrder'], $ini, $end, $dur, $row['ses_IP'], $info); + } + + // Control no rows + if (empty($tableData['data'])) { + $tableData['data'] = ''; + } + + // Return json + echo (json_encode ($tableData)); +} + + +//------------------------------------------------------------------------------ +// Query Device Presence Calendar +//------------------------------------------------------------------------------ +function queryDevicePresence() { + global $db; + + // Request Parameters + $mac = $_REQUEST['mac']; + $periodDate = getDateFromPeriod(); + $startDate = '"'. formatDateISO ($_REQUEST ['start']) .'"'; + $endDate = '"'. formatDateISO ($_REQUEST ['end']) .'"'; + + // SQL + $result = $db->query('SELECT ses_EventTypeConnection, ses_DateTimeConnection, + ses_EventTypeDisconnection, ses_DateTimeDisconnection, ses_IP, ses_AdditionalInfo, + + CASE WHEN ses_EventTypeConnection = "" THEN + IFNULL ((SELECT MAX(ses_DateTimeDisconnection) FROM Sessions AS SES2 WHERE SES2.ses_MAC = SES1.ses_MAC AND SES2.ses_DateTimeDisconnection < SES1.ses_DateTimeDisconnection), DATETIME(ses_DateTimeDisconnection, "-1 hour")) + ELSE ses_DateTimeConnection + END AS ses_DateTimeConnectionCorrected, + + CASE WHEN ses_EventTypeDisconnection = "" THEN + (SELECT MIN(ses_DateTimeConnection) FROM Sessions AS SES2 WHERE SES2.ses_MAC = SES1.ses_MAC AND SES2.ses_DateTimeConnection > SES1.ses_DateTimeConnection) + ELSE ses_DateTimeDisconnection + END AS ses_DateTimeDisconnectionCorrected + + FROM Sessions AS SES1 + WHERE ses_MAC="' . $mac .'" + AND (ses_DateTimeConnectionCorrected <= date('. $endDate .') + AND (ses_DateTimeDisconnectionCorrected >= date('. $startDate .') OR ses_StillConnected = 1 )) + '); + + // arrays of rows + while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { + // Event color + if ($row['ses_EventTypeConnection'] == '' || $row['ses_EventTypeDisconnection'] == '') { + $color = '#f39c12'; + } else { + $color = '#0073b7'; + } + + // tooltip + $tooltip = 'Connection: ' . formatEventDate ($row['ses_DateTimeConnection'], $row['ses_EventTypeConnection']) . chr(13) . + 'Disconnection: ' . formatEventDate ($row['ses_DateTimeDisconnection'], $row['ses_EventTypeDisconnection']) . chr(13) . + 'IP: ' . $row['ses_IP']; + + // Save row data +// 'start' => formatDateISO ($row['ses_DateTimeConnectionCorrected']), +// 'end' => formatDateISO ($row['ses_DateTimeDisconnectionCorrected']), +// 'start' => $row['ses_DateTimeConnectionCorrected'], +// 'end' => $row['ses_DateTimeDisconnectionCorrected'], + $tableData[] = array( + 'title' => '', + 'start' => formatDateISO ($row['ses_DateTimeConnectionCorrected']), + 'end' => formatDateISO ($row['ses_DateTimeDisconnectionCorrected']), + 'color' => $color, + 'tooltip' => $tooltip + ); + } + + // Control no rows + if (empty($tableData)) { + $tableData = ''; + } + + // Return json + echo (json_encode($tableData)); +} + + +//------------------------------------------------------------------------------ +// Query Presence Calendar for all Devices +//------------------------------------------------------------------------------ +function queryCalendarPresence() { + global $db; + + // Request Parameters + $periodDate = getDateFromPeriod(); + $startDate = '"'. $_REQUEST ['start'] .'"'; + $endDate = '"'. $_REQUEST ['end'] .'"'; + + // SQL + $result = $db->query('SELECT ses_MAC, ses_EventTypeConnection, ses_DateTimeConnection, + ses_EventTypeDisconnection, ses_DateTimeDisconnection, ses_IP, ses_AdditionalInfo, + + CASE WHEN ses_EventTypeConnection = "" THEN + IFNULL ((SELECT MAX(ses_DateTimeDisconnection) FROM Sessions AS SES2 WHERE SES2.ses_MAC = SES1.ses_MAC AND SES2.ses_DateTimeDisconnection < SES1.ses_DateTimeDisconnection), DATETIME(ses_DateTimeDisconnection, "-1 hour")) + ELSE ses_DateTimeConnection + END AS ses_DateTimeConnectionCorrected, + + CASE WHEN ses_EventTypeDisconnection = "" THEN + (SELECT MIN(ses_DateTimeConnection) FROM Sessions AS SES2 WHERE SES2.ses_MAC = SES1.ses_MAC AND SES2.ses_DateTimeConnection > SES1.ses_DateTimeConnection) + ELSE ses_DateTimeDisconnection + END AS ses_DateTimeDisconnectionCorrected + + FROM Sessions AS SES1 + WHERE ( ses_DateTimeConnectionCorrected <= Date('. $endDate .') + AND (ses_DateTimeDisconnectionCorrected >= Date('. $startDate .') OR ses_StillConnected = 1 )) + '); + + // arrays of rows + while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { + // Event color + if ($row['ses_EventTypeConnection'] == '' || $row['ses_EventTypeDisconnection'] == '') { + $color = '#f39c12'; + } else { + $color = '#0073b7'; + } + + // tooltip + $tooltip = 'Connection: ' . formatEventDate ($row['ses_DateTimeConnection'], $row['ses_EventTypeConnection']) . chr(13) . + 'Disconnection: ' . formatEventDate ($row['ses_DateTimeDisconnection'], $row['ses_EventTypeDisconnection']) . chr(13) . + 'IP: ' . $row['ses_IP']; + + // Save row data + $tableData[] = array( + 'resourceId' => $row['ses_MAC'], + 'title' => '', + 'start' => formatDateISO ($row['ses_DateTimeConnectionCorrected']), + 'end' => formatDateISO ($row['ses_DateTimeDisconnectionCorrected']), + 'color' => $color, + 'tooltip' => $tooltip, + 'className' => 'no-border' + ); + } + + // Control no rows + if (empty($tableData)) { + $tableData = ''; + } + + // Return json + echo (json_encode($tableData)); +} + + +//------------------------------------------------------------------------------ +// Query Device events +//------------------------------------------------------------------------------ +function queryDeviceEvents() { + global $db; + + // Request Parameters + $mac = $_REQUEST['mac']; + $periodDate = getDateFromPeriod(); + $hideConnections = $_REQUEST ['hideConnections']; + + // SQL + $result = $db->query('SELECT eve_DateTime, eve_EventType, eve_IP, eve_AdditionalInfo + FROM Events + WHERE eve_MAC="'. $mac .'" AND eve_DateTime >= '. $periodDate .' + AND ( (eve_EventType <> "Connected" AND eve_EventType <> "Disconnected" AND + eve_EventType <> "VOIDED - Connected" AND eve_EventType <> "VOIDED - Disconnected") + OR "'. $hideConnections .'" = "false" ) + '); + + // arrays of rows + $tableData = array(); + while ($row = $result -> fetchArray (SQLITE3_NUM)) { + $row[0] = formatDate ($row[0]); + $tableData['data'][] = $row; + } + + // Control no rows + if (empty($tableData['data'])) { + $tableData['data'] = ''; + } + + // Return json + echo (json_encode ($tableData)); +} + + +?> diff --git a/front/php/server/util.php b/front/php/server/util.php new file mode 100644 index 00000000..30a6d6d3 --- /dev/null +++ b/front/php/server/util.php @@ -0,0 +1,48 @@ + format ('%ad %H:%I'); +} + +function formatDateISO ($date1) { + return date_format (new DateTime ($date1),'c'); +} + +function formatEventDate ($date1, $eventType) { + if (!empty ($date1) ) { + $ret = formatDate ($date1); + } elseif ($eventType == '') { + $ret = ''; + } else { + $ret = ''; + } + + return $ret; +} + +function formatIPlong ($IP) { + return sprintf('%u', ip2long($IP) ); +} + + +//------------------------------------------------------------------------------ +// Others functions +//------------------------------------------------------------------------------ +function getDateFromPeriod () { + $period = $_REQUEST['period']; + return '"'. date ('Y-m-d', strtotime ('+1 day -'.$period) ) .'"'; +} + +function logServerConsole ($text) { + $x = array(); + $y = $x['__________'. $text .'__________']; +} + +?> diff --git a/front/php/templates/footer.php b/front/php/templates/footer.php new file mode 100644 index 00000000..5f44717d --- /dev/null +++ b/front/php/templates/footer.php @@ -0,0 +1,41 @@ + +
+ + © 2020 Puche + +
+ Pi.alert  2.50  (2019-12-30) +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/front/php/templates/header.php b/front/php/templates/header.php new file mode 100644 index 00000000..52fc396e --- /dev/null +++ b/front/php/templates/header.php @@ -0,0 +1,275 @@ + + + + + + Pi.alert + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + +
+ + + + + + +
+ + diff --git a/front/presence.php b/front/presence.php new file mode 100644 index 00000000..bf170536 --- /dev/null +++ b/front/presence.php @@ -0,0 +1,413 @@ + + + + +
+ + +
+

+ Presence by Devices +

+ + + + New Devices period: + + + +
+ + +
+ + + + +
+
+
+
+

Devices

+
+ + +
+ + + +
+
+ +
+ +
+ +
+ + + +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +