pihole.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 = array(
  17. 'debug' => true,
  18. 'settings' => array(
  19. 'Enable' => array(
  20. array(
  21. 'type' => 'switch',
  22. 'name' => 'homepagePiholeEnabled',
  23. 'label' => 'Enable',
  24. 'value' => $this->config['homepagePiholeEnabled']
  25. ),
  26. array(
  27. 'type' => 'select',
  28. 'name' => 'homepagePiholeAuth',
  29. 'label' => 'Minimum Authentication',
  30. 'value' => $this->config['homepagePiholeAuth'],
  31. 'options' => $this->groupOptions
  32. )
  33. ),
  34. 'Connection' => array(
  35. array(
  36. 'type' => 'input',
  37. 'name' => 'piholeURL',
  38. 'label' => 'URL',
  39. 'value' => $this->config['piholeURL'],
  40. '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.',
  41. 'placeholder' => 'http(s)://hostname:port/admin/'
  42. ),
  43. ),
  44. 'Misc' => array(
  45. array(
  46. 'type' => 'switch',
  47. 'name' => 'piholeHeaderToggle',
  48. 'label' => 'Toggle Title',
  49. 'value' => $this->config['piholeHeaderToggle'],
  50. 'help' => 'Shows/hides the title of this homepage module'
  51. ),
  52. array(
  53. 'type' => 'switch',
  54. 'name' => 'homepagePiholeCombine',
  55. 'label' => 'Combine stat cards',
  56. 'value' => $this->config['homepagePiholeCombine'],
  57. 'help' => 'This controls whether to combine the stats for multiple pihole instances into 1 card.',
  58. ),
  59. ),
  60. 'Test Connection' => array(
  61. array(
  62. 'type' => 'blank',
  63. 'label' => 'Please Save before Testing'
  64. ),
  65. array(
  66. 'type' => 'button',
  67. 'label' => '',
  68. 'icon' => 'fa fa-flask',
  69. 'class' => 'pull-right',
  70. 'text' => 'Test Connection',
  71. 'attr' => 'onclick="testAPIConnection(\'pihole\')"'
  72. ),
  73. )
  74. )
  75. );
  76. return array_merge($homepageInformation, $homepageSettings);
  77. }
  78. public function testConnectionPihole()
  79. {
  80. if (empty($this->config['piholeURL'])) {
  81. $this->setAPIResponse('error', 'Pihole URL is not defined', 422);
  82. return false;
  83. }
  84. $api = array();
  85. $failed = false;
  86. $errors = '';
  87. $urls = explode(',', $this->config['piholeURL']);
  88. foreach ($urls as $url) {
  89. $url = $url . '/api.php?';
  90. try {
  91. $response = Requests::get($url, [], []);
  92. if ($response->success) {
  93. @$test = json_decode($response->body, true);
  94. if (!is_array($test)) {
  95. $ip = $this->qualifyURL($url, true)['host'];
  96. $errors .= $ip . ': Response was not JSON';
  97. $failed = true;
  98. }
  99. }
  100. if (!$response->success) {
  101. $ip = $this->qualifyURL($url, true)['host'];
  102. $errors .= $ip . ': Unknown Failure';
  103. $failed = true;
  104. }
  105. } catch (Requests_Exception $e) {
  106. $failed = true;
  107. $ip = $this->qualifyURL($url, true)['host'];
  108. $errors .= $ip . ': ' . $e->getMessage();
  109. $this->writeLog('error', 'Pi-hole Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  110. };
  111. }
  112. if ($failed) {
  113. $this->setAPIResponse('error', $errors, 500);
  114. return false;
  115. } else {
  116. $this->setAPIResponse('success', null, 200);
  117. return true;
  118. }
  119. }
  120. public function piholeHomepagePermissions($key = null)
  121. {
  122. $permissions = [
  123. 'main' => [
  124. 'enabled' => [
  125. 'homepagePiholeEnabled'
  126. ],
  127. 'auth' => [
  128. 'homepagePiholeAuth'
  129. ],
  130. 'not_empty' => [
  131. 'piholeURL'
  132. ]
  133. ]
  134. ];
  135. if (array_key_exists($key, $permissions)) {
  136. return $permissions[$key];
  137. } elseif ($key == 'all') {
  138. return $permissions;
  139. } else {
  140. return [];
  141. }
  142. }
  143. public function homepageOrderPihole()
  144. {
  145. if ($this->homepageItemPermissions($this->piholeHomepagePermissions('main'))) {
  146. return '
  147. <div id="' . __FUNCTION__ . '">
  148. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Pihole...</h2></div>
  149. <script>
  150. // Pi-hole Stats
  151. homepagePihole("' . $this->config['homepagePiholeRefresh'] . '");
  152. // End Pi-hole Stats
  153. </script>
  154. </div>
  155. ';
  156. }
  157. }
  158. public function getPiholeHomepageStats()
  159. {
  160. if (!$this->homepageItemPermissions($this->piholeHomepagePermissions('main'), true)) {
  161. return false;
  162. }
  163. $api = array();
  164. $urls = explode(',', $this->config['piholeURL']);
  165. foreach ($urls as $url) {
  166. $url = $url . '/api.php?';
  167. try {
  168. $response = Requests::get($url, [], []);
  169. if ($response->success) {
  170. @$piholeResults = json_decode($response->body, true);
  171. if (is_array($piholeResults)) {
  172. $ip = $this->qualifyURL($url, true)['host'];
  173. $api['data'][$ip] = $piholeResults;
  174. }
  175. }
  176. } catch (Requests_Exception $e) {
  177. $this->setAPIResponse('error', $e->getMessage(), 500);
  178. $this->writeLog('error', 'Pi-hole Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  179. return false;
  180. };
  181. }
  182. $api['options']['combine'] = $this->config['homepagePiholeCombine'];
  183. $api['options']['title'] = $this->config['piholeHeaderToggle'];
  184. $api = isset($api) ? $api : null;
  185. $this->setAPIResponse('success', null, 200, $api);
  186. return $api;
  187. }
  188. }