|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2014 Robin Appelman <[email protected]> |
|
4
|
|
|
* This file is licensed under the Licensed under the MIT license: |
|
5
|
|
|
* http://opensource.org/licenses/MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Icewind\SMB; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Stream optimized for read only usage |
|
12
|
|
|
*/ |
|
13
|
|
|
class NativeReadStream extends NativeStream { |
|
14
|
|
|
const CHUNK_SIZE = 1048576; // 1MB chunks |
|
15
|
|
|
/** |
|
16
|
|
|
* @var resource |
|
17
|
|
|
*/ |
|
18
|
|
|
private $readBuffer = null; |
|
19
|
|
|
|
|
20
|
|
|
private $bufferSize = 0; |
|
21
|
|
|
|
|
22
|
|
|
private $pos = 0; |
|
23
|
|
|
|
|
24
|
40 |
|
public function stream_open($path, $mode, $options, &$opened_path) { |
|
25
|
40 |
|
$this->readBuffer = fopen('php://memory', 'r+'); |
|
26
|
|
|
|
|
27
|
40 |
|
return parent::stream_open($path, $mode, $options, $opened_path); |
|
28
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Wrap a stream from libsmbclient-php into a regular php stream |
|
33
|
|
|
* |
|
34
|
|
|
* @param \Icewind\SMB\NativeState $state |
|
35
|
|
|
* @param resource $smbStream |
|
36
|
|
|
* @param string $mode |
|
37
|
|
|
* @param string $url |
|
38
|
|
|
* @return resource |
|
39
|
|
|
*/ |
|
40
|
40 |
|
public static function wrap($state, $smbStream, $mode, $url) { |
|
41
|
40 |
|
stream_wrapper_register('nativesmb', '\Icewind\SMB\NativeReadStream'); |
|
42
|
40 |
|
$context = stream_context_create(array( |
|
43
|
|
|
'nativesmb' => array( |
|
44
|
40 |
|
'state' => $state, |
|
45
|
40 |
|
'handle' => $smbStream, |
|
46
|
|
|
'url' => $url |
|
47
|
40 |
|
) |
|
48
|
40 |
|
)); |
|
49
|
40 |
|
$fh = fopen('nativesmb://', $mode, false, $context); |
|
50
|
40 |
|
stream_wrapper_unregister('nativesmb'); |
|
51
|
40 |
|
return $fh; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
38 |
|
public function stream_read($count) { |
|
55
|
|
|
// php reads 8192 bytes at once |
|
56
|
|
|
// however due to network latency etc, it's faster to read in larger chunks |
|
57
|
|
|
// and buffer the result |
|
58
|
38 |
|
if (!parent::stream_eof() && $this->bufferSize < $count) { |
|
|
|
|
|
|
59
|
38 |
|
$remaining = $this->readBuffer; |
|
60
|
38 |
|
$this->readBuffer = fopen('php://memory', 'r+'); |
|
61
|
38 |
|
$this->bufferSize = 0; |
|
62
|
38 |
|
stream_copy_to_stream($remaining, $this->readBuffer); |
|
63
|
38 |
|
$this->bufferSize += fwrite($this->readBuffer, parent::stream_read(self::CHUNK_SIZE)); |
|
64
|
38 |
|
fseek($this->readBuffer, 0); |
|
65
|
38 |
|
} |
|
66
|
|
|
|
|
67
|
38 |
|
$result = fread($this->readBuffer, $count); |
|
68
|
38 |
|
$this->bufferSize -= $count; |
|
69
|
|
|
|
|
70
|
38 |
|
$read = strlen($result); |
|
71
|
38 |
|
$this->pos += $read; |
|
72
|
|
|
|
|
73
|
38 |
|
return $result; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
public function stream_seek($offset, $whence = SEEK_SET) { |
|
77
|
2 |
|
$result = parent::stream_seek($offset, $whence); |
|
78
|
2 |
|
if ($result) { |
|
79
|
2 |
|
$this->readBuffer = fopen('php://memory', 'r+'); |
|
80
|
2 |
|
$this->bufferSize = 0; |
|
81
|
2 |
|
$this->pos = parent::stream_tell(); |
|
|
|
|
|
|
82
|
2 |
|
} |
|
83
|
2 |
|
return $result; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
38 |
|
public function stream_eof() { |
|
87
|
38 |
|
return $this->bufferSize <= 0 && parent::stream_eof(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
public function stream_tell() { |
|
91
|
2 |
|
return $this->pos; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function stream_write($data) { |
|
95
|
|
|
return false; |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function stream_truncate($size) { |
|
99
|
|
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.