Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       0.00%  | 
       0 / 22  | 
               | 
       0.00%  | 
       0 / 1  | 
       CRAP |         | 
       0.00%  | 
       0 / 1  | 
      
| Datatypes |         | 
       0.00%  | 
       0 / 22  | 
               | 
       0.00%  | 
       0 / 1  | 
       42 |         | 
       0.00%  | 
       0 / 1  | 
      
| decodeFile |         | 
       0.00%  | 
       0 / 22  | 
               | 
       0.00%  | 
       0 / 1  | 
       42 | |||
| 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-2022 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); | 
| 19 | * | 
| 20 | */ | 
| 21 | |
| 22 | namespace oat\taoOutcomeUi\helper; | 
| 23 | |
| 24 | /** | 
| 25 | * A class focusing on providing utility methods for the various result datatypes | 
| 26 | * that might be sent/stored to/by a result server. | 
| 27 | * | 
| 28 | * @author Jérôme Bogaerts <jerome@taotesting.com> | 
| 29 | * | 
| 30 | */ | 
| 31 | class Datatypes | 
| 32 | { | 
| 33 | /** | 
| 34 | * Decode a binary string representing a file into an array. | 
| 35 | * | 
| 36 | * The binary string that is decoded must respect the following scheme: | 
| 37 | * | 
| 38 | * * filename length, unsigned short | 
| 39 | * * filename, string | 
| 40 | * * mimetype length, unsigned short | 
| 41 | * * mimetype, string | 
| 42 | * * binary content of the file, string | 
| 43 | * | 
| 44 | * The returned array contains tree cells with the following keys: | 
| 45 | * | 
| 46 | * * name, string (might be empty) | 
| 47 | * * mime, string | 
| 48 | * * data, string | 
| 49 | * | 
| 50 | * @param string $binary A binary string representing the file to be decoded. | 
| 51 | * @return array The decoded file as an array. | 
| 52 | */ | 
| 53 | public static function decodeFile($binary) | 
| 54 | { | 
| 55 | $returnValue = ['name' => '', 'mime' => '', 'data' => '']; | 
| 56 | |
| 57 | if (empty($binary)) { | 
| 58 | return $returnValue; | 
| 59 | } | 
| 60 | |
| 61 | $binaryStream = fopen('php://memory', 'r+'); | 
| 62 | fwrite($binaryStream, $binary); | 
| 63 | rewind($binaryStream); | 
| 64 | |
| 65 | $binaryLength = strlen($binary); | 
| 66 | $filenameLen = current(unpack('S', fread($binaryStream, 2))); | 
| 67 | |
| 68 | //validate case when unpacked filename length more or equal entire provided string | 
| 69 | // what mean that provided string definitely in incorrect format | 
| 70 | if ($filenameLen >= $binaryLength) { | 
| 71 | return $returnValue; | 
| 72 | } | 
| 73 | |
| 74 | if ($filenameLen > 0) { | 
| 75 | $returnValue['name'] = fread($binaryStream, $filenameLen); | 
| 76 | } | 
| 77 | |
| 78 | $packedMimeTypeLen = fread($binaryStream, 2); | 
| 79 | if ($packedMimeTypeLen === false) { | 
| 80 | return $returnValue; | 
| 81 | } | 
| 82 | |
| 83 | $unpackedMimeTypeLen = unpack('S', $packedMimeTypeLen); | 
| 84 | if ($unpackedMimeTypeLen === false) { | 
| 85 | return $returnValue; | 
| 86 | } | 
| 87 | |
| 88 | $mimetypeLen = current($unpackedMimeTypeLen); | 
| 89 | $returnValue['mime'] = fread($binaryStream, $mimetypeLen); | 
| 90 | $returnValue['data'] = stream_get_contents($binaryStream); | 
| 91 | |
| 92 | return $returnValue; | 
| 93 | } | 
| 94 | } |