monitorr.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. trait MonitorrHomepageItem
  3. {
  4. public function getMonitorrHomepageData()
  5. {
  6. if (!$this->config['homepageMonitorrEnabled']) {
  7. $this->setAPIResponse('error', 'Monitorr homepage item is not enabled', 409);
  8. return false;
  9. }
  10. if (!$this->qualifyRequest($this->config['homepageMonitorrAuth'])) {
  11. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  12. return false;
  13. }
  14. if (empty($this->config['monitorrURL'])) {
  15. $this->setAPIResponse('error', 'Monitorr URL is not defined', 422);
  16. return false;
  17. }
  18. $api = [];
  19. $url = $this->qualifyURL($this->config['monitorrURL']);
  20. $dataUrl = $url . '/assets/php/loop.php';
  21. try {
  22. $response = Requests::get($dataUrl, ['Token' => $this->config['organizrAPI']], []);
  23. if ($response->success) {
  24. $html = html_entity_decode($response->body);
  25. // This section grabs the names of all services by regex
  26. $services = [];
  27. $servicesMatch = [];
  28. $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">/';
  29. preg_match_all($servicePattern, $html, $servicesMatch);
  30. $services = array_filter($servicesMatch[1]) + array_filter($servicesMatch[2]) + array_filter($servicesMatch[3]) + array_filter($servicesMatch[4]);
  31. $statuses = [];
  32. foreach ($services as $key => $service) {
  33. $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>/';
  34. $status = [];
  35. preg_match($statusPattern, $html, $status);
  36. $statuses[$service] = $status;
  37. foreach ($status as $match) {
  38. if ($match == 'Online') {
  39. $statuses[$service] = [
  40. 'status' => true
  41. ];
  42. } else if ($match == 'Offline') {
  43. $statuses[$service] = [
  44. 'status' => false
  45. ];
  46. } else if ($match == 'Unresponsive') {
  47. $statuses[$service] = [
  48. 'status' => 'unresponsive'
  49. ];
  50. }
  51. }
  52. $statuses[$service]['sort'] = $key;
  53. $imageMatch = [];
  54. $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 . '/';
  55. preg_match($imgPattern, $html, $imageMatch);
  56. unset($imageMatch[0]);
  57. $imageMatch = array_values($imageMatch);
  58. // array_push($api['imagematches'][$service], $imageMatch);
  59. foreach ($imageMatch as $match) {
  60. if ($match !== '') {
  61. $image = $match;
  62. }
  63. }
  64. $ext = explode('.', $image);
  65. $ext = $ext[key(array_slice($ext, -1, 1, true))];
  66. $imageUrl = $url . '/assets' . $image;
  67. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  68. $img = Requests::get($imageUrl, ['Token' => $this->config['organizrAPI']], []);
  69. if ($img->success) {
  70. $base64 = 'data:image/' . $ext . ';base64,' . base64_encode($img->body);
  71. $statuses[$service]['image'] = $base64;
  72. } else {
  73. $statuses[$service]['image'] = $cacheDirectory . 'no-list.png';
  74. }
  75. $linkMatch = [];
  76. $linkPattern = '/<a class="servicetile" href="(.*)" target="_blank" style="display: block"><div id="serviceimg"><div><img id="' . strtolower($service) . '-service-img/';
  77. preg_match($linkPattern, $html, $linkMatch);
  78. $linkMatch = array_values($linkMatch);
  79. unset($linkMatch[0]);
  80. foreach ($linkMatch as $link) {
  81. if ($link !== '') {
  82. $statuses[$service]['link'] = $link;
  83. }
  84. }
  85. }
  86. foreach ($statuses as $status) {
  87. foreach ($status as $key => $value) {
  88. if (!isset($sortArray[$key])) {
  89. $sortArray[$key] = array();
  90. }
  91. $sortArray[$key][] = $value;
  92. }
  93. }
  94. array_multisort($sortArray['status'], SORT_ASC, $sortArray['sort'], SORT_ASC, $statuses);
  95. $api['services'] = $statuses;
  96. $api['options'] = [
  97. 'title' => $this->config['monitorrHeader'],
  98. 'titleToggle' => $this->config['monitorrHeaderToggle'],
  99. 'compact' => $this->config['monitorrCompact'],
  100. ];
  101. }
  102. } catch (Requests_Exception $e) {
  103. $this->writeLog('error', 'Monitorr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  104. $this->setAPIResponse('error', $e->getMessage(), 401);
  105. return false;
  106. };
  107. $api = isset($api) ? $api : false;
  108. $this->setAPIResponse('success', null, 200, $api);
  109. return $api;
  110. }
  111. }