Procházet zdrojové kódy

Added backend handling of Monitorr connection

Monitorr doesn't have an API, so have had to use regex to grab the data from one of the Monitorr pages.
Henry Whitaker před 6 roky
rodič
revize
e9da406023

+ 8 - 1
api/config/default.php

@@ -164,6 +164,7 @@ return array(
 	'homepageOrderunifi' => '19',
 	'homepageOrderPihole' => '20',
 	'homepageOrdertautulli' => '21',
+	'homepageOrderMonitorr' => '22',
 	'homepageShowStreamNames' => false,
 	'homepageShowStreamNamesAuth' => '1',
 	'homepageStreamRefresh' => '60000',
@@ -286,5 +287,11 @@ return array(
 	'homepagePiholeRefresh' => '10000',
 	'homepagePiholeCombine' => false,
 	'piholeHeaderToggle' => true,
-	'piholeURL' => ''
+	'piholeURL' => '',
+	'homepageMonitorrEnabled' => false,
+	'homepageMonitorrAuth' => '1',
+	'homepageMonitorrRefresh' => '5000',
+	'monitorrURL' => '',
+	'monitorrHeaderToggle' => true,
+	'monitorrHeader' => 'Monitorr',
 );

+ 78 - 0
api/functions/homepage-connect-functions.php

@@ -65,6 +65,9 @@ function homepageConnect($array)
 		case 'getPihole':
 			return getPihole();
 			break;
+		case 'getMonitorr':
+			return getMonitorr();
+			break;
 		default:
 			# code...
 			break;
@@ -2510,6 +2513,81 @@ function getTautulli()
 	return false;
 }
 
+function getMonitorr()
+{
+	if ($GLOBALS['homepageMonitorrEnabled'] && !empty($GLOBALS['monitorrURL']) && qualifyRequest($GLOBALS['homepageMonitorrAuth'])) {
+		$api = [];
+		$url = qualifyURL($GLOBALS['monitorrURL']);
+		$dataUrl = $url . '/assets/php/loop.php';
+		try {
+			$response = Requests::get($dataUrl, [ 'Token' => $GLOBALS['organizrAPI'] ], []);
+			if ($response->success) {
+				$html = html_entity_decode($response->body);
+
+				// This section grabs the names of all services by regex
+				$services = [];
+				$servicesMatch = [];
+				$servicePattern = '/<div id="servicetitle"><div>(.*)<\/div><\/div><div class="btnonline">Online<\/div><\/a><\/div><\/div>|<div id="servicetitleoffline" style="cursor: default"><div>(.*)<\/div><\/div><div class="btnoffline" style="cursor: default">Offline<\/div><\/div><\/div>/';
+				preg_match_all($servicePattern, $html, $servicesMatch);
+				unset($servicesMatch[0]);
+				$servicesMatch = array_values($servicesMatch);
+				foreach($servicesMatch as $group) {
+					foreach($group as $service) {
+						if($service !== '') {
+							array_push($services, $service);
+						}
+					}
+				}
+
+				// This section then grabs the status and image of that service with regex
+				$statuses = [];
+				foreach($services as $service) {
+					$statusPattern = '/' . $service . '<\/div><\/div><div class="btnonline">(Online)<\/div><\/a><\/div><\/div>|' . $service . '<\/div><\/div><div class="btnoffline" style="cursor: default">(Offline)<\/div><\/div><\/div>/';
+					$status = [];
+					preg_match($statusPattern, $html, $status);
+					$statuses[$service] = $status;
+					foreach($status as $match) {
+						if($match == 'Online') {
+							$statuses[$service] = [
+								'status' => true
+							];
+						} else if($match == 'Offline') {
+							$statuses[$service] = [
+								'status' => false
+							];
+						}
+					}
+
+					$imageMatch = [];
+
+					$imgPattern = '/assets\/img\/..\/img\/(.*)" class="serviceimg" alt=.*><\/div><\/div><div id="servicetitle"><div>' . $service . '|assets\/img\/\.\.\/img\/(.*)" class="serviceimg imgoffline" alt=.*><\/div><\/div><div id="servicetitleoffline" style="cursor: default"><div>' . $service . '/';
+
+					preg_match($imgPattern, $html, $imageMatch);
+					unset($imageMatch[0]);
+					$imageMatch = array_values($imageMatch);
+					// array_push($api['imagematches'][$service], $imageMatch);
+					foreach($imageMatch as $match) {
+						if($match!== '') {
+							$image = $match;
+						}
+					}
+					$statuses[$service]['image'] = $url . '/assets/img/' . $image;
+				}
+
+				$api['services'] = $statuses;
+				$api['options'] = [
+					'title' => $GLOBALS['monitorrHeader'],
+					'titleToggle' => $GLOBALS['monitorrHeaderToggle'],
+				];
+			}
+		} catch (Requests_Exception $e) {
+			writeLog('error', 'Monitorr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
+		};
+		$api = isset($api) ? $api : false;
+		return $api;
+	}
+}
+
 function testAPIConnection($array)
 {
 	switch ($array['data']['action']) {

+ 77 - 0
api/functions/homepage-functions.php

@@ -24,6 +24,7 @@ function homepageOrder()
 		"homepageOrderunifi" => $GLOBALS['homepageOrderunifi'],
 		"homepageOrdertautulli" => $GLOBALS['homepageOrdertautulli'],
 		"homepageOrderPihole" => $GLOBALS['homepageOrderPihole'],
+		"homepageOrderMonitorr" => $GLOBALS['homepageOrderMonitorr'],
 	);
 	asort($homepageOrder);
 	return $homepageOrder;
@@ -356,6 +357,19 @@ function buildHomepageItem($homepageItem)
 				</script>
 				';
 			}
+			break;
+		case 'homepageOrderMonitorr':
+			if ($GLOBALS['homepageMonitorrEnabled']) {
+				$item .= '<div class="white-box"><h2 class="text-center" lang="en">Loading Monitorr...</h2></div>';
+				$item .= '
+				<script>
+				// Monitorr
+				homepageMonitorr("' . $GLOBALS['homepageMonitorrRefresh'] . '");
+				// End Monitorr
+				</script>
+				';
+			}
+			break;
 		default:
 			# code...
 			break;
@@ -2701,6 +2715,62 @@ function getHomepageList()
 				),
 			)
 		),
+		array(
+			'name' => 'Monitorr',
+			'enabled' => true,
+			'image' => 'plugins/images/tabs/monitorr.png',
+			'category' => 'Monitor',
+			'settings' => array(
+				'Enable' => array(
+					array(
+						'type' => 'switch',
+						'name' => 'homepageMonitorrEnabled',
+						'label' => 'Enable',
+						'value' => $GLOBALS['homepageMonitorrEnabled']
+					),
+					array(
+						'type' => 'select',
+						'name' => 'homepageMonitorrAuth',
+						'label' => 'Minimum Authentication',
+						'value' => $GLOBALS['homepageMonitorrAuth'],
+						'options' => $groups
+					)
+				),
+				'Connection' => array(
+					array(
+						'type' => 'input',
+						'name' => 'monitorrURL',
+						'label' => 'URL',
+						'value' => $GLOBALS['monitorrURL'],
+						'help' => 'URL for Monitorr. Please use the revers proxy URL i.e. https://domain.com/monitorr/.',
+						'placeholder' => 'http://domain.com/monitorr/'
+					),
+					array(
+						'type' => 'select',
+						'name' => 'homepageMonitorrRefresh',
+						'label' => 'Refresh Seconds',
+						'value' => $GLOBALS['homepageMonitorrRefresh'],
+						'options' => optionTime()
+					),
+				),
+				'Options' => array(
+					array(
+						'type' => 'input',
+						'name' => 'monitorrHeader',
+						'label' => 'Title',
+						'value' => $GLOBALS['monitorrHeader'],
+						'help' => 'Sets the title of this homepage module',
+					),
+					array(
+						'type' => 'switch',
+						'name' => 'monitorrHeaderToggle',
+						'label' => 'Toggle Title',
+						'value' => $GLOBALS['monitorrHeaderToggle'],
+						'help' => 'Shows/hides the title of this homepage module'
+					)
+				),
+			)
+		),
 	);
 }
 
@@ -2840,6 +2910,13 @@ function buildHomepageSettings()
 					$class .= ' faded';
 				}
 				break;
+			case 'homepageOrderMonitorr':
+				$class = 'bg-info';
+				$image = 'plugins/images/tabs/monitorr.png';
+				if (!$GLOBALS['homepageMonitorrEnabled']) {
+					$class .= ' faded';
+				}
+				break;
 			default:
 				$class = 'blue-bg';
 				$image = '';