/** * add_directory_to_zip() * * Adds a directory to a new or existing (TODO: not yet available) ZIP file. * * @param string Full path & filename of ZIP file to create. * @param string Full directory to add to zip file. * @param array( string ) Array of strings of paths/files to exclude from zipping * @param string Full directory path to directory to temporarily place ZIP * @param boolean True: only use PCLZip. False: try all available * * @return true on success, false otherwise * */ public function add_directory_to_zip($zip_file, $add_directory, $excludes = array(), $temporary_zip_directory = '') { if (true === $this->_is_experimental) { pb_backupbuddy::status('message', __('Running alternative ZIP system (BETA) based on settings.', 'it-l10n-backupbuddy')); } else { pb_backupbuddy::status('message', __('Running standard ZIP system based on settings.', 'it-l10n-backupbuddy')); } // Let's just log if this is a 32 or 64 bit system $php_size = pluginbuddy_stat::is_php(pluginbuddy_stat::THIRTY_TWO_BIT) ? "32" : "64"; pb_backupbuddy::status('details', sprintf(__('Running under %1$s-bit PHP', 'it-l10n-backupbuddy'), $php_size)); // Make sure we tell what the sapi is pb_backupbuddy::status('details', sprintf(__('Server API: %1$s', 'it-l10n-backupbuddy'), $this->get_sapi_name())); $zip_methods = array(); $sanitized_excludes = array(); $listmaker = NULL; // Set some additional system excludes here for now - these are all from the site install root $additional_excludes = array(self::NORM_DIRECTORY_SEPARATOR . 'importbuddy' . self::NORM_DIRECTORY_SEPARATOR, self::NORM_DIRECTORY_SEPARATOR . 'importbuddy.php', self::NORM_DIRECTORY_SEPARATOR . 'wp-content' . self::NORM_DIRECTORY_SEPARATOR . 'uploads' . self::NORM_DIRECTORY_SEPARATOR . 'pb_backupbuddy' . self::NORM_DIRECTORY_SEPARATOR); // Make sure we have a valid zip method strategy setting to use otherwise fall back to emergency compatibility if (isset(pb_backupbuddy::$options['zip_method_strategy']) && '0' !== pb_backupbuddy::$options['zip_method_strategy']) { $zip_method_strategy = pb_backupbuddy::$options['zip_method_strategy']; switch ($zip_method_strategy) { case "1": // Best Available $zip_methods = $this->get_best_zip_methods(array('is_archiver')); pb_backupbuddy::status('details', __('Using Best Available zip method based on settings.', 'it-l10n-backupbuddy')); break; case "2": // All Available $zip_methods = $this->_zip_methods; pb_backupbuddy::status('details', __('Using All Available zip methods in preferred order based on settings.', 'it-l10n-backupbuddy')); break; case "3": // Force Compatibility $zip_methods = $this->get_compatibility_zip_methods(); pb_backupbuddy::status('message', __('Using Forced Compatibility zip method based on settings.', 'it-l10n-backupbuddy')); break; default: // Hmm...unrecognized value - emergency compatibility $zip_methods = $this->get_compatibility_zip_methods(); pb_backupbuddy::status('message', sprintf(__('Forced Compatibility Mode as Zip Method Strategy setting not recognized: %1$s', 'it-l10n-backupbuddy'), $zip_method_strategy)); } } else { // We got no or an invalid zip method strategy which is a bad situation - emergency compatibility is the order of the day $zip_methods = $this->get_compatibility_zip_methods(); pb_backupbuddy::status('message', __('Forced Compatibility Mode as Zip Method Strategy not set or setting not recognized.', 'it-l10n-backupbuddy')); } // Better make sure we have some available methods if (empty($zip_methods)) { // Hmm, we don't seem to have any available methods, oops, best go no further pb_backupbuddy::status('details', __('Failed to create a Zip Archive file - no available methods.', 'it-l10n-backupbuddy')); // We should have a temporary directory, must get rid of it, can simply rmdir it as it will (should) be empty if (!empty($temporary_zip_directory) && file_exists($temporary_zip_directory)) { if (!rmdir($temporary_zip_directory)) { pb_backupbuddy::status('details', __('Temporary directory could not be deleted: ', 'it-l10n-backupbuddy') . $temporary_zip_directory); } } return false; } pb_backupbuddy::status('details', __('Creating ZIP file', 'it-l10n-backupbuddy') . ' `' . $zip_file . '`. ' . __('Adding directory', 'it-l10n-backupbuddy') . ' `' . $add_directory . '`. ' . __('Excludes', 'it-l10n-backupbuddy') . ': ' . implode(',', $excludes)); // We'll try and allow exclusions for pclzip if we can include_once pb_backupbuddy::plugin_path() . '/lib/' . $this->_whereami . '/zbdir.php'; if (class_exists('pluginbuddy_zbdir')) { // Generate our sanitized list of directories/files to exclude as absolute paths (normalized) for zbdir $sanitized_excludes = $this->sanitize_excludes($excludes, $additional_excludes, $add_directory); // Now let's create the list of items to add to the zip - first build the tree $listmaker = new pluginbuddy_zbdir($add_directory, $sanitized_excludes); // Re-generate our sanitized list of directories/files to exclude as relative paths // Slight kludge to deal with being able to enable/disable the inclusion processing // (currently configured in wp-config.php) so always need to provide the excludes as // relative path for now. This needs to be tidied up in future if/when the capability // is established as standard $sanitized_excludes = $this->sanitize_excludes($excludes, $additional_excludes); } else { // Generate our sanitized list of directories/files to exclude as relative paths $sanitized_excludes = $this->sanitize_excludes($excludes, $additional_excludes); } // Iterate over the methods - once we succeed just return directly otherwise drop through foreach ($zip_methods as $method_tag) { // First make sure we can archive with this method if ($this->_zip_methods_details[$method_tag]['attr']['is_archiver'] === true) { $class_name = 'pluginbuddy_zbzip' . $method_tag; $zipper = new $class_name($this); $zipper->set_status_callback(array(&$this, 'status')); // We need to tell the method what details belong to it $zipper->set_method_details($this->_zip_methods_details[$method_tag]); // Tell the method the server api in use $zipper->set_sapi_name($this->get_sapi_name()); pb_backupbuddy::status('details', __('Trying ', 'it-l10n-backupbuddy') . $method_tag . __(' method for ZIP.', 'it-l10n-backupbuddy')); // As we are looping make sure we have no stale file information clearstatcache(); // The temporary zip directory _must_ exist if (!empty($temporary_zip_directory)) { if (!file_exists($temporary_zip_directory)) { // Create temp dir if it does not exist. mkdir($temporary_zip_directory); } } // Now we are ready to try and produce the backup if ($zipper->create($zip_file, $add_directory, $sanitized_excludes, $temporary_zip_directory, $listmaker) === true) { // Got a valid zip file so we can just return - method will have cleaned up the temporary directory pb_backupbuddy::status('details', __('The ', 'it-l10n-backupbuddy') . $method_tag . __(' method for ZIP was successful.', 'it-l10n-backupbuddy')); unset($zipper); // We have to return here because we cannot break out of foreach return true; } else { // Method will have cleaned up the temporary directory pb_backupbuddy::status('details', __('The ', 'it-l10n-backupbuddy') . $method_tag . __(' method for ZIP was unsuccessful.', 'it-l10n-backupbuddy')); unset($zipper); } } else { // This method is not considered suitable (reliable enough) for creating archives or lacked zip capability pb_backupbuddy::status('details', __('The ', 'it-l10n-backupbuddy') . $method_tag . __(' method is not currently supported for backup.', 'it-l10n-backupbuddy')); } } // If we get here then have failed in all attempts pb_backupbuddy::status('details', __('Failed to create a Zip Archive file with any nominated method.', 'it-l10n-backupbuddy')); return false; }
/** * add_directory_to_zip() * * Adds a directory to a new or existing (TODO: not yet available) ZIP file. * * @param string $zip_file Full path & filename of ZIP file to create. * @param string $add_directory Full directory to add to zip file. * @param array $excludes Array of strings of paths/files to exclude from zipping * @param string $temporary_zip_directory Full directory path to directory to temporarily place ZIP * * @return true on success, false otherwise * */ public function add_directory_to_zip($zip_file, $add_directory, $excludes = array(), $temporary_zip_directory = '') { if (true === $this->_is_experimental) { $this->log('message', __('Running alternative ZIP system (BETA) based on settings.', 'it-l10n-backupbuddy')); } else { $this->log('message', __('Running standard ZIP system based on settings.', 'it-l10n-backupbuddy')); } // Let's just log if this is a 32 or 64 bit system $php_size = pluginbuddy_stat::is_php(pluginbuddy_stat::THIRTY_TWO_BIT) ? "32" : "64"; $this->log('details', sprintf(__('Running under %1$s-bit PHP', 'it-l10n-backupbuddy'), $php_size)); // Make sure we tell what the sapi is $this->log('details', sprintf(__('Server API: %1$s', 'it-l10n-backupbuddy'), $this->get_sapi_name())); $zip_methods = array(); $sanitized_excludes = array(); // Set some additional system excludes here for now - these are all from the site install root $additional_excludes = array(self::NORM_DIRECTORY_SEPARATOR . 'importbuddy' . self::NORM_DIRECTORY_SEPARATOR, self::NORM_DIRECTORY_SEPARATOR . 'importbuddy.php', self::NORM_DIRECTORY_SEPARATOR . 'wp-content' . self::NORM_DIRECTORY_SEPARATOR . 'uploads' . self::NORM_DIRECTORY_SEPARATOR . 'pb_backupbuddy' . self::NORM_DIRECTORY_SEPARATOR); // Make sure we have a valid zip method strategy setting to use otherwise fall back to emergency compatibility if (isset(pb_backupbuddy::$options['zip_method_strategy']) && '0' !== pb_backupbuddy::$options['zip_method_strategy']) { $zip_method_strategy = pb_backupbuddy::$options['zip_method_strategy']; switch ($zip_method_strategy) { case "1": // Best Available $zip_methods = $this->get_best_zip_methods(array('is_archiver')); $this->log('details', __('Using Best Available zip method based on settings.', 'it-l10n-backupbuddy')); break; case "2": // All Available $zip_methods = $this->_zip_methods; $this->log('details', __('Using All Available zip methods in preferred order based on settings.', 'it-l10n-backupbuddy')); break; case "3": // Force Compatibility $zip_methods = $this->get_compatibility_zip_methods(); $this->log('message', __('Using Forced Compatibility zip method based on settings.', 'it-l10n-backupbuddy')); break; default: // Hmm...unrecognized value - emergency compatibility $zip_methods = $this->get_compatibility_zip_methods(); $this->log('message', sprintf(__('Forced Compatibility Mode as Zip Method Strategy setting not recognized: %1$s', 'it-l10n-backupbuddy'), $zip_method_strategy)); } } else { // We got no or an invalid zip method strategy which is a bad situation - emergency compatibility is the order of the day $zip_methods = $this->get_compatibility_zip_methods(); $this->log('message', __('Forced Compatibility Mode as Zip Method Strategy not set or setting not recognized.', 'it-l10n-backupbuddy')); } // Better make sure we have some available methods if (empty($zip_methods)) { // Hmm, we don't seem to have any available methods, oops, best go no further $this->log('details', __('Failed to create a Zip Archive file - no available methods.', 'it-l10n-backupbuddy')); // We should have a temporary directory, must get rid of it, can simply rmdir it as it will (should) be empty if (!empty($temporary_zip_directory) && file_exists($temporary_zip_directory)) { if (!rmdir($temporary_zip_directory)) { $this->log('details', __('Temporary directory could not be deleted: ', 'it-l10n-backupbuddy') . $temporary_zip_directory); } } return false; } $this->log('details', __('Creating ZIP file', 'it-l10n-backupbuddy') . ' `' . $zip_file . '`. ' . __('Adding directory', 'it-l10n-backupbuddy') . ' `' . $add_directory . '`. ' . __('Excludes', 'it-l10n-backupbuddy') . ': ' . implode(',', $excludes)); // We need the classes for being able to build backup file list require_once pb_backupbuddy::plugin_path() . '/lib/' . $this->_whereami . '/zbdir.php'; if (!class_exists('pluginbuddy_zbdir')) { // Hmm, require_once() didn't bomb but we haven't got the class we expect - bail out $this->log('details', __('Unable to load classes for backup file list builder.', 'it-l10n-backupbuddy')); return false; } // Generate our sanitized list of directories/files to exclude as relative paths $sanitized_excludes = $this->sanitize_excludes($excludes, $additional_excludes); // Do the same for directories/files to include //$sanitized_includes = $this->sanitize_excludes( $includes, $additional_includes ); // Iterate over the methods - once we succeed just return directly otherwise drop through foreach ($zip_methods as $method_tag) { // First make sure we can archive with this method if ($this->_zip_methods_details[$method_tag]['attr']['is_archiver'] === true) { $class_name = 'pluginbuddy_zbzip' . $method_tag; // Zipper will initially inherit our logger and our // process monitor $zipper = new $class_name($this); // Now override logger - will define a prefix here $zipper->set_logger($this->_default_child_logger); // Set these on specific zipper based on the values we derived at construnction or // overridden by subsequent method calls $zipper->set_compression($this->get_compression()); $zipper->set_ignore_symlinks($this->get_ignore_symlinks()); $zipper->set_ignore_warnings($this->get_ignore_warnings()); $zipper->set_step_period($this->get_step_period()); $zipper->set_burst_gap($this->get_burst_gap()); $zipper->set_min_burst_content($this->get_min_burst_content()); $zipper->set_max_burst_content($this->get_max_burst_content()); // We need to tell the method what details belong to it $zipper->set_method_details($this->_zip_methods_details[$method_tag]); // Tell the method the server api in use $zipper->set_sapi_name($this->get_sapi_name()); $this->log('details', __('Trying ', 'it-l10n-backupbuddy') . $method_tag . __(' method for ZIP.', 'it-l10n-backupbuddy')); // As we are looping make sure we have no stale file information clearstatcache(); // The temporary zip directory _must_ exist if (!empty($temporary_zip_directory)) { if (!file_exists($temporary_zip_directory)) { // Create temp dir if it does not exist. mkdir($temporary_zip_directory); } } // Now we are ready to try and produce the backup if (true === ($result = $zipper->create($zip_file, $add_directory, $sanitized_excludes, $temporary_zip_directory))) { // Got a valid zip file so we can just return - method will have cleaned up the temporary directory $this->log('details', __('The ', 'it-l10n-backupbuddy') . $method_tag . __(' method for ZIP was successful.', 'it-l10n-backupbuddy')); unset($zipper); // We have to return here because we cannot break out of foreach return true; } elseif (is_array($result)) { // Didn't finish zip creation on that step so we need to set up for another step // Add in any addiitonal state information and simply return the state $this->log('details', __('The ', 'it-l10n-backupbuddy') . $method_tag . __(' method for ZIP partially completed.', 'it-l10n-backupbuddy')); unset($zipper); return $result; } else { // We failed on the first step for one eason or another - may be an option // to try with another method... // Method will have cleaned up the temporary directory $this->log('details', __('The ', 'it-l10n-backupbuddy') . $method_tag . __(' method for ZIP was unsuccessful.', 'it-l10n-backupbuddy')); unset($zipper); } } else { // This method is not considered suitable (reliable enough) for creating archives or lacked zip capability $this->log('details', __('The ', 'it-l10n-backupbuddy') . $method_tag . __(' method is not currently supported for backup.', 'it-l10n-backupbuddy')); } } // If we get here then have failed in all attempts $this->log('details', __('Failed to create a Zip Archive file with any nominated method.', 'it-l10n-backupbuddy')); return false; }