monitorr.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 getMonitorrHomepageData()
  71. {
  72. if (!$this->config['homepageMonitorrEnabled']) {
  73. $this->setAPIResponse('error', 'Monitorr homepage item is not enabled', 409);
  74. return false;
  75. }
  76. if (!$this->qualifyRequest($this->config['homepageMonitorrAuth'])) {
  77. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  78. return false;
  79. }
  80. if (empty($this->config['monitorrURL'])) {
  81. $this->setAPIResponse('error', 'Monitorr URL is not defined', 422);
  82. return false;
  83. }
  84. $api = [];
  85. $url = $this->qualifyURL($this->config['monitorrURL']);
  86. $dataUrl = $url . '/assets/php/loop.php';
  87. try {
  88. $response = Requests::get($dataUrl, ['Token' => $this->config['organizrAPI']], []);
  89. if ($response->success) {
  90. $html = html_entity_decode($response->body);
  91. // This section grabs the names of all services by regex
  92. $services = [];
  93. $servicesMatch = [];
  94. $servicePattern = '/<div id="servicetitle"><div>(.*)<\/div><\/div><div class="btnonline">Online<\/div><\/a><\/div><\/div>|<div id="servicetitleoffline".*><div>(.*)<\/div><\/div><div class="btnoffline".*>Offline<\/div><\/div><\/div>|<div id="servicetitlenolink".*><div>(.*)<\/div><\/div><div class="btnonline".*>Online<\/div><\/div><\/div>|<div id="servicetitle"><div>(.*)<\/div><\/div><div class="btnunknown">/';
  95. preg_match_all($servicePattern, $html, $servicesMatch);
  96. $services = array_filter($servicesMatch[1]) + array_filter($servicesMatch[2]) + array_filter($servicesMatch[3]) + array_filter($servicesMatch[4]);
  97. $statuses = [];
  98. foreach ($services as $key => $service) {
  99. $statusPattern = '/' . $service . '<\/div><\/div><div class="btnonline">(Online)<\/div>|' . $service . '<\/div><\/div><div class="btnoffline".*>(Offline)<\/div><\/div><\/div>|' . $service . '<\/div><\/div><div class="btnunknown">(.*)<\/div><\/a>/';
  100. $status = [];
  101. preg_match($statusPattern, $html, $status);
  102. $statuses[$service] = $status;
  103. foreach ($status as $match) {
  104. if ($match == 'Online') {
  105. $statuses[$service] = [
  106. 'status' => true
  107. ];
  108. } else if ($match == 'Offline') {
  109. $statuses[$service] = [
  110. 'status' => false
  111. ];
  112. } else if ($match == 'Unresponsive') {
  113. $statuses[$service] = [
  114. 'status' => 'unresponsive'
  115. ];
  116. }
  117. }
  118. $statuses[$service]['sort'] = $key;
  119. $imageMatch = [];
  120. $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 . '/';
  121. preg_match($imgPattern, $html, $imageMatch);
  122. unset($imageMatch[0]);
  123. $imageMatch = array_values($imageMatch);
  124. // array_push($api['imagematches'][$service], $imageMatch);
  125. foreach ($imageMatch as $match) {
  126. if ($match !== '') {
  127. $image = $match;
  128. }
  129. }
  130. $ext = explode('.', $image);
  131. $ext = $ext[key(array_slice($ext, -1, 1, true))];
  132. $imageUrl = $url . '/assets' . $image;
  133. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  134. $img = Requests::get($imageUrl, ['Token' => $this->config['organizrAPI']], []);
  135. if ($img->success) {
  136. $base64 = 'data:image/' . $ext . ';base64,' . base64_encode($img->body);
  137. $statuses[$service]['image'] = $base64;
  138. } else {
  139. $statuses[$service]['image'] = $cacheDirectory . 'no-list.png';
  140. }
  141. $linkMatch = [];
  142. $linkPattern = '/<a class="servicetile" href="(.*)" target="_blank" style="display: block"><div id="serviceimg"><div><img id="' . strtolower($service) . '-service-img/';
  143. preg_match($linkPattern, $html, $linkMatch);
  144. $linkMatch = array_values($linkMatch);
  145. unset($linkMatch[0]);
  146. foreach ($linkMatch as $link) {
  147. if ($link !== '') {
  148. $statuses[$service]['link'] = $link;
  149. }
  150. }
  151. }
  152. foreach ($statuses as $status) {
  153. foreach ($status as $key => $value) {
  154. if (!isset($sortArray[$key])) {
  155. $sortArray[$key] = array();
  156. }
  157. $sortArray[$key][] = $value;
  158. }
  159. }
  160. array_multisort($sortArray['status'], SORT_ASC, $sortArray['sort'], SORT_ASC, $statuses);
  161. $api['services'] = $statuses;
  162. $api['options'] = [
  163. 'title' => $this->config['monitorrHeader'],
  164. 'titleToggle' => $this->config['monitorrHeaderToggle'],
  165. 'compact' => $this->config['monitorrCompact'],
  166. ];
  167. }
  168. } catch (Requests_Exception $e) {
  169. $this->writeLog('error', 'Monitorr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  170. $this->setAPIResponse('error', $e->getMessage(), 401);
  171. return false;
  172. };
  173. $api = isset($api) ? $api : false;
  174. $this->setAPIResponse('success', null, 200, $api);
  175. return $api;
  176. }
  177. }