Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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      public function __construct($temporaryStorage = self::STORAGE_MEMORY, $temporaryStorageFolder = null)
  30      {
  31          // Open temporary storage
  32          if ($temporaryStorage == self::STORAGE_MEMORY) {
  33              $this->openMemory();
  34          } else {
  35              // Create temporary filename
  36              if ($temporaryStorageFolder === null) {
  37                  $temporaryStorageFolder = File::sysGetTempDir();
  38              }
  39              $this->tempFileName = (string) @tempnam($temporaryStorageFolder, 'xml');
  40  
  41              // Open storage
  42              if (empty($this->tempFileName) || $this->openUri($this->tempFileName) === false) {
  43                  // Fallback to memory...
  44                  $this->openMemory();
  45              }
  46          }
  47  
  48          // Set default values
  49          if (self::$debugEnabled) {
  50              $this->setIndent(true);
  51          }
  52      }
  53  
  54      /**
  55       * Destructor.
  56       */
  57      public function __destruct()
  58      {
  59          // Unlink temporary files
  60          // There is nothing reasonable to do if unlink fails.
  61          if ($this->tempFileName != '') {
  62              /** @scrutinizer ignore-unhandled */
  63              @unlink($this->tempFileName);
  64          }
  65      }
  66  
  67      public function __wakeup(): void
  68      {
  69          $this->tempFileName = '';
  70  
  71          throw new SpreadsheetException('Unserialize not permitted');
  72      }
  73  
  74      /**
  75       * Get written data.
  76       *
  77       * @return string
  78       */
  79      public function getData()
  80      {
  81          if ($this->tempFileName == '') {
  82              return $this->outputMemory(true);
  83          }
  84          $this->flush();
  85  
  86          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      public function writeRawData($rawTextData)
  97      {
  98          if (is_array($rawTextData)) {
  99              $rawTextData = implode("\n", $rawTextData);
 100          }
 101  
 102          return $this->writeRaw(htmlspecialchars($rawTextData ?? ''));
 103      }
 104  }