Output.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\AppFramework\Http;
  8. use OCP\AppFramework\Http\IOutput;
  9. /**
  10. * Very thin wrapper class to make output testable
  11. */
  12. class Output implements IOutput {
  13. /** @var string */
  14. private $webRoot;
  15. /**
  16. * @param $webRoot
  17. */
  18. public function __construct($webRoot) {
  19. $this->webRoot = $webRoot;
  20. }
  21. /**
  22. * @param string $out
  23. */
  24. public function setOutput($out) {
  25. print($out);
  26. }
  27. /**
  28. * @param string|resource $path or file handle
  29. *
  30. * @return bool false if an error occurred
  31. */
  32. public function setReadfile($path) {
  33. if (is_resource($path)) {
  34. $output = fopen('php://output', 'w');
  35. return stream_copy_to_stream($path, $output) > 0;
  36. } else {
  37. return @readfile($path);
  38. }
  39. }
  40. /**
  41. * @param string $header
  42. */
  43. public function setHeader($header) {
  44. header($header);
  45. }
  46. /**
  47. * @param int $code sets the http status code
  48. */
  49. public function setHttpResponseCode($code) {
  50. http_response_code($code);
  51. }
  52. /**
  53. * @return int returns the current http response code
  54. */
  55. public function getHttpResponseCode() {
  56. return http_response_code();
  57. }
  58. /**
  59. * @param string $name
  60. * @param string $value
  61. * @param int $expire
  62. * @param string $path
  63. * @param string $domain
  64. * @param bool $secure
  65. * @param bool $httpOnly
  66. */
  67. public function setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite = 'Lax') {
  68. $path = $this->webRoot ? : '/';
  69. setcookie($name, $value, [
  70. 'expires' => $expire,
  71. 'path' => $path,
  72. 'domain' => $domain,
  73. 'secure' => $secure,
  74. 'httponly' => $httpOnly,
  75. 'samesite' => $sameSite
  76. ]);
  77. }
  78. }