|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of PHPPresentation - A pure PHP library for reading and writing |
| 5 | + * presentations documents. |
| 6 | + * |
| 7 | + * PHPPresentation is free software distributed under the terms of the GNU Lesser |
| 8 | + * General Public License version 3 as published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * For the full copyright and license information, please read the LICENSE |
| 11 | + * file that was distributed with this source code. For the full list of |
| 12 | + * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors. |
| 13 | + * |
| 14 | + * @see https://github.com/PHPOffice/PHPPresentation |
| 15 | + * |
| 16 | + * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
| 17 | + */ |
| 18 | + |
| 19 | +declare(strict_types=1); |
| 20 | + |
| 21 | +namespace PhpOffice\PhpPresentation\Reader; |
| 22 | + |
| 23 | +use DOMElement; |
| 24 | +use PhpOffice\Common\XMLReader; |
| 25 | +use PhpOffice\PhpPresentation\Exception\FileNotFoundException; |
| 26 | +use PhpOffice\PhpPresentation\Exception\InvalidFileFormatException; |
| 27 | +use PhpOffice\PhpPresentation\PhpPresentation; |
| 28 | +use PhpOffice\PhpPresentation\Shape\RichText; |
| 29 | +use PhpOffice\PhpPresentation\Slide; |
| 30 | +use ZipArchive; |
| 31 | + |
| 32 | +/** |
| 33 | + * Reader for the Apple Keynote (.key) presentation format. |
| 34 | + * |
| 35 | + * This reader targets the legacy iWork '09 single-file structure, where the |
| 36 | + * presentation is described by an `index.apxl` XML document stored inside the |
| 37 | + * Keynote package (a Zip archive). The modern IWA binary format (Keynote '13 and |
| 38 | + * later) is intentionally not handled and is safely rejected by canRead(). |
| 39 | + */ |
| 40 | +class Keynote implements ReaderInterface |
| 41 | +{ |
| 42 | + /** |
| 43 | + * Keynote namespace. |
| 44 | + * |
| 45 | + * @var string |
| 46 | + */ |
| 47 | + public const NS_KEY = 'http://developer.apple.com/namespaces/keynote2'; |
| 48 | + |
| 49 | + /** |
| 50 | + * iWork shared namespace. |
| 51 | + * |
| 52 | + * @var string |
| 53 | + */ |
| 54 | + public const NS_SF = 'http://developer.apple.com/namespaces/sf'; |
| 55 | + |
| 56 | + /** |
| 57 | + * iWork shared abstract namespace. |
| 58 | + * |
| 59 | + * @var string |
| 60 | + */ |
| 61 | + public const NS_SFA = 'http://developer.apple.com/namespaces/sfa'; |
| 62 | + |
| 63 | + /** |
| 64 | + * Output object. |
| 65 | + * |
| 66 | + * @var PhpPresentation |
| 67 | + */ |
| 68 | + protected $oPhpPresentation; |
| 69 | + |
| 70 | + /** |
| 71 | + * Should the images be loaded? |
| 72 | + * |
| 73 | + * @var bool |
| 74 | + */ |
| 75 | + protected $loadImages = true; |
| 76 | + |
| 77 | + /** |
| 78 | + * Can the current reader read the file? |
| 79 | + */ |
| 80 | + public function canRead(string $pFilename): bool |
| 81 | + { |
| 82 | + return $this->fileSupportsUnserializePhpPresentation($pFilename); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Does a file support being read by this reader? |
| 87 | + */ |
| 88 | + public function fileSupportsUnserializePhpPresentation(string $pFilename = ''): bool |
| 89 | + { |
| 90 | + if (!file_exists($pFilename)) { |
| 91 | + throw new FileNotFoundException($pFilename); |
| 92 | + } |
| 93 | + |
| 94 | + $oZip = new ZipArchive(); |
| 95 | + if (true === $oZip->open($pFilename)) { |
| 96 | + $isApxl = is_array($oZip->statName('index.apxl')); |
| 97 | + $oZip->close(); |
| 98 | + |
| 99 | + return $isApxl; |
| 100 | + } |
| 101 | + |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Loads a Keynote file. |
| 107 | + */ |
| 108 | + public function load(string $pFilename, int $flags = 0): PhpPresentation |
| 109 | + { |
| 110 | + if (!$this->fileSupportsUnserializePhpPresentation($pFilename)) { |
| 111 | + throw new InvalidFileFormatException($pFilename, self::class); |
| 112 | + } |
| 113 | + |
| 114 | + $this->loadImages = !((bool) ($flags & self::SKIP_IMAGES)); |
| 115 | + |
| 116 | + return $this->loadFile($pFilename); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Loads the file content into a PhpPresentation object. |
| 121 | + */ |
| 122 | + protected function loadFile(string $pFilename): PhpPresentation |
| 123 | + { |
| 124 | + $this->oPhpPresentation = new PhpPresentation(); |
| 125 | + $this->oPhpPresentation->removeSlideByIndex(); |
| 126 | + |
| 127 | + $oXMLReader = new XMLReader(); |
| 128 | + if (false !== $oXMLReader->getDomFromZip($pFilename, 'index.apxl')) { |
| 129 | + $oXMLReader->registerNamespace('key', self::NS_KEY); |
| 130 | + $oXMLReader->registerNamespace('sf', self::NS_SF); |
| 131 | + $oXMLReader->registerNamespace('sfa', self::NS_SFA); |
| 132 | + $this->loadSlides($oXMLReader); |
| 133 | + } |
| 134 | + |
| 135 | + return $this->oPhpPresentation; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Reads every slide of the presentation. |
| 140 | + */ |
| 141 | + protected function loadSlides(XMLReader $oXMLReader): void |
| 142 | + { |
| 143 | + foreach ($oXMLReader->getElements('/key:presentation/key:slide-list/key:slide') as $oNodeSlide) { |
| 144 | + if (!$oNodeSlide instanceof DOMElement) { |
| 145 | + continue; |
| 146 | + } |
| 147 | + $oSlide = $this->oPhpPresentation->createSlide(); |
| 148 | + $this->loadShapes($oXMLReader, $oNodeSlide, $oSlide); |
| 149 | + $this->loadNote($oXMLReader, $oNodeSlide, $oSlide); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Reads every text placeholder of a slide. |
| 155 | + */ |
| 156 | + protected function loadShapes(XMLReader $oXMLReader, DOMElement $oNodeSlide, Slide $oSlide): void |
| 157 | + { |
| 158 | + foreach ($oXMLReader->getElements('./key:drawables/key:body-placeholder', $oNodeSlide) as $oNodeShape) { |
| 159 | + if (!$oNodeShape instanceof DOMElement) { |
| 160 | + continue; |
| 161 | + } |
| 162 | + $oRichText = $oSlide->createRichTextShape(); |
| 163 | + $this->loadGeometry($oXMLReader, $oNodeShape, $oRichText); |
| 164 | + $this->loadText($oXMLReader, $oNodeShape, $oRichText); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Reads the position and size of a shape. |
| 170 | + */ |
| 171 | + protected function loadGeometry(XMLReader $oXMLReader, DOMElement $oNodeShape, RichText $oRichText): void |
| 172 | + { |
| 173 | + $oNodePosition = $oXMLReader->getElement('./sf:geometry/sf:position', $oNodeShape); |
| 174 | + if ($oNodePosition instanceof DOMElement) { |
| 175 | + $oRichText->setOffsetX((int) $oNodePosition->getAttributeNS(self::NS_SFA, 'x')); |
| 176 | + $oRichText->setOffsetY((int) $oNodePosition->getAttributeNS(self::NS_SFA, 'y')); |
| 177 | + } |
| 178 | + |
| 179 | + $oNodeSize = $oXMLReader->getElement('./sf:geometry/sf:size', $oNodeShape); |
| 180 | + if ($oNodeSize instanceof DOMElement) { |
| 181 | + $oRichText->setWidth((int) $oNodeSize->getAttributeNS(self::NS_SFA, 'w')); |
| 182 | + $oRichText->setHeight((int) $oNodeSize->getAttributeNS(self::NS_SFA, 'h')); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Reads the paragraphs and runs of a text container. |
| 188 | + */ |
| 189 | + protected function loadText(XMLReader $oXMLReader, DOMElement $oNodeContainer, RichText $oRichText): void |
| 190 | + { |
| 191 | + $oNodeBody = $oXMLReader->getElement('./sf:text/sf:text-storage/sf:text-body', $oNodeContainer); |
| 192 | + if (!$oNodeBody instanceof DOMElement) { |
| 193 | + return; |
| 194 | + } |
| 195 | + |
| 196 | + $isFirstParagraph = true; |
| 197 | + foreach ($oXMLReader->getElements('./sf:p', $oNodeBody) as $oNodeParagraph) { |
| 198 | + if (!$oNodeParagraph instanceof DOMElement) { |
| 199 | + continue; |
| 200 | + } |
| 201 | + $oParagraph = $isFirstParagraph |
| 202 | + ? $oRichText->getActiveParagraph() |
| 203 | + : $oRichText->createParagraph(); |
| 204 | + $isFirstParagraph = false; |
| 205 | + |
| 206 | + foreach ($oXMLReader->getElements('./sf:span', $oNodeParagraph) as $oNodeSpan) { |
| 207 | + if (!$oNodeSpan instanceof DOMElement) { |
| 208 | + continue; |
| 209 | + } |
| 210 | + $oParagraph->createTextRun((string) $oNodeSpan->nodeValue); |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * Reads the speaker note of a slide. |
| 217 | + */ |
| 218 | + protected function loadNote(XMLReader $oXMLReader, DOMElement $oNodeSlide, Slide $oSlide): void |
| 219 | + { |
| 220 | + $oNodeNote = $oXMLReader->getElement('./key:notes', $oNodeSlide); |
| 221 | + if (!$oNodeNote instanceof DOMElement) { |
| 222 | + return; |
| 223 | + } |
| 224 | + |
| 225 | + $oRichText = $oSlide->getNote()->createRichTextShape(); |
| 226 | + $this->loadText($oXMLReader, $oNodeNote, $oRichText); |
| 227 | + } |
| 228 | +} |
0 commit comments