0337d704 |
1 | <?php |
2 | /*************************************************************************** |
5ddeb07c |
3 | * Copyright (C) 2003-2007 Polytechnique.org * |
0337d704 |
4 | * http://opensource.polytechnique.org/ * |
5 | * * |
6 | * This program is free software; you can redistribute it and/or modify * |
7 | * it under the terms of the GNU General Public License as published by * |
8 | * the Free Software Foundation; either version 2 of the License, or * |
9 | * (at your option) any later version. * |
10 | * * |
11 | * This program is distributed in the hope that it will be useful, * |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
14 | * GNU General Public License for more details. * |
15 | * * |
16 | * You should have received a copy of the GNU General Public License * |
17 | * along with this program; if not, write to the Free Software * |
18 | * Foundation, Inc., * |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
20 | ***************************************************************************/ |
21 | |
0337d704 |
22 | class VarStream |
23 | { |
0337d704 |
24 | // Stream handler to read from global variables |
c83168b1 |
25 | private $varname; |
26 | private $position; |
0337d704 |
27 | |
28 | function stream_open($path, $mode, $options, &$opened_path) |
29 | { |
30 | $url = parse_url($path); |
31 | $this->varname = $url['host']; |
32 | $this->position = 0; |
33 | if (!isset($GLOBALS[$this->varname])) |
34 | { |
35 | trigger_error('Global variable '.$this->varname.' does not exist', E_USER_WARNING); |
36 | return false; |
37 | } |
38 | return true; |
39 | } |
40 | |
0337d704 |
41 | function stream_close() |
42 | { |
43 | } |
63528107 |
44 | |
0337d704 |
45 | function stream_read($count) |
46 | { |
47 | $ret = substr($GLOBALS[$this->varname], $this->position, $count); |
48 | $this->position += strlen($ret); |
49 | return $ret; |
50 | } |
51 | |
0337d704 |
52 | function stream_write($data) |
53 | { |
54 | $len = strlen($data); |
55 | if ($len > $this->position + strlen($GLOBALS[$this->varname])) { |
56 | str_pad($GLOBALS[$this->varname], $len); |
57 | } |
58 | |
59 | $GLOBALS[$this->varname] = substr_replace($GLOBALS[$this->varname], $data, $this->position, $len); |
60 | $this->position += $len; |
61 | } |
63528107 |
62 | |
0337d704 |
63 | function stream_eof() |
64 | { |
65 | return $this->position >= strlen($GLOBALS[$this->varname]); |
66 | } |
67 | |
0337d704 |
68 | function stream_tell() |
69 | { |
70 | return $this->position; |
71 | } |
72 | |
0337d704 |
73 | function stream_seek($offs, $whence) |
74 | { |
75 | switch ($whence) { |
76 | case SEEK_SET: |
77 | $final = $offs; |
78 | break; |
79 | |
80 | case SEEK_CUR: |
81 | $final += $offs; |
82 | break; |
83 | |
84 | case SEEK_END: |
85 | $final = strlen($GLOBALS[$this->varname]) + $offs; |
86 | break; |
87 | } |
88 | |
89 | if ($final < 0) { |
90 | return -1; |
91 | } |
92 | $this->position = $final; |
93 | return 0; |
94 | } |
95 | |
0337d704 |
96 | function stream_flush() |
97 | { |
98 | } |
63528107 |
99 | |
c83168b1 |
100 | static function init() |
101 | { |
102 | stream_wrapper_register('var','VarStream'); |
103 | } |
0337d704 |
104 | } |
105 | |
0337d704 |
106 | ?> |