Output.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Stefan Weil <sw@weilnetz.de>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\AppFramework\Http;
  28. use OCP\AppFramework\Http\IOutput;
  29. /**
  30. * Very thin wrapper class to make output testable
  31. */
  32. class Output implements IOutput {
  33. /** @var string */
  34. private $webRoot;
  35. /**
  36. * @param $webRoot
  37. */
  38. public function __construct($webRoot) {
  39. $this->webRoot = $webRoot;
  40. }
  41. /**
  42. * @param string $out
  43. */
  44. public function setOutput($out) {
  45. print($out);
  46. }
  47. /**
  48. * @param string|resource $path or file handle
  49. *
  50. * @return bool false if an error occurred
  51. */
  52. public function setReadfile($path) {
  53. if (is_resource($path)) {
  54. $output = fopen('php://output', 'w');
  55. return stream_copy_to_stream($path, $output) > 0;
  56. } else {
  57. return @readfile($path);
  58. }
  59. }
  60. /**
  61. * @param string $header
  62. */
  63. public function setHeader($header) {
  64. header($header);
  65. }
  66. /**
  67. * @param int $code sets the http status code
  68. */
  69. public function setHttpResponseCode($code) {
  70. http_response_code($code);
  71. }
  72. /**
  73. * @return int returns the current http response code
  74. */
  75. public function getHttpResponseCode() {
  76. return http_response_code();
  77. }
  78. /**
  79. * @param string $name
  80. * @param string $value
  81. * @param int $expire
  82. * @param string $path
  83. * @param string $domain
  84. * @param bool $secure
  85. * @param bool $httpOnly
  86. */
  87. public function setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite = 'Lax') {
  88. $path = $this->webRoot ? : '/';
  89. setcookie($name, $value, [
  90. 'expires' => $expire,
  91. 'path' => $path,
  92. 'domain' => $domain,
  93. 'secure' => $secure,
  94. 'httponly' => $httpOnly,
  95. 'samesite' => $sameSite
  96. ]);
  97. }
  98. }