pihole.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. trait PiHoleHomepageItem
  3. {
  4. public function testConnectionPihole()
  5. {
  6. if (empty($this->config['piholeURL'])) {
  7. $this->setAPIResponse('error', 'Pihole URL is not defined', 422);
  8. return false;
  9. }
  10. $api = array();
  11. $failed = false;
  12. $errors = '';
  13. $urls = explode(',', $this->config['piholeURL']);
  14. foreach ($urls as $url) {
  15. $url = $url . '/api.php?';
  16. try {
  17. $response = Requests::get($url, [], []);
  18. if ($response->success) {
  19. @$test = json_decode($response->body, true);
  20. if (!is_array($test)) {
  21. $ip = $this->qualifyURL($url, true)['host'];
  22. $errors .= $ip . ': Response was not JSON';
  23. $failed = true;
  24. }
  25. }
  26. if (!$response->success) {
  27. $ip = $this->qualifyURL($url, true)['host'];
  28. $errors .= $ip . ': Unknown Failure';
  29. $failed = true;
  30. }
  31. } catch (Requests_Exception $e) {
  32. $failed = true;
  33. $ip = $this->qualifyURL($url, true)['host'];
  34. $errors .= $ip . ': ' . $e->getMessage();
  35. $this->writeLog('error', 'Pi-hole Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  36. };
  37. }
  38. if ($failed) {
  39. $this->setAPIResponse('error', $errors, 500);
  40. return false;
  41. } else {
  42. $this->setAPIResponse('success', null, 200);
  43. return true;
  44. }
  45. }
  46. public function getPiholeHomepageStats()
  47. {
  48. if (!$this->config['homepagePiholeEnabled']) {
  49. $this->setAPIResponse('error', 'Pihole homepage item is not enabled', 409);
  50. return false;
  51. }
  52. if (!$this->qualifyRequest($this->config['homepagePiholeAuth'])) {
  53. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  54. return false;
  55. }
  56. if (empty($this->config['piholeURL'])) {
  57. $this->setAPIResponse('error', 'Pihole URL is not defined', 422);
  58. return false;
  59. }
  60. $api = array();
  61. $urls = explode(',', $this->config['piholeURL']);
  62. foreach ($urls as $url) {
  63. $url = $url . '/api.php?';
  64. try {
  65. $response = Requests::get($url, [], []);
  66. if ($response->success) {
  67. @$piholeResults = json_decode($response->body, true);
  68. if (is_array($piholeResults)) {
  69. $ip = $this->qualifyURL($url, true)['host'];
  70. $api['data'][$ip] = $piholeResults;
  71. }
  72. }
  73. } catch (Requests_Exception $e) {
  74. $this->setAPIResponse('error', $e->getMessage(), 500);
  75. $this->writeLog('error', 'Pi-hole Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  76. return false;
  77. };
  78. }
  79. $api['options']['combine'] = $this->config['homepagePiholeCombine'];
  80. $api['options']['title'] = $this->config['piholeHeaderToggle'];
  81. $api = isset($api) ? $api : null;
  82. $this->setAPIResponse('success', null, 200, $api);
  83. return $api;
  84. }
  85. }