monitorr.php 7.2 KB

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