monitorr.php 7.2 KB

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