Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 51 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| PortableElementPackageParser | |
0.00% |
0 / 51 |
|
0.00% |
0 / 5 |
420 | |
0.00% |
0 / 1 |
| validate | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
30 | |||
| extract | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
30 | |||
| getManifestContent | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
30 | |||
| hasValidPortableElement | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| assertSourceAsFile | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2016 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoQtiItem\model\portableElement\parser\element; |
| 23 | |
| 24 | use oat\taoQtiItem\model\portableElement\exception\PortableElementException; |
| 25 | use oat\taoQtiItem\model\portableElement\exception\PortableElementExtractException; |
| 26 | use oat\taoQtiItem\model\portableElement\exception\PortableElementInconsistencyModelException; |
| 27 | use oat\taoQtiItem\model\portableElement\exception\PortableElementParserException; |
| 28 | use oat\taoQtiItem\model\portableElement\model\PortableElementModelTrait; |
| 29 | use oat\taoQtiItem\model\portableElement\parser\PortableElementParser; |
| 30 | use oat\taoQtiItem\model\qti\exception\ExtractException; |
| 31 | use oat\taoQtiItem\helpers\QtiPackage; |
| 32 | use common_Exception; |
| 33 | use ZipArchive; |
| 34 | |
| 35 | /** |
| 36 | * Parser of a QTI PCI package |
| 37 | * A PCI package must contain a manifest pciCreator.json in the root as well as a pciCreator.js creator file |
| 38 | * |
| 39 | * @package taoQtiItem |
| 40 | */ |
| 41 | abstract class PortableElementPackageParser implements PortableElementParser |
| 42 | { |
| 43 | use PortableElementModelTrait; |
| 44 | |
| 45 | /** |
| 46 | * Validate the zip package |
| 47 | * |
| 48 | * @param string $source Zip package location to validate |
| 49 | * @return bool |
| 50 | * @throws PortableElementException |
| 51 | * @throws PortableElementParserException |
| 52 | * @throws PortableElementInconsistencyModelException |
| 53 | */ |
| 54 | public function validate($source) |
| 55 | { |
| 56 | try { |
| 57 | $this->assertSourceAsFile($source); |
| 58 | |
| 59 | if (! QtiPackage::isValidZip($source)) { |
| 60 | throw new PortableElementParserException('Source package is not a valid zip.'); |
| 61 | } |
| 62 | } catch (common_Exception $e) { |
| 63 | throw new PortableElementParserException('A problem has occured during package parsing.', 0, $e); |
| 64 | } |
| 65 | |
| 66 | $zip = new ZipArchive(); |
| 67 | $zip->open($source, ZIPARCHIVE::CHECKCONS); |
| 68 | |
| 69 | $definitionFiles = $this->getModel()->getDefinitionFiles(); |
| 70 | foreach ($definitionFiles as $file) { |
| 71 | if ($zip->locateName($file) === false) { |
| 72 | throw new PortableElementParserException( |
| 73 | 'The portable element package "' . $this->getModel()->getId() . '" must contains a "' |
| 74 | . $file . '" file at the root of the archive.' |
| 75 | ); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | $zip->close(); |
| 80 | |
| 81 | $this->getModel()->createDataObject($this->getManifestContent($source)); |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Extract zip package into temp directory |
| 87 | * |
| 88 | * @param string $source Zip path |
| 89 | * @return string Tmp directory to find extracted zip |
| 90 | * @throws PortableElementExtractException |
| 91 | * @throws common_Exception |
| 92 | */ |
| 93 | public function extract($source) |
| 94 | { |
| 95 | $tmpDirectory = null; |
| 96 | |
| 97 | $this->assertSourceAsFile($source); |
| 98 | $folder = \tao_helpers_File::createTempDir(); |
| 99 | $zip = new ZipArchive(); |
| 100 | if ($zip->open($source) === true) { |
| 101 | if (\tao_helpers_File::checkWhetherArchiveIsBomb($zip)) { |
| 102 | throw new PortableElementExtractException(sprintf('Source %s seems to be a ZIP bomb', $source)); |
| 103 | } |
| 104 | if ($zip->extractTo($folder)) { |
| 105 | $tmpDirectory = $folder; |
| 106 | } |
| 107 | $zip->close(); |
| 108 | } |
| 109 | |
| 110 | if (! is_dir($tmpDirectory)) { |
| 111 | throw new PortableElementExtractException('Unable to extract portable element.'); |
| 112 | } |
| 113 | |
| 114 | return $tmpDirectory; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Extract JSON representation of $source package e.q. Manifest |
| 119 | * |
| 120 | * @param string $source Zip path |
| 121 | * @return string JSON representation of $this->source |
| 122 | * @throws PortableElementException |
| 123 | * @throws PortableElementParserException |
| 124 | */ |
| 125 | public function getManifestContent($source) |
| 126 | { |
| 127 | $zip = new ZipArchive(); |
| 128 | if ($zip->open($source) === false) { |
| 129 | throw new PortableElementParserException('Unable to open the ZIP file located at: ' . $source); |
| 130 | } |
| 131 | |
| 132 | $manifestName = $this->getModel()->getManifestName(); |
| 133 | if ($zip->locateName($manifestName) === false) { |
| 134 | throw new PortableElementParserException( |
| 135 | 'ZIP package does not have a manifest at root path: ' . $this->getModel()->getManifestName() |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | $content = $zip->getFromName($manifestName); |
| 140 | |
| 141 | if (! $content) { |
| 142 | throw new PortableElementParserException('Manifest file "' . $manifestName . '" found but not readable.'); |
| 143 | } |
| 144 | |
| 145 | $content = json_decode($content, true); |
| 146 | |
| 147 | if (json_last_error() === JSON_ERROR_NONE) { |
| 148 | return $content; |
| 149 | } |
| 150 | throw new PortableElementException('Portable element manifest is not a valid json file.'); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Check if $source has valid portable element |
| 155 | * |
| 156 | * @param string $source Zip path |
| 157 | * @return bool |
| 158 | */ |
| 159 | public function hasValidPortableElement($source) |
| 160 | { |
| 161 | try { |
| 162 | if ($this->validate($source)) { |
| 163 | return true; |
| 164 | } |
| 165 | } catch (common_Exception $e) { |
| 166 | } |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Check if source if file |
| 172 | * |
| 173 | * @param string $source Zip path |
| 174 | * @throws ExtractException |
| 175 | */ |
| 176 | protected function assertSourceAsFile($source) |
| 177 | { |
| 178 | if (! is_file($source)) { |
| 179 | throw new ExtractException('Zip file to extract is not a file. Got "' . $source . '"'); |
| 180 | } |
| 181 | } |
| 182 | } |