4ec4fe4316e92bdcbcd48febd59652456bc9eaf1.svn-base 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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_Excel2007
  23. * @copyright Copyright (c) 2006 - 2014 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. /**
  28. * PHPExcel_Writer_Excel2007_ContentTypes
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_Excel2007
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Writer_Excel2007_ContentTypes extends PHPExcel_Writer_Excel2007_WriterPart
  35. {
  36. /**
  37. * Write content types to XML format
  38. *
  39. * @param PHPExcel $pPHPExcel
  40. * @param boolean $includeCharts Flag indicating if we should include drawing details for charts
  41. * @return string XML Output
  42. * @throws PHPExcel_Writer_Exception
  43. */
  44. public function writeContentTypes(PHPExcel $pPHPExcel = null, $includeCharts = FALSE)
  45. {
  46. // Create XML writer
  47. $objWriter = null;
  48. if ($this->getParentWriter()->getUseDiskCaching()) {
  49. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  50. } else {
  51. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  52. }
  53. // XML header
  54. $objWriter->startDocument('1.0','UTF-8','yes');
  55. // Types
  56. $objWriter->startElement('Types');
  57. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types');
  58. // Theme
  59. $this->_writeOverrideContentType(
  60. $objWriter, '/xl/theme/theme1.xml', 'application/vnd.openxmlformats-officedocument.theme+xml'
  61. );
  62. // Styles
  63. $this->_writeOverrideContentType(
  64. $objWriter, '/xl/styles.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml'
  65. );
  66. // Rels
  67. $this->_writeDefaultContentType(
  68. $objWriter, 'rels', 'application/vnd.openxmlformats-package.relationships+xml'
  69. );
  70. // XML
  71. $this->_writeDefaultContentType(
  72. $objWriter, 'xml', 'application/xml'
  73. );
  74. // VML
  75. $this->_writeDefaultContentType(
  76. $objWriter, 'vml', 'application/vnd.openxmlformats-officedocument.vmlDrawing'
  77. );
  78. // Workbook
  79. if($pPHPExcel->hasMacros()){ //Macros in workbook ?
  80. // Yes : not standard content but "macroEnabled"
  81. $this->_writeOverrideContentType(
  82. $objWriter, '/xl/workbook.xml', 'application/vnd.ms-excel.sheet.macroEnabled.main+xml'
  83. );
  84. //... and define a new type for the VBA project
  85. $this->_writeDefaultContentType(
  86. $objWriter, 'bin', 'application/vnd.ms-office.vbaProject'
  87. );
  88. if($pPHPExcel->hasMacrosCertificate()){// signed macros ?
  89. // Yes : add needed information
  90. $this->_writeOverrideContentType(
  91. $objWriter, '/xl/vbaProjectSignature.bin', 'application/vnd.ms-office.vbaProjectSignature'
  92. );
  93. }
  94. }else{// no macros in workbook, so standard type
  95. $this->_writeOverrideContentType(
  96. $objWriter, '/xl/workbook.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml'
  97. );
  98. }
  99. // DocProps
  100. $this->_writeOverrideContentType(
  101. $objWriter, '/docProps/app.xml', 'application/vnd.openxmlformats-officedocument.extended-properties+xml'
  102. );
  103. $this->_writeOverrideContentType(
  104. $objWriter, '/docProps/core.xml', 'application/vnd.openxmlformats-package.core-properties+xml'
  105. );
  106. $customPropertyList = $pPHPExcel->getProperties()->getCustomProperties();
  107. if (!empty($customPropertyList)) {
  108. $this->_writeOverrideContentType(
  109. $objWriter, '/docProps/custom.xml', 'application/vnd.openxmlformats-officedocument.custom-properties+xml'
  110. );
  111. }
  112. // Worksheets
  113. $sheetCount = $pPHPExcel->getSheetCount();
  114. for ($i = 0; $i < $sheetCount; ++$i) {
  115. $this->_writeOverrideContentType(
  116. $objWriter, '/xl/worksheets/sheet' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml'
  117. );
  118. }
  119. // Shared strings
  120. $this->_writeOverrideContentType(
  121. $objWriter, '/xl/sharedStrings.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml'
  122. );
  123. // Add worksheet relationship content types
  124. $chart = 1;
  125. for ($i = 0; $i < $sheetCount; ++$i) {
  126. $drawings = $pPHPExcel->getSheet($i)->getDrawingCollection();
  127. $drawingCount = count($drawings);
  128. $chartCount = ($includeCharts) ? $pPHPExcel->getSheet($i)->getChartCount() : 0;
  129. // We need a drawing relationship for the worksheet if we have either drawings or charts
  130. if (($drawingCount > 0) || ($chartCount > 0)) {
  131. $this->_writeOverrideContentType(
  132. $objWriter, '/xl/drawings/drawing' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.drawing+xml'
  133. );
  134. }
  135. // If we have charts, then we need a chart relationship for every individual chart
  136. if ($chartCount > 0) {
  137. for ($c = 0; $c < $chartCount; ++$c) {
  138. $this->_writeOverrideContentType(
  139. $objWriter, '/xl/charts/chart' . $chart++ . '.xml', 'application/vnd.openxmlformats-officedocument.drawingml.chart+xml'
  140. );
  141. }
  142. }
  143. }
  144. // Comments
  145. for ($i = 0; $i < $sheetCount; ++$i) {
  146. if (count($pPHPExcel->getSheet($i)->getComments()) > 0) {
  147. $this->_writeOverrideContentType(
  148. $objWriter, '/xl/comments' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml'
  149. );
  150. }
  151. }
  152. // Add media content-types
  153. $aMediaContentTypes = array();
  154. $mediaCount = $this->getParentWriter()->getDrawingHashTable()->count();
  155. for ($i = 0; $i < $mediaCount; ++$i) {
  156. $extension = '';
  157. $mimeType = '';
  158. if ($this->getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_Drawing) {
  159. $extension = strtolower($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getExtension());
  160. $mimeType = $this->_getImageMimeType( $this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getPath() );
  161. } else if ($this->getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_MemoryDrawing) {
  162. $extension = strtolower($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getMimeType());
  163. $extension = explode('/', $extension);
  164. $extension = $extension[1];
  165. $mimeType = $this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getMimeType();
  166. }
  167. if (!isset( $aMediaContentTypes[$extension]) ) {
  168. $aMediaContentTypes[$extension] = $mimeType;
  169. $this->_writeDefaultContentType(
  170. $objWriter, $extension, $mimeType
  171. );
  172. }
  173. }
  174. if($pPHPExcel->hasRibbonBinObjects()){//Some additional objects in the ribbon ?
  175. //we need to write "Extension" but not already write for media content
  176. $tabRibbonTypes=array_diff($pPHPExcel->getRibbonBinObjects('types'), array_keys($aMediaContentTypes));
  177. foreach($tabRibbonTypes as $aRibbonType){
  178. $mimeType='image/.'.$aRibbonType;//we wrote $mimeType like customUI Editor
  179. $this->_writeDefaultContentType(
  180. $objWriter, $aRibbonType, $mimeType
  181. );
  182. }
  183. }
  184. $sheetCount = $pPHPExcel->getSheetCount();
  185. for ($i = 0; $i < $sheetCount; ++$i) {
  186. if (count($pPHPExcel->getSheet()->getHeaderFooter()->getImages()) > 0) {
  187. foreach ($pPHPExcel->getSheet()->getHeaderFooter()->getImages() as $image) {
  188. if (!isset( $aMediaContentTypes[strtolower($image->getExtension())]) ) {
  189. $aMediaContentTypes[strtolower($image->getExtension())] = $this->_getImageMimeType( $image->getPath() );
  190. $this->_writeDefaultContentType(
  191. $objWriter, strtolower($image->getExtension()), $aMediaContentTypes[strtolower($image->getExtension())]
  192. );
  193. }
  194. }
  195. }
  196. }
  197. $objWriter->endElement();
  198. // Return
  199. return $objWriter->getData();
  200. }
  201. /**
  202. * Get image mime type
  203. *
  204. * @param string $pFile Filename
  205. * @return string Mime Type
  206. * @throws PHPExcel_Writer_Exception
  207. */
  208. private function _getImageMimeType($pFile = '')
  209. {
  210. if (PHPExcel_Shared_File::file_exists($pFile)) {
  211. $image = getimagesize($pFile);
  212. return image_type_to_mime_type($image[2]);
  213. } else {
  214. throw new PHPExcel_Writer_Exception("File $pFile does not exist");
  215. }
  216. }
  217. /**
  218. * Write Default content type
  219. *
  220. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  221. * @param string $pPartname Part name
  222. * @param string $pContentType Content type
  223. * @throws PHPExcel_Writer_Exception
  224. */
  225. private function _writeDefaultContentType(PHPExcel_Shared_XMLWriter $objWriter = null, $pPartname = '', $pContentType = '')
  226. {
  227. if ($pPartname != '' && $pContentType != '') {
  228. // Write content type
  229. $objWriter->startElement('Default');
  230. $objWriter->writeAttribute('Extension', $pPartname);
  231. $objWriter->writeAttribute('ContentType', $pContentType);
  232. $objWriter->endElement();
  233. } else {
  234. throw new PHPExcel_Writer_Exception("Invalid parameters passed.");
  235. }
  236. }
  237. /**
  238. * Write Override content type
  239. *
  240. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  241. * @param string $pPartname Part name
  242. * @param string $pContentType Content type
  243. * @throws PHPExcel_Writer_Exception
  244. */
  245. private function _writeOverrideContentType(PHPExcel_Shared_XMLWriter $objWriter = null, $pPartname = '', $pContentType = '')
  246. {
  247. if ($pPartname != '' && $pContentType != '') {
  248. // Write content type
  249. $objWriter->startElement('Override');
  250. $objWriter->writeAttribute('PartName', $pPartname);
  251. $objWriter->writeAttribute('ContentType', $pContentType);
  252. $objWriter->endElement();
  253. } else {
  254. throw new PHPExcel_Writer_Exception("Invalid parameters passed.");
  255. }
  256. }
  257. }