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\Native; |
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
|
20 |
|
public function stream_open($path, $mode, $options, &$opened_path) { |
25
|
20 |
|
$this->readBuffer = fopen('php://memory', 'r+'); |
26
|
|
|
|
27
|
20 |
|
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
|
20 |
View Code Duplication |
public static function wrap($state, $smbStream, $mode, $url) { |
|
|
|
|
41
|
20 |
|
stream_wrapper_register('nativesmb', NativeReadStream::class); |
42
|
20 |
|
$context = stream_context_create(array( |
43
|
|
|
'nativesmb' => array( |
44
|
20 |
|
'state' => $state, |
45
|
20 |
|
'handle' => $smbStream, |
46
|
|
|
'url' => $url |
47
|
20 |
|
) |
48
|
20 |
|
)); |
49
|
20 |
|
$fh = fopen('nativesmb://', $mode, false, $context); |
50
|
20 |
|
stream_wrapper_unregister('nativesmb'); |
51
|
20 |
|
return $fh; |
52
|
|
|
} |
53
|
|
|
|
54
|
19 |
|
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
|
19 |
|
if (!parent::stream_eof() && $this->bufferSize < $count) { |
|
|
|
|
59
|
19 |
|
$remaining = $this->readBuffer; |
60
|
19 |
|
$this->readBuffer = fopen('php://memory', 'r+'); |
61
|
19 |
|
$this->bufferSize = 0; |
62
|
19 |
|
stream_copy_to_stream($remaining, $this->readBuffer); |
63
|
19 |
|
$this->bufferSize += fwrite($this->readBuffer, parent::stream_read(self::CHUNK_SIZE)); |
64
|
19 |
|
fseek($this->readBuffer, 0); |
65
|
19 |
|
} |
66
|
|
|
|
67
|
19 |
|
$result = fread($this->readBuffer, $count); |
68
|
19 |
|
$this->bufferSize -= $count; |
69
|
|
|
|
70
|
19 |
|
$read = strlen($result); |
71
|
19 |
|
$this->pos += $read; |
72
|
|
|
|
73
|
19 |
|
return $result; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public function stream_seek($offset, $whence = SEEK_SET) { |
77
|
1 |
|
$result = parent::stream_seek($offset, $whence); |
78
|
1 |
|
if ($result) { |
79
|
1 |
|
$this->readBuffer = fopen('php://memory', 'r+'); |
80
|
1 |
|
$this->bufferSize = 0; |
81
|
1 |
|
$this->pos = parent::stream_tell(); |
|
|
|
|
82
|
1 |
|
} |
83
|
1 |
|
return $result; |
84
|
|
|
} |
85
|
|
|
|
86
|
19 |
|
public function stream_eof() { |
87
|
19 |
|
return $this->bufferSize <= 0 && parent::stream_eof(); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
public function stream_tell() { |
91
|
1 |
|
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
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.