monitorr.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. trait MonitorrHomepageItem
  3. {
  4. public function monitorrSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'Monitorr',
  8. 'enabled' => true,
  9. 'image' => 'plugins/images/tabs/monitorr.png',
  10. 'category' => 'Monitor',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $homepageSettings = [
  17. 'debug' => true,
  18. 'settings' => [
  19. 'Enable' => [
  20. $this->settingsOption('enable', 'homepageMonitorrEnabled'),
  21. $this->settingsOption('auth', 'homepageMonitorrAuth'),
  22. ],
  23. 'Connection' => [
  24. $this->settingsOption('url', 'monitorrURL', ['help' => 'URL for Monitorr. Please use the reverse proxy URL i.e. https://domain.com/monitorr/.', 'placeholder' => 'http://domain.com/monitorr/']),
  25. $this->settingsOption('blank'),
  26. $this->settingsOption('disable-cert-check', 'monitorrDisableCertCheck'),
  27. $this->settingsOption('use-custom-certificate', 'monitorrUseCustomCertificate'),
  28. ],
  29. 'Options' => [
  30. $this->settingsOption('refresh', 'homepageMonitorrRefresh'),
  31. $this->settingsOption('switch', 'monitorrCompact', ['label' => 'Compact view', 'help' => 'Toggles the compact view of this homepage module']),
  32. $this->settingsOption('title', 'monitorrHeader'),
  33. $this->settingsOption('toggle-title', 'monitorrHeaderToggle'),
  34. ],
  35. ]
  36. ];
  37. return array_merge($homepageInformation, $homepageSettings);
  38. }
  39. public function monitorrHomepagePermissions($key = null)
  40. {
  41. $permissions = [
  42. 'main' => [
  43. 'enabled' => [
  44. 'homepageMonitorrEnabled'
  45. ],
  46. 'auth' => [
  47. 'homepageMonitorrAuth'
  48. ],
  49. 'not_empty' => [
  50. 'monitorrURL'
  51. ]
  52. ]
  53. ];
  54. return $this->homepageCheckKeyPermissions($key, $permissions);
  55. }
  56. public function homepageOrderMonitorr()
  57. {
  58. if ($this->homepageItemPermissions($this->monitorrHomepagePermissions('main'))) {
  59. return '
  60. <div id="' . __FUNCTION__ . '">
  61. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Monitorr...</h2></div>
  62. <script>
  63. // Monitorr
  64. homepageMonitorr("' . $this->config['homepageMonitorrRefresh'] . '");
  65. // End Monitorr
  66. </script>
  67. </div>
  68. ';
  69. }
  70. }
  71. public function getMonitorrHomepageData()
  72. {
  73. if (!$this->homepageItemPermissions($this->monitorrHomepagePermissions('main'), true)) {
  74. return false;
  75. }
  76. $api = [];
  77. $url = $this->qualifyURL($this->config['monitorrURL']);
  78. $dataUrl = $url . '/assets/php/loop.php';
  79. try {
  80. $options = $this->requestOptions($url, $this->config['homepageMonitorrRefresh'], $this->config['monitorrDisableCertCheck'], $this->config['monitorrUseCustomCertificate']);
  81. $response = Requests::get($dataUrl, ['Token' => $this->config['organizrAPI']], $options);
  82. if ($response->success) {
  83. $html = html_entity_decode($response->body);
  84. // This section grabs the names of all services by regex
  85. $services = [];
  86. $servicesMatch = [];
  87. $servicePattern = '/<div id="servicetitle(?:offline|nolink)?".*><div>(.*)<\/div><\/div><div class="(?:btnonline|btnoffline|btnunknown)".*>(Online|Offline|Unresponsive)<\/div>(:?<\/a>)?<\/div><\/div>/';
  88. preg_match_all($servicePattern, $html, $servicesMatch);
  89. $services = array_filter($servicesMatch[1]);
  90. $status = array_filter($servicesMatch[2]);
  91. $statuses = [];
  92. foreach ($services as $key => $service) {
  93. $match = $status[$key];
  94. $statuses[$service] = $match;
  95. if ($match == 'Online') {
  96. $statuses[$service] = [
  97. 'status' => true
  98. ];
  99. } else if ($match == 'Offline') {
  100. $statuses[$service] = [
  101. 'status' => false
  102. ];
  103. } else if ($match == 'Unresponsive') {
  104. $statuses[$service] = [
  105. 'status' => 'unresponsive'
  106. ];
  107. }
  108. $statuses[$service]['sort'] = $key;
  109. $imageMatch = [];
  110. $imgPattern = '/assets\/img\/\.\.(.*)" class="serviceimg" alt=.*><\/div><\/div><div id="servicetitle"><div>' . $service . '|assets\/img\/\.\.(.*)" class="serviceimg imgoffline" alt=.*><\/div><\/div><div id="servicetitleoffline".*><div>' . $service . '|assets\/img\/\.\.(.*)" class="serviceimg" alt=.*><\/div><\/div><div id="servicetitlenolink".*><div>' . $service . '/';
  111. preg_match($imgPattern, $html, $imageMatch);
  112. unset($imageMatch[0]);
  113. $imageMatch = array_values($imageMatch);
  114. // array_push($api['imagematches'][$service], $imageMatch);
  115. foreach ($imageMatch as $match) {
  116. if ($match !== '') {
  117. $image = $match;
  118. }
  119. }
  120. $ext = explode('.', $image);
  121. $ext = $ext[key(array_slice($ext, -1, 1, true))];
  122. $imageUrl = $url . '/assets' . $image;
  123. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  124. $img = Requests::get($imageUrl, ['Token' => $this->config['organizrAPI']], $options);
  125. if ($img->success) {
  126. $base64 = 'data:image/' . $ext . ';base64,' . base64_encode($img->body);
  127. $statuses[$service]['image'] = $base64;
  128. } else {
  129. $statuses[$service]['image'] = 'plugins/images/homepage/no-list.png';
  130. }
  131. $linkMatch = [];
  132. $linkPattern = '/<a class="servicetile" href="(.*)" target="_blank" style="display: block"><div id="serviceimg"><div><img id="' . strtolower($service) . '-service-img/';
  133. preg_match($linkPattern, $html, $linkMatch);
  134. $linkMatch = array_values($linkMatch);
  135. unset($linkMatch[0]);
  136. foreach ($linkMatch as $link) {
  137. if ($link !== '') {
  138. $statuses[$service]['link'] = $link;
  139. }
  140. }
  141. }
  142. foreach ($statuses as $status) {
  143. foreach ($status as $key => $value) {
  144. if (!isset($sortArray[$key])) {
  145. $sortArray[$key] = array();
  146. }
  147. $sortArray[$key][] = $value;
  148. }
  149. }
  150. array_multisort($sortArray['status'], SORT_ASC, $sortArray['sort'], SORT_ASC, $statuses);
  151. $api['services'] = $statuses;
  152. $api['options'] = [
  153. 'title' => $this->config['monitorrHeader'],
  154. 'titleToggle' => $this->config['monitorrHeaderToggle'],
  155. 'compact' => $this->config['monitorrCompact'],
  156. ];
  157. }
  158. } catch (Requests_Exception $e) {
  159. $this->setLoggerChannel('Monitorr')->error($e);
  160. $this->setAPIResponse('error', $e->getMessage(), 401);
  161. return false;
  162. };
  163. $api = isset($api) ? $api : false;
  164. $this->setAPIResponse('success', null, 200, $api);
  165. return $api;
  166. }
  167. }