Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 35 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| QtiPackage | |
0.00% |
0 / 35 |
|
0.00% |
0 / 2 |
156 | |
0.00% |
0 / 1 |
| isValidZip | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
90 | |||
| isValidQtiZip | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
| 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) 2014 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoQtiItem\helpers; |
| 24 | |
| 25 | use ZipArchive; |
| 26 | use tao_helpers_File; |
| 27 | use common_Exception; |
| 28 | |
| 29 | /** |
| 30 | * @access public |
| 31 | * @package tao |
| 32 | |
| 33 | */ |
| 34 | class QtiPackage |
| 35 | { |
| 36 | public static function isValidZip($source) |
| 37 | { |
| 38 | |
| 39 | $returnValue = false; |
| 40 | |
| 41 | if (!file_exists($source)) { |
| 42 | throw new common_Exception("File {$source} not found."); |
| 43 | } |
| 44 | if (!is_readable($source)) { |
| 45 | throw new common_Exception("Unable to read file {$source}."); |
| 46 | } |
| 47 | if (!tao_helpers_File::securityCheck($source)) { |
| 48 | throw new common_Exception("{$source} seems to contain some security issues"); |
| 49 | } |
| 50 | |
| 51 | $zip = new ZipArchive(); |
| 52 | //check the archive opening and the consistency |
| 53 | $res = $zip->open($source, ZIPARCHIVE::CHECKCONS); |
| 54 | if ($res !== true) { |
| 55 | switch ($res) { |
| 56 | case ZipArchive::ER_NOZIP: |
| 57 | $msg = 'not a zip archive'; |
| 58 | break; |
| 59 | case ZipArchive::ER_INCONS: |
| 60 | $msg = 'consistency check failed'; |
| 61 | break; |
| 62 | case ZipArchive::ER_CRC: |
| 63 | $msg = 'checksum failed'; |
| 64 | break; |
| 65 | default: |
| 66 | $msg = 'Bad Zip file'; |
| 67 | } |
| 68 | throw new common_Exception($msg); |
| 69 | } else { |
| 70 | $returnValue = true; |
| 71 | } |
| 72 | $zip->close(); |
| 73 | |
| 74 | return $returnValue; |
| 75 | } |
| 76 | |
| 77 | public static function isValidQtiZip($source) |
| 78 | { |
| 79 | |
| 80 | $returnValue = false; |
| 81 | |
| 82 | if (self::isValidZip($source)) { |
| 83 | $zip = new ZipArchive(); |
| 84 | $zip->open($source, ZIPARCHIVE::CHECKCONS); |
| 85 | if ($zip->locateName("imsmanifest.xml") === false) { |
| 86 | throw new common_Exception( |
| 87 | "A QTI package must contains a imsmanifest.xml file at the root of the archive" |
| 88 | ); |
| 89 | } else { |
| 90 | $returnValue = true; |
| 91 | } |
| 92 | |
| 93 | $zip->close(); |
| 94 | } |
| 95 | |
| 96 | return $returnValue; |
| 97 | } |
| 98 | } |