RequestStream.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\AppFramework\Http;
  7. /**
  8. * Copy of http://dk1.php.net/manual/en/stream.streamwrapper.example-1.php
  9. * Used to simulate php://input for Request tests
  10. */
  11. class RequestStream {
  12. protected int $position = 0;
  13. protected string $varname = '';
  14. /* @var resource */
  15. public $context;
  16. public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool {
  17. $url = parse_url($path);
  18. $this->varname = $url["host"] ?? '';
  19. $this->position = 0;
  20. return true;
  21. }
  22. public function stream_read(int $count): string {
  23. $ret = substr($GLOBALS[$this->varname], $this->position, $count);
  24. $this->position += strlen($ret);
  25. return $ret;
  26. }
  27. public function stream_write(string $data): int {
  28. $left = substr($GLOBALS[$this->varname], 0, $this->position);
  29. $right = substr($GLOBALS[$this->varname], $this->position + strlen($data));
  30. $GLOBALS[$this->varname] = $left . $data . $right;
  31. $this->position += strlen($data);
  32. return strlen($data);
  33. }
  34. public function stream_tell(): int {
  35. return $this->position;
  36. }
  37. public function stream_eof(): bool {
  38. return $this->position >= strlen($GLOBALS[$this->varname]);
  39. }
  40. public function stream_seek(int $offset, int $whence = SEEK_SET): bool {
  41. switch ($whence) {
  42. case SEEK_SET:
  43. if ($offset < strlen($GLOBALS[$this->varname]) && $offset >= 0) {
  44. $this->position = $offset;
  45. return true;
  46. } else {
  47. return false;
  48. }
  49. break;
  50. case SEEK_CUR:
  51. if ($offset >= 0) {
  52. $this->position += $offset;
  53. return true;
  54. } else {
  55. return false;
  56. }
  57. break;
  58. case SEEK_END:
  59. if (strlen($GLOBALS[$this->varname]) + $offset >= 0) {
  60. $this->position = strlen($GLOBALS[$this->varname]) + $offset;
  61. return true;
  62. } else {
  63. return false;
  64. }
  65. break;
  66. default:
  67. return false;
  68. }
  69. }
  70. public function stream_stat(): array {
  71. $size = strlen($GLOBALS[$this->varname]);
  72. $time = time();
  73. $data = [
  74. 'dev' => 0,
  75. 'ino' => 0,
  76. 'mode' => 0777,
  77. 'nlink' => 1,
  78. 'uid' => 0,
  79. 'gid' => 0,
  80. 'rdev' => '',
  81. 'size' => $size,
  82. 'atime' => $time,
  83. 'mtime' => $time,
  84. 'ctime' => $time,
  85. 'blksize' => -1,
  86. 'blocks' => -1,
  87. ];
  88. return array_values($data) + $data;
  89. //return false;
  90. }
  91. public function stream_metadata(string $path, int $option, $var): bool {
  92. if ($option == STREAM_META_TOUCH) {
  93. $url = parse_url($path);
  94. $varname = $url["host"] ?? '';
  95. if (!isset($GLOBALS[$varname])) {
  96. $GLOBALS[$varname] = '';
  97. }
  98. return true;
  99. }
  100. return false;
  101. }
  102. }