Output.php 2.3 KB

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