function _import_file($file)
 {
     if ($this->had_error()) {
         return false;
     }
     if (!is_file($file)) {
         $this->add_error(new WP_Error('builder-import-export-no-file', __('Unable to find requested export file', 'it-l10n-Builder-Cohen')));
         return false;
     }
     it_classes_load('it-file-utility.php');
     it_classes_load('it-zip.php');
     $path = ITFileUtility::create_writable_directory(array('name' => 'deleteme-builder-import-export-upload-temp', 'random' => true));
     if (is_wp_error($path)) {
         $this->add_error($path);
         return false;
     }
     $result = ITZip::unzip($file, $path);
     if (is_wp_error($result) || !is_file("{$path}/info.txt") || !is_dir("{$path}/data-sources")) {
         ITFileUtility::delete_directory($path);
         $this->add_error(new WP_Error('builder-import-export-invalid-export-format', __('The uploaded file is not a valid Builder export file. It cannot be imported.', 'it-l10n-Builder-Cohen')));
         return false;
     }
     $info = $this->_get_file_content("{$path}/info.txt");
     $zip = new ITZip();
     $export_path = $this->_get_export_path($info['guid'], $info['name']);
     $new_guid = false;
     if (is_wp_error($export_path)) {
         $info['guid'] = $this->_generate_guid();
         if (false === $info['guid']) {
             return false;
         }
         $export_path = $this->_get_export_path($info['guid'], $info['name']);
         $new_guid = true;
     }
     if (true === $new_guid) {
         $this->_add_zip_file_content($zip, 'info.txt', $info);
     }
     $files = ITFileUtility::get_flat_file_listing($path);
     foreach ((array) $files as $file) {
         if (true === $new_guid && 'info.txt' === $file) {
             continue;
         }
         $dir = dirname($file);
         $dir = '.' === $dir ? '' : "{$dir}/";
         $zip->add_file("{$path}/{$file}", $dir);
     }
     $file = $zip->create_zip(array('file' => $export_path));
     ITFileUtility::delete_directory($path);
     return $info['guid'];
 }
示例#2
0
 public static function unzip($file, $path, $args = array())
 {
     if (!is_file($file)) {
         return new WP_Error('it_zip_load_no_file', 'Unable to find the requested file to be unzipped.');
     }
     if (!is_writable($path)) {
         return new WP_Error('it_zip_load_no_writable_path', 'Unable to write to destination path for zip file content.');
     }
     $default_args = array('force_compatibility' => false);
     $args = array_merge($default_args, $args);
     if (true !== $args['force_compatibility']) {
         $result = ITZip::_load_native_zip($file, $path, $args);
     }
     if (true !== $result) {
         $result = ITZip::_load_compatibility_zip($file, $path, $args);
     }
     if (is_wp_error($result)) {
         return $result;
     } else {
         if (true !== $result) {
             return new WP_Error('unknown_zip_failure', 'Zip file loading failed for an unknown reason');
         }
     }
     return true;
 }