This repository was archived by the owner on May 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathStreamStream.php
More file actions
176 lines (156 loc) · 5.04 KB
/
Copy pathStreamStream.php
File metadata and controls
176 lines (156 loc) · 5.04 KB
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
<?php
/**
* Copyright 2013 Splunk, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"): you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/**
* A stream wrapper implementation that reads from another underlying stream.
*
* This is useful to pass a stream as an input to a PHP API that only accepts
* URIs for its inputs, such as XMLReader.
*
* @package Splunk
* @internal
*/
class Splunk_StreamStream
{
// === Registration ===
private static $registeredStreams = array();
/**
* Makes the specified stream accessible by URI.
*
* @param resource $stream A stream created by fopen().
* @return string A URI that the provided stream can be
* opened with via another fopen() call.
* For example "splunkstream://5016d2d5c9d90".
*/
public static function createUriForStream($stream)
{
$streamId = uniqid();
Splunk_StreamStream::$registeredStreams[$streamId] = $stream;
return 'splunkstream://' . $streamId;
}
/**
* @return integer The number of streams that have been
* registered with createUriForStream()
* that haven't been closed.
*/
public static function getOpenStreamCount()
{
return count(Splunk_StreamStream::$registeredStreams);
}
// === Stream ===
private $stream;
private $streamId;
public function stream_open($path, $mode, $options, &$opened_path)
{
$url = parse_url($path);
$streamId = $url['host'];
if (array_key_exists($streamId, Splunk_StreamStream::$registeredStreams))
{
$this->stream = Splunk_StreamStream::$registeredStreams[$streamId];
$this->streamId = $streamId;
return TRUE;
}
else
{
return FALSE;
}
}
public function stream_read($count)
{
return fread($this->stream, $count);
}
public function stream_write($data)
{
return fwrite($this->stream, $data);
}
public function stream_seek($offset, $whence = SEEK_SET)
{
return fseek($this->stream, $offset, $whence);
}
public function stream_tell()
{
return ftell($this->stream);
}
public function stream_eof()
{
return feof($this->stream);
}
public function stream_stat()
{
return fstat($this->stream);
}
public function stream_flush()
{
return fflush($this->stream);
}
public function stream_close()
{
fclose($this->stream);
// (When called from a shutdown hook, sometimes this variable no
// longer exists when this method is called.)
if (isset(Splunk_StreamStream::$registeredStreams))
{
unset(Splunk_StreamStream::$registeredStreams[$this->streamId]);
}
}
public function url_stat($path, $flags)
{
$url = parse_url($path);
$streamId = $url['host'];
if (array_key_exists($streamId, Splunk_StreamStream::$registeredStreams))
{
$stream = Splunk_StreamStream::$registeredStreams[$streamId];
$statResult = fstat($stream);
if ($statResult === FALSE)
{
/*
* The API for url_stat() always requires a valid (non-FALSE),
* result, even though fstat() can return FALSE. The docs say
* to set unknown values to "a rational value (usually 0)".
*
* XMLReader::open() enforces this, printing out a cryptic
* "Unable to open source data" error if a FALSE result for
* url_stat() is returned.
*/
return array(
'dev' => 0,
'ino' => 0,
'mode' => 0,
'nlink' => 1,
'uid' => 0,
'gid' => 0,
'rdev' => -1,
'size' => 0,
'atime' => 0,
'mtime' => 0,
'ctime' => 0,
'blksize' => -1,
'blocks' => -1,
);
}
else
{
return $statResult;
}
}
else
{
return FALSE;
}
}
}
stream_wrapper_register('splunkstream', 'Splunk_StreamStream') or
die('Could not register protocol.');