pihole.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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('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. ],
  26. 'Misc' => [
  27. $this->settingsOption('toggle-title', 'piholeHeaderToggle'),
  28. $this->settingsOption('switch', 'homepagePiholeCombine', ['label' => 'Combine stat cards', 'help' => 'This controls whether to combine the stats for multiple pihole instances into 1 card.']),
  29. ],
  30. 'Test Connection' => [
  31. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  32. $this->settingsOption('test', 'pihole'),
  33. ]
  34. ]
  35. ];
  36. return array_merge($homepageInformation, $homepageSettings);
  37. }
  38. public function testConnectionPihole()
  39. {
  40. if (empty($this->config['piholeURL'])) {
  41. $this->setAPIResponse('error', 'Pihole URL is not defined', 422);
  42. return false;
  43. }
  44. $api = array();
  45. $failed = false;
  46. $errors = '';
  47. $urls = explode(',', $this->config['piholeURL']);
  48. foreach ($urls as $url) {
  49. $url = $url . '/api.php?';
  50. try {
  51. $response = Requests::get($url, [], []);
  52. if ($response->success) {
  53. @$test = json_decode($response->body, true);
  54. if (!is_array($test)) {
  55. $ip = $this->qualifyURL($url, true)['host'];
  56. $errors .= $ip . ': Response was not JSON';
  57. $failed = true;
  58. }
  59. }
  60. if (!$response->success) {
  61. $ip = $this->qualifyURL($url, true)['host'];
  62. $errors .= $ip . ': Unknown Failure';
  63. $failed = true;
  64. }
  65. } catch (Requests_Exception $e) {
  66. $failed = true;
  67. $ip = $this->qualifyURL($url, true)['host'];
  68. $errors .= $ip . ': ' . $e->getMessage();
  69. $this->writeLog('error', 'Pi-hole Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  70. };
  71. }
  72. if ($failed) {
  73. $this->setAPIResponse('error', $errors, 500);
  74. return false;
  75. } else {
  76. $this->setAPIResponse('success', null, 200);
  77. return true;
  78. }
  79. }
  80. public function piholeHomepagePermissions($key = null)
  81. {
  82. $permissions = [
  83. 'main' => [
  84. 'enabled' => [
  85. 'homepagePiholeEnabled'
  86. ],
  87. 'auth' => [
  88. 'homepagePiholeAuth'
  89. ],
  90. 'not_empty' => [
  91. 'piholeURL'
  92. ]
  93. ]
  94. ];
  95. return $this->homepageCheckKeyPermissions($key, $permissions);
  96. }
  97. public function homepageOrderPihole()
  98. {
  99. if ($this->homepageItemPermissions($this->piholeHomepagePermissions('main'))) {
  100. return '
  101. <div id="' . __FUNCTION__ . '">
  102. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Pihole...</h2></div>
  103. <script>
  104. // Pi-hole Stats
  105. homepagePihole("' . $this->config['homepagePiholeRefresh'] . '");
  106. // End Pi-hole Stats
  107. </script>
  108. </div>
  109. ';
  110. }
  111. }
  112. public function getPiholeHomepageStats()
  113. {
  114. if (!$this->homepageItemPermissions($this->piholeHomepagePermissions('main'), true)) {
  115. return false;
  116. }
  117. $api = array();
  118. $urls = explode(',', $this->config['piholeURL']);
  119. foreach ($urls as $url) {
  120. $url = $url . '/api.php?';
  121. try {
  122. $response = Requests::get($url, [], []);
  123. if ($response->success) {
  124. @$piholeResults = json_decode($response->body, true);
  125. if (is_array($piholeResults)) {
  126. $ip = $this->qualifyURL($url, true)['host'];
  127. $api['data'][$ip] = $piholeResults;
  128. }
  129. }
  130. } catch (Requests_Exception $e) {
  131. $this->setAPIResponse('error', $e->getMessage(), 500);
  132. $this->writeLog('error', 'Pi-hole Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  133. return false;
  134. };
  135. }
  136. $api['options']['combine'] = $this->config['homepagePiholeCombine'];
  137. $api['options']['title'] = $this->config['piholeHeaderToggle'];
  138. $api = isset($api) ? $api : null;
  139. $this->setAPIResponse('success', null, 200, $api);
  140. return $api;
  141. }
  142. }