Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 30 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| SharedStimulusMediaEncoder | |
0.00% |
0 / 30 |
|
0.00% |
0 / 3 |
110 | |
0.00% |
0 / 1 |
| encodeAssets | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
12 | |||
| validateSource | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| secureEncode | |
0.00% |
0 / 5 |
|
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) 2022 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoMediaManager\model\sharedStimulus\encoder; |
| 24 | |
| 25 | use common_exception_Error; |
| 26 | use helpers_File; |
| 27 | use oat\oatbox\service\ConfigurableService; |
| 28 | use oat\tao\model\import\InvalidSourcePathException; |
| 29 | use qtism\data\content\xhtml\Img; |
| 30 | use qtism\data\content\xhtml\ObjectElement; |
| 31 | use qtism\data\storage\xml\marshalling\MarshallingException; |
| 32 | use qtism\data\storage\xml\XmlDocument; |
| 33 | use qtism\data\storage\xml\XmlStorageException; |
| 34 | use tao_helpers_File; |
| 35 | use tao_models_classes_FileNotFoundException; |
| 36 | |
| 37 | class SharedStimulusMediaEncoder extends ConfigurableService implements SharedStimulusMediaEncoderInterface |
| 38 | { |
| 39 | /** |
| 40 | * @throws common_exception_Error |
| 41 | * @throws MarshallingException |
| 42 | * @throws XmlStorageException |
| 43 | * @throws tao_models_classes_FileNotFoundException |
| 44 | * @throws InvalidSourcePathException |
| 45 | */ |
| 46 | public function encodeAssets(string $passageXmlFilePath): string |
| 47 | { |
| 48 | $baseDir = dirname($passageXmlFilePath) . DIRECTORY_SEPARATOR; |
| 49 | |
| 50 | $xmlDocument = new XmlDocument(); |
| 51 | $xmlDocument->load($passageXmlFilePath, true); |
| 52 | |
| 53 | $images = $xmlDocument->getDocumentComponent()->getComponentsByClassName('img'); |
| 54 | $objects = $xmlDocument->getDocumentComponent()->getComponentsByClassName('object'); |
| 55 | |
| 56 | /** @var $image Img */ |
| 57 | foreach ($images as $image) { |
| 58 | $source = $image->getSrc(); |
| 59 | $this->validateSource($baseDir, $source); |
| 60 | $image->setSrc($this->secureEncode($baseDir, $source)); |
| 61 | } |
| 62 | |
| 63 | /** @var $object ObjectElement */ |
| 64 | foreach ($objects as $object) { |
| 65 | $data = $object->getData(); |
| 66 | $this->validateSource($baseDir, $data); |
| 67 | $object->setData($this->secureEncode($baseDir, $data)); |
| 68 | } |
| 69 | |
| 70 | // save the document to a tempfile |
| 71 | $newPassageXmlFilePath = tempnam(sys_get_temp_dir(), 'sharedStimulus_') . '.xml'; |
| 72 | $xmlDocument->save($newPassageXmlFilePath); |
| 73 | |
| 74 | return $newPassageXmlFilePath; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @throws InvalidSourcePathException |
| 79 | * @throws common_exception_Error |
| 80 | * @throws tao_models_classes_FileNotFoundException |
| 81 | */ |
| 82 | protected function validateSource(string $baseDir, string $sourcePath): void |
| 83 | { |
| 84 | $urlData = parse_url($sourcePath); |
| 85 | |
| 86 | if (!empty($urlData['scheme'])) { |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | if (!helpers_File::isFileInsideDirectory($sourcePath, $baseDir)) { |
| 91 | throw new InvalidSourcePathException($baseDir, $sourcePath); |
| 92 | } |
| 93 | |
| 94 | if (!tao_helpers_File::securityCheck($sourcePath, false)) { |
| 95 | throw new common_exception_Error('Invalid source path "' . $sourcePath . '"'); |
| 96 | } |
| 97 | |
| 98 | if (!file_exists($baseDir . $sourcePath)) { |
| 99 | throw new tao_models_classes_FileNotFoundException($sourcePath); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Build base64 binary if path is internal |
| 105 | */ |
| 106 | protected function secureEncode(string $basedir, string $source): string |
| 107 | { |
| 108 | $components = parse_url($source); |
| 109 | |
| 110 | if (!isset($components['scheme'])) { |
| 111 | return 'data:' . tao_helpers_File::getMimeType($basedir . $source) . ';' |
| 112 | . 'base64,' . base64_encode(file_get_contents($basedir . $source)); |
| 113 | } |
| 114 | |
| 115 | return $source; |
| 116 | } |
| 117 | } |