Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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