1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Shared; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException; |
6
|
|
|
|
7
|
|
|
class XMLWriter extends \XMLWriter |
8
|
|
|
{ |
9
|
|
|
/** @var bool */ |
10
|
|
|
public static $debugEnabled = false; |
11
|
|
|
|
12
|
|
|
/** Temporary storage method */ |
13
|
|
|
const STORAGE_MEMORY = 1; |
14
|
|
|
const STORAGE_DISK = 2; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Temporary filename. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $tempFileName = ''; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Create a new XMLWriter instance. |
25
|
|
|
* |
26
|
|
|
* @param int $temporaryStorage Temporary storage location |
27
|
|
|
* @param string $temporaryStorageFolder Temporary storage folder |
28
|
|
|
*/ |
29
|
333 |
|
public function __construct($temporaryStorage = self::STORAGE_MEMORY, $temporaryStorageFolder = null) |
30
|
|
|
{ |
31
|
|
|
// Open temporary storage |
32
|
333 |
|
if ($temporaryStorage == self::STORAGE_MEMORY) { |
33
|
332 |
|
$this->openMemory(); |
34
|
|
|
} else { |
35
|
|
|
// Create temporary filename |
36
|
1 |
|
if ($temporaryStorageFolder === null) { |
37
|
1 |
|
$temporaryStorageFolder = File::sysGetTempDir(); |
38
|
|
|
} |
39
|
1 |
|
$this->tempFileName = (string) @tempnam($temporaryStorageFolder, 'xml'); |
40
|
|
|
|
41
|
|
|
// Open storage |
42
|
1 |
|
if (empty($this->tempFileName) || $this->openUri($this->tempFileName) === false) { |
43
|
|
|
// Fallback to memory... |
44
|
|
|
$this->openMemory(); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Set default values |
49
|
333 |
|
if (self::$debugEnabled) { |
50
|
1 |
|
$this->setIndent(true); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Destructor. |
56
|
|
|
*/ |
57
|
333 |
|
public function __destruct() |
58
|
|
|
{ |
59
|
|
|
// Unlink temporary files |
60
|
|
|
// There is nothing reasonable to do if unlink fails. |
61
|
333 |
|
if ($this->tempFileName != '') { |
62
|
|
|
/** @scrutinizer ignore-unhandled */ |
63
|
1 |
|
@unlink($this->tempFileName); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
public function __wakeup(): void |
68
|
|
|
{ |
69
|
1 |
|
$this->tempFileName = ''; |
70
|
|
|
|
71
|
1 |
|
throw new SpreadsheetException('Unserialize not permitted'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get written data. |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
333 |
|
public function getData() |
80
|
|
|
{ |
81
|
333 |
|
if ($this->tempFileName == '') { |
82
|
332 |
|
return $this->outputMemory(true); |
83
|
|
|
} |
84
|
1 |
|
$this->flush(); |
85
|
|
|
|
86
|
1 |
|
return file_get_contents($this->tempFileName) ?: ''; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Wrapper method for writeRaw. |
91
|
|
|
* |
92
|
|
|
* @param null|string|string[] $rawTextData |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
277 |
|
public function writeRawData($rawTextData) |
97
|
|
|
{ |
98
|
277 |
|
if (is_array($rawTextData)) { |
99
|
2 |
|
$rawTextData = implode("\n", $rawTextData); |
100
|
|
|
} |
101
|
|
|
|
102
|
277 |
|
return $this->writeRaw(htmlspecialchars($rawTextData ?? '')); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|