1<?php
2
3if (!defined('DATE_W3C')) {
4    define('DATE_W3C', 'Y-m-d\TH:i:sP');
5}
6
7if (!defined('DEBUGMODE_ENABLED')) {
8    define('DEBUGMODE_ENABLED', false);
9}
10
11/**
12 * PHPExcel_Shared_XMLWriter
13 *
14 * Copyright (c) 2006 - 2015 PHPExcel
15 *
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24 * Lesser General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
29 *
30 * @category   PHPExcel
31 * @package    PHPExcel_Shared
32 * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
33 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
34 * @version    ##VERSION##, ##DATE##
35 */
36class PHPExcel_Shared_XMLWriter extends XMLWriter
37{
38    /** Temporary storage method */
39    const STORAGE_MEMORY    = 1;
40    const STORAGE_DISK      = 2;
41
42    /**
43     * Temporary filename
44     *
45     * @var string
46     */
47    private $tempFileName  = '';
48
49    /**
50     * Create a new PHPExcel_Shared_XMLWriter instance
51     *
52     * @param int      $pTemporaryStorage        Temporary storage location
53     * @param string   $pTemporaryStorageFolder  Temporary storage folder
54     */
55    public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageFolder = null)
56    {
57        // Open temporary storage
58        if ($pTemporaryStorage == self::STORAGE_MEMORY) {
59            $this->openMemory();
60        } else {
61            // Create temporary filename
62            if ($pTemporaryStorageFolder === null) {
63                $pTemporaryStorageFolder = PHPExcel_Shared_File::sys_get_temp_dir();
64            }
65            $this->tempFileName = @tempnam($pTemporaryStorageFolder, 'xml');
66
67            // Open storage
68            if ($this->openUri($this->tempFileName) === false) {
69                // Fallback to memory...
70                $this->openMemory();
71            }
72        }
73
74        // Set default values
75        if (DEBUGMODE_ENABLED) {
76            $this->setIndent(true);
77        }
78    }
79
80    /**
81     * Destructor
82     */
83    public function __destruct()
84    {
85        // Unlink temporary files
86        if ($this->tempFileName != '') {
87            @unlink($this->tempFileName);
88        }
89    }
90
91    /**
92     * Get written data
93     *
94     * @return $data
95     */
96    public function getData()
97    {
98        if ($this->tempFileName == '') {
99            return $this->outputMemory(true);
100        } else {
101            $this->flush();
102            return file_get_contents($this->tempFileName);
103        }
104    }
105
106    /**
107     * Fallback method for writeRaw, introduced in PHP 5.2
108     *
109     * @param string $text
110     * @return string
111     */
112    public function writeRawData($text)
113    {
114        if (is_array($text)) {
115            $text = implode("\n", $text);
116        }
117
118        if (method_exists($this, 'writeRaw')) {
119            return $this->writeRaw(htmlspecialchars($text));
120        }
121
122        return $this->text($text);
123    }
124}
125