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 write only usage |
12
|
|
|
*/ |
13
|
|
|
class NativeWriteStream extends NativeStream { |
14
|
|
|
const CHUNK_SIZE = 1048576; // 1MB chunks |
15
|
|
|
/** |
16
|
|
|
* @var resource |
17
|
|
|
*/ |
18
|
|
|
private $writeBuffer = null; |
19
|
|
|
|
20
|
|
|
private $bufferSize = 0; |
21
|
|
|
|
22
|
|
|
private $pos = 0; |
23
|
|
|
|
24
|
21 |
|
public function stream_open($path, $mode, $options, &$opened_path) { |
25
|
21 |
|
$this->writeBuffer = fopen('php://memory', 'r+'); |
26
|
|
|
|
27
|
21 |
|
return parent::stream_open($path, $mode, $options, $opened_path); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Wrap a stream from libsmbclient-php into a regular php stream |
32
|
|
|
* |
33
|
|
|
* @param \Icewind\SMB\NativeState $state |
34
|
|
|
* @param resource $smbStream |
35
|
|
|
* @param string $mode |
36
|
|
|
* @param string $url |
37
|
|
|
* @return resource |
38
|
|
|
*/ |
39
|
21 |
View Code Duplication |
public static function wrap($state, $smbStream, $mode, $url) { |
40
|
21 |
|
stream_wrapper_register('nativesmb', NativeWriteStream::class); |
41
|
21 |
|
$context = stream_context_create([ |
42
|
|
|
'nativesmb' => [ |
43
|
21 |
|
'state' => $state, |
44
|
21 |
|
'handle' => $smbStream, |
45
|
21 |
|
'url' => $url |
46
|
|
|
] |
47
|
|
|
]); |
48
|
21 |
|
$fh = fopen('nativesmb://', $mode, false, $context); |
49
|
21 |
|
stream_wrapper_unregister('nativesmb'); |
50
|
21 |
|
return $fh; |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
public function stream_seek($offset, $whence = SEEK_SET) { |
54
|
1 |
|
$this->flushWrite(); |
55
|
1 |
|
$result = parent::stream_seek($offset, $whence); |
56
|
1 |
|
if ($result) { |
57
|
1 |
|
$this->pos = parent::stream_tell(); |
|
|
|
|
58
|
|
|
} |
59
|
1 |
|
return $result; |
60
|
|
|
} |
61
|
|
|
|
62
|
21 |
|
private function flushWrite() { |
63
|
21 |
|
rewind($this->writeBuffer); |
64
|
21 |
|
$this->state->write($this->handle, stream_get_contents($this->writeBuffer)); |
65
|
21 |
|
$this->writeBuffer = fopen('php://memory', 'r+'); |
66
|
21 |
|
$this->bufferSize = 0; |
67
|
21 |
|
} |
68
|
|
|
|
69
|
19 |
|
public function stream_write($data) { |
70
|
19 |
|
$written = fwrite($this->writeBuffer, $data); |
71
|
19 |
|
$this->bufferSize += $written; |
72
|
19 |
|
$this->pos += $written; |
73
|
|
|
|
74
|
19 |
|
if ($this->bufferSize >= self::CHUNK_SIZE) { |
75
|
|
|
$this->flushWrite(); |
76
|
|
|
} |
77
|
|
|
|
78
|
19 |
|
return $written; |
79
|
|
|
} |
80
|
|
|
|
81
|
21 |
|
public function stream_close() { |
82
|
21 |
|
$this->flushWrite(); |
83
|
21 |
|
return parent::stream_close(); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
public function stream_tell() { |
87
|
1 |
|
return $this->pos; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function stream_read($count) { |
91
|
|
|
return false; |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
public function stream_truncate($size) { |
95
|
1 |
|
$this->flushWrite(); |
96
|
1 |
|
$this->pos = $size; |
97
|
1 |
|
return parent::stream_truncate($size); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.