480 public function unzipFile($filePath, $destination, $progressCallback =
null)
484 $sevenZipPath = $this->
getLibsPath() .
'/7zip/7za.exe';
486 if ( !file_exists( $sevenZipPath ) ) {
493 $testCommand = escapeshellarg( $sevenZipPath ) .
' t ' . escapeshellarg( $filePath ) .
' -y -bsp1';
494 $testOutput = shell_exec( $testCommand );
497 preg_match(
'/Files: (\d+)/', $testOutput, $matches );
498 $numFiles = isset( $matches[1] ) ? (int) $matches[1] : 0;
499 Util::logTrace(
'Number of files to be extracted: ' . $numFiles );
502 $command = escapeshellarg( $sevenZipPath ) .
' x ' . escapeshellarg( $filePath ) .
' -y -bsp1 -bb0 -o' . escapeshellarg( $destination );
505 $process = popen( $command,
'rb' );
509 while ( !feof( $process ) ) {
510 $buffer .= fread( $process, 8192 );
511 while ( ($pos = strpos( $buffer,
"\r" )) !==
false ) {
512 $line = substr( $buffer, 0, $pos );
513 $buffer = substr( $buffer, $pos + 1 );
514 $line = trim( $line );
518 if ( $line ===
"Everything is Ok" ) {
519 if ( $progressCallback ) {
521 call_user_func( $progressCallback, 100 );
522 Util::logTrace(
"Progress callback called with percentage: 100" );
525 else if ( $progressCallback && preg_match(
'/(?:^|\s)(\d+)%/', $line, $matches ) ) {
526 $currentPercentage = intval( $matches[1] );
528 call_user_func( $progressCallback, $currentPercentage );
529 Util::logTrace(
"Progress callback called with percentage: $currentPercentage" );
538 if ( !empty( $buffer ) ) {
539 $line = trim( $buffer );
543 if ( $line ===
"Everything is Ok" ) {
544 if ( $progressCallback ) {
546 call_user_func( $progressCallback, 100 );
547 Util::logTrace(
"Progress callback called with percentage: 100" );
550 else if ( $progressCallback && preg_match(
'/(?:^|\s)(\d+)%/', $line, $matches ) ) {
551 $currentPercentage = intval( $matches[1] );
553 call_user_func( $progressCallback, $currentPercentage );
554 Util::logTrace(
"Progress callback called with percentage: $currentPercentage" );
561 $returnVar = pclose( $process );
565 if ( $returnVar === 0 && $progressCallback ) {
566 Util::logTrace(
"Extraction completed successfully. Setting progress to 100%" );
567 call_user_func( $progressCallback, 100 );
568 Util::logTrace(
"Progress callback called with percentage: 100" );
574 if ( $returnVar === 0 ) {
575 Util::logDebug(
'Successfully unzipped file to: ' . $destination );
577 return [
'success' =>
true,
'numFiles' => $numFiles];
580 Util::logError(
'Failed to unzip file. Command return value: ' . $returnVar );
582 return [
'error' =>
'Failed to unzip file',
'numFiles' => $numFiles];
586 Util::logError(
'Failed to open process for command: ' . $command );
588 return [
'error' =>
'Failed to open process',
'numFiles' => $numFiles];
606 public function getFileFromUrl(
string $moduleUrl,
string $filePath, $progressBar =
false)
609 $inputStream = @fopen( $moduleUrl,
'rb' );
610 if ( $inputStream ===
false ) {
611 Util::logError(
'Error fetching content from URL: ' . $moduleUrl );
613 return [
'error' =>
'Error fetching module'];
617 $outputStream = @fopen( $filePath,
'wb' );
618 if ( $outputStream ===
false ) {
620 fclose( $inputStream );
622 return [
'error' =>
'Error saving module'];
629 while ( !feof( $inputStream ) ) {
630 $buffer = fread( $inputStream, $bufferSize );
631 fwrite( $outputStream, $buffer );
635 if ( $progressBar ) {
636 $progress = $chunksRead;
637 echo json_encode( [
'progress' => $progress] );
640 if ( ob_get_length() !==
false ) {
647 fclose( $inputStream );
648 fclose( $outputStream );
650 return [
'success' =>
true];