monitorr.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. $options = $this->requestOptions($this->config['monitorrURL'], false, $this->config['homepageMonitorrRefresh']);
  118. $response = Requests::get($dataUrl, ['Token' => $this->config['organizrAPI']], $options);
  119. if ($response->success) {
  120. $html = html_entity_decode($response->body);
  121. // This section grabs the names of all services by regex
  122. $services = [];
  123. $servicesMatch = [];
  124. $servicePattern = '/<div id="servicetitle(?:offline|nolink)?".*><div>(.*)<\/div><\/div><div class="(?:btnonline|btnoffline|btnunknown)".*>(Online|Offline|Unresponsive)<\/div>(:?<\/a>)?<\/div><\/div>/';
  125. preg_match_all($servicePattern, $html, $servicesMatch);
  126. $services = array_filter($servicesMatch[1]);
  127. $status = array_filter($servicesMatch[2]);
  128. $statuses = [];
  129. foreach ($services as $key => $service) {
  130. $match = $status[$key];
  131. $statuses[$service] = $match;
  132. if ($match == 'Online') {
  133. $statuses[$service] = [
  134. 'status' => true
  135. ];
  136. } else if ($match == 'Offline') {
  137. $statuses[$service] = [
  138. 'status' => false
  139. ];
  140. } else if ($match == 'Unresponsive') {
  141. $statuses[$service] = [
  142. 'status' => 'unresponsive'
  143. ];
  144. }
  145. $statuses[$service]['sort'] = $key;
  146. $imageMatch = [];
  147. $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 . '/';
  148. preg_match($imgPattern, $html, $imageMatch);
  149. unset($imageMatch[0]);
  150. $imageMatch = array_values($imageMatch);
  151. // array_push($api['imagematches'][$service], $imageMatch);
  152. foreach ($imageMatch as $match) {
  153. if ($match !== '') {
  154. $image = $match;
  155. }
  156. }
  157. $ext = explode('.', $image);
  158. $ext = $ext[key(array_slice($ext, -1, 1, true))];
  159. $imageUrl = $url . '/assets' . $image;
  160. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  161. $options = $this->requestOptions($this->config['monitorrURL'], false, $this->config['homepageMonitorrRefresh']);
  162. $img = Requests::get($imageUrl, ['Token' => $this->config['organizrAPI']], $options);
  163. if ($img->success) {
  164. $base64 = 'data:image/' . $ext . ';base64,' . base64_encode($img->body);
  165. $statuses[$service]['image'] = $base64;
  166. } else {
  167. $statuses[$service]['image'] = $cacheDirectory . 'no-list.png';
  168. }
  169. $linkMatch = [];
  170. $linkPattern = '/<a class="servicetile" href="(.*)" target="_blank" style="display: block"><div id="serviceimg"><div><img id="' . strtolower($service) . '-service-img/';
  171. preg_match($linkPattern, $html, $linkMatch);
  172. $linkMatch = array_values($linkMatch);
  173. unset($linkMatch[0]);
  174. foreach ($linkMatch as $link) {
  175. if ($link !== '') {
  176. $statuses[$service]['link'] = $link;
  177. }
  178. }
  179. }
  180. foreach ($statuses as $status) {
  181. foreach ($status as $key => $value) {
  182. if (!isset($sortArray[$key])) {
  183. $sortArray[$key] = array();
  184. }
  185. $sortArray[$key][] = $value;
  186. }
  187. }
  188. array_multisort($sortArray['status'], SORT_ASC, $sortArray['sort'], SORT_ASC, $statuses);
  189. $api['services'] = $statuses;
  190. $api['options'] = [
  191. 'title' => $this->config['monitorrHeader'],
  192. 'titleToggle' => $this->config['monitorrHeaderToggle'],
  193. 'compact' => $this->config['monitorrCompact'],
  194. ];
  195. }
  196. } catch (Requests_Exception $e) {
  197. $this->writeLog('error', 'Monitorr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  198. $this->setAPIResponse('error', $e->getMessage(), 401);
  199. return false;
  200. };
  201. $api = isset($api) ? $api : false;
  202. $this->setAPIResponse('success', null, 200, $api);
  203. return $api;
  204. }
  205. }