Completed
Pull Request — develop (#186)
by
unknown
07:55
created

Manifest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 93.59%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 16
c 5
b 0
f 1
lcom 1
cbo 11
dl 0
loc 150
ccs 73
cts 78
cp 0.9359
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
D writePart() 0 109 12
B getImageMimeType() 0 24 4
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 44
    public function writePart()
39
    {
40 44
        $parentWriter = $this->getParentWriter();
41 44
        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 43
        $objWriter = $this->getXMLWriter();
47
48
        // XML header
49 43
        $objWriter->startDocument('1.0', 'UTF-8');
50
51
        // manifest:manifest
52 43
        $objWriter->startElement('manifest:manifest');
53 43
        $objWriter->writeAttribute('xmlns:manifest', 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0');
54 43
        $objWriter->writeAttribute('manifest:version', '1.2');
55
56
        // manifest:file-entry
57 43
        $objWriter->startElement('manifest:file-entry');
58 43
        $objWriter->writeAttribute('manifest:media-type', 'application/vnd.oasis.opendocument.presentation');
59 43
        $objWriter->writeAttribute('manifest:version', '1.2');
60 43
        $objWriter->writeAttribute('manifest:full-path', '/');
61 43
        $objWriter->endElement();
62
        // manifest:file-entry
63 43
        $objWriter->startElement('manifest:file-entry');
64 43
        $objWriter->writeAttribute('manifest:media-type', 'text/xml');
65 43
        $objWriter->writeAttribute('manifest:full-path', 'content.xml');
66 43
        $objWriter->endElement();
67
        // manifest:file-entry
68 43
        $objWriter->startElement('manifest:file-entry');
69 43
        $objWriter->writeAttribute('manifest:media-type', 'text/xml');
70 43
        $objWriter->writeAttribute('manifest:full-path', 'meta.xml');
71 43
        $objWriter->endElement();
72
        // manifest:file-entry
73 43
        $objWriter->startElement('manifest:file-entry');
74 43
        $objWriter->writeAttribute('manifest:media-type', 'text/xml');
75 43
        $objWriter->writeAttribute('manifest:full-path', 'styles.xml');
76 43
        $objWriter->endElement();
77
        
78
        // Charts
79 43
        foreach ($parentWriter->chartArray as $key => $shape) {
80 15
            $objWriter->startElement('manifest:file-entry');
81 15
            $objWriter->writeAttribute('manifest:full-path', 'Object '.$key.'/');
82 15
            $objWriter->writeAttribute('manifest:media-type', 'application/vnd.oasis.opendocument.chart');
83 15
            $objWriter->endElement();
84 15
            $objWriter->startElement('manifest:file-entry');
85 15
            $objWriter->writeAttribute('manifest:full-path', 'Object '.$key.'/content.xml');
86 15
            $objWriter->writeAttribute('manifest:media-type', 'text/xml');
87 15
            $objWriter->endElement();
88
        }
89
90 43
        $arrMedia = array();
91 43
        for ($i = 0; $i < $parentWriter->getDrawingHashTable()->count(); ++$i) {
92 19
            $shape = $parentWriter->getDrawingHashTable()->getByIndex($i);
93 19
            if ($shape instanceof ShapeDrawing) {
94 4
                if (!in_array(md5($shape->getPath()), $arrMedia)) {
95 4
                    $arrMedia[] = md5($shape->getPath());
96 4
                    $mimeType   = $this->getImageMimeType($shape->getPath());
97
98 4
                    $objWriter->startElement('manifest:file-entry');
99 4
                    $objWriter->writeAttribute('manifest:media-type', $mimeType);
100 4
                    $objWriter->writeAttribute('manifest:full-path', 'Pictures/' . md5($shape->getPath()) . '.' . $shape->getExtension());
101 4
                    $objWriter->endElement();
102
                }
103
            } 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
                }
113
            }
114
        }
115
116 43
        foreach ($parentWriter->getPhpPresentation()->getAllSlides() as $numSlide => $oSlide) {
117 43
            $oBkgImage = $oSlide->getBackground();
118 43
            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 43
                $objWriter->endElement();
125
            }
126
        }
127
128 43
        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
            }
140
        }
141
142 43
        $objWriter->endElement();
143
144
        // Return
145 43
        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
     */
155 4
    private function getImageMimeType($pFile = '')
156
    {
157 4
        if (File::fileExists($pFile)) {
158 4
            if (strpos($pFile, 'zip://') === 0) {
159
                $pZIPFile = str_replace('zip://', '', $pFile);
160
                $pZIPFile = substr($pZIPFile, 0, strpos($pZIPFile, '#'));
161
                $pImgFile = substr($pFile, strpos($pFile, '#') + 1);
162
                $oArchive = new \ZipArchive();
163
                $oArchive->open($pZIPFile);
164
                if (!function_exists('getimagesizefromstring')) {
165
                    $uri = 'data://application/octet-stream;base64,' . base64_encode($oArchive->getFromName($pImgFile));
166
                    $image = getimagesize($uri);
167
                } else {
168
                    $image = getimagesizefromstring($oArchive->getFromName($pImgFile));
169
                }
170
            } else {
171 4
                $image = getimagesize($pFile);
172
            }
173
174 4
            return image_type_to_mime_type($image[2]);
175
        } else {
176
            throw new \Exception("File $pFile does not exist");
177
        }
178
    }
179
}
180