Passed
Push — master ( 5f7ed9...f1e82a )
by Mark
28:04
created

src/PhpSpreadsheet/Shared/XMLWriter.php (1 issue)

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Shared;
4
5
class XMLWriter extends \XMLWriter
6
{
7
    public static $debugEnabled = false;
8
9
    /** Temporary storage method */
10
    const STORAGE_MEMORY = 1;
11
    const STORAGE_DISK = 2;
12
13
    /**
14
     * Temporary filename.
15
     *
16
     * @var string
17
     */
18
    private $tempFileName = '';
19
20
    /**
21
     * Create a new XMLWriter instance.
22
     *
23
     * @param int $pTemporaryStorage Temporary storage location
24
     * @param string $pTemporaryStorageFolder Temporary storage folder
25
     */
26 92
    public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageFolder = null)
27
    {
28
        // Open temporary storage
29 92
        if ($pTemporaryStorage == self::STORAGE_MEMORY) {
30 92
            $this->openMemory();
31
        } else {
32
            // Create temporary filename
33
            if ($pTemporaryStorageFolder === null) {
34
                $pTemporaryStorageFolder = File::sysGetTempDir();
35
            }
36
            $this->tempFileName = @tempnam($pTemporaryStorageFolder, 'xml');
37
38
            // Open storage
39
            if ($this->openUri($this->tempFileName) === false) {
40
                // Fallback to memory...
41
                $this->openMemory();
42
            }
43
        }
44
45
        // Set default values
46 92
        if (self::$debugEnabled) {
47
            $this->setIndent(true);
48
        }
49 92
    }
50
51
    /**
52
     * Destructor.
53
     */
54 92
    public function __destruct()
55
    {
56
        // Unlink temporary files
57 92
        if ($this->tempFileName != '') {
58
            @unlink($this->tempFileName);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

58
            /** @scrutinizer ignore-unhandled */ @unlink($this->tempFileName);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
59
        }
60 92
    }
61
62
    /**
63
     * Get written data.
64
     *
65
     * @return string
66
     */
67 92
    public function getData()
68
    {
69 92
        if ($this->tempFileName == '') {
70 92
            return $this->outputMemory(true);
71
        }
72
        $this->flush();
73
74
        return file_get_contents($this->tempFileName);
75
    }
76
77
    /**
78
     * Wrapper method for writeRaw.
79
     *
80
     * @param string|string[] $text
81
     *
82
     * @return bool
83
     */
84 85
    public function writeRawData($text)
85
    {
86 85
        if (is_array($text)) {
87
            $text = implode("\n", $text);
88
        }
89
90 85
        return $this->writeRaw(htmlspecialchars($text));
91
    }
92
}
93