Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Archive Zip: The execution failed. Please check error.log (Solved) #4388

Open
ahrana opened this issue Oct 22, 2024 · 2 comments
Open

Archive Zip: The execution failed. Please check error.log (Solved) #4388

ahrana opened this issue Oct 22, 2024 · 2 comments

Comments

@ahrana
Copy link

ahrana commented Oct 22, 2024

if you can't take backup using backups and demo extension, then use my below modified code, it will work and you can take backup.

screencapture-dev-cxrana-pantheonsite-io-wp-admin-tools-php-2024-10-22-23_12_35

File Name : class-fw-ext-backups-task-type-zip.php

Go to the file /code/wp-content/plugins/unyson/framework/extensions/backups/includes/module/tasks/type/class-fw-ext-backups-task-type-zip.php.
Look at line 120, where ZipArchive->setCompressionName() is called.

You can fix the issue in the FW_Ext_Backups_Task_Type_Zip class by adding a validation check to ensure the compression name is not empty when calling the setCompressionName() method

below is my full modified file, just replace the file:

`<?php if (!defined('FW')) die('Forbidden');

/**

  • Create zip
    */
    class FW_Ext_Backups_Task_Type_Zip extends FW_Ext_Backups_Task_Type {
    public function get_type() {
    return 'zip';
    }

    public function get_title(array $args = array(), array $state = array()) {
    return __('Archive Zip', 'fw');
    }

    /**

    • When the zip is big, adding just a single file will recompile the entire zip.
    • So it can't be executed in steps.
    • {@inheritdoc}
      */
      public function get_custom_timeout(array $args, array $state = array()) {
      return fw_ext('backups')->get_config('max_timeout');
      }

    /**

    • {@inheritdoc}

    • @param array $args

      • source_dir - everything from this directory will be added in zip
      • destination_dir - where the zip file will be created
    • Warning!

    • Zip can't be executed in steps, it will execute way too long,

    • because it is impossible to update a zip file, every time you add a file to zip,

    • a new temp copy of original zip is created with new modifications, it is compressed,

    • and the original zip is replaced. So when the zip will grow in size,

    • just adding a single file, will take a very long time.
      */
      public function execute(array $args, array $state = array()) {
      {
      if (!isset($args['source_dir'])) {
      return new WP_Error(
      'no_source_dir', __('Source dir not specified', 'fw')
      );
      } elseif (!file_exists($args['source_dir'] = fw_fix_path($args['source_dir']))) {
      return new WP_Error(
      'invalid_source_dir', __('Source dir does not exist', 'fw')
      );
      }

      if (!isset($args['destination_dir'])) {
          return new WP_Error(
              'no_destination_dir', __('Destination dir not specified', 'fw')
          );
      } else {
          $args['destination_dir'] = fw_fix_path($args['destination_dir']);
      }
      

      }

      if (empty($state)) {
      $state = array(
      'files_count' => 0,
      // generate the file name only on first step
      'zip_path' => $args['source_dir'] .'/'. implode('-', array(
      'fw-backup',
      date('Y_m_d-H_i_s'),
      fw_ext('backups')->manifest->get_version()
      )) .'.zip'
      );
      }

      {
      if (!class_exists('ZipArchive')) {
      return new WP_Error(
      'zip_ext_missing', __('Zip extension missing', 'fw')
      );
      }

      $zip = new ZipArchive();
      
      if (false === ($zip_error_code = $zip->open($state['zip_path'], ZipArchive::CREATE))) {
          return new WP_Error(
              'cannot_open_zip', sprintf(__('Cannot open zip (Error code: %s)', 'fw'), $zip_error_code)
          );
      }
      

      }

      /** @var FW_Extension_Backups $ext /
      $ext = fw_ext('backups');
      /
      *

      • Files for zip (in pending) are added very fast
      • but on zip close the processing/zipping starts and it is time consuming
      • so allocate little time for files add and leave as much time as possible for $zip->close();
        */
        $max_time = time() + min( abs( $ext->get_timeout() / 2 ), 10 );
        $set_compression_is_available = method_exists( $zip, 'setCompressionName' );
        $files = array_slice( $this->get_all_files( $args['source_dir'] ), $state['files_count'] );

      foreach ($files as $file) {
      if ($execution_not_finished = (time() > $max_time)) {
      break;
      }

      ++$state['files_count'];
      
      $file_path = fw_fix_path($file->getRealPath());
      $file_zip_path = substr($file_path, strlen($args['source_dir']) + 1); // relative
      
      if (
          $file->isDir() // Skip directories (they will be created automatically)
          ||
          $file_path === $state['zip_path'] // this happens when zip exists from previous step
          ||
          preg_match("|^".$args['destination_dir']."/.+\\.zip\$|", $file_path) // this happens when it's a old backup zip
      ) {
          continue;
      }
      
      $zip->addFile($file_path, $file_zip_path);
      
      if ( $set_compression_is_available ) {
          // Ensure compression name is not empty before setting
          $compression_method = apply_filters( 'fw_backup_compression_method', ZipArchive::CM_DEFAULT );
          if (!empty($file_zip_path) && $compression_method !== false) {
              $zip->setCompressionName( $file_zip_path, $compression_method );
          }
      }
      

      }

      // Zip archive will be created only after closing the object
      if ( ! $zip->close() ) {
      return new WP_Error(
      'cannot_close_zip', __( 'Cannot close the zip file', 'fw' )
      );
      }

      if ( $execution_not_finished ) {
      // There are more files to be processed, the execution hasn't finished
      return $state;
      }

      /**

      • Happens on Content Backup when uploads/ is empty
        */
        if ( ! $files ) {
        return true;
        }

      if (!rename($state['zip_path'], $args['destination_dir'] .'/'. basename($state['zip_path']))) {
      return new WP_Error(
      'cannot_move_zip',
      __('Cannot move zip in destination dir', 'fw')
      );
      }

      return true;
      }

    public function get_all_files( $source_dir ) {

     static $all_files = array();
    
     if ( $all_files ) {
         return $all_files;
     }
    
     $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $source_dir ), RecursiveIteratorIterator::LEAVES_ONLY );
    
     foreach ( $files as $file ) {
         $all_files[] = $file;
     }
    
     return $all_files;
    

    }
    }`

@ahrana
Copy link
Author

ahrana commented Oct 22, 2024

### Look at line 120, where ZipArchive->setCompressionName() is called

In the line where setCompressionName() is called, I've added a check to ensure that both the file path and compression method are not empty before attempting to set the compression

Modified Line: 120 no line

 if ( $set_compression_is_available ) {
                // Ensure compression name is not empty before setting
                $compression_method = apply_filters( 'fw_backup_compression_method', ZipArchive::CM_DEFAULT );
                if (!empty($file_zip_path) && $compression_method !== false) {
                    $zip->setCompressionName( $file_zip_path, $compression_method );
                }
            }
        }

        // Zip archive will be created only after closing the object

@ahrana
Copy link
Author

ahrana commented Nov 21, 2024

if you need any Unyson errors quick help, email me, i will fix within 10 minutes, you can pay little bonus.

thanks

[email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@ahrana and others