OpenDocument.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * PHPExcel_Writer_OpenDocument
  4. *
  5. * Copyright (c) 2006 - 2015 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Writer_OpenDocument
  23. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. class PHPExcel_Writer_OpenDocument extends PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter
  28. {
  29. /**
  30. * Private writer parts
  31. *
  32. * @var PHPExcel_Writer_OpenDocument_WriterPart[]
  33. */
  34. private $writerParts = array();
  35. /**
  36. * Private PHPExcel
  37. *
  38. * @var PHPExcel
  39. */
  40. private $spreadSheet;
  41. /**
  42. * Create a new PHPExcel_Writer_OpenDocument
  43. *
  44. * @param PHPExcel $pPHPExcel
  45. */
  46. public function __construct(PHPExcel $pPHPExcel = null)
  47. {
  48. $this->setPHPExcel($pPHPExcel);
  49. $writerPartsArray = array(
  50. 'content' => 'PHPExcel_Writer_OpenDocument_Content',
  51. 'meta' => 'PHPExcel_Writer_OpenDocument_Meta',
  52. 'meta_inf' => 'PHPExcel_Writer_OpenDocument_MetaInf',
  53. 'mimetype' => 'PHPExcel_Writer_OpenDocument_Mimetype',
  54. 'settings' => 'PHPExcel_Writer_OpenDocument_Settings',
  55. 'styles' => 'PHPExcel_Writer_OpenDocument_Styles',
  56. 'thumbnails' => 'PHPExcel_Writer_OpenDocument_Thumbnails'
  57. );
  58. foreach ($writerPartsArray as $writer => $class) {
  59. $this->writerParts[$writer] = new $class($this);
  60. }
  61. }
  62. /**
  63. * Get writer part
  64. *
  65. * @param string $pPartName Writer part name
  66. * @return PHPExcel_Writer_Excel2007_WriterPart
  67. */
  68. public function getWriterPart($pPartName = '')
  69. {
  70. if ($pPartName != '' && isset($this->writerParts[strtolower($pPartName)])) {
  71. return $this->writerParts[strtolower($pPartName)];
  72. } else {
  73. return null;
  74. }
  75. }
  76. /**
  77. * Save PHPExcel to file
  78. *
  79. * @param string $pFilename
  80. * @throws PHPExcel_Writer_Exception
  81. */
  82. public function save($pFilename = null)
  83. {
  84. if (!$this->spreadSheet) {
  85. throw new PHPExcel_Writer_Exception('PHPExcel object unassigned.');
  86. }
  87. // garbage collect
  88. $this->spreadSheet->garbageCollect();
  89. // If $pFilename is php://output or php://stdout, make it a temporary file...
  90. $originalFilename = $pFilename;
  91. if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
  92. $pFilename = @tempnam(PHPExcel_Shared_File::sys_get_temp_dir(), 'phpxltmp');
  93. if ($pFilename == '') {
  94. $pFilename = $originalFilename;
  95. }
  96. }
  97. $objZip = $this->createZip($pFilename);
  98. $objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('meta_inf')->writeManifest());
  99. $objZip->addFromString('Thumbnails/thumbnail.png', $this->getWriterPart('thumbnails')->writeThumbnail());
  100. $objZip->addFromString('content.xml', $this->getWriterPart('content')->write());
  101. $objZip->addFromString('meta.xml', $this->getWriterPart('meta')->write());
  102. $objZip->addFromString('mimetype', $this->getWriterPart('mimetype')->write());
  103. $objZip->addFromString('settings.xml', $this->getWriterPart('settings')->write());
  104. $objZip->addFromString('styles.xml', $this->getWriterPart('styles')->write());
  105. // Close file
  106. if ($objZip->close() === false) {
  107. throw new PHPExcel_Writer_Exception("Could not close zip file $pFilename.");
  108. }
  109. // If a temporary file was used, copy it to the correct file stream
  110. if ($originalFilename != $pFilename) {
  111. if (copy($pFilename, $originalFilename) === false) {
  112. throw new PHPExcel_Writer_Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
  113. }
  114. @unlink($pFilename);
  115. }
  116. }
  117. /**
  118. * Create zip object
  119. *
  120. * @param string $pFilename
  121. * @throws PHPExcel_Writer_Exception
  122. * @return ZipArchive
  123. */
  124. private function createZip($pFilename)
  125. {
  126. // Create new ZIP file and open it for writing
  127. $zipClass = PHPExcel_Settings::getZipClass();
  128. $objZip = new $zipClass();
  129. // Retrieve OVERWRITE and CREATE constants from the instantiated zip class
  130. // This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP
  131. $ro = new ReflectionObject($objZip);
  132. $zipOverWrite = $ro->getConstant('OVERWRITE');
  133. $zipCreate = $ro->getConstant('CREATE');
  134. if (file_exists($pFilename)) {
  135. unlink($pFilename);
  136. }
  137. // Try opening the ZIP file
  138. if ($objZip->open($pFilename, $zipOverWrite) !== true) {
  139. if ($objZip->open($pFilename, $zipCreate) !== true) {
  140. throw new PHPExcel_Writer_Exception("Could not open $pFilename for writing.");
  141. }
  142. }
  143. return $objZip;
  144. }
  145. /**
  146. * Get PHPExcel object
  147. *
  148. * @return PHPExcel
  149. * @throws PHPExcel_Writer_Exception
  150. */
  151. public function getPHPExcel()
  152. {
  153. if ($this->spreadSheet !== null) {
  154. return $this->spreadSheet;
  155. } else {
  156. throw new PHPExcel_Writer_Exception('No PHPExcel assigned.');
  157. }
  158. }
  159. /**
  160. * Set PHPExcel object
  161. *
  162. * @param PHPExcel $pPHPExcel PHPExcel object
  163. * @throws PHPExcel_Writer_Exception
  164. * @return PHPExcel_Writer_Excel2007
  165. */
  166. public function setPHPExcel(PHPExcel $pPHPExcel = null)
  167. {
  168. $this->spreadSheet = $pPHPExcel;
  169. return $this;
  170. }
  171. }