web/wp-content/plugins/duplicator/inc/class.zip.php
changeset 195 c7c0fbc09788
child 204 09a1c134465b
equal deleted inserted replaced
194:32102edaa81b 195:c7c0fbc09788
       
     1 <?php 
       
     2 
       
     3 class Duplicator_Zip 
       
     4 {
       
     5 	protected $limit = DUPLICATOR_ZIP_FILE_POOL;
       
     6 	protected $zipArchive;
       
     7 	protected $rootFolder;
       
     8 	protected $skipNames;
       
     9 	protected $zipFilePath;
       
    10 	private   $limitItems = 0;
       
    11 	
       
    12 	/**
       
    13 	 *  DUPLICATOR ZIP
       
    14 	 *  Creates the zip file
       
    15 	 *
       
    16 	 *  @param string $zipFilePath	The full path to the zip file that will be made
       
    17 	 *  @param string $folderPath	The folder that will be zipped
       
    18 	 *  @param string $sqlfilepath	The path to the database file to include in the package
       
    19 	 */
       
    20 	function __construct($zipFilePath, $folderPath, $sqlfilepath) {
       
    21 		try 
       
    22 		{
       
    23 		
       
    24 			duplicator_log("log:class.zip=>started");
       
    25 			duplicator_log("archive this folder: {$folderPath}");
       
    26 			duplicator_log("archive file name:   {$zipFilePath}");
       
    27 			
       
    28 			$this->zipArchive  = new ZipArchive();
       
    29 			$this->zipFilePath = duplicator_safe_path($zipFilePath);
       
    30 			$this->rootFolder  = rtrim(duplicator_safe_path($folderPath), '/');
       
    31 			$this->skipNames   = $GLOBALS['duplicator_skip_ext-array'];
       
    32 			
       
    33 			$exts_list = implode(";", $this->skipNames);
       
    34 			$path_list = implode(";", $GLOBALS['duplicator_bypass-array']);
       
    35 			duplicator_log("filter file extensions: '{$exts_list}'");
       
    36 			duplicator_log("filter directory paths: '{$path_list}'");
       
    37 			
       
    38 
       
    39 			if ($this->zipArchive->open($this->zipFilePath, ZIPARCHIVE::CREATE) === TRUE) {
       
    40 				duplicator_log("zipArchive opened");
       
    41 			} 
       
    42 			else {
       
    43 				$err = "cannot open <{$this->zipFilePath}>";
       
    44 				duplicator_log($err);
       
    45 				throw new Exception($err);
       
    46 			}
       
    47 			
       
    48 			//Recursive Call
       
    49 			$this->resursiveZip($this->rootFolder);
       
    50 			
       
    51 			//ADD SQL File
       
    52 			$this->zipArchive->addFile($sqlfilepath, "database.sql");
       
    53 
       
    54 			duplicator_log("archive info: " . print_r($this->zipArchive, true));
       
    55 			duplicator_log("close returned: " . $this->zipArchive->close() . " (if null check your disk quota)" );
       
    56 			duplicator_log("log:class.zip=>ended");
       
    57 		} 
       
    58 		catch(Exception $e) 
       
    59 		{
       
    60 			duplicator_log("log:class.zip=>runtime error: " . $e);
       
    61 		}
       
    62 	}
       
    63 	
       
    64 	
       
    65 	function resursiveZip($directory)
       
    66 	{
       
    67 		try 
       
    68 		{
       
    69 			$folderPath = duplicator_safe_path($directory);
       
    70 			if(!$dh = opendir($folderPath)) {
       
    71 				return false;
       
    72 			}
       
    73 			
       
    74 			//EXCLUDE: Snapshot directory
       
    75 			if( strstr($folderPath, DUPLICATOR_SSDIR_PATH)) {
       
    76 				return;
       
    77 			}
       
    78 			
       
    79 			//EXCLUDE: Directory Exclusions List
       
    80 			if ($GLOBALS['duplicator_bypass-array'] != null) {
       
    81 				foreach ($GLOBALS['duplicator_bypass-array'] as $val) {
       
    82 					if (duplicator_safe_path($val) == $folderPath) {
       
    83 						duplicator_log("path filter found: {$val}", 2);
       
    84 						return;
       
    85 					}
       
    86 				}
       
    87 			}
       
    88 			
       
    89 			while (false !== ($file = @readdir($dh))) { 
       
    90 				if ($file != '.' && $file != '..') { 
       
    91 					$fullpath = "{$folderPath}/{$file}";
       
    92 					if(is_dir($fullpath)) {
       
    93 						duplicator_fcgi_flush();
       
    94 						$this->resursiveZip($fullpath);
       
    95 					}
       
    96 					else if(is_file($fullpath) && is_readable($fullpath)) {
       
    97 						//Check filter extensions
       
    98 						if(!in_array(@pathinfo($fullpath, PATHINFO_EXTENSION), $this->skipNames)) {
       
    99 							$localpath = str_replace($this->rootFolder, '', $folderPath);
       
   100 							$localname = empty($localpath) ? '' : ltrim("{$localpath}/", '/');
       
   101 							$this->zipArchive->addFile("{$folderPath}/{$file}", "{$localname}{$file}");
       
   102 						}
       
   103 					} 
       
   104 					$this->limitItems++;
       
   105 				}
       
   106 			} 
       
   107 			
       
   108 			//Check if were over our count
       
   109 			if($this->limitItems > $this->limit) {
       
   110 				duplicator_log("log:class.zip=>new open handle {$this->zipArchive->numFiles}");
       
   111 				$this->zipArchive->close();
       
   112 				$this->zipArchive->open($this->zipFilePath, ZIPARCHIVE::CREATE);
       
   113 				$this->limitItems = 0;
       
   114 				duplicator_fcgi_flush();
       
   115 			}
       
   116 			
       
   117 			closedir($dh);
       
   118 		} 
       
   119 		catch(Exception $e) 
       
   120 		{
       
   121 			duplicator_log("log:class.zip=>runtime error: " . $e);
       
   122 		}		
       
   123 	}
       
   124 }
       
   125 ?>