1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
<?php
class ArrayFileStream
{
private $open_file;
private $position;
private static $fs;
static function set_filesystem(array $fs) {
ArrayFileStream::$fs = $fs;
}
private static function &get_element($path) {
$result =& ArrayFileStream::$fs;
foreach (preg_split('/\//', $path, -1, PREG_SPLIT_NO_EMPTY) as $element) {
if (!isset($result[$element])) {
$null = NULL;
return $null;
}
$result =& $result[$element];
}
return $result;
}
private static function &get_file($path) {
$url = parse_url($path);
$file =& self::get_element($url['host'] . $url['path']);
if (is_null($file)) {
throw new Exception("file not found.");
}
return $file;
}
public function stream_close() {
# nothing to do
}
public function stream_flush() {
# nothing to do
}
public function stream_open($path, $mode, $options, $opened_path) {
try {
$this->open_file =& self::get_file($path);
$this->position = 0;
return true;
} catch (Exception $e) {
return false;
}
}
public function stream_read($count) {
$ret = mb_substr($this->open_file, $this->position, $count);
$this->position += mb_strlen($ret);
return $ret;
}
public function stream_write($data) {
$left = mb_substr($this->open_file, 0, $this->position);
$right = mb_substr($this->open_file, $this->position + mb_strlen($data));
$this->open_file = $left . $data . $right;
$this->position += mb_strlen($data);
return mb_strlen($data);
}
public function stream_tell() {
return $this->position;
}
public function stream_eof() {
return $this->position >= mb_strlen($this->open_file);
}
public function stream_seek($offset, $whence) {
switch ($whence) {
case SEEK_SET:
if ($offset < mb_strlen($this->open_file) && $offset >= 0) {
$this->position = $offset;
return true;
}
else {
return false;
}
break;
case SEEK_CUR:
if ($offset >= 0) {
$this->position += $offset;
return true;
}
else {
return false;
}
break;
case SEEK_END:
if (mb_strlen($this->open_file) + $offset >= 0) {
$this->position = mb_strlen($this->open_file) + $offset;
return true;
}
else {
return false;
}
break;
default:
return false;
}
}
public function stream_set_option(int $option, int $arg1, int $arg2)
{
}
public function stream_stat() {
return array('size' => is_array($this->open_file)
? sizeof($this->open_file)
: mb_strlen($this->open_file));
}
public function unlink($path) {
$parent =& self::get_file(dirname($path));
if (is_array($parent) && isset($parent[basename($path)])) {
unset($parent[basename($path)]);
return TRUE;
}
return FALSE;
}
public function rename($path_from, $path_to) {
throw new Exception('not implemented yet');
}
public function mkdir($path, $mode, $options) {
throw new Exception('not implemented yet');
}
public function rmdir($path, $options) {
throw new Exception('not implemented yet');
}
public function dir_opendir($path, $options) {
throw new Exception('not implemented yet');
}
public function url_stat($path, $flags) {
$time = time();
$keys = array(
'dev' => 0,
'ino' => 0,
'mode' => 33216, // chmod 700
'nlink' => 0,
'uid' => function_exists('posix_getuid') ? posix_getuid() : 0,
'gid' => function_exists('posix_getgid') ? posix_getgid() : 0,
'rdev' => 0,
'size' => $flags & STREAM_URL_STAT_QUIET
? @mb_strlen($this->open_file) : mb_strlen($this->open_file),
'atime' => $time,
'mtime' => $time,
'ctime' => $time,
'blksize' => 0,
'blocks' => 0
);
return array_merge(array_values($keys), $keys);
}
public function dir_readdir() {
throw new Exception('not implemented yet');
}
public function dir_rewinddir() {
throw new Exception('not implemented yet');
}
public function dir_closedir() {
throw new Exception('not implemented yet');
}
}
#ArrayFileStream::set_filesystem(
# array('tmp' =>
# array('a' => 'my content',
# 'b' => '<? echo "hallo welt!";')));
#stream_wrapper_register("var", "ArrayFileStream")
# or die("Failed to register protocol");
#$fp = fopen("var://tmp/a", "rw+");
#while (!feof($fp)) {
# var_dump(fgets($fp));
#}
#fwrite($fp, "line1\n");
#fwrite($fp, "line2\n");
#fwrite($fp, "line3\n");
#rewind($fp);
#var_dump(file_get_contents('var://tmp/a'));
#fclose($fp);
#unlink('var://tmp/a');
#var_dump(include 'var://tmp/b');
|