adguard.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. trait AdGuardHomepageItem
  3. {
  4. public function adguardSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'AdGuardHome',
  8. 'enabled' => true,
  9. 'image' => 'plugins/images/tabs/AdGuardHome.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', 'homepageAdGuardEnabled'),
  21. $this->settingsOption('auth', 'homepageAdGuardAuth'),
  22. ],
  23. 'Connection' => [
  24. $this->settingsOption('url', 'adguardURL', ['help' => 'Please make sure to use local IP address and port. You can add multiple AdGuard Homes by comma separating the URLs.', 'placeholder' => 'http(s)://hostname:port']),
  25. $this->settingsOption('username', 'adGuardUsername'),
  26. $this->settingsOption('password', 'adGuardPassword'),
  27. ],
  28. 'Misc' => [
  29. $this->settingsOption('toggle-title', 'adguardHeaderToggle'),
  30. $this->settingsOption('switch', 'homepageAdGuardCombine', ['label' => 'Combine stat cards', 'help' => 'This controls whether to combine the stats for multiple adguard instances into 1 card.']),
  31. ],
  32. 'Stats' => [
  33. $this->settingsOption('switch', 'adguardQueriesToggle', ['label' => 'Total Queries']),
  34. $this->settingsOption('switch', 'adguardQueriesBlockedToggle', ['label' => 'Queries Blocked']),
  35. $this->settingsOption('switch', 'adguardPercentToggle', ['label' => 'Percent Blocked']),
  36. $this->settingsOption('switch', 'adguardProcessingToggle', ['label' => 'Processing Time']),
  37. $this->settingsOption('switch', 'adguardDomainListToggle', ['label' => 'Domains on Blocklist']),
  38. ],
  39. 'Test Connection' => [
  40. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  41. $this->settingsOption('test', 'adguard'),
  42. ]
  43. ]
  44. ];
  45. return array_merge($homepageInformation, $homepageSettings);
  46. }
  47. public function testConnectionAdGuard()
  48. {
  49. if (empty($this->config['adguardURL'])) {
  50. $this->setAPIResponse('error', 'AdGuard URL is not defined', 422);
  51. return false;
  52. }
  53. $api = array();
  54. $failed = false;
  55. $errors = '';
  56. $urls = explode(',', $this->config['adguardURL']);
  57. foreach ($urls as $url) {
  58. $url = $url . '/control/stats';
  59. try {
  60. $options = array(
  61. 'auth' => array($this->config['adGuardUsername'], $this->decrypt($this->config['adGuardPassword']))
  62. );
  63. $response = Requests::get($url, [], $options);
  64. if ($response->success) {
  65. @$test = json_decode($response->body, true);
  66. if (!is_array($test)) {
  67. $ip = $this->qualifyURL($url, true)['host'];
  68. $errors .= $ip . ': Response was not JSON';
  69. $failed = true;
  70. }
  71. }
  72. if (!$response->success) {
  73. $ip = $this->qualifyURL($url, true)['host'];
  74. $errors .= $ip . ": Unknown Failure";
  75. $failed = true;
  76. }
  77. } catch (Requests_Exception $e) {
  78. $failed = true;
  79. $ip = $this->qualifyURL($url, true)['host'];
  80. $errors .= $ip . ': ' . $e->getMessage();
  81. $this->setLoggerChannel('AdGuard')->error($e);
  82. };
  83. }
  84. if ($failed) {
  85. $this->setAPIResponse('error', $errors, 500);
  86. return false;
  87. } else {
  88. $this->setAPIResponse('success', null, 200);
  89. return true;
  90. }
  91. }
  92. public function adguardHomepagePermissions($key = null)
  93. {
  94. $permissions = [
  95. 'main' => [
  96. 'enabled' => [
  97. 'homepageAdGuardEnabled'
  98. ],
  99. 'auth' => [
  100. 'homepageAdGuardAuth'
  101. ],
  102. 'not_empty' => [
  103. 'adguardURL'
  104. ]
  105. ]
  106. ];
  107. return $this->homepageCheckKeyPermissions($key, $permissions);
  108. }
  109. public function homepageOrderAdGuard()
  110. {
  111. if ($this->homepageItemPermissions($this->adguardHomepagePermissions('main'))) {
  112. return '
  113. <div id="' . __FUNCTION__ . '">
  114. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading AdGuard...</h2></div>
  115. <script>
  116. // Pi-hole Stats
  117. homepageAdGuard("' . $this->config['homepageAdGuardRefresh'] . '");
  118. // End Pi-hole Stats
  119. </script>
  120. </div>
  121. ';
  122. }
  123. }
  124. public function getAdGuardHomepageStats()
  125. {
  126. if (!$this->homepageItemPermissions($this->adguardHomepagePermissions('main'), true)) {
  127. return false;
  128. }
  129. $stats = array();
  130. $urls = explode(',', $this->config['adguardURL']);
  131. foreach ($urls as $url) {
  132. $stats_url = $url . '/control/stats?';
  133. $filter_url = $url . '/control/filtering/status?';
  134. try {
  135. $options = array(
  136. 'auth' => array($this->config['adGuardUsername'], $this->decrypt($this->config['adGuardPassword']))
  137. );
  138. $response = Requests::get($stats_url, [], $options);
  139. if ($response->success) {
  140. @$adguardResults = json_decode($response->body, true);
  141. if (is_array($adguardResults)) {
  142. $ip = $this->qualifyURL($stats_url, true)['host'];
  143. $stats['data'][$ip] = $adguardResults;
  144. }
  145. }
  146. $response = Requests::get($filter_url, [], $options);
  147. if ($response->success) {
  148. @$adguardFilterResults = json_decode($response->body, true);
  149. if (is_array($adguardFilterResults)) {
  150. $ip = $this->qualifyURL($filter_url, true)['host'];
  151. $stats['filters'][$ip] = $adguardFilterResults;
  152. }
  153. }
  154. } catch (Requests_Exception $e) {
  155. $this->setResponse(500, $e->getMessage());
  156. $this->setLoggerChannel('AdGuard')->error($e);
  157. return false;
  158. };
  159. }
  160. $stats['options']['combine'] = $this->config['homepageAdGuardCombine'];
  161. $stats['options']['title'] = $this->config['adguardHeaderToggle'];
  162. $stats['options']['queries'] = $this->config['adguardQueriesToggle'];
  163. $stats['options']['blocked_count'] = $this->config['adguardQueriesBlockedToggle'];
  164. $stats['options']['blocked_percent'] = $this->config['adguardPercentToggle'];
  165. $stats['options']['processing_time'] = $this->config['adguardProcessingToggle'];
  166. $stats['options']['domain_count'] = $this->config['adguardDomainListToggle'];
  167. $stats = isset($stats) ? $stats : null;
  168. $this->setAPIResponse('success', null, 200, $stats);
  169. return $stats;
  170. }
  171. }