prompage.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. use GuzzleHttp\Client;
  3. use GuzzleHttp\Exception\GuzzleException;
  4. trait PromPageHomepageItem
  5. {
  6. private static Client $kumaClient;
  7. public function promPageSettingsArray($infoOnly = false)
  8. {
  9. $homepageInformation = [
  10. 'name' => 'PromPage',
  11. 'enabled' => true,
  12. 'image' => 'plugins/images/tabs/prompage.png',
  13. 'category' => 'Monitor',
  14. 'settingsArray' => __FUNCTION__
  15. ];
  16. if ($infoOnly) {
  17. return $homepageInformation;
  18. }
  19. $homepageSettings = [
  20. 'debug' => true,
  21. 'settings' => [
  22. 'Enable' => [
  23. $this->settingsOption('html', null, ['override' => 6, 'label' => 'Info', 'html' => '<p>This homepage item requires <a href="https://github.com/henrywhitaker3/prompage" target="_blank" rel="noreferrer noopener">PromPage <i class="fa fa-external-link" aria-hidden="true"></i></a> to be running.</p>']),
  24. $this->settingsOption('enable', 'homepagePromPageEnabled'),
  25. ],
  26. 'Connection' => [
  27. $this->settingsOption('url', 'promPageURL', ['help' => 'URL for Uptime Kuma e.g. http://kuma:3001 (no trailing slash)', 'placeholder' => 'http://prompage:3000']),
  28. ],
  29. 'Options' => [
  30. $this->settingsOption('refresh', 'homepagePromPageRefresh'),
  31. $this->settingsOption('title', 'homepagePromPageHeader'),
  32. $this->settingsOption('toggle-title', 'homepagePromPageHeaderToggle'),
  33. $this->settingsOption('switch', 'homepagePromPageCompact', ['label' => 'Compact view', 'help' => 'Toggles the compact view of this homepage module']),
  34. $this->settingsOption('switch', 'homepagePromPageShowUptime', ['label' => 'Show monitor uptime']),
  35. ],
  36. ]
  37. ];
  38. return array_merge($homepageInformation, $homepageSettings);
  39. }
  40. public function promPageHomepagePermissions($key = null)
  41. {
  42. $permissions = [
  43. 'main' => [
  44. 'enabled' => [
  45. 'homepagePromPageEnabled'
  46. ],
  47. 'not_empty' => [
  48. 'promPageURL',
  49. ]
  50. ]
  51. ];
  52. return $this->homepageCheckKeyPermissions($key, $permissions);
  53. }
  54. public function homepageOrderPromPage()
  55. {
  56. if ($this->homepageItemPermissions($this->promPageHomepagePermissions('main'))) {
  57. return '
  58. <div id="' . __FUNCTION__ . '">
  59. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Status Page...</h2></div>
  60. <script>
  61. // PromPage
  62. homepagePromPage("' . $this->config['homepagePromPageRefresh'] . '");
  63. // End PromPage
  64. </script>
  65. </div>
  66. ';
  67. }
  68. }
  69. public function getpromPageHomepageData()
  70. {
  71. if (!$this->homepageItemPermissions($this->promPageHomepagePermissions('main'), true)) {
  72. return false;
  73. }
  74. $api = [];
  75. $url = $this->qualifyURL($this->config['promPageURL']);
  76. try {
  77. $services = json_decode($this->getPromPageClient($url, $this->config['promPageToken'])
  78. ->get('/api/services')
  79. ->getBody()
  80. ->getContents())->services;
  81. $api = [
  82. 'data' => $services,
  83. 'options' => [
  84. 'title' => $this->config['homepagePromPageHeader'],
  85. 'titleToggle' => $this->config['homepagePromPageHeaderToggle'],
  86. 'compact' => $this->config['homepagePromPageCompact'],
  87. 'showUptime' => $this->config['homepagePromPageShowUptime'],
  88. ]
  89. ];
  90. } catch (GuzzleException $e) {
  91. $this->setLoggerChannel('promPage')->error($e);
  92. $this->setAPIResponse('error', $e->getMessage(), 401);
  93. return false;
  94. };
  95. $api = isset($api) ? $api : false;
  96. $this->setAPIResponse('success', null, 200, $api);
  97. return $api;
  98. }
  99. private function getPromPageClient(string $url): Client
  100. {
  101. if (!isset(static::$kumaClient)) {
  102. static::$kumaClient = new Client([
  103. 'base_uri' => $url,
  104. ]);
  105. }
  106. return static::$kumaClient;
  107. }
  108. }