| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
if (!defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
// allow-no-test-found: covered by tests/LoggingTest.php zip-failure mail fallback entry-point test. |
| 9 |
|
| 10 |
/** |
| 11 |
* Builds the developer-feedback debug-log zip attachment. |
| 12 |
* |
| 13 |
* The builder only creates the archive from paths supplied by the caller. It |
| 14 |
* does not send mail and does not decide whether a missing/failed archive |
| 15 |
* should abort the report. |
| 16 |
*/ |
| 17 |
class ABJ_404_Solution_DebugLogArchiveBuilder { |
| 18 |
|
| 19 |
/** |
| 20 |
* Create a zip containing the current and rotated debug logs. |
| 21 |
* |
| 22 |
* @param string $zipPath |
| 23 |
* @param string $debugFilePath |
| 24 |
* @param string $oldDebugFilePath |
| 25 |
* @return string The requested zip path, or an empty string when ZipArchive is unavailable. |
| 26 |
*/ |
| 27 |
public function build(string $zipPath, string $debugFilePath, string $oldDebugFilePath): string { |
| 28 |
if (file_exists($zipPath)) { |
| 29 |
ABJ_404_Solution_FileSystemService::safeUnlink($zipPath); |
| 30 |
} |
| 31 |
if (!class_exists('ZipArchive')) { |
| 32 |
return ''; |
| 33 |
} |
| 34 |
$zipDirectory = dirname($zipPath); |
| 35 |
if (!is_dir($zipDirectory)) { |
| 36 |
abj404_logPhpFallback('logger-internal', 'debug log zip directory does not exist: ' . $zipDirectory); |
| 37 |
return $zipPath; |
| 38 |
} |
| 39 |
$zip = new ZipArchive; |
| 40 |
$openResult = $zip->open($zipPath, ZipArchive::CREATE); |
| 41 |
if ($openResult === true) { |
| 42 |
if (file_exists($debugFilePath)) { |
| 43 |
$zip->addFile($debugFilePath, basename($debugFilePath)); |
| 44 |
} |
| 45 |
if (file_exists($oldDebugFilePath)) { |
| 46 |
$zip->addFile($oldDebugFilePath, basename($oldDebugFilePath)); |
| 47 |
} |
| 48 |
if (!$zip->close()) { |
| 49 |
abj404_logPhpFallback('logger-internal', 'debug log zip close failed for ' . $zipPath); |
| 50 |
} |
| 51 |
} else { |
| 52 |
abj404_logPhpFallback( |
| 53 |
'logger-internal', |
| 54 |
'debug log zip open failed for ' . $zipPath . ' (status ' . (string)$openResult . ')' |
| 55 |
); |
| 56 |
} |
| 57 |
return $zipPath; |
| 58 |
} |
| 59 |
} |
| 60 |
|