staticstream.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  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, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Files\Stream;
  24. class StaticStream {
  25. const MODE_FILE = 0100000;
  26. public $context;
  27. protected static $data = array();
  28. protected $path = '';
  29. protected $pointer = 0;
  30. protected $writable = false;
  31. public function stream_close() {
  32. }
  33. public function stream_eof() {
  34. return $this->pointer >= strlen(self::$data[$this->path]);
  35. }
  36. public function stream_flush() {
  37. }
  38. public static function clear() {
  39. self::$data = array();
  40. }
  41. public function stream_open($path, $mode, $options, &$opened_path) {
  42. switch ($mode[0]) {
  43. case 'r':
  44. if (!isset(self::$data[$path])) return false;
  45. $this->path = $path;
  46. $this->writable = isset($mode[1]) && $mode[1] == '+';
  47. break;
  48. case 'w':
  49. self::$data[$path] = '';
  50. $this->path = $path;
  51. $this->writable = true;
  52. break;
  53. case 'a':
  54. if (!isset(self::$data[$path])) self::$data[$path] = '';
  55. $this->path = $path;
  56. $this->writable = true;
  57. $this->pointer = strlen(self::$data[$path]);
  58. break;
  59. case 'x':
  60. if (isset(self::$data[$path])) return false;
  61. $this->path = $path;
  62. $this->writable = true;
  63. break;
  64. case 'c':
  65. if (!isset(self::$data[$path])) self::$data[$path] = '';
  66. $this->path = $path;
  67. $this->writable = true;
  68. break;
  69. default:
  70. return false;
  71. }
  72. $opened_path = $this->path;
  73. return true;
  74. }
  75. public function stream_read($count) {
  76. $bytes = min(strlen(self::$data[$this->path]) - $this->pointer, $count);
  77. $data = substr(self::$data[$this->path], $this->pointer, $bytes);
  78. $this->pointer += $bytes;
  79. return $data;
  80. }
  81. public function stream_seek($offset, $whence = SEEK_SET) {
  82. $len = strlen(self::$data[$this->path]);
  83. switch ($whence) {
  84. case SEEK_SET:
  85. if ($offset <= $len) {
  86. $this->pointer = $offset;
  87. return true;
  88. }
  89. break;
  90. case SEEK_CUR:
  91. if ($this->pointer + $offset <= $len) {
  92. $this->pointer += $offset;
  93. return true;
  94. }
  95. break;
  96. case SEEK_END:
  97. if ($len + $offset <= $len) {
  98. $this->pointer = $len + $offset;
  99. return true;
  100. }
  101. break;
  102. }
  103. return false;
  104. }
  105. public function stream_stat() {
  106. return $this->url_stat($this->path);
  107. }
  108. public function stream_tell() {
  109. return $this->pointer;
  110. }
  111. public function stream_write($data) {
  112. if (!$this->writable) return 0;
  113. $size = strlen($data);
  114. if ($this->stream_eof()) {
  115. self::$data[$this->path] .= $data;
  116. } else {
  117. self::$data[$this->path] = substr_replace(
  118. self::$data[$this->path],
  119. $data,
  120. $this->pointer
  121. );
  122. }
  123. $this->pointer += $size;
  124. return $size;
  125. }
  126. public function unlink($path) {
  127. if (isset(self::$data[$path])) {
  128. unset(self::$data[$path]);
  129. }
  130. return true;
  131. }
  132. public function url_stat($path) {
  133. if (isset(self::$data[$path])) {
  134. $size = strlen(self::$data[$path]);
  135. $time = time();
  136. $data = array(
  137. 'dev' => 0,
  138. 'ino' => 0,
  139. 'mode' => self::MODE_FILE | 0777,
  140. 'nlink' => 1,
  141. 'uid' => 0,
  142. 'gid' => 0,
  143. 'rdev' => '',
  144. 'size' => $size,
  145. 'atime' => $time,
  146. 'mtime' => $time,
  147. 'ctime' => $time,
  148. 'blksize' => -1,
  149. 'blocks' => -1,
  150. );
  151. return array_values($data) + $data;
  152. }
  153. return false;
  154. }
  155. }