summaryrefslogtreecommitdiff
path: root/ext/standard/tests/file/include_streams.phpt
blob: 5f59c87d516c5d07475ae9bba5a41d078097a5f9 (plain)
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
--TEST--
Stream wrappers in include_path
--FILE--
<?php
$data1 = $data2 = $data3 = $data4 = $data5 = $data6 = <<<'EOD'
<?php echo __FILE__ . "\n";?>

EOD;
/*<?*/

class mystream
{
	public $path;
	public $mode;
	public $options;

	public $position;
	public $varname;

	function url_stat($path, $flags) {
		return array();
	}

	function stream_stat() {
		return array();
	}

	function stream_open($path, $mode, $options, &$opened_path)
	{
		$this->path = $path;
		$this->mode = $mode;
		$this->options = $options;

		$split = parse_url($path);
		if ($split["host"] !== b"GLOBALS" ||
		    empty($split["path"]) ||
		    empty($GLOBALS[substr($split["path"],1)])) {
		    return false;
		}
		$this->varname = substr($split["path"],1);

		if (strchr($mode, 'a'))
			$this->position = strlen($GLOBALS[$this->varname]);
		else
			$this->position = 0;

		return true;
	}

	function stream_read($count)
	{
		$ret = substr($GLOBALS[$this->varname], $this->position, $count);
		$this->position += strlen($ret);
		return $ret;
	}

	function stream_tell()
	{
		return $this->position;
	}

	function stream_eof()
	{
		return $this->position >= strlen($GLOBALS[$this->varname]);
	}

	function stream_seek($offset, $whence)
	{
		switch($whence) {
			case SEEK_SET:
				if ($offset < strlen($GLOBALS[$this->varname]) && $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 (strlen($GLOBALS[$this->varname]) + $offset >= 0) {
					$this->position = strlen($GLOBALS[$this->varname]) + $offset;
					return true;
				} else {
					return false;
				}
				break;
			default:
				return false;
		}
	}

}

if (!stream_wrapper_register("test", "mystream")) {
	die("test wrapper registration failed");
}

echo file_get_contents("test://GLOBALS/data1");
include("test://GLOBALS/data1");
include_once("test://GLOBALS/data2");
include_once("test://GLOBALS/data2");
$include_path = get_include_path();
set_include_path($include_path . PATH_SEPARATOR . "test://GLOBALS");
echo file_get_contents("data3", true);
include("data3");
include_once("data4");
include_once("data4");
set_include_path("test://GLOBALS"  . PATH_SEPARATOR .  $include_path);
echo file_get_contents("data5", true);
include("data5");
include_once("data6");
include_once("data6");
?>
--EXPECT--
<?php echo __FILE__ . "\n";?>
test://GLOBALS/data1
test://GLOBALS/data2
<?php echo __FILE__ . "\n";?>
test://GLOBALS/data3
test://GLOBALS/data4
<?php echo __FILE__ . "\n";?>
test://GLOBALS/data5
test://GLOBALS/data6