Skip to content

Commit fc68b05

Browse files
committed
ITST-15927
- Add additional checks of the used temporary directory to uncover potential issues earlier. - Add additional error information to failed ZipArchive::extractTo() calls.
1 parent 3d718f6 commit fc68b05

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

lib/Reader.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ public function open($file_path)
241241
throw new RuntimeException('XLSXReader: File not readable (' . $file_path . ')');
242242
}
243243

244+
if (!mkdir($this->temp_dir, 0777, true) || !file_exists($this->temp_dir)) {
245+
throw new RuntimeException(
246+
'XLSXReader: Could neither create nor confirm existance of temporary directory (' . $this->temp_dir . ')'
247+
);
248+
}
249+
244250
$zip = new ZipArchive;
245251
$status = $zip->open($file_path);
246252
if ($status !== true) {
@@ -1035,7 +1041,10 @@ private function initWorksheets(ZipArchive $zip)
10351041
$worksheet_path_zip = $worksheet->getOriginalPath();
10361042
$worksheet_path_conv = str_replace(RelationshipData::ZIP_DIR_SEP, DIRECTORY_SEPARATOR, $worksheet_path_zip);
10371043
$worksheet_path_unzipped = $this->temp_dir . $worksheet_path_conv;
1038-
$zip->extractTo($this->temp_dir, $worksheet_path_zip);
1044+
if (!$zip->extractTo($this->temp_dir, $worksheet_path_zip)) {
1045+
$message = 'XLSXReader: Could not extract file [' . $worksheet_path_zip . '] to directory [' . $this->temp_dir . '].';
1046+
$this->reportZipExtractionFailure($zip, $message);
1047+
}
10391048
$worksheet->setAccessPath($worksheet_path_unzipped);
10401049
$this->temp_files[] = $worksheet_path_unzipped;
10411050
}
@@ -1072,7 +1081,11 @@ private function initSharedStrings(
10721081
$path_to_extracted_file = $dir_of_extracted_file . $filename_of_extracted_file;
10731082

10741083
// Extract file and note it in relevant variables
1075-
$zip->extractTo($this->temp_dir, $inzip_path);
1084+
if (!$zip->extractTo($this->temp_dir, $inzip_path)) {
1085+
$message = 'XLSXReader: Could not extract file [' . $inzip_path . '] to directory [' . $this->temp_dir . '].';
1086+
$this->reportZipExtractionFailure($zip, $message);
1087+
}
1088+
10761089
$first_shared_string_element->setAccessPath($path_to_extracted_file);
10771090
$this->temp_files[] = $path_to_extracted_file;
10781091

@@ -1087,6 +1100,7 @@ private function initSharedStrings(
10871100
$this->temp_files = array_merge($this->temp_files, $this->shared_strings->getTempFiles());
10881101
}
10891102
}
1103+
10901104
/**
10911105
* Reads and prepares information on styles declared by the document for later usage.
10921106
*
@@ -1182,4 +1196,28 @@ private function deleteTempfiles() {
11821196
@rmdir($this->temp_dir);
11831197
}
11841198
}
1199+
1200+
/**
1201+
* Gather data on zip extractTo() fault and throw an appropriate Exception.
1202+
*
1203+
* @param ZipArchive $zip
1204+
* @param string $message Optional error message to prefix the error details with.
1205+
*
1206+
* @throws RuntimeException
1207+
*/
1208+
private function reportZipExtractionFailure($zip, $message = '')
1209+
{
1210+
$status_code = $zip->status;
1211+
$status_message = $zip->getStatusString();
1212+
if ($status_code || $status_message) {
1213+
$message .= ' Status from ZipArchive:';
1214+
if ($status_code) {
1215+
$message .= ' Code [' . $status_code . '];';
1216+
}
1217+
if ($status_message) {
1218+
$message .= ' Message [' . $status_message . '];';
1219+
}
1220+
}
1221+
throw new RuntimeException($message);
1222+
}
11851223
}

0 commit comments

Comments
 (0)