web/wp-content/plugins/duplicator/inc/functions.php
changeset 195 c7c0fbc09788
child 204 09a1c134465b
equal deleted inserted replaced
194:32102edaa81b 195:c7c0fbc09788
       
     1 <?php
       
     2 
       
     3 /**
       
     4  *  DUPLICATOR_CREATE_DBSCRIPT
       
     5  *  Create the SQL DataDump File
       
     6  *  @param string $destination		The full path and filname where the sql script will be written
       
     7  */
       
     8 function duplicator_create_dbscript($destination) {
       
     9 	try {
       
    10 
       
    11 		global $wpdb;
       
    12 		$dbiconv = ($GLOBALS['duplicator_opts']['dbiconv'] == "0" && function_exists("iconv")) ? false : true;
       
    13 		$handle  = fopen($destination,'w+');
       
    14 		$tables  = $wpdb->get_col('SHOW TABLES');
       
    15 		
       
    16 		duplicator_log("log:fun__create_dbscript=>started");
       
    17 		if ($dbiconv) {
       
    18 			duplicator_log("log:fun__create_dbscript=>dbiconv enabled");
       
    19 		}
       
    20 		
       
    21 		foreach ($tables as $table) {
       
    22 		
       
    23 			//Generate Drop Statement
       
    24 			//$sql_del = ($GLOBALS['duplicator_opts']['dbadd_drop']) ? "DROP TABLE IF EXISTS {$table};\n\n" : "";
       
    25 			//@fwrite($handle, $sql_del);
       
    26 			
       
    27 			//Generate Create Statement
       
    28 			$row_count  = $wpdb->get_var("SELECT Count(*) FROM `{$table}`");
       
    29 			duplicator_log("start: {$table} ({$row_count})");	
       
    30 		
       
    31 			$create  = $wpdb->get_row("SHOW CREATE TABLE `{$table}`", ARRAY_N);
       
    32 			$sql_crt = "{$create[1]};\n\n";
       
    33 			@fwrite($handle, $sql_crt);
       
    34 			
       
    35 			if ($row_count > 100) {
       
    36 				$row_count = ceil($row_count / 100);
       
    37 			} else if ($row_count > 0) {
       
    38 				$row_count = 1;  
       
    39 			}
       
    40 			
       
    41 			//PERFORM ICONV ROUTINE
       
    42 			//Chunck the query results to avoid memory issues
       
    43 			if ($dbiconv) {
       
    44 			
       
    45 				for ($i = 0; $i < $row_count; $i++) {
       
    46 					$sql   = "";
       
    47 					$limit = $i * 100;
       
    48 					$query = "SELECT * FROM `{$table}` LIMIT {$limit}, 100";
       
    49 					$rows  = $wpdb->get_results($query, ARRAY_A);
       
    50 					if (is_array($rows)) {
       
    51 						foreach ($rows as $row) {
       
    52 							$sql .= "INSERT INTO `{$table}` VALUES(";
       
    53 							$num_values  = count($row);
       
    54 							$num_counter = 1;
       
    55 							foreach ($row as $value) {
       
    56 								$value = @iconv(DUPLICATOR_DB_ICONV_IN, DUPLICATOR_DB_ICONV_OUT, $value);
       
    57 								($num_values == $num_counter) 
       
    58 									? $sql .= '"' . @mysql_real_escape_string($value) . '"'
       
    59 									: $sql .= '"' . @mysql_real_escape_string($value) . '", ';
       
    60 								$num_counter++;
       
    61 							}
       
    62 							$sql .= ");\n";
       
    63 						}
       
    64 						@fwrite($handle, $sql);
       
    65 						duplicator_fcgi_flush();
       
    66 					}
       
    67 				}
       
    68 				
       
    69 			//DO NOT PERFORM ICONV
       
    70 			} else {
       
    71 
       
    72 				for ($i = 0; $i < $row_count; $i++) {
       
    73 					$sql   = "";
       
    74 					$limit = $i * 100;
       
    75 					$query = "SELECT * FROM `{$table}` LIMIT {$limit}, 100";
       
    76 					$rows  = $wpdb->get_results($query, ARRAY_A);
       
    77 					if (is_array($rows)) {
       
    78 						foreach ($rows as $row) {
       
    79 							$sql .= "INSERT INTO `{$table}` VALUES(";
       
    80 							$num_values  = count($row);
       
    81 							$num_counter = 1;
       
    82 							foreach ($row as $value) {
       
    83 								($num_values == $num_counter) 
       
    84 									? $sql .= '"' . @mysql_real_escape_string($value) . '"'
       
    85 									: $sql .= '"' . @mysql_real_escape_string($value) . '", ';
       
    86 								$num_counter++;
       
    87 							}
       
    88 							$sql .= ");\n";
       
    89 						}
       
    90 						@fwrite($handle, $sql);
       
    91 						duplicator_fcgi_flush();
       
    92 					}
       
    93 				}
       
    94 			
       
    95 			}
       
    96 			
       
    97 			@fwrite($handle, "\n\n");
       
    98 			duplicator_log("done:  {$table}");
       
    99 		}		
       
   100 	
       
   101 		duplicator_log("log:fun__create_dbscript=>sql file written to {$destination}");
       
   102 		fclose($handle);
       
   103 		$wpdb->flush();
       
   104 		duplicator_log("log:fun__create_dbscript=>ended");
       
   105 	
       
   106 	} catch(Exception $e) {
       
   107 		duplicator_log("log:fun__create_dbscript=>runtime error: " . $e);
       
   108 	}
       
   109 }
       
   110 
       
   111 /**
       
   112  *  DUPLICATOR_CREATE_INSTALLERFILE
       
   113  *  Prep the Installer file for use. use %string% token for replacing 
       
   114  *  @param string $uniquename	The unique name this installer file will be associated with
       
   115  */
       
   116 function duplicator_create_installerFile($uniquename) {
       
   117 
       
   118 	duplicator_log("log:fun__create_installerFile=>started");
       
   119 	
       
   120 	global $wpdb;
       
   121 	$template		 = duplicator_safe_path(DUPLICATOR_PLUGIN_PATH  . 'files/installer.template.php');
       
   122 	$installerRescue = duplicator_safe_path(DUPLICATOR_PLUGIN_PATH  . 'files/installer.rescue.php');
       
   123 	$installerCore	 = duplicator_safe_path(DUPLICATOR_SSDIR_PATH)  . "/{$uniquename}_installer.php";
       
   124 
       
   125 	$err_msg    = "\n!!!WARNING!!! unable to read/write installer\nSee file:{$installerCore} \nPlease check permission and owner on file and parent folder."; 
       
   126 	
       
   127 	get_option('duplicator_options') == ""  ? "" : $duplicator_opts = unserialize(get_option('duplicator_options'));
       
   128 	$replace_items = Array(
       
   129 		"fwrite_current_url" 		=> get_option('siteurl'),
       
   130 		"fwrite_package_name"  	 	=> "{$uniquename}_package.zip",
       
   131 		"fwrite_secure_name"	 	=> "{$uniquename}",
       
   132 		"fwrite_nurl" 				=> $duplicator_opts['nurl'],
       
   133 		"fwrite_dbhost" 			=> $duplicator_opts['dbhost'],
       
   134 		"fwrite_dbname" 			=> $duplicator_opts['dbname'],
       
   135 		"fwrite_dbuser" 			=> $duplicator_opts['dbuser'],
       
   136 		"fwrite_wp_tableprefix" 	=> $wpdb->prefix,
       
   137 		"fwrite_site_title"			=> get_option('blogname'),
       
   138 		"fwrite_rescue_flag"		=> "");
       
   139 		
       
   140 	if( file_exists($template) && is_readable($template)) {
       
   141 	
       
   142 		$install_str = duplicator_parse_template($template, $replace_items);
       
   143 		if (empty($install_str)) {
       
   144 			die(duplicator_log("log:fun__create_installerFile=>file-empty-read" . $err_msg));
       
   145 		}
       
   146 		
       
   147 		//RESCUE FILE
       
   148 		$replace_items["fwrite_rescue_flag"] = '(rescue file)';
       
   149 		$rescue_str = duplicator_parse_template($template, $replace_items);
       
   150 		$fp  = fopen($installerRescue, (!file_exists($installerRescue)) ? 'x+' : 'w');	
       
   151 		@fwrite($fp, $rescue_str, strlen($rescue_str));
       
   152 		@fclose($fp);
       
   153 		$rescue_str = null;
       
   154 		
       
   155 		//INSTALLER FILE
       
   156 		if (!file_exists($installerCore)) {
       
   157 			$fp2 = fopen($installerCore, 'x+') or die(duplicator_log("log:fun__create_installerFile=>file-open-error-x" . $err_msg));
       
   158 		} else {
       
   159 			$fp2 = fopen($installerCore, 'w')  or die(duplicator_log("log:fun__create_installerFile=>file-open-error-w" . $err_msg));
       
   160 		}
       
   161 		
       
   162 		if (fwrite($fp2, $install_str, strlen($install_str))) {
       
   163 			duplicator_log("log:fun__create_installerFile=>installer.php updated at: {$installerCore}");
       
   164 		} else {
       
   165 			duplicator_log("log:fun__create_installerFile=>file-create-error" . $err_msg);
       
   166 		}
       
   167 				
       
   168 		@fclose($fp2);
       
   169 	} 
       
   170 	else
       
   171 	{
       
   172 		die(duplicator_log("log:fun__create_installerFile=>Template missing or unreadable: '$template'"));
       
   173 	}
       
   174 	
       
   175 	duplicator_log("log:fun__create_installerFile=>ended");
       
   176 }
       
   177 
       
   178 /**
       
   179  *  DUPLICATOR_PARSE_TEMPLATE
       
   180  *  Tokenize a file based on an array key 
       
   181  *
       
   182  *  @param string $filename		The filename to tokenize
       
   183  *  @param array  $data			The array of key value items to tokenize
       
   184  */
       
   185 function duplicator_parse_template($filename, $data) {
       
   186     $q = file_get_contents($filename);
       
   187     foreach ($data as $key => $value) {
       
   188         $q = str_replace('%'.$key.'%', $value, $q);
       
   189     }
       
   190     return $q;
       
   191 }
       
   192 
       
   193 
       
   194 /**
       
   195  *  DUPLICATOR_BYTESIZE
       
   196  *  Display human readable byte sizes
       
   197  *  @param string $size		The size in bytes
       
   198  */
       
   199 function duplicator_bytesize($size) {
       
   200     try {
       
   201 		$units = array('B', 'KB', 'MB', 'GB', 'TB');
       
   202 		for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
       
   203 		return round($size, 2).$units[$i];
       
   204 	} catch (Exception $e) {
       
   205 		return "n/a";
       
   206 	}
       
   207 }
       
   208 
       
   209 /**
       
   210  *  DUPLICATOR_DIRSIZE
       
   211  *  Get the directory size recursively, but don't calc the snapshot directory, exclusion diretories
       
   212  *  @param string $directory		The directory to calculate
       
   213  */
       
   214 function duplicator_dirInfo($directory) { 
       
   215 	try {
       
   216 		
       
   217 		$size = 0; 
       
   218 		$count = 0;
       
   219 		$folders = 0;
       
   220 		$flag = false;
       
   221 
       
   222 		//EXCLUDE: Snapshot directory
       
   223 		$directory = duplicator_safe_path($directory);
       
   224 		if( strstr($directory, DUPLICATOR_SSDIR_PATH)) {
       
   225 			return;
       
   226 		}
       
   227 		
       
   228 		//EXCLUDE: Directory Exclusions List
       
   229 		if ($GLOBALS['duplicator_bypass-array'] != null) {
       
   230 			foreach ($GLOBALS['duplicator_bypass-array'] as $val) {
       
   231 				if (duplicator_safe_path($val) == $directory) {
       
   232 					return;
       
   233 				}
       
   234 			}
       
   235 		}
       
   236 
       
   237 		if ($handle = @opendir($directory)) { 
       
   238 			while (false !== ($file = @readdir($handle))) { 
       
   239 				if ($file != '.' && $file != '..') { 
       
   240 					$nextpath = $directory . '/' . $file; 
       
   241 					if (is_dir($nextpath)) { 
       
   242 						$folders++;
       
   243 						$result = duplicator_dirInfo($nextpath); 
       
   244 						$size  += $result['size']; 
       
   245 						$count += $result['count']; 
       
   246 						$folders += $result['folders']; 
       
   247 					} 
       
   248 					else if (is_file($nextpath) && is_readable($nextpath)) { 
       
   249 						if(!in_array(@pathinfo($nextpath, PATHINFO_EXTENSION), $GLOBALS['duplicator_skip_ext-array'])) {
       
   250 							$fmod  = @filesize($nextpath);
       
   251 							if ($fmod === false) {
       
   252 								$flag = true;
       
   253 							} else {
       
   254 								$size +=  @filesize($nextpath);
       
   255 							}
       
   256 							$count++;
       
   257 						}
       
   258 					} 
       
   259 				} 
       
   260 			} 
       
   261 		} 
       
   262 		closedir($handle); 
       
   263 		$total['size']    = $size; 
       
   264 		$total['count']   = $count; 
       
   265 		$total['folders'] = $folders; 
       
   266 		$total['flag']    = $flag; 
       
   267 		return $total; 
       
   268 		
       
   269 	}  catch(Exception $e) {
       
   270 		duplicator_log("log:fun__dirInfo=>runtime error: " . $e . "\nNOTE: Try excluding the stat failed to the Duplicators directory exclusion list or change the permissions.");
       
   271 	}
       
   272 } 
       
   273 
       
   274 /**
       
   275  *  DUPLICATOR_CREATE_SNAPSHOTPATH
       
   276  *  Creates the snapshot directory if it doesn't already exisit
       
   277  */
       
   278 function duplicator_init_snapshotpath() {
       
   279 
       
   280 	$path_wproot = duplicator_safe_path(DUPLICATOR_WPROOTPATH);
       
   281 	$path_ssdir  = duplicator_safe_path(DUPLICATOR_SSDIR_PATH);
       
   282 	$path_plugin = duplicator_safe_path(DUPLICATOR_PLUGIN_PATH);
       
   283 	
       
   284 	//--------------------------------
       
   285 	//CHMOD DIRECTORY ACCESS
       
   286 	//wordpress root directory
       
   287 	@chmod($path_wproot , 0755);
       
   288 
       
   289 	//snapshot directory
       
   290 	@mkdir($path_ssdir, 0755);
       
   291 	@chmod($path_ssdir, 0755);
       
   292 	
       
   293 	//plugins dir/files
       
   294 	@chmod($path_plugin . 'files', 0755);
       
   295 	@chmod(duplicator_safe_path($path_plugin . 'files/installer.rescue.php'), 0644);
       
   296 	
       
   297 	//--------------------------------
       
   298 	//FILE CREATION	
       
   299 	//SSDIR: Create Index File
       
   300 	$ssfile = @fopen($path_ssdir .'/index.php', 'w');
       
   301 	@fwrite($ssfile, '<?php error_reporting(0);  if (stristr(php_sapi_name(), "fcgi")) { $url  =  "http://" . $_SERVER["HTTP_HOST"]; header("Location: {$url}/404.html");} else { header("HTML/1.1 404 Not Found", true, 404);} exit(); ?>');
       
   302 	@fclose($ssfile);
       
   303 	
       
   304 	//SSDIR: Create token file in snapshot
       
   305 	$tokenfile = @fopen($path_ssdir .'/dtoken.php', 'w');
       
   306 	@fwrite($tokenfile, '<?php error_reporting(0);  if (stristr(php_sapi_name(), "fcgi")) { $url  =  "http://" . $_SERVER["HTTP_HOST"]; header("Location: {$url}/404.html");} else { header("HTML/1.1 404 Not Found", true, 404);} exit(); ?>');
       
   307 	@fclose($tokenfile);
       
   308 	
       
   309 	//SSDIR: Create .htaccess
       
   310 	$htfile = @fopen($path_ssdir .'/.htaccess', 'w');
       
   311 	@fwrite($htfile, "Options -Indexes");
       
   312 	@fclose($htfile);
       
   313 	
       
   314 	//SSDIR: Robots.txt file
       
   315 	$robotfile = @fopen($path_ssdir .'/robots.txt', 'w');
       
   316 	@fwrite($robotfile, "User-agent: * \nDisallow: /" . DUPLICATOR_SSDIR_NAME . '/');
       
   317 	@fclose($robotfile);
       
   318 	
       
   319 	//PLUG DIR: Create token file in plugin
       
   320 	$tokenfile2 = @fopen($path_plugin .'files/dtoken.php', 'w');
       
   321 	@fwrite($tokenfile2, '<?php @error_reporting(0); @require_once("../../../../wp-admin/admin.php"); global $wp_query; $wp_query->set_404(); header("HTML/1.1 404 Not Found", true, 404); header("Status: 404 Not Found"); @include(get_template_directory () . "/404.php"); ?>');
       
   322 	@fclose($tokenfile2);
       
   323 }
       
   324 
       
   325 
       
   326 /**
       
   327  *  DUPLICATOR_SAFE_PATH
       
   328  *  Makes path safe for any OS
       
   329  *  Paths should ALWAYS READ be "/"
       
   330  *		uni: /home/path/file.xt
       
   331  *		win:  D:/home/path/file.txt 
       
   332  *  @param string $path		The path to make safe
       
   333  */
       
   334 function duplicator_safe_path($path) {
       
   335 	return str_replace("\\", "/", $path);
       
   336 }
       
   337 
       
   338 
       
   339 /**
       
   340  *  DUPLICATOR_FCGI_FLUSH
       
   341  *  PHP_SAPI for fcgi requires a data flush of at least 256
       
   342  *  bytes every 40 seconds or else it forces a script hault
       
   343  */
       
   344 function duplicator_fcgi_flush() {
       
   345 	echo(str_repeat(' ',256));
       
   346 	@flush();
       
   347 }
       
   348 
       
   349 /**
       
   350  *  DUPLICATOR_SNAPSHOT_URLPATH
       
   351  *	returns the snapshot url
       
   352  */
       
   353 function duplicator_snapshot_urlpath() {
       
   354 	return get_site_url(null, '', is_ssl() ? 'https' : 'http') . '/' . DUPLICATOR_SSDIR_NAME . '/' ;
       
   355 }
       
   356 
       
   357 /**
       
   358  *  DUPLICATOR_LOG
       
   359  *  Centralized logging method
       
   360  *  @param string $msg		The message to log
       
   361  */
       
   362 function duplicator_log($msg, $level = 0) {
       
   363 	$stamp = date('h:i:s');
       
   364 	@fwrite($GLOBALS['duplicator_package_log_handle'], "{$stamp} {$msg} \n");
       
   365 }
       
   366 
       
   367 ?>