streamwrappers.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. global $FAKEDIRS;
  3. $FAKEDIRS=array();
  4. class OC_FakeDirStream{
  5. public static $dirs=array();
  6. private $name;
  7. private $index;
  8. public function dir_opendir($path,$options){
  9. global $FAKEDIRS;
  10. $url=parse_url($path);
  11. $this->name=substr($path,strlen('fakedir://'));
  12. $this->index=0;
  13. if(!isset(self::$dirs[$this->name])){
  14. self::$dirs[$this->name]=array();
  15. }
  16. return true;
  17. }
  18. public function dir_readdir(){
  19. if($this->index>=count(self::$dirs[$this->name])){
  20. return false;
  21. }
  22. $filename=self::$dirs[$this->name][$this->index];
  23. $this->index++;
  24. return $filename;
  25. }
  26. public function dir_closedir() {
  27. $this->name='';
  28. return true;
  29. }
  30. public function dir_rewinddir() {
  31. $this->index=0;
  32. return true;
  33. }
  34. }
  35. class OC_StaticStreamWrapper {
  36. public $context;
  37. protected static $data = array();
  38. protected $path = '';
  39. protected $pointer = 0;
  40. protected $writable = false;
  41. public function stream_close() {}
  42. public function stream_eof() {
  43. return $this->pointer >= strlen(self::$data[$this->path]);
  44. }
  45. public function stream_flush() {}
  46. public function stream_open($path, $mode, $options, &$opened_path) {
  47. switch ($mode[0]) {
  48. case 'r':
  49. if (!isset(self::$data[$path])) return false;
  50. $this->path = $path;
  51. $this->writable = isset($mode[1]) && $mode[1] == '+';
  52. break;
  53. case 'w':
  54. self::$data[$path] = '';
  55. $this->path = $path;
  56. $this->writable = true;
  57. break;
  58. case 'a':
  59. if (!isset(self::$data[$path])) self::$data[$path] = '';
  60. $this->path = $path;
  61. $this->writable = true;
  62. $this->pointer = strlen(self::$data[$path]);
  63. break;
  64. case 'x':
  65. if (isset(self::$data[$path])) return false;
  66. $this->path = $path;
  67. $this->writable = true;
  68. break;
  69. case 'c':
  70. if (!isset(self::$data[$path])) self::$data[$path] = '';
  71. $this->path = $path;
  72. $this->writable = true;
  73. break;
  74. default:
  75. return false;
  76. }
  77. $opened_path = $this->path;
  78. return true;
  79. }
  80. public function stream_read($count) {
  81. $bytes = min(strlen(self::$data[$this->path]) - $this->pointer, $count);
  82. $data = substr(self::$data[$this->path], $this->pointer, $bytes);
  83. $this->pointer += $bytes;
  84. return $data;
  85. }
  86. public function stream_seek($offset, $whence = SEEK_SET) {
  87. $len = strlen(self::$data[$this->path]);
  88. switch ($whence) {
  89. case SEEK_SET:
  90. if ($offset <= $len) {
  91. $this->pointer = $offset;
  92. return true;
  93. }
  94. break;
  95. case SEEK_CUR:
  96. if ($this->pointer + $offset <= $len) {
  97. $this->pointer += $offset;
  98. return true;
  99. }
  100. break;
  101. case SEEK_END:
  102. if ($len + $offset <= $len) {
  103. $this->pointer = $len + $offset;
  104. return true;
  105. }
  106. break;
  107. }
  108. return false;
  109. }
  110. public function stream_stat() {
  111. $size = strlen(self::$data[$this->path]);
  112. $time = time();
  113. return array(
  114. 0 => 0,
  115. 'dev' => 0,
  116. 1 => 0,
  117. 'ino' => 0,
  118. 2 => 0777,
  119. 'mode' => 0777,
  120. 3 => 1,
  121. 'nlink' => 1,
  122. 4 => 0,
  123. 'uid' => 0,
  124. 5 => 0,
  125. 'gid' => 0,
  126. 6 => '',
  127. 'rdev' => '',
  128. 7 => $size,
  129. 'size' => $size,
  130. 8 => $time,
  131. 'atime' => $time,
  132. 9 => $time,
  133. 'mtime' => $time,
  134. 10 => $time,
  135. 'ctime' => $time,
  136. 11 => -1,
  137. 'blksize' => -1,
  138. 12 => -1,
  139. 'blocks' => -1,
  140. );
  141. }
  142. public function stream_tell() {
  143. return $this->pointer;
  144. }
  145. public function stream_write($data) {
  146. if (!$this->writable) return 0;
  147. $size = strlen($data);
  148. $len = strlen(self::$data[$this->path]);
  149. if ($this->stream_eof()) {
  150. self::$data[$this->path] .= $data;
  151. } else {
  152. self::$data[$this->path] = substr_replace(
  153. self::$data[$this->path],
  154. $data,
  155. $this->pointer
  156. );
  157. }
  158. $this->pointer += $size;
  159. return $size;
  160. }
  161. public function unlink($path) {
  162. if (isset(self::$data[$path])) {
  163. unset(self::$data[$path]);
  164. }
  165. return true;
  166. }
  167. public function url_stat($path) {
  168. if (isset(self::$data[$path])) {
  169. $size = strlen(self::$data[$path]);
  170. $time = time();
  171. return array(
  172. 0 => 0,
  173. 'dev' => 0,
  174. 1 => 0,
  175. 'ino' => 0,
  176. 2 => 0777,
  177. 'mode' => 0777,
  178. 3 => 1,
  179. 'nlink' => 1,
  180. 4 => 0,
  181. 'uid' => 0,
  182. 5 => 0,
  183. 'gid' => 0,
  184. 6 => '',
  185. 'rdev' => '',
  186. 7 => $size,
  187. 'size' => $size,
  188. 8 => $time,
  189. 'atime' => $time,
  190. 9 => $time,
  191. 'mtime' => $time,
  192. 10 => $time,
  193. 'ctime' => $time,
  194. 11 => -1,
  195. 'blksize' => -1,
  196. 12 => -1,
  197. 'blocks' => -1,
  198. );
  199. }
  200. return false;
  201. }
  202. }
  203. /**
  204. * stream wrapper that provides a callback on stream close
  205. */
  206. class OC_CloseStreamWrapper{
  207. public static $callBacks=array();
  208. private $path='';
  209. private $source;
  210. private static $open=array();
  211. public function stream_open($path, $mode, $options, &$opened_path){
  212. $path=substr($path,strlen('close://'));
  213. $this->path=$path;
  214. $this->source=fopen($path,$mode);
  215. if(is_resource($this->source)){
  216. $this->meta=stream_get_meta_data($this->source);
  217. }
  218. self::$open[]=$path;
  219. return is_resource($this->source);
  220. }
  221. public function stream_seek($offset, $whence=SEEK_SET){
  222. fseek($this->source,$offset,$whence);
  223. }
  224. public function stream_tell(){
  225. return ftell($this->source);
  226. }
  227. public function stream_read($count){
  228. return fread($this->source,$count);
  229. }
  230. public function stream_write($data){
  231. return fwrite($this->source,$data);
  232. }
  233. public function stream_set_option($option,$arg1,$arg2){
  234. switch($option){
  235. case STREAM_OPTION_BLOCKING:
  236. stream_set_blocking($this->source,$arg1);
  237. break;
  238. case STREAM_OPTION_READ_TIMEOUT:
  239. stream_set_timeout($this->source,$arg1,$arg2);
  240. break;
  241. case STREAM_OPTION_WRITE_BUFFER:
  242. stream_set_write_buffer($this->source,$arg1,$arg2);
  243. }
  244. }
  245. public function stream_stat(){
  246. return fstat($this->source);
  247. }
  248. public function stream_lock($mode){
  249. flock($this->source,$mode);
  250. }
  251. public function stream_flush(){
  252. return fflush($this->source);
  253. }
  254. public function stream_eof(){
  255. return feof($this->source);
  256. }
  257. public function url_stat($path) {
  258. $path=substr($path,strlen('close://'));
  259. if(file_exists($path)){
  260. return stat($path);
  261. }else{
  262. return false;
  263. }
  264. }
  265. public function stream_close(){
  266. fclose($this->source);
  267. if(isset(self::$callBacks[$this->path])){
  268. call_user_func(self::$callBacks[$this->path],$this->path);
  269. }
  270. }
  271. public function unlink($path){
  272. $path=substr($path,strlen('close://'));
  273. return unlink($path);
  274. }
  275. }