staticstream.php 3.9 KB

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