BaseResponse.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\AppFramework\OCS;
  24. use OCP\AppFramework\Http;
  25. use OCP\AppFramework\Http\DataResponse;
  26. use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
  27. use OCP\AppFramework\Http\Response;
  28. abstract class BaseResponse extends Response {
  29. /** @var array */
  30. protected $data;
  31. /** @var string */
  32. protected $format;
  33. /** @var string */
  34. protected $statusMessage;
  35. /** @var int */
  36. protected $itemsCount;
  37. /** @var int */
  38. protected $itemsPerPage;
  39. /**
  40. * BaseResponse constructor.
  41. *
  42. * @param DataResponse|null $dataResponse
  43. * @param string $format
  44. * @param string|null $statusMessage
  45. * @param int|null $itemsCount
  46. * @param int|null $itemsPerPage
  47. */
  48. public function __construct(DataResponse $dataResponse,
  49. $format = 'xml',
  50. $statusMessage = null,
  51. $itemsCount = null,
  52. $itemsPerPage = null) {
  53. $this->format = $format;
  54. $this->statusMessage = $statusMessage;
  55. $this->itemsCount = $itemsCount;
  56. $this->itemsPerPage = $itemsPerPage;
  57. $this->data = $dataResponse->getData();
  58. $this->setHeaders($dataResponse->getHeaders());
  59. $this->setStatus($dataResponse->getStatus());
  60. $this->setETag($dataResponse->getETag());
  61. $this->setLastModified($dataResponse->getLastModified());
  62. $this->setCookies($dataResponse->getCookies());
  63. $this->setContentSecurityPolicy(new EmptyContentSecurityPolicy());
  64. if ($format === 'json') {
  65. $this->addHeader(
  66. 'Content-Type', 'application/json; charset=utf-8'
  67. );
  68. } else {
  69. $this->addHeader(
  70. 'Content-Type', 'application/xml; charset=utf-8'
  71. );
  72. }
  73. }
  74. /**
  75. * @param string[] $meta
  76. * @return string
  77. */
  78. protected function renderResult(array $meta): string {
  79. $status = $this->getStatus();
  80. if ($status === Http::STATUS_NO_CONTENT ||
  81. $status === Http::STATUS_NOT_MODIFIED ||
  82. ($status >= 100 && $status <= 199)) {
  83. // Those status codes are not supposed to have a body:
  84. // https://stackoverflow.com/q/8628725
  85. return '';
  86. }
  87. $response = [
  88. 'ocs' => [
  89. 'meta' => $meta,
  90. 'data' => $this->data,
  91. ],
  92. ];
  93. if ($this->format === 'json') {
  94. return json_encode($response, JSON_HEX_TAG);
  95. }
  96. $writer = new \XMLWriter();
  97. $writer->openMemory();
  98. $writer->setIndent(true);
  99. $writer->startDocument();
  100. $this->toXML($response, $writer);
  101. $writer->endDocument();
  102. return $writer->outputMemory(true);
  103. }
  104. /**
  105. * @param array $array
  106. * @param \XMLWriter $writer
  107. */
  108. protected function toXML(array $array, \XMLWriter $writer) {
  109. foreach ($array as $k => $v) {
  110. if ($k[0] === '@') {
  111. $writer->writeAttribute(substr($k, 1), $v);
  112. continue;
  113. }
  114. if (\is_numeric($k)) {
  115. $k = 'element';
  116. }
  117. if (\is_array($v)) {
  118. $writer->startElement($k);
  119. $this->toXML($v, $writer);
  120. $writer->endElement();
  121. } else {
  122. $writer->writeElement($k, $v);
  123. }
  124. }
  125. }
  126. public function getOCSStatus() {
  127. return parent::getStatus();
  128. }
  129. }