BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

json.class.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * Adapted from
  5. * https://github.com/OPSnet/Gazelle/blob/master/app/Json.php
  6. */
  7. abstract class Json
  8. {
  9. protected $version;
  10. protected $source;
  11. protected $mode;
  12. /**
  13. * __construct
  14. */
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. $this->source = SITE_NAME;
  19. $this->mode = 0;
  20. $this->version = 1;
  21. }
  22. /**
  23. * The payload of a valid JSON response, implemented in the child class.
  24. * @return array Payload to be passed to json_encode()
  25. * null if the payload cannot be produced (permissions, id not found, ...).
  26. */
  27. abstract public function payload(): ?array;
  28. /**
  29. * Configure JSON printing (any of the json_encode JSON_* constants)
  30. *
  31. * @param int $mode the bit-or'ed values to confgure encoding results
  32. */
  33. public function setMode(string $mode)
  34. {
  35. $this->mode = $mode;
  36. return $this;
  37. }
  38. /**
  39. * set the version of the Json payload. Increment the
  40. * value when there is significant change in the payload.
  41. * If not called, the version defaults to 1.
  42. *
  43. * @param int version
  44. */
  45. public function setVersion(int $version)
  46. {
  47. $this->version = $version;
  48. return $this;
  49. }
  50. /**
  51. * General failure routine for when bad things happen.
  52. *
  53. * @param string $message The error set in the JSON response
  54. */
  55. public function failure(string $message)
  56. {
  57. print json_encode(
  58. array_merge(
  59. [
  60. 'status' => 'failure',
  61. 'response' => [],
  62. 'error' => $message,
  63. ],
  64. $this->info(),
  65. $this->debug(),
  66. ),
  67. $this->mode
  68. );
  69. }
  70. /**
  71. * emit
  72. */
  73. public function emit()
  74. {
  75. $payload = $this->payload();
  76. if (!$payload) {
  77. return;
  78. }
  79. print json_encode(
  80. array_merge(
  81. [
  82. 'status' => 'success',
  83. 'response' => $payload,
  84. ],
  85. $this->info(),
  86. $this->debug()
  87. ),
  88. $this->mode
  89. );
  90. }
  91. /**
  92. * debug
  93. */
  94. protected function debug()
  95. {
  96. if (!check_perms('site_debug')) {
  97. return [];
  98. }
  99. global $Debug;
  100. return [
  101. 'debug' => [
  102. 'queries' => $Debug->get_queries(),
  103. 'searches' => $Debug->get_sphinxql_queries(),
  104. ],
  105. ];
  106. }
  107. /**
  108. * info
  109. */
  110. protected function info()
  111. {
  112. return [
  113. 'info' => [
  114. 'source' => $this->source,
  115. 'version' => $this->version,
  116. ]
  117. ];
  118. }
  119. /**
  120. * fetch
  121. *
  122. * Get resources over the API to populate Gazelle display.
  123. * Instead of copy-pasting the same SQL queries in many places.
  124. *
  125. * Takes a query string, e.g., "action=torrentgroup&id=1."
  126. * Requires an API key for the user ID 0 (minor database surgery).
  127. */
  128. public function fetch($Action, $Params = [])
  129. {
  130. $ENV = ENV::go();
  131. $Token = $ENV->getPriv('SELF_API');
  132. $Params = implode('&', $Params);
  133. $ch = curl_init();
  134. # todo: Make this use localhost and not HTTPS
  135. curl_setopt($ch, CURLOPT_URL, "https://$ENV->SITE_DOMAIN/api.php?action=$Action&$Params");
  136. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  137. # https://docs.biotorrents.de
  138. curl_setopt(
  139. $ch,
  140. CURLOPT_HTTPHEADER,
  141. [
  142. 'Accept: application/json',
  143. "Authorization: Bearer $Token",
  144. ]
  145. );
  146. $Data = curl_exec($ch);
  147. curl_close($ch);
  148. # Error out on bad query
  149. if (!$Data) {
  150. return error();
  151. } else {
  152. return json_decode($Data);
  153. }
  154. }
  155. }