Completed
Push — develop ( 344c77...9b66e5 )
by Franck
28s queued 12s
created

Serialized::canRead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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\Reader;
19
20
use PhpOffice\Common\File;
21
use PhpOffice\PhpPresentation\Shape\Drawing\AbstractDrawingAdapter;
22
23
/**
24
 * Serialized format reader
25
 */
26
class Serialized implements ReaderInterface
27
{
28
    /**
29
     * Can the current \PhpOffice\PhpPresentation\Reader\ReaderInterface read the file?
30
     *
31
     * @param  string $pFilename
32
     * @throws \Exception
33
     * @return boolean
34
     */
35 3
    public function canRead($pFilename)
36
    {
37 3
        return $this->fileSupportsUnserializePhpPresentation($pFilename);
38
    }
39
40
    /**
41
     * Does a file support UnserializePhpPresentation ?
42
     *
43
     * @param  string    $pFilename
44
     * @throws \Exception
45
     * @return boolean
46
     */
47 6
    public function fileSupportsUnserializePhpPresentation($pFilename = '')
48
    {
49
        // Check if file exists
50 6
        if (!file_exists($pFilename)) {
51 1
            throw new \Exception("Could not open " . $pFilename . " for reading! File does not exist.");
52
        }
53
54
        // File exists, does it contain PhpPresentation.xml?
55 5
        return File::fileExists("zip://$pFilename#PhpPresentation.xml");
56
    }
57
58
    /**
59
     * Loads PhpPresentation Serialized file
60
     *
61
     * @param  string        $pFilename
62
     * @return \PhpOffice\PhpPresentation\PhpPresentation
63
     * @throws \Exception
64
     */
65 4
    public function load($pFilename)
66
    {
67
        // Check if file exists
68 4
        if (!file_exists($pFilename)) {
69 1
            throw new \Exception("Could not open " . $pFilename . " for reading! File does not exist.");
70
        }
71
72
        // Unserialize... First make sure the file supports it!
73 3
        if (!$this->fileSupportsUnserializePhpPresentation($pFilename)) {
74 1
            throw new \Exception("Invalid file format for PhpOffice\PhpPresentation\Reader\Serialized: " . $pFilename . ".");
75
        }
76
77 2
        return $this->loadSerialized($pFilename);
78
    }
79
80
    /**
81
     * Load PhpPresentation Serialized file
82
     *
83
     * @param  string        $pFilename
84
     * @return \PhpOffice\PhpPresentation\PhpPresentation
85
     */
86 2
    private function loadSerialized($pFilename)
87
    {
88 2
        $oArchive = new \ZipArchive();
89 2
        if ($oArchive->open($pFilename) === true) {
90 2
            $xmlContent = $oArchive->getFromName('PhpPresentation.xml');
91
92 2
            if (!empty($xmlContent)) {
93 1
                $xmlData = simplexml_load_string($xmlContent);
94 1
                $file    = unserialize(base64_decode((string) $xmlData->data));
95
96
                // Update media links
97 1
                for ($i = 0; $i < $file->getSlideCount(); ++$i) {
98 1
                    for ($j = 0; $j < $file->getSlide($i)->getShapeCollection()->count(); ++$j) {
99 1
                        if ($file->getSlide($i)->getShapeCollection()->offsetGet($j) instanceof AbstractDrawingAdapter) {
100 1
                            $imgTemp = $file->getSlide($i)->getShapeCollection()->offsetGet($j);
101 1
                            $imgTemp->setPath('zip://' . $pFilename . '#media/' . $imgTemp->getImageIndex() . '/' . pathinfo($imgTemp->getPath(), PATHINFO_BASENAME), false);
102
                        }
103
                    }
104
                }
105
106 1
                $oArchive->close();
107 1
                return $file;
108
            }
109
        }
110
111 1
        return null;
112
    }
113
}
114