Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 75 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| PackageParser | |
0.00% |
0 / 75 |
|
0.00% |
0 / 2 |
930 | |
0.00% |
0 / 1 |
| validate | |
0.00% |
0 / 54 |
|
0.00% |
0 / 1 |
462 | |||
| extract | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
90 | |||
| 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) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoQtiItem\model\qti; |
| 24 | |
| 25 | use tao_models_classes_Parser; |
| 26 | use Exception; |
| 27 | use tao_helpers_File; |
| 28 | use ZipArchive; |
| 29 | use common_exception_Error; |
| 30 | use oat\oatbox\filesystem\File; |
| 31 | |
| 32 | /** |
| 33 | * Enables you to parse and validate a QTI Package. |
| 34 | * The Package is formated as a zip archive containing the manifest and the |
| 35 | * (item files and media files) |
| 36 | * |
| 37 | * @access public |
| 38 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 39 | * @package taoQTI |
| 40 | * @see http://www.imsglobal.org/question/qti_v2p0/imsqti_intgv2p0.html#section10003 |
| 41 | |
| 42 | */ |
| 43 | class PackageParser extends tao_models_classes_Parser |
| 44 | { |
| 45 | protected $extracted; |
| 46 | |
| 47 | /** |
| 48 | * Short description of method validate |
| 49 | * |
| 50 | * @access public |
| 51 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 52 | * @param string schema |
| 53 | * @throws Exception if file or archive is not valid |
| 54 | * @return boolean |
| 55 | */ |
| 56 | public function validate($schema = '') |
| 57 | { |
| 58 | |
| 59 | $forced = $this->valid; |
| 60 | $this->valid = true; |
| 61 | |
| 62 | try { |
| 63 | switch ($this->sourceType) { |
| 64 | case self::SOURCE_FILE: |
| 65 | //check file |
| 66 | if (!file_exists($this->source)) { |
| 67 | throw new Exception("File {$this->source} not found."); |
| 68 | } |
| 69 | if (!is_readable($this->source)) { |
| 70 | throw new Exception("Unable to read file {$this->source}."); |
| 71 | } |
| 72 | if (!preg_match("/\.zip$/", basename($this->source))) { |
| 73 | throw new Exception("Wrong file extension in {$this->source}, zip extension is expected"); |
| 74 | } |
| 75 | if (!tao_helpers_File::securityCheck($this->source)) { |
| 76 | throw new Exception("{$this->source} seems to contain some security issues"); |
| 77 | } |
| 78 | break; |
| 79 | case self::SOURCE_FLYFILE: |
| 80 | //check file |
| 81 | if (!$this->source->exists()) { |
| 82 | throw new Exception("File {$this->source->getBasename()} not found."); |
| 83 | } |
| 84 | if (!preg_match("/\.zip$/", $this->source->getBasename())) { |
| 85 | throw new Exception( |
| 86 | "Wrong file extension in {$this->source->getBasename()}, zip extension is expected" |
| 87 | ); |
| 88 | } |
| 89 | $this->extract(); |
| 90 | break; |
| 91 | default: |
| 92 | throw new Exception("Only regular files are allowed as package source"); |
| 93 | break; |
| 94 | } |
| 95 | } catch (Exception $e) { |
| 96 | if ($forced) { |
| 97 | throw $e; |
| 98 | } else { |
| 99 | $this->addError($e); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if ($this->valid && !$forced) { //valida can be true if forceValidation has been called |
| 104 | $this->valid = false; |
| 105 | |
| 106 | try { |
| 107 | $zip = new ZipArchive(); |
| 108 | //check the archive opening and the consistency |
| 109 | $res = $zip->open($this->source, ZIPARCHIVE::CHECKCONS); |
| 110 | if ($res !== true) { |
| 111 | switch ($res) { |
| 112 | case ZipArchive::ER_NOZIP: |
| 113 | $msg = 'not a zip archive'; |
| 114 | break; |
| 115 | case ZipArchive::ER_INCONS: |
| 116 | $msg = 'consistency check failed'; |
| 117 | break; |
| 118 | case ZipArchive::ER_CRC: |
| 119 | $msg = 'checksum failed'; |
| 120 | break; |
| 121 | default: |
| 122 | $msg = 'Bad Zip file'; |
| 123 | } |
| 124 | throw new Exception($msg); |
| 125 | } else { |
| 126 | //check if the manifest is there |
| 127 | if ($zip->locateName("imsmanifest.xml") === false) { |
| 128 | throw new Exception( |
| 129 | "A QTI package must contains a imsmanifest.xml file at the root of the archive" |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | $this->valid = true; |
| 134 | } |
| 135 | $zip->close(); |
| 136 | } catch (Exception $e) { |
| 137 | $this->addError($e); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | $returnValue = $this->valid; |
| 142 | |
| 143 | return (bool) $returnValue; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Short description of method extract |
| 148 | * |
| 149 | * @access public |
| 150 | * @throws \common_exception_Error |
| 151 | * @throws \common_exception |
| 152 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 153 | * @return string|null |
| 154 | */ |
| 155 | public function extract() |
| 156 | { |
| 157 | if ($this->extracted === null) { |
| 158 | if ($this->source instanceof File) { |
| 159 | $archiveFolder = tao_helpers_File::createTempDir(); |
| 160 | if (!is_dir($archiveFolder)) { |
| 161 | mkdir($archiveFolder); |
| 162 | } |
| 163 | $filename = $archiveFolder . basename($this->source->getPrefix()); |
| 164 | file_put_contents($filename, $this->source->read()); |
| 165 | $this->source = $filename; |
| 166 | } |
| 167 | |
| 168 | if (!is_file($this->source)) { //ultimate verification |
| 169 | throw new common_exception_Error("source " . $this->source . " not a file"); |
| 170 | } |
| 171 | |
| 172 | $folder = tao_helpers_File::createTempDir(); |
| 173 | if (!is_dir($folder)) { |
| 174 | mkdir($folder); |
| 175 | } |
| 176 | |
| 177 | $zip = new ZipArchive(); |
| 178 | if ($zip->open($this->source) === true) { |
| 179 | if (tao_helpers_File::checkWhetherArchiveIsBomb($zip)) { |
| 180 | throw new common_exception_Error(sprintf('Source %s seems to be a ZIP bomb', $this->source)); |
| 181 | } |
| 182 | if ($zip->extractTo($folder)) { |
| 183 | $this->extracted = $folder; |
| 184 | } |
| 185 | $zip->close(); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | return (string) $this->extracted; |
| 190 | } |
| 191 | } |