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