monitorr.php 7.0 KB

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