Completed
Push — develop ( 985c50...159711 )
by Franck
12s
created

Manifest   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 89.09%

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 19
c 6
b 0
f 1
lcom 1
cbo 11
dl 0
loc 163
ccs 98
cts 110
cp 0.8909
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
D writePart() 0 109 12
C getImageMimeType() 0 36 7
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\ODPresentation;
19
20
use PhpOffice\Common\File;
21
use PhpOffice\PhpPresentation\PhpPresentation;
22
use PhpOffice\PhpPresentation\Shape\Drawing as ShapeDrawing;
23
use PhpOffice\PhpPresentation\Shape\MemoryDrawing;
24
use PhpOffice\PhpPresentation\Slide\Background\Image;
25
use PhpOffice\PhpPresentation\Writer\ODPresentation;
26
27
/**
28
 * \PhpOffice\PhpPresentation\Writer\ODPresentation\Manifest
29
 */
30
class Manifest extends AbstractPart
31
{
32
    /**
33
     * Write Manifest file to XML format
34
     *
35
     * @return string        XML Output
36
     * @throws \Exception
37
     */
38 55
    public function writePart()
39
    {
40 55
        $parentWriter = $this->getParentWriter();
41 55
        if (!$parentWriter instanceof ODPresentation) {
42 1
            throw new \Exception('The $parentWriter is not an instance of \PhpOffice\PhpPresentation\Writer\ODPresentation');
43
        }
44
        
45
        // Create XML writer
46 54
        $objWriter = $this->getXMLWriter();
47
48
        // XML header
49 54
        $objWriter->startDocument('1.0', 'UTF-8');
50
51
        // manifest:manifest
52 54
        $objWriter->startElement('manifest:manifest');
53 54
        $objWriter->writeAttribute('xmlns:manifest', 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0');
54 54
        $objWriter->writeAttribute('manifest:version', '1.2');
55
56
        // manifest:file-entry
57 54
        $objWriter->startElement('manifest:file-entry');
58 54
        $objWriter->writeAttribute('manifest:media-type', 'application/vnd.oasis.opendocument.presentation');
59 54
        $objWriter->writeAttribute('manifest:version', '1.2');
60 54
        $objWriter->writeAttribute('manifest:full-path', '/');
61 54
        $objWriter->endElement();
62
        // manifest:file-entry
63 54
        $objWriter->startElement('manifest:file-entry');
64 54
        $objWriter->writeAttribute('manifest:media-type', 'text/xml');
65 54
        $objWriter->writeAttribute('manifest:full-path', 'content.xml');
66 54
        $objWriter->endElement();
67
        // manifest:file-entry
68 54
        $objWriter->startElement('manifest:file-entry');
69 54
        $objWriter->writeAttribute('manifest:media-type', 'text/xml');
70 54
        $objWriter->writeAttribute('manifest:full-path', 'meta.xml');
71 54
        $objWriter->endElement();
72
        // manifest:file-entry
73 54
        $objWriter->startElement('manifest:file-entry');
74 54
        $objWriter->writeAttribute('manifest:media-type', 'text/xml');
75 54
        $objWriter->writeAttribute('manifest:full-path', 'styles.xml');
76 54
        $objWriter->endElement();
77
        
78
        // Charts
79 54
        foreach ($parentWriter->chartArray as $key => $shape) {
80 20
            $objWriter->startElement('manifest:file-entry');
81 20
            $objWriter->writeAttribute('manifest:full-path', 'Object '.$key.'/');
82 20
            $objWriter->writeAttribute('manifest:media-type', 'application/vnd.oasis.opendocument.chart');
83 20
            $objWriter->endElement();
84 20
            $objWriter->startElement('manifest:file-entry');
85 20
            $objWriter->writeAttribute('manifest:full-path', 'Object '.$key.'/content.xml');
86 20
            $objWriter->writeAttribute('manifest:media-type', 'text/xml');
87 20
            $objWriter->endElement();
88 54
        }
89
90 54
        $arrMedia = array();
91 54
        for ($i = 0; $i < $parentWriter->getDrawingHashTable()->count(); ++$i) {
92 28
            $shape = $parentWriter->getDrawingHashTable()->getByIndex($i);
93 28
            if ($shape instanceof ShapeDrawing) {
94 8
                if (!in_array(md5($shape->getPath()), $arrMedia)) {
95 8
                    $arrMedia[] = md5($shape->getPath());
96 8
                    $mimeType   = $this->getImageMimeType($shape->getPath());
97
98 6
                    $objWriter->startElement('manifest:file-entry');
99 6
                    $objWriter->writeAttribute('manifest:media-type', $mimeType);
100 6
                    $objWriter->writeAttribute('manifest:full-path', 'Pictures/' . md5($shape->getPath()) . '.' . $shape->getExtension());
101 6
                    $objWriter->endElement();
102 6
                }
103 26
            } elseif ($shape instanceof MemoryDrawing) {
104 1
                if (!in_array(str_replace(' ', '_', $shape->getIndexedFilename()), $arrMedia)) {
105 1
                    $arrMedia[] = str_replace(' ', '_', $shape->getIndexedFilename());
106 1
                    $mimeType = $shape->getMimeType();
107
108 1
                    $objWriter->startElement('manifest:file-entry');
109 1
                    $objWriter->writeAttribute('manifest:media-type', $mimeType);
110 1
                    $objWriter->writeAttribute('manifest:full-path', 'Pictures/' . str_replace(' ', '_', $shape->getIndexedFilename()));
111 1
                    $objWriter->endElement();
112 1
                }
113 1
            }
114 26
        }
115
116 52
        foreach ($parentWriter->getPhpPresentation()->getAllSlides() as $numSlide => $oSlide) {
117 52
            $oBkgImage = $oSlide->getBackground();
118 52
            if ($oBkgImage instanceof Image) {
119
                $mimeType   = $this->getImageMimeType($oBkgImage->getPath());
120
121
                $objWriter->startElement('manifest:file-entry');
122
                $objWriter->writeAttribute('manifest:media-type', $mimeType);
123
                $objWriter->writeAttribute('manifest:full-path', 'Pictures/' . str_replace(' ', '_', $oBkgImage->getIndexedFilename($numSlide)));
124
                $objWriter->endElement();
125
            }
126 52
        }
127
128 52
        if ($parentWriter->getPhpPresentation()->getPresentationProperties()->getThumbnailPath()) {
129 1
            $pathThumbnail = $parentWriter->getPhpPresentation()->getPresentationProperties()->getThumbnailPath();
130
            // Size : 128x128 pixel
131
            // PNG : 8bit, non-interlaced with full alpha transparency
132 1
            $gdImage = imagecreatefromstring(file_get_contents($pathThumbnail));
133 1
            if ($gdImage) {
134 1
                imagedestroy($gdImage);
135 1
                $objWriter->startElement('manifest:file-entry');
136 1
                $objWriter->writeAttribute('manifest:media-type', 'image/png');
137 1
                $objWriter->writeAttribute('manifest:full-path', 'Thumbnails/thumbnail.png');
138 1
                $objWriter->endElement();
139 1
            }
140 1
        }
141
142 52
        $objWriter->endElement();
143
144
        // Return
145 52
        return $objWriter->getData();
146
    }
147
148
    /**
149
     * Get image mime type
150
     *
151
     * @param  string    $pFile Filename
152
     * @return string    Mime Type
153
     * @throws \Exception
154
     * @todo PowerPoint2007\ContentTypes duplicate Code : getImageMimeType
155
     */
156 8
    private function getImageMimeType($pFile = '')
157
    {
158 8
        if (strpos($pFile, 'zip://') === 0) {
159 2
            $pZIPFile = str_replace('zip://', '', $pFile);
160 2
            $pZIPFile = substr($pZIPFile, 0, strpos($pZIPFile, '#'));
161 2
            if (!File::fileExists($pZIPFile)) {
162 1
                throw new \Exception("File $pFile does not exist");
163
            }
164 1
            $pImgFile = substr($pFile, strpos($pFile, '#') + 1);
165 1
            $oArchive = new \ZipArchive();
166 1
            $oArchive->open($pZIPFile);
167 1
            if (!function_exists('getimagesizefromstring')) {
168
                $uri = 'data://application/octet-stream;base64,' . base64_encode($oArchive->getFromName($pImgFile));
169
                $image = getimagesize($uri);
170
            } else {
171 1
                $image = getimagesizefromstring($oArchive->getFromName($pImgFile));
172
            }
173 7
        } elseif (strpos($pFile, 'data:image/') === 0) {
174 1
            $sImage = $pFile;
175 1
            list(, $sImage) = explode(';', $sImage);
176 1
            list(, $sImage) = explode(',', $sImage);
177 1
            if (!function_exists('getimagesizefromstring')) {
178
                $uri = 'data://application/octet-stream;base64,' . base64_encode($sImage);
179
                $image = getimagesize($uri);
180
            } else {
181 1
                $image = getimagesizefromstring($sImage);
182
            }
183 1
        } else {
184 5
            if (!File::fileExists($pFile)) {
185 1
                throw new \Exception("File $pFile does not exist");
186
            }
187 4
            $image = getimagesize($pFile);
188
        }
189
190 6
        return image_type_to_mime_type($image[2]);
191
    }
192
}
193