|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of PHPPresentation - A pure PHP library for reading and writing |
|
4
|
|
|
* presentations documents. |
|
5
|
|
|
* |
|
6
|
|
|
* PHPPresentation is free software distributed under the terms of the GNU Lesser |
|
7
|
|
|
* General Public License version 3 as published by the Free Software Foundation. |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please read the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. For the full list of |
|
11
|
|
|
* contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors. |
|
12
|
|
|
* |
|
13
|
|
|
* @link https://github.com/PHPOffice/PHPPresentation |
|
14
|
|
|
* @copyright 2009-2015 PHPPresentation contributors |
|
15
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007; |
|
19
|
|
|
|
|
20
|
|
|
use PhpOffice\PhpPresentation\Shape\Chart as ShapeChart; |
|
21
|
|
|
use PhpOffice\PhpPresentation\Shape\Drawing as ShapeDrawing; |
|
22
|
|
|
use PhpOffice\Common\XMLWriter; |
|
23
|
|
|
use PhpOffice\PhpPresentation\Writer\PowerPoint2007; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* \PhpOffice\PhpPresentation\Writer\PowerPoint2007\ContentTypes |
|
27
|
|
|
*/ |
|
28
|
|
|
class ContentTypes extends AbstractDecoratorWriter |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @return \PhpOffice\Common\Adapter\Zip\ZipInterface |
|
32
|
|
|
* @throws \Exception |
|
33
|
|
|
*/ |
|
34
|
|
|
public function render() |
|
35
|
98 |
|
{ |
|
36
|
|
|
// Create XML writer |
|
37
|
98 |
|
$objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); |
|
38
|
|
|
|
|
39
|
|
|
// XML header |
|
40
|
98 |
|
$objWriter->startDocument('1.0', 'UTF-8', 'yes'); |
|
41
|
|
|
|
|
42
|
|
|
// Types |
|
43
|
98 |
|
$objWriter->startElement('Types'); |
|
44
|
|
|
$objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types'); |
|
45
|
|
|
|
|
46
|
98 |
|
// Rels |
|
47
|
98 |
|
$this->writeDefaultContentType($objWriter, 'rels', 'application/vnd.openxmlformats-package.relationships+xml'); |
|
48
|
|
|
|
|
49
|
|
|
// XML |
|
50
|
98 |
|
$this->writeDefaultContentType($objWriter, 'xml', 'application/xml'); |
|
51
|
|
|
|
|
52
|
|
|
// Presentation |
|
53
|
98 |
|
$this->writeOverrideContentType($objWriter, '/ppt/presentation.xml', 'application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml'); |
|
54
|
|
|
|
|
55
|
|
|
// PptProps |
|
56
|
98 |
|
$this->writeOverrideContentType($objWriter, '/ppt/presProps.xml', 'application/vnd.openxmlformats-officedocument.presentationml.presProps+xml'); |
|
57
|
98 |
|
$this->writeOverrideContentType($objWriter, '/ppt/tableStyles.xml', 'application/vnd.openxmlformats-officedocument.presentationml.tableStyles+xml'); |
|
58
|
98 |
|
$this->writeOverrideContentType($objWriter, '/ppt/viewProps.xml', 'application/vnd.openxmlformats-officedocument.presentationml.viewProps+xml'); |
|
59
|
98 |
|
|
|
60
|
|
|
// DocProps |
|
61
|
|
|
$this->writeOverrideContentType($objWriter, '/docProps/app.xml', 'application/vnd.openxmlformats-officedocument.extended-properties+xml'); |
|
62
|
98 |
|
$this->writeOverrideContentType($objWriter, '/docProps/core.xml', 'application/vnd.openxmlformats-package.core-properties+xml'); |
|
63
|
|
|
$this->writeOverrideContentType($objWriter, '/docProps/custom.xml', 'application/vnd.openxmlformats-officedocument.custom-properties+xml'); |
|
64
|
|
|
|
|
65
|
98 |
|
// Slide masters |
|
66
|
98 |
|
$sldLayoutNr = 0; |
|
67
|
98 |
|
$sldLayoutId = time() + 689016272; // requires minimum value of 2 147 483 648 |
|
68
|
|
|
foreach ($this->oPresentation->getAllMasterSlides() as $idx => $oSlideMaster) { |
|
69
|
|
|
$oSlideMaster->setRelsIndex($idx + 1); |
|
70
|
98 |
|
$this->writeOverrideContentType($objWriter, '/ppt/slideMasters/slideMaster' . $oSlideMaster->getRelsIndex() . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml'); |
|
71
|
98 |
|
$this->writeOverrideContentType($objWriter, '/ppt/theme/theme' . $oSlideMaster->getRelsIndex() . '.xml', 'application/vnd.openxmlformats-officedocument.theme+xml'); |
|
72
|
98 |
|
foreach ($oSlideMaster->getAllSlideLayouts() as &$oSlideLayout) { |
|
|
|
|
|
|
73
|
|
|
$oSlideLayout->layoutNr = ++$sldLayoutNr; |
|
74
|
|
|
$oSlideLayout->layoutId = ++$sldLayoutId; |
|
75
|
98 |
|
$this->writeOverrideContentType($objWriter, '/ppt/slideLayouts/slideLayout' . $oSlideLayout->layoutNr . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml'); |
|
76
|
98 |
|
} |
|
77
|
98 |
|
} |
|
78
|
98 |
|
|
|
79
|
|
|
// Slides |
|
80
|
|
|
$slideCount = $this->oPresentation->getSlideCount(); |
|
81
|
98 |
|
for ($i = 0; $i < $slideCount; ++$i) { |
|
82
|
98 |
|
$this->writeOverrideContentType($objWriter, '/ppt/slides/slide' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.slide+xml'); |
|
83
|
98 |
|
if ($this->oPresentation->getSlide($i)->getNote()->getShapeCollection()->count() > 0) { |
|
84
|
98 |
|
$this->writeOverrideContentType($objWriter, '/ppt/notesSlides/notesSlide' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.notesSlide+xml'); |
|
85
|
98 |
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
98 |
|
// Add layoutpack content types |
|
89
|
98 |
|
/* $otherRelations = $oLayoutPack->getMasterSlideRelations(); |
|
90
|
98 |
|
foreach ($otherRelations as $otherRelation) { |
|
91
|
98 |
|
if (strpos($otherRelation['target'], 'http://') !== 0 && $otherRelation['contentType'] != '') { |
|
92
|
1 |
|
$this->writeOverrideContentType($objWriter, '/ppt/slideMasters/' . $otherRelation['target'], $otherRelation['contentType']); |
|
93
|
1 |
|
} |
|
94
|
98 |
|
} |
|
95
|
|
|
$otherRelations = $oLayoutPack->getThemeRelations(); |
|
96
|
|
|
foreach ($otherRelations as $otherRelation) { |
|
97
|
98 |
|
if (strpos($otherRelation['target'], 'http://') !== 0 && $otherRelation['contentType'] != '') { |
|
98
|
98 |
|
$this->writeOverrideContentType($objWriter, '/ppt/theme/' . $otherRelation['target'], $otherRelation['contentType']); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
$otherRelations = $oLayoutPack->getLayoutRelations(); |
|
102
|
98 |
|
foreach ($otherRelations as $otherRelation) { |
|
103
|
98 |
|
if (strpos($otherRelation['target'], 'http://') !== 0 && $otherRelation['contentType'] != '') { |
|
104
|
98 |
|
$this->writeOverrideContentType($objWriter, '/ppt/slideLayouts/' . $otherRelation['target'], $otherRelation['contentType']); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
*/ |
|
108
|
98 |
|
// Add media content-types |
|
109
|
98 |
|
$aMediaContentTypes = array(); |
|
110
|
98 |
|
|
|
111
|
|
|
// GIF, JPEG, PNG |
|
112
|
|
|
$aMediaContentTypes['gif'] = 'image/gif'; |
|
113
|
|
|
$aMediaContentTypes['jpg'] = 'image/jpeg'; |
|
114
|
98 |
|
$aMediaContentTypes['jpeg'] = 'image/jpeg'; |
|
115
|
|
|
$aMediaContentTypes['png'] = 'image/png'; |
|
116
|
|
|
foreach ($aMediaContentTypes as $key => $value) { |
|
117
|
98 |
|
$this->writeDefaultContentType($objWriter, $key, $value); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
98 |
|
// XLSX |
|
121
|
98 |
|
$this->writeDefaultContentType($objWriter, 'xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); |
|
122
|
98 |
|
|
|
123
|
98 |
|
// Other media content types |
|
124
|
98 |
|
$mediaCount = $this->getDrawingHashTable()->count(); |
|
125
|
98 |
|
for ($i = 0; $i < $mediaCount; ++$i) { |
|
126
|
98 |
|
$shapeIndex = $this->getDrawingHashTable()->getByIndex($i); |
|
127
|
|
|
if ($shapeIndex instanceof ShapeChart) { |
|
128
|
|
|
// Chart content type |
|
129
|
98 |
|
$this->writeOverrideContentType($objWriter, '/ppt/charts/chart' . $shapeIndex->getImageIndex() . '.xml', 'application/vnd.openxmlformats-officedocument.drawingml.chart+xml'); |
|
130
|
|
|
} else { |
|
131
|
|
|
$extension = strtolower($shapeIndex->getExtension()); |
|
132
|
98 |
|
$mimeType = $shapeIndex->getMimeType(); |
|
133
|
98 |
|
|
|
134
|
35 |
|
if (!isset($aMediaContentTypes[$extension])) { |
|
135
|
35 |
|
$aMediaContentTypes[$extension] = $mimeType; |
|
136
|
|
|
|
|
137
|
35 |
|
$this->writeDefaultContentType($objWriter, $extension, $mimeType); |
|
138
|
35 |
|
} |
|
139
|
|
|
} |
|
140
|
25 |
|
} |
|
141
|
25 |
|
|
|
142
|
10 |
|
$objWriter->endElement(); |
|
143
|
10 |
|
|
|
144
|
|
|
$this->oZip->addFromString('[Content_Types].xml', $objWriter->getData()); |
|
145
|
8 |
|
|
|
146
|
1 |
|
return $this->oZip; |
|
147
|
|
|
} |
|
148
|
1 |
|
|
|
149
|
1 |
|
/** |
|
150
|
|
|
* Write Default content type |
|
151
|
33 |
|
* |
|
152
|
|
|
* @param \PhpOffice\Common\XMLWriter $objWriter XML Writer |
|
153
|
96 |
|
* @param string $pPartname Part name |
|
154
|
|
|
* @param string $pContentType Content type |
|
155
|
96 |
|
* @throws \Exception |
|
156
|
|
|
*/ |
|
157
|
96 |
|
private function writeDefaultContentType(XMLWriter $objWriter, $pPartname = '', $pContentType = '') |
|
158
|
|
|
{ |
|
159
|
|
|
if ($pPartname == '' || $pContentType == '') { |
|
160
|
|
|
throw new \Exception("Invalid parameters passed."); |
|
161
|
|
|
} |
|
162
|
|
|
// Write content type |
|
163
|
|
|
$objWriter->startElement('Default'); |
|
164
|
|
|
$objWriter->writeAttribute('Extension', $pPartname); |
|
165
|
|
|
$objWriter->writeAttribute('ContentType', $pContentType); |
|
166
|
|
|
$objWriter->endElement(); |
|
167
|
|
|
} |
|
168
|
98 |
|
|
|
169
|
|
|
/** |
|
170
|
98 |
|
* Write Override content type |
|
171
|
|
|
* |
|
172
|
|
|
* @param \PhpOffice\Common\XMLWriter $objWriter XML Writer |
|
173
|
|
|
* @param string $pPartname Part name |
|
174
|
98 |
|
* @param string $pContentType Content type |
|
175
|
98 |
|
* @throws \Exception |
|
176
|
98 |
|
*/ |
|
177
|
98 |
|
private function writeOverrideContentType(XMLWriter $objWriter, $pPartname = '', $pContentType = '') |
|
178
|
98 |
|
{ |
|
179
|
|
|
if ($pPartname == '' || $pContentType == '') { |
|
180
|
|
|
throw new \Exception("Invalid parameters passed."); |
|
181
|
|
|
} |
|
182
|
|
|
// Write content type |
|
183
|
|
|
$objWriter->startElement('Override'); |
|
184
|
|
|
$objWriter->writeAttribute('PartName', $pPartname); |
|
185
|
|
|
$objWriter->writeAttribute('ContentType', $pContentType); |
|
186
|
|
|
$objWriter->endElement(); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
Let?s assume that you have the following
foreachstatement:$itemValueis assigned by reference. This is possible because the expression (in the example$array) can be used as a reference target.However, if we were to replace
$arraywith something different like the result of a function call as inthen assigning by reference is not possible anymore as there is no target that could be modified.
Available Fixes
1. Do not assign by reference
2. Assign to a local variable first
3. Return a reference