Exemplo n.º 1
0
 /**
  *	file_exists()
  *	
  *	Tests whether a file (with path) exists in the given zip file
  *	If leave_open is true then the zip object will be left open for faster checking for subsequent files within this zip
  *	
  *	@param		string	$zip_file		The zip file to check
  *	@param		string	$locate_file	The file to test for
  *	@param		bool	$leave_open		Optional: True if the zip file should be left open
  *	@return		bool/array				True if the file is found in the zip and false if not, array for other problem
  *
  */
 public function file_exists($zip_file, $locate_file, $leave_open = false)
 {
     $result = false;
     $za = NULL;
     // This should give us a new archive object, of not catch it and bail out
     try {
         $za = new pluginbuddy_ZipArchive();
         $result = true;
     } catch (Exception $e) {
         // Something fishy - the methods indicated ziparchive but we couldn't find the class
         $error_string = $e->getMessage();
         pb_backupbuddy::status('details', sprintf(__('ziparchive indicated as available method but error reported: %1$s', 'it-l10n-backupbuddy'), $error_string));
         $result = false;
     }
     // Only continue if we have a valid archive object
     if (true === $result) {
         $result = $za->open($zip_file);
         // Make sure we opened the zip ok
         if (true === $result) {
             // Now try and find the index of the file
             $index = $za->locateName($locate_file);
             // If we got an index we found it otherwise not found
             if (false !== $index) {
                 pb_backupbuddy::status('details', __('File found (ziparchive)', 'it-l10n-backupbuddy') . ': ' . $locate_file);
                 $result = true;
             } else {
                 pb_backupbuddy::status('details', __('File not found (ziparchive)', 'it-l10n-backupbuddy') . ': ' . $locate_file);
                 $result = false;
             }
         } else {
             // Couldn't open archive - will return for maybe another method to try
             $error_string = $za->errorInfo($result);
             pb_backupbuddy::status('details', sprintf(__('ZipArchive failed to open file to check if file exists (looking for %1$s in %2$s) - Error Info: %3$s.', 'it-l10n-backupbuddy'), $locate_file, $zip_file, $error_string));
             // Return an error code and a description - this needs to be handled more generically
             $result = array(1, "Unable to get archive contents");
         }
         // We have finished with the archive (leave_open ignored for now)
         $za->close();
     }
     if (NULL != $za) {
         unset($za);
     }
     return $result;
 }