pihole.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. trait PiHoleHomepageItem
  3. {
  4. public function piholeSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'Pi-hole',
  8. 'enabled' => true,
  9. 'image' => 'plugins/images/tabs/pihole.png',
  10. 'category' => 'Monitor',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $homepageSettings = [
  17. 'debug' => true,
  18. 'settings' => [
  19. 'Enable' => [
  20. $this->settingsOption('enable', 'homepagePiholeEnabled'),
  21. $this->settingsOption('auth', 'homepagePiholeAuth'),
  22. ],
  23. 'Connection' => [
  24. $this->settingsOption('multiple-url', 'piholeURL', ['help' => 'Please make sure to use local IP address and port and to include \'/admin/\' at the end of the URL. You can add multiple Pi-holes by comma separating the URLs.', 'placeholder' => 'http(s)://hostname:port/admin/']),
  25. $this->settingsOption('multiple-token', 'piholeToken'),
  26. ],
  27. 'Misc' => [
  28. $this->settingsOption('toggle-title', 'piholeHeaderToggle'),
  29. $this->settingsOption('switch', 'homepagePiholeCombine', ['label' => 'Combine stat cards', 'help' => 'This controls whether to combine the stats for multiple pihole instances into 1 card.']),
  30. ],
  31. 'Test Connection' => [
  32. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  33. $this->settingsOption('test', 'pihole'),
  34. ]
  35. ]
  36. ];
  37. return array_merge($homepageInformation, $homepageSettings);
  38. }
  39. public function testConnectionPihole()
  40. {
  41. if (empty($this->config['piholeURL'])) {
  42. $this->setAPIResponse('error', 'Pihole URL is not defined', 422);
  43. return false;
  44. }
  45. $failed = false;
  46. $errors = '';
  47. $list = $this->csvHomepageUrlToken($this->config['piholeURL'], $this->config['piholeToken']);
  48. foreach ($list as $key => $value) {
  49. $url = $value['url'] . '/api.php?status';
  50. if ($value['token'] !== '' && $value['token'] !== null) {
  51. $url = $url . '&auth=' . $value['token'];
  52. }
  53. $ip = $this->qualifyURL($url, true)['host'];
  54. try {
  55. $response = Requests::get($url, [], []);
  56. if ($response->success) {
  57. $test = $this->testAndFormatString($response->body);
  58. if (($test['type'] !== 'json')) {
  59. $errors .= $ip . ': Response was not JSON';
  60. $failed = true;
  61. } else {
  62. if (!isset($test['data']['status'])) {
  63. $errors .= $ip . ': Missing API Token';
  64. $failed = true;
  65. }
  66. }
  67. }
  68. if (!$response->success) {
  69. $errors .= $ip . ': Unknown Failure';
  70. $failed = true;
  71. }
  72. } catch (Requests_Exception $e) {
  73. $failed = true;
  74. $errors .= $ip . ': ' . $e->getMessage();
  75. $this->setLoggerChannel('PiHole')->error($e);
  76. };
  77. }
  78. if ($failed) {
  79. $this->setAPIResponse('error', $errors, 500);
  80. return false;
  81. } else {
  82. $this->setAPIResponse('success', null, 200);
  83. return true;
  84. }
  85. }
  86. public function piholeHomepagePermissions($key = null)
  87. {
  88. $permissions = [
  89. 'main' => [
  90. 'enabled' => [
  91. 'homepagePiholeEnabled'
  92. ],
  93. 'auth' => [
  94. 'homepagePiholeAuth'
  95. ],
  96. 'not_empty' => [
  97. 'piholeURL'
  98. ]
  99. ]
  100. ];
  101. return $this->homepageCheckKeyPermissions($key, $permissions);
  102. }
  103. public function homepageOrderPihole()
  104. {
  105. if ($this->homepageItemPermissions($this->piholeHomepagePermissions('main'))) {
  106. return '
  107. <div id="' . __FUNCTION__ . '">
  108. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Pihole...</h2></div>
  109. <script>
  110. // Pi-hole Stats
  111. homepagePihole("' . $this->config['homepagePiholeRefresh'] . '");
  112. // End Pi-hole Stats
  113. </script>
  114. </div>
  115. ';
  116. }
  117. }
  118. public function getPiholeHomepageStats()
  119. {
  120. if (!$this->homepageItemPermissions($this->piholeHomepagePermissions('main'), true)) {
  121. return false;
  122. }
  123. $api = [];
  124. $list = $this->csvHomepageUrlToken($this->config['piholeURL'], $this->config['piholeToken']);
  125. foreach ($list as $key => $value) {
  126. $url = $value['url'] . '/api.php?summaryRaw';
  127. if ($value['token'] !== '' && $value['token'] !== null) {
  128. $url = $url . '&auth=' . $value['token'];
  129. }
  130. try {
  131. $response = Requests::get($url, [], []);
  132. if ($response->success) {
  133. @$piholeResults = json_decode($response->body, true);
  134. if (is_array($piholeResults)) {
  135. $ip = $this->qualifyURL($url, true)['host'];
  136. $api['data'][$ip] = $piholeResults;
  137. }
  138. }
  139. } catch (Requests_Exception $e) {
  140. $this->setResponse(500, $e->getMessage());
  141. $this->setLoggerChannel('PiHole')->error($e);
  142. return false;
  143. };
  144. }
  145. $api['options']['combine'] = $this->config['homepagePiholeCombine'];
  146. $api['options']['title'] = $this->config['piholeHeaderToggle'];
  147. $api = $api ?? null;
  148. $this->setAPIResponse('success', null, 200, $api);
  149. return $api;
  150. }
  151. }