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 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * Adapted from
  5. * https://github.com/OPSnet/Gazelle/blob/master/app/Json.php
  6. *
  7. * Unused as of 2020-12-12
  8. */
  9. abstract class Json
  10. {
  11. protected $version;
  12. protected $source;
  13. protected $mode;
  14. /**
  15. * __construct
  16. */
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. $this->source = SITE_NAME;
  21. $this->mode = 0;
  22. $this->version = 1;
  23. }
  24. /**
  25. * The payload of a valid JSON response, implemented in the child class.
  26. * @return array Payload to be passed to json_encode()
  27. * null if the payload cannot be produced (permissions, id not found, ...).
  28. */
  29. abstract public function payload(): ?array;
  30. /**
  31. * Configure JSON printing (any of the json_encode JSON_* constants)
  32. *
  33. * @param int $mode the bit-or'ed values to confgure encoding results
  34. */
  35. public function setMode(string $mode)
  36. {
  37. $this->mode = $mode;
  38. return $this;
  39. }
  40. /**
  41. * set the version of the Json payload. Increment the
  42. * value when there is significant change in the payload.
  43. * If not called, the version defaults to 1.
  44. *
  45. * @param int version
  46. */
  47. public function setVersion(int $version)
  48. {
  49. $this->version = $version;
  50. return $this;
  51. }
  52. /**
  53. * General failure routine for when bad things happen.
  54. *
  55. * @param string $message The error set in the JSON response
  56. */
  57. public function failure(string $message)
  58. {
  59. print json_encode(
  60. array_merge(
  61. [
  62. 'status' => 'failure',
  63. 'response' => [],
  64. 'error' => $message,
  65. ],
  66. $this->info(),
  67. $this->debug(),
  68. ),
  69. $this->mode
  70. );
  71. }
  72. /**
  73. * emit
  74. */
  75. public function emit()
  76. {
  77. $payload = $this->payload();
  78. if (!$payload) {
  79. return;
  80. }
  81. print json_encode(
  82. array_merge(
  83. [
  84. 'status' => 'success',
  85. 'response' => $payload,
  86. ],
  87. $this->info(),
  88. $this->debug()
  89. ),
  90. $this->mode
  91. );
  92. }
  93. /**
  94. * debug
  95. */
  96. protected function debug()
  97. {
  98. if (!check_perms('site_debug')) {
  99. return [];
  100. }
  101. global $Debug;
  102. return [
  103. 'debug' => [
  104. 'queries' => $Debug->get_queries(),
  105. 'searches' => $Debug->get_sphinxql_queries(),
  106. ],
  107. ];
  108. }
  109. /**
  110. * info
  111. */
  112. protected function info()
  113. {
  114. return [
  115. 'info' => [
  116. 'source' => $this->source,
  117. 'version' => $this->version,
  118. ]
  119. ];
  120. }
  121. }