Complex classes like PowerPoint2007 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PowerPoint2007, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class PowerPoint2007 extends AbstractReader implements ReaderInterface |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * Output Object |
||
| 37 | * @var PhpPresentation |
||
| 38 | */ |
||
| 39 | protected $oPhpPresentation; |
||
| 40 | /** |
||
| 41 | * Output Object |
||
| 42 | * @var \ZipArchive |
||
| 43 | */ |
||
| 44 | protected $oZip; |
||
| 45 | /** |
||
| 46 | * @var string[] |
||
| 47 | */ |
||
| 48 | protected $arrayRels = array(); |
||
| 49 | /* |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $filename; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Can the current \PhpOffice\PhpPresentation\Reader\ReaderInterface read the file? |
||
| 56 | * |
||
| 57 | * @param string $pFilename |
||
| 58 | * @throws \Exception |
||
| 59 | * @return boolean |
||
| 60 | */ |
||
| 61 | 2 | public function canRead($pFilename) |
|
| 62 | { |
||
| 63 | 2 | return $this->fileSupportsUnserializePhpPresentation($pFilename); |
|
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Does a file support UnserializePhpPresentation ? |
||
| 68 | * |
||
| 69 | * @param string $pFilename |
||
| 70 | * @throws \Exception |
||
| 71 | * @return boolean |
||
| 72 | */ |
||
| 73 | 8 | public function fileSupportsUnserializePhpPresentation($pFilename = '') |
|
| 74 | { |
||
| 75 | // Check if file exists |
||
| 76 | 8 | if (!file_exists($pFilename)) { |
|
| 77 | 2 | throw new \Exception("Could not open " . $pFilename . " for reading! File does not exist."); |
|
| 78 | } |
||
| 79 | |||
| 80 | 6 | $oZip = new ZipArchive(); |
|
| 81 | // Is it a zip ? |
||
| 82 | 6 | if ($oZip->open($pFilename) === true) { |
|
| 83 | // Is it an OpenXML Document ? |
||
| 84 | // Is it a Presentation ? |
||
| 85 | 3 | if (is_array($oZip->statName('[Content_Types].xml')) && is_array($oZip->statName('ppt/presentation.xml'))) { |
|
| 86 | 3 | return true; |
|
| 87 | } |
||
| 88 | } else { |
||
| 89 | 3 | $oOLE = new OLERead(); |
|
| 90 | try { |
||
| 91 | 3 | $oOLE->read($pFilename); |
|
| 92 | 2 | return true; |
|
| 93 | 1 | } catch (Exception $e) { |
|
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | return false; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Loads PhpPresentation Serialized file |
||
| 102 | * |
||
| 103 | * @param string $pFilename |
||
| 104 | * @return \PhpOffice\PhpPresentation\PhpPresentation |
||
| 105 | * @throws \Exception |
||
| 106 | */ |
||
| 107 | 5 | public function load($pFilename) |
|
| 108 | { |
||
| 109 | // Unserialize... First make sure the file supports it! |
||
| 110 | 5 | if (!$this->fileSupportsUnserializePhpPresentation($pFilename)) { |
|
| 111 | throw new \Exception("Invalid file format for PhpOffice\PhpPresentation\Reader\PowerPoint2007: " . $pFilename . "."); |
||
| 112 | } |
||
| 113 | |||
| 114 | 4 | return $this->loadFile($pFilename); |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Load PhpPresentation Serialized file |
||
| 119 | * |
||
| 120 | * @param string $pFilename |
||
| 121 | * @return \PhpOffice\PhpPresentation\PhpPresentation |
||
| 122 | */ |
||
| 123 | 4 | protected function loadFile($pFilename) |
|
| 124 | { |
||
| 125 | 4 | $this->oPhpPresentation = new PhpPresentation(); |
|
| 126 | 4 | $this->oPhpPresentation->removeSlideByIndex(); |
|
| 127 | 4 | $this->filename = $pFilename; |
|
| 128 | |||
| 129 | 4 | $this->oZip = new ZipArchive(); |
|
| 130 | |||
| 131 | 4 | if ($this->oZip->open($this->filename) == ZipArchive::ER_NOZIP) { |
|
| 132 | 4 | $this->loadEncryptedFile(); |
|
| 133 | return $this->oPhpPresentation; |
||
| 134 | } |
||
| 135 | |||
| 136 | $docPropsCore = $this->oZip->getFromName('docProps/core.xml'); |
||
| 137 | if ($docPropsCore !== false) { |
||
| 138 | $this->loadDocumentProperties($docPropsCore); |
||
| 139 | } |
||
| 140 | |||
| 141 | $docPropsCustom = $this->oZip->getFromName('docProps/custom.xml'); |
||
| 142 | if ($docPropsCustom !== false) { |
||
| 143 | $this->loadCustomProperties($docPropsCustom); |
||
| 144 | } |
||
| 145 | |||
| 146 | $pptViewProps = $this->oZip->getFromName('ppt/viewProps.xml'); |
||
| 147 | if ($pptViewProps !== false) { |
||
| 148 | $this->loadViewProperties($pptViewProps); |
||
| 149 | } |
||
| 150 | |||
| 151 | $pptPresentation = $this->oZip->getFromName('ppt/presentation.xml'); |
||
| 152 | if ($pptPresentation !== false) { |
||
| 153 | $this->loadSlides($pptPresentation); |
||
| 154 | } |
||
| 155 | |||
| 156 | return $this->oPhpPresentation; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Read Document Properties |
||
| 161 | * @param string $sPart |
||
| 162 | */ |
||
| 163 | protected function loadDocumentProperties($sPart) |
||
| 164 | { |
||
| 165 | $xmlReader = new XMLReader(); |
||
| 166 | if ($xmlReader->getDomFromString($sPart)) { |
||
| 167 | $arrayProperties = array( |
||
| 168 | '/cp:coreProperties/dc:creator' => 'setCreator', |
||
| 169 | '/cp:coreProperties/cp:lastModifiedBy' => 'setLastModifiedBy', |
||
| 170 | '/cp:coreProperties/dc:title' => 'setTitle', |
||
| 171 | '/cp:coreProperties/dc:description' => 'setDescription', |
||
| 172 | '/cp:coreProperties/dc:subject' => 'setSubject', |
||
| 173 | '/cp:coreProperties/cp:keywords' => 'setKeywords', |
||
| 174 | '/cp:coreProperties/cp:category' => 'setCategory', |
||
| 175 | '/cp:coreProperties/dcterms:created' => 'setCreated', |
||
| 176 | '/cp:coreProperties/dcterms:modified' => 'setModified', |
||
| 177 | ); |
||
| 178 | $oProperties = $this->oPhpPresentation->getProperties(); |
||
| 179 | foreach ($arrayProperties as $path => $property) { |
||
| 180 | if (is_object($oElement = $xmlReader->getElement($path))) { |
||
| 181 | if ($oElement->hasAttribute('xsi:type') && $oElement->getAttribute('xsi:type') == 'dcterms:W3CDTF') { |
||
| 182 | $oDateTime = new \DateTime(); |
||
| 183 | $oDateTime->createFromFormat(\DateTime::W3C, $oElement->nodeValue); |
||
| 184 | $oProperties->{$property}($oDateTime->getTimestamp()); |
||
| 185 | } else { |
||
| 186 | $oProperties->{$property}($oElement->nodeValue); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Read Custom Properties |
||
| 195 | * @param string $sPart |
||
| 196 | */ |
||
| 197 | protected function loadCustomProperties($sPart) |
||
| 198 | { |
||
| 199 | $xmlReader = new XMLReader(); |
||
| 200 | $sPart = str_replace(' xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"', '', |
||
| 201 | $sPart); |
||
| 202 | if ($xmlReader->getDomFromString($sPart)) { |
||
| 203 | $pathMarkAsFinal = '/Properties/property[@pid="2"][@fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"][@name="_MarkAsFinal"]/vt:bool'; |
||
| 204 | if (is_object($oElement = $xmlReader->getElement($pathMarkAsFinal))) { |
||
| 205 | if ($oElement->nodeValue == 'true') { |
||
| 206 | $this->oPhpPresentation->markAsFinal(true); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Read View Properties |
||
| 214 | * @param string $sPart |
||
| 215 | */ |
||
| 216 | protected function loadViewProperties($sPart) |
||
| 217 | { |
||
| 218 | $xmlReader = new XMLReader(); |
||
| 219 | if ($xmlReader->getDomFromString($sPart)) { |
||
| 220 | $pathZoom = '/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sx'; |
||
| 221 | if (is_object($oElement = $xmlReader->getElement($pathZoom))) { |
||
| 222 | if ($oElement->hasAttribute('d') && $oElement->hasAttribute('n')) { |
||
| 223 | $this->oPhpPresentation->setZoom($oElement->getAttribute('n') / $oElement->getAttribute('d')); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Extract all slides |
||
| 231 | */ |
||
| 232 | protected function loadSlides($sPart) |
||
| 233 | { |
||
| 234 | $xmlReader = new XMLReader(); |
||
| 235 | if ($xmlReader->getDomFromString($sPart)) { |
||
| 236 | $fileRels = 'ppt/_rels/presentation.xml.rels'; |
||
| 237 | $this->loadRels($fileRels); |
||
| 238 | foreach ($xmlReader->getElements('/p:presentation/p:sldIdLst/p:sldId') as $oElement) { |
||
| 239 | $rId = $oElement->getAttribute('r:id'); |
||
| 240 | $pathSlide = isset($this->arrayRels[$fileRels][$rId]) ? $this->arrayRels[$fileRels][$rId]['Target'] : ''; |
||
| 241 | if (!empty($pathSlide)) { |
||
| 242 | $pptSlide = $this->oZip->getFromName('ppt/' . $pathSlide); |
||
| 243 | if ($pptSlide !== false) { |
||
| 244 | $this->loadRels('ppt/slides/_rels/' . basename($pathSlide) . '.rels'); |
||
| 245 | $this->loadSlide($pptSlide, basename($pathSlide)); |
||
| 246 | } |
||
| 247 | } |
||
| 248 | } |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Extract data from slide |
||
| 254 | * @param string $sPart |
||
| 255 | * @param string $baseFile |
||
| 256 | */ |
||
| 257 | protected function loadSlide($sPart, $baseFile) |
||
| 258 | { |
||
| 259 | $xmlReader = new XMLReader(); |
||
| 260 | if ($xmlReader->getDomFromString($sPart)) { |
||
| 261 | // Core |
||
| 262 | $this->oPhpPresentation->createSlide(); |
||
| 263 | $this->oPhpPresentation->setActiveSlideIndex($this->oPhpPresentation->getSlideCount() - 1); |
||
| 264 | |||
| 265 | // Background |
||
| 266 | $oElement = $xmlReader->getElement('/p:sld/p:cSld/p:bg/p:bgPr'); |
||
| 267 | if ($oElement) { |
||
| 268 | $oElementColor = $xmlReader->getElement('a:solidFill/a:srgbClr', $oElement); |
||
| 269 | if ($oElementColor) { |
||
| 270 | // Color |
||
| 271 | $oColor = new Color(); |
||
| 272 | $oColor->setRGB($oElementColor->hasAttribute('val') ? $oElementColor->getAttribute('val') : null); |
||
| 273 | // Background |
||
| 274 | $oBackground = new \PhpOffice\PhpPresentation\Slide\Background\Color(); |
||
| 275 | $oBackground->setColor($oColor); |
||
| 276 | // Slide Background |
||
| 277 | $oSlide = $this->oPhpPresentation->getActiveSlide(); |
||
| 278 | $oSlide->setBackground($oBackground); |
||
| 279 | } |
||
| 280 | $oElementImage = $xmlReader->getElement('a:blipFill/a:blip', $oElement); |
||
| 281 | if ($oElementImage) { |
||
| 282 | $relImg = $this->arrayRels['ppt/slides/_rels/' . $baseFile . '.rels'][$oElementImage->getAttribute('r:embed')]; |
||
| 283 | if (is_array($relImg)) { |
||
| 284 | // File |
||
| 285 | $pathImage = 'ppt/slides/' . $relImg['Target']; |
||
| 286 | $pathImage = explode('/', $pathImage); |
||
| 287 | foreach ($pathImage as $key => $partPath) { |
||
| 288 | if ($partPath == '..') { |
||
| 289 | unset($pathImage[$key - 1]); |
||
| 290 | unset($pathImage[$key]); |
||
| 291 | } |
||
| 292 | } |
||
| 293 | $pathImage = implode('/', $pathImage); |
||
| 294 | $contentImg = $this->oZip->getFromName($pathImage); |
||
| 295 | |||
| 296 | $tmpBkgImg = tempnam(sys_get_temp_dir(), 'PhpPresentationReaderPpt2007Bkg'); |
||
| 297 | file_put_contents($tmpBkgImg, $contentImg); |
||
| 298 | // Background |
||
| 299 | $oBackground = new \PhpOffice\PhpPresentation\Slide\Background\Image(); |
||
| 300 | $oBackground->setPath($tmpBkgImg); |
||
| 301 | // Slide Background |
||
| 302 | $oSlide = $this->oPhpPresentation->getActiveSlide(); |
||
| 303 | $oSlide->setBackground($oBackground); |
||
| 304 | } |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | // Shapes |
||
| 309 | foreach ($xmlReader->getElements('/p:sld/p:cSld/p:spTree/*') as $oNode) { |
||
| 310 | switch ($oNode->tagName) { |
||
| 311 | case 'p:pic': |
||
| 312 | $this->loadShapeDrawing($xmlReader, $oNode, $baseFile); |
||
| 313 | break; |
||
| 314 | case 'p:sp': |
||
| 315 | $this->loadShapeRichText($xmlReader, $oNode, $baseFile); |
||
| 316 | break; |
||
| 317 | default: |
||
| 318 | //var_export($oNode->tagName); |
||
| 319 | } |
||
| 320 | } |
||
| 321 | // Layout |
||
| 322 | $oLayoutPack = new TemplateBased($this->filename); |
||
| 323 | $oSlide = $this->oPhpPresentation->getActiveSlide(); |
||
| 324 | foreach ($this->arrayRels['ppt/slides/_rels/' . $baseFile . '.rels'] as $valueRel) { |
||
| 325 | if ($valueRel['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout') { |
||
| 326 | $layoutId = $valueRel['Target']; |
||
| 327 | $layoutId = str_replace('../slideLayouts/slideLayout', '', $layoutId); |
||
| 328 | $layoutId = str_replace('.xml', '', $layoutId); |
||
| 329 | $layoutName = $oLayoutPack->findLayoutName((int)$layoutId, $oSlide->getSlideMasterId()); |
||
| 330 | $oSlide->setSlideLayout($layoutName); |
||
| 331 | break; |
||
| 332 | } |
||
| 333 | } |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * |
||
| 339 | * @param XMLReader $document |
||
| 340 | * @param \DOMElement $node |
||
| 341 | * @param string $baseFile |
||
| 342 | */ |
||
| 343 | protected function loadShapeDrawing(XMLReader $document, \DOMElement $node, $baseFile) |
||
| 344 | { |
||
| 345 | // Core |
||
| 346 | $oShape = new Gd(); |
||
| 347 | $oShape->getShadow()->setVisible(false); |
||
| 348 | // Variables |
||
| 349 | $fileRels = 'ppt/slides/_rels/' . $baseFile . '.rels'; |
||
| 350 | |||
| 351 | $oElement = $document->getElement('p:nvPicPr/p:cNvPr', $node); |
||
| 352 | if ($oElement) { |
||
| 353 | $oShape->setName($oElement->hasAttribute('name') ? $oElement->getAttribute('name') : ''); |
||
| 354 | $oShape->setDescription($oElement->hasAttribute('descr') ? $oElement->getAttribute('descr') : ''); |
||
| 355 | } |
||
| 356 | |||
| 357 | $oElement = $document->getElement('p:blipFill/a:blip', $node); |
||
| 358 | if ($oElement) { |
||
| 359 | if ($oElement->hasAttribute('r:embed') && isset($this->arrayRels[$fileRels][$oElement->getAttribute('r:embed')]['Target'])) { |
||
| 360 | $pathImage = 'ppt/slides/' . $this->arrayRels[$fileRels][$oElement->getAttribute('r:embed')]['Target']; |
||
| 361 | $pathImage = explode('/', $pathImage); |
||
| 362 | foreach ($pathImage as $key => $partPath) { |
||
| 363 | if ($partPath == '..') { |
||
| 364 | unset($pathImage[$key - 1]); |
||
| 365 | unset($pathImage[$key]); |
||
| 366 | } |
||
| 367 | } |
||
| 368 | $pathImage = implode('/', $pathImage); |
||
| 369 | $imageFile = $this->oZip->getFromName($pathImage); |
||
| 370 | if (!empty($imageFile)) { |
||
| 371 | $oShape->setImageResource(imagecreatefromstring($imageFile)); |
||
| 372 | } |
||
| 373 | } |
||
| 374 | } |
||
| 375 | |||
| 376 | $oElement = $document->getElement('p:spPr/a:xfrm', $node); |
||
| 377 | if ($oElement) { |
||
| 378 | if ($oElement->hasAttribute('rot')) { |
||
| 379 | $oShape->setRotation(CommonDrawing::angleToDegrees($oElement->getAttribute('rot'))); |
||
| 380 | } |
||
| 381 | } |
||
| 382 | |||
| 383 | $oElement = $document->getElement('p:spPr/a:xfrm/a:off', $node); |
||
| 384 | if ($oElement) { |
||
| 385 | if ($oElement->hasAttribute('x')) { |
||
| 386 | $oShape->setOffsetX(CommonDrawing::emuToPixels($oElement->getAttribute('x'))); |
||
| 387 | } |
||
| 388 | if ($oElement->hasAttribute('y')) { |
||
| 389 | $oShape->setOffsetY(CommonDrawing::emuToPixels($oElement->getAttribute('y'))); |
||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | $oElement = $document->getElement('p:spPr/a:xfrm/a:ext', $node); |
||
| 394 | if ($oElement) { |
||
| 395 | if ($oElement->hasAttribute('cx')) { |
||
| 396 | $oShape->setWidth(CommonDrawing::emuToPixels($oElement->getAttribute('cx'))); |
||
| 397 | } |
||
| 398 | if ($oElement->hasAttribute('cy')) { |
||
| 399 | $oShape->setHeight(CommonDrawing::emuToPixels($oElement->getAttribute('cy'))); |
||
| 400 | } |
||
| 401 | } |
||
| 402 | |||
| 403 | $oElement = $document->getElement('p:spPr/a:effectLst', $node); |
||
| 404 | if ($oElement) { |
||
| 405 | $oShape->getShadow()->setVisible(true); |
||
| 406 | |||
| 407 | $oSubElement = $document->getElement('a:outerShdw', $oElement); |
||
| 408 | if ($oSubElement) { |
||
| 409 | if ($oSubElement->hasAttribute('blurRad')) { |
||
| 410 | $oShape->getShadow()->setBlurRadius(CommonDrawing::emuToPixels($oSubElement->getAttribute('blurRad'))); |
||
| 411 | } |
||
| 412 | if ($oSubElement->hasAttribute('dist')) { |
||
| 413 | $oShape->getShadow()->setDistance(CommonDrawing::emuToPixels($oSubElement->getAttribute('dist'))); |
||
| 414 | } |
||
| 415 | if ($oSubElement->hasAttribute('dir')) { |
||
| 416 | $oShape->getShadow()->setDirection(CommonDrawing::angleToDegrees($oSubElement->getAttribute('dir'))); |
||
| 417 | } |
||
| 418 | if ($oSubElement->hasAttribute('algn')) { |
||
| 419 | $oShape->getShadow()->setAlignment($oSubElement->getAttribute('algn')); |
||
| 420 | } |
||
| 421 | } |
||
| 422 | |||
| 423 | $oSubElement = $document->getElement('a:outerShdw/a:srgbClr', $oElement); |
||
| 424 | if ($oSubElement) { |
||
| 425 | if ($oSubElement->hasAttribute('val')) { |
||
| 426 | $oColor = new Color(); |
||
| 427 | $oColor->setRGB($oSubElement->getAttribute('val')); |
||
| 428 | $oShape->getShadow()->setColor($oColor); |
||
| 429 | } |
||
| 430 | } |
||
| 431 | |||
| 432 | $oSubElement = $document->getElement('a:outerShdw/a:srgbClr/a:alpha', $oElement); |
||
| 433 | if ($oSubElement) { |
||
| 434 | if ($oSubElement->hasAttribute('val')) { |
||
| 435 | $oShape->getShadow()->setAlpha((int)$oSubElement->getAttribute('val') / 1000); |
||
| 436 | } |
||
| 437 | } |
||
| 438 | } |
||
| 439 | |||
| 440 | $this->oPhpPresentation->getActiveSlide()->addShape($oShape); |
||
| 441 | } |
||
| 442 | |||
| 443 | protected function loadShapeRichText(XMLReader $document, \DOMElement $node, $baseFile) |
||
| 444 | { |
||
| 445 | // Core |
||
| 446 | $oShape = $this->oPhpPresentation->getActiveSlide()->createRichTextShape(); |
||
| 447 | $oShape->setParagraphs(array()); |
||
| 448 | // Variables |
||
| 449 | $fileRels = 'ppt/slides/_rels/' . $baseFile . '.rels'; |
||
| 450 | |||
| 451 | $oElement = $document->getElement('p:spPr/a:xfrm', $node); |
||
| 452 | if ($oElement && $oElement->hasAttribute('rot')) { |
||
| 453 | $oShape->setRotation(CommonDrawing::angleToDegrees($oElement->getAttribute('rot'))); |
||
| 454 | } |
||
| 455 | |||
| 456 | $oElement = $document->getElement('p:spPr/a:xfrm/a:off', $node); |
||
| 457 | if ($oElement) { |
||
| 458 | if ($oElement->hasAttribute('x')) { |
||
| 459 | $oShape->setOffsetX(CommonDrawing::emuToPixels($oElement->getAttribute('x'))); |
||
| 460 | } |
||
| 461 | if ($oElement->hasAttribute('y')) { |
||
| 462 | $oShape->setOffsetY(CommonDrawing::emuToPixels($oElement->getAttribute('y'))); |
||
| 463 | } |
||
| 464 | } |
||
| 465 | |||
| 466 | $oElement = $document->getElement('p:spPr/a:xfrm/a:ext', $node); |
||
| 467 | if ($oElement) { |
||
| 468 | if ($oElement->hasAttribute('cx')) { |
||
| 469 | $oShape->setWidth(CommonDrawing::emuToPixels($oElement->getAttribute('cx'))); |
||
| 470 | } |
||
| 471 | if ($oElement->hasAttribute('cy')) { |
||
| 472 | $oShape->setHeight(CommonDrawing::emuToPixels($oElement->getAttribute('cy'))); |
||
| 473 | } |
||
| 474 | } |
||
| 475 | |||
| 476 | $arrayElements = $document->getElements('p:txBody/a:p', $node); |
||
| 477 | foreach ($arrayElements as $oElement) { |
||
| 478 | // Core |
||
| 479 | $oParagraph = $oShape->createParagraph(); |
||
| 480 | $oParagraph->setRichTextElements(array()); |
||
| 481 | |||
| 482 | $oSubElement = $document->getElement('a:pPr', $oElement); |
||
| 483 | if ($oSubElement) { |
||
| 484 | if ($oSubElement->hasAttribute('algn')) { |
||
| 485 | $oParagraph->getAlignment()->setHorizontal($oSubElement->getAttribute('algn')); |
||
| 486 | } |
||
| 487 | if ($oSubElement->hasAttribute('fontAlgn')) { |
||
| 488 | $oParagraph->getAlignment()->setVertical($oSubElement->getAttribute('fontAlgn')); |
||
| 489 | } |
||
| 490 | if ($oSubElement->hasAttribute('marL')) { |
||
| 491 | $oParagraph->getAlignment()->setMarginLeft(CommonDrawing::emuToPixels($oSubElement->getAttribute('marL'))); |
||
| 492 | } |
||
| 493 | if ($oSubElement->hasAttribute('marR')) { |
||
| 494 | $oParagraph->getAlignment()->setMarginRight(CommonDrawing::emuToPixels($oSubElement->getAttribute('marR'))); |
||
| 495 | } |
||
| 496 | if ($oSubElement->hasAttribute('indent')) { |
||
| 497 | $oParagraph->getAlignment()->setIndent(CommonDrawing::emuToPixels($oSubElement->getAttribute('indent'))); |
||
| 498 | } |
||
| 499 | if ($oSubElement->hasAttribute('lvl')) { |
||
| 500 | $oParagraph->getAlignment()->setLevel($oSubElement->getAttribute('lvl')); |
||
| 501 | } |
||
| 502 | |||
| 503 | $oElementBuFont = $document->getElement('a:buFont', $oSubElement); |
||
| 504 | $oParagraph->getBulletStyle()->setBulletType(Bullet::TYPE_NONE); |
||
| 505 | if ($oElementBuFont) { |
||
| 506 | if ($oElementBuFont->hasAttribute('typeface')) { |
||
| 507 | $oParagraph->getBulletStyle()->setBulletFont($oElementBuFont->getAttribute('typeface')); |
||
| 508 | } |
||
| 509 | } |
||
| 510 | $oElementBuChar = $document->getElement('a:buChar', $oSubElement); |
||
| 511 | if ($oElementBuChar) { |
||
| 512 | $oParagraph->getBulletStyle()->setBulletType(Bullet::TYPE_BULLET); |
||
| 513 | if ($oElementBuChar->hasAttribute('char')) { |
||
| 514 | $oParagraph->getBulletStyle()->setBulletChar($oElementBuChar->getAttribute('char')); |
||
| 515 | } |
||
| 516 | } |
||
| 517 | /*$oElementBuAutoNum = $document->getElement('a:buAutoNum', $oSubElement); |
||
| 518 | if ($oElementBuAutoNum) { |
||
| 519 | $oParagraph->getBulletStyle()->setBulletType(Bullet::TYPE_NUMERIC); |
||
| 520 | if ($oElementBuAutoNum->hasAttribute('type')) { |
||
| 521 | $oParagraph->getBulletStyle()->setBulletNumericStyle($oElementBuAutoNum->getAttribute('type')); |
||
| 522 | } |
||
| 523 | if ($oElementBuAutoNum->hasAttribute('startAt') && $oElementBuAutoNum->getAttribute('startAt') != 1) { |
||
| 524 | $oParagraph->getBulletStyle()->setBulletNumericStartAt($oElementBuAutoNum->getAttribute('startAt')); |
||
| 525 | } |
||
| 526 | }*/ |
||
| 527 | } |
||
| 528 | $arraySubElements = $document->getElements('(a:r|a:br)', $oElement); |
||
| 529 | foreach ($arraySubElements as $oSubElement) { |
||
| 530 | if ($oSubElement->tagName == 'a:br') { |
||
| 531 | $oParagraph->createBreak(); |
||
| 532 | } |
||
| 533 | if ($oSubElement->tagName == 'a:r') { |
||
| 534 | $oElementrPr = $document->getElement('a:rPr', $oSubElement); |
||
| 535 | if (is_object($oElementrPr)) { |
||
| 536 | $oText = $oParagraph->createTextRun(); |
||
| 537 | |||
| 538 | if ($oElementrPr->hasAttribute('b')) { |
||
| 539 | $oText->getFont()->setBold($oElementrPr->getAttribute('b') == 'true' ? true : false); |
||
| 540 | } |
||
| 541 | if ($oElementrPr->hasAttribute('i')) { |
||
| 542 | $oText->getFont()->setItalic($oElementrPr->getAttribute('i') == 'true' ? true : false); |
||
| 543 | } |
||
| 544 | if ($oElementrPr->hasAttribute('strike')) { |
||
| 545 | $oText->getFont()->setStrikethrough($oElementrPr->getAttribute('strike') == 'noStrike' ? false : true); |
||
| 546 | } |
||
| 547 | if ($oElementrPr->hasAttribute('sz')) { |
||
| 548 | $oText->getFont()->setSize((int)($oElementrPr->getAttribute('sz') / 100)); |
||
| 549 | } |
||
| 550 | if ($oElementrPr->hasAttribute('u')) { |
||
| 551 | $oText->getFont()->setUnderline($oElementrPr->getAttribute('u')); |
||
| 552 | } |
||
| 553 | // Color |
||
| 554 | $oElementSrgbClr = $document->getElement('a:solidFill/a:srgbClr', $oElementrPr); |
||
| 555 | if (is_object($oElementSrgbClr) && $oElementSrgbClr->hasAttribute('val')) { |
||
| 556 | $oColor = new Color(); |
||
| 557 | $oColor->setRGB($oElementSrgbClr->getAttribute('val')); |
||
| 558 | $oText->getFont()->setColor($oColor); |
||
| 559 | } |
||
| 560 | // Hyperlink |
||
| 561 | $oElementHlinkClick = $document->getElement('a:hlinkClick', $oElementrPr); |
||
| 562 | if (is_object($oElementHlinkClick)) { |
||
| 563 | if ($oElementHlinkClick->hasAttribute('tooltip')) { |
||
| 564 | $oText->getHyperlink()->setTooltip($oElementHlinkClick->getAttribute('tooltip')); |
||
| 565 | } |
||
| 566 | if ($oElementHlinkClick->hasAttribute('r:id') && isset($this->arrayRels[$fileRels][$oElementHlinkClick->getAttribute('r:id')]['Target'])) { |
||
| 567 | $oText->getHyperlink()->setUrl($this->arrayRels[$fileRels][$oElementHlinkClick->getAttribute('r:id')]['Target']); |
||
| 568 | } |
||
| 569 | } |
||
| 570 | //} else { |
||
| 571 | // $oText = $oParagraph->createText(); |
||
| 572 | |||
| 573 | $oSubSubElement = $document->getElement('a:t', $oSubElement); |
||
| 574 | $oText->setText($oSubSubElement->nodeValue); |
||
| 575 | } |
||
| 576 | } |
||
| 577 | } |
||
| 578 | } |
||
| 579 | |||
| 580 | if (count($oShape->getParagraphs()) > 0) { |
||
| 581 | $oShape->setActiveParagraph(0); |
||
| 582 | } |
||
| 583 | } |
||
| 584 | |||
| 585 | /** |
||
| 586 | * |
||
| 587 | * @param string $fileRels |
||
| 588 | * @return string |
||
| 589 | */ |
||
| 590 | protected function loadRels($fileRels) |
||
| 591 | { |
||
| 592 | $sPart = $this->oZip->getFromName($fileRels); |
||
| 593 | if ($sPart !== false) { |
||
| 594 | $xmlReader = new XMLReader(); |
||
| 595 | if ($xmlReader->getDomFromString($sPart)) { |
||
| 596 | foreach ($xmlReader->getElements('*') as $oNode) { |
||
| 597 | $this->arrayRels[$fileRels][$oNode->getAttribute('Id')] = array( |
||
| 598 | 'Target' => $oNode->getAttribute('Target'), |
||
| 599 | 'Type' => $oNode->getAttribute('Type'), |
||
| 600 | ); |
||
| 601 | } |
||
| 602 | } |
||
| 603 | } |
||
| 604 | } |
||
| 605 | |||
| 606 | 4 | protected function loadEncryptedFile() |
|
| 607 | { |
||
| 608 | //return false; |
||
| 609 | 4 | $oOLE = new OLERead(); |
|
| 610 | 4 | $oOLE->read($this->filename); |
|
| 611 | |||
| 612 | 1 | $oStreamEncrypted = $oOLE->getStream($oOLE->encryptedPackage); |
|
| 613 | $pos = 0; |
||
| 614 | $size = self::getInt4d($oStreamEncrypted, $pos); |
||
| 615 | var_dump($size); |
||
| 616 | $pos += 8; |
||
| 617 | $data = ''; |
||
| 618 | for ($inc = 0 ; $inc < $size ; $inc++) { |
||
| 619 | $data .= pack('v', self::getInt1d($oStreamEncrypted, $pos + $inc)); |
||
| 620 | } |
||
| 621 | |||
| 622 | $oStream = $oOLE->getStream($oOLE->encryptionInfo); |
||
| 623 | $pos = 0; |
||
| 624 | // EncryptionVersionInfo |
||
| 625 | $vMajor = self::getInt2d($oStream, $pos); |
||
| 626 | $pos += 2; |
||
| 627 | $vMinor = self::getInt2d($oStream, $pos); |
||
| 628 | $pos += 2; |
||
| 629 | // EncryptionHeader.Flags |
||
| 630 | $pos += 4; |
||
| 631 | // EncryptionHeaderSize |
||
| 632 | $size = self::getInt4d($oStream, $pos); |
||
| 633 | $pos += 4; |
||
| 634 | // EncryptionHeader |
||
| 635 | // EncryptionHeader > Flags |
||
| 636 | $flags = self::getInt4d($oStream, $pos); |
||
| 637 | var_dump($flags); |
||
| 638 | var_dump('EncryptionHeader > Flags > fCryptoAPI :' . (($flags >> 2) & bindec('1'))); // |
||
| 639 | var_dump('EncryptionHeader > Flags > fDocProps :' . (($flags >> 3) & bindec('1'))); // |
||
| 640 | var_dump('EncryptionHeader > Flags > fExternal :' . (($flags >> 4) & bindec('1'))); // |
||
| 641 | var_dump('EncryptionHeader > Flags > fAES :' . (($flags >> 5) & bindec('1'))); // |
||
| 642 | $pos += 4; |
||
| 643 | $size -= 4; |
||
| 644 | // EncryptionHeader > SizeExtra |
||
| 645 | $sizeExtra = self::getInt4d($oStream, $pos); |
||
| 646 | var_dump('EncryptionHeader > SizeExtra : '.$sizeExtra); |
||
| 647 | $pos += 4; |
||
| 648 | $size -= 4; |
||
| 649 | // EncryptionHeader > AlgID |
||
| 650 | $algID = self::getInt4d($oStream, $pos); |
||
| 651 | var_dump('EncryptionHeader > AlgID :'.$algID.' => 128-bit AES : ' . var_export(0x0000660E, true)); |
||
| 652 | $pos += 4; |
||
| 653 | $size -= 4; |
||
| 654 | // EncryptionHeader > AlgIDHash |
||
| 655 | $algIDHash = self::getInt4d($oStream, $pos); |
||
| 656 | var_dump('EncryptionHeader > AlgIDHash : '.$algIDHash. ' = 0x00008004'); |
||
| 657 | $pos += 4; |
||
| 658 | $size -= 4; |
||
| 659 | // EncryptionHeader > KeySize |
||
| 660 | $keySize = self::getInt4d($oStream, $pos); |
||
| 661 | var_dump('EncryptionHeader > KeySize : '.$keySize. ' = 0x00000080'); |
||
| 662 | $pos += 4; |
||
| 663 | $size -= 4; |
||
| 664 | // EncryptionHeader > ProviderType |
||
| 665 | $providerType = self::getInt4d($oStream, $pos); |
||
| 666 | var_dump('EncryptionHeader > ProviderType : '.$providerType. ' = 0x00000018'); |
||
| 667 | $pos += 4; |
||
| 668 | $size -= 4; |
||
| 669 | // EncryptionHeader > Reserved1 |
||
| 670 | $pos += 4; |
||
| 671 | $size -= 4; |
||
| 672 | // EncryptionHeader > Reserved2 |
||
| 673 | $pos += 4; |
||
| 674 | $size -= 4; |
||
| 675 | // EncryptionHeader > CSPName |
||
| 676 | $CSPName = ''; |
||
| 677 | for ($inc = 0 ; $inc <= $size ; $inc += 2) { |
||
| 678 | $chr = self::getInt2d($oStream, $pos); |
||
| 679 | $pos += 2; |
||
| 680 | if ($chr == 0) { |
||
| 681 | break; |
||
| 682 | } |
||
| 683 | $CSPName .= chr($chr); |
||
| 684 | } |
||
| 685 | var_dump('EncryptionHeader > CSPName : '.$CSPName); |
||
| 686 | // EncryptionVerifier |
||
| 687 | // EncryptionVerifier > SaltSize |
||
| 688 | $saltSize = self::getInt4d($oStream, $pos); |
||
| 689 | var_dump('EncryptionVerifier > SaltSize : '.$saltSize.' = 0x00000010'); |
||
| 690 | $pos += 4; |
||
| 691 | // EncryptionVerifier > Salt |
||
| 692 | $salt = ''; |
||
| 693 | for ($inc = 0 ; $inc < 16 ; $inc ++) { |
||
| 694 | $salt .= pack('v', self::getInt1d($oStream, $pos)); |
||
| 695 | $pos += 1; |
||
| 696 | } |
||
| 697 | var_dump('EncryptionVerifier > Salt : '.$salt); |
||
| 698 | // EncryptionVerifier > EncryptedVerifier |
||
| 699 | $encryptedVerifier = ''; |
||
| 700 | for ($inc = 0 ; $inc < 16 ; $inc ++) { |
||
| 701 | $encryptedVerifier = pack('v', self::getInt1d($oStream, $pos)); |
||
| 702 | $pos += 1; |
||
| 703 | } |
||
| 704 | var_dump('EncryptionVerifier > EncryptedVerifier : '.$encryptedVerifier); |
||
| 705 | // EncryptionVerifier > VerifierHashSize |
||
| 706 | $verifierHashSize = self::getInt4d($oStream, $pos); |
||
| 707 | var_dump('EncryptionVerifier > VerifierHashSize : '.$verifierHashSize.' = 0x00000010'); |
||
| 708 | $pos += 4; |
||
| 709 | // EncryptionVerifier > EncryptedVerifierHash |
||
| 710 | // mon cas : AES donc 32 |
||
| 711 | $encryptedVerifierHash = ''; |
||
| 712 | for ($inc = 0 ; $inc < 32 ; $inc ++) { |
||
| 713 | $encryptedVerifierHash .= pack('v', self::getInt1d($oStream, $pos)); |
||
| 714 | $pos += 1; |
||
| 715 | } |
||
| 716 | var_dump($encryptedVerifierHash); |
||
| 717 | |||
| 718 | $hash = $salt + $this->getPassword(); |
||
| 719 | for($inc = 0 ; $inc < 50000 ; $inc++) { |
||
| 720 | $hash = sha1($hash); |
||
| 721 | } |
||
| 722 | $hash = sha1($hash . 0x00000000); |
||
| 723 | var_dump($hash); |
||
| 724 | } |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Read 8-bit unsigned integer |
||
| 728 | * |
||
| 729 | * @param string $data |
||
| 730 | * @param int $pos |
||
| 731 | * @return int |
||
| 732 | */ |
||
| 733 | public static function getInt1d($data, $pos) |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Read 16-bit unsigned integer |
||
| 740 | * |
||
| 741 | * @param string $data |
||
| 742 | * @param int $pos |
||
| 743 | * @return int |
||
| 744 | */ |
||
| 745 | public static function getInt2d($data, $pos) |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Read 32-bit signed integer |
||
| 752 | * |
||
| 753 | * @param string $data |
||
| 754 | * @param int $pos |
||
| 755 | * @return int |
||
| 756 | */ |
||
| 757 | public static function getInt4d($data, $pos) |
||
| 771 | } |
||
| 772 |