pihole.php 5.2 KB

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