userWatchStatsTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. trait UserWatchStatsTestHomepageItem
  3. {
  4. public function userWatchStatsTestSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'User Watch Statistics Test',
  8. 'enabled' => true,
  9. 'image' => 'plugins/images/homepage/userWatchStats.png',
  10. 'category' => 'Media Server',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $homepageSettings = [
  17. 'debug' => true,
  18. 'settings' => [
  19. 'Enable' => [
  20. $this->settingsOption('enable', 'homepageUserWatchStatsTestEnabled'),
  21. $this->settingsOption('auth', 'homepageUserWatchStatsTestAuth'),
  22. ],
  23. 'Connection' => [
  24. $this->settingsOption('url', 'plexURL', ['label' => 'Tautulli URL']),
  25. $this->settingsOption('token', 'plexToken', ['label' => 'Tautulli API Key']),
  26. $this->settingsOption('disable-cert-check', 'plexDisableCertCheck'),
  27. $this->settingsOption('use-custom-certificate', 'plexUseCustomCertificate'),
  28. ],
  29. 'Test Connection' => [
  30. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  31. $this->settingsOption('test', 'userWatchStatsTest'),
  32. ]
  33. ]
  34. ];
  35. return array_merge($homepageInformation, $homepageSettings);
  36. }
  37. public function testConnectionUserWatchStatsTest()
  38. {
  39. if (!$this->homepageItemPermissions($this->userWatchStatsTestHomepagePermissions('test'), true)) {
  40. return false;
  41. }
  42. $url = $this->qualifyURL($this->config['plexURL']);
  43. $url = $url . "/api/v2?apikey=" . $this->config['plexToken'] . '&cmd=get_server_info';
  44. $options = $this->requestOptions($url, null, $this->config['plexDisableCertCheck'], $this->config['plexUseCustomCertificate']);
  45. try {
  46. $response = Requests::get($url, [], $options);
  47. if ($response->success) {
  48. $data = json_decode($response->body, true);
  49. if (isset($data['response']['result']) && $data['response']['result'] === 'success') {
  50. $this->setAPIResponse('success', 'Successfully connected to Tautulli', 200);
  51. return true;
  52. } else {
  53. $this->setAPIResponse('error', 'Invalid response from Tautulli server', 500);
  54. }
  55. } else {
  56. $this->setAPIResponse('error', 'Tautulli Connection Error', 500);
  57. return false;
  58. }
  59. } catch (Requests_Exception $e) {
  60. $this->setResponse(500, $e->getMessage());
  61. return false;
  62. }
  63. }
  64. public function userWatchStatsTestHomepagePermissions($key = null)
  65. {
  66. $permissions = [
  67. 'test' => [
  68. 'enabled' => [
  69. 'homepageUserWatchStatsTestEnabled',
  70. ],
  71. 'auth' => [
  72. 'homepageUserWatchStatsTestAuth',
  73. ],
  74. 'not_empty' => [
  75. 'plexURL',
  76. 'plexToken'
  77. ]
  78. ],
  79. 'main' => [
  80. 'enabled' => [
  81. 'homepageUserWatchStatsTestEnabled'
  82. ],
  83. 'auth' => [
  84. 'homepageUserWatchStatsTestAuth'
  85. ],
  86. 'not_empty' => [
  87. 'plexURL',
  88. 'plexToken'
  89. ]
  90. ]
  91. ];
  92. return $this->homepageCheckKeyPermissions($key, $permissions);
  93. }
  94. public function homepageOrderUserWatchStatsTest()
  95. {
  96. if ($this->homepageItemPermissions($this->userWatchStatsTestHomepagePermissions('main'))) {
  97. return '
  98. <div id="' . __FUNCTION__ . '">
  99. <div class="white-box">
  100. <div class="white-box-header">
  101. <i class="fa fa-bar-chart"></i> User Watch Statistics Test
  102. </div>
  103. <div class="white-box-content">
  104. <div class="row">
  105. <div class="col-lg-12 text-center">
  106. <h4>Test Plugin Working!</h4>
  107. <p>This is a test version of the User Watch Statistics plugin.</p>
  108. </div>
  109. </div>
  110. </div>
  111. </div>
  112. </div>
  113. ';
  114. }
  115. }
  116. public function getUserWatchStatsTest($options = null)
  117. {
  118. if (!$this->homepageItemPermissions($this->userWatchStatsTestHomepagePermissions('main'), true)) {
  119. return false;
  120. }
  121. try {
  122. $stats = [
  123. 'message' => 'Test plugin is working',
  124. 'status' => 'success'
  125. ];
  126. $this->setAPIResponse('success', 'Test plugin loaded successfully', 200, $stats);
  127. return true;
  128. } catch (Exception $e) {
  129. $this->setAPIResponse('error', 'Test plugin error: ' . $e->getMessage(), 500);
  130. return false;
  131. }
  132. }
  133. }